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