Annotation of FM2SQL/DBBean.java, revision 1.32
1.11 rogo 1: /*
2: * DBBean.java -- Class that ecapsulates all database actions
3: * Filemake to SQL Converter
4: * Copyright (C) 2004 Robert Gordesch (rogo@mpiwg-berlin.mpg.de)
5: * This program is free software; you can redistribute it and/or modify it
6: * under the terms of the GNU General Public License as published by the Free
7: * Software Foundation; either version 2 of the License, or (at your option)
8: * any later version. Please read license.txt for the full details. A copy of
9: * the GPL may be found at http://www.gnu.org/copyleft/lgpl.html You should
10: * have received a copy of the GNU General Public License along with this
11: * program; if not, write to the Free Software Foundation, Inc., 59 Temple
12: * Place, Suite 330, Boston, MA 02111-1307 USA Created on 15.09.2003 by
13: * rogo
14: */
1.3 rogo 15:
1.1 rogo 16: import java.sql.*;
1.15 rogo 17: import java.text.DateFormat;
1.20 rogo 18: import java.text.ParseException;
1.1 rogo 19: import java.util.*;
1.15 rogo 20:
1.1 rogo 21: import com.fmi.jdbc.*;
22:
23: /**
24: *
25: *
26: * DBBean - Database bean
27: *
28: *<p> a Javabean to perform queries on a JDBC Database,
29: * or excute any other SQL statement
30: * </p>
31: * <p>
32: * Usage:
33: * <pre>
34: * DBBean bean = new DBBean();
35: * // setting user and passwd
36: * bean.setUserAndPasswd("bla","bla");
37: * try
38: * {
39: * bean.setConnection("jdbc:fmpro:http://localhost");
40: * Vector names=bean.getTableNames();
41: * Vector[] result=bean.getQueryData(names.get(0).toString());
42: * // print results to screen
43: * for(int i=0;i<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<result[0].size();++j)
50: * {
51: * Vector row=(Vector)result[0].get(j);
52: * //print rows
53: * for(int k=0;k<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: {
1.25 rogo 68: private boolean useNormanToUnicodeMapper = false;
1.1 rogo 69: Connection connection;
70: String url = "";
71: DatabaseMetaData dbMetaData;
72: Vector columnNames;
1.4 rogo 73: Vector ids = new Vector();
1.1 rogo 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());
1.9 rogo 87: DriverManager.registerDriver((Driver) Class.forName("com.mysql.jdbc.Driver").newInstance());
1.1 rogo 88: DriverManager.registerDriver((Driver) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance());
1.32 ! rogo 89: DriverManager.registerDriver((Driver) Class.forName("com.ddtek.jdbc.sequelink.SequeLinkDriver").newInstance());
1.19 rogo 90: DriverManager.registerDriver((Driver) Class.forName("acs.jdbc.Driver").newInstance());
1.32 ! rogo 91:
1.1 rogo 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: {
1.25 rogo 158: createConnection();
159:
1.1 rogo 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!!!");
1.25 rogo 173: createConnection();
174:
175: // connection = DriverManager.getConnection(conPool.url, conPool.user, conPool.passwd);
1.1 rogo 176: conPool.con = connection;
177: }
178: }
179: if (url != "" && connection == null)
1.18 rogo 180: createConnection();
1.25 rogo 181: //connection = DriverManager.getConnection(url, user, passwd);
1.1 rogo 182: dbMetaData = connection.getMetaData();
183: quoteChar = dbMetaData.getIdentifierQuoteString();
184: if (quoteChar == null)
185: quoteChar = "\""; // needed for postgres
186:
187: return connection;
188: }
1.18 rogo 189: private void createConnection() throws SQLException
190: {
1.25 rogo 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)
1.18 rogo 198: {
1.25 rogo 199: // Connect to the database
1.18 rogo 200: connection = DriverManager.getConnection(url, prop);
201: System.out.println("odbc with properties inited");
1.25 rogo 202: } else
203: connection = DriverManager.getConnection(url, user, passwd);
1.18 rogo 204: connectionPool.put(url, new ConnectionPool(url, user, passwd, connection));
1.25 rogo 205:
1.18 rogo 206: }
1.1 rogo 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 != "")
1.18 rogo 215: //connection = DriverManager.getConnection(url, user, passwd);
1.25 rogo 216: createConnection();
1.1 rogo 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 != "")
1.25 rogo 232: createConnection();
233: // connection = DriverManager.getConnection(url, user, passwd);
1.1 rogo 234: dbMetaData = connection.getMetaData();
235: quoteChar = dbMetaData.getIdentifierQuoteString();
236: if (quoteChar == null)
237: quoteChar = "\""; // needed for postgres
238: }
239:
1.4 rogo 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: }
1.1 rogo 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: */
1.25 rogo 381: public Vector[] getQueryData(String table) throws SQLException, ParseException
1.1 rogo 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: */
1.20 rogo 461: public Vector[] getQueryData(String query, int maxHits) throws SQLException, ParseException
1.1 rogo 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:
1.25 rogo 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")
1.1 rogo 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: {
1.29 rogo 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);
1.1 rogo 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);
1.13 rogo 645: m_columnClasses.addElement(Double.class);
1.1 rogo 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: byte[] b = resultSet.getBytes(i);
657: String utf8 = null;
658: if (metaData instanceof ResultSetMetaDataExt)
659: utf8 = (b == null) ? null : new String(b);
660: else
661: utf8 = (b == null) ? null : new String(b, "UTF-8");
662: utf8 = (utf8 == null) ? null : new String(utf8.getBytes("UTF-8"), "UTF-8");
663: tableRow.addElement(utf8);
664: } catch (Exception e)
665: {
666: System.out.println("Hey I Got an error" + e);
667: }
668: m_columnClasses.addElement(java.lang.String.class);
669: }
670: }
671:
672: // add the tableRow Vector to the tableData Vector
673: tableData.addElement(tableRow);
674: }
675:
676: // retrieve the column names from the result set; the column names
677: // are used for the table header
678: columnNames = new Vector();
679:
680: for (int i = 1; i <= columnCount; i++)
681: columnNames.addElement(metaData.getColumnName(i));
682: Vector data[] = new Vector[2];
683: data[0] = tableData;
684: data[1] = columnNames;
685: System.out.println("Rows " + tableData.size() + " " + ((Vector) tableData.get(0)).size());
686: long timeEnd = System.currentTimeMillis();
687: System.out.println("Time needed for query and data retrieval " + (timeEnd - timeStart) + " ms");
688: return data;
689: }
690:
691: public Vector getColumnNames()
692: {
693: if (result == null)
694: return null;
695: try
696: {
697: ResultSetMetaData metaData = result.getMetaData();
698: int columnCount = metaData.getColumnCount();
699: columnNames = new Vector();
700:
701: for (int i = 1; i <= columnCount; i++)
702: columnNames.addElement(metaData.getColumnName(i));
703: } catch (Exception e)
704: {
705: }
706: return columnNames;
707: }
708: /**
709: * makes the database Query
710: * with the numberOfHits as maximum
711: * @return the result as an ResultSet object
712: */
713: public ResultSet makeQuery(String query, int numberOfHits) throws SQLException
714: {
715: result = null;
716: Statement stm = null;
1.25 rogo 717:
1.1 rogo 718: // System.out.println("Query " + query);
719:
720: if (!connection.isClosed())
721: stm = connection.createStatement();
1.25 rogo 722: else
723: {
724:
1.12 rogo 725: try
726: {
727: connection = getConnection();
1.25 rogo 728: stm = connection.createStatement();
1.12 rogo 729: } catch (Exception e)
730: {
731: // TODO Auto-generated catch block
732: e.printStackTrace();
733: }
734: }
1.1 rogo 735: stm.setMaxRows(numberOfHits);
1.2 rogo 736: long time = System.currentTimeMillis();
1.25 rogo 737: try
738: {
739: stm.execute(query);
740: long time2 = System.currentTimeMillis();
741:
742: System.out.println("time to execute " + (time2 - time));
743: // stm.setMaxRows(numberOfHits);
744:
745: result = stm.getResultSet();
746: // System.out.println(result+" "+stm.getUpdateCount());
747: metaData = result.getMetaData();
748: } catch (Exception e)
749: {
1.19 rogo 750: // TODO remove
1.25 rogo 751: if (FM2SQL.fmInstance != null)
752: FM2SQL.showErrorDialog("Error caught!! \n Query was " + query + " \n", "Debug Info");
753: }
754:
1.1 rogo 755: return result;
756: }
757: /**
758: * sets the database user
759: */
760: public void setUser(String user)
761: {
762: this.user = user;
763: }
764: /**
765: * sets the database passwd
766: */
767: public void setPasswd(String passwd)
768: {
769: this.passwd = passwd;
770: }
771:
772: /**
773: * sets the database user and passwd
774: */
775: public void setUserAndPasswd(String user, String passwd)
776: {
777: this.user = user;
778: this.passwd = passwd;
779:
780: }
781: /**
782: * just sets the connection URL
783: */
784: public void setURL(String url)
785: {
786: this.url = url;
787: }
788:
789: /**
790: * Test the database drivers features given by the DatabaseMetaData object
791: */
792: public Vector[] TestDB(DatabaseMetaData d) throws SQLException
793: {
794:
795: Vector data[] = new Vector[2];
796: Vector[] rows = new Vector[120];
797: for (int i = 0; i < rows.length; ++i)
798: rows[i] = new Vector();
799: Vector columnNames = new Vector();
800: columnNames.add("Feature");
801: columnNames.add("Supported");
802:
803: Vector cols = new Vector();
804: rows[0].add("allProceduresAreCallable");
805: rows[0].add(new Boolean(d.allProceduresAreCallable()));
806: //boolean allProceduresAreCallable() throws SQLException;
807: rows[1].add("allTablesAreSelectable");
808: rows[1].add(new Boolean(d.allTablesAreSelectable()));
809: // boolean allTablesAreSelectable() throws SQLException;
810: rows[2].add("isReadOnly");
811: rows[2].add(new Boolean(d.isReadOnly()));
812: // boolean isReadOnly() throws SQLException;
813: rows[3].add("nullsAreSortedHigh");
814: rows[3].add(new Boolean(d.nullsAreSortedHigh()));
815: // boolean nullsAreSortedHigh() throws SQLException;
816: rows[4].add("nullsAreSortedLow");
817: rows[4].add(new Boolean(d.nullsAreSortedLow()));
818: // boolean nullsAreSortedLow() throws SQLException;
819: rows[5].add("nullsAreSortedAtStart");
820: rows[5].add(new Boolean(d.nullsAreSortedAtStart()));
821: // boolean nullsAreSortedAtStart() throws SQLException;
822: rows[6].add("nullsAreSortedAtEnd");
823: rows[6].add(new Boolean(d.nullsAreSortedAtEnd()));
824: // boolean nullsAreSortedAtEnd() throws SQLException;
825: rows[7].add("usesLocalFiles");
826: rows[7].add(new Boolean(d.usesLocalFiles()));
827: // boolean usesLocalFiles() throws SQLException;
828: rows[8].add("usesLocalFilePerTable");
829: rows[8].add(new Boolean(d.usesLocalFilePerTable()));
830: // boolean usesLocalFilePerTable() throws SQLException;
831: rows[9].add("supportsMixedCaseIdentifiers");
832: rows[9].add(new Boolean(d.supportsMixedCaseIdentifiers()));
833: //boolean supportsMixedCaseIdentifiers() throws SQLException;
834: rows[10].add("storesUpperCaseIdentifiers");
835: rows[10].add(new Boolean(d.storesUpperCaseIdentifiers()));
836: // boolean storesUpperCaseIdentifiers() throws SQLException;
837: rows[11].add("storesLowerCaseIdentifiers");
838: rows[11].add(new Boolean(d.storesLowerCaseIdentifiers()));
839: // boolean storesLowerCaseIdentifiers() throws SQLException;
840: rows[12].add("storesMixedCaseIdentifiers");
841: rows[12].add(new Boolean(d.storesMixedCaseIdentifiers()));
842: // boolean storesMixedCaseIdentifiers() throws SQLException;
843: rows[13].add("supportsMixedCaseQuotedIdentifiers");
844: rows[13].add(new Boolean(d.supportsMixedCaseQuotedIdentifiers()));
845: // boolean supportsMixedCaseQuotedIdentifiers() throws SQLException;
846: rows[14].add("storesUpperCaseQuotedIdentifiers");
847: rows[14].add(new Boolean(d.storesUpperCaseQuotedIdentifiers()));
848: // boolean storesUpperCaseQuotedIdentifiers() throws SQLException;
849: rows[15].add("storesLowerCaseQuotedIdentifiers");
850: rows[15].add(new Boolean(d.storesLowerCaseQuotedIdentifiers()));
851: //boolean storesLowerCaseQuotedIdentifiers() throws SQLException;
852: rows[16].add("storesMixedCaseQuotedIdentifiers");
853: rows[16].add(new Boolean(d.storesMixedCaseQuotedIdentifiers()));
854: // boolean storesMixedCaseQuotedIdentifiers() throws SQLException;
855: rows[17].add("supportsAlterTableWithAddColumn");
856: rows[17].add(new Boolean(d.supportsAlterTableWithAddColumn()));
857: // boolean supportsAlterTableWithAddColumn() throws SQLException;
858: rows[18].add("supportsAlterTableWithDropColumn");
859: rows[18].add(new Boolean(d.supportsAlterTableWithDropColumn()));
860: // boolean supportsAlterTableWithDropColumn() throws SQLException;
861: rows[19].add("nullPlusNonNullIsNull");
862: rows[19].add(new Boolean(d.nullPlusNonNullIsNull()));
863: // boolean nullPlusNonNullIsNull() throws SQLException;
864: rows[20].add("supportsConvert");
865: rows[20].add(new Boolean(d.supportsConvert()));
866: // boolean supportsConvert() throws SQLException;
867:
868: // boolean supportsConvert(int fromType, int toType) throws SQLException;
869: rows[21].add("supportsTableCorrelationNames");
870: rows[21].add(new Boolean(d.supportsTableCorrelationNames()));
871: // boolean supportsTableCorrelationNames() throws SQLException;
872: rows[22].add("supportsDifferentTableCorrelationNames");
873: rows[22].add(new Boolean(d.supportsDifferentTableCorrelationNames()));
874: // boolean supportsDifferentTableCorrelationNames() throws SQLException;
875: rows[23].add("supportsExpressionsInOrderBy");
876: rows[23].add(new Boolean(d.supportsExpressionsInOrderBy()));
877: // boolean supportsExpressionsInOrderBy() throws SQLException;
878: rows[24].add("supportsOrderByUnrelated");
879: rows[24].add(new Boolean(d.supportsOrderByUnrelated()));
880: // boolean supportsOrderByUnrelated() throws SQLException;
881: rows[25].add("supportsGroupBy");
882: rows[25].add(new Boolean(d.supportsGroupBy()));
883: // boolean supportsGroupBy() throws SQLException;
884: rows[26].add("supportsGroupByUnrelated");
885: rows[26].add(new Boolean(d.supportsGroupByUnrelated()));
886: // boolean supportsGroupByUnrelated() throws SQLException;
887: rows[27].add("supportsGroupByBeyondSelect");
888: rows[27].add(new Boolean(d.supportsGroupByBeyondSelect()));
889: // boolean supportsGroupByBeyondSelect() throws SQLException;
890: rows[28].add("supportsLikeEscapeClause");
891: rows[28].add(new Boolean(d.supportsLikeEscapeClause()));
892: // boolean supportsLikeEscapeClause() throws SQLException;
893: rows[29].add("supportsMultipleResultSets");
894: rows[29].add(new Boolean(d.supportsMultipleResultSets()));
895: // boolean supportsMultipleResultSets() throws SQLException;
896: rows[30].add("supportsMultipleTransactions");
897: rows[30].add(new Boolean(d.supportsMultipleTransactions()));
898: // boolean supportsMultipleTransactions() throws SQLException;
899: rows[31].add("supportsNonNullableColumns");
900: rows[31].add(new Boolean(d.supportsNonNullableColumns()));
901: // boolean supportsNonNullableColumns() throws SQLException;
902: rows[32].add("supportsMinimumSQLGrammar");
903: rows[32].add(new Boolean(d.supportsMinimumSQLGrammar()));
904: // boolean supportsMinimumSQLGrammar() throws SQLException;
905: rows[33].add("supportsCoreSQLGrammar");
906: rows[33].add(new Boolean(d.supportsCoreSQLGrammar()));
907: // boolean supportsCoreSQLGrammar() throws SQLException;
908: rows[34].add("supportsExtendedSQLGrammar");
909: rows[34].add(new Boolean(d.supportsExtendedSQLGrammar()));
910: // boolean supportsExtendedSQLGrammar() throws SQLException;
911: rows[35].add("supportsANSI92EntryLevelSQL");
912: rows[35].add(new Boolean(d.supportsANSI92EntryLevelSQL()));
913: // boolean supportsANSI92EntryLevelSQL() throws SQLException;
914: rows[36].add("supportsANSI92IntermediateSQL");
915: rows[36].add(new Boolean(d.supportsANSI92IntermediateSQL()));
916: //boolean supportsANSI92IntermediateSQL() throws SQLException;
917: rows[37].add("supportsANSI92FullSQL");
918: rows[37].add(new Boolean(d.supportsANSI92FullSQL()));
919: //boolean supportsANSI92FullSQL() throws SQLException;
920: rows[38].add("supportsIntegrityEnhancementFacility");
921: rows[38].add(new Boolean(d.supportsIntegrityEnhancementFacility()));
922: //boolean supportsIntegrityEnhancementFacility() throws SQLException;
923: rows[39].add("supportsOuterJoins");
924: rows[39].add(new Boolean(d.supportsOuterJoins()));
925: //boolean supportsOuterJoins() throws SQLException;
926: rows[40].add("supportsFullOuterJoins");
927: rows[40].add(new Boolean(d.supportsFullOuterJoins()));
928: //boolean supportsFullOuterJoins() throws SQLException;
929: rows[41].add("supportsLimitedOuterJoins");
930: rows[41].add(new Boolean(d.supportsLimitedOuterJoins()));
931: //boolean supportsLimitedOuterJoins() throws SQLException;
932: rows[42].add("isCatalogAtStart");
933: rows[42].add(new Boolean(d.isCatalogAtStart()));
934: //boolean isCatalogAtStart() throws SQLException;
935: rows[43].add("supportsSchemasInDataManipulation");
936: rows[43].add(new Boolean(d.supportsSchemasInDataManipulation()));
937: //boolean supportsSchemasInDataManipulation() throws SQLException;
938: rows[44].add("supportsSchemasInProcedureCalls");
939: rows[44].add(new Boolean(d.supportsSchemasInProcedureCalls()));
940: //boolean supportsSchemasInProcedureCalls() throws SQLException;
941: rows[45].add("supportsSchemasInTableDefinitions");
942: rows[45].add(new Boolean(d.supportsSchemasInTableDefinitions()));
943: //boolean supportsSchemasInTableDefinitions() throws SQLException;
944: rows[46].add("supportsSchemasInIndexDefinitions");
945: rows[46].add(new Boolean(d.supportsSchemasInIndexDefinitions()));
946: //boolean supportsSchemasInIndexDefinitions() throws SQLException;
947: rows[47].add("supportsSchemasInPrivilegeDefinitions");
948: rows[47].add(new Boolean(d.supportsSchemasInPrivilegeDefinitions()));
949: //boolean supportsSchemasInPrivilegeDefinitions() throws SQLException;
950: rows[48].add("supportsCatalogsInDataManipulation");
951: rows[48].add(new Boolean(d.supportsCatalogsInDataManipulation()));
952: //boolean supportsCatalogsInDataManipulation() throws SQLException;
953: rows[49].add("supportsCatalogsInProcedureCalls");
954: rows[49].add(new Boolean(d.supportsCatalogsInProcedureCalls()));
955: //boolean supportsCatalogsInProcedureCalls() throws SQLException;
956: rows[50].add("supportsCatalogsInTableDefinitions");
957: rows[50].add(new Boolean(d.supportsCatalogsInTableDefinitions()));
958: //boolean supportsCatalogsInTableDefinitions() throws SQLException;
959: rows[51].add("supportsCatalogsInIndexDefinitions");
960: rows[51].add(new Boolean(d.supportsCatalogsInIndexDefinitions()));
961: //boolean supportsCatalogsInIndexDefinitions() throws SQLException;
962: rows[52].add("supportsCatalogsInPrivilegeDefinitions");
963: rows[52].add(new Boolean(d.supportsCatalogsInPrivilegeDefinitions()));
964: //boolean supportsCatalogsInPrivilegeDefinitions() throws SQLException;
965: rows[53].add("supportsPositionedDelete");
966: rows[53].add(new Boolean(d.supportsPositionedDelete()));
967: //boolean supportsPositionedDelete() throws SQLException;
968: rows[54].add("supportsPositionedUpdate");
969: rows[54].add(new Boolean(d.supportsPositionedUpdate()));
970: //boolean supportsPositionedUpdate() throws SQLException;
971: rows[55].add("supportsSelectForUpdate");
972: rows[55].add(new Boolean(d.supportsSelectForUpdate()));
973: //boolean supportsSelectForUpdate() throws SQLException;
974: rows[56].add("supportsStoredProcedures");
975: rows[56].add(new Boolean(d.supportsStoredProcedures()));
976: //boolean supportsStoredProcedures() throws SQLException;
977: rows[57].add("supportsSubqueriesInComparisons");
978: rows[57].add(new Boolean(d.supportsSubqueriesInComparisons()));
979: //boolean supportsSubqueriesInComparisons() throws SQLException;
980: rows[58].add("supportsSubqueriesInExists");
981: rows[58].add(new Boolean(d.supportsSubqueriesInExists()));
982: //boolean supportsSubqueriesInExists() throws SQLException;
983: rows[59].add("supportsSubqueriesInIns");
984: rows[59].add(new Boolean(d.supportsSubqueriesInIns()));
985: //boolean supportsSubqueriesInIns() throws SQLException;
986: rows[60].add("supportsSubqueriesInQuantifieds");
987: rows[60].add(new Boolean(d.supportsSubqueriesInQuantifieds()));
988: //boolean supportsSubqueriesInQuantifieds() throws SQLException;
989: rows[61].add("supportsCorrelatedSubqueries");
990: rows[61].add(new Boolean(d.supportsCorrelatedSubqueries()));
991: //boolean supportsCorrelatedSubqueries() throws SQLException;
992: rows[62].add("supportsUnion");
993: rows[62].add(new Boolean(d.supportsUnion()));
994: //boolean supportsUnion() throws SQLException;
995: rows[63].add("supportsUnionAll");
996: rows[63].add(new Boolean(d.supportsUnionAll()));
997: //boolean supportsUnionAll() throws SQLException;
998: rows[64].add("supportsOpenCursorsAcrossCommit");
999: rows[64].add(new Boolean(d.supportsOpenCursorsAcrossCommit()));
1000: //boolean supportsOpenCursorsAcrossCommit() throws SQLException;
1001: rows[65].add("supportsOpenCursorsAcrossRollback");
1002: rows[65].add(new Boolean(d.supportsOpenCursorsAcrossRollback()));
1003: //boolean supportsOpenCursorsAcrossRollback() throws SQLException;
1004: rows[66].add("supportsOpenStatementsAcrossCommit");
1005: rows[66].add(new Boolean(d.supportsOpenStatementsAcrossCommit()));
1006: //boolean supportsOpenStatementsAcrossCommit() throws SQLException;
1007: rows[67].add("supportsOpenStatementsAcrossRollback");
1008: rows[67].add(new Boolean(d.supportsOpenStatementsAcrossRollback()));
1009: //boolean supportsOpenStatementsAcrossRollback() throws SQLException;
1010: rows[68].add("doesMaxRowSizeIncludeBlobs");
1011: rows[68].add(new Boolean(d.doesMaxRowSizeIncludeBlobs()));
1012: //boolean doesMaxRowSizeIncludeBlobs() throws SQLException;
1013: rows[69].add("supportsTransactions");
1014: rows[69].add(new Boolean(d.supportsTransactions()));
1015: //boolean supportsTransactions() throws SQLException;
1016: rows[70].add("supportsTransactionIsolationLevel");
1017: rows[70].add(new Boolean(d.supportsTransactionIsolationLevel(1)));
1018: //boolean supportsTransactionIsolationLevel(int level) throws SQLException;
1019: rows[71].add("supportsDataDefinitionAndDataManipulationTransactions");
1020: rows[71].add(new Boolean(d.supportsDataDefinitionAndDataManipulationTransactions()));
1021: //boolean supportsDataDefinitionAndDataManipulationTransactions() throws SQLException;
1022: rows[72].add("supportsDataManipulationTransactionsOnly");
1023: rows[72].add(new Boolean(d.supportsDataManipulationTransactionsOnly()));
1024: //boolean supportsDataManipulationTransactionsOnly() throws SQLException;
1025: rows[73].add("dataDefinitionCausesTransactionCommit");
1026: rows[73].add(new Boolean(d.dataDefinitionCausesTransactionCommit()));
1027: //boolean dataDefinitionCausesTransactionCommit() throws SQLException;
1028: rows[74].add("dataDefinitionIgnoredInTransactions");
1029: rows[74].add(new Boolean(d.dataDefinitionIgnoredInTransactions()));
1030: //boolean dataDefinitionIgnoredInTransactions() throws SQLException;
1031: rows[75].add("getMaxBinaryLiteralLength");
1032: rows[75].add(new Integer(d.getMaxBinaryLiteralLength()));
1033: // int getMaxBinaryLiteralLength() throws SQLException;
1034: rows[76].add("getMaxCharLiteralLength");
1035: rows[76].add(new Integer(d.getMaxCharLiteralLength()));
1036: //int getMaxCharLiteralLength() throws SQLException;
1037: rows[77].add("getMaxColumnNameLength");
1038: rows[77].add(new Integer(d.getMaxColumnNameLength()));
1039: // int getMaxColumnNameLength() throws SQLException;
1040: rows[78].add("getMaxColumnsInGroupBy");
1041: rows[78].add(new Integer(d.getMaxColumnsInGroupBy()));
1042: //int getMaxColumnsInGroupBy() throws SQLException;
1043: rows[79].add("getMaxColumnsInIndex");
1044: rows[79].add(new Integer(d.getMaxColumnsInIndex()));
1045: //int getMaxColumnsInIndex() throws SQLException;
1046: rows[80].add("getMaxColumnsInOrderBy");
1047: rows[80].add(new Integer(d.getMaxColumnsInOrderBy()));
1048: //int getMaxColumnsInOrderBy() throws SQLException;
1049: rows[81].add("getMaxColumnsInSelect");
1050: rows[81].add(new Integer(d.getMaxColumnsInSelect()));
1051: //int getMaxColumnsInSelect() throws SQLException;
1052: rows[82].add("getMaxColumnsInTable");
1053: rows[82].add(new Integer(d.getMaxColumnsInTable()));
1054: //int getMaxColumnsInTable() throws SQLException;
1055: rows[83].add("getMaxConnections");
1056: rows[83].add(new Integer(d.getMaxConnections()));
1057: //int getMaxConnections() throws SQLException;
1058: rows[84].add("getMaxCursorNameLength");
1059: rows[84].add(new Integer(d.getMaxCursorNameLength()));
1060: // int getMaxCursorNameLength() throws SQLException;
1061: rows[85].add("getMaxIndexLength");
1062: rows[85].add(new Integer(d.getMaxIndexLength()));
1063: //int getMaxIndexLength() throws SQLException;
1064: rows[86].add("getMaxSchemaNameLength");
1065: rows[86].add(new Integer(d.getMaxSchemaNameLength()));
1066: //int getMaxSchemaNameLength() throws SQLException;
1067: rows[87].add("getMaxProcedureNameLength");
1068: rows[87].add(new Integer(d.getMaxProcedureNameLength()));
1069: //int getMaxProcedureNameLength() throws SQLException;
1070: rows[88].add("getMaxCatalogNameLength");
1071: rows[88].add(new Integer(d.getMaxCatalogNameLength()));
1072: //int getMaxCatalogNameLength() throws SQLException;
1073: rows[89].add("getMaxRowSize");
1074: rows[89].add(new Integer(d.getMaxRowSize()));
1075: //int getMaxRowSize() throws SQLException;
1076: rows[90].add("getMaxStatementLength");
1077: rows[90].add(new Integer(d.getMaxStatementLength()));
1078: //int getMaxStatementLength() throws SQLException;
1079: rows[91].add("getMaxStatements");
1080: rows[91].add(new Integer(d.getMaxStatements()));
1081: //int getMaxStatements() throws SQLException;
1082: rows[92].add("getMaxTableNameLength");
1083: rows[92].add(new Integer(d.getMaxTableNameLength()));
1084: //int getMaxTableNameLength() throws SQLException;
1085: rows[93].add("getMaxTablesInSelect");
1086: rows[93].add(new Integer(d.getMaxTablesInSelect()));
1087: //int getMaxTablesInSelect() throws SQLException;
1088: rows[94].add("getMaxUserNameLength");
1089: rows[94].add(new Integer(d.getMaxUserNameLength()));
1090: // int getMaxUserNameLength() throws SQLException;
1091: rows[95].add("getDefaultTransactionIsolation");
1092: rows[95].add(new Integer(d.getDefaultTransactionIsolation()));
1093: //int getDefaultTransactionIsolation() throws SQLException;
1094:
1095: rows[96].add("getDatabaseProductName");
1096: rows[96].add(d.getDatabaseProductName());
1097: // String getDatabaseProductName() throws SQLException;
1098: rows[97].add("getDatabaseProductVersion");
1099: rows[97].add(d.getDatabaseProductVersion());
1100: //String getDatabaseProductVersion() throws SQLException;
1101:
1102: rows[98].add("getURL");
1103: rows[98].add(d.getURL());
1104: //String getURL() throws SQLException;
1105: rows[99].add("getUserName");
1106: rows[99].add(d.getUserName());
1107: //String getUserName() throws SQLException;
1108: rows[100].add("getDriverName");
1109: rows[100].add(d.getDriverName());
1110: // String getDriverName() throws SQLException;
1111: rows[101].add("getIdentifierQuoteString");
1112: rows[101].add(d.getIdentifierQuoteString());
1113: //String getIdentifierQuoteString() throws SQLException;
1114:
1115: rows[102].add("getDriverVersion");
1116: rows[102].add(d.getDriverVersion());
1117: //String getDriverVersion() throws SQLException;
1118: rows[103].add("getDriverMajorVersion");
1119: rows[103].add(new Integer(d.getDriverMajorVersion()));
1120: //int getDriverMajorVersion();
1121: rows[104].add("getDriverMinorVersion");
1122: rows[104].add(new Integer(d.getDriverMinorVersion()));
1123: //int getDriverMinorVersion();
1124: rows[105].add("getSQLKeywords");
1125: rows[105].add(d.getSQLKeywords());
1126: //String getSQLKeywords() throws SQLException;
1127: rows[106].add("getNumericFunctions");
1128: rows[106].add(d.getNumericFunctions());
1129: //String getNumericFunctions() throws SQLException;
1130: rows[107].add("getStringFunctions");
1131: rows[107].add(d.getStringFunctions());
1132: // String getStringFunctions() throws SQLException;
1133: rows[108].add("getSystemFunctions");
1134: rows[108].add(d.getSystemFunctions());
1135: //String getSystemFunctions() throws SQLException;
1136: rows[109].add("getTimeDateFunctions");
1137: rows[109].add(d.getTimeDateFunctions());
1138: //String getTimeDateFunctions() throws SQLException;
1139: rows[110].add("getSearchStringEscape");
1140: rows[110].add(d.getSearchStringEscape());
1141: //String getSearchStringEscape() throws SQLException;
1142: rows[111].add("getExtraNameCharacters");
1143: rows[111].add(d.getExtraNameCharacters());
1144: //String getExtraNameCharacters() throws SQLException;
1145: rows[112].add("getSchemaTerm");
1146: rows[112].add(d.getSchemaTerm());
1147: //String getSchemaTerm() throws SQLException;
1148: rows[113].add("getProcedureTerm");
1149: rows[113].add(d.getProcedureTerm());
1150: //String getProcedureTerm() throws SQLException;
1151: rows[114].add("getCatalogTerm");
1152: rows[114].add(d.getCatalogTerm());
1153: // String getCatalogTerm() throws SQLException;
1154: rows[115].add("getCatalogSeparator");
1155: rows[115].add(d.getCatalogSeparator());
1156: //String getCatalogSeparator() throws SQLException;
1157:
1158: /*
1159: boolean supportsResultSetType(int type) throws SQLException;
1160:
1161: boolean supportsResultSetConcurrency(int type, int concurrency) throws SQLException;
1162:
1163: boolean ownUpdatesAreVisible(int type) throws SQLException;
1164:
1165: boolean ownDeletesAreVisible(int type) throws SQLException;
1166:
1167: boolean ownInsertsAreVisible(int type) throws SQLException;
1168:
1169: boolean othersUpdatesAreVisible(int type) throws SQLException;
1170:
1171: boolean othersDeletesAreVisible(int type) throws SQLException;
1172:
1173: boolean othersInsertsAreVisible(int type) throws SQLException;
1174: boolean updatesAreDetected(int type) throws SQLException;
1175: boolean deletesAreDetected(int type) throws SQLException;
1176:
1177: boolean insertsAreDetected(int type) throws SQLException;
1178: */
1179: // not in filemaker
1180: // rows[96].add("supportsBatchUpdates");
1181: // rows[96].add(new Boolean(d.supportsBatchUpdates()));
1182: //boolean supportsBatchUpdates() throws SQLException;
1183:
1184: /*
1185: ResultSet getProcedures(String catalog, String schemaPattern, String procedureNamePattern) throws SQLException;
1186:
1187: ResultSet getProcedureColumns(String catalog, String schemaPattern, String procedureNamePattern, String columnNamePattern) throws SQLException;
1188: ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String types[]) throws SQLException;
1189: ResultSet getSchemas() throws SQLException;
1190: ResultSet getCatalogs() throws SQLException;
1191: ResultSet getTableTypes() throws SQLException;
1192: ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException;
1193: ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException;
1194: ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException;
1195: ResultSet getExportedKeys(String catalog, String schema, String table) throws SQLException;
1196: ResultSet getCrossReference(String primaryCatalog, String primarySchema, String primaryTable, String foreignCatalog, String foreignSchema, String foreignTable) throws SQLException;
1197: ResultSet getTypeInfo() throws SQLException;
1198: ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) throws SQLException;
1199:
1200: ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) throws SQLException;
1201:
1202: Connection getConnection() throws SQLException;
1203:
1204: */
1205: for (int i = 0; i < rows.length; ++i)
1206: cols.add(rows[i]);
1207: data[0] = cols;
1208: data[1] = columnNames;
1209: return data;
1210: }
1211: public Vector getNextRow() throws Exception
1212: {
1213:
1214: if (result == null)
1215: return null;
1216: boolean check = false;
1217: ResultSet resultSet = result;
1218: ResultSetMetaData metaData = resultSet.getMetaData();
1219: int columnCount = metaData.getColumnCount();
1220: Vector tableData = new Vector();
1221: check = resultSet.next();
1222: // System.out.println("hallo check "+check);
1223: if (!check)
1224: return null;
1225: Vector tableRow = new Vector(), m_columnClasses = new Vector();
1226: for (int i = 1; i <= columnCount; i++)
1227: {
1228: // repeating fields and fields from related databases may contain
1229: // multliple data values; the data values are stored using
1230: // a Vector which is then added to the tableRow
1231: // if (metaData instanceof ResultSetMetaDataExt)
1232: if ((metaData instanceof ResultSetMetaDataExt) && (((ResultSetMetaDataExt) metaData).isRelated(i) || ((ResultSetMetaDataExt) metaData).isRepeating(i)))
1233: {
1234: //System.out.println("Related fields");
1235: // retrieve the repeating or related field contents as a
1236: // com.fmi.jdbc.Array via the ResultSet.getObject method
1237: com.fmi.jdbc.Array array = (com.fmi.jdbc.Array) resultSet.getObject(i);
1238: // create a Vector for storing all of the data values
1239: ArrayList columnData = new ArrayList();
1240: try
1241: {
1242:
1243: // call the Array.getStringArray method since the data will
1244: // only be displayed
1245: Object[] fieldData = (Object[]) array.getArray();
1246:
1247: if (fieldData != null)
1248: {
1249: // add each value to the Vector
1250: for (int j = 0; j < fieldData.length; j++)
1251: {
1252: if (fieldData[j] != null)
1253: columnData.add(fieldData[j]);
1254: }
1255: }
1256: } catch (Exception e)
1257: {
1258: //System.out.println(e);
1259: }
1260:
1261: if (columnData.isEmpty())
1262: tableRow.add(null);
1263: else
1264: tableRow.addElement(columnData);
1265: //System.out.println(columnData);
1266: //System.out.println("Related fields"+columnData.size()+" "+tableRow.size());
1267:
1268: // m_columnClasses.addElement(java.util.Vector.class);
1269: } else if (metaData.getColumnType(i) == Types.LONGVARBINARY)
1270: {
1271: // use the ResultSet.getObject method for retrieving images
1272: // from FileMaker Pro container fields; the ResultSet.getObject
1273: // method returns a java.awt.Image object for FileMaker Pro
1274: // container fields
1275:
1276: try
1277: {
1278: tableRow.addElement(resultSet.getObject(i));
1279: } catch (Exception e)
1280: {
1281: // TODO Auto-generated catch block
1282: // e.printStackTrace();
1283: tableRow.addElement(null);
1284: }
1285: // m_columnClasses.addElement(java.awt.Image.class);
1286: } else if (metaData.getColumnType(i) == Types.TIME)
1287: {
1288: // use the ResultSet.getObject method for retieving images
1289: // from FileMaker Pro container fields; the ResultSet.getObject
1290: // method returns a java.awt.Image object for FileMaker Pro
1291: // container fields
1292: try
1293: {
1294: tableRow.addElement(resultSet.getTime(i).toString());
1295: // m_columnClasses.addElement(java.sql.Time.class);
1296: } catch (Exception e)
1297: {
1298:
1299: String value = resultSet.getString(i);
1300: if (value != null)
1301: {
1302: //System.out.println("SQLTime new "+Time.valueOf("17:00:00").toString());
1303: int index = 0;
1304: for (int j = 0; j < value.length(); ++j)
1305: {
1306: if (!Character.isLetter(value.charAt(j)))
1307: index = j + 1;
1308: else
1309: break;
1310: }
1311:
1312: tableRow.addElement(value.substring(0, index));
1313: //m_columnClasses.addElement(java.sql.Time.class);
1314: } else
1315: tableRow.add(null);
1316: // m_columnClasses.addElement(String.class);
1317: } // to catch
1318:
1.10 rogo 1319: } else if (metaData.getColumnType(i) == Types.INTEGER)
1.25 rogo 1320: {
1321: // use the ResultSet.getObject method for retieving images
1322: // from FileMaker Pro container fields; the ResultSet.getObject
1323: // method returns a java.awt.Image object for FileMaker Pro
1324: // container fields
1.10 rogo 1325:
1.25 rogo 1326: tableRow.addElement(new Integer(resultSet.getInt(i)));
1327: // m_columnClasses.addElement(java.sql.Date.class);
1328: } else if (metaData.getColumnType(i) == Types.DATE)
1329: {
1.1 rogo 1330: // use the ResultSet.getObject method for retieving images
1331: // from FileMaker Pro container fields; the ResultSet.getObject
1332: // method returns a java.awt.Image object for FileMaker Pro
1333: // container fields
1.25 rogo 1334: try
1335: {
1336: tableRow.addElement(resultSet.getDate(i));
1.1 rogo 1337:
1.25 rogo 1338: } catch (Exception e)
1339: {
1340: // work around for parse bug in FM JDBC Driver
1341: // for dates of format dd-mm-yyyy
1342: String date = resultSet.getString(i);
1343: date = date.replace('-', '.');
1344: java.text.DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
1345: java.util.Date d = dateFormat.parse(date);
1346: // Calendar cal=Calendar.getInstance(Locale.GERMAN);
1347: // cal.setTime(d);
1348: // date=(cal.get(Calendar.YEAR))+"-"+(cal.get(Calendar.MONTH)+1)+"-"+cal.get(Calendar.DATE);
1349: tableRow.addElement(new java.sql.Date(d.getTime()));
1350: System.out.println("Date " + date);
1351: }
1.1 rogo 1352: // m_columnClasses.addElement(java.sql.Date.class);
1353: } else if (metaData.getColumnTypeName(i) == "NUMBER")
1354: {
1355: // use the ResultSet.getObject method for retieving images
1356: // from FileMaker Pro container fields; the ResultSet.getObject
1357: // method returns a java.awt.Image object for FileMaker Pro
1358: // container fields
1359: try
1360: {
1.26 rogo 1361: Double value =new Double(resultSet.getDouble(i));
1362:
1363:
1364: // tableRow.addElement(new Double(resultSet.getDouble(i)));
1365: String tVal =value.toString();
1366: tVal = tVal.substring(tVal.indexOf('.')+1);
1367: boolean checkMe = tVal.length()==1&&tVal.equals("0");
1.27 rogo 1368: // System.out.println("check was"+checkMe+" "+tVal);
1.26 rogo 1369: if(checkMe)
1370: tableRow.addElement(new Integer(value.intValue()));
1371: else
1372: tableRow.addElement(value);
1373:
1.1 rogo 1374: // m_columnClasses.addElement(Integer.class);
1375:
1376: } catch (Exception e)
1377: {
1378:
1379: StringBuffer number = new StringBuffer();
1380: String value = resultSet.getString(i);
1381: System.out.println(value);
1382: for (int c = 0; c < value.length(); ++c)
1383: {
1384: if (Character.isDigit(value.charAt(c)))
1385: {
1386: number.append(value.charAt(c));
1387: }
1388: }
1389: if (number.length() > 0)
1390: {
1391: tableRow.addElement(null);
1392: // m_columnClasses.addElement(Integer.class);
1393: } else
1394: tableRow.addElement(null);
1395: }
1396: } else
1397: {
1398: // all other field values are retrieved as strings and
1399: // added to the tableRow Vector
1400: // System.out.println("row "+resultSet.getString(i));
1401: try
1402: {
1403: byte[] b = null;
1404: if (metaData instanceof ResultSetMetaDataExt)
1405: b = resultSet.getBytes(i);
1.25 rogo 1406: /* if (b != null)
1407: {
1408: java.io.ByteArrayInputStream stream = (java.io.ByteArrayInputStream) resultSet.getBinaryStream(i);
1409: // System.out.println(" stream "+resultSet.getBinaryStream(i));
1410: byte[] c = new byte[stream.available()];
1411: int length = stream.read(c, 0, c.length);
1412: int count = 0;
1413: b = new byte[c.length];
1414: for (int n = 0; n < length; ++n)
1415: {
1416:
1417: if (c[n] != 0)
1418: {
1419: // System.out.println(c[n]+" "+(int)'?'+" "+(char)c[n]+" "+count+" "+b.length);
1420: b[count++] = c[n];
1421: }
1422: }
1423: byte[] bCopy = new byte[count];
1424: System.arraycopy(b, 0, bCopy, 0, count);
1425: b = bCopy;
1426: }*/
1.1 rogo 1427: String utf8 = null;
1.24 rogo 1428: utf8 = (b == null) ? null : new String(b);
1.1 rogo 1429: if (metaData instanceof ResultSetMetaDataExt)
1.24 rogo 1430: {
1431: String rowElement = "";
1432: if (b != null)
1433: {
1434: rowElement = resultSet.getString(i);
1.25 rogo 1435: if (useNormanToUnicodeMapper)
1436: rowElement = Convert.normanToUnicode(rowElement);
1.24 rogo 1437: tableRow.addElement(rowElement);
1438:
1439: } else
1440: tableRow.addElement(null);
1.25 rogo 1441: } else
1.1 rogo 1442: {
1.25 rogo 1443: if (url.toLowerCase().indexOf("odbc") >= 0)
1444: {
1445: byte[] val = resultSet.getBytes(i);
1446: for (int j = 0; j < val.length; ++j)
1447: System.out.println(Integer.toHexString(val[j]));
1448: tableRow.addElement((val == null) ? null : new String(val));
1449:
1450: } else
1451: // byte[] val = resultSet.getBytes(i);
1452: tableRow.add(resultSet.getString(i));
1.1 rogo 1453: //tableRow.addElement((val==null) ? null:new String(val,"UTF-8"));
1454: }
1455: } catch (Exception e)
1456: {
1.24 rogo 1457: System.out.println("Hey I got an error" + e);
1.1 rogo 1458: e.printStackTrace();
1459: }
1460: // m_columnClasses.addElement(java.lang.String.class);
1461: }
1462: }
1463: // tableData.addElement(tableRow);
1464: if (check)
1465: return tableRow;
1466: else
1467: return null;
1468: }
1469: class ConnectionPool
1470: {
1471: String user = "", passwd = "", url = "";
1472: Connection con;
1473: public ConnectionPool(String url, String user, String passwd, Connection con)
1474: {
1475: this.con = con;
1476: this.user = user;
1477: this.passwd = passwd;
1478: this.url = url;
1479: }
1480:
1481: }
1482: public String getQC()
1483: {
1484: // if (connection == null)
1485: // return "";
1486:
1487: // check if connection null if null try to get one
1488: if (connection == null)
1489: try
1490: {
1491: getConnection();
1492: } catch (Exception e)
1493: {
1494: if (FM2SQL.debug)
1495: System.out.println("cannot get a connection");
1496: }
1497: if (connection == null)
1498: {
1499: if (url.toLowerCase().indexOf("fmpro") >= 0 || url.toLowerCase().indexOf("postgres") >= 0)
1500: quoteChar = "\"";
1501: else if (url.toLowerCase().indexOf("mysql") >= 0)
1502: quoteChar = "`";
1503: }
1504: if (quoteChar == null)
1505: quoteChar = "\""; // needed for postgres
1506: return quoteChar;
1507: }
1508: public int getRowCount(String query) throws SQLException
1509: {
1510: String table = query.substring(query.indexOf("from") + 4).trim();
1511: int index = table.indexOf(" ");
1512: table = table.substring(0, (index >= 0) ? index : table.length());
1513: System.out.println(table);
1514: Statement stm = null;
1515:
1516: if (metaData instanceof ResultSetMetaDataExt)
1517: return 1000;
1518: if (!connection.isClosed())
1519: stm = connection.createStatement();
1520: stm.setMaxRows(1);
1521: ResultSet resultSet = stm.executeQuery("select count(*) from " + table);
1522: resultSet.next();
1523: return resultSet.getInt(1);
1524: }
1.25 rogo 1525: public TreeSet getIDVector(String id, String table, String query, int numHits) throws Exception
1526: {
1527: TreeSet t = new TreeSet();
1528: getConnection();
1529: ResultSet result = this.result;
1530: String subQuery = query.substring(query.lastIndexOf(table) + table.length() + 1);
1531: System.out.println("subQuery " + subQuery);
1.30 rogo 1532: makeQuery("select " + id + " from " + getQC() + table + getQC() + subQuery+"order by "+id, numHits);
1.25 rogo 1533: while (true)
1534: {
1.6 rogo 1535: Vector vec = getNextRow();
1536: if (vec == null)
1537: break;
1538: t.add(vec.get(0));
1.25 rogo 1539: }
1540: this.result = result;
1541: metaData = (this.result == null) ? null : this.result.getMetaData();
1.6 rogo 1542: return t;
1.25 rogo 1543: }
1.24 rogo 1544: /**
1545: * @return
1546: */
1547: public boolean isUseNormanToUnicodeMapper()
1548: {
1549: return useNormanToUnicodeMapper;
1550: }
1551:
1552: /**
1553: * @param b
1554: */
1555: public void setUseNormanToUnicodeMapper(boolean b)
1556: {
1557: useNormanToUnicodeMapper = b;
1558: }
1.28 rogo 1559: /**
1560: *
1561: */
1562: protected void closeAllConnections()
1563: {
1564: Enumeration enumeration = connectionPool.elements();
1565: while(enumeration.hasMoreElements())
1566: {
1567: ConnectionPool conPol =(ConnectionPool)enumeration.nextElement();
1568: try
1569: {
1570: System.out.println(conPol);
1571: conPol.con.close();
1572: } catch (SQLException e)
1573: {
1574: // TODO Auto-generated catch block
1575: e.printStackTrace();
1576: }
1577: }
1578:
1579: }
1.31 rogo 1580: /**
1581: * @param indexField
1582: */
1583: public String getColumnType(String indexField) throws SQLException
1584: {
1585: Vector names=getColumnNames();
1586: for (int i = 0; i < names.size(); i++)
1587: {
1588: if(names.get(i).toString().equals(indexField))
1589: {
1590: System .out.println("found field "+names.get(i)+" "+metaData.getColumnTypeName(i+1));
1591: return metaData.getColumnTypeName(i+1);
1592:
1593: }
1594: }
1595: return "";
1596: }
1.24 rogo 1597:
1.5 rogo 1598: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>