empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1201418 - in /incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db: DBDatabaseDriver.java sqlserver/DBDatabaseDriverMSSQL.java
Date Sun, 13 Nov 2011 10:04:19 GMT
Author: doebele
Date: Sun Nov 13 10:04:19 2011
New Revision: 1201418

URL: http://svn.apache.org/viewvc?rev=1201418&view=rev
Log:
EMPIREDB-122
unicode patch for sql server driver

Modified:
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/sqlserver/DBDatabaseDriverMSSQL.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=1201418&r1=1201417&r2=1201418&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
Sun Nov 13 10:04:19 2011
@@ -597,36 +597,6 @@ public abstract class DBDatabaseDriver i
             log.error("close statement:" + sqle.toString());
         }
     }
-
-    // helper for Date and DateTime Strings
-    protected String getDateTimeString(Object value, int sqlTemplate, int sqlPattern, int
sqlCurrentDate)
-    {
-        // is it a sysdate expression
-        if (DBDatabase.SYSDATE.equals(value))
-            return getSQLPhrase(sqlCurrentDate);
-        // Format the date (ymd)
-        String datetime = value.toString(); 
-        SimpleDateFormat sqlFormat = new SimpleDateFormat(getSQLPhrase(sqlPattern));
-        if ((value instanceof Date)==false)
-        {   // Convert String to Date
-            try
-            {
-                DateFormat source = DateFormat.getDateInstance(DateFormat.SHORT);
-                Date dt = source.parse(value.toString());
-               datetime = sqlFormat.format(dt);
-            } catch (ParseException e)
-            {
-                log.error("Date parsing error ", e);
-            }
-        }
-        else
-        {   // Format the date as string
-            datetime = sqlFormat.format((Date)value);
-        }
-        // Now Build String
-        String template = getSQLPhrase(sqlTemplate);
-        return StringUtils.replace(template, "{0}", datetime);
-    }
     
     /**
      * Creates a sql string for a given value. 
@@ -638,7 +608,7 @@ public abstract class DBDatabaseDriver i
      * @param type the sql data type of the supplied value
      * @return the sql string representing this value
      */
-    public final String getValueString(Object value, DataType type)
+    public String getValueString(Object value, DataType type)
     { 
         if (ObjectUtils.isEmpty(value))
         {
@@ -660,14 +630,7 @@ public abstract class DBDatabaseDriver i
             case CLOB:
             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());
-                valBuf.append("'");
-                return valBuf.toString();
+                return getTextString(type, value);
             }
             case BOOL:
             {   // Get Boolean value   
@@ -677,7 +640,7 @@ public abstract class DBDatabaseDriver i
                 } 
                 else
                 { // Boolean from String
-                    boolVal = stringToBoolean(value);
+                    boolVal = stringToBoolean(value.toString());
                 }
                 return getSQLPhrase((boolVal) ? SQL_BOOLEAN_TRUE : SQL_BOOLEAN_FALSE);
             }
