Package org.xmlBlaster.contrib.dbwriter.info

Examples of org.xmlBlaster.contrib.dbwriter.info.SqlRow


            SqlDescription desc = obj.getDescription();

            conn.setAutoCommit(false);
            try {
               for (int i=0; i < rows.size(); i++) {
                  SqlRow row = (SqlRow)rows.get(i);
                  ClientProperty oldRowProp = new ClientProperty(ReplicationConstants.OLD_CONTENT_ATTR, null, null, row.toXml("", false));
                  row.setAttribute(oldRowProp);
                  try {
                     I_Parser parser = new SqlInfoParser();
                     parser.init(this.info);
                     int ret = desc.update(conn, row, parser);
                     if (ret != 1)
                        throw new Exception("the number of updated entries is wrong '" + ret + "' but should be 1");
                  }
                  catch(Exception ex) {
                     log.info("exception when updating '" + row.toXml("") + " where description is '" + desc.toXml("") + "'");
                     throw ex;
                  }
                  try {
                     int ret = desc.delete(conn, row);
                     if (ret != 1)
                        throw new Exception("the number of deleted entries is wrong '" + ret + "' but should be 1");
                  }
                  catch(Exception ex) {
                     log.info("exception when deleting '" + row.toXml("") + " where description is '" + desc.toXml("") + "'");
                     throw ex;
                  }
                  try {
                     int ret = desc.insert(conn, row);
                     if (ret != 1)
                        throw new Exception("the number of inserted entries is wrong '" + ret + "' but should be 1");
                  }
                  catch(Exception ex) {
                     log.info("exception when inserting '" + row.toXml("") + " where description is '" + desc.toXml("") + "'");
                     throw ex;
                  }
               }
               Thread.sleep(this.sleepDelay);
               conn.commit();
View Full Code Here


            String tmp = attrs.getValue(SqlRow.NUM_ATTR);
            if (tmp != null) {
               number = Integer.parseInt(tmp.trim());
            }
         }
         this.recordRow = new SqlRow(this.info, number);
         this.updateRecord.getRows().add(this.recordRow);
              
         return;
      }
View Full Code Here

         st = conn.createStatement();
         String sql = "select * from " + completeTable + " WHERE rowid=CHARTOROWID('" + guid + "')";
         ResultSet rs = st.executeQuery(sql);
         if (rs.next()) {
            obj.fillOneRowWithObjects(rs, transformer);
            SqlRow row = (SqlRow)obj.getRows().get(0);
            rs.close();
            return row.toXml("", false);
         }
         else {
            log.severe("The entry guid='" + guid + "' for table '" + completeTable + "' was not found (anymore)");
            return "";
         }
View Full Code Here

      ClientProperty prop = dbInfo.getDescription().getAttribute(ReplicationConstants.ALREADY_PROCESSED_ATTR);
      if (prop != null)
         return true;
      List rows = dbInfo.getRows();
      for (int i=0; i < rows.size(); i++) {
         SqlRow row = (SqlRow)rows.get(i);
         prop = row.getAttribute(ReplicationConstants.ALREADY_PROCESSED_ATTR);
         if (prop != null)
            return true;
      }
      return false;
   }
