Source Code Cross Referenced for DatabaseModelLoaderImpl.java in  » Database-ORM » ODAL » com » completex » objective » components » persistency » meta » impl » 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 ORM » ODAL » com.completex.objective.components.persistency.meta.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *  Objective Database Abstraction Layer (ODAL)
003:         *  Copyright (c) 2004, The ODAL Development Group
004:         *  All rights reserved.
005:         *  For definition of the ODAL Development Group please refer to LICENCE.txt file
006:         *
007:         *  Distributable under LGPL license.
008:         *  See terms of license at gnu.org.
009:         */package com.completex.objective.components.persistency.meta.impl;
010:
011:        import com.completex.objective.components.persistency.MetaColumn;
012:        import com.completex.objective.components.persistency.MetaTable;
013:        import com.completex.objective.components.persistency.ColumnType;
014:        import com.completex.objective.components.persistency.meta.MetaModel;
015:        import com.completex.objective.components.persistency.meta.ModelLoader;
016:        import com.completex.objective.util.TypeUtil;
017:
018:        import java.sql.Connection;
019:        import java.sql.DatabaseMetaData;
020:        import java.sql.ResultSet;
021:        import java.sql.ResultSetMetaData;
022:        import java.sql.SQLException;
023:        import java.sql.Statement;
024:        import java.util.Collection;
025:        import java.util.LinkedHashMap;
026:
027:        /**
028:         * @author Gennady Krizhevsky
029:         */
030:        public abstract class DatabaseModelLoaderImpl extends
031:                AbstractDatabaseModelLoader implements  ModelLoader {
032:
033:            private String schema;
034:            private String catalog;
035:
036:            abstract protected Connection getConnection() throws SQLException;
037:
038:            protected DatabaseModelLoaderImpl(String schema, String catalog,
039:                    String filterPattern) {
040:                this .schema = schema;
041:                this .catalog = catalog;
042:                this .filterPattern = filterPattern;
043:            }
044:
045:            public MetaModel load(MetaModel model) throws Exception {
046:                debug("DatabaseModelLoaderImpl.load:: enter");
047:                if (model == null) {
048:                    model = new MetaModel(filterPattern);
049:                }
050:                Connection connection = getConnection();
051:                DatabaseMetaData metaData = connection.getMetaData();
052:                ResultSet tablesResultSet = null;
053:                ResultSet columnsResultSet = null;
054:                ResultSet primaryKeysResultSet = null;
055:                ResultSet typeInfoResultSet = null;
056:                debug("model = " + model);
057:                info("schema = " + schema);
058:                try {
059:                    typeInfoResultSet = metaData.getTypeInfo();
060:                    while (typeInfoResultSet.next()) {
061:                        debug("TYPE_NAME: "
062:                                + typeInfoResultSet.getString("TYPE_NAME"));
063:                    }
064:
065:                    /*
066:                     1. TYPE_CAT String => the type's catalog (may be null)
067:                     2. TYPE_SCHEM String => type's schema (may be null)
068:                     3. TYPE_NAME String => type name
069:                     4. CLASS_NAME String => Java class name
070:                     5. DATA_TYPE int => type value defined in java.sql.Types. One of JAVA_OBJECT, STRUCT, or DISTINCT
071:                     6. REMARKS String => explanatory comment on the type
072:                     7. BASE_TYPE short => type code of the source type of a DISTINCT type or the type that implements the user-generated reference type of the SELF_REFERENCING_COLUMN of a structured type as defined in java.sql.Types (null if DATA_TYPE is not DISTINCT or not STRUCT with REFERENCE_GENERATION = USER_DEFINED)
073:                     */
074:
075:                    tablesResultSet = metaData.getTables(null, schema, "%",
076:                            new String[] { "TABLE" });
077:                    while (tablesResultSet.next()) {
078:                        String tableName = tablesResultSet
079:                                .getString("TABLE_NAME");
080:
081:                        for (int i = 1; i < tablesResultSet.getMetaData()
082:                                .getColumnCount(); i++) {
083:                            Object name = tablesResultSet.getObject(i);
084:                            debug("Tables col name " + name);
085:                        }
086:
087:                        if (!model.isTableAllowed(tableName)) {
088:                            continue;
089:                        }
090:
091:                        MetaTable table = defineTable(model, tableName);
092:                        if (table == null) {
093:                            continue;
094:                        }
095:
096:                        Statement statement = connection.createStatement();
097:                        ResultSet rsColsInfo = statement
098:                                .executeQuery("select * from " + tableName
099:                                        + " where 0=1");
100:                        ResultSetMetaData rsMetaColsInfo = rsColsInfo
101:                                .getMetaData();
102:
103:                        LinkedHashMap colsInfo = new LinkedHashMap();
104:                        for (int i = 1; i <= rsMetaColsInfo.getColumnCount(); i++) {
105:                            String columnName = rsMetaColsInfo.getColumnName(i);
106:
107:                            debug("Columns col " + "name ["
108:                                    + rsMetaColsInfo.getColumnName(i)
109:                                    + "]; isAutoIncrement ["
110:                                    + rsMetaColsInfo.isAutoIncrement(i) + "]");
111:                            ColumnInfo columnInfo = new ColumnInfo(columnName,
112:                                    rsMetaColsInfo.isAutoIncrement(i),
113:                                    rsMetaColsInfo.getColumnType(i),
114:                                    rsMetaColsInfo.getColumnTypeName(i));
115:                            colsInfo.put(columnName, columnInfo);
116:                            debug(columnInfo.toString());
117:                        }
118:
119:                        rsColsInfo.close();
120:                        statement.close();
121:
122:                        columnsResultSet = metaData.getColumns(null, schema,
123:                                tableName, null);
124:                        int columnIndex = 0;
125:                        while (columnsResultSet.next()) {
126:
127:                            String columnName = columnsResultSet
128:                                    .getString("COLUMN_NAME");
129:                            String columnDefaultValue = columnsResultSet
130:                                    .getString("COLUMN_DEF");
131:                            int columnSize = columnsResultSet
132:                                    .getInt("COLUMN_SIZE");
133:                            int decimalDigits = columnsResultSet
134:                                    .getInt("DECIMAL_DIGITS");
135:                            ColumnInfo columnInfo = ((ColumnInfo) colsInfo
136:                                    .get(columnName));
137:
138:                            String nullable = columnsResultSet
139:                                    .getString("IS_NULLABLE");
140:                            boolean required = !TypeUtil.S2b(nullable,
141:                                    TypeUtil.YES_NO);
142:                            String dataType = String
143:                                    .valueOf(columnInfo.jdbcType);
144:                            ColumnType columnType = columnType(dataType,
145:                                    columnSize, decimalDigits, required);
146:                            MetaColumn column = defineColumn(model, table,
147:                                    columnName, columnInfo.jdbcType,
148:                                    columnType, nullable, columnsResultSet
149:                                            .getString("REMARKS"),
150:                                    columnDefaultValue, columnSize,
151:                                    decimalDigits, columnInfo);
152:                            column.setColumnIndex(columnIndex++);
153:                        }
154:                        closeRs(columnsResultSet);
155:                        columnsResultSet = null;
156:                        //                debug("Columns for tableName [" + tableName + "]: " + model.getTable(tableName));
157:                        primaryKeysResultSet = metaData.getPrimaryKeys(null,
158:                                schema, tableName);
159:                        String pkColumnName;
160:                        while (primaryKeysResultSet.next()) {
161:                            pkColumnName = primaryKeysResultSet
162:                                    .getString("COLUMN_NAME");
163:                            definePrimaryKey(model, tableName, pkColumnName);
164:                        }
165:                        Collection foreignKeys = getForeignKeys(model,
166:                                metaData, schema, tableName);
167:                        if (foreignKeys.size() > 0) {
168:                            populateForeignKeysToModel(model, tableName,
169:                                    foreignKeys);
170:                        }
171:
172:                        closeRs(primaryKeysResultSet);
173:                        primaryKeysResultSet = null;
174:                    }
175:                    closeRs(tablesResultSet);
176:                    tablesResultSet = null;
177:                } catch (SQLException e) {
178:                    throw new Exception("Cannot get database meta-data", e);
179:                } finally {
180:                    closeRs(tablesResultSet);
181:                    closeRs(columnsResultSet);
182:                    closeRs(primaryKeysResultSet);
183:                    closeRs(typeInfoResultSet);
184:                    debug("DatabaseModelLoaderImpl.load:: exit");
185:                }
186:                return model;
187:            }
188:
189:            public String getSchema() {
190:                return schema;
191:            }
192:
193:            public String getCatalog() {
194:                return catalog;
195:            }
196:
197:        }
w_w_w__.__j___av_a___2___s___.__c_o_m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.