JavaDoc


001    /**
002     * Copyright (c) 2003 Daffodil Software Ltd all rights reserved,
003     * Modifications Copyright (c) 2008 Regiscope Digital Imaging Co, LLC, All rights reserved.
004     * This program is free software; you can redistribute it and/or modify
005     * it under the terms of version 2 of the GNU General Public License as
006     * published by the Free Software Foundation.
007     * There are special exceptions to the terms and conditions of the GPL
008     * as it is applied to this software. See the GNU General Public License for more details.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018     */
019    
020    package org.dbreplicator.replication;
021    
022    import java.sql.*;
023    import java.util.*;
024    
025    import org.dbreplicator.replication.DBHandler.*;
026    import org.apache.log4j.Logger;
027    import org.dbreplicator.graph.DirectedGraph;
028    
029    /**
030     * This Class gets the connection object of the specified publisher or subscriber
031     * and then stores it's metadata information in the DataBaseMetaData object(dbmd).
032     * This inforamtion is used for performing different operations on the tables of
033     * the publication or subscription and on the columns and constraint.
034     */
035    
036    public class CommonMetaDataInfo extends MetaDataInfo {
037    
038      protected static Logger log = Logger.getLogger(CommonMetaDataInfo.class.getName());
039    
040      public CommonMetaDataInfo(ConnectionPool connectionPool0, String pubsubName) throws
041          RepException {
042        Connection con = connectionPool0.getConnection(pubsubName);
043        try {
044            dbmd = con.getMetaData();
045          }
046          catch (SQLException ex) {
047            throw new RepException("REP101", new Object[] {ex.getMessage()});
048          }
049          finally {
050            connectionPool0.returnConnection(con);
051          }
052      }
053    
054      public CommonMetaDataInfo(Connection connection0) throws RepException {
055        try {
056          dbmd = connection0.getMetaData();
057        }
058        catch (SQLException ex) {
059          throw new RepException("REP101", new Object[] {ex.getMessage()});
060        }
061      }
062    
063      public void checkTableExistance(SchemaQualifiedName sname) throws
064          RepException {
065        String schema = sname.getSchemaName();
066        String table = sname.getTableName();
067        //System.out.println(" schema table : " + schema + table);
068        //ResultSet rs2 = dbmd.getTables(null,"%","%",new String[] {"TABLE"});
069        //showResultSet(rs2);
070        //ResultSet rs3 = dbmd.getTables(null,"%",table.toUpperCase(),new String[] {"TABLE"}); // Oracle
071        //showResultSet(rs3);
072    //      System.out.println(" schmea Name == " + schema + " table Name " + table);
073        //ResultSet rs = dbmd.getTables(null,schema.toUpperCase(),table.toUpperCase(),new String[] {"TABLE"}); // Oracle
074        try {
075          ResultSet rs = null;
076          try {
077            rs = dbmd.getTables(null, schema, table, new String[] {"TABLE"}); // SQL Server Oracle (also works for: PostgreSQL)
078            //rs = dbmd.getTables(null, schema, table, null); // MySQL
079            if (rs == null || !rs.next()) {
080              log.debug("Resultset  found  " + rs + " OR Resultset found false");
081              log.debug("Table = " + table + ", Schema = " + schema);
082              throw new RepException("REP017", new Object[] {sname.toString()});
083            }
084            sname.setSchemaName(this, rs.getString("TABLE_SCHEM"));
085            if (rs.next()) {
086              throw new RepException("REP018", new Object[] {table});
087            }
088          }
089          finally {
090            if (rs != null)
091              rs.close();
092          }
093    
094        }
095        catch (RepException ex) {
096          log.error(ex.getMessage(), ex);
097          throw ex;
098        }
099        catch (SQLException ex) {
100          log.error(ex.getMessage(), ex);
101          throw new RepException("REP006", new Object[] {ex.getMessage()});
102        }
103      }
104    
105      public String[] getTablesHierarchy(String[] tableNames) throws RepException {
106        ArrayList givenListOfTables = new ArrayList();
107        for (int i = 0; i < tableNames.length; i++) {
108          givenListOfTables.add(tableNames[i].toUpperCase());
109        }
110        ArrayList listToReturn = new ArrayList();
111        ArrayList sublist1 = new ArrayList();
112        for (int i = 0; i < tableNames.length; i++) {
113          SchemaQualifiedName sname = new SchemaQualifiedName(this, tableNames[i]);
114          String schemaName = sname.getSchemaName();
115          String tableName = sname.getTableName();
116    //      System.out.println(" tableName in gTH "+ sname);
117    //       checkParentTablesIncludedInList(givenListOfTables,schemaName,tableName);
118          listToReturn.add(sname.toString());
119    //      System.out.println(" list to return "+ listToReturn);
120          sublist1 = getTableSequenceRegardingForeignKey(schemaName, tableName,
121              listToReturn);
122    //  System.out.println(" sublist1 "+ sublist1);
123          addTablesRecursively(givenListOfTables, listToReturn, sublist1);
124          sublist1.clear();
125        }
126        String[] arr = new String[listToReturn.size()];
127        listToReturn.toArray(arr);
128        log.debug(listToReturn);
129        return arr;
130      }
131    
132      private void addTablesRecursively(ArrayList givenListOfTables,ArrayList listToReturn, ArrayList sublist) throws RepException {
133        for (int j = 0; j < sublist.size(); j++) {
134          String table = (String) sublist.get(j);
135          if (listToReturn.contains(table)) {
136    //System.out.println(" ATR tableName = "+table);
137            SchemaQualifiedName sname = new SchemaQualifiedName(this, table);
138            String schemaName = sname.getSchemaName();
139            String tableName = sname.getTableName();
140            ArrayList subList1 = getTableSequenceRegardingForeignKey(schemaName,
141                tableName, listToReturn);
142    //          if(givenListOfTables.contains(table)) {
143            listToReturn.remove(listToReturn.indexOf(table));
144            listToReturn.add(table);
145    //System.out.println(" list to return in ATR "+ listToReturn);
146    //          }
147            addTablesRecursively(givenListOfTables, listToReturn, subList1);
148    
149            subList1.clear();
150          }
151          else {
152    //            if(givenListOfTables.contains(table)) {
153            listToReturn.add(table);
154    //            }
155          }
156        }
157      }
158    
159      public void checkParentTablesIncludedInList(ArrayList givenListOfTables, String schemaName, String tableName) throws RepException {
160        try {
161          ResultSet rs = dbmd.getImportedKeys(null, schemaName, tableName);
162          try {
163            if (rs != null && rs.next()) {
164              do {
165                String pk_tableName = rs.getString("PKTABLE_SCHEM") + "." +rs.getString("PKTABLE_NAME");
166                log.debug("Primary Tablename " + pk_tableName);
167                if (!givenListOfTables.contains(pk_tableName.toUpperCase())) {
168                  int lastIndexOfDot = pk_tableName.lastIndexOf(".");
169                  if (lastIndexOfDot != -1) {
170                    String pktableName = pk_tableName.substring(lastIndexOfDot + 1);
171                    if (!givenListOfTables.contains(pktableName.toUpperCase())) {
172                      throw new RepException("REP0201",
173                                             new Object[] {pk_tableName.toUpperCase(),
174                                             tableName.toUpperCase()});
175                    }
176                  }
177                  else {
178                    throw new RepException("REP0201",
179                                           new Object[] {pk_tableName.toUpperCase(),
180                                           tableName.toUpperCase()});
181                  }
182                }
183              }
184              while (rs.next());
185            }
186          }
187          finally {
188            rs.close();
189          }
190        }
191        catch (SQLException ex) {
192          log.error(ex.getMessage(), ex);
193          throw new RepException("REP0202", new Object[] {tableName, ex.getMessage()});
194    
195        }
196      }
197    
198      protected ArrayList getTableSequenceRegardingForeignKey(String schemaName,
199          String tableName, ArrayList tableList) throws RepException {
200        ArrayList list = new ArrayList();
201        try {
202          log.debug(" TableName : " + schemaName + "." + tableName);
203          ResultSet rs2 = dbmd.getExportedKeys(null, schemaName, tableName);
204          log.debug(" ExportedKeys Table Name's ResultSet " + rs2);
205          if (rs2 != null && rs2.next()) {
206            do {
207    //          String primaryTable = rs2.getString("PKTABLE_SCHEM") + "." +rs2.getString("PKTABLE_NAME");
208              String foreignTable = rs2.getString("FKTABLE_SCHEM") + "." +rs2.getString("FKTABLE_NAME");
209              if (tableList.size() > 0) {
210                String table_list = (String) tableList.get(0);
211                if (table_list.indexOf(".") == -1) {
212                  int indexInforiegnTable = foreignTable.lastIndexOf(".");
213                  if (indexInforiegnTable != -1) {
214                    foreignTable = foreignTable.substring(indexInforiegnTable + 1);
215                  }
216                }
217              }
218              String qualifiedName = null;
219              if (schemaName != null) {
220                qualifiedName = schemaName + "." + tableName;
221              }
222              else {
223                qualifiedName = tableName;
224              }
225              if (tableList.contains(foreignTable.toUpperCase()) &&
226                  !foreignTable.equalsIgnoreCase(qualifiedName)) {
227                if (!list.contains(foreignTable.toUpperCase())) {
228                  list.add(foreignTable.toUpperCase());
229    
230    //                          tableList.remove(foreignTable.toUpperCase());
231    //      throw new RepException("REP015", new Object[] {primaryTable,foreignTable});
232                }
233    
234              }
235            }
236            while (rs2.next());
237          }
238          /** @todo   rs has been closed  */
239          rs2.close();
240        }
241        catch (SQLException ex) {
242          log.error(ex.getMessage(), ex);
243          throw new RepException("REP006", new Object[] {ex.getMessage()});
244        }
245        log.debug(" Sublist of tables  =" + list);
246        return list;
247      }
248    
249      public void checkTableSequenceAccordingForeignKey(String schemaName,
250          String tableName, ArrayList tableList) throws RepException {
251        log.debug(" TableName : " + schemaName + "." + tableName);
252        try {
253          ResultSet rs2 = dbmd.getExportedKeys(null, schemaName, tableName);
254          log.debug(" ExportedKeys Table Name's ResultSet " + rs2);
255          if (rs2 != null && rs2.next()) {
256            do {
257              String primaryTable = rs2.getString("PKTABLE_SCHEM") + "." +rs2.getString("PKTABLE_NAME");
258              String foreignTable = rs2.getString("FKTABLE_SCHEM") + "." +rs2.getString("FKTABLE_NAME");
259              if (primaryTable.equalsIgnoreCase(foreignTable))
260                continue;
261              if (tableList.size() > 0) {
262                String table_list = (String) tableList.get(0);
263                if (table_list.indexOf(".") == -1) {
264                  int indexInforiegnTable = foreignTable.lastIndexOf(".");
265                  if (indexInforiegnTable != -1) {
266                    foreignTable = foreignTable.substring(indexInforiegnTable + 1);
267                  }
268                }
269              }
270              if (tableList.contains(foreignTable.toUpperCase())) {
271                throw new RepException("REP015", new Object[] {primaryTable,foreignTable});
272              }
273            }
274            while (rs2.next());
275          }
276          /** @todo   rs has been closed  */
277          rs2.close();
278    
279        }
280        catch (RepException ex) {
281          log.error(ex.getMessage(), ex);
282          throw ex;
283        }
284        catch (SQLException ex) {
285          log.error(ex.getMessage(), ex);
286          throw new RepException("REP006", new Object[] {ex.getMessage()});
287        }
288      }
289    
290      public ArrayList getColumnDataTypeInfo(AbstractDataBaseHandler dbh,
291          String schemaName, String tableName) throws RepException, SQLException {
292    
293        ArrayList colInfoList = new ArrayList();
294        ResultSet rs = dbmd.getColumns(null, schemaName, tableName, "%");
295        if (rs == null || !rs.next()) {
296          log.debug("Resultset  found" + rs + " OR Resultset found false");
297          throw new RepException("Internal Error", null);
298        }
299        do {
300    
301          TypeInfo typeInfo = new TypeInfo(dbh.updateDataType(rs.getString("TYPE_NAME")), rs.getInt("DATA_TYPE"));
302          int columnPrecision = rs.getInt("COLUMN_SIZE");
303          String typeName = rs.getString("TYPE_NAME").trim();
304          columnPrecision = dbh.getAppropriatePrecision(columnPrecision, typeName);
305          typeInfo.setOptionalSizeProperty(dbh.isDataTypeOptionalSizeSupported(typeInfo));
306          typeInfo.setColumnSize(rs.getInt("COLUMN_SIZE"));
307          typeInfo.setColumnScale(rs.getInt("DECIMAL_DIGITS"));
308          dbh.setTypeInfo(typeInfo, rs);
309          //colInfoMap.put(rs.getString("COLUMN_NAME").trim(),typeInfo.getTypeDeclaration(rs.getInt("COLUMN_SIZE")));
310          colInfoList.add(new ColumnsInfo(rs.getString("COLUMN_NAME").trim(),typeInfo.getTypeDeclaration(columnPrecision)));
311          log.debug("Column Name " + rs.getString("COLUMN_NAME").trim());
312          log.debug("Type declaration " +typeInfo.getTypeDeclaration(columnPrecision));
313        }
314        while (rs.next());
315        rs.close();
316        return colInfoList;
317      }
318    
319      /*public HashMap getColumnDataTypeInfo(AbstractDataBaseHandler dbh,String schemaName, String tableName) throws RepException,SQLException {
320         HashMap colInfoMap = new HashMap();
321         ResultSet rs = dbmd.getColumns(null,schemaName,tableName,"%");
322         if(rs == null || !rs.next())
323           throw new RepException("Internal Error",null);
324         int i = 1;
325         do {
326           TypeInfo typeInfo = new TypeInfo(
327       dbh.updateDataType(rs.getString("TYPE_NAME")) ,rs.getInt("DATA_TYPE"));
328       typeInfo.setOptionalSizeProperty(dbh.isDataTypeOptionalSizeSupported(typeInfo));
329           //colInfoMap.put(rs.getString("COLUMN_NAME").trim(),typeInfo.getTypeDeclaration(rs.getInt("COLUMN_SIZE")));
330           colInfoMap.put(new Integer(i++), new ColumnsInfo( rs.getString("COLUMN_NAME").trim(),typeInfo.getTypeDeclaration(rs.getInt("COLUMN_SIZE")) ));
331    //        System.out.println(" VALUE " + rs.getString("COLUMN_NAME"));
332         }while(rs.next());
333         return colInfoMap;
334          }*/
335    
336      public HashMap getColumnsInfo(String schemaName, String tableName) throws
337          RepException, SQLException {
338        HashMap colInfoMap = new HashMap();
339        ResultSet rs = dbmd.getColumns(null, schemaName, tableName, "%");
340        if (rs == null || !rs.next()) {
341          log.debug("Resultset  found" + rs + " OR Resultset found false");
342          throw new RepException("Internal Error", null);
343        }
344        int i = 0;
345        do {
346          //colInfoMap.put(rs.getString("COLUMN_NAME").trim(),"");
347          colInfoMap.put(new Integer(i++),new ColumnsInfo(rs.getString("COLUMN_NAME").trim(), null));
348        }
349        while (rs.next());
350        log.debug(" colInfoMap  : " + colInfoMap);
351        if (rs != null)
352          rs.close();
353        return colInfoMap;
354      }
355    
356      /*public String generateQueryForLocalShadowTable(AbstractDataBaseHandler dbh,String schemaName, String tableName,ArrayList ciList, int vendorType) throws SQLException {
357         StringBuffer sb = new StringBuffer();
358         //System.out.println(" dbh " + dbh.getClass());
359         ResultSet rs = dbmd.getColumns(null,schemaName,tableName,"%");
360         if(rs!=null && rs.next()) {
361            do {
362               TypeInfo typeInfo = new TypeInfo(rs.getString("TYPE_NAME"),
363                     rs.getInt("DATA_TYPE"));
364               int size = rs.getInt("COLUMN_SIZE");
365               String columnName = rs.getString("COLUMN_NAME");
366       typeInfo.setOptionalSizeProperty(dbh.isDataTypeOptionalSizeSupported(typeInfo));
367       sb.append(columnName).append(" ").append(typeInfo.getTypeDeclaration(size));
368    
369               switch (vendorType) {
370                  case Utility.DataBase_SqlServer:
371                     ciList.add(new ColumnsInfo(columnName,typeInfo,size));
372                     break;
373                  case Utility.DataBase_DaffodilDB:
374                  case Utility.DataBase_Oracle:
375                     ciList.add(columnName);
376                  default:
377               }
378               sb.append(" , ");
379            }while(rs.next());
380         }
381         //System.out.println(" sb " + sb.toString());
382         return sb.toString();
383          }*/
384    
385      /**
386       * Generating a query ----
387       * [  columnName dataType[(size)] [default] [not null] ,
388       *  [ columnName dataType[(size)] [default] [not null] ,...]]
389       * @param dbh
390       * @param srcVendorType
391       * @param schemaName
392       * @param tableName
393       * @param tgtVendorType
394       * @return String
395       * @throws SQLException
396       * @throws RepException
397       */
398      public String generateColumnsQueryForClientNode(AbstractDataBaseHandler dbh,
399                                                      int srcVendorType,
400                                                      String schemaName,
401                                                      String tableName,
402                                                      int tgtVendorType) throws
403          RepException {
404        StringBuffer sb = new StringBuffer();
405        AbstractDataBaseHandler remotedbh = Utility.getDatabaseHandler(tgtVendorType);
406        AbstractDataBaseHandler optSizedbh = (srcVendorType == tgtVendorType) ?dbh : remotedbh;
407        try {
408          //ResultSet rs2 = dbmd.getColumns(null,schemaName, tableName,"%");
409          //showResultSet(rs2);
410          ResultSet rs = dbmd.getColumns(null, schemaName, tableName, "%");
411          if (rs == null || !rs.next()) {
412            log.debug("Resultset  found" + rs + " OR Resultset found false");
413            throw new RepException("REP033", new Object[] {schemaName + "." + tableName});
414          }
415    
416          do {
417            String typeName = dbh.updateDataType(rs.getString("TYPE_NAME"));
418            int dataType = rs.getInt("DATA_TYPE");
419            TypeInfo typeInfo = new TypeInfo(typeName, dataType);
420            int columnPrecision = rs.getInt("COLUMN_SIZE");
421            int columnScale=rs.getInt("DECIMAL_DIGITS");
422            String columnName = rs.getString("COLUMN_NAME");
423            columnPrecision = optSizedbh.getAppropriatePrecision(columnPrecision,typeName);
424            typeInfo.setColumnSize(columnPrecision);
425            columnScale=optSizedbh.getAppropriateScale(columnScale);
426            typeInfo.setColumnScale(columnScale);
427            optSizedbh.setTypeInfo(typeInfo, rs);
428            typeInfo.setOptionalSizeProperty(optSizedbh.isDataTypeOptionalSizeSupported(typeInfo));
429            String nullable = rs.getString("IS_NULLABLE").trim();
430    //          String columnInfo =remotedbh.getTableColumns(dbh.getvendorName(),columnName,typeInfo,columnPrecision,rs);
431    //      System.out.println("CommonMetaDataInfo.GENERATECOLUMNQUERYFORCLIENTNODE ="+columnInfo);
432    //               sb.append(columnInfo);
433            if (nullable.equalsIgnoreCase("NO") &&
434                tgtVendorType == Utility.DataBase_Cloudscape) {
435              sb.append(columnName).append(" ").append(typeInfo.getTypeDeclaration(columnPrecision)).append(" NOT NULL ");
436            }
437            else if (nullable.equalsIgnoreCase("NO") && tgtVendorType == Utility.DataBase_DB2) {
438              sb.append(columnName).append(" ").append(typeInfo.getTypeDeclaration(
439                  columnPrecision)).append(" NOT NULL ");
440            }
441            else if (nullable.equalsIgnoreCase("NO") &&
442              tgtVendorType == Utility.DataBase_Firebird) {
443            sb.append(columnName).append(" ").append(typeInfo.getTypeDeclaration(
444                columnPrecision)).append(" NOT NULL ");
445            }
446    
447            else {
448              sb.append(columnName).append(" ").append(typeInfo.getTypeDeclaration(columnPrecision));
449            }
450    //      sb.append(columnName).append(" ").append(typeInfo.getTypeDeclaration(columnPrecision));
451            if (srcVendorType == tgtVendorType) {
452              typeInfo.setTypeName(typeName);
453            }
454            //System.out.println(" type Name === " + typeName + " sql Type " + sqlType ) ;
455            String defaultValue = rs.getString("COLUMN_DEF"); //String => default value (may be <code>null</code>)
456    //         if (defaultValue != null && !defaultValue.equalsIgnoreCase("NULL"))
457    //           sb.append(" DEFAULT  ").append("  ").append(defaultValue);
458    
459            nullable = rs.getString("IS_NULLABLE").trim(); // String => "NO" means column definitely does not allow NULL values; "YES" means the column might allow NULL values.  An empty string means nobody knows.
460            //System.out.println(" nullable ===  [" + nullable + "] nullable.equalsIgnoreCase: " + nullable.equalsIgnoreCase("NO"));
461            if (nullable.equalsIgnoreCase("NO")) {
462              //System.out.println(" I m here");
463              if (notNullColumns == null) {
464                notNullColumns = new ArrayList();
465              }
466              notNullColumns.add(columnName);
467            }
468            //new ColumnsInfo(columnName,typeName,sqlType,columnSize/*,defaultValue,nullable*/);
469            sb.append(" , ");
470          }
471          while (rs.next());
472    
473          rs.close();
474          //System.out.println(" Not Null Columns Are :: " + notNullColumns);
475          //System.out.println(" Table Columns Query === " + sb.toString());
476        }
477        catch (RepException ex) {
478          log.error(ex.getMessage(), ex);
479          throw ex;
480        }
481        catch (SQLException ex) {
482          log.error(ex.getMessage(), ex);
483          throw new RepException("REP006", new Object[] {ex.getMessage()});
484        }
485        log.debug(sb.toString());
486        return sb.toString();
487      }
488    
489      public String getAppliedConstraints(String schemaName, String tableName,TreeMap primConsMap) throws RepException {
490        StringBuffer cons = new StringBuffer();
491        appendPrimaryConstraints(schemaName, tableName, cons, primConsMap);
492        appendUniqueKeyConstraints(schemaName, tableName, cons);
493    //    appendForeignConstraints(schemaName, tableName, cons, primConsMap);
494        appendCheckConstraints(cons);
495        return cons.toString();
496      }
497    
498      public String getAppliedConstraintsForExistingTable(String schemaName,
499          String tableName, TreeMap primConsMap, AbstractDataBaseHandler dbh) throws
500          RepException {
501        StringBuffer cons = new StringBuffer();
502        appendPrimaryConstraints(schemaName, tableName, cons, primConsMap);
503        appendUniqueKeyConstraints(schemaName, tableName, cons);
504        //    appendForeignConstraints(schemaName, tableName, cons, primConsMap);
505        addCheckConstraintForExistingTable(schemaName, tableName, primConsMap, dbh);
506        appendCheckConstraints(cons);
507        return cons.toString();
508      }
509    
510      public void addCheckConstraintForExistingTable(String schemaName,
511                                                     String tableName,
512                                                     TreeMap primConsMap,
513                                                     AbstractDataBaseHandler dbh) throws
514          RepException {
515        try {
516          ResultSet rs = dbmd.getColumns(null, schemaName, tableName, "%");
517          if (rs == null || !rs.next()) {
518            log.debug("Resultset  found  " + rs + " OR Resultset found false");
519            throw new RepException("REP033", new Object[] {schemaName + "." + tableName});
520          }
521          do {
522            String columnName = rs.getString("COLUMN_NAME");
523            String nullable = rs.getString("IS_NULLABLE").trim();
524            if (nullable.equalsIgnoreCase("NO")) {
525              if (notNullColumns == null) {
526                notNullColumns = new ArrayList();
527              }
528              notNullColumns.add(columnName);
529            }
530          }
531          while (rs.next());
532          /** @todo   rs has been closed  */
533          rs.close();
534        }
535        catch (RepException ex) {
536          log.error(ex.getMessage(), ex);
537          throw ex;
538        }
539        catch (SQLException ex) {
540          log.error(ex.getMessage(), ex);
541          throw new RepException("REP006", new Object[] {ex.getMessage()});
542        }
543    
544      }
545    
546      private void appendPrimaryConstraints(String schemaName, String tableName,
547                                            StringBuffer cols, TreeMap consTableMap) throws
548          RepException {
549        HashMap primcolmap = null;
550        try {
551          ResultSet rs = dbmd.getPrimaryKeys(null, schemaName, tableName);
552          if (rs == null || !rs.next()) {
553            log.debug("Resultset  found  " + rs + " OR Resultset found false");
554            throw new RepException("REP034", new Object[] {schemaName + "." + tableName});
555          }
556          primcolmap = new HashMap();
557          do {
558            primcolmap.put(new Integer(rs.getInt("KEY_SEQ")),rs.getString("COLUMN_NAME"));
559          }
560          while (rs.next());
561          /** @todo rs has been closed */
562          rs.close();
563        }
564        catch (RepException ex) {
565          log.error(ex.getMessage(), ex);
566          throw ex;
567        }
568        catch (SQLException ex) {
569          log.error(ex.getMessage(), ex);
570          throw new RepException("REP006", new Object[] {ex.getMessage()});
571        }
572        Object[] indexes = primcolmap.keySet().toArray();
573        Arrays.sort(indexes);
574        cols.append(" Primary Key (");
575        StringBuffer temp = new StringBuffer();
576        for (int i = 0; i < indexes.length; i++) {
577          if (i != 0) {
578            temp.append(",");
579          }
580          temp.append(primcolmap.get(indexes[i]));
581        }
582        cols.append(temp.toString()).append(" )");
583    log.debug(cols.toString());
584        consTableMap.put(schemaName + "." + tableName, schemaName + "." + tableName + "(" + temp.toString() + ")");
585      }
586    
587      // PKTABLE_CAT</B> String => primary key table catalog being imported (may be <code>null</code>)
588      // PKTABLE_SCHEM</B> String => primary key table schema being imported (may be <code>null</code>)
589      // PKTABLE_NAME</B> String => primary key table name being imported
590      // PKCOLUMN_NAME</B> String => primary key column name being imported
591      // FKTABLE_CAT</B> String => foreign key table catalog (may be <code>null</code>)
592      // FKTABLE_SCHEM</B> String => foreign key table schema (may be <code>null</code>)
593      // FKTABLE_NAME</B> String => foreign key table name
594      // FKCOLUMN_NAME</B> String => foreign key column name
595      // KEY_SEQ</B> short => sequence number within a foreign key
596      // UPDATE_RULE</B> short =>
597      // DELETE_RULE</B> short =>
598      // FK_NAME</B> String => foreign key name (may be <code>null</code>)
599      // PK_NAME</B> String => primary key name (may be <code>null</code>)
600      // DEFERRABILITY</B> short => can the evaluation of foreign key constraints be deferred until commit
601      private void appendForeignConstraints(String schemaName, String tableName,
602                                            StringBuffer cols, TreeMap consTableMap) throws
603          RepException {
604        HashMap fk_Keys = new HashMap();
605        HashMap fk_pk = new HashMap();
606        try {
607          ResultSet rs = dbmd.getImportedKeys(null, schemaName, tableName);
608          if (rs == null || !rs.next()) {
609            log.debug("Resultset  found  " + rs + " OR Resultset found false");
610            return;
611          }
612          do {
613            String pk_tableName = rs.getString("PKTABLE_SCHEM") + "." +rs.getString("PKTABLE_NAME");
614            //changed to handle the case for unique key
615            Object primObject =pk_tableName+"("+rs.getString("PKCOLUMN_NAME")+")";// consTableMap.get(pk_tableName.toUpperCase());
616            if (primObject == null) {
617              ResultSet primaryColumns = dbmd.getPrimaryKeys(null, rs.getString("PKTABLE_SCHEM"), rs.getString("PKTABLE_NAME"));
618              HashMap map = new HashMap();
619              while (primaryColumns.next()) {
620                int columnIndex = primaryColumns.getInt("KEY_SEQ");
621                String columnName = primaryColumns.getString("COLUMN_NAME");
622                map.put(new Integer(columnIndex), columnName);
623              }
624              Object[] indexes = map.keySet().toArray();
625              Arrays.sort(indexes);
626              StringBuffer temp = new StringBuffer();
627              for (int i = 0; i < indexes.length; i++) {
628                if (i != 0) {
629                  temp.append(",");
630                }
631                temp.append(map.get(indexes[i]));
632              }
633              consTableMap.put(pk_tableName.toUpperCase(),
634                               pk_tableName.toUpperCase() + "(" + temp.toString() +") ");
635    
636            }
637            primObject = consTableMap.get(pk_tableName.toUpperCase());
638            if (primObject != null) {
639              String fk_tableName = rs.getString("FKTABLE_SCHEM") + "." +rs.getString("FKTABLE_NAME");
640              String mapKey = rs.getString("FK_NAME");
641              Object ob = fk_Keys.get(mapKey);
642              if (ob == null) {
643                HashMap colsMap = new HashMap();
644                colsMap.put(new Integer(rs.getInt("KEY_SEQ")),rs.getString("FKCOLUMN_NAME"));
645                fk_pk.put(mapKey, primObject);
646                fk_Keys.put(mapKey, colsMap);
647              }
648              else {
649                ( (HashMap) ob).put(new Integer(rs.getInt("KEY_SEQ")), rs.getString("FKCOLUMN_NAME"));
650              }
651            }
652          }
653          while (rs.next());
654          /** @todo   rs has been closed  */
655          rs.close();
656        }
657        catch (SQLException ex) {
658          log.error(ex.getMessage(), ex);
659          throw new RepException("REP006", new Object[] {ex.getMessage()});
660        }
661        Object[] fkeys = fk_Keys.keySet().toArray();
662        Arrays.sort(fkeys);
663        for (int i = 0; i < fkeys.length; i++) {
664          HashMap map = (HashMap) fk_Keys.get(fkeys[i]);
665          Object[] indexes = map.keySet().toArray();
666          Arrays.sort(indexes);
667          cols.append(" , Foreign Key (");
668          for (int j = 0; j < indexes.length; j++) {
669            if (j != 0) {
670              cols.append(",");
671            }
672            cols.append(map.get(indexes[j]));
673          }
674          cols.append(" ) References " + fk_pk.get(fkeys[i]));
675          log.debug(cols.toString());
676        }
677      }
678    
679      private void appendCheckConstraints(StringBuffer sb) {
680        for (int i = 0, length = notNullColumns.size(); i < length; i++) {
681          sb.append(" , Check ( ").append(notNullColumns.get(i)).append(" is not null ) ");
682        }
683        log.debug(sb.toString());
684        notNullColumns = null;
685      }
686    
687      public void setPrimaryColumns(RepTable repTable, String schemaName, String tableName) throws RepException,
688          SQLException {
689        //ResultSet rs2 = dbmd.getPrimaryKeys(null,schemaName,tableName.toLowerCase());
690        //Util.showResultSet(rs2);
691        HashMap primcolmap = new HashMap();
692        //ResultSet rs = dbmd.getPrimaryKeys(null,schemaName,tableName.toLowerCase()); // SQL Server / Daffodil DB
693        ResultSet rs = dbmd.getPrimaryKeys(null, schemaName, tableName); // Oracle
694        if (rs == null || !rs.next()) {
695          log.debug("Resultset  found " + rs + " OR Resultset found false");
696          throw new RepException("REP034", new Object[] {tableName});
697        }
698    
699        do {
700          primcolmap.put(new Integer(rs.getInt("KEY_SEQ")),
701                         rs.getString("COLUMN_NAME"));
702        }
703        while (rs.next());
704        /** @todo   rs has been closed  */
705        rs.close();
706    
707        String[] primColumns = new String[primcolmap.size()];
708        Object[] indexes = primcolmap.keySet().toArray();
709        Arrays.sort(indexes);
710        for (int i = 0; i < indexes.length; i++) {
711          primColumns[i] = (String) primcolmap.get(indexes[i]);
712        }
713        repTable.setPrimaryColumns(primColumns);
714      }
715    
716      public String getExistingTableQuery(AbstractDataBaseHandler dbh, SchemaQualifiedName sname, int pubVendorType) throws RepException,
717          SQLException {
718    
719        StringBuffer sb = new StringBuffer();
720        String table = sname.getTableName();
721        String schema = sname.getSchemaName();
722        sb.append(" Create Table ").append(sname.toString()).append(" ( ");
723    //      ResultSet rs = dbmd.getColumns(null, schema.toLowerCase(), table.toLowerCase(),"%");
724        //CHANGED BY SUBE DUE PROBLEM OCCRE IN ORACLE IF ALREADY EXIST WITH DIFF STRUCUTRE
725        ResultSet rs = dbmd.getColumns(null, schema, table, "%");
726        //ResultSet rs = dbmd.getColumns(null,schema, table,"%");
727        if (rs == null || !rs.next()) {
728          log.debug("Resultset  found " + rs + " OR Resultset found false");
729          throw new RepException("REP033", new Object[] {sname.toString()});
730        }
731        do {
732          String typeName = dbh.updateDataType(rs.getString("TYPE_NAME"));
733          int dataType = rs.getInt("DATA_TYPE");
734          TypeInfo typeInfo = new TypeInfo(typeName, dataType);
735          int columnPrecision = rs.getInt("COLUMN_SIZE");
736          columnPrecision = dbh.getAppropriatePrecision(columnPrecision, typeName);
737          String columnName = rs.getString("COLUMN_NAME");
738          typeInfo.setColumnSize(rs.getInt("COLUMN_SIZE"));
739          dbh.setTypeInfo(typeInfo, rs);
740          typeInfo.setOptionalSizeProperty(dbh.isDataTypeOptionalSizeSupported(typeInfo));
741    
742    //         String columnInfo =dbh.getTableColumns(pubVendorType,columnName,typeInfo,columnPrecision,rs);
743    //System.out.println("CommonMetaDataInfo.getExistingTableQuery(dbh, sname) ="+columnInfo);
744    //         sb.append(columnInfo);
745    
746          sb.append(columnName).append(" ").append(typeInfo.getTypeDeclaration(columnPrecision));
747          sb.append(" , ");
748        }
749        while (rs.next());
750        /** @todo rs has been closed */
751        rs.close();
752    //      appendPrimaryConstraints(schema,table,sb,new HashMap());
753        sb.append(" " +getAppliedConstraintsForExistingTable(schema, table,
754        new TreeMap(String.CASE_INSENSITIVE_ORDER),dbh) + " ) ");
755        log.debug(sb.toString());
756        return sb.toString();
757      }
758    
759    
760      protected void checkChildTableIncludedInDropTableList(ArrayList pubRepTableList,String[] dropTableArray) throws RepException {
761        ResultSet rs2=null;
762          ArrayList pubTablelist = new ArrayList();
763          for (int i = 0; i < pubRepTableList.size(); i++) {
764            pubTablelist.add(((RepTable)pubRepTableList.get(i)).getSchemaQualifiedName().toString());
765          }
766    // System.out.println("pubTablelist::"+pubTablelist);
767          ArrayList dropTableList=new ArrayList();
768          for (int i = 0; i <dropTableArray.length; i++) {
769          SchemaQualifiedName sname = new SchemaQualifiedName(this,dropTableArray[i]);
770    
771         // Add by sube get the table name with schema
772          checkTableExistance(sname);
773          dropTableList.add(sname.toString());
774          }
775    // System.out.println("dropTableList::"+dropTableList);
776          try {
777            for (int i = 0; i < dropTableList.size(); i++) {
778              // following code is optimizable because there is already a SchemaQualifiedName
779              // Refer line no 757 --- sube
780              SchemaQualifiedName sname = new SchemaQualifiedName(this,dropTableList.get(i).toString());
781              String schemaName = sname.getSchemaName();
782              String tableName = sname.getTableName();
783               rs2 = dbmd.getExportedKeys(null, schemaName, tableName);
784              log.debug(" ExportedKeys Table Name's ResultSet " + rs2);
785              if (rs2 != null && rs2.next()) {
786                do {
787                  String primaryTable = rs2.getString("PKTABLE_SCHEM") + "." +rs2.getString("PKTABLE_NAME");
788                  String foreignTable = rs2.getString("FKTABLE_SCHEM") + "." +rs2.getString("FKTABLE_NAME");
789    //System.out.println("primaryTable::"+primaryTable+" foreignTable::"+foreignTable);
790                  if ((!dropTableList.contains(foreignTable.toUpperCase())) && pubTablelist.contains(foreignTable.toUpperCase())) {
791                      throw new RepException("REP321", new Object[] {primaryTable,foreignTable});
792                    }
793                  }
794                while (rs2.next());
795              }
796            }
797          }
798        catch (RepException ex) {
799          throw ex;
800        }
801          catch (SQLException ex) {
802            log.error(ex.getMessage(), ex);
803            throw new RepException("REP006", new Object[] {ex.getMessage()});
804          }finally{
805            try {
806              if (rs2 != null) {
807                rs2.close();
808              }
809            }
810            catch (SQLException ex1) {
811            }
812          }
813        }
814    
815    
816      public ArrayList getChildTables(String parentTable)throws RepException {
817        String foreignTable =null;
818        ArrayList childTableList =new ArrayList();
819        try {
820          SchemaQualifiedName sname =new SchemaQualifiedName(this,parentTable);
821          ResultSet rs2 = dbmd.getExportedKeys(null, sname.getSchemaName(),sname.getTableName());
822          log.debug(" ExportedKeys Table Name's ResultSet " + rs2);
823          if (rs2 != null && rs2.next()) {
824            do {
825               foreignTable = rs2.getString("FKTABLE_SCHEM") + "." +rs2.getString("FKTABLE_NAME");
826               childTableList.add(foreignTable);
827            }
828            while (rs2.next());
829          }
830          /** @todo   rs has been closed  */
831          rs2.close();
832    
833        }
834        catch (SQLException ex) {
835          log.error(ex.getMessage(), ex);
836          throw new RepException("REP006", new Object[] {ex.getMessage()});
837        }
838    //System.out.println("CommonMetaDataInfo.getChildTables() : "+childTableList);
839        return childTableList;
840      }
841    
842    
843    
844    
845    
846    
847      public Object[] getImportedColsOfChildTable(String parentTable,String childTable)throws RepException {
848        ArrayList fkColsList=new ArrayList();
849        ArrayList referColsList=new ArrayList();
850        try {
851           SchemaQualifiedName sname =new SchemaQualifiedName(this,childTable);
852          ResultSet rs = dbmd.getImportedKeys(null, sname.getSchemaName(), sname.getTableName());
853          if (rs == null || !rs.next()) {
854            log.debug("Resultset  found  " + rs + " OR Resultset found false");
855            return null;
856          }
857          do {
858            String pk_tableName = rs.getString("PKTABLE_SCHEM") + "." +rs.getString("PKTABLE_NAME");
859            if(pk_tableName.equalsIgnoreCase(parentTable)){
860              fkColsList.add(rs.getString("FKCOLUMN_NAME"));
861              referColsList.add(rs.getString("PKCOLUMN_NAME"));
862            }
863          }
864          while (rs.next());
865          /** @todo   rs has been closed  */
866          rs.close();
867        }
868        catch (SQLException ex) {
869          log.error(ex.getMessage(), ex);
870          throw new RepException("REP006", new Object[] {ex.getMessage()});
871        }
872    
873        String[] fkCols =new String[fkColsList.size()];
874        fkColsList.toArray(fkCols);
875        String[] pkCols =new String[referColsList.size()];
876        referColsList.toArray(pkCols);
877    
878          return new Object[]{fkCols,pkCols};
879      }
880    
881      public ArrayList getImportedTables(SchemaQualifiedName schemaQualifiedName, List passedSchemaQualifiedNamesList,DirectedGraph graph,String[] removeCycleTableNames0) throws RepException {
882            try {
883              ResultSet rs = dbmd.getImportedKeys(null, schemaQualifiedName.getSchemaName(), schemaQualifiedName.getTableName());
884              ArrayList listOfSuperTables = new ArrayList();
885              while (rs.next()) {
886                SchemaQualifiedName superTableSchemaQualifiedName = new SchemaQualifiedName(this, null,rs.getString(2), rs.getString(3));
887                /** @todo
888                 * define Ex in properties file
889                 *  */
890    
891                if (!passedSchemaQualifiedNamesList.contains(superTableSchemaQualifiedName)) {
892                           throw new RepException("REP038", new Object[] {
893                      superTableSchemaQualifiedName, schemaQualifiedName});
894                }
895                //to handle tables in cycle case.we do not add edge for tables in removeCycleTableNames arraylist provided by user
896                String [] removeCycleTableNames=removeCycleTableNames0;
897                boolean addEdge=true;
898                if(removeCycleTableNames!=null){
899                  for (int i = 0; i < removeCycleTableNames.length; i++) {
900                    StringTokenizer str = new StringTokenizer(removeCycleTableNames[i],"-");
901                    SchemaQualifiedName sname = new SchemaQualifiedName(this, (String)str.nextElement());
902                    SchemaQualifiedName sourceSname = sname;
903                    checkTableExistance(sname);
904                    if(!passedSchemaQualifiedNamesList.contains(sname))
905                      throw new RepException("Rep0206",new Object[] {sname});
906    //              System.out.println("source::::"+sourceSname.toString());
907                    sname = new SchemaQualifiedName(this, (String)str.nextElement());
908                    checkTableExistance(sname);
909                    if(!passedSchemaQualifiedNamesList.contains(sname))
910                      throw new RepException("Rep0206",new Object[] {sname});
911                    SchemaQualifiedName targetSname = sname;
912    //System.out.println("target::::"+targetSname.toString());
913    //System.out.println("schemaQualifiedName.toString():::"+schemaQualifiedName.toString());
914    //System.out.println("superTableSchemaQualifiedName.toString():::"+superTableSchemaQualifiedName.toString());
915                    if (sourceSname.toString().equalsIgnoreCase(schemaQualifiedName.toString()) &&
916                        targetSname.toString().equalsIgnoreCase(superTableSchemaQualifiedName.toString()))
917                      addEdge = false;
918                  }
919                }
920                // to handle self referencing case
921                if(schemaQualifiedName.toString().equalsIgnoreCase(superTableSchemaQualifiedName.toString()))
922                     addEdge = false;
923    
924                if(addEdge){
925    //System.out.println("schemaQualifiedName.toString():::" +schemaQualifiedName.toString());
926    //System.out.println("superTableSchemaQualifiedName.toString():::" +superTableSchemaQualifiedName.toString());
927                  graph.addEdge(schemaQualifiedName, superTableSchemaQualifiedName, 1);
928                }
929    
930                if(!listOfSuperTables.contains(superTableSchemaQualifiedName))
931                  listOfSuperTables.add(superTableSchemaQualifiedName);
932              }
933              rs.close();
934              return listOfSuperTables;
935            }catch(NoSuchElementException ex1){
936              throw new RepException("Rep0204",null);
937            }
938            catch (SQLException ex) {
939              RepConstants.writeERROR_FILE(ex);
940              throw new RepException("REP039",new Object[]{ex.getMessage()});
941            }
942          }
943    
944          public ArrayList getForeignKeyConstraints(String schemaName, String tableName) throws RepException {
945               HashMap fk_Keys = new HashMap();
946               HashMap fk_pk = new HashMap();
947               HashMap consPkTableMap=new HashMap();
948               HashMap referenceString=new HashMap();
949               StringBuffer alterTableQuery = new StringBuffer();
950               ResultSet rs;
951               ArrayList foreignKeyConstraintsList = new ArrayList();
952               try {
953                 rs = dbmd.getImportedKeys(null, schemaName, tableName);
954                 boolean next = rs.next();
955                 if (!next) {
956                   return null;
957                 }
958                 do {
959                   String pk_tableName = rs.getString("PKTABLE_SCHEM") + "." + rs.getString("PKTABLE_NAME");
960    
961                   String mapPkKey = rs.getString("FK_NAME");
962                   //changed to handle the case for unique key
963    
964                   Object primObjectTemp = consPkTableMap.get(mapPkKey);
965                   if (primObjectTemp == null) {
966                     HashMap colsPkMap = new HashMap();
967                     colsPkMap.put(new Integer(rs.getInt("KEY_SEQ")),rs.getString("PKCOLUMN_NAME"));
968    
969                     consPkTableMap.put(mapPkKey, colsPkMap);
970                   }
971                   else {
972                     ((HashMap) primObjectTemp).put(new Integer(rs.getInt("KEY_SEQ")),rs.getString("PKCOLUMN_NAME"));
973                   }
974                   Object primObject = consPkTableMap.get(mapPkKey);
975                   if (primObject != null) {
976                     String fk_tableName = rs.getString("FKTABLE_SCHEM") + "." + rs.getString("FKTABLE_NAME");
977                     String mapKey = rs.getString("FK_NAME");
978                     Object ob = fk_Keys.get(mapKey);
979                     if (ob == null) {
980                       HashMap colsMap = new HashMap();
981                       colsMap.put(new Integer(rs.getInt("KEY_SEQ")), rs.getString("FKCOLUMN_NAME"));
982                       fk_pk.put(mapKey, primObject);
983                       fk_Keys.put(mapKey, colsMap);
984                     }
985                     else {
986                       ( (HashMap) ob).put(new Integer(rs.getInt("KEY_SEQ")), rs.getString("FKCOLUMN_NAME"));
987                     }
988                   }
989                     HashMap mapTemp=(HashMap)primObject;
990                     Iterator itr=mapTemp.values().iterator();
991                     Object colNames = null;
992                     do {
993                       if(colNames!=null){
994                         colNames =itr.next()+ "," +colNames  ;
995                       }
996                       else{
997                         colNames = itr.next();
998                       }
999                     }while(itr.hasNext()) ;
1000                     referenceString.put(mapPkKey, pk_tableName + " ( " + colNames + " )");
1001                 }
1002                 while (rs.next());
1003                 rs.close();
1004               }
1005               catch (SQLException ex) {
1006                 throw new RepException("REP006", new Object[] {
1007                     ex.getMessage()});
1008               }
1009    
1010               Object[] fkeys = fk_Keys.keySet().toArray();
1011               Object[] pkeys = referenceString.keySet().toArray();
1012               for (int i = 0; i < fkeys.length; i++) {
1013                   alterTableQuery.append("alter table ")
1014                       .append(schemaName + "." + tableName).append(" add constraint ")
1015                       .append(fkeys[i]);
1016                   HashMap map = (HashMap) fk_Keys.get(fkeys[i]);
1017                   Object[] indexes = map.keySet().toArray();
1018                   Arrays.sort(indexes);
1019                   alterTableQuery.append(" Foreign Key (");
1020                   for (int j = 0; j < indexes.length; j++) {
1021                     if (j != 0) {
1022                       alterTableQuery.append(",");
1023                     }
1024                     alterTableQuery.append(map.get(indexes[j]));
1025                   }
1026                   alterTableQuery.append(" ) References ");
1027    
1028    //               for (int j = 0; j < pkeys.length; j++) {
1029                     alterTableQuery.append(referenceString.get(pkeys[i])); //fk_pk.get(fkeys[i]));
1030    //               }
1031    // System.out.println("Alter table query is : " + alterTableQuery.toString());
1032                   foreignKeyConstraintsList.add(alterTableQuery.toString());
1033                   alterTableQuery.setLength(0);
1034    
1035               }
1036               return foreignKeyConstraintsList;
1037             }
1038    
1039             public void appendUniqueKeyConstraints(String schemaName, String tableName,
1040                                                     StringBuffer cols) throws RepException {
1041    
1042               HashMap consPkTableMap = new HashMap();
1043               HashMap uniqueCol = new HashMap();
1044               ArrayList primaryKeyColumns = new ArrayList();
1045               ResultSet rs=null,rsPk=null;
1046    
1047               try {
1048                  rsPk = dbmd.getPrimaryKeys(null, schemaName, tableName);
1049                 while (rsPk.next()) {
1050                   primaryKeyColumns.add(rsPk.getString("PK_NAME"));
1051                 }
1052                 rs = dbmd.getExportedKeys(null, schemaName, tableName);
1053                 boolean next = rs.next();
1054                 if (!next) {
1055                   return;
1056                 }
1057                 do {
1058                   String pk_tableName = rs.getString("PKTABLE_SCHEM") + "." +rs.getString("PKTABLE_NAME");
1059    //System.out.println("pk_tableName" + rs.getString("PKTABLE_NAME"));
1060    //System.out.println("KEY_SEQ::::" + rs.getShort("KEY_SEQ"));
1061    //System.out.println("PKCOLUMN_NAME " + pk_tableName + "(" +rs.getString("PKCOLUMN_NAME") + ")");
1062                   String mapPkKey = rs.getString("PK_NAME");
1063                   if (!primaryKeyColumns.contains(mapPkKey)) {
1064                     Object primObjectTemp = consPkTableMap.get(mapPkKey);
1065                     if (primObjectTemp == null) {
1066                       HashMap colsPkMap = new HashMap();
1067                       colsPkMap.put(new Integer(rs.getInt("KEY_SEQ")),
1068                                     rs.getString("PKCOLUMN_NAME"));
1069    
1070                       consPkTableMap.put(mapPkKey, colsPkMap);
1071                     }
1072                     else {
1073                       ( (HashMap) primObjectTemp).put(new Integer(rs.getInt("KEY_SEQ")),
1074                                                       rs.getString("PKCOLUMN_NAME"));
1075                     }
1076    
1077                     Object primObject = consPkTableMap.get(mapPkKey);
1078                     HashMap mapTemp = (HashMap) primObject;
1079                     Iterator itr = mapTemp.values().iterator();
1080                     Object colNames = null;
1081                     do {
1082                       if (colNames != null) {
1083                         colNames = itr.next() + "," + colNames;
1084                       }
1085                       else {
1086                         colNames = itr.next();
1087                       }
1088                     }
1089                     while (itr.hasNext());
1090                     uniqueCol.put(mapPkKey, " Unique " + " ( " + colNames + " )");
1091                   }
1092                 }
1093                 while (rs.next());
1094    
1095               }
1096               catch (SQLException ex) {
1097                 throw new RepException("REP006", new Object[] {
1098                                        ex.getMessage()});
1099               }finally{
1100                  try {
1101                    if (rs != null) {
1102                      rs.close();
1103                    }
1104                    if (rsPk != null) {
1105                      rsPk.close();
1106                    }
1107                  }
1108                  catch (SQLException ex1) {
1109                  }
1110               }
1111    // System.out.println("unique cols::" + uniqueCol);
1112               Object[] ukeys = uniqueCol.keySet().toArray();
1113               for (int j = 0; j < ukeys.length; j++) {
1114                 cols.append("," + uniqueCol.get(ukeys[j]));
1115    // System.out.println("Ukeys::::" + cols.toString());
1116               }
1117             }
1118    
1119    
1120           public ArrayList getExportedTableCols(SchemaQualifiedName repTable) throws RepException{
1121             ResultSet rs=null;
1122             ArrayList exportedColumns = new ArrayList();
1123             try {
1124               rs = dbmd.getExportedKeys(null, repTable.getSchemaName(), repTable.getTableName());
1125               boolean next = rs.next();
1126               if (!next) {
1127                 return null;
1128               }
1129               do {
1130                 exportedColumns.add(rs.getString("PKCOLUMN_NAME"));
1131         // System.out.println("Ukeys::::" + cols.toString());
1132               }
1133               while (rs.next());
1134             } catch (SQLException ex) {
1135                 throw new RepException("REP006", new Object[] {
1136                                        ex.getMessage()});
1137               }finally{
1138                  try {
1139                    if (rs != null) {
1140                      rs.close();
1141                    }
1142                  }
1143                  catch (SQLException ex1) {
1144                  }
1145               }
1146    //      System.out.println("CommonMetaDataInfo.getExportedTableCols(repTable)::"+exportedColumns);
1147    return exportedColumns;
1148           }
1149    
1150    
1151        public void setAllColumns(RepTable repTable, String schemaName,
1152                                     String tableName) throws RepException, SQLException {
1153    
1154             ResultSet rs = dbmd.getColumns(null, schemaName, tableName, "%");
1155             if (rs == null || !rs.next()) {
1156               log.debug("Resultset  found  " + rs + " OR Resultset found false");
1157               throw new RepException("REP033",
1158                                      new Object[] {schemaName + "." + tableName});
1159             }
1160             ArrayList allColumnsList=new ArrayList();
1161             try {
1162               do {
1163                 allColumnsList.add(rs.getString("COLUMN_NAME"));
1164               }
1165               while (rs.next());
1166    
1167             }
1168             finally {
1169               if (rs != null)
1170                 rs.close();
1171             }
1172             for (int i = 0; i < allColumnsList.toArray().length; i++) {
1173    // System.out.println(" allColumnsList.toArray() : "+allColumnsList.toArray()[i].getClass());
1174             }
1175             int numberOfColumns =allColumnsList.size();
1176             String columns[] =new String[numberOfColumns];
1177             for (int i = 0; i < numberOfColumns; i++) {
1178               columns[i] = (String)allColumnsList.get(i);
1179             }
1180             repTable.setAllColumns(columns);
1181           }
1182    
1183    }





























































Powered by Drupal - Theme by Danger4k