Return-Path: Delivered-To: apmail-incubator-empire-db-commits-archive@minotaur.apache.org Received: (qmail 85341 invoked from network); 3 Dec 2009 23:11:11 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 3 Dec 2009 23:11:11 -0000 Received: (qmail 57950 invoked by uid 500); 3 Dec 2009 23:11:11 -0000 Delivered-To: apmail-incubator-empire-db-commits-archive@incubator.apache.org Received: (qmail 57931 invoked by uid 500); 3 Dec 2009 23:11:10 -0000 Mailing-List: contact empire-db-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: empire-db-dev@incubator.apache.org Delivered-To: mailing list empire-db-commits@incubator.apache.org Received: (qmail 57921 invoked by uid 99); 3 Dec 2009 23:11:10 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 03 Dec 2009 23:11:10 +0000 X-ASF-Spam-Status: No, hits=-2.5 required=5.0 tests=AWL,BAYES_00 X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 03 Dec 2009 23:11:08 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 286B8238888F; Thu, 3 Dec 2009 23:10:48 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r886977 - /incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenParser.java Date: Thu, 03 Dec 2009 23:10:47 -0000 To: empire-db-commits@incubator.apache.org From: francisdb@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20091203231048.286B8238888F@eris.apache.org> Author: francisdb Date: Thu Dec 3 23:10:46 2009 New Revision: 886977 URL: http://svn.apache.org/viewvc?rev=886977&view=rev Log: get rid of all toUpperCase() since there are databases (mysql) that are case-sensitive. If we want uppercase table variables in the code that is something to do in the CodeGenWriter. removed unneeded exception catching removed obsolete comments as they only clutter the code Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenParser.java Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenParser.java URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenParser.java?rev=886977&r1=886976&r2=886977&view=diff ============================================================================== --- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenParser.java (original) +++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenParser.java Thu Dec 3 23:10:46 2009 @@ -54,7 +54,6 @@ private DatabaseMetaData dbMeta; private Connection con; private CodeGenConfig config; - private DBDatabase db; /** * create a empty in memory Database and populates it @@ -67,72 +66,49 @@ * returns the populated DBDatabase */ public DBDatabase loadDbModel() { - this.db = new InMemoryDatabase(); + DBDatabase db = new InMemoryDatabase(); try { - // Get a JDBC Connection - con = getJDBCConnection(config); - - // create the database in memory - - this.dbMeta = con.getMetaData(); - populateDatabase(); - + con = openJDBCConnection(config); + populateDatabase(db); } catch (SQLException e) { - throw new RuntimeException("Unable to read database metadata!", e); + throw new RuntimeException("Unable to read database metadata: " + e.getMessage(), e); } - catch (Exception e) - { - log.error(e.getMessage(), e); - } finally { DBUtil.close(con, log); } - return db; + return db; } - // ----------- private members - - /** - *
+	/**
      * Opens and returns a JDBC-Connection.
      * JDBC url, user and password for the connection are obained from the SampleConfig bean
      * Please use the config.xml file to change connection params.
-     * 
*/ - private Connection getJDBCConnection(CodeGenConfig config) { - // Establish a new database connection + private Connection openJDBCConnection(CodeGenConfig config) throws SQLException{ + log.info("Connecting to Database'" + config.getJdbcURL() + "' / User=" + config.getJdbcUser()); Connection conn = null; - log.info("Connecting to Database'" + config.getJdbcURL() + "' / User=" - + config.getJdbcUser()); try { - // Connect to the databse Class.forName(config.getJdbcClass()).newInstance(); - conn = DriverManager.getConnection(config.getJdbcURL(), config - .getJdbcUser(), config.getJdbcPwd()); - log.info("Connected successfully"); - // set the AutoCommit to false this session. You must commit - // explicitly now - conn.setAutoCommit(true); - log.info("AutoCommit is " + conn.getAutoCommit()); - - } catch (Exception e) { - log.fatal("Failed to connect directly to '" + config.getJdbcURL() - + "' / User=" + config.getJdbcUser(), e); - throw new RuntimeException(e); + }catch(Exception ex){ + throw new SQLException("Could not load database driver: " + config.getJdbcClass()); } + conn = DriverManager.getConnection(config.getJdbcURL(), config.getJdbcUser(), config.getJdbcPwd()); + log.info("Connected successfully"); return conn; } /** - * queries the metadata of the database for tables and populates the + * Queries the metadata of the database for tables and populates the * database with those + * @throws SQLException */ - private void populateDatabase() { + private void populateDatabase(DBDatabase db) throws SQLException { ResultSet tables = null; - try { + try{ + this.dbMeta = con.getMetaData(); // Get table metadata tables = dbMeta.getTables( config.getDbCatalog(), @@ -140,30 +116,27 @@ config.getDbTablePattern(), new String[] { "TABLE" }); // Add all tables - int count = 0; + int tableCount = 0; while (tables.next()) { String tableName = tables.getString("TABLE_NAME"); - // Ignore system tables containing a '$' symbol (required for - // Oracle!) + // Ignore system tables containing a '$' symbol (required for Oracle!) if (tableName.indexOf('$') >= 0) { log.info("Ignoring system table " + tableName); continue; } - // end system table exclusion - log.info("Adding table " + tableName); - addTable(tableName); - count++; + log.info("TABLE: " + tableName); + DBTable table = new DBTable(tableName, db); + populateTable(table); + tableCount++; } - // Count added - if (count==0) { + + if (tableCount==0) { // getTables returned no result String info = "catalog="+config.getDbCatalog(); info += "/ schema="+config.getDbSchema(); info += "/ pattern="+config.getDbTablePattern(); log.warn("DatabaseMetaData.getTables() returned no tables! Please check parameters: "+info); } - } catch (SQLException e) { - error(e); } finally { DBUtil.close(tables, log); } @@ -172,23 +145,23 @@ /** * queries the metadata for columns of a specific table and populates the * table with that information + * @throws SQLException */ - private void addTable(String name) { - DBTable t = new DBTable(name.toUpperCase(), db); - List pkCols = this.findPkColumns(name); + private void populateTable(DBTable t) throws SQLException { + List pkCols = this.findPkColumns(t.getName()); String lockColName = config.getTimestampColumn(); DBColumn[] keys = new DBColumn[pkCols.size()]; ResultSet rs = null; try { rs = dbMeta.getColumns(config.getDbCatalog(), config.getDbSchema(), - name, null); + t.getName(), null); int i=0; while (rs.next()) { DBTableColumn c = addColumn(t, rs); // check if it is a KeyColumn - if (pkCols.contains(c.getName().toUpperCase())) + if (pkCols.contains(c.getName())) keys[i++] = c; - + // check if it is the Timestamp/Locking Column if (lockColName!=null && c.getName().equalsIgnoreCase(lockColName)) t.setTimestampColumn(c); @@ -197,10 +170,9 @@ for (i=0; i 0){ + t.setPrimaryKey(keys); + } } finally { DBUtil.close(rs, log); } @@ -209,19 +181,17 @@ /** * Returns a list of column names that define the primarykey of the given * table. + * @throws SQLException */ - private List findPkColumns(String tableName) { + private List findPkColumns(String tableName) throws SQLException { List cols = new ArrayList(); ResultSet rs = null; try { rs = dbMeta.getPrimaryKeys(config.getDbCatalog(), config .getDbSchema(), tableName); while (rs.next()) { - cols.add(rs.getString("COLUMN_NAME").toUpperCase()); + cols.add(rs.getString("COLUMN_NAME")); } - - } catch (SQLException e) { - throw new RuntimeException(e); } finally { DBUtil.close(rs, log); } @@ -242,7 +212,7 @@ if (rs.getString("IS_NULLABLE").equalsIgnoreCase("NO")) required = true; - log.info("\tCOLUMN:\t" + name); + log.info("\tCOLUMN:\t" + name + " ("+empireType+")"); return t.addColumn(name, empireType, colSize, required, defaultValue); } @@ -298,7 +268,7 @@ empireType = DataType.UNKNOWN; log.warn("SQL column type " + sqlType + " not supported."); } - log.info("Mapping date type " + String.valueOf(sqlType) + " to " + log.debug("Mapping date type " + String.valueOf(sqlType) + " to " + empireType); return empireType; }