empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From franci...@apache.org
Subject svn commit: r905455 - in /incubator/empire-db/trunk: empire-db-codegen/src/main/java/org/apache/empire/db/codegen/ empire-db-codegen/src/main/resources/templates/ empire-db-examples/empire-db-example-codegen/ empire-db-examples/empire-db-example-codege...
Date Mon, 01 Feb 2010 23:29:15 GMT
Author: francisdb
Date: Mon Feb  1 23:29:14 2010
New Revision: 905455

URL: http://svn.apache.org/viewvc?rev=905455&view=rev
Log:
EMPIREDB-64
Fix codegen issues
Better example application that actually uses the generated code and works in eclipse (should
later maybe be converted to the maven-plugin integration test)

Added:
    incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/
    incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/hsqldb/
    incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/hsqldb/sample.properties
    incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/hsqldb/sample.script
    incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/log4j.properties
Modified:
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenConfig.java
    incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java
    incubator/empire-db/trunk/empire-db-codegen/src/main/resources/templates/Table.vm
    incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/pom.xml
    incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/java/org/apache/empire/db/examples/codegen/ValidatePluginRunTest.java
    incubator/empire-db/trunk/empire-db-maven-plugin/pom.xml
    incubator/empire-db/trunk/empire-db-maven-plugin/src/main/java/org/apache/empire/db/maven/CodeGenMojo.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenConfig.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenConfig.java?rev=905455&r1=905454&r2=905455&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenConfig.java
(original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenConfig.java
Mon Feb  1 23:29:14 2010
@@ -90,13 +90,13 @@
 	 * Target name of the generated table class. This class extends DBTable and
 	 * is the base class for all generated individual table classes.
 	 */
-	private String tableBaseName = "MyTable";
+	private String tableBaseName = "BaseTable";
 	
 	/**
 	 * Target name of the generated view class. This class extends DBView and is
 	 * the base class for all generated individual view classes.
 	 */
-	private String viewBaseName = "MyView";
+	private String viewBaseName = "BaseView";
 	
 	/**
 	 * Target name of the generated record class. This is a template class that

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java?rev=905455&r1=905454&r2=905455&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java
(original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/java/org/apache/empire/db/codegen/CodeGenWriter.java
Mon Feb  1 23:29:14 2010
@@ -298,6 +298,7 @@
 		String templatePath = templatePathFor(template);
 		Writer writer = null;
 		try {
+			log.info("Writing " + file);
 			Template velocityTemplate = Velocity.getTemplate(templatePath);
 			writer = new FileWriter(file);
 			velocityTemplate.merge(context, writer);

Modified: incubator/empire-db/trunk/empire-db-codegen/src/main/resources/templates/Table.vm
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-codegen/src/main/resources/templates/Table.vm?rev=905455&r1=905454&r2=905455&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-codegen/src/main/resources/templates/Table.vm (original)
+++ incubator/empire-db/trunk/empire-db-codegen/src/main/resources/templates/Table.vm Mon
Feb  1 23:29:14 2010
@@ -16,7 +16,6 @@
  * specific language governing permissions and limitations
  * under the License.
  *#
-
 #if($nestTables == false)
 package $tablePackageName;
 
@@ -28,9 +27,9 @@
 import $basePackageName.${dbClassName};
 #end
 
-public #if($nestTables == true)static#end  class $parser.getTableClassName($table.name) extends
${baseTableClassName} {
+public #if($nestTables == true)static#end class $parser.getTableClassName($table.name) extends
${baseTableClassName} {
 	// Primary key column
-	private List<DBTableColumn> keyColumns= new ArrayList<DBTableColumn>();
+	private List<DBTableColumn> keyColumns = new ArrayList<DBTableColumn>();
 	
 	// Regular attributes
 #foreach ($col in $table.columns)
@@ -38,11 +37,11 @@
 #end
 	
 	public $parser.getTableClassName($table.name)(${dbClassName} db) {
-		super("$parser.getTableClassName($table.name)", db);
+		super("$table.name", db);
 		
 		// all columns
 #foreach ($col in $table.columns)
-		C_${parser.getAttributeName($col)} = super.addColumn("${col.name}", DataType.${parser.getDataType($col)},
${col.size}, ${col.isRequired()}, ${parser.getDefaultValue($col)});
+		C_${parser.getAttributeName($col)} = addColumn("${col.name}", DataType.${parser.getDataType($col)},
${col.size}, ${col.isRequired()}, ${parser.getDefaultValue($col)});
 #end
 
 
@@ -50,16 +49,16 @@
 #foreach ($col in $table.keyColumns)
     	keyColumns.add(C_${col.name});
 #end
-		super.setPrimaryKey((DBTableColumn[]) keyColumns.toArray());
+		setPrimaryKey(keyColumns.toArray(new DBTableColumn[keyColumns.size()]));
 		
 		// Optimistic locking column
 #if($parser.hasLockingColumn($table) == true)
-		super.setTimestampColumn(C_${parser.getAttributeName($table.getTimestampColumn())});
+		setTimestampColumn(C_${parser.getAttributeName($table.getTimestampColumn())});
 #else
 		/*no locking column specified*/
 #end
 
 		// Set cascade delete
-		super.setCascadeDelete(true);		
+		setCascadeDelete(true);		
 	}
 }

