File:  [Repository] / FM2SQL / Attic / DBBean.java
Revision 1.35: download - view: text, annotated - select for diffs - revision graph
Tue Sep 28 08:58:26 2004 UTC (19 years, 9 months ago) by rogo
Branches: MAIN
CVS tags: HEAD
bug for string retrieval for sequelink driver

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

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