View Full Code Here

               oldAutoCommitKnown = true;
               conn.setAutoCommit(false); // everything will be handled within the same transaction

               if (command.equalsIgnoreCase(REPLICATION_CMD)) {
                  for (int i=0; i < rows.size(); i++) {
                     SqlRow row = ((SqlRow)rows.get(i)).cloneRow();

                     // TODO consistency check
                     action = getStringAttribute(ACTION_ATTR, row, description);

                     originalCatalog = getStringAttribute(CATALOG_ATTR, row, description);
                     originalSchema = getStringAttribute(SCHEMA_ATTR, row, description);
                     originalTable = getStringAttribute(TABLE_NAME_ATTR, row, description);
                     // row specific but still without considering colums
                     catalog = this.mapper.getMappedCatalog(originalCatalog, originalSchema, originalTable, null, originalCatalog);
                     schema = this.mapper.getMappedSchema(originalCatalog, originalSchema, originalTable, null, originalSchema);
                     table = this.mapper.getMappedTable(originalCatalog, originalSchema, originalTable, null, originalTable);

                     if (action == null)
                        throw new Exception(ME + ".store: row with no action invoked '" + row.toXml("", true, false, true));
                     int count = modifyColumnsIfNecessary(originalCatalog, originalSchema, originalTable, row);
                     if (count != 0) {
                        if (log.isLoggable(Level.FINE)) log.fine("modified '" + count  + "' entries");
                     }
                     if (log.isLoggable(Level.FINE)) log.fine("store: " + row.toXml("", true, true, true));
                     SqlDescription desc = getTableDescription(catalog, schema, table, conn);
                     boolean process = true;
                     if (this.prePostStatement != null) // row is the modified column name
                        process = this.prePostStatement.preStatement(action, conn, dbInfo, desc, row);
                     if (process) {
                        if (action.equalsIgnoreCase(INSERT_ACTION)) {
                           desc.insert(conn, row);
                        }
                        else if (action.equalsIgnoreCase(UPDATE_ACTION)) {
                           desc.update(conn, row, this.parserForOldInUpdates);
                        }
                        else if (action.equalsIgnoreCase(DELETE_ACTION)) {
                           desc.delete(conn, row);
                        }
                        else { // TODO implement this possibility too
                           if (action.equalsIgnoreCase(CREATE_ACTION) ||
                               action.equalsIgnoreCase(ALTER_ACTION) ||
                               action.equalsIgnoreCase(DROP_ACTION)) {
                              throw new Exception("The execution of action='" + action + "' inside a multi-operation transaction is not implemented");
                           }
                           else // we don't throw an exception here to be backwards compatible. In future we can throw one
                              log.severe("The action='" + action + "' is not recognized as an SQL operation and will therefore not be executed");
                        }
                        if (this.prePostStatement != null)
                           this.prePostStatement.postStatement(action, conn, dbInfo, desc, row);
                     }
                  }
               }
               else { // then it is a CREATE / DROP / ALTER or DUMP command (does not have any rows associated)
                  if (action.equalsIgnoreCase(CREATE_ACTION)) {
                     if (this.doCreate) {
                        // check if the table already exists ...
                        ResultSet rs = conn.getMetaData().getTables(catalog, schema, table, null);
                        boolean tableExistsAlready = rs.next();
                        rs.close();

                        boolean invokeCreate = true;
                        completeTableName = table;
                        if (schema != null && schema.length() > 1)
                           completeTableName = schema + "." + table;

                        boolean process = true;
                        SqlDescription desc = getTableDescription(catalog, schema, table, conn);
                        if (this.prePostStatement != null) {
                           final SqlRow currentRow = null;
                           process = this.prePostStatement.preStatement(action, conn, dbInfo, desc, currentRow);
                        }
                       
                        if (process) {
                           if (tableExistsAlready) {
                              if (!this.overwriteTables) {
                                 throw new Exception("ReplicationStorer.store: the table '" + completeTableName + "' exists already and 'replication.overwriteTables' is set to 'false'");
                              }
                              else {
                                 if (this.recreateTables) {
                                    log.warning("store: the table '" + completeTableName + "' exists already. 'replication.overwriteTables' is set to 'true': will drop the table and recreate it");
                                    Statement st = conn.createStatement();
                                    st.executeUpdate("DROP TABLE " + completeTableName);
                                    st.close();
                                 }
                                 else {
                                    log.warning("store: the table '" + completeTableName + "' exists already. 'replication.overwriteTables' is set to 'true' and 'replication.recreateTables' is set to false. Will only delete contents of table but keep the old structure");
                                    invokeCreate = false;
                                 }
                              }
                           }
                           String sql = null;
                           if (invokeCreate) {
                              sql = this.dbSpecific.getCreateTableStatement(description, this.mapper);
                              log.info("CREATE STATEMENT: '" + sql + "'");
                           }
                           else {
                              sql = "DELETE FROM " + completeTableName;
                              log.info("CLEANING UP TABLE '" + completeTableName + "'");
                           }
                           Statement st = conn.createStatement();
                           try {
                              st.executeUpdate(sql);
                           }
                           finally {
                              st.close();
                           }
                           if (this.prePostStatement != null) {
                              final SqlRow currentRow = null;
                              this.prePostStatement.postStatement(action, conn, dbInfo, desc, currentRow);
                           }
                        }
                     }
                     else
                        log.fine("CREATE is disabled for this writer");
                  }
                  else if (action.equalsIgnoreCase(DROP_ACTION)) {
                     if (this.doDrop) {
                        boolean process = true;
                        SqlDescription desc = getTableDescription(catalog, schema, table, conn);
                        if (this.prePostStatement != null) {
                           final SqlRow currentRow = null;
                           process = this.prePostStatement.preStatement(action, conn, dbInfo, desc, currentRow);
                        }
                        if (process) {
                           completeTableName = table;
                           if (schema != null && schema.length() > 1)
                              completeTableName = schema + "." + table;
                           String sql = "DROP TABLE " + completeTableName;
                           Statement st = conn.createStatement();
                           try {
                              st.executeUpdate(sql);
                           }
                           catch (SQLException e) {
                              // this is currently only working on oracle: TODO make it work for other DB too.
                              if (e.getMessage().indexOf("does not exist") > -1)
                                 log.warning("table '" + completeTableName + "' was not found and could therefore not be dropped. Continuing anyway");
                              else
                                 throw e;
                           }
                           finally {
                              st.close();
                           }
                           if (this.prePostStatement != null) {
                              final SqlRow currentRow = null;
                              this.prePostStatement.postStatement(action, conn, dbInfo, desc, currentRow);
                           }
                        }
                     }
                     else
                        log.fine("DROP is disabled for this writer");
                  }
                  else if (action.equalsIgnoreCase(ALTER_ACTION)) {
                     if (this.doAlter) {
                        boolean process = true;
                        SqlDescription desc = getTableDescription(catalog, schema, table, conn);
                        if (this.prePostStatement != null) {
                           final SqlRow currentRow = null;
                           process = this.prePostStatement.preStatement(action, conn, dbInfo, desc, currentRow);
                        }
                        if (process) {
                           log.severe("store: operation '" + action + "' invoked but not implemented yet '" + description.toXml(""));
                           if (this.prePostStatement != null) {
                              final SqlRow currentRow = null;
                              this.prePostStatement.postStatement(action, conn, dbInfo, desc, currentRow);
                           }
                        }
                     }
                     else
View Full Code Here

            description.setCommand(action);
            description.addAttributes(completeAttrs);
            dbSpecific.addTrigger(conn, catalog, schema, tableName);
         }
         else if (action.equalsIgnoreCase(INSERT_ACTION)) {
            SqlRow row = this.sqlInfo.fillOneRow(rs, newContent, this.transformer);
            row.addAttributes(completeAttrs);
         }
         else if (action.equalsIgnoreCase(UPDATE_ACTION)) {
            boolean doSend = true;
            if (!this.sendUnchangedUpdates && oldContent.equals(newContent))
               doSend = false;
            if (doSend) {
               completeAttrs.put(OLD_CONTENT_ATTR, oldContent);
               SqlRow row = this.sqlInfo.fillOneRow(rs, newContent, this.transformer);
               row.addAttributes(completeAttrs);
            }
            else
               log.fine("an update with unchanged content was detected on table '" + tableName + "' and transId='" + this.transactionId + "': will not send it");
         }
         else if (action.equalsIgnoreCase(DELETE_ACTION)) {
            SqlRow row = this.sqlInfo.fillOneRow(rs, oldContent, this.transformer);
            row.addAttributes(completeAttrs);
         }
         else if (action.equalsIgnoreCase(STATEMENT_ACTION)) {
            String sql = ReplaceVariable.extractWithMatchingAttrs(newContent, "attr", " id='" + STATEMENT_ATTR + "'");
            String statementPrio = ReplaceVariable.extractWithMatchingAttrs(newContent, "attr", " id='" + STATEMENT_PRIO_ATTR + "'");
            String maxEntries = ReplaceVariable.extractWithMatchingAttrs(newContent, "attr", " id='" + MAX_ENTRIES_ATTR + "'");
View Full Code Here

    * If the table does not exist we expect a null ResultSet
    * @throws Exception Any type is possible
    */
   public final void testSqlRowClone() throws Exception {

      SqlRow sqlRow = new SqlRow(this.info, 0);
     
      sqlRow.setAttribute("attr1", "value1");
      sqlRow.setAttribute("attr2", "value2");
      sqlRow.setAttribute("attr3", "value3");
      sqlRow.setCaseSensitive(true);
      ClientProperty col1 = new ClientProperty("dummy1", null, null);
      col1.setValue("valDummy1");
      sqlRow.setColumn(col1);
      ClientProperty col2 = new ClientProperty("dummy2", null, null);
      col2.setValue("valDummy2");
      sqlRow.setColumn(col2);
     
      assertEquals("the caseSensitive has been modified", true, sqlRow.isCaseSensitive());
      assertEquals("The value of the attribute has been modified", "value1", sqlRow.getAttribute("attr1").getStringValue());
      assertEquals("The value of the attribute has been modified", "value2", sqlRow.getAttribute("attr2").getStringValue());
      assertEquals("The value of the attribute has been modified", "value3", sqlRow.getAttribute("attr3").getStringValue());
      assertEquals("The value of the row has been modified", "valDummy1", sqlRow.getColumn("dummy1").getStringValue());
      assertEquals("The value of the row has been modified", "valDummy2", sqlRow.getColumn("dummy2").getStringValue());
      assertEquals("The number of attributes has been modified", 3, sqlRow.getAttributeNames().length);
      assertEquals("The value of columns has been modified", 2, sqlRow.getColumnNames().length);
     
      SqlRow row2 = sqlRow.cloneRow();

      assertEquals("the caseSensitive has been modified", true, row2.isCaseSensitive());
      assertEquals("The value of the attribute has been modified", "value1", row2.getAttribute("attr1").getStringValue());
      assertEquals("The value of the attribute has been modified", "value2", row2.getAttribute("attr2").getStringValue());
      assertEquals("The value of the attribute has been modified", "value3", row2.getAttribute("attr3").getStringValue());
      assertEquals("The value of the row has been modified", "valDummy1", row2.getColumn("dummy1").getStringValue());
      assertEquals("The value of the row has been modified", "valDummy2", row2.getColumn("dummy2").getStringValue());
      assertEquals("The number of attributes has been modified", 3, row2.getAttributeNames().length);
      assertEquals("The value of columns has been modified", 2, row2.getColumnNames().length);
     
      row2.setCaseSensitive(false);
      row2.setAttribute("attr1", "newValue1");
      row2.setAttribute("attr4", "newValue4");
      row2.renameColumn("dummy1", "newDummy1");
     
      assertEquals("the caseSensitive has been modified", true, sqlRow.isCaseSensitive());
      assertEquals("The value of the attribute has been modified", "value1", sqlRow.getAttribute("attr1").getStringValue());
      assertEquals("The value of the row has been modified", "valDummy1", sqlRow.getColumn("dummy1").getStringValue());
     
      log.info(row2.toXml(""));
      assertEquals("the caseSensitive has been modified", false, row2.isCaseSensitive());
      assertEquals("The value of the attribute has been modified", "newValue1", row2.getAttribute("attr1").getStringValue());
      Object obj = row2.getColumn("dummy1");
      assertNull("The value of the row has been modified", obj);
      assertEquals("The value of the row has been modified", "valDummy1", row2.getColumn("newDummy1").getStringValue());
     
      ClientProperty tmpCol = row2.getColumn("dummy2");
      tmpCol.setValue("newValueDummy2");
      assertEquals("the content of the column has been modified", "valDummy2", sqlRow.getColumn("dummy2").getStringValue());
      assertEquals("the content of the column has been modified", "newValueDummy2", row2.getColumn("dummy2").getStringValue());
     
      ClientProperty tmpAttr = row2.getAttribute("attr3");
      tmpAttr.setValue("newValueAttr3");
      assertEquals("the content of the attribute has been modified", "value3", sqlRow.getAttribute("attr3").getStringValue());
      assertEquals("the content of the attribute has been modified", "newValueAttr3", row2.getAttribute("attr3").getStringValue());
     
     
      log.info("SUCCESS");
   }
View Full Code Here

     
      List rows = record.getRows();
      assertEquals("the number of rows is wrong", 3, rows.size());
      int[] attr = new int[] { 2, 0, 1 };
      for (int i=0; i < 3; i++) {
         SqlRow row = (SqlRow)rows.get(i);
         assertEquals("wrong number of columns for row '" + i+ "'", 4, row.getColumnNames().length);
         assertEquals("wrong number of attributes for row '" + i+ "'", attr[i], row.getAttributeNames().length);
      }
     
      System.out.println("\n\nshould be:\n" + xml);
      System.out.println("\nis:\n" + record.toXml(""));
      assertXMLEqual("output xml is not the same as input xml", xml, record.toXml(""));
View Full Code Here

     
      List rows = record.getRows();
      assertEquals("the number of rows is wrong", 3, rows.size());
      int[] attr = new int[] { 2, 0, 1 };
      for (int i=0; i < 3; i++) {
         SqlRow row = (SqlRow)rows.get(i);
         assertEquals("wrong number of columns for row '" + i+ "'", 4, row.getColumnNames().length);
         assertEquals("wrong number of attributes for row '" + i+ "'", attr[i], row.getAttributeNames().length);
      }
     
      System.out.println("\n\nshould be:\n" + xml);
      System.out.println("\nis:\n" + record.toXml(""));
      assertXMLEqual("output xml is not the same as input xml", xml, record.toXml(""));
View Full Code Here

            SqlInfoParser  parser = new SqlInfoParser();
            parser.init(info);
            SqlInfo sqlInfo = parser.parse(xmlTxt);
            List rows = sqlInfo.getRows();
            assertEquals("The number of rows is wrong", 1, rows.size());
            SqlRow row = (SqlRow)rows.get(0);
            log.info(row.toXml(""));
            row.renameColumn(oldName, newName);
            log.info(row.toXml(""));
         }
         catch (Exception ex) {
            ex.printStackTrace();
            assertTrue("An Exception should not occur when testing renaming of columns " + ex.getMessage(), false);
         }
View Full Code Here

TOP

Related Classes of org.xmlBlaster.contrib.dbwriter.info.SqlRow

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.