empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From benni...@apache.org
Subject svn commit: r1142233 - /incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenParser.java
Date Sat, 02 Jul 2011 14:59:22 GMT
Author: benniven
Date: Sat Jul  2 14:59:22 2011
New Revision: 1142233

URL: http://svn.apache.org/viewvc?rev=1142233&view=rev
Log:
This commit is for EMPIREDB-110:
- querry the DatabaseMetaData for relations and populate the empire db model
- printed information to the log, which schemas and catalogs are available in the database

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=1142233&r1=1142232&r2=1142233&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
Sat Jul  2 14:59:22 2011
@@ -130,6 +130,7 @@ public class CodeGenParser extends Error
 	 */
 	private void populateDatabase(DBDatabase db) throws SQLException {
 		ResultSet tables = null;
+		ArrayList<String> populatedTables=new ArrayList<String>();
 		try{
             this.dbMeta = con.getMetaData();
             String[] tablePatterns = {null}; // Could be null, so start that way.
@@ -164,10 +165,14 @@ public class CodeGenParser extends Error
 					} else {
 						DBTable table = new DBTable(tableName, db);
 						populateTable(table);
+						populatedTables.add(tableName);
 						tableCount++;
 					}
 				}
-            }
+				
+				// Add all relations
+				gatherRelations(db, dbMeta, populatedTables);
+			}
 
 			if (tableCount==0 && viewCount==0) {
 			    // getTables returned no result
@@ -175,11 +180,87 @@ public class CodeGenParser extends Error
                 info += "/ schema="+config.getDbSchema(); 
                 info += "/ pattern="+config.getDbTablePattern(); 
 			    log.warn("DatabaseMetaData.getTables() returned no tables or views! Please check parameters:
"+info);
+				log.info("Available catalogs: " + getCatalogs(dbMeta));
+				log.info("Available schemata: " + getSchemata(dbMeta));
 			}
 		} finally {
 			DBUtil.close(tables, log);
 		}
 	}
+	
+	private void gatherRelations(DBDatabase db, DatabaseMetaData dbMeta, ArrayList<String>
tables) throws SQLException{
+		ResultSet relations = null;
+		String fkTableName, pkTableName, fkColName, pkColName, relName;
+		DBTableColumn fkCol, pkCol;
+		DBTable fkTable, pkTable;
+		DBColumn col;
+		
+		// Add all Relations
+		for (String tableName :tables) {
+			
+			// check for foreign-keys
+			relations = dbMeta.getImportedKeys(config.getDbCatalog(), config .getDbSchema(), tableName);
+			while (relations.next()) {
+				pkCol=fkCol=null;
+				
+				fkTableName=relations.getString("FKTABLE_NAME");
+				pkTableName=relations.getString("PKTABLE_NAME");
+				fkColName=relations.getString("FKCOLUMN_NAME");
+				pkColName=relations.getString("PKCOLUMN_NAME");
+				relName=fkTableName+"."+fkColName+"-"+pkTableName+"."+pkColName;
+				
+				pkTable = db.getTable(pkTableName);
+				fkTable = db.getTable(fkTableName);
+				
+				// check if both tables really exist in the model
+				if(pkTable==null || fkTable==null){
+					log.error("Unable to add the relation \""+relName+"\"! One of the tables could not be
found.");
+					continue;
+				}
+				
+				col=pkTable.getColumn(pkColName);
+				if(col instanceof DBTableColumn)
+					pkCol = (DBTableColumn) col;
+	
+				col=fkTable.getColumn(fkColName);
+				if(col instanceof DBTableColumn)
+					fkCol = (DBTableColumn) col;
+				
+				// check if both columns really exist in the model
+				if(fkCol==null || pkCol==null){
+					log.error("Unable to add the relation \""+relName+"\"! One of the columns could not
be found.");
+					continue;
+				}
+				
+				// add the relation
+				db.addRelation(fkCol.referenceOn(pkCol));
+				log.error("Added relation (FK-PK): "+relName);
+			}
+		}
+	}
+
+	private String getCatalogs(DatabaseMetaData dbMeta) throws SQLException {
+		String retVal = "";
+		ResultSet rs = dbMeta.getCatalogs();
+		while (rs.next()) {
+			retVal += rs.getString("TABLE_CAT") + ", ";
+		}
+		if(retVal.length()>2)
+			retVal=retVal.substring(0,retVal.length()-2);
+		
+		return retVal;
+	}
+
+	private String getSchemata(DatabaseMetaData dbMeta) throws SQLException {
+		String retVal = "";
+		ResultSet rs = dbMeta.getSchemas();
+		while (rs.next()) {
+			retVal += rs.getString("TABLE_SCHEM") + ", ";
+		}
+		if(retVal.length()>2)
+			retVal=retVal.substring(0,retVal.length()-2);
+		return retVal;
+	}
 
 	/**
 	 * queries the metadata for columns of a specific table and populates the



Mime
View raw message