@@ -687,6 +650,93 @@ public abstract class DBDatabaseDriver i
     }
 
     /**
+     * encodes a Date value for an SQL command string. 
+     * @param value
+     * @param sqlTemplate
+     * @param sqlPattern
+     * @param sqlCurrentDate
+     * @return
+     */
+    protected String getDateTimeString(Object value, int sqlTemplate, int sqlPattern, int
sqlCurrentDate)
+    {
+        // is it a sysdate expression
+        if (DBDatabase.SYSDATE.equals(value))
+            return getSQLPhrase(sqlCurrentDate);
+        // Format the date (ymd)
+        String datetime = value.toString(); 
+        SimpleDateFormat sqlFormat = new SimpleDateFormat(getSQLPhrase(sqlPattern));
+        if ((value instanceof Date)==false)
+        {   // Convert String to Date
+            try
+            {
+                DateFormat source = DateFormat.getDateInstance(DateFormat.SHORT);
+                Date dt = source.parse(value.toString());
+               datetime = sqlFormat.format(dt);
+            } catch (ParseException e)
+            {
+                log.error("Date parsing error ", e);
+            }
+        }
+        else
+        {   // Format the date as string
+            datetime = sqlFormat.format((Date)value);
+        }
+        // Now Build String
+        String template = getSQLPhrase(sqlTemplate);
+        return StringUtils.replace(template, "{0}", datetime);
+    }
+
+    /**
+     * encodes Text values for an SQL command string.
+     * @param type date type (can only be TEXT, CHAR, CLOB and UNIQUEID)
+     * @param value the text to be encoded
+     * @return the encoded sql value
+     */
+    protected String getTextString(DataType type, Object value)
+    {
+        StringBuilder valBuf = new StringBuilder();
+        valBuf.append("'");
+        if (DBDatabase.EMPTY_STRING.equals(value)==false)
+            appendTextValue(valBuf, value.toString());
+        valBuf.append("'");
+        return valBuf.toString();
+    }
+
+    /** 
+     * this helper function doubles up single quotes for SQL 
+     */
+    protected void appendTextValue(StringBuilder buf, String value)
+    {
+        if (value.indexOf('\'') >= 0)
+        { // a routine to double up single quotes for SQL
+            int len = value.length();
+            for (int i = 0; i < len; i++)
+            {
+                if (value.charAt(i) == '\'')
+                    buf.append("''");
+                else
+                    buf.append(value.charAt(i));
+            }
+        } 
+        else
+        {
+            buf.append(value);
+        }
+    }
+
+    /**
+     * this function converts a string containing a boolean expression to a boolean. 
+     * @param value the string containing a boolean expression
+     * @return true if the string contains either "true", "y" or "1" or false otherwise
+     */
+    protected boolean stringToBoolean(final String value) 
+    {
+        return "1".equals(value) ||
+               "true".equalsIgnoreCase(value) ||
+               "y".equalsIgnoreCase(value);
+    }
+    
+    /**
      * Called when a database is opened
      */
     protected void attachDatabase(DBDatabase db, Connection conn)
@@ -763,33 +813,4 @@ public abstract class DBDatabaseDriver i
         return new java.sql.Timestamp(date.getTime());
     }
 
-    /** 
-     * this helper function doubles up single quotes for SQL 
-     */
-    protected void appendTextValue(StringBuilder buf, String value)
-    {
-        if (value.indexOf('\'') >= 0)
-        { // a routine to double up single quotes for SQL
-            int len = value.length();
-            for (int i = 0; i < len; i++)
-            {
-                if (value.charAt(i) == '\'')
-                    buf.append("''");
-                else
-                    buf.append(value.charAt(i));
-            }
-        } 
-        else
-        {
-            buf.append(value);
-        }
-    }
-    
-	private boolean stringToBoolean(final Object value) {
-		String strVal = value.toString();
-		return "1".equals(strVal) ||
-		          "true".equalsIgnoreCase(strVal) ||
-		          "y".equalsIgnoreCase(strVal);
-	}
-
 }
\ No newline at end of file

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/sqlserver/DBDatabaseDriverMSSQL.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/sqlserver/DBDatabaseDriverMSSQL.java?rev=1201418&r1=1201417&r2=1201418&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/sqlserver/DBDatabaseDriverMSSQL.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/sqlserver/DBDatabaseDriverMSSQL.java
Sun Nov 13 10:04:19 2011
@@ -349,6 +349,21 @@ public class DBDatabaseDriverMSSQL exten
             return null;
         }
     }
+
+    /**
+     * @see DBDatabaseDriver#getTextString(DataType type, Object value)
+     */
+    @Override
+    protected String getTextString(DataType type, Object value)
+    {
+        StringBuilder valBuf = new StringBuilder();
+        // for SQLSERVER utf8 support, see EMPIREDB-122
+        valBuf.append("N'");
+        if (DBDatabase.EMPTY_STRING.equals(value)==false)
+            appendTextValue(valBuf, value.toString());
+        valBuf.append("'");
+        return valBuf.toString();
+    }
     
     /**
      * Overridden. Returns a timestamp that is used for record updates created by the database
server.



Mime
View raw message