Modified: incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/pom.xml
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/pom.xml?rev=905455&r1=905454&r2=905455&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/pom.xml (original)
+++ incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/pom.xml Mon Feb
 1 23:29:14 2010
@@ -35,7 +35,7 @@
 				<configuration>
 					<goalPrefix>empiredb</goalPrefix>
 					<jdbcClass>org.hsqldb.jdbcDriver</jdbcClass>
-					<jdbcURL>jdbc:hsqldb:file:target/hsqldb/sample;shutdown=true</jdbcURL>
+					<jdbcURL>jdbc:hsqldb:file:${project.basedir}/src/test/resources/hsqldb/sample;shutdown=true</jdbcURL>
 					<jdbcUser>sa</jdbcUser>
 					<jdbcPwd></jdbcPwd>
 					<packageName>org.apache.empire.db.example</packageName>
@@ -60,6 +60,10 @@
 			<groupId>org.apache.empire-db</groupId>
 			<artifactId>empire-db</artifactId>
 		</dependency>
+		<dependency>
+		    <groupId>hsqldb</groupId>
+		    <artifactId>hsqldb</artifactId>
+		</dependency> 
 	</dependencies>
 	
 </project>
\ No newline at end of file

Modified: incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/java/org/apache/empire/db/examples/codegen/ValidatePluginRunTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/java/org/apache/empire/db/examples/codegen/ValidatePluginRunTest.java?rev=905455&r1=905454&r2=905455&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/java/org/apache/empire/db/examples/codegen/ValidatePluginRunTest.java
(original)
+++ incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/java/org/apache/empire/db/examples/codegen/ValidatePluginRunTest.java
Mon Feb  1 23:29:14 2010
@@ -18,27 +18,90 @@
  */
 package org.apache.empire.db.examples.codegen;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
-
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.apache.empire.db.DBCommand;
+import org.apache.empire.db.DBDatabaseDriver;
+import org.apache.empire.db.DBReader;
+import org.apache.empire.db.example.MyDB;
+import org.apache.empire.db.example.tables.TEMPLOYEESTable;
+import org.apache.empire.db.hsql.DBDatabaseDriverHSql;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 import org.junit.Test;
 
