File:  [Repository] / FM2SQL / Attic / DBBean.java
Revision 1.17: download - view: text, annotated - select for diffs - revision graph
Fri Apr 30 10:56:06 2004 UTC (20 years, 2 months ago) by rogo
Branches: MAIN
CVS tags: HEAD
for odbc added different string read bytes make string

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

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