Source Code Cross Referenced for MetaDataValues.java in  » Database-Client » executequery » org » executequery » databasemediators » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » executequery » org.executequery.databasemediators 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * MetaDataValues.java
0003:         *
0004:         * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
0005:         *
0006:         * This program is free software; you can redistribute it and/or
0007:         * modify it under the terms of the GNU General Public License
0008:         * as published by the Free Software Foundation; either version 2
0009:         * of the License, or any later version.
0010:         *
0011:         * This program is distributed in the hope that it will be useful,
0012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014:         * GNU General Public License for more details.
0015:         *
0016:         * You should have received a copy of the GNU General Public License
0017:         * along with this program; if not, write to the Free Software
0018:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
0019:         *
0020:         */
0021:
0022:        package org.executequery.databasemediators;
0023:
0024:        import java.lang.reflect.Method;
0025:
0026:        import java.sql.Connection;
0027:        import java.sql.DatabaseMetaData;
0028:        import java.sql.ResultSet;
0029:        import java.sql.ResultSetMetaData;
0030:        import java.sql.SQLException;
0031:        import java.sql.Statement;
0032:
0033:        import java.util.ArrayList;
0034:        import java.util.Arrays;
0035:        import java.util.Collections;
0036:        import java.util.HashMap;
0037:        import java.util.Hashtable;
0038:        import java.util.Iterator;
0039:        import java.util.List;
0040:        import java.util.Map;
0041:        import java.util.StringTokenizer;
0042:        import java.util.Vector;
0043:        import org.executequery.EventMediator;
0044:        import org.executequery.event.ConnectionEvent;
0045:        import org.executequery.datasource.ConnectionDataSource;
0046:        import org.executequery.datasource.ConnectionManager;
0047:        import org.executequery.event.ConnectionListener;
0048:        import org.underworldlabs.jdbc.DataSourceException;
0049:        import org.executequery.gui.browser.ColumnConstraint;
0050:        import org.executequery.gui.browser.ColumnData;
0051:        import org.executequery.gui.browser.ColumnIndex;
0052:        import org.executequery.gui.browser.DatabaseObject;
0053:        import org.executequery.gui.browser.TablePrivilege;
0054:        import org.executequery.util.Log;
0055:        import org.underworldlabs.util.MiscUtils;
0056:
0057:        /* ----------------------------------------------------------
0058:         * CVS NOTE: Changes to the CVS repository prior to the 
0059:         *           release of version 3.0.0beta1 has meant a 
0060:         *           resetting of CVS revision numbers.
0061:         * ----------------------------------------------------------
0062:         */
0063:
0064:        /** 
0065:         * This class provides access to the current connection's
0066:         * database meta data. Each method performs specific requests
0067:         * as may be required by the calling object to display the
0068:         * relevant data usually within a table or similar widget.
0069:         *
0070:         * Depending on the calling class and its requirements,
0071:         * the connection to the database may be left open thereby
0072:         * removing the overhead associated with connection retrieval -
0073:         * as in the case of the Database Browser which makes frequent
0074:         * database access requests. Other objects not requiring a
0075:         * dedicated connection simply choose not to maintain one and
0076:         * make their requests as required.
0077:         *
0078:         * @author   Takis Diakoumis
0079:         * @version  $Revision: 1.15 $
0080:         * @date     $Date: 2006/09/21 13:17:46 $
0081:         */
0082:        public class MetaDataValues implements  ConnectionListener {
0083:
0084:            /** The open database connection. */
0085:            private Connection connection;
0086:
0087:            /** Whether to keep the connection open. */
0088:            private boolean keepAlive;
0089:
0090:            /** the database connection object associated with this instance */
0091:            private DatabaseConnection databaseConnection;
0092:
0093:            /** the connection 'container' */
0094:            private Map<DatabaseConnection, Connection> connections;
0095:
0096:            /** <p>Constructs a new instance where the conection
0097:             *  is returned following each request.
0098:             */
0099:            public MetaDataValues() {
0100:                this (false);
0101:            }
0102:
0103:            /** <p>Constructs a new instance where the conection
0104:             *  is returned following each request only if the
0105:             *  passed boolean value is 'false'. Otherwise the
0106:             *  connection is initialised and maintained following
0107:             *  the first request and reused for any subsequent requests.
0108:             *
0109:             *  @param whether to keep the connection open
0110:             */
0111:            public MetaDataValues(boolean keepAlive) {
0112:                this (null, keepAlive);
0113:            }
0114:
0115:            public MetaDataValues(DatabaseConnection databaseConnection,
0116:                    boolean keepAlive) {
0117:                this .databaseConnection = databaseConnection;
0118:                this .keepAlive = keepAlive;
0119:                connections = Collections.synchronizedMap(new HashMap());
0120:                // register for connection events
0121:                EventMediator.registerListener(EventMediator.CONNECTION_EVENT,
0122:                        this );
0123:            }
0124:
0125:            /**
0126:             * Sets the database connection object to that specified.
0127:             */
0128:            public void setDatabaseConnection(DatabaseConnection dc) {
0129:                if (!connections.containsKey(dc)) {
0130:                    connections.put(dc, null);
0131:                }
0132:
0133:                if (this .databaseConnection != dc) {
0134:                    connection = null; // null out for pending change
0135:                    this .databaseConnection = dc;
0136:                }
0137:
0138:                /*
0139:                if (this.databaseConnection == dc) {
0140:                    Log.debug("same conn");
0141:                    return;
0142:                }
0143:                Log.debug("new conn");
0144:                closeConnection();
0145:                this.databaseConnection = dc;
0146:                 */
0147:            }
0148:
0149:            private void ensureConnection() throws DataSourceException {
0150:                try {
0151:
0152:                    if (connection == null || connection.isClosed()) {
0153:
0154:                        if (Log.isDebugEnabled()) {
0155:                            if (connection != null) {
0156:                                Log.debug("Connection is closed.");
0157:                            } else {
0158:                                Log
0159:                                        .debug("Connection is null - checking cache");
0160:                            }
0161:                        }
0162:
0163:                        // try the cache first
0164:                        connection = connections.get(databaseConnection);
0165:                        if (connection == null) {
0166:
0167:                            if (Log.isDebugEnabled()) {
0168:                                Log
0169:                                        .debug("ensureConnection: Connection is null in cache.");
0170:                                Log
0171:                                        .debug("ensureConnection: Retrieving new connection.");
0172:                            }
0173:                            // retrieve and add to the cache
0174:                            connection = ConnectionManager
0175:                                    .getConnection(databaseConnection);
0176:                            connections.put(databaseConnection, connection);
0177:                        }
0178:
0179:                        //connection = ConnectionManager.getConnection(databaseConnection);
0180:
0181:                        // if still null - something bad has happened, or maybe closed
0182:                        if (connection == null || connection.isClosed()) {
0183:                            throw new DataSourceException(
0184:                                    "No connection available", true);
0185:                        }
0186:                    }
0187:
0188:                } catch (SQLException e) {
0189:                    throw new DataSourceException(e);
0190:                }
0191:
0192:            }
0193:
0194:            /** 
0195:             * Retrieves the current connection's hosted
0196:             * schema names. The names are stored within a
0197:             * <code>Vector</code> object as single String objects.
0198:             *
0199:             * @return the schema names within a <code>Vector</code>
0200:             */
0201:            public Vector<String> getHostedCatalogsVector()
0202:                    throws DataSourceException {
0203:
0204:                ResultSet rs = null;
0205:
0206:                try {
0207:                    ensureConnection();
0208:                    DatabaseMetaData dmd = connection.getMetaData();
0209:                    rs = dmd.getCatalogs();
0210:
0211:                    Vector<String> v = new Vector<String>();
0212:                    while (rs.next()) {
0213:                        v.add(rs.getString(1));
0214:                    }
0215:
0216:                    return v;
0217:                } catch (SQLException e) {
0218:                    throw new DataSourceException(e);
0219:                } finally {
0220:                    releaseResources(rs);
0221:                }
0222:
0223:            }
0224:
0225:            public String[] getExportedKeyTables(String catalog, String schema,
0226:                    String table) throws DataSourceException {
0227:                ResultSet rs = null;
0228:                try {
0229:                    ensureConnection();
0230:                    DatabaseMetaData dmd = connection.getMetaData();
0231:                    rs = dmd.getExportedKeys(catalog, schema, table);
0232:
0233:                    List list = new ArrayList();
0234:                    while (rs.next()) {
0235:                        list.add(rs.getString(7));
0236:                    }
0237:                    return (String[]) list.toArray(new String[list.size()]);
0238:                } catch (SQLException e) {
0239:                    throw new DataSourceException(e);
0240:                } finally {
0241:                    releaseResources(rs);
0242:                }
0243:            }
0244:
0245:            public String[] getImportedKeyTables(String catalog, String schema,
0246:                    String table) throws DataSourceException {
0247:
0248:                ResultSet rs = null;
0249:                try {
0250:
0251:                    ensureConnection();
0252:                    DatabaseMetaData dmd = connection.getMetaData();
0253:                    rs = dmd.getImportedKeys(catalog, schema, table);
0254:
0255:                    List list = new ArrayList();
0256:                    while (rs.next()) {
0257:                        list.add(rs.getString(3));
0258:                    }
0259:                    return (String[]) list.toArray(new String[list.size()]);
0260:                } catch (SQLException e) {
0261:                    throw new DataSourceException(e);
0262:                } finally {
0263:                    releaseResources(rs);
0264:                }
0265:
0266:            }
0267:
0268:            public List getHostedCatalogSchemas() throws DataSourceException {
0269:                ResultSet rs = null;
0270:                try {
0271:
0272:                    ensureConnection();
0273:                    DatabaseMetaData dmd = connection.getMetaData();
0274:                    rs = dmd.getSchemas();
0275:
0276:                    List list = new ArrayList();
0277:                    while (rs.next()) {
0278:                        list.add(rs.getString(1));
0279:                    }
0280:                    return list;
0281:                } catch (SQLException e) {
0282:                    throw new DataSourceException(e);
0283:                } finally {
0284:                    releaseResources(rs);
0285:                }
0286:
0287:            }
0288:
0289:            /** <p>Retrieves the current connection's hosted
0290:             *  schema names. The names are stored within a
0291:             *  <code>Vector</code> object as single String objects.
0292:             *
0293:             *  @return the schema names within a <code>Vector</code>
0294:             */
0295:            public Vector<String> getHostedSchemasVector()
0296:                    throws DataSourceException {
0297:                ResultSet rs = null;
0298:                try {
0299:                    ensureConnection();
0300:                    DatabaseMetaData dmd = connection.getMetaData();
0301:                    rs = dmd.getSchemas();
0302:
0303:                    Vector v = new Vector<String>();
0304:                    while (rs.next()) {
0305:                        v.add(rs.getString(1));
0306:                    }
0307:
0308:                    int size = v.size();
0309:                    if (size == 1) {
0310:                        String value = (String) v.elementAt(0);
0311:                        if (MiscUtils.isNull(value)) {
0312:                            return new Vector<String>(0);
0313:                        }
0314:                    }
0315:                    /*
0316:                    else if (size == 0) {
0317:                        return getHostedCatalogsVector();
0318:                    }
0319:                     */
0320:                    return v;
0321:
0322:                } catch (SQLException e) {
0323:                    throw new DataSourceException(e);
0324:                } finally {
0325:                    releaseResources(rs);
0326:                }
0327:
0328:            }
0329:
0330:            public String[] getTableTypes() throws DataSourceException {
0331:                ResultSet rs = null;
0332:                try {
0333:                    ensureConnection();
0334:                    DatabaseMetaData dmd = connection.getMetaData();
0335:                    rs = dmd.getTableTypes();
0336:
0337:                    List list = new ArrayList();
0338:                    while (rs.next()) {
0339:                        list.add(rs.getString(1));
0340:                    }
0341:                    return (String[]) list.toArray(new String[list.size()]);
0342:                } catch (SQLException e) {
0343:                    throw new DataSourceException(e);
0344:                } finally {
0345:                    releaseResources(rs);
0346:                }
0347:            }
0348:
0349:            public TablePrivilege[] getPrivileges(String catalog,
0350:                    String schema, String table) throws DataSourceException {
0351:
0352:                ResultSet rs = null;
0353:                try {
0354:                    ensureConnection();
0355:                    DatabaseMetaData dmd = connection.getMetaData();
0356:                    rs = dmd.getTablePrivileges(catalog, schema, table);
0357:
0358:                    List list = new ArrayList();
0359:                    while (rs.next()) {
0360:                        list
0361:                                .add(new TablePrivilege(rs.getString(4), rs
0362:                                        .getString(5), rs.getString(6), rs
0363:                                        .getString(7)));
0364:                    }
0365:
0366:                    return (TablePrivilege[]) list
0367:                            .toArray(new TablePrivilege[list.size()]);
0368:
0369:                } catch (SQLException e) {
0370:                    throw new DataSourceException(e);
0371:                } finally {
0372:                    releaseResources(rs);
0373:                }
0374:
0375:            }
0376:
0377:            /** <p>Retrieves the column names for the specified
0378:             *  database table and schema as an array.
0379:             *
0380:             *  @param the database table name
0381:             *  @param the database schema name
0382:             *  @return the column name
0383:             */
0384:            public Map getColumnProperties(String schema, String table,
0385:                    String column) throws DataSourceException {
0386:
0387:                ResultSet rs = null;
0388:
0389:                try {
0390:
0391:                    ensureConnection();
0392:                    if (schema == null) {
0393:                        schema = getSchemaName().toUpperCase();
0394:                    }
0395:
0396:                    DatabaseMetaData dmd = connection.getMetaData();
0397:                    rs = dmd.getColumns(null, schema, table, column);
0398:
0399:                    ResultSetMetaData rsmd = rs.getMetaData();
0400:                    int columnCount = rsmd.getColumnCount();
0401:
0402:                    String[] metaColumnNames = new String[columnCount];
0403:                    for (int i = 1; i < columnCount; i++) {
0404:                        metaColumnNames[i - 1] = rsmd.getColumnName(i);
0405:                    }
0406:
0407:                    Map map = new HashMap();
0408:
0409:                    if (rs.next()) {
0410:
0411:                        for (int i = 1; i < columnCount; i++) {
0412:                            map.put(metaColumnNames[i - 1], rs.getString(i));
0413:                        }
0414:
0415:                    }
0416:
0417:                    return map;
0418:                } catch (SQLException e) {
0419:                    throw new DataSourceException(e);
0420:                } finally {
0421:                    releaseResources(rs);
0422:                }
0423:
0424:            }
0425:
0426:            /** 
0427:             * Retrieves a <code>Vector</code> of <code>ColumnIndexData</code> 
0428:             * objects containing all relevant information on the table indexes
0429:             * for the specified table.
0430:             *
0431:             * @param the table's name
0432:             * @return a <code>Vector</code> of <code>ColumnIndexData</code> objects
0433:             */
0434:            public Vector<ColumnIndex> getTableIndexes(String catalog,
0435:                    String schema, String table) throws DataSourceException {
0436:
0437:                ResultSet rs = null;
0438:
0439:                try {
0440:
0441:                    ensureConnection();
0442:                    DatabaseMetaData dmd = connection.getMetaData();
0443:
0444:                    rs = dmd.getIndexInfo(catalog, schema, table, false, true);
0445:
0446:                    Vector v = new Vector();
0447:                    while (rs.next()) {
0448:                        String name = rs.getString(6);
0449:
0450:                        if (MiscUtils.isNull(name)) {
0451:                            continue;
0452:                        }
0453:
0454:                        ColumnIndex cid = new ColumnIndex();
0455:                        cid.setNonUnique(rs.getBoolean(4));
0456:                        cid.setIndexName(name);
0457:                        cid.setIndexedColumn(rs.getString(9));
0458:                        v.add(cid);
0459:                    }
0460:
0461:                    return v;
0462:                } catch (SQLException e) {
0463:                    throw new DataSourceException(e);
0464:                } finally {
0465:                    releaseResources(rs);
0466:                }
0467:
0468:            }
0469:
0470:            /**
0471:             * Returns the table column meta data as a result set.
0472:             *
0473:             * @param the table name
0474:             * @param the schema name
0475:             * @param the table name
0476:             */
0477:            public ResultSet getTableMetaData(String catalog, String schema,
0478:                    String name) throws DataSourceException {
0479:                try {
0480:                    ensureConnection();
0481:                    DatabaseMetaData dmd = connection.getMetaData();
0482:                    if (!dmd.supportsCatalogsInTableDefinitions()) {
0483:                        catalog = null;
0484:                    }
0485:
0486:                    if (!dmd.supportsSchemasInTableDefinitions()) {
0487:                        schema = null;
0488:                    }
0489:
0490:                    return dmd.getColumns(catalog, schema, name, null);
0491:                } catch (SQLException e) {
0492:                    throw new DataSourceException(e);
0493:                } finally {
0494:                    // TODO: release ????
0495:                }
0496:            }
0497:
0498:            /** <p>Retrieves complete and detailed meta data for all columns
0499:             *  within the specified table and schema.
0500:             *  <p>The meta data will include data type, size and all
0501:             *  primary and foreign keys for the specified table. The results
0502:             *  of this method are specifically displayed within the Database
0503:             *  Browser feature for each selected table from the browser's
0504:             *  tree structure.
0505:             *
0506:             *  @param the table name
0507:             *  @param the schema name
0508:             *  @return the column meta data as a <code>ColumnData</code> array
0509:             */
0510:            public ColumnData[] getColumnMetaData(String tableName,
0511:                    String schemaName) throws DataSourceException {
0512:                return getColumnMetaData(null, schemaName, tableName);
0513:            }
0514:
0515:            public ColumnData[] getColumnMetaData(String catalog,
0516:                    String schema, String name) throws DataSourceException {
0517:
0518:                ResultSet rs = null;
0519:                try {
0520:                    ensureConnection();
0521:                    DatabaseMetaData dmd = connection.getMetaData();
0522:
0523:                    if (!dmd.supportsCatalogsInTableDefinitions()) {
0524:                        catalog = null;
0525:                    }
0526:
0527:                    if (!dmd.supportsSchemasInTableDefinitions()) {
0528:                        schema = null;
0529:                    }
0530:
0531:                    // -----------------------------------------
0532:                    // retrieve the primary keys for this table
0533:                    // -----------------------------------------
0534:
0535:                    ResultSet keys = dmd.getPrimaryKeys(catalog, schema, name);
0536:                    ArrayList _primaryKeys = new ArrayList();
0537:                    String pKey = null;
0538:                    String pKeyName = null;
0539:
0540:                    while (keys.next()) {
0541:                        ColumnConstraint cc = new ColumnConstraint();
0542:                        cc.setRefSchema(keys.getString(2));
0543:                        cc.setTable(keys.getString(3));
0544:                        cc.setColumn(keys.getString(4));
0545:                        cc.setName(keys.getString(6));
0546:                        cc.setType(ColumnConstraint.PRIMARY_KEY);
0547:                        _primaryKeys.add(cc);
0548:
0549:                        pKey = keys.getString(4);
0550:                        pKeyName = keys.getString(6);
0551:                    }
0552:
0553:                    keys.close();
0554:
0555:                    int v_size = _primaryKeys.size();
0556:                    ColumnConstraint[] primaryKeys = new ColumnConstraint[v_size];
0557:
0558:                    for (int i = 0; i < v_size; i++) {
0559:                        primaryKeys[i] = (ColumnConstraint) _primaryKeys.get(i);
0560:                    }
0561:
0562:                    // -----------------------------------------
0563:                    // retrieve the foreign keys of this table
0564:                    // -----------------------------------------
0565:
0566:                    keys = dmd.getImportedKeys(catalog, schema, name);
0567:
0568:                    // put the foreign key details in a temporary collection
0569:                    ArrayList _foreignKeys = new ArrayList();
0570:                    while (keys.next()) {
0571:                        ColumnConstraint cc = new ColumnConstraint();
0572:                        cc.setTable(name);
0573:                        cc.setRefSchema(keys.getString(2));
0574:                        cc.setRefTable(keys.getString(3));
0575:                        cc.setRefColumn(keys.getString(4));
0576:                        cc.setColumn(keys.getString(8));
0577:                        cc.setName(keys.getString(12));
0578:                        cc.setType(ColumnConstraint.FOREIGN_KEY);
0579:                        _foreignKeys.add(cc);
0580:                    }
0581:
0582:                    v_size = _foreignKeys.size();
0583:                    ColumnConstraint[] foreignKeys = new ColumnConstraint[v_size];
0584:
0585:                    for (int i = 0; i < v_size; i++) {
0586:                        foreignKeys[i] = (ColumnConstraint) _foreignKeys.get(i);
0587:                    }
0588:
0589:                    keys.close();
0590:
0591:                    // The primary key count
0592:                    int primaryKeyCount = 0;
0593:                    // The foreign key count
0594:                    int foreignKeyCount = 0;
0595:                    // The current column name
0596:                    String columnName = null;
0597:                    // The current key's column name
0598:                    String columnNameForKey = null;
0599:                    // to store the result set
0600:                    ArrayList _columns = new ArrayList();
0601:
0602:                    //Log.debug("catalog: " + catalog + " schema: " + schema);
0603:
0604:                    // retrieve the column data
0605:                    rs = dmd.getColumns(catalog, schema, name, null);
0606:
0607:                    while (rs.next()) {
0608:
0609:                        columnName = rs.getString(4);
0610:
0611:                        ColumnData cd = new ColumnData();
0612:                        cd.setCatalog(catalog);
0613:                        cd.setSchema(schema);
0614:                        cd.setColumnName(columnName);
0615:                        cd.setSQLType(rs.getShort(5));
0616:                        cd.setColumnType(rs.getString(6));
0617:                        cd.setColumnSize(rs.getInt(7));
0618:                        cd.setColumnScale(rs.getInt(9));
0619:                        cd.setColumnRequired(rs.getInt(11));
0620:                        cd.setDefaultValue(rs.getString(13));
0621:                        cd.setTableName(name);
0622:
0623:                        // check if all primary keys have been identified
0624:                        if (primaryKeyCount < primaryKeys.length) {
0625:
0626:                            // determine if the current column is a primary key
0627:                            for (int j = 0; j < primaryKeys.length; j++) {
0628:                                columnNameForKey = primaryKeys[j].getColumn();
0629:
0630:                                if (columnNameForKey.compareTo(columnName) == 0) {
0631:                                    cd.addConstraint(primaryKeys[j]);
0632:                                    cd.setPrimaryKey(true);
0633:                                    primaryKeyCount++;
0634:                                    break;
0635:                                }
0636:
0637:                            }
0638:
0639:                        }
0640:
0641:                        // check if all foreign keys have been identified
0642:                        if (foreignKeyCount < foreignKeys.length) {
0643:
0644:                            // determine if the current column is a foreign key
0645:                            for (int j = 0; j < foreignKeys.length; j++) {
0646:                                columnNameForKey = foreignKeys[j].getColumn();
0647:
0648:                                if (columnNameForKey.compareTo(columnName) == 0) {
0649:                                    cd.addConstraint(foreignKeys[j]);
0650:                                    cd.setForeignKey(true);
0651:                                    foreignKeyCount++;
0652:                                    break;
0653:                                }
0654:
0655:                            }
0656:
0657:                        }
0658:
0659:                        columnName = null;
0660:                        columnNameForKey = null;
0661:                        cd.setNamesToUpper();
0662:                        _columns.add(cd);
0663:
0664:                    }
0665:
0666:                    v_size = _columns.size();
0667:                    ColumnData[] columnDataArray = new ColumnData[v_size];
0668:
0669:                    for (int i = 0; i < v_size; i++) {
0670:                        columnDataArray[i] = (ColumnData) _columns.get(i);
0671:                    }
0672:
0673:                    return columnDataArray;
0674:
0675:                } catch (SQLException e) {
0676:                    throw new DataSourceException(e);
0677:                } finally {
0678:                    releaseResources(rs);
0679:                }
0680:
0681:            }
0682:
0683:            /** 
0684:             * Retrieves the database product name from
0685:             * the connection's meta data.
0686:             *
0687:             * @return the database product name
0688:             */
0689:            public String getDatabaseProductName() throws DataSourceException {
0690:                try {
0691:                    ensureConnection();
0692:                    DatabaseMetaData dmd = connection.getMetaData();
0693:                    return dmd.getDatabaseProductName();
0694:                } catch (SQLException e) {
0695:                    throw new DataSourceException(e);
0696:                } finally {
0697:                    releaseResources();
0698:                }
0699:            }
0700:
0701:            /** 
0702:             * Retrieves the database product version from
0703:             * the connection's meta data.
0704:             *
0705:             * @return the database product version
0706:             */
0707:            public String getDatabaseProductVersion()
0708:                    throws DataSourceException {
0709:                try {
0710:                    ensureConnection();
0711:                    DatabaseMetaData dmd = connection.getMetaData();
0712:                    return dmd.getDatabaseProductVersion();
0713:                } catch (SQLException e) {
0714:                    throw new DataSourceException(e);
0715:                } finally {
0716:                    releaseResources();
0717:                }
0718:
0719:            }
0720:
0721:            /** 
0722:             * Retrieves the database product version from
0723:             * the connection's meta data.
0724:             *
0725:             * @return the database product version
0726:             */
0727:            public String getDatabaseProductNameVersion()
0728:                    throws DataSourceException {
0729:                try {
0730:                    ensureConnection();
0731:                    DatabaseMetaData dmd = connection.getMetaData();
0732:                    return dmd.getDatabaseProductName() + " "
0733:                            + dmd.getDatabaseProductVersion();
0734:                } catch (SQLException e) {
0735:                    throw new DataSourceException(e);
0736:                } finally {
0737:                    releaseResources();
0738:                }
0739:
0740:            }
0741:
0742:            public DatabaseProcedure[] getProcedures(String schema,
0743:                    String[] names) throws DataSourceException {
0744:                return getProcedures(null, schema, names);
0745:            }
0746:
0747:            public DatabaseProcedure[] getProcedures(String catalog,
0748:                    String schema, String[] names) throws DataSourceException {
0749:                ResultSet rs = null;
0750:                try {
0751:                    ensureConnection();
0752:                    DatabaseMetaData dmd = connection.getMetaData();
0753:
0754:                    List<DatabaseProcedure> list = new ArrayList<DatabaseProcedure>(
0755:                            names.length);
0756:                    for (int i = 0; i < names.length; i++) {
0757:                        rs = dmd.getProcedureColumns(catalog, schema, names[i],
0758:                                null);
0759:
0760:                        DatabaseProcedure proc = new DatabaseProcedure(schema,
0761:                                names[i]);
0762:                        while (rs.next()) {
0763:                            proc.addParameter(rs.getString(4), rs.getInt(5), rs
0764:                                    .getInt(6), rs.getString(7), rs.getInt(8));
0765:                        }
0766:                        list.add(proc);
0767:                        rs.close();
0768:                    }
0769:
0770:                    return (DatabaseProcedure[]) list
0771:                            .toArray(new DatabaseProcedure[names.length]);
0772:                } catch (SQLException e) {
0773:                    throw new DataSourceException(e);
0774:                } finally {
0775:                    releaseResources(rs);
0776:                }
0777:
0778:            }
0779:
0780:            public DatabaseProcedure getProcedureColumns(String schema,
0781:                    String name) throws DataSourceException {
0782:                return getProcedureColumns(null, schema, name);
0783:            }
0784:
0785:            public DatabaseProcedure getProcedureColumns(String catalog,
0786:                    String schema, String name) throws DataSourceException {
0787:                ResultSet rs = null;
0788:                /*
0789:                 if (schema == null) {
0790:                 schema = getSchemaName();
0791:                 }
0792:                 */
0793:
0794:                /*
0795:                Log.debug("catalog: " + catalog + 
0796:                        " schema: " + schema + " name: " + name);
0797:                 */
0798:
0799:                try {
0800:                    ensureConnection();
0801:                    DatabaseMetaData dmd = connection.getMetaData();
0802:                    rs = dmd.getProcedureColumns(catalog, schema, name, null);
0803:                    DatabaseProcedure dbproc = new DatabaseProcedure(schema,
0804:                            name);
0805:
0806:                    while (rs.next()) {
0807:                        dbproc.addParameter(rs.getString(4), rs.getInt(5), rs
0808:                                .getInt(6), rs.getString(7), rs.getInt(8));
0809:                    }
0810:                    return dbproc;
0811:
0812:                } catch (SQLException e) {
0813:                    throw new DataSourceException(e);
0814:                } finally {
0815:                    releaseResources(rs);
0816:                }
0817:            }
0818:
0819:            /**
0820:             * Retrieves the data in its entirety from the specified table 
0821:             * using <code>SELECT * FROM table_name</code>.
0822:             *
0823:             * @param schema - the schema name (may be null)
0824:             * @param table - the table name
0825:             * @return the table data
0826:             */
0827:            public ResultSet getTableData(String schema, String table)
0828:                    throws DataSourceException {
0829:                ResultSet rs = null;
0830:                Statement stmnt = null;
0831:                try {
0832:                    ensureConnection();
0833:                    StringBuffer sb = new StringBuffer();
0834:                    sb.append("SELECT * FROM ");
0835:
0836:                    if (!MiscUtils.isNull(schema)) {
0837:                        sb.append(schema);
0838:                        sb.append(".");
0839:                    }
0840:                    sb.append(table);
0841:
0842:                    stmnt = connection.createStatement();
0843:                    return stmnt.executeQuery(sb.toString());
0844:                } catch (SQLException e) {
0845:                    throw new DataSourceException(e);
0846:                }
0847:            }
0848:
0849:            /**
0850:             * Retrieves the table data row count for the specified table.
0851:             *
0852:             * @param schema - the schema name (may be null)
0853:             * @param table - the table name
0854:             * @return the data row count
0855:             */
0856:            public int getTableDataRowCount(String schema, String table)
0857:                    throws DataSourceException {
0858:                ResultSet rs = null;
0859:                Statement stmnt = null;
0860:                try {
0861:                    ensureConnection();
0862:                    StringBuffer sb = new StringBuffer();
0863:                    sb.append("SELECT COUNT(*) FROM ");
0864:
0865:                    if (!MiscUtils.isNull(schema)) {
0866:                        sb.append(schema);
0867:                        sb.append(".");
0868:                    }
0869:                    sb.append(table);
0870:
0871:                    stmnt = connection.createStatement();
0872:                    rs = stmnt.executeQuery(sb.toString());
0873:                    if (rs.next()) {
0874:                        return rs.getInt(1);
0875:                    }
0876:                    return 0;
0877:                } catch (SQLException e) {
0878:                    throw new DataSourceException(e);
0879:                } finally {
0880:                    releaseResources(stmnt, rs);
0881:                }
0882:            }
0883:
0884:            public boolean hasStoredObjects(String schema, String[] types)
0885:                    throws DataSourceException {
0886:                return hasStoredObjects(null, schema, types);
0887:            }
0888:
0889:            public boolean hasStoredObjects(String catalog, String schema,
0890:                    String[] types) throws DataSourceException {
0891:
0892:                if (schema == null) {
0893:                    schema = getSchemaName();
0894:                }
0895:
0896:                ResultSet rs = null;
0897:                try {
0898:                    ensureConnection();
0899:                    DatabaseMetaData dmd = connection.getMetaData();
0900:                    rs = dmd.getTables(catalog, schema, null, types);
0901:                    return rs.next();
0902:                } catch (SQLException e) {
0903:                    throw new DataSourceException(e);
0904:                } finally {
0905:                    releaseResources(rs);
0906:                }
0907:            }
0908:
0909:            public DatabaseProcedure[] getStoredObjects(String schema,
0910:                    String[] types) throws DataSourceException {
0911:                return getStoredObjects(null, schema, types);
0912:            }
0913:
0914:            public DatabaseProcedure[] getStoredObjects(String catalog,
0915:                    String schema, String[] types) throws DataSourceException {
0916:
0917:                ResultSet rs = null;
0918:
0919:                if (schema == null) {
0920:                    schema = getSchemaName();
0921:                }
0922:
0923:                try {
0924:                    ensureConnection();
0925:                    DatabaseMetaData dmd = connection.getMetaData();
0926:                    rs = dmd.getTables(catalog, schema, null, types);
0927:
0928:                    ArrayList list = new ArrayList();
0929:
0930:                    while (rs.next()) {
0931:                        list.add(rs.getString(3));
0932:                    }
0933:
0934:                    rs.close();
0935:
0936:                    String[] procedures = (String[]) list
0937:                            .toArray(new String[list.size()]);
0938:                    list.clear();
0939:
0940:                    for (int i = 0; i < procedures.length; i++) {
0941:
0942:                        rs = dmd.getProcedures(null, schema, procedures[i]);
0943:
0944:                        while (rs.next()) {
0945:                            String name = rs.getString(3);
0946:                            DatabaseProcedure dbproc = new DatabaseProcedure(rs
0947:                                    .getString(2), name);
0948:
0949:                            ResultSet _rs = dmd.getProcedureColumns(null,
0950:                                    schema, name, null);
0951:                            while (_rs.next()) {
0952:                                dbproc.addParameter(_rs.getString(4), _rs
0953:                                        .getInt(5), _rs.getInt(6), _rs
0954:                                        .getString(7), _rs.getInt(8));
0955:                            }
0956:
0957:                            _rs.close();
0958:                            list.add(dbproc);
0959:                        }
0960:
0961:                    }
0962:
0963:                    DatabaseProcedure[] procs = (DatabaseProcedure[]) list
0964:                            .toArray(new DatabaseProcedure[list.size()]);
0965:                    return procs;
0966:
0967:                } catch (SQLException e) {
0968:                    throw new DataSourceException(e);
0969:                } finally {
0970:                    releaseResources(rs);
0971:                }
0972:
0973:            }
0974:
0975:            /**
0976:             * Recycles the specified connection object.
0977:             *
0978:             * @param dc - the connection to be recycled
0979:             */
0980:            public void recycleConnection(DatabaseConnection dc)
0981:                    throws DataSourceException {
0982:                if (connections.containsKey(dc)) {
0983:                    //Log.debug("Recycling connection");
0984:                    // close the connection held in the local cache
0985:                    // another will be retrieved on the next call to it
0986:                    Connection c = connections.get(dc);
0987:                    ConnectionManager.close(dc, c);
0988:                    connections.put(dc, null);
0989:                }
0990:            }
0991:
0992:            /** 
0993:             * Closes the open connection and releases
0994:             * all resources attached to it.
0995:             */
0996:            public void closeConnection() {
0997:                try {
0998:                    for (Iterator i = connections.keySet().iterator(); i
0999:                            .hasNext();) {
1000:                        DatabaseConnection dc = (DatabaseConnection) i.next();
1001:                        connection = connections.get(dc);
1002:                        if (connection != null) {
1003:                            connection.close();
1004:                        }
1005:                        connection = null;
1006:                    }
1007:                    /*
1008:                    if (connection != null) {
1009:                        connection.close();
1010:                    }
1011:                    connection = null;
1012:                     */
1013:                } catch (SQLException sqlExc) {
1014:                    sqlExc.printStackTrace();
1015:                }
1016:            }
1017:
1018:            /** <p>Retrieves key/value type pairs using the
1019:             *  <code>Reflection</code> API to call and retrieve
1020:             *  values from the connection's meta data object's methods
1021:             *  and variables.
1022:             *  <p>The values are returned within a 2-dimensional
1023:             *  array of key/value pairs.
1024:             *
1025:             *  @return the database properties as key/value pairs
1026:             */
1027:            public Hashtable getDatabaseProperties() throws DataSourceException {
1028:                try {
1029:
1030:                    ensureConnection();
1031:                    DatabaseMetaData dmd = connection.getMetaData();
1032:
1033:                    Class metaClass = dmd.getClass();
1034:                    Method[] metaMethods = metaClass.getMethods();
1035:
1036:                    Object[] p = new Object[] {};
1037:
1038:                    Hashtable h = new Hashtable();
1039:                    String STRING = "String";
1040:                    String GET = "get";
1041:
1042:                    for (int i = 0; i < metaMethods.length; i++) {
1043:                        try {
1044:                            Class c = metaMethods[i].getReturnType();
1045:                            String s = metaMethods[i].getName();
1046:
1047:                            if (s == null || c == null) {
1048:                                continue;
1049:                            }
1050:
1051:                            if (c.isPrimitive() || c.getName().endsWith(STRING)) {
1052:
1053:                                if (s.startsWith(GET)) {
1054:                                    s = s.substring(3);
1055:                                }
1056:
1057:                                try {
1058:                                    Object res = metaMethods[i].invoke(dmd, p);
1059:                                    h.put(s, res.toString());
1060:                                } catch (AbstractMethodError abe) {
1061:                                    continue;
1062:                                }
1063:
1064:                            }
1065:                        } catch (Exception e) {
1066:                            continue;
1067:                        }
1068:                    }
1069:                    /*            
1070:                     int count = 0;
1071:                     // prepare for key sort
1072:                     String[] keys = new String[h.size()];
1073:                     for (Enumeration i = h.keys(); i.hasMoreElements();) {
1074:                     keys[count++] = (String)i.nextElement();
1075:                     }
1076:                    
1077:                     Arrays.sort(keys);
1078:                     String[][] dbData = new String[keys.length][2];
1079:                     for (int i = 0; i < keys.length; i++) {
1080:                     dbData[i][0] = keys[i];
1081:                     dbData[i][1] = (String)h.get(keys[i]);
1082:                     }
1083:                     */
1084:                    return h;
1085:
1086:                } catch (SQLException e) {
1087:                    throw new DataSourceException(e);
1088:                } finally {
1089:                    releaseResources();
1090:                }
1091:
1092:            }
1093:
1094:            /** <p>Retrieves the connected databases SQL keyword
1095:             *  list via a call to the <code>DatabaseMetaData</code>
1096:             *  object's <code>getSQLKeywords()</code> method.
1097:             *  <p>The retrieved keywords are stored within a
1098:             *  2-dimensional array for display with the relevant
1099:             *  header within a table.
1100:             *
1101:             *  @return the schema names array
1102:             */
1103:            public String[] getDatabaseKeywords() throws DataSourceException {
1104:                try {
1105:                    ensureConnection();
1106:                    DatabaseMetaData dmd = connection.getMetaData();
1107:
1108:                    String sql = dmd.getSQLKeywords();
1109:                    releaseResources();
1110:
1111:                    StringTokenizer st = new StringTokenizer(sql, ",");
1112:                    List<String> values = new ArrayList<String>();
1113:
1114:                    while (st.hasMoreTokens()) {
1115:                        values.add(st.nextToken());
1116:                    }
1117:
1118:                    int size = values.size();
1119:                    String[] words = new String[size];
1120:                    for (int i = 0; i < size; i++) {
1121:                        words[i] = values.get(i);
1122:                    }
1123:                    return words;
1124:
1125:                } catch (SQLException e) {
1126:                    throw new DataSourceException(e);
1127:                }
1128:            }
1129:
1130:            private void releaseResources(Statement stmnt) {
1131:                try {
1132:                    if (stmnt != null) {
1133:                        stmnt.close();
1134:                    }
1135:                } catch (SQLException sqlExc) {
1136:                } finally {
1137:                    releaseResources();
1138:                }
1139:            }
1140:
1141:            private void releaseResources(Statement stmnt, ResultSet rs) {
1142:                try {
1143:                    if (rs != null) {
1144:                        rs.close();
1145:                    }
1146:                    if (stmnt != null) {
1147:                        stmnt.close();
1148:                    }
1149:                } catch (SQLException sqlExc) {
1150:                } finally {
1151:                    releaseResources();
1152:                }
1153:            }
1154:
1155:            private void releaseResources(ResultSet rs) {
1156:                try {
1157:                    if (rs != null) {
1158:                        rs.close();
1159:                    }
1160:                } catch (SQLException sqlExc) {
1161:                } finally {
1162:                    releaseResources();
1163:                }
1164:            }
1165:
1166:            /** <p>Releases this object's connection resources */
1167:            private void releaseResources() {
1168:                if (keepAlive) {
1169:                    return;
1170:                }
1171:                closeConnection();
1172:            }
1173:
1174:            /** <p>Retrieves the database SQL data types as a
1175:             *  <code>ResultSet</code> object.
1176:             *  <p>This will be typically used to display the
1177:             *  complete data types meta data retrieved from the JDBC driver.
1178:             *
1179:             *  @return the SQL data types
1180:             */
1181:            public ResultSet getDataTypesResultSet() throws DataSourceException {
1182:                try {
1183:                    ensureConnection();
1184:                    DatabaseMetaData dmd = connection.getMetaData();
1185:                    return dmd.getTypeInfo();
1186:                } catch (SQLException e) {
1187:                    throw new DataSourceException(e);
1188:                }
1189:            }
1190:
1191:            /** <p>Retrieves the database SQL data type names only.
1192:             *
1193:             *  @return the SQL data type names within an array
1194:             */
1195:            public String[] getDataTypesArray() throws DataSourceException {
1196:                ResultSet rs = null;
1197:                try {
1198:                    ensureConnection();
1199:                    DatabaseMetaData dmd = connection.getMetaData();
1200:                    rs = dmd.getTypeInfo();
1201:
1202:                    String underscore = "_";
1203:                    List<String> _dataTypes = new ArrayList<String>();
1204:                    while (rs.next()) {
1205:                        String type = rs.getString(1);
1206:                        if (!type.startsWith(underscore)) {
1207:                            _dataTypes.add(type);
1208:                        }
1209:                    }
1210:
1211:                    int size = _dataTypes.size();
1212:                    String[] dataTypes = new String[size];
1213:                    for (int i = 0; i < size; i++) {
1214:                        dataTypes[i] = _dataTypes.get(i);
1215:                    }
1216:
1217:                    Arrays.sort(dataTypes);
1218:                    return dataTypes;
1219:                } catch (SQLException e) {
1220:                    throw new DataSourceException(e);
1221:                } finally {
1222:                    releaseResources(rs);
1223:                }
1224:
1225:            }
1226:
1227:            /** <p>Retrieves the currently connected schema's
1228:             *  database table names within a <code>Vector</code>.
1229:             *
1230:             *  @return the table names
1231:             */
1232:            public Vector getDatabaseTablesVector() throws DataSourceException {
1233:                ResultSet rs = null;
1234:                try {
1235:                    ensureConnection();
1236:                    DatabaseMetaData dmd = connection.getMetaData();
1237:                    String[] type = { "TABLE" };
1238:                    rs = dmd.getTables(null, getSchemaName(), null, type);
1239:
1240:                    Vector v = new Vector();
1241:                    while (rs.next()) {
1242:                        v.add(rs.getString(3));
1243:                    }
1244:
1245:                    return v;
1246:                } catch (SQLException e) {
1247:                    throw new DataSourceException(e);
1248:                } finally {
1249:                    releaseResources(rs);
1250:                }
1251:            }
1252:
1253:            /** <p>Retrieves the column names for the specified
1254:             *  database table and schema as an array.
1255:             *
1256:             *  @param the database table name
1257:             *  @param the database schema name
1258:             *  @return the column names array
1259:             */
1260:            public String[] getColumnNames(String table, String schema)
1261:                    throws DataSourceException {
1262:                ResultSet rs = null;
1263:                try {
1264:                    ensureConnection();
1265:                    if (schema == null) {
1266:                        schema = getSchemaName().toUpperCase();
1267:                    }
1268:
1269:                    DatabaseMetaData dmd = connection.getMetaData();
1270:                    rs = dmd.getColumns(null, schema, table, null);
1271:
1272:                    Vector<String> v = new Vector<String>();
1273:                    while (rs.next()) {
1274:                        v.add(rs.getString(4));
1275:                    }
1276:
1277:                    int v_size = v.size();
1278:                    String[] columns = new String[v_size];
1279:                    for (int i = 0; i < v_size; i++) {
1280:                        columns[i] = v.get(i);
1281:                    }
1282:
1283:                    return columns;
1284:                } catch (SQLException e) {
1285:                    throw new DataSourceException(e);
1286:                } finally {
1287:                    releaseResources(rs);
1288:                }
1289:
1290:            }
1291:
1292:            /** <p>Retrieves the column names for the specified
1293:             *  database table and schema as a <code>Vector</code>
1294:             *  object.
1295:             *
1296:             *  @param the database table name
1297:             *  @param the database schema name
1298:             *  @return the column names <code>Vector</code>
1299:             */
1300:            public Vector<String> getColumnNamesVector(String table,
1301:                    String schema) throws DataSourceException {
1302:                ResultSet rs = null;
1303:                try {
1304:                    ensureConnection();
1305:                    if (schema == null) {
1306:                        schema = getSchemaName().toUpperCase();
1307:                    }
1308:
1309:                    DatabaseMetaData dmd = connection.getMetaData();
1310:                    rs = dmd.getColumns(null, schema, table, null);
1311:
1312:                    Vector<String> v = new Vector<String>();
1313:                    while (rs.next()) {
1314:                        v.add(rs.getString(4).toUpperCase());
1315:                    }
1316:                    return v;
1317:                } catch (SQLException e) {
1318:                    throw new DataSourceException(e);
1319:                } finally {
1320:                    releaseResources(rs);
1321:                }
1322:            }
1323:
1324:            /** <p>Retrieves the specified schema's
1325:             *  database table names within a <code>Vector</code>.
1326:             *
1327:             *  @return the table names
1328:             */
1329:            public Vector<String> getSchemaTables(String schema)
1330:                    throws DataSourceException {
1331:                ResultSet rs = null;
1332:                try {
1333:                    ensureConnection();
1334:                    String _schema = null;
1335:                    boolean valueFound = false;
1336:
1337:                    DatabaseMetaData dmd = connection.getMetaData();
1338:                    rs = dmd.getSchemas();
1339:
1340:                    while (rs.next()) {
1341:                        _schema = rs.getString(1);
1342:
1343:                        if (_schema.equalsIgnoreCase(schema)) {
1344:                            valueFound = true;
1345:                            break;
1346:                        }
1347:
1348:                    }
1349:
1350:                    rs.close();
1351:
1352:                    if (!valueFound) {
1353:                        _schema = schema;
1354:                    }
1355:
1356:                    String[] type = { "TABLE" };
1357:                    rs = dmd.getTables(null, _schema, null, type);
1358:
1359:                    Vector<String> v = new Vector<String>();
1360:
1361:                    while (rs.next()) {
1362:                        v.add(rs.getString(3));
1363:                    }
1364:
1365:                    rs.close();
1366:                    return v;
1367:                } catch (SQLException e) {
1368:                    throw new DataSourceException(e);
1369:                } finally {
1370:                    releaseResources(rs);
1371:                }
1372:
1373:            }
1374:
1375:            public Vector<ColumnData> getColumnMetaDataVector(String name,
1376:                    String schema, String catalog) throws DataSourceException {
1377:
1378:                ResultSet rs = null;
1379:
1380:                try {
1381:
1382:                    ensureConnection();
1383:                    if (schema == null) {
1384:                        schema = getSchemaName().toUpperCase();
1385:                    }
1386:
1387:                    DatabaseMetaData dmd = connection.getMetaData();
1388:
1389:                    if (!dmd.supportsCatalogsInTableDefinitions()) {
1390:                        catalog = null;
1391:                    }
1392:
1393:                    if (!dmd.supportsSchemasInTableDefinitions()) {
1394:                        schema = null;
1395:                    }
1396:
1397:                    rs = dmd.getColumns(catalog, schema, name, null);
1398:
1399:                    Vector v = new Vector();
1400:                    String columnName = null;
1401:
1402:                    while (rs.next()) {
1403:                        ColumnData cd = new ColumnData();
1404:                        cd.setColumnName(rs.getString(4));
1405:                        cd.setSQLType(rs.getInt(5));
1406:                        cd.setColumnType(rs.getString(6));
1407:                        cd.setColumnSize(rs.getInt(7));
1408:                        cd.setColumnRequired(rs.getInt(11));
1409:                        cd.setTableName(name);
1410:                        v.add(cd);
1411:                    }
1412:
1413:                    return v;
1414:                } catch (SQLException e) {
1415:                    throw new DataSourceException(e);
1416:                } finally {
1417:                    releaseResources(rs);
1418:                }
1419:
1420:            }
1421:
1422:            /** <p>Retrieves the complete column meta data
1423:             *  for the specified database table and schema.
1424:             *  <p>Each column and associated data is stored within
1425:             *  <code>ColumnData</code> objects and added to the
1426:             *  <code>Vector</code> object to be returned.
1427:             *
1428:             *  @param the database table name
1429:             *  @param the database schema name
1430:             *  @return the table column meta data
1431:             */
1432:            public Vector<ColumnData> getColumnMetaDataVector(String name,
1433:                    String schema) throws DataSourceException {
1434:                return getColumnMetaDataVector(name, schema, null);
1435:            }
1436:
1437:            public DatabaseObject[] getTables(String catalog, String schema,
1438:                    String[] types) throws DataSourceException {
1439:
1440:                ResultSet rs = null;
1441:                try {
1442:                    ensureConnection();
1443:                    DatabaseMetaData dmd = connection.getMetaData();
1444:                    rs = dmd.getTables(catalog, schema, null, types);
1445:
1446:                    if (rs == null) {
1447:                        return new DatabaseObject[0];
1448:                    }
1449:
1450:                    ArrayList list = new ArrayList();
1451:                    while (rs.next()) {
1452:                        DatabaseObject object = new DatabaseObject();
1453:                        object.setCatalogName(rs.getString(1));
1454:                        object.setSchemaName(rs.getString(2));
1455:                        object.setName(rs.getString(3));
1456:                        object.setMetaDataKey(rs.getString(4));
1457:                        object.setRemarks(rs.getString(5));
1458:                        list.add(object);
1459:                    }
1460:
1461:                    return (DatabaseObject[]) list
1462:                            .toArray(new DatabaseObject[list.size()]);
1463:
1464:                } catch (SQLException e) {
1465:                    throw new DataSourceException(e);
1466:                } finally {
1467:                    releaseResources(rs);
1468:                }
1469:            }
1470:
1471:            public static final int STRING_FUNCTIONS = 0;
1472:
1473:            public static final int TIME_DATE_FUNCTIONS = 1;
1474:
1475:            public static final int NUMERIC_FUNCTIONS = 2;
1476:
1477:            public String[] getSystemFunctions(int type)
1478:                    throws DataSourceException {
1479:                try {
1480:                    ensureConnection();
1481:                    DatabaseMetaData dmd = connection.getMetaData();
1482:                    String functions = null;
1483:
1484:                    switch (type) {
1485:
1486:                    case STRING_FUNCTIONS:
1487:                        functions = dmd.getStringFunctions();
1488:                        break;
1489:
1490:                    case TIME_DATE_FUNCTIONS:
1491:                        functions = dmd.getTimeDateFunctions();
1492:                        break;
1493:
1494:                    case NUMERIC_FUNCTIONS:
1495:                        functions = dmd.getNumericFunctions();
1496:                        break;
1497:
1498:                    }
1499:
1500:                    if (!MiscUtils.isNull(functions)) {
1501:                        StringTokenizer st = new StringTokenizer(functions, ",");
1502:
1503:                        List<String> list = new ArrayList<String>(st
1504:                                .countTokens());
1505:                        while (st.hasMoreTokens()) {
1506:                            list.add(st.nextToken());
1507:                        }
1508:
1509:                        return (String[]) list.toArray(new String[list.size()]);
1510:                    }
1511:
1512:                    return new String[0];
1513:                } catch (SQLException e) {
1514:                    throw new DataSourceException(e);
1515:                }
1516:
1517:            }
1518:
1519:            /**
1520:             * Returns the procedure term used in the current connected 
1521:             * database.
1522:             *
1523:             * @return the procedure term
1524:             */
1525:            public String getProcedureTerm() throws DataSourceException {
1526:                try {
1527:                    ensureConnection();
1528:                    DatabaseMetaData dmd = connection.getMetaData();
1529:                    return dmd.getProcedureTerm();
1530:                } catch (SQLException e) {
1531:                    throw new DataSourceException(e);
1532:                }
1533:            }
1534:
1535:            public String[] getProcedureNames(String catalog, String schema,
1536:                    String name) throws DataSourceException {
1537:                ResultSet rs = null;
1538:                try {
1539:                    ensureConnection();
1540:                    DatabaseMetaData dmd = connection.getMetaData();
1541:                    rs = dmd.getProcedures(catalog, schema, name);
1542:
1543:                    List list = new ArrayList();
1544:                    while (rs.next()) {
1545:                        list.add(rs.getString(3));
1546:                    }
1547:
1548:                    return (String[]) list.toArray(new String[list.size()]);
1549:                } catch (SQLException e) {
1550:                    throw new DataSourceException(e);
1551:                } finally {
1552:                    releaseResources(rs);
1553:                }
1554:            }
1555:
1556:            public String[] getTables(String catalog, String schema,
1557:                    String metaType) throws DataSourceException {
1558:
1559:                ResultSet rs = null;
1560:                try {
1561:                    ensureConnection();
1562:                    DatabaseMetaData dmd = connection.getMetaData();
1563:
1564:                    if (!dmd.supportsCatalogsInTableDefinitions()) {
1565:                        catalog = null;
1566:                    }
1567:
1568:                    if (!dmd.supportsSchemasInTableDefinitions()) {
1569:                        schema = null;
1570:                    }
1571:
1572:                    rs = dmd.getTables(catalog, schema, null,
1573:                            new String[] { metaType });
1574:
1575:                    if (rs != null) { // some odd null rs behaviour on some drivers
1576:                        ArrayList list = new ArrayList();
1577:                        while (rs.next()) {
1578:                            list.add(rs.getString(3));
1579:                        }
1580:                        return (String[]) list.toArray(new String[list.size()]);
1581:                    } else {
1582:                        return new String[0];
1583:                    }
1584:
1585:                } catch (SQLException e) {
1586:                    throw new DataSourceException(e);
1587:                } finally {
1588:                    releaseResources(rs);
1589:                }
1590:
1591:            }
1592:
1593:            /** <p>Executes the specified query (SELECT) and returns
1594:             *  a <code>ResultSet</code> object from this query.
1595:             *  <p>This is employed primarily by the Database Browser
1596:             *  to populate the 'Data' tab.
1597:             *
1598:             *  @param  the SQL query to execute
1599:             *  @return the query result
1600:             */
1601:            /*
1602:            public ResultSet getResultSet(String query) throws Exception {
1603:                Statement stmnt = null;
1604:                
1605:                try {
1606:                    ensureConnection();
1607:                    stmnt = connection.createStatement();
1608:                    return stmnt.executeQuery(query);            
1609:                } 
1610:                catch (SQLException e) {
1611:                    return null;
1612:                } 
1613:                catch (OutOfMemoryError e) {
1614:                    return null;
1615:                } 
1616:                
1617:                finally {
1618:                    try {
1619:                        if (stmnt != null) {
1620:                            stmnt.close();
1621:                        }
1622:                    }
1623:                    catch (SQLException sqlExc) {}
1624:                    releaseResources();
1625:                } 
1626:                
1627:            }*/
1628:
1629:            // ----------------------------------------------------------
1630:            // convenience methods for simple values from the connection
1631:            // ----------------------------------------------------------
1632:            /** <p>Retrieves the connected data source name.
1633:             *  @return the data source name
1634:             */
1635:            public String getDataSourceName() {
1636:                String name = databaseConnection.getSourceName();
1637:                return name == null ? "Not Available" : name.toUpperCase();
1638:            }
1639:
1640:            /** <p>Retrieves the connected port number.
1641:             *  @return the port number
1642:             */
1643:            public int getPort() {
1644:                return databaseConnection.getPortInt();
1645:            }
1646:
1647:            /** <p>Retrieves the connected user.
1648:             *  @return the user name
1649:             */
1650:            public String getUser() {
1651:                return databaseConnection.getUserName();
1652:            }
1653:
1654:            /** <p>Retrieves the connected JDBC URL.
1655:             *  @return the JDBC URL
1656:             */
1657:            public String getURL() {
1658:                return getDataSource().getJdbcUrl();
1659:            }
1660:
1661:            /** <p>Retrieves the connected host name.
1662:             *  @return the host name
1663:             */
1664:            public String getHost() {
1665:                String host = databaseConnection.getHost();
1666:                return host == null ? "Not Available" : host.toUpperCase();
1667:            }
1668:
1669:            /** 
1670:             * Retrieves the connected schema name.
1671:             *
1672:             * @return the schema name
1673:             */
1674:            public String getSchemaName() {
1675:                String schema = databaseConnection.getUserName();
1676:                return schema == null ? "Not Available" : schema.toUpperCase();
1677:            }
1678:
1679:            public String getCatalogName() {
1680:                String catalog = null;
1681:                try {
1682:                    ensureConnection();
1683:                    catalog = connection.getCatalog();
1684:                    if (MiscUtils.isNull(catalog)) {
1685:                        catalog = getSchemaName();
1686:                    }
1687:                } catch (SQLException e) {
1688:                } catch (DataSourceException e) {
1689:                } finally {
1690:                    releaseResources();
1691:                }
1692:                return catalog == null ? "Not Available" : catalog
1693:                        .toUpperCase();
1694:            }
1695:
1696:            private ConnectionDataSource getDataSource() {
1697:                return (ConnectionDataSource) ConnectionManager
1698:                        .getDataSource(databaseConnection);
1699:            }
1700:
1701:            /**
1702:             * Indicates a connection has been established.
1703:             * 
1704:             * @param the encapsulating event
1705:             */
1706:            public void connected(ConnectionEvent connectionEvent) {
1707:            }
1708:
1709:            /**
1710:             * Indicates a connection has been closed.
1711:             * 
1712:             * @param the encapsulating event
1713:             */
1714:            public void disconnected(ConnectionEvent connectionEvent) {
1715:                DatabaseConnection dc = connectionEvent.getSource();
1716:                if (connections.containsKey(dc)) {
1717:                    connections.remove(dc);
1718:                    // null out the connection if its the one disconnected
1719:                    if (databaseConnection == dc) {
1720:                        connection = null;
1721:                    }
1722:                }
1723:            }
1724:
1725:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.