-
 public class ValidatePluginRunTest {
-	
+
+	private static Connection conn = null;
+
+	@BeforeClass
+	public static void openConnection() throws Exception {
+		Class.forName("org.hsqldb.jdbcDriver").newInstance();
+		conn = DriverManager.getConnection("jdbc:hsqldb:file:src/test/resources/hsqldb/sample;shutdown=true",
"sa", "");
+	}
+
+	@AfterClass
+	public static void closeConnection() throws Exception {
+		if (conn != null) {
+			try {
+				Statement st = conn.createStatement();
+				// properly shutdown hsqldb
+				st.execute("SHUTDOWN");
+			} catch (SQLException ex) {
+				ex.printStackTrace();
+			}
+			conn.close();
+		}
+	}
+
 	@Test
-	public void testTargetFolder(){
+	public void testTargetFolder() {
 		File file = new File("target/generated-sources/empiredb");
 		assertTrue("No sources generated", file.exists());
 		// TODO add extra validation for the real generated sources
 	}
-	
+
 	@Test
-	public void testGeneratedClass() throws ClassNotFoundException{
+	public void testGeneratedClass() throws ClassNotFoundException {
 		Class<?> cls = Class.forName("org.apache.empire.db.example.MyDB");
 		assertNotNull("Could not load generated class.", cls);
 	}
 
+	@Test
+	public void useGeneratedCode() throws Exception {
+
+		MyDB db = MyDB.get();
+		TEMPLOYEESTable EMP = db.T_EMPLOYEES;
+
+		DBDatabaseDriver driver = new DBDatabaseDriverHSql();
+		db.open(driver, conn);
+		DBCommand cmd = db.createCommand();
+		cmd.select(EMP.C_EMPLOYEE_ID, EMP.C_FIRSTNAME);
+
+		int rowCount = 0;
+		DBReader reader = new DBReader();
+		try {
+			System.err.println(cmd.getSelect());
+			boolean succes = reader.open(cmd, conn);
+			assertTrue(db.getErrorMessage(), succes);
+			while (reader.moveNext()) {
+				rowCount++;
+				System.out.println(reader.getString(EMP.C_EMPLOYEE_ID) + "\t" + reader.getString(EMP.C_FIRSTNAME));
+			}
+		} finally {
+			reader.close();
+		}
+		
+		assertEquals("We expect 3 rows", 3, rowCount);
+
+	}
+
 }

Added: incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/hsqldb/sample.properties
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/hsqldb/sample.properties?rev=905455&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/hsqldb/sample.properties
(added)
+++ incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/hsqldb/sample.properties
Mon Feb  1 23:29:14 2010
@@ -0,0 +1,17 @@
+#HSQL Database Engine 1.8.0.10
+#Tue Feb 02 00:26:54 CET 2010
+hsqldb.script_format=0
+runtime.gc_interval=0
+sql.enforce_strict_size=false
+hsqldb.cache_size_scale=8
+readonly=false
+hsqldb.nio_data_file=true
+hsqldb.cache_scale=14
+version=1.8.0
+hsqldb.default_table_type=memory
+hsqldb.cache_file_scale=1
+hsqldb.log_size=200
+modified=no
+hsqldb.cache_version=1.7.0
+hsqldb.original_version=1.8.0
+hsqldb.compatible_version=1.8.0

Added: incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/hsqldb/sample.script
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/hsqldb/sample.script?rev=905455&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/hsqldb/sample.script
(added)
+++ incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/hsqldb/sample.script
Mon Feb  1 23:29:14 2010
@@ -0,0 +1,16 @@
+CREATE SCHEMA PUBLIC AUTHORIZATION DBA
+CREATE SEQUENCE DEP_ID_SEQUENCE AS INTEGER START WITH 3 
+CREATE SEQUENCE EMPLOYEE_ID_SEQUENCE AS INTEGER START WITH 4 
+CREATE MEMORY TABLE DEPARTMENTS(DEPARTMENT_ID BIGINT NOT NULL,NAME VARCHAR(80) NOT NULL,HEAD
VARCHAR(80),BUSINESS_UNIT VARCHAR(4) NOT NULL,UPDATE_TIMESTAMP TIMESTAMP NOT NULL,CONSTRAINT
DEPARTMENTS_PK PRIMARY KEY(DEPARTMENT_ID))
+CREATE UNIQUE INDEX DEARTMENT_NAME_IDX ON DEPARTMENTS(NAME)
+CREATE MEMORY TABLE EMPLOYEES(EMPLOYEE_ID BIGINT NOT NULL,SALUTATION VARCHAR(20),FIRSTNAME
VARCHAR(40) NOT NULL,LASTNAME VARCHAR(40) NOT NULL,DATE_OF_BIRTH DATE,DEPARTMENT_ID BIGINT
NOT NULL,GENDER VARCHAR(1),PHONE_NUMBER VARCHAR(40),EMAIL VARCHAR(80),SALARY DECIMAL(10,2),RETIRED
BOOLEAN NOT NULL,UPDATE_TIMESTAMP TIMESTAMP NOT NULL,CONSTRAINT EMPLOYEES_PK PRIMARY KEY(EMPLOYEE_ID),CONSTRAINT
EMPLOYEES_DEPARTMENT_I_FK FOREIGN KEY(DEPARTMENT_ID) REFERENCES DEPARTMENTS(DEPARTMENT_ID))
+CREATE UNIQUE INDEX EMPLOYEE_NAME_IDX ON EMPLOYEES(FIRSTNAME,LASTNAME,DATE_OF_BIRTH)
+CREATE USER SA PASSWORD ""
+GRANT DBA TO SA
+SET WRITE_DELAY 10
+SET SCHEMA PUBLIC
+INSERT INTO DEPARTMENTS VALUES(1,'Development',NULL,'ITTK','2010-02-01 22:27:44.200000000')
+INSERT INTO DEPARTMENTS VALUES(2,'Sales',NULL,'ITTK','2010-02-01 22:27:44.212000000')
+INSERT INTO EMPLOYEES VALUES(1,NULL,'Peter','Sharp',NULL,1,'M','+49-7531-457160',NULL,NULL,FALSE,'2010-02-01
22:27:44.226000000')
+INSERT INTO EMPLOYEES VALUES(2,NULL,'Fred','Bloggs',NULL,1,'M','+49-5555-505050',NULL,NULL,FALSE,'2010-02-01
22:27:44.240000000')
+INSERT INTO EMPLOYEES VALUES(3,NULL,'Emma','White',NULL,2,'F','+49-040-125486',NULL,NULL,FALSE,'2010-02-01
22:27:44.243000000')

Added: incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/log4j.properties?rev=905455&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/log4j.properties
(added)
+++ incubator/empire-db/trunk/empire-db-examples/empire-db-example-codegen/src/test/resources/log4j.properties
Mon Feb  1 23:29:14 2010
@@ -0,0 +1,21 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+log4j.rootCategory=FATAL, console
+log4j.appender.console=org.apache.log4j.ConsoleAppender
+log4j.appender.console.layout=org.apache.log4j.PatternLayout
+log4j.appender.console.layout.conversionPattern = %d{ISO8601} %-5p [%c] - %m%n

Modified: incubator/empire-db/trunk/empire-db-maven-plugin/pom.xml
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-maven-plugin/pom.xml?rev=905455&r1=905454&r2=905455&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-maven-plugin/pom.xml (original)
+++ incubator/empire-db/trunk/empire-db-maven-plugin/pom.xml Mon Feb  1 23:29:14 2010
@@ -42,6 +42,10 @@
 			<version>2.0</version>
 		</dependency>
 		<dependency>
+			<groupId>log4j</groupId>
+			<artifactId>log4j</artifactId>
+		</dependency>
+		<dependency>
 			<groupId>junit</groupId>
 			<artifactId>junit</artifactId>
 			<scope>test</scope>

Modified: incubator/empire-db/trunk/empire-db-maven-plugin/src/main/java/org/apache/empire/db/maven/CodeGenMojo.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db-maven-plugin/src/main/java/org/apache/empire/db/maven/CodeGenMojo.java?rev=905455&r1=905454&r2=905455&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db-maven-plugin/src/main/java/org/apache/empire/db/maven/CodeGenMojo.java
(original)
+++ incubator/empire-db/trunk/empire-db-maven-plugin/src/main/java/org/apache/empire/db/maven/CodeGenMojo.java
Mon Feb  1 23:29:14 2010
@@ -24,6 +24,10 @@
 import org.apache.empire.db.codegen.CodeGenConfig;
 import org.apache.empire.db.codegen.CodeGenParser;
 import org.apache.empire.db.codegen.CodeGenWriter;
+import org.apache.log4j.AppenderSkeleton;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.spi.LoggingEvent;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
@@ -36,7 +40,7 @@
  * @phase generate-sources
  */
 public class CodeGenMojo extends AbstractMojo {
-	
+
 	/**
 	 * @parameter expression="${project}" 
 	 * @readonly
@@ -108,6 +112,8 @@
 
 	public void execute() throws MojoExecutionException {
 		
+		setupLogging();
+		
 		CodeGenConfig config = new CodeGenConfig();
 		if(configFile != null){
 			getLog().info("Loading configuration file: " + configFile);
@@ -122,6 +128,8 @@
 			config.setPackageName(packageName);
 		}
 		
+		//config.setExceptionsEnabled(true);
+		
 		getLog().info("Generating code for " + jdbcURL + " ...");
 		
 		CodeGenParser parser = new CodeGenParser(config);
@@ -138,5 +146,44 @@
 		project.addCompileSourceRoot(targetDirectory.getAbsolutePath());
 		
 	}
+
+	private void setupLogging() {
+		Logger logger = Logger.getRootLogger();
+		logger.addAppender(new MavenAppender(this));
+	}
 	
+	/**
+	 * Forwards Log4j logging to maven logging
+	 * 
+	 */
+	private static final class MavenAppender extends AppenderSkeleton {
+		
+		private final AbstractMojo mojo;
+		
+		public MavenAppender(final AbstractMojo mojo) {
+			this.mojo = mojo;
+		}
+		
+		@Override
+		public boolean requiresLayout() {
+			return false;
+		}
+
+		@Override
+		public void close() {
+			// nothing to do here
+		}
+
+		@Override
+		protected void append(LoggingEvent event) {
+			if(Level.INFO.equals(event.getLevel())){
+				mojo.getLog().info(String.valueOf(event.getMessage()));
+			}else if(Level.ERROR.equals(event.getLevel())){
+				// TODO support throwables?
+				mojo.getLog().error(String.valueOf(event.getMessage()));
+			}else if(Level.WARN.equals(event.getLevel())){
+				mojo.getLog().warn(String.valueOf(event.getMessage()));
+			}
+		}
+	}
 }

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java?rev=905455&r1=905454&r2=905455&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
Mon Feb  1 23:29:14 2010
@@ -589,6 +589,7 @@
      */
     public DBCommand createCommand()
     {
+        // TODO whouldn't it be better to throw an exception here?
         if (checkOpen()==false)
             return null;
         return driver.createCommand(this);



Mime
View raw message