empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1150343 - in /incubator/empire-db/branches/EMPIREDB-99: empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java empire-db/src/main/java/org/apache/empire/data/bean/BeanResult.java
Date Sun, 24 Jul 2011 11:45:37 GMT
Author: doebele
Date: Sun Jul 24 11:45:36 2011
New Revision: 1150343

URL: http://svn.apache.org/viewvc?rev=1150343&view=rev
Log:
EMPIREDB-99
small API-Improvement - BeanResult

Added:
    incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/data/bean/BeanResult.java
  (with props)
Modified:
    incubator/empire-db/branches/EMPIREDB-99/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java

Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java?rev=1150343&r1=1150342&r2=1150343&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
(original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db-examples/empire-db-example-basic/src/main/java/org/apache/empire/samples/db/SampleApp.java
Sun Jul 24 11:45:36 2011
@@ -23,6 +23,7 @@ import java.sql.DriverManager;
 import java.util.List;
 
 import org.apache.empire.commons.StringUtils;
+import org.apache.empire.data.bean.BeanResult;
 import org.apache.empire.db.DBColumnExpr;
 import org.apache.empire.db.DBCommand;
 import org.apache.empire.db.DBDatabaseDriver;
@@ -141,6 +142,9 @@ public class SampleApp 
 			System.out.println("*** Step 8 Option 3: queryRecords() / XML-Output ***");
 			queryRecords(conn, QueryType.XmlDocument); // XML-Output
 
+			// STEP 9: Use Bean Result to query beans
+			queryBeans(conn);
+			
 			// Done
 			System.out.println("DB Sample finished successfully.");
 
@@ -373,8 +377,7 @@ public class SampleApp 
 		// Select required columns
 		cmd.select(EMP.EMPLOYEE_ID, EMPLOYEE_FULLNAME);
 		if(db.getDriver() instanceof DBDatabaseDriverPostgreSQL)
-		{
-			// postgres does not support the substring expression
+		{	// postgres does not support the substring expression
 			cmd.select(EMP.GENDER, EMP.PHONE_NUMBER);
 		}else{
 			cmd.select(EMP.GENDER, EMP.PHONE_NUMBER, PHONE_EXT_NUMBER);
@@ -444,4 +447,20 @@ public class SampleApp 
 		}
 	}
 	
+	private static void queryBeans(Connection conn)
+	{
+        DBCommand cmd = db.createCommand();
+        cmd.select(db.EMPLOYEES.getColumns());
+        cmd.where (db.EMPLOYEES.GENDER.is("M"));
+	    
+        // Query all males
+	    BeanResult<SampleBean> result = new BeanResult<SampleBean>(SampleBean.class,
cmd);
+	    result.fetch(conn);
+
+	    // And now, the females
+	    result.getCommand().where(db.EMPLOYEES.GENDER.is("F"));
+	    result.fetch(conn);
+	    
+	}
+	
 }

Added: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/data/bean/BeanResult.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/data/bean/BeanResult.java?rev=1150343&view=auto
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/data/bean/BeanResult.java
(added)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/data/bean/BeanResult.java
Sun Jul 24 11:45:36 2011
@@ -0,0 +1,60 @@
+/*
+ * ESTEAM Software GmbH, 24.07.2011
+ */
+package org.apache.empire.data.bean;
+
+import java.sql.Connection;
+import java.util.ArrayList;
+
+import org.apache.empire.db.DBCommand;
+import org.apache.empire.db.DBReader;
+
+/**
+ * BeanResult
+ * This is a simple helper class that performs reading a list of beans from a query
+ * Internally DBReader.getBeanList() is used.
+ *  
+ * @author doebele
+ */
+public class BeanResult<T extends Object> extends ArrayList<T>
+{
+    /**
+     * Comment for <code>serialVersionUID</code>
+     */
+    private static final long serialVersionUID = 1L;
+
+    private DBCommand cmd;
+    private Class<T> type;
+    
+    public BeanResult(Class<T> type, DBCommand cmd)
+    {
+        this.type = type;
+        this.cmd = cmd;
+    }
+    
+    public DBCommand getCommand()
+    {
+        return cmd;
+    }
+    
+    public int fetch(Connection conn, int maxItems)
+    {
+        clear();
+        DBReader reader = new DBReader();
+        try {
+            // Open and Read
+            reader.open(cmd, conn);
+            reader.getBeanList(this, type, maxItems);
+            return size();
+            
+        } finally {
+            reader.close();
+        }
+    }
+
+    public final int fetch(Connection conn)
+    {
+        return fetch(conn, -1);
+    }
+    
+}

Propchange: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/data/bean/BeanResult.java
------------------------------------------------------------------------------
    eol-style = native

Propchange: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/data/bean/BeanResult.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message