Annotation of FM2SQL/DBBean.java, revision 1.18

1.11      rogo        1: /*
                      2:  * DBBean.java -- Class that ecapsulates all database actions 
                      3:  * Filemake to SQL Converter 
                      4:  * Copyright (C) 2004 Robert Gordesch (rogo@mpiwg-berlin.mpg.de) 
                      5:  * This program is free software; you can redistribute it and/or modify it
                      6:  * under the terms of the GNU General Public License as published by the Free
                      7:  * Software Foundation; either version 2 of the License, or (at your option)
                      8:  * any later version.  Please read license.txt for the full details. A copy of
                      9:  * the GPL may be found at http://www.gnu.org/copyleft/lgpl.html  You should
                     10:  * have received a copy of the GNU General Public License along with this
                     11:  * program; if not, write to the Free Software Foundation, Inc., 59 Temple
                     12:  * Place, Suite 330, Boston, MA 02111-1307 USA  Created on 15.09.2003 by
                     13:  * rogo  
                     14:  */
1.3       rogo       15: 
                     16: 
1.1       rogo       17: import java.sql.*;
1.15      rogo       18: import java.text.DateFormat;
1.1       rogo       19: import java.util.*;
1.15      rogo       20: 
1.1       rogo       21: import com.fmi.jdbc.*;
                     22: 
                     23: /**
                     24:  *
                     25:  *
                     26:  * DBBean - Database bean
                     27:  *
                     28:  *<p> a Javabean  to perform queries on a JDBC Database,
                     29:  * or excute any other SQL statement
                     30:  * </p>
                     31:  * <p>
                     32:  * Usage:
                     33:  * <pre>
                     34:  *   DBBean bean = new DBBean();
                     35:  * // setting user and passwd
                     36:  *  bean.setUserAndPasswd("bla","bla");
                     37:  *  try
                     38:  *  {
                     39:  *    bean.setConnection("jdbc:fmpro:http://localhost");
                     40:  *    Vector names=bean.getTableNames();
                     41:  *    Vector[] result=bean.getQueryData(names.get(0).toString());
                     42:  *  // print results to screen
                     43:  *     for(int i=0;i&lt;result[1].size();++i)
                     44:  *     {
                     45:  *       //print Header
                     46:  *       System.out.print(" "+result[1].get(i));
                     47:  *     }
                     48:  *  System.out.println();
                     49:  *  for(int j=0;j&lt;result[0].size();++j)
                     50:  *  {
                     51:  *     Vector row=(Vector)result[0].get(j);
                     52:  *     //print rows
                     53:  *     for(int k=0;k&lt;row.size();++k)
                     54:  *     System.out.print(" "+row.get(k));
                     55:  *     System.out.println();
                     56:  *  }
                     57:  * } catch(Exception e)
                     58:  *   {
                     59:  *     System.out.println("Error while connecting to database"+ e);
                     60:  *   }
                     61:  * </pre>
                     62:  *
                     63:  * </p>
                     64:  * @author rogo
                     65:  */
                     66: public class DBBean
                     67: {
                     68:   Connection connection;
                     69:   String url = "";
                     70:   DatabaseMetaData dbMetaData;
                     71:   Vector columnNames;
1.4       rogo       72:   Vector ids = new Vector();
1.1       rogo       73:   String user = (System.getProperty("user.name") == null) ? "" : System.getProperty("user.name"); //"postgres";
                     74:   String passwd = ""; //"3333";//"rogo";
                     75:   public int maxHits = 10;
                     76:   ResultSet result;
                     77:   String quoteChar = "";
                     78:   Hashtable connectionPool = new Hashtable();
                     79:   public ResultSetMetaData metaData;
                     80:   // register DataBase Drivers
                     81:   static {
                     82:     try
                     83:     {
                     84:       DriverManager.registerDriver(new com.fmi.jdbc.JdbcDriver());
                     85:       DriverManager.registerDriver((Driver) Class.forName("org.postgresql.Driver").newInstance());
1.9       rogo       86:       DriverManager.registerDriver((Driver) Class.forName("com.mysql.jdbc.Driver").newInstance());
1.1       rogo       87:       DriverManager.registerDriver((Driver) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance());
1.9       rogo       88:      
1.1       rogo       89:       // wait a maximum of 10 seconds when attempting to establish a connection
                     90:       DriverManager.setLoginTimeout(10);
                     91:     } catch (Exception e)
                     92:     {
                     93:       System.out.println(e);
                     94:     }
                     95:   }
                     96:   /**
                     97:    * Constructs a database bean
                     98:    */
                     99:   public DBBean()
                    100:   {
                    101: 
                    102:   }
                    103:   /**
                    104:    *Constructs a database bean
                    105:    * and tries to connect to database
                    106:    *specified in the jdbcConnectionURL
                    107:    * @param jdbcConnectionURL url to connect to database
                    108:     */
                    109: 
                    110:   public DBBean(String jdbcConnectionURL) throws Exception
                    111:   {
                    112: 
                    113:     this.url = jdbcConnectionURL;
                    114: 
                    115:     connection = getConnection();
                    116:     if (connection == null)
                    117:       return;
                    118:     // get the meta data for the current connection
                    119:     DatabaseMetaData dbMetaData = (DatabaseMetaData) connection.getMetaData();
                    120:     quoteChar = dbMetaData.getIdentifierQuoteString();
                    121:     if (quoteChar == null)
                    122:       quoteChar = "\""; // needed for postgres
                    123: 
                    124:     // create the root node of the tree
                    125:     // get the open tables from the current connection; the FileMaker Pro
                    126:     // JDBC driver ignores all of the parameters to this method
                    127: 
                    128:     // iterate over the table names and add them to the root node of the tree
                    129: 
                    130:   }
                    131:   /**
                    132:    * sets the maximum number of hits
                    133:    */
                    134:   public void setMaxHits(int maxHits)
                    135:   {
                    136:     this.maxHits = maxHits;
                    137:   }
                    138:   /**
                    139:     * gets the maximum number of hits
                    140:     */
                    141:   public int getMaxHits()
                    142:   {
                    143:     return maxHits;
                    144:   }
                    145: 
                    146:   /**
                    147:    * returns the Connection if null creates a new one from the url property.
                    148:    *
                    149:    */
                    150:   public Connection getConnection() throws Exception
                    151:   {
                    152:     ConnectionPool conPool = (ConnectionPool) connectionPool.get(url);
                    153:     if (conPool == null)
                    154:     {
1.18    ! rogo      155:       createConnection();  
        !           156:       
1.1       rogo      157:     } else
                    158:     {
                    159:       if (!conPool.user.equals(user) || !conPool.passwd.equals(passwd))
                    160:       {
                    161:         conPool.con.close();
                    162:         conPool.user = user;
                    163:         conPool.passwd = passwd;
                    164: 
                    165:       }
                    166:       connection = conPool.con;
                    167:       if (connection.isClosed())
                    168:       {
                    169:         System.out.println("Made new connection!!!");
1.18    ! rogo      170:         createConnection();  
        !           171:    
        !           172:        // connection = DriverManager.getConnection(conPool.url, conPool.user, conPool.passwd);
1.1       rogo      173:         conPool.con = connection;
                    174:       }
                    175:     }
                    176:     if (url != "" && connection == null)
1.18    ! rogo      177:       createConnection();
        !           178:       //connection = DriverManager.getConnection(url, user, passwd);
1.1       rogo      179:     dbMetaData = connection.getMetaData();
                    180:     quoteChar = dbMetaData.getIdentifierQuoteString();
                    181:     if (quoteChar == null)
                    182:       quoteChar = "\""; // needed for postgres
                    183: 
                    184:     return connection;
                    185:   }
1.18    ! rogo      186:   private void createConnection() throws SQLException
        !           187:   {
        !           188:       // setup the properties 
        !           189:        java.util.Properties prop = new java.util.Properties();
        !           190:        prop.put("charSet", "Cp1256");
        !           191:        prop.put("user", user);
        !           192:        prop.put("password", passwd);
        !           193:     System.out.println("url "+url);
        !           194:     if(url.indexOf("odbc")>=0)
        !           195:     {
        !           196:     // Connect to the database
        !           197:       connection = DriverManager.getConnection(url, prop);
        !           198:       System.out.println("odbc with properties inited");
        !           199:     }
        !           200:     else
        !           201:     connection = DriverManager.getConnection(url, user, passwd);
        !           202:     connectionPool.put(url, new ConnectionPool(url, user, passwd, connection));
        !           203:     
        !           204:   }
1.1       rogo      205:   /**
                    206:    * sets the connection of this DBBean to the database specified in the url
                    207:    *  property
                    208:    */
                    209:   public void setConnection(String url) throws Exception
                    210:   {
                    211:     this.url = url;
                    212:     if (url != "")
1.18    ! rogo      213:       //connection = DriverManager.getConnection(url, user, passwd);
        !           214:     createConnection();
1.1       rogo      215:     dbMetaData = connection.getMetaData();
                    216:     quoteChar = dbMetaData.getIdentifierQuoteString();
                    217:     if (quoteChar == null)
                    218:       quoteChar = "\""; // needed for postgres
                    219:   }
                    220:   /**
                    221:    * sets the connection of this DBBean to the database specified in the url
                    222:    * and the url,user and passwd property of this DBBean instance
                    223:    */
                    224:   public void setConnection(String url, String user, String passwd) throws Exception
                    225:   {
                    226:     this.user = user;
                    227:     this.passwd = passwd;
                    228:     this.url = url;
                    229:     if (url != "")
1.18    ! rogo      230:      createConnection();
        !           231:      // connection = DriverManager.getConnection(url, user, passwd);
1.1       rogo      232:     dbMetaData = connection.getMetaData();
                    233:     quoteChar = dbMetaData.getIdentifierQuoteString();
                    234:     if (quoteChar == null)
                    235:       quoteChar = "\""; // needed for postgres
                    236:   }
                    237: 
1.4       rogo      238:   public void setIDVector(Vector ids)
                    239:   {
                    240:     this.ids = ids;
                    241:   }
                    242: 
                    243:   /** 
                    244:    * returns a Vector containing the ID Row Name 
                    245:    **/
                    246:   public Vector getIDVector()
                    247:   {
                    248:     return ids;
                    249:   }
1.1       rogo      250:   /**
                    251:    * returns a Vector containing the Tablenames or an error message in the Vector
                    252:    */
                    253:   public Vector getTableNames()
                    254:   {
                    255:     Vector tableNameVec = new Vector();
                    256:     try
                    257:     {
                    258:       if (connection == null)
                    259:       {
                    260:         Vector vec = new Vector();
                    261:         vec.add("no database connection");
                    262:         return vec;
                    263:       }
                    264:       if (dbMetaData == null)
                    265:         dbMetaData = connection.getMetaData();
                    266:       ResultSet tableNames = dbMetaData.getTables(null, null, null, null);
                    267:       // System.out.println(dbMetaData.supportsAlterTableWithAddColumn());
                    268:       // iterate over the table names and add them to the root node of the tree
                    269: 
                    270:       while (tableNames.next())
                    271:       {
                    272:         String tableName = tableNames.getString("TABLE_NAME");
                    273:         tableNameVec.add(tableName);
                    274: 
                    275:       }
                    276:     } catch (Exception e)
                    277:     {
                    278:       e.printStackTrace();
                    279:     }
                    280:     return tableNameVec;
                    281:   }
                    282:   /**
                    283:    * returns a Vector containing the Tablenames or an error message in the Vector
                    284:    */
                    285:   public Vector getTableNames(String catalog)
                    286:   {
                    287:     Vector tableNameVec = new Vector();
                    288:     try
                    289:     {
                    290:       if (connection == null)
                    291:       {
                    292:         Vector vec = new Vector();
                    293:         vec.add("no database connection");
                    294:         return vec;
                    295:       }
                    296:       setConnection(url.substring(0, url.lastIndexOf("/") + 1) + catalog);
                    297:       if (dbMetaData == null)
                    298:         dbMetaData = connection.getMetaData();
                    299:       System.out.println("catalog " + catalog + " " + dbMetaData.getCatalogSeparator() + " " + url.substring(0, url.lastIndexOf("/") + 1));
                    300:       ResultSet tableNames = dbMetaData.getTables(null, null, null, null);
                    301:       // System.out.println(dbMetaData.supportsAlterTableWithAddColumn());
                    302:       // iterate over the table names and add them to the root node of the tree
                    303: 
                    304:       while (tableNames.next())
                    305:       {
                    306:         String tableName = tableNames.getString("TABLE_NAME");
                    307:         tableNameVec.add(tableName);
                    308: 
                    309:       }
                    310:     } catch (Exception e)
                    311:     {
                    312:       e.printStackTrace();
                    313:     }
                    314:     return tableNameVec;
                    315:   }
                    316: 
                    317:   /**
                    318:    * returns a Vector containing the Catalog or an error message in the Vector
                    319:    */
                    320:   public Vector getCatalogs()
                    321:   {
                    322:     Vector tableNameVec = new Vector();
                    323:     try
                    324:     {
                    325:       if (connection == null)
                    326:       {
                    327:         Vector vec = new Vector();
                    328:         vec.add("no database connection");
                    329:         return vec;
                    330:       }
                    331:       if (dbMetaData == null)
                    332:         dbMetaData = connection.getMetaData();
                    333:       ResultSet tableNames = dbMetaData.getCatalogs();
                    334:       // System.out.println(dbMetaData.supportsAlterTableWithAddColumn());
                    335:       // iterate over the table names and add them to the root node of the tree
                    336:       System.out.println(tableNames.getMetaData().getColumnName(1));
                    337:       while (tableNames.next())
                    338:       {
                    339:         String tableName = tableNames.getString(1);
                    340:         tableNameVec.add(tableName);
                    341:         //tableName = tableNames.getString(1);
                    342:         //tableNameVec.add(tableName);
                    343: 
                    344:       }
                    345:     } catch (Exception e)
                    346:     {
                    347:       e.printStackTrace();
                    348:     }
                    349:     return tableNameVec;
                    350:   }
                    351: 
                    352:   /**
                    353:   * returns a Vector containing the layoutNames for the specified Table
                    354:   * if the database supports this otherwise Vector containing an empty String
                    355:   */
                    356: 
                    357:   public Vector getLayoutNames(String tableName) throws SQLException
                    358:   {
                    359:     Vector layouts = new Vector();
                    360:     if (dbMetaData instanceof DatabaseMetaDataExt)
                    361:       layouts.add("");
                    362:     if (dbMetaData == null)
                    363:       dbMetaData = connection.getMetaData();
                    364: 
                    365:     if (dbMetaData instanceof DatabaseMetaDataExt)
                    366:     {
                    367:       ResultSet layoutNames = ((DatabaseMetaDataExt) dbMetaData).getLayouts(null, null, tableName, null);
                    368: 
                    369:       // iterate over the layout names and add them to the "Layouts" node
                    370:       while (layoutNames.next())
                    371:         layouts.add(layoutNames.getString("LAYOUT_NAME"));
                    372:     }
                    373:     return layouts;
                    374:   }
                    375:   /**
                    376:    *   Returns the result for select * from table
                    377:    *   with maxHits = 500 default value
                    378:    */
                    379:   public Vector[] getQueryData(String table) throws SQLException
                    380:   {
                    381: 
                    382:     return getQueryData("SELECT * from " + quoteChar + table + quoteChar, maxHits);
                    383: 
                    384:   }
                    385: 
                    386:   /**
                    387:    *    Returns the result of the query
                    388:    *    or an Vector array of Vectors containing error messages
                    389:    */
                    390:   public Vector[] getQueryData(String query, FM2SQL.ProgressDialog dialog, int maxHits) throws SQLException
                    391:   {
                    392:     long timeStart = System.currentTimeMillis();
                    393:     ResultSet resultSet = null;
                    394:     if (connection == null)
                    395:     {
                    396:       Vector[] noData = new Vector[2];
                    397:       //System.out.println("Exception occured");
                    398:       noData[1] = new Vector();
                    399:       Vector vec2 = new Vector();
                    400:       noData[0] = new Vector();
                    401:       vec2.add("no Connection available");
                    402:       noData[0].add(vec2);
                    403:       noData[1].add("Exception occured! No results available");
                    404:       //noData[1].add("no Results were produced");
                    405: 
                    406:       return noData;
                    407:     }
                    408:     if (dialog != null)
                    409:       dialog.progress.setValue(0);
                    410: 
                    411:     resultSet = makeQuery(query, maxHits);
                    412: 
                    413:     metaData = resultSet.getMetaData();
                    414:     int columnCount = metaData.getColumnCount();
                    415:     int rowCount = 0;
                    416:     if (maxHits == 0)
                    417:       rowCount = (metaData instanceof ResultSetMetaDataExt) ? 1000 : getRowCount(query);
                    418:     else
                    419:       rowCount = maxHits;
                    420:     int counter = 0;
                    421:     Vector tableData = new Vector();
                    422:     Vector tableRow = new Vector();
                    423:     //  System.out.println("rowCount "+rowCount+" "+maxHits);
                    424:     try
                    425:     {
                    426:       while ((tableRow = getNextRow()) != null)
                    427:       {
                    428:         counter++;
                    429:         if (dialog != null)
                    430:           dialog.progress.setValue((int) ((double) counter / (double) rowCount * 100.0));
                    431: 
                    432:         tableData.add(tableRow);
                    433: 
                    434:       }
                    435:     } catch (Exception e)
                    436:     {
                    437:       // TODO Auto-generated catch block
                    438:       e.printStackTrace();
                    439:     }
                    440: 
                    441:     // retrieve the column names from the result set; the column names
                    442:     // are used for the table header
                    443:     columnNames = new Vector();
                    444: 
                    445:     for (int i = 1; i <= columnCount; i++)
                    446:       columnNames.addElement(metaData.getColumnName(i));
                    447:     Vector data[] = new Vector[2];
                    448:     data[0] = tableData;
                    449:     data[1] = columnNames;
                    450:     System.out.println("Rows " + tableData.size() + " " + ((Vector) tableData.get(0)).size());
                    451:     long timeEnd = System.currentTimeMillis();
                    452:     System.out.println("Time needed for query and data retrieval " + (timeEnd - timeStart) + " ms");
                    453:     return data;
                    454:   }
                    455:   /**
                    456:    *    Returns the result of the query
                    457:    *    or an Vector array of Vectors containing error messages
                    458:    */
                    459:   public Vector[] getQueryData(String query, int maxHits) throws SQLException
                    460:   {
                    461:     long timeStart = System.currentTimeMillis();
                    462:     ResultSet resultSet = null;
                    463:     if (connection == null)
                    464:     {
                    465:       Vector[] noData = new Vector[2];
                    466:       //System.out.println("Exception occured");
                    467:       noData[1] = new Vector();
                    468:       Vector vec2 = new Vector();
                    469:       noData[0] = new Vector();
                    470:       vec2.add("no Connection available");
                    471:       noData[0].add(vec2);
                    472:       noData[1].add("Exception occured! No results available");
                    473:       //noData[1].add("no Results were produced");
                    474: 
                    475:       return noData;
                    476:     }
                    477:     resultSet = makeQuery(query, maxHits);
                    478:     metaData = resultSet.getMetaData();
                    479:     int columnCount = metaData.getColumnCount();
                    480: 
                    481:     Vector tableData = new Vector();
                    482:     while (resultSet.next())
                    483:     {
                    484:       //System.out.println("datatype "+(Types.LONGVARCHAR ==metaData.getColumnType(3)));
                    485:       Vector tableRow = new Vector(), m_columnClasses = new Vector();
                    486:       for (int i = 1; i <= columnCount; i++)
                    487:       {
                    488:         // repeating fields and fields from related databases may contain
                    489:         // multliple data values; the data values are stored using
                    490:         // a Vector which is then added to the tableRow
                    491:         //      if (metaData instanceof ResultSetMetaDataExt)
                    492:         if ((metaData instanceof ResultSetMetaDataExt) && (((ResultSetMetaDataExt) metaData).isRelated(i) || ((ResultSetMetaDataExt) metaData).isRepeating(i)))
                    493:         {
                    494:           //System.out.println("Related fields");
                    495:           // retrieve the repeating or related field contents as a
                    496:           // com.fmi.jdbc.Array via the ResultSet.getObject method
                    497:           com.fmi.jdbc.Array array = (com.fmi.jdbc.Array) resultSet.getObject(i);
                    498:           //            create a Vector for storing all of the data values
                    499:           ArrayList columnData = new ArrayList();
                    500: 
                    501:           try
                    502:           {
                    503: 
                    504:             // call the Array.getStringArray method since the data will
                    505:             // only be displayed
                    506:             Object[] fieldData = (Object[]) array.getArray();
                    507: 
                    508:             if (fieldData != null)
                    509:             {
                    510:               // add each value to the Vector
                    511:               for (int j = 0; j < fieldData.length; j++)
                    512:               {
                    513:                 if (fieldData[j] != null)
                    514:                   columnData.add(fieldData[j]);
                    515:               }
                    516: 
                    517:             }
                    518:           } catch (Exception e)
                    519:           {
                    520:             //System.out.println(e);
                    521:           }
                    522:           if (columnData.isEmpty())
                    523:             tableRow.add(null);
                    524:           else
                    525:             tableRow.addElement(columnData);
                    526:           //System.out.println(columnData);
                    527:           //System.out.println("Related fields"+columnData.size()+" "+tableRow.size());
                    528: 
                    529:           m_columnClasses.addElement(java.util.Vector.class);
                    530:         } else if (metaData.getColumnType(i) == Types.LONGVARBINARY)
                    531:         {
                    532:           // use the ResultSet.getObject method for retrieving images
                    533:           // from FileMaker Pro container fields; the ResultSet.getObject
                    534:           // method returns a java.awt.Image object for FileMaker Pro
                    535:           // container fields
                    536:           try
                    537:           {
                    538: 
                    539:             tableRow.addElement(resultSet.getObject(i));
                    540:           } catch (Exception e)
                    541:           {
                    542:             // TODO Auto-generated catch block
                    543:             //e.printStackTrace();
                    544:             tableRow.addElement(null);
                    545:           }
                    546:           //tableRow.addElement("Picture ignored");
                    547:           m_columnClasses.addElement(java.awt.Image.class);
                    548:         } else if (metaData.getColumnType(i) == Types.TIME)
                    549:         {
                    550:           // use the ResultSet.getObject method for retieving images
                    551:           // from FileMaker Pro container fields; the ResultSet.getObject
                    552:           // method returns a java.awt.Image object for FileMaker Pro
                    553:           // container fields
                    554:           try
                    555:           {
                    556:             tableRow.addElement(resultSet.getTime(i).toString());
                    557:             m_columnClasses.addElement(java.sql.Time.class);
                    558:           } catch (Exception e)
                    559:           {
                    560: 
                    561:             String value = resultSet.getString(i);
                    562:             if (value != null)
                    563:             {
                    564:               //System.out.println("SQLTime new "+Time.valueOf("17:00:00").toString());
                    565:               int index = 0;
                    566:               for (int j = 0; j < value.length(); ++j)
                    567:               {
                    568:                 if (!Character.isLetter(value.charAt(j)))
                    569:                   index = j + 1;
                    570:                 else
                    571:                   break;
                    572:               }
                    573: 
                    574:               tableRow.addElement(value.substring(0, index));
                    575:               //m_columnClasses.addElement(java.sql.Time.class);
                    576:             } else
                    577:               tableRow.add(null);
                    578:             m_columnClasses.addElement(String.class);
                    579:           } // to catch
                    580: 
                    581:         } else if (metaData.getColumnType(i) == Types.DATE)
                    582:         {
                    583:           // use the ResultSet.getObject method for retieving images
                    584:           // from FileMaker Pro container fields; the ResultSet.getObject
                    585:           // method returns a java.awt.Image object for FileMaker Pro
                    586:           // container fields
                    587: 
                    588:           tableRow.addElement(resultSet.getDate(i));
                    589:           m_columnClasses.addElement(java.sql.Date.class);
                    590:         } else if (metaData.getColumnTypeName(i) == "NUMBER")
                    591:         {
                    592:           // use the ResultSet.getObject method for retieving images
                    593:           // from FileMaker Pro container fields; the ResultSet.getObject
                    594:           // method returns a java.awt.Image object for FileMaker Pro
                    595:           // container fields
                    596:           try
                    597:           {
1.13      rogo      598:             tableRow.addElement(new Double(resultSet.getDouble(i)));
                    599:             m_columnClasses.addElement(Double.class);
1.1       rogo      600: 
                    601:           } catch (Exception e)
                    602:           {
                    603: 
                    604:             StringBuffer number = new StringBuffer();
                    605:             String value = resultSet.getString(i);
                    606:             System.out.println(value);
                    607:             for (int c = 0; c < value.length(); ++c)
                    608:             {
                    609:               if (Character.isDigit(value.charAt(c)))
                    610:               {
                    611:                 number.append(value.charAt(c));
                    612:               }
                    613:             }
                    614:             if (number.length() > 0)
                    615:             {
                    616:               tableRow.addElement(null);
1.13      rogo      617:               m_columnClasses.addElement(Double.class);
1.1       rogo      618:             } else
                    619:               tableRow.addElement(null);
                    620:           }
                    621:         } else
                    622:         {
                    623:           // all other field values are retrieved as strings and
                    624:           // added to the tableRow Vector
                    625:           // if(resultSet.getObject(i)!=null) System.out.println(resultSet.getObject(i));
                    626:           try
                    627:           {
                    628:             byte[] b = resultSet.getBytes(i);
                    629:             String utf8 = null;
                    630:             if (metaData instanceof ResultSetMetaDataExt)
                    631:               utf8 = (b == null) ? null : new String(b);
                    632:             else
                    633:               utf8 = (b == null) ? null : new String(b, "UTF-8");
                    634:             utf8 = (utf8 == null) ? null : new String(utf8.getBytes("UTF-8"), "UTF-8");
                    635:             tableRow.addElement(utf8);
                    636:           } catch (Exception e)
                    637:           {
                    638:             System.out.println("Hey I Got an error" + e);
                    639:           }
                    640:           m_columnClasses.addElement(java.lang.String.class);
                    641:         }
                    642:       }
                    643: 
                    644:       // add the tableRow Vector to the tableData Vector
                    645:       tableData.addElement(tableRow);
                    646:     }
                    647: 
                    648:     // retrieve the column names from the result set; the column names
                    649:     // are used for the table header
                    650:     columnNames = new Vector();
                    651: 
                    652:     for (int i = 1; i <= columnCount; i++)
                    653:       columnNames.addElement(metaData.getColumnName(i));
                    654:     Vector data[] = new Vector[2];
                    655:     data[0] = tableData;
                    656:     data[1] = columnNames;
                    657:     System.out.println("Rows " + tableData.size() + " " + ((Vector) tableData.get(0)).size());
                    658:     long timeEnd = System.currentTimeMillis();
                    659:     System.out.println("Time needed for query and data retrieval " + (timeEnd - timeStart) + " ms");
                    660:     return data;
                    661:   }
                    662: 
                    663:   public Vector getColumnNames()
                    664:   {
                    665:     if (result == null)
                    666:       return null;
                    667:     try
                    668:     {
                    669:       ResultSetMetaData metaData = result.getMetaData();
                    670:       int columnCount = metaData.getColumnCount();
                    671:       columnNames = new Vector();
                    672: 
                    673:       for (int i = 1; i <= columnCount; i++)
                    674:         columnNames.addElement(metaData.getColumnName(i));
                    675:     } catch (Exception e)
                    676:     {
                    677:     }
                    678:     return columnNames;
                    679:   }
                    680:   /**
                    681:    * makes the database Query
                    682:    *   with the numberOfHits as maximum
                    683:    * @return the result as an ResultSet object
                    684:   */
                    685:   public ResultSet makeQuery(String query, int numberOfHits) throws SQLException
                    686:   {
                    687:     result = null;
                    688:     Statement stm = null;
1.2       rogo      689:    
1.1       rogo      690:     //  System.out.println("Query " + query);
                    691: 
                    692:     if (!connection.isClosed())
                    693:       stm = connection.createStatement();
1.12      rogo      694:     else {
                    695:       
                    696:       
                    697:       try
                    698:       {
                    699:         connection = getConnection();
                    700:         stm= connection.createStatement();
                    701:       } catch (Exception e)
                    702:       {
                    703:         // TODO Auto-generated catch block
                    704:         e.printStackTrace();
                    705:       }
                    706:     }
1.1       rogo      707:     stm.setMaxRows(numberOfHits);
1.2       rogo      708:     long time = System.currentTimeMillis();
1.17      rogo      709:     try {
1.2       rogo      710:     stm.execute(query);
1.17      rogo      711:     } catch(Exception e) {
                    712:      // TODO remove
1.18    ! rogo      713:     // if(FM2SQL.fmInstance!=null)
        !           714:   //   FM2SQL.showErrorDialog("Error caught!! \n Query was  "+query+" \n","Debug Info");
1.17      rogo      715:     }
1.2       rogo      716:     long time2 = System.currentTimeMillis();
                    717:    
                    718:     System.out.println("time to execute "+(time2-time));
                    719:    // stm.setMaxRows(numberOfHits);
                    720:  
                    721:     result = stm.getResultSet();
                    722:    // System.out.println(result+" "+stm.getUpdateCount());
1.1       rogo      723:     metaData = result.getMetaData();
                    724:     return result;
                    725:   }
                    726:   /**
                    727:    *  sets the database user
                    728:    */
                    729:   public void setUser(String user)
                    730:   {
                    731:     this.user = user;
                    732:   }
                    733:   /**
                    734:    *     sets the database passwd
                    735:    */
                    736:   public void setPasswd(String passwd)
                    737:   {
                    738:     this.passwd = passwd;
                    739:   }
                    740: 
                    741:   /**
                    742:    * sets the database user and passwd
                    743:    */
                    744:   public void setUserAndPasswd(String user, String passwd)
                    745:   {
                    746:     this.user = user;
                    747:     this.passwd = passwd;
                    748: 
                    749:   }
                    750:   /**
                    751:    *  just  sets the connection URL
                    752:    */
                    753:   public void setURL(String url)
                    754:   {
                    755:     this.url = url;
                    756:   }
                    757: 
                    758:   /**
                    759:    *    Test the database drivers features given by the DatabaseMetaData object
                    760:    */
                    761:   public Vector[] TestDB(DatabaseMetaData d) throws SQLException
                    762:   {
                    763: 
                    764:     Vector data[] = new Vector[2];
                    765:     Vector[] rows = new Vector[120];
                    766:     for (int i = 0; i < rows.length; ++i)
                    767:       rows[i] = new Vector();
                    768:     Vector columnNames = new Vector();
                    769:     columnNames.add("Feature");
                    770:     columnNames.add("Supported");
                    771: 
                    772:     Vector cols = new Vector();
                    773:     rows[0].add("allProceduresAreCallable");
                    774:     rows[0].add(new Boolean(d.allProceduresAreCallable()));
                    775:     //boolean allProceduresAreCallable() throws SQLException;
                    776:     rows[1].add("allTablesAreSelectable");
                    777:     rows[1].add(new Boolean(d.allTablesAreSelectable()));
                    778:     //    boolean allTablesAreSelectable() throws SQLException;
                    779:     rows[2].add("isReadOnly");
                    780:     rows[2].add(new Boolean(d.isReadOnly()));
                    781:     //    boolean isReadOnly() throws SQLException;
                    782:     rows[3].add("nullsAreSortedHigh");
                    783:     rows[3].add(new Boolean(d.nullsAreSortedHigh()));
                    784:     //    boolean nullsAreSortedHigh() throws SQLException;
                    785:     rows[4].add("nullsAreSortedLow");
                    786:     rows[4].add(new Boolean(d.nullsAreSortedLow()));
                    787:     // boolean nullsAreSortedLow() throws SQLException;
                    788:     rows[5].add("nullsAreSortedAtStart");
                    789:     rows[5].add(new Boolean(d.nullsAreSortedAtStart()));
                    790:     //  boolean nullsAreSortedAtStart() throws SQLException;
                    791:     rows[6].add("nullsAreSortedAtEnd");
                    792:     rows[6].add(new Boolean(d.nullsAreSortedAtEnd()));
                    793:     //  boolean nullsAreSortedAtEnd() throws SQLException;
                    794:     rows[7].add("usesLocalFiles");
                    795:     rows[7].add(new Boolean(d.usesLocalFiles()));
                    796:     //    boolean usesLocalFiles() throws SQLException;
                    797:     rows[8].add("usesLocalFilePerTable");
                    798:     rows[8].add(new Boolean(d.usesLocalFilePerTable()));
                    799:     // boolean usesLocalFilePerTable() throws SQLException;
                    800:     rows[9].add("supportsMixedCaseIdentifiers");
                    801:     rows[9].add(new Boolean(d.supportsMixedCaseIdentifiers()));
                    802:     //boolean supportsMixedCaseIdentifiers() throws SQLException;
                    803:     rows[10].add("storesUpperCaseIdentifiers");
                    804:     rows[10].add(new Boolean(d.storesUpperCaseIdentifiers()));
                    805:     // boolean storesUpperCaseIdentifiers() throws SQLException;
                    806:     rows[11].add("storesLowerCaseIdentifiers");
                    807:     rows[11].add(new Boolean(d.storesLowerCaseIdentifiers()));
                    808:     //    boolean storesLowerCaseIdentifiers() throws SQLException;
                    809:     rows[12].add("storesMixedCaseIdentifiers");
                    810:     rows[12].add(new Boolean(d.storesMixedCaseIdentifiers()));
                    811:     //    boolean storesMixedCaseIdentifiers() throws SQLException;
                    812:     rows[13].add("supportsMixedCaseQuotedIdentifiers");
                    813:     rows[13].add(new Boolean(d.supportsMixedCaseQuotedIdentifiers()));
                    814:     //    boolean supportsMixedCaseQuotedIdentifiers() throws SQLException;
                    815:     rows[14].add("storesUpperCaseQuotedIdentifiers");
                    816:     rows[14].add(new Boolean(d.storesUpperCaseQuotedIdentifiers()));
                    817:     //   boolean storesUpperCaseQuotedIdentifiers() throws SQLException;
                    818:     rows[15].add("storesLowerCaseQuotedIdentifiers");
                    819:     rows[15].add(new Boolean(d.storesLowerCaseQuotedIdentifiers()));
                    820:     //boolean storesLowerCaseQuotedIdentifiers() throws SQLException;
                    821:     rows[16].add("storesMixedCaseQuotedIdentifiers");
                    822:     rows[16].add(new Boolean(d.storesMixedCaseQuotedIdentifiers()));
                    823:     // boolean storesMixedCaseQuotedIdentifiers() throws SQLException;
                    824:     rows[17].add("supportsAlterTableWithAddColumn");
                    825:     rows[17].add(new Boolean(d.supportsAlterTableWithAddColumn()));
                    826:     //    boolean supportsAlterTableWithAddColumn() throws SQLException;
                    827:     rows[18].add("supportsAlterTableWithDropColumn");
                    828:     rows[18].add(new Boolean(d.supportsAlterTableWithDropColumn()));
                    829:     //  boolean supportsAlterTableWithDropColumn() throws SQLException;
                    830:     rows[19].add("nullPlusNonNullIsNull");
                    831:     rows[19].add(new Boolean(d.nullPlusNonNullIsNull()));
                    832:     //   boolean nullPlusNonNullIsNull() throws SQLException;
                    833:     rows[20].add("supportsConvert");
                    834:     rows[20].add(new Boolean(d.supportsConvert()));
                    835:     // boolean supportsConvert() throws SQLException;
                    836: 
                    837:     // boolean supportsConvert(int fromType, int toType) throws SQLException;
                    838:     rows[21].add("supportsTableCorrelationNames");
                    839:     rows[21].add(new Boolean(d.supportsTableCorrelationNames()));
                    840:     //  boolean supportsTableCorrelationNames() throws SQLException;
                    841:     rows[22].add("supportsDifferentTableCorrelationNames");
                    842:     rows[22].add(new Boolean(d.supportsDifferentTableCorrelationNames()));
                    843:     // boolean supportsDifferentTableCorrelationNames() throws SQLException;
                    844:     rows[23].add("supportsExpressionsInOrderBy");
                    845:     rows[23].add(new Boolean(d.supportsExpressionsInOrderBy()));
                    846:     // boolean supportsExpressionsInOrderBy() throws SQLException;
                    847:     rows[24].add("supportsOrderByUnrelated");
                    848:     rows[24].add(new Boolean(d.supportsOrderByUnrelated()));
                    849:     //   boolean supportsOrderByUnrelated() throws SQLException;
                    850:     rows[25].add("supportsGroupBy");
                    851:     rows[25].add(new Boolean(d.supportsGroupBy()));
                    852:     //  boolean supportsGroupBy() throws SQLException;
                    853:     rows[26].add("supportsGroupByUnrelated");
                    854:     rows[26].add(new Boolean(d.supportsGroupByUnrelated()));
                    855:     // boolean supportsGroupByUnrelated() throws SQLException;
                    856:     rows[27].add("supportsGroupByBeyondSelect");
                    857:     rows[27].add(new Boolean(d.supportsGroupByBeyondSelect()));
                    858:     //  boolean supportsGroupByBeyondSelect() throws SQLException;
                    859:     rows[28].add("supportsLikeEscapeClause");
                    860:     rows[28].add(new Boolean(d.supportsLikeEscapeClause()));
                    861:     // boolean supportsLikeEscapeClause() throws SQLException;
                    862:     rows[29].add("supportsMultipleResultSets");
                    863:     rows[29].add(new Boolean(d.supportsMultipleResultSets()));
                    864:     // boolean supportsMultipleResultSets() throws SQLException;
                    865:     rows[30].add("supportsMultipleTransactions");
                    866:     rows[30].add(new Boolean(d.supportsMultipleTransactions()));
                    867:     //  boolean supportsMultipleTransactions() throws SQLException;
                    868:     rows[31].add("supportsNonNullableColumns");
                    869:     rows[31].add(new Boolean(d.supportsNonNullableColumns()));
                    870:     //    boolean supportsNonNullableColumns() throws SQLException;
                    871:     rows[32].add("supportsMinimumSQLGrammar");
                    872:     rows[32].add(new Boolean(d.supportsMinimumSQLGrammar()));
                    873:     // boolean supportsMinimumSQLGrammar() throws SQLException;
                    874:     rows[33].add("supportsCoreSQLGrammar");
                    875:     rows[33].add(new Boolean(d.supportsCoreSQLGrammar()));
                    876:     // boolean supportsCoreSQLGrammar() throws SQLException;
                    877:     rows[34].add("supportsExtendedSQLGrammar");
                    878:     rows[34].add(new Boolean(d.supportsExtendedSQLGrammar()));
                    879:     // boolean supportsExtendedSQLGrammar() throws SQLException;
                    880:     rows[35].add("supportsANSI92EntryLevelSQL");
                    881:     rows[35].add(new Boolean(d.supportsANSI92EntryLevelSQL()));
                    882:     // boolean supportsANSI92EntryLevelSQL() throws SQLException;
                    883:     rows[36].add("supportsANSI92IntermediateSQL");
                    884:     rows[36].add(new Boolean(d.supportsANSI92IntermediateSQL()));
                    885:     //boolean supportsANSI92IntermediateSQL() throws SQLException;
                    886:     rows[37].add("supportsANSI92FullSQL");
                    887:     rows[37].add(new Boolean(d.supportsANSI92FullSQL()));
                    888:     //boolean supportsANSI92FullSQL() throws SQLException;
                    889:     rows[38].add("supportsIntegrityEnhancementFacility");
                    890:     rows[38].add(new Boolean(d.supportsIntegrityEnhancementFacility()));
                    891:     //boolean supportsIntegrityEnhancementFacility() throws SQLException;
                    892:     rows[39].add("supportsOuterJoins");
                    893:     rows[39].add(new Boolean(d.supportsOuterJoins()));
                    894:     //boolean supportsOuterJoins() throws SQLException;
                    895:     rows[40].add("supportsFullOuterJoins");
                    896:     rows[40].add(new Boolean(d.supportsFullOuterJoins()));
                    897:     //boolean supportsFullOuterJoins() throws SQLException;
                    898:     rows[41].add("supportsLimitedOuterJoins");
                    899:     rows[41].add(new Boolean(d.supportsLimitedOuterJoins()));
                    900:     //boolean supportsLimitedOuterJoins() throws SQLException;
                    901:     rows[42].add("isCatalogAtStart");
                    902:     rows[42].add(new Boolean(d.isCatalogAtStart()));
                    903:     //boolean isCatalogAtStart() throws SQLException;
                    904:     rows[43].add("supportsSchemasInDataManipulation");
                    905:     rows[43].add(new Boolean(d.supportsSchemasInDataManipulation()));
                    906:     //boolean supportsSchemasInDataManipulation() throws SQLException;
                    907:     rows[44].add("supportsSchemasInProcedureCalls");
                    908:     rows[44].add(new Boolean(d.supportsSchemasInProcedureCalls()));
                    909:     //boolean supportsSchemasInProcedureCalls() throws SQLException;
                    910:     rows[45].add("supportsSchemasInTableDefinitions");
                    911:     rows[45].add(new Boolean(d.supportsSchemasInTableDefinitions()));
                    912:     //boolean supportsSchemasInTableDefinitions() throws SQLException;
                    913:     rows[46].add("supportsSchemasInIndexDefinitions");
                    914:     rows[46].add(new Boolean(d.supportsSchemasInIndexDefinitions()));
                    915:     //boolean supportsSchemasInIndexDefinitions() throws SQLException;
                    916:     rows[47].add("supportsSchemasInPrivilegeDefinitions");
                    917:     rows[47].add(new Boolean(d.supportsSchemasInPrivilegeDefinitions()));
                    918:     //boolean supportsSchemasInPrivilegeDefinitions() throws SQLException;
                    919:     rows[48].add("supportsCatalogsInDataManipulation");
                    920:     rows[48].add(new Boolean(d.supportsCatalogsInDataManipulation()));
                    921:     //boolean supportsCatalogsInDataManipulation() throws SQLException;
                    922:     rows[49].add("supportsCatalogsInProcedureCalls");
                    923:     rows[49].add(new Boolean(d.supportsCatalogsInProcedureCalls()));
                    924:     //boolean supportsCatalogsInProcedureCalls() throws SQLException;
                    925:     rows[50].add("supportsCatalogsInTableDefinitions");
                    926:     rows[50].add(new Boolean(d.supportsCatalogsInTableDefinitions()));
                    927:     //boolean supportsCatalogsInTableDefinitions() throws SQLException;
                    928:     rows[51].add("supportsCatalogsInIndexDefinitions");
                    929:     rows[51].add(new Boolean(d.supportsCatalogsInIndexDefinitions()));
                    930:     //boolean supportsCatalogsInIndexDefinitions() throws SQLException;
                    931:     rows[52].add("supportsCatalogsInPrivilegeDefinitions");
                    932:     rows[52].add(new Boolean(d.supportsCatalogsInPrivilegeDefinitions()));
                    933:     //boolean supportsCatalogsInPrivilegeDefinitions() throws SQLException;
                    934:     rows[53].add("supportsPositionedDelete");
                    935:     rows[53].add(new Boolean(d.supportsPositionedDelete()));
                    936:     //boolean supportsPositionedDelete() throws SQLException;
                    937:     rows[54].add("supportsPositionedUpdate");
                    938:     rows[54].add(new Boolean(d.supportsPositionedUpdate()));
                    939:     //boolean supportsPositionedUpdate() throws SQLException;
                    940:     rows[55].add("supportsSelectForUpdate");
                    941:     rows[55].add(new Boolean(d.supportsSelectForUpdate()));
                    942:     //boolean supportsSelectForUpdate() throws SQLException;
                    943:     rows[56].add("supportsStoredProcedures");
                    944:     rows[56].add(new Boolean(d.supportsStoredProcedures()));
                    945:     //boolean supportsStoredProcedures() throws SQLException;
                    946:     rows[57].add("supportsSubqueriesInComparisons");
                    947:     rows[57].add(new Boolean(d.supportsSubqueriesInComparisons()));
                    948:     //boolean supportsSubqueriesInComparisons() throws SQLException;
                    949:     rows[58].add("supportsSubqueriesInExists");
                    950:     rows[58].add(new Boolean(d.supportsSubqueriesInExists()));
                    951:     //boolean supportsSubqueriesInExists() throws SQLException;
                    952:     rows[59].add("supportsSubqueriesInIns");
                    953:     rows[59].add(new Boolean(d.supportsSubqueriesInIns()));
                    954:     //boolean supportsSubqueriesInIns() throws SQLException;
                    955:     rows[60].add("supportsSubqueriesInQuantifieds");
                    956:     rows[60].add(new Boolean(d.supportsSubqueriesInQuantifieds()));
                    957:     //boolean supportsSubqueriesInQuantifieds() throws SQLException;
                    958:     rows[61].add("supportsCorrelatedSubqueries");
                    959:     rows[61].add(new Boolean(d.supportsCorrelatedSubqueries()));
                    960:     //boolean supportsCorrelatedSubqueries() throws SQLException;
                    961:     rows[62].add("supportsUnion");
                    962:     rows[62].add(new Boolean(d.supportsUnion()));
                    963:     //boolean supportsUnion() throws SQLException;
                    964:     rows[63].add("supportsUnionAll");
                    965:     rows[63].add(new Boolean(d.supportsUnionAll()));
                    966:     //boolean supportsUnionAll() throws SQLException;
                    967:     rows[64].add("supportsOpenCursorsAcrossCommit");
                    968:     rows[64].add(new Boolean(d.supportsOpenCursorsAcrossCommit()));
                    969:     //boolean supportsOpenCursorsAcrossCommit() throws SQLException;
                    970:     rows[65].add("supportsOpenCursorsAcrossRollback");
                    971:     rows[65].add(new Boolean(d.supportsOpenCursorsAcrossRollback()));
                    972:     //boolean supportsOpenCursorsAcrossRollback() throws SQLException;
                    973:     rows[66].add("supportsOpenStatementsAcrossCommit");
                    974:     rows[66].add(new Boolean(d.supportsOpenStatementsAcrossCommit()));
                    975:     //boolean supportsOpenStatementsAcrossCommit() throws SQLException;
                    976:     rows[67].add("supportsOpenStatementsAcrossRollback");
                    977:     rows[67].add(new Boolean(d.supportsOpenStatementsAcrossRollback()));
                    978:     //boolean supportsOpenStatementsAcrossRollback() throws SQLException;
                    979:     rows[68].add("doesMaxRowSizeIncludeBlobs");
                    980:     rows[68].add(new Boolean(d.doesMaxRowSizeIncludeBlobs()));
                    981:     //boolean doesMaxRowSizeIncludeBlobs() throws SQLException;
                    982:     rows[69].add("supportsTransactions");
                    983:     rows[69].add(new Boolean(d.supportsTransactions()));
                    984:     //boolean supportsTransactions() throws SQLException;
                    985:     rows[70].add("supportsTransactionIsolationLevel");
                    986:     rows[70].add(new Boolean(d.supportsTransactionIsolationLevel(1)));
                    987:     //boolean supportsTransactionIsolationLevel(int level) throws SQLException;
                    988:     rows[71].add("supportsDataDefinitionAndDataManipulationTransactions");
                    989:     rows[71].add(new Boolean(d.supportsDataDefinitionAndDataManipulationTransactions()));
                    990:     //boolean supportsDataDefinitionAndDataManipulationTransactions() throws SQLException;
                    991:     rows[72].add("supportsDataManipulationTransactionsOnly");
                    992:     rows[72].add(new Boolean(d.supportsDataManipulationTransactionsOnly()));
                    993:     //boolean supportsDataManipulationTransactionsOnly() throws SQLException;
                    994:     rows[73].add("dataDefinitionCausesTransactionCommit");
                    995:     rows[73].add(new Boolean(d.dataDefinitionCausesTransactionCommit()));
                    996:     //boolean dataDefinitionCausesTransactionCommit() throws SQLException;
                    997:     rows[74].add("dataDefinitionIgnoredInTransactions");
                    998:     rows[74].add(new Boolean(d.dataDefinitionIgnoredInTransactions()));
                    999:     //boolean dataDefinitionIgnoredInTransactions() throws SQLException;
                   1000:     rows[75].add("getMaxBinaryLiteralLength");
                   1001:     rows[75].add(new Integer(d.getMaxBinaryLiteralLength()));
                   1002:     // int getMaxBinaryLiteralLength() throws SQLException;
                   1003:     rows[76].add("getMaxCharLiteralLength");
                   1004:     rows[76].add(new Integer(d.getMaxCharLiteralLength()));
                   1005:     //int getMaxCharLiteralLength() throws SQLException;
                   1006:     rows[77].add("getMaxColumnNameLength");
                   1007:     rows[77].add(new Integer(d.getMaxColumnNameLength()));
                   1008:     // int getMaxColumnNameLength() throws SQLException;
                   1009:     rows[78].add("getMaxColumnsInGroupBy");
                   1010:     rows[78].add(new Integer(d.getMaxColumnsInGroupBy()));
                   1011:     //int getMaxColumnsInGroupBy() throws SQLException;
                   1012:     rows[79].add("getMaxColumnsInIndex");
                   1013:     rows[79].add(new Integer(d.getMaxColumnsInIndex()));
                   1014:     //int getMaxColumnsInIndex() throws SQLException;
                   1015:     rows[80].add("getMaxColumnsInOrderBy");
                   1016:     rows[80].add(new Integer(d.getMaxColumnsInOrderBy()));
                   1017:     //int getMaxColumnsInOrderBy() throws SQLException;
                   1018:     rows[81].add("getMaxColumnsInSelect");
                   1019:     rows[81].add(new Integer(d.getMaxColumnsInSelect()));
                   1020:     //int getMaxColumnsInSelect() throws SQLException;
                   1021:     rows[82].add("getMaxColumnsInTable");
                   1022:     rows[82].add(new Integer(d.getMaxColumnsInTable()));
                   1023:     //int getMaxColumnsInTable() throws SQLException;
                   1024:     rows[83].add("getMaxConnections");
                   1025:     rows[83].add(new Integer(d.getMaxConnections()));
                   1026:     //int getMaxConnections() throws SQLException;
                   1027:     rows[84].add("getMaxCursorNameLength");
                   1028:     rows[84].add(new Integer(d.getMaxCursorNameLength()));
                   1029:     //    int getMaxCursorNameLength() throws SQLException;
                   1030:     rows[85].add("getMaxIndexLength");
                   1031:     rows[85].add(new Integer(d.getMaxIndexLength()));
                   1032:     //int getMaxIndexLength() throws SQLException;
                   1033:     rows[86].add("getMaxSchemaNameLength");
                   1034:     rows[86].add(new Integer(d.getMaxSchemaNameLength()));
                   1035:     //int getMaxSchemaNameLength() throws SQLException;
                   1036:     rows[87].add("getMaxProcedureNameLength");
                   1037:     rows[87].add(new Integer(d.getMaxProcedureNameLength()));
                   1038:     //int getMaxProcedureNameLength() throws SQLException;
                   1039:     rows[88].add("getMaxCatalogNameLength");
                   1040:     rows[88].add(new Integer(d.getMaxCatalogNameLength()));
                   1041:     //int getMaxCatalogNameLength() throws SQLException;
                   1042:     rows[89].add("getMaxRowSize");
                   1043:     rows[89].add(new Integer(d.getMaxRowSize()));
                   1044:     //int getMaxRowSize() throws SQLException;
                   1045:     rows[90].add("getMaxStatementLength");
                   1046:     rows[90].add(new Integer(d.getMaxStatementLength()));
                   1047:     //int getMaxStatementLength() throws SQLException;
                   1048:     rows[91].add("getMaxStatements");
                   1049:     rows[91].add(new Integer(d.getMaxStatements()));
                   1050:     //int getMaxStatements() throws SQLException;
                   1051:     rows[92].add("getMaxTableNameLength");
                   1052:     rows[92].add(new Integer(d.getMaxTableNameLength()));
                   1053:     //int getMaxTableNameLength() throws SQLException;
                   1054:     rows[93].add("getMaxTablesInSelect");
                   1055:     rows[93].add(new Integer(d.getMaxTablesInSelect()));
                   1056:     //int getMaxTablesInSelect() throws SQLException;
                   1057:     rows[94].add("getMaxUserNameLength");
                   1058:     rows[94].add(new Integer(d.getMaxUserNameLength()));
                   1059:     // int getMaxUserNameLength() throws SQLException;
                   1060:     rows[95].add("getDefaultTransactionIsolation");
                   1061:     rows[95].add(new Integer(d.getDefaultTransactionIsolation()));
                   1062:     //int getDefaultTransactionIsolation() throws SQLException;
                   1063: 
                   1064:     rows[96].add("getDatabaseProductName");
                   1065:     rows[96].add(d.getDatabaseProductName());
                   1066:     // String getDatabaseProductName() throws SQLException;
                   1067:     rows[97].add("getDatabaseProductVersion");
                   1068:     rows[97].add(d.getDatabaseProductVersion());
                   1069:     //String getDatabaseProductVersion() throws SQLException;
                   1070: 
                   1071:     rows[98].add("getURL");
                   1072:     rows[98].add(d.getURL());
                   1073:     //String getURL() throws SQLException;
                   1074:     rows[99].add("getUserName");
                   1075:     rows[99].add(d.getUserName());
                   1076:     //String getUserName() throws SQLException;
                   1077:     rows[100].add("getDriverName");
                   1078:     rows[100].add(d.getDriverName());
                   1079:     //    String getDriverName() throws SQLException;
                   1080:     rows[101].add("getIdentifierQuoteString");
                   1081:     rows[101].add(d.getIdentifierQuoteString());
                   1082:     //String getIdentifierQuoteString() throws SQLException;
                   1083: 
                   1084:     rows[102].add("getDriverVersion");
                   1085:     rows[102].add(d.getDriverVersion());
                   1086:     //String getDriverVersion() throws SQLException;
                   1087:     rows[103].add("getDriverMajorVersion");
                   1088:     rows[103].add(new Integer(d.getDriverMajorVersion()));
                   1089:     //int getDriverMajorVersion();
                   1090:     rows[104].add("getDriverMinorVersion");
                   1091:     rows[104].add(new Integer(d.getDriverMinorVersion()));
                   1092:     //int getDriverMinorVersion();
                   1093:     rows[105].add("getSQLKeywords");
                   1094:     rows[105].add(d.getSQLKeywords());
                   1095:     //String getSQLKeywords() throws SQLException;
                   1096:     rows[106].add("getNumericFunctions");
                   1097:     rows[106].add(d.getNumericFunctions());
                   1098:     //String getNumericFunctions() throws SQLException;
                   1099:     rows[107].add("getStringFunctions");
                   1100:     rows[107].add(d.getStringFunctions());
                   1101:     // String getStringFunctions() throws SQLException;
                   1102:     rows[108].add("getSystemFunctions");
                   1103:     rows[108].add(d.getSystemFunctions());
                   1104:     //String getSystemFunctions() throws SQLException;
                   1105:     rows[109].add("getTimeDateFunctions");
                   1106:     rows[109].add(d.getTimeDateFunctions());
                   1107:     //String getTimeDateFunctions() throws SQLException;
                   1108:     rows[110].add("getSearchStringEscape");
                   1109:     rows[110].add(d.getSearchStringEscape());
                   1110:     //String getSearchStringEscape() throws SQLException;
                   1111:     rows[111].add("getExtraNameCharacters");
                   1112:     rows[111].add(d.getExtraNameCharacters());
                   1113:     //String getExtraNameCharacters() throws SQLException;
                   1114:     rows[112].add("getSchemaTerm");
                   1115:     rows[112].add(d.getSchemaTerm());
                   1116:     //String getSchemaTerm() throws SQLException;
                   1117:     rows[113].add("getProcedureTerm");
                   1118:     rows[113].add(d.getProcedureTerm());
                   1119:     //String getProcedureTerm() throws SQLException;
                   1120:     rows[114].add("getCatalogTerm");
                   1121:     rows[114].add(d.getCatalogTerm());
                   1122:     // String getCatalogTerm() throws SQLException;
                   1123:     rows[115].add("getCatalogSeparator");
                   1124:     rows[115].add(d.getCatalogSeparator());
                   1125:     //String getCatalogSeparator() throws SQLException;
                   1126: 
                   1127:     /*
                   1128:      boolean supportsResultSetType(int type) throws SQLException;
                   1129:     
                   1130:      boolean supportsResultSetConcurrency(int type, int concurrency) throws SQLException;
                   1131:     
                   1132:      boolean ownUpdatesAreVisible(int type) throws SQLException;
                   1133:     
                   1134:      boolean ownDeletesAreVisible(int type) throws SQLException;
                   1135:     
                   1136:      boolean ownInsertsAreVisible(int type) throws SQLException;
                   1137:     
                   1138:      boolean othersUpdatesAreVisible(int type) throws SQLException;
                   1139:     
                   1140:      boolean othersDeletesAreVisible(int type) throws SQLException;
                   1141:     
                   1142:      boolean othersInsertsAreVisible(int type) throws SQLException;
                   1143:      boolean updatesAreDetected(int type) throws SQLException;
                   1144:      boolean deletesAreDetected(int type) throws SQLException;
                   1145:     
                   1146:      boolean insertsAreDetected(int type) throws SQLException;
                   1147:     */
                   1148:     // not in filemaker
                   1149:     // rows[96].add("supportsBatchUpdates");
                   1150:     // rows[96].add(new Boolean(d.supportsBatchUpdates()));
                   1151:     //boolean supportsBatchUpdates() throws SQLException;
                   1152: 
                   1153:     /*
                   1154:     ResultSet getProcedures(String catalog, String schemaPattern, String procedureNamePattern) throws SQLException;
                   1155:     
                   1156:     ResultSet getProcedureColumns(String catalog, String schemaPattern, String procedureNamePattern, String columnNamePattern) throws SQLException;
                   1157:     ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String types[]) throws SQLException;
                   1158:     ResultSet getSchemas() throws SQLException;
                   1159:     ResultSet getCatalogs() throws SQLException;
                   1160:     ResultSet getTableTypes() throws SQLException;
                   1161:     ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException;
                   1162:     ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException;
                   1163:     ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException;
                   1164:     ResultSet getExportedKeys(String catalog, String schema, String table) throws SQLException;
                   1165:     ResultSet getCrossReference(String primaryCatalog, String primarySchema, String primaryTable, String foreignCatalog, String foreignSchema, String foreignTable) throws SQLException;
                   1166:     ResultSet getTypeInfo() throws SQLException;
                   1167:     ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) throws SQLException;
                   1168:     
                   1169:     ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) throws SQLException;
                   1170:     
                   1171:     Connection getConnection() throws SQLException;
                   1172:     
                   1173:     */
                   1174:     for (int i = 0; i < rows.length; ++i)
                   1175:       cols.add(rows[i]);
                   1176:     data[0] = cols;
                   1177:     data[1] = columnNames;
                   1178:     return data;
                   1179:   }
                   1180:   public Vector getNextRow() throws Exception
                   1181:   {
                   1182: 
                   1183:     if (result == null)
                   1184:       return null;
                   1185:     boolean check = false;
                   1186:     ResultSet resultSet = result;
                   1187:     ResultSetMetaData metaData = resultSet.getMetaData();
                   1188:     int columnCount = metaData.getColumnCount();
                   1189:     Vector tableData = new Vector();
                   1190:     check = resultSet.next();
                   1191:     //  System.out.println("hallo check "+check);
                   1192:     if (!check)
                   1193:       return null;
                   1194:     Vector tableRow = new Vector(), m_columnClasses = new Vector();
                   1195:     for (int i = 1; i <= columnCount; i++)
                   1196:     {
                   1197:       // repeating fields and fields from related databases may contain
                   1198:       // multliple data values; the data values are stored using
                   1199:       // a Vector which is then added to the tableRow
                   1200:       //      if (metaData instanceof ResultSetMetaDataExt)
                   1201:       if ((metaData instanceof ResultSetMetaDataExt) && (((ResultSetMetaDataExt) metaData).isRelated(i) || ((ResultSetMetaDataExt) metaData).isRepeating(i)))
                   1202:       {
                   1203:         //System.out.println("Related fields");
                   1204:         // retrieve the repeating or related field contents as a
                   1205:         // com.fmi.jdbc.Array via the ResultSet.getObject method
                   1206:         com.fmi.jdbc.Array array = (com.fmi.jdbc.Array) resultSet.getObject(i);
                   1207:         //            create a Vector for storing all of the data values
                   1208:         ArrayList columnData = new ArrayList();
                   1209:         try
                   1210:         {
                   1211: 
                   1212:           // call the Array.getStringArray method since the data will
                   1213:           // only be displayed
                   1214:           Object[] fieldData = (Object[]) array.getArray();
                   1215: 
                   1216:           if (fieldData != null)
                   1217:           {
                   1218:             // add each value to the Vector
                   1219:             for (int j = 0; j < fieldData.length; j++)
                   1220:             {
                   1221:               if (fieldData[j] != null)
                   1222:                 columnData.add(fieldData[j]);
                   1223:             }
                   1224:           }
                   1225:         } catch (Exception e)
                   1226:         {
                   1227:           //System.out.println(e);
                   1228:         }
                   1229: 
                   1230:         if (columnData.isEmpty())
                   1231:           tableRow.add(null);
                   1232:         else
                   1233:           tableRow.addElement(columnData);
                   1234:         //System.out.println(columnData);
                   1235:         //System.out.println("Related fields"+columnData.size()+" "+tableRow.size());
                   1236: 
                   1237:         // m_columnClasses.addElement(java.util.Vector.class);
                   1238:       } else if (metaData.getColumnType(i) == Types.LONGVARBINARY)
                   1239:       {
                   1240:         // use the ResultSet.getObject method for retrieving images
                   1241:         // from FileMaker Pro container fields; the ResultSet.getObject
                   1242:         // method returns a java.awt.Image object for FileMaker Pro
                   1243:         // container fields
                   1244: 
                   1245:         try
                   1246:         {
                   1247:           tableRow.addElement(resultSet.getObject(i));
                   1248:         } catch (Exception e)
                   1249:         {
                   1250:           // TODO Auto-generated catch block
                   1251:           // e.printStackTrace();
                   1252:           tableRow.addElement(null);
                   1253:         }
                   1254:         //    m_columnClasses.addElement(java.awt.Image.class);
                   1255:       } else if (metaData.getColumnType(i) == Types.TIME)
                   1256:       {
                   1257:         // use the ResultSet.getObject method for retieving images
                   1258:         // from FileMaker Pro container fields; the ResultSet.getObject
                   1259:         // method returns a java.awt.Image object for FileMaker Pro
                   1260:         // container fields
                   1261:         try
                   1262:         {
                   1263:           tableRow.addElement(resultSet.getTime(i).toString());
                   1264:           //    m_columnClasses.addElement(java.sql.Time.class);
                   1265:         } catch (Exception e)
                   1266:         {
                   1267: 
                   1268:           String value = resultSet.getString(i);
                   1269:           if (value != null)
                   1270:           {
                   1271:             //System.out.println("SQLTime new "+Time.valueOf("17:00:00").toString());
                   1272:             int index = 0;
                   1273:             for (int j = 0; j < value.length(); ++j)
                   1274:             {
                   1275:               if (!Character.isLetter(value.charAt(j)))
                   1276:                 index = j + 1;
                   1277:               else
                   1278:                 break;
                   1279:             }
                   1280: 
                   1281:             tableRow.addElement(value.substring(0, index));
                   1282:             //m_columnClasses.addElement(java.sql.Time.class);
                   1283:           } else
                   1284:             tableRow.add(null);
                   1285:           //  m_columnClasses.addElement(String.class);
                   1286:         } // to catch
                   1287: 
1.10      rogo     1288:       } else if (metaData.getColumnType(i) == Types.INTEGER)
                   1289:         {
                   1290:           // use the ResultSet.getObject method for retieving images
                   1291:           // from FileMaker Pro container fields; the ResultSet.getObject
                   1292:           // method returns a java.awt.Image object for FileMaker Pro
                   1293:           // container fields
                   1294: 
                   1295:           tableRow.addElement(new Integer(resultSet.getInt(i)));
                   1296:           //  m_columnClasses.addElement(java.sql.Date.class);
                   1297:         } else if (metaData.getColumnType(i) == Types.DATE)
                   1298:         {
1.1       rogo     1299:         // use the ResultSet.getObject method for retieving images
                   1300:         // from FileMaker Pro container fields; the ResultSet.getObject
                   1301:         // method returns a java.awt.Image object for FileMaker Pro
                   1302:         // container fields
1.14      rogo     1303:           try
                   1304:           {
                   1305:             tableRow.addElement(resultSet.getDate(i));
1.1       rogo     1306: 
1.14      rogo     1307:           } catch (Exception e)
                   1308:           {
1.15      rogo     1309:             // work around for parse bug in FM JDBC Driver 
                   1310:             // for dates of format dd-mm-yyyy
1.14      rogo     1311:             String date=resultSet.getString(i);
1.15      rogo     1312:             date=date.replace('-','.');
                   1313:             java.text.DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT,Locale.GERMAN);
                   1314:             java.util.Date d= dateFormat.parse(date);
1.16      rogo     1315:            // Calendar cal=Calendar.getInstance(Locale.GERMAN);
                   1316:            // cal.setTime(d);
                   1317:            // date=(cal.get(Calendar.YEAR))+"-"+(cal.get(Calendar.MONTH)+1)+"-"+cal.get(Calendar.DATE);
                   1318:             tableRow.addElement(new java.sql.Date(d.getTime()));
                   1319:              System.out.println("Date "+date);      
1.14      rogo     1320:           }
1.1       rogo     1321:         //  m_columnClasses.addElement(java.sql.Date.class);
                   1322:       } else if (metaData.getColumnTypeName(i) == "NUMBER")
                   1323:       {
                   1324:         // use the ResultSet.getObject method for retieving images
                   1325:         // from FileMaker Pro container fields; the ResultSet.getObject
                   1326:         // method returns a java.awt.Image object for FileMaker Pro
                   1327:         // container fields
                   1328:         try
                   1329:         {
1.13      rogo     1330:           tableRow.addElement(new Double(resultSet.getDouble(i)));
1.1       rogo     1331:           // m_columnClasses.addElement(Integer.class);
                   1332: 
                   1333:         } catch (Exception e)
                   1334:         {
                   1335: 
                   1336:           StringBuffer number = new StringBuffer();
                   1337:           String value = resultSet.getString(i);
                   1338:           System.out.println(value);
                   1339:           for (int c = 0; c < value.length(); ++c)
                   1340:           {
                   1341:             if (Character.isDigit(value.charAt(c)))
                   1342:             {
                   1343:               number.append(value.charAt(c));
                   1344:             }
                   1345:           }
                   1346:           if (number.length() > 0)
                   1347:           {
                   1348:             tableRow.addElement(null);
                   1349:             //   m_columnClasses.addElement(Integer.class);
                   1350:           } else
                   1351:             tableRow.addElement(null);
                   1352:         }
                   1353:       } else
                   1354:       {
                   1355:         // all other field values are retrieved as strings and
                   1356:         // added to the tableRow Vector
                   1357:         //   System.out.println("row "+resultSet.getString(i));
                   1358:         try
                   1359:         {
                   1360:           byte[] b = null;
                   1361:           if (metaData instanceof ResultSetMetaDataExt)
                   1362:             b = resultSet.getBytes(i);
                   1363:           String utf8 = null;
                   1364:           utf8 = (b == null) ? null : new String(b);
                   1365:           if (metaData instanceof ResultSetMetaDataExt)
                   1366:             tableRow.addElement((b != null) ? new String(utf8.getBytes()) : null);
                   1367:           else
                   1368:           {
1.17      rogo     1369:             if(url.toLowerCase().indexOf("odbc")>=0)
                   1370:            {
                   1371:                byte[] val = resultSet.getBytes(i);
1.18    ! rogo     1372:              for(int j=0;j<val.length;++j)
        !          1373:              System.out.println(Integer.toHexString(val[j]));
        !          1374:              tableRow.addElement((val==null) ? null:new String(val));
1.17      rogo     1375:       
                   1376:            } else
1.1       rogo     1377:             //  byte[] val = resultSet.getBytes(i);
                   1378:             tableRow.add(resultSet.getString(i));
                   1379:             //tableRow.addElement((val==null) ? null:new String(val,"UTF-8"));
                   1380:           }
                   1381:         } catch (Exception e)
                   1382:         {
                   1383:           System.out.println("Hey I Got an error" + e);
                   1384:           e.printStackTrace();
                   1385:         }
                   1386:         // m_columnClasses.addElement(java.lang.String.class);
                   1387:       }
                   1388:     }
                   1389:     //  tableData.addElement(tableRow);
                   1390:     if (check)
                   1391:       return tableRow;
                   1392:     else
                   1393:       return null;
                   1394:   }
                   1395:   class ConnectionPool
                   1396:   {
                   1397:     String user = "", passwd = "", url = "";
                   1398:     Connection con;
                   1399:     public ConnectionPool(String url, String user, String passwd, Connection con)
                   1400:     {
                   1401:       this.con = con;
                   1402:       this.user = user;
                   1403:       this.passwd = passwd;
                   1404:       this.url = url;
                   1405:     }
                   1406: 
                   1407:   }
                   1408:   public String getQC()
                   1409:   {
                   1410:     // if (connection == null)
                   1411:     // return "";
                   1412: 
                   1413:     // check if connection null if null try to get one
                   1414:     if (connection == null)
                   1415:       try
                   1416:       {
                   1417:         getConnection();
                   1418:       } catch (Exception e)
                   1419:       {
                   1420:         if (FM2SQL.debug)
                   1421:           System.out.println("cannot get a connection");
                   1422:       }
                   1423:     if (connection == null)
                   1424:     {
                   1425:       if (url.toLowerCase().indexOf("fmpro") >= 0 || url.toLowerCase().indexOf("postgres") >= 0)
                   1426:         quoteChar = "\"";
                   1427:       else if (url.toLowerCase().indexOf("mysql") >= 0)
                   1428:         quoteChar = "`";
                   1429:     }
                   1430:     if (quoteChar == null)
                   1431:       quoteChar = "\""; // needed for postgres
                   1432:     return quoteChar;
                   1433:   }
                   1434:   public int getRowCount(String query) throws SQLException
                   1435:   {
                   1436:     String table = query.substring(query.indexOf("from") + 4).trim();
                   1437:     int index = table.indexOf(" ");
                   1438:     table = table.substring(0, (index >= 0) ? index : table.length());
                   1439:     System.out.println(table);
                   1440:     Statement stm = null;
                   1441: 
                   1442:     if (metaData instanceof ResultSetMetaDataExt)
                   1443:       return 1000;
                   1444:     if (!connection.isClosed())
                   1445:       stm = connection.createStatement();
                   1446:     stm.setMaxRows(1);
                   1447:     ResultSet resultSet = stm.executeQuery("select count(*) from " + table);
                   1448:     resultSet.next();
                   1449:     return resultSet.getInt(1);
                   1450:   }
1.8       rogo     1451:    public TreeSet getIDVector(String id,String table,String query,int numHits) throws Exception
1.5       rogo     1452:    {
                   1453:      TreeSet t= new TreeSet();
                   1454:      getConnection();
                   1455:      ResultSet result = this.result;
1.7       rogo     1456:      String subQuery = query.substring(query.lastIndexOf(table)+table.length()+1);
                   1457:     System.out.println("subQuery "+subQuery);
1.8       rogo     1458:     makeQuery("select "+id+" from "+getQC()+table+getQC()+subQuery,numHits );
1.5       rogo     1459:      while(true)
                   1460:      {
1.6       rogo     1461:       Vector vec = getNextRow();
                   1462:       if (vec == null)
                   1463:         break;
                   1464:       t.add(vec.get(0));
1.5       rogo     1465:      }
                   1466:        this.result=result;
1.10      rogo     1467:     metaData = (this.result==null) ?null:this.result.getMetaData();
1.6       rogo     1468:     return t;
1.5       rogo     1469:    }
                   1470:    
                   1471: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>