openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r686905 - /openjpa/branches/1.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Date Tue, 19 Aug 2008 00:15:35 GMT
Author: ppoddar
Date: Mon Aug 18 17:15:35 2008
New Revision: 686905

URL: http://svn.apache.org/viewvc?rev=686905&view=rev
Log:
Merge 686325, 686349, 686419 -- missed a whole block during manual merge.

Modified:
    openjpa/branches/1.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java

Modified: openjpa/branches/1.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java?rev=686905&r1=686904&r2=686905&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
(original)
+++ openjpa/branches/1.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Mon Aug 18 17:15:35 2008
@@ -20,6 +20,8 @@
 
 import java.io.Serializable;
 import java.lang.reflect.Method;
+import java.sql.Time;
+import java.sql.Timestamp;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
@@ -356,6 +358,52 @@
 					
 				}
 			}
+
+		} else if (_named != null) {
+			LinkedMap expected = _query.getParameterTypes();
+			// key : name of the parameter used while binding
+			// value : user supplied parameter value. null may mean either
+			// user has supplied a value or not specified at all
+			Map<String, Object> actual = _named;
+			for (Object o : expected.keySet()) {
+				String expectedName = (String) o;
+				Class expectedParamType = (Class) expected.get(expectedName);
+				Object actualValue = actual.get(expectedName);
+				boolean valueUnspecified = !actual.containsKey(expectedName);
+				if (valueUnspecified) {
+					newValidationException("param-missing", expectedName, query,
+							Arrays.toString(actual.keySet().toArray()));
+				}
+				if (expectedParamType.isPrimitive() && actualValue == null)
+					newValidationException("param-type-null", 
+							expectedName, query, expectedParamType.getName());
+				if (actualValue != null 
+				 && !Filters.wrap(expectedParamType).isInstance(actualValue)) {
+					newValidationException("param-type-mismatch",
+							expectedName, query, actualValue,
+							actualValue.getClass().getName(),
+							expectedParamType.getName());
+				}
+			}
+			for (String actualName : actual.keySet()) {
+				Object actualValue = actual.get(actualName);
+				Class expectedParamType = (Class) expected.get(actualName);
+				boolean paramExpected = expected.containsKey(actualName);
+				if (!paramExpected) {
+					newValidationException("param-extra", actualName, query,
+							expected.asList());
+				}
+				if (expectedParamType.isPrimitive() && actualValue == null)
+					newValidationException("param-type-null", 
+							actualName, query, expectedParamType.getName());
+				if (actualValue != null 
+				 && !Filters.wrap(expectedParamType).isInstance(actualValue)) {
+					newValidationException("param-type-mismatch",
+							actualName, query, actualValue,
+							actualValue.getClass().getName(),
+							expectedParamType.getName());
+				}
+			}
 		}
 	}
 	
@@ -521,15 +569,35 @@
 
     public OpenJPAQuery setParameter(int position, Calendar value,
         TemporalType t) {
-        return setParameter(position, value);
+        return setParameter(position, convertTemporalType(value, t));
     }
 
     public OpenJPAQuery setParameter(int position, Date value,
         TemporalType type) {
-        return setParameter(position, value);
+        return setParameter(position, convertTemporalType(value, type));
     }
 
-	public OpenJPAQuery setParameter(int position, Object value) {
+    /**
+     * Converts the given Date to a value corresponding to given temporal type.
+     */
+    Object convertTemporalType(Date value, TemporalType type) {
+            switch (type) {
+            case DATE:
+                    return value;
+            case TIME:
+                    return new Time(value.getTime());
+            case TIMESTAMP:
+                    return new Timestamp(value.getTime());
+            default:
+                    return null;
+            }
+    }
+    
+    Object convertTemporalType(Calendar value, TemporalType type) {
+            return convertTemporalType(value.getTime(), type);
+    }
+    
+    public OpenJPAQuery setParameter(int position, Object value) {
 		_query.assertOpen();
 		_em.assertNotCloseInvoked();
 		_query.lock();
@@ -565,12 +633,12 @@
 
     public OpenJPAQuery setParameter(String name, Calendar value,
         TemporalType t) {
-        return setParameter(name, value);
+        return setParameter(name, convertTemporalType(value, t));
     }
 
     public OpenJPAQuery setParameter(String name, Date value,
         TemporalType type) {
-        return setParameter(name, value);
+        return setParameter(name, convertTemporalType(value, type));
     }
 
     public OpenJPAQuery setParameter(String name, Object value) {



Mime
View raw message