empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From franci...@apache.org
Subject svn commit: r1199939 - in /incubator/empire-db/trunk/empire-db/src: main/java/org/apache/empire/db/DBDatabaseDriver.java test/java/org/apache/empire/db/mssql/DBDatabaseDriverMSSqlTest.java
Date Wed, 09 Nov 2011 19:56:01 GMT
Author: francisdb
Date: Wed Nov  9 19:56:00 2011
New Revision: 1199939

URL: http://svn.apache.org/viewvc?rev=1199939&view=rev
Log:
EMPIREDB-122
Chinese characters being inserted as ??? on SqlServer (non-prepared statement)

Modified:
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
    incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/mssql/DBDatabaseDriverMSSqlTest.java

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java?rev=1199939&r1=1199938&r2=1199939&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
Wed Nov  9 19:56:00 2011
@@ -661,6 +661,8 @@ public abstract class DBDatabaseDriver i
             case UNIQUEID:
             {   // Text value
                 StringBuilder valBuf = new StringBuilder();
+                // for SQLSERVER utf8 support, see EMPIREDB-122
+                // valBuf.append("N'");
                 valBuf.append("'");
                 if (DBDatabase.EMPTY_STRING.equals(value)==false)
                     appendTextValue(valBuf, value.toString());

Modified: incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/mssql/DBDatabaseDriverMSSqlTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/mssql/DBDatabaseDriverMSSqlTest.java?rev=1199939&r1=1199938&r2=1199939&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/mssql/DBDatabaseDriverMSSqlTest.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/mssql/DBDatabaseDriverMSSqlTest.java
Wed Nov  9 19:56:00 2011
@@ -25,6 +25,7 @@ import java.sql.Connection;
 import org.apache.empire.DBResource;
 import org.apache.empire.DBResource.DB;
 import org.apache.empire.db.CompanyDB;
+import org.apache.empire.db.DBCommand;
 import org.apache.empire.db.DBDatabaseDriver;
 import org.apache.empire.db.DBRecord;
 import org.apache.empire.db.DBSQLScript;
@@ -32,6 +33,7 @@ import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 
+@Ignore
 public class DBDatabaseDriverMSSqlTest 
 {
 
@@ -39,7 +41,6 @@ public class DBDatabaseDriverMSSqlTest 
 	public DBResource dbResource = new DBResource(DB.MSSQL);
 
 	@Test
-	@Ignore
 	public void testChineseCharacters() 
 	{
 		Connection conn = dbResource.getConnection();
@@ -48,14 +49,20 @@ public class DBDatabaseDriverMSSqlTest 
 		CompanyDB db = new CompanyDB();
 
 		// Encoding issue occur when prepared statement is disabled
-		// db.setPreparedStatementsEnabled(true);
+		//db.setPreparedStatementsEnabled(true);
 
 		db.open(driver, dbResource.getConnection());
 
-		DBSQLScript script = new DBSQLScript();
-		db.getCreateDDLScript(db.getDriver(), script);
-		System.out.println(script.toString());
-		script.run(db.getDriver(), dbResource.getConnection(), false);
+		if(!databaseExists(conn, db)){
+			DBSQLScript script = new DBSQLScript();
+			db.getCreateDDLScript(db.getDriver(), script);
+			System.out.println(script.toString());
+			script.run(db.getDriver(), dbResource.getConnection(), false);
+		}
+		
+		// STEP 5: Clear Database (Delete all records)
+		System.out.println("*** Step 5: clearDatabase() ***");
+		clearDatabase(conn, db);
 
 		DBRecord dep = new DBRecord();
 		dep.create(db.DEPARTMENT);
@@ -72,4 +79,34 @@ public class DBDatabaseDriverMSSqlTest 
 		// You may see ?? in the DB record
 		assertEquals("中文", depRead.getString(db.DEPARTMENT.BUSINESS_UNIT));
 	}
+	
+	/**
+     * <PRE>
+	 * Empties all Tables.
+     * </PRE>
+	 */
+	private static void clearDatabase(Connection conn, CompanyDB db)
+    {
+		DBCommand cmd = db.createCommand();
+		// Delete all Employees (no constraints)
+		db.executeSQL(cmd.getDelete(db.DEPARTMENT), conn);
+	}
+	
+	/**
+     * <PRE>
+	 * Checks whether the database exists or not by executing
+	 *     select count(*) from DEPARTMENTS
+	 * If the Departments table does not exist the querySingleInt() function return -1 for failure.
+	 * Please note that in this case an error will appear in the log which can be ignored.
+     * </PRE>
+	 */
+	private static boolean databaseExists(Connection conn, CompanyDB db)
+    {
+		// Check whether DB exists
+		DBCommand cmd = db.createCommand();
+		cmd.select(db.DEPARTMENT.count());
+		// Check using "select count(*) from DEPARTMENTS"
+		System.out.println("Checking whether table DEPARTMENTS exists (SQLException will be logged
if not - please ignore) ...");
+		return (db.querySingleInt(cmd.getSelect(), -1, conn) >= 0);
+	}
 }



Mime
View raw message