Menu

Commit [r2713]  Maximize  Restore  History

Added a feature so that the value of a pair may be a string designating a method call

hotzst 2009-10-12

changed /trunk/ch.sahits.codegen.java/src/ch/sahits/codegen/java/model/util/SQLProvider.java
changed /trunk/ch.sahits.codegen.test/src/ch/sahits/codegen/java/model/util/SQLProviderTest.java
/trunk/ch.sahits.codegen.java/src/ch/sahits/codegen/java/model/util/SQLProvider.java Diff Switch to side-by-side view
--- a/trunk/ch.sahits.codegen.java/src/ch/sahits/codegen/java/model/util/SQLProvider.java
+++ b/trunk/ch.sahits.codegen.java/src/ch/sahits/codegen/java/model/util/SQLProvider.java
@@ -20,11 +20,25 @@
 	
 	private DataBaseTable model;
 	/**
+	 * Flag indicates if the value is effectivly a value (false)
+	 * or represents a method call to retrieve the value
+	 */
+	private boolean useExternalValues=false;
+	/**
 	 * Initialize the model
 	 * @param _model of the table
 	 */
 	public SQLProvider(DataBaseTable _model) {
 		this.model = _model;
+	}
+	/**
+	 * Initialize the model
+	 * @param _model of the table
+	 * @param exteranlValue Flag indicating if the values are values or method calls
+	 */
+	public SQLProvider(DataBaseTable _model,boolean exteranlValue) {
+		this.model = _model;
+		useExternalValues= exteranlValue;
 	}
 
 	/**
@@ -185,12 +199,12 @@
 				if (!index.contains(fieldName)) {
 					// This is a field that should be updated
 					result += fieldName + "=" //$NON-NLS-1$
-							+ getFieldValueString(pair.getValue()) + ", "; //$NON-NLS-1$
+							+ getFieldValueStringWithExternalMethodCall(pair.getValue()) + ", "; //$NON-NLS-1$
 				}
 			} else {
 				// everything is updated
 				result += fieldName + "=" //$NON-NLS-1$
-				+ getFieldValueString(pair.getValue()) + ", "; //$NON-NLS-1$
+				+ getFieldValueStringWithExternalMethodCall(pair.getValue()) + ", "; //$NON-NLS-1$
 			}
 		}
 		return result.substring(0,result.lastIndexOf(","))+" "; //$NON-NLS-1$ //$NON-NLS-2$
@@ -206,6 +220,48 @@
 			return value.toString();
 		} else {
 			return "'"+value.toString()+"'"; //$NON-NLS-1$ //$NON-NLS-2$
+		}
+	}
+	/**
+	 * Get the value correct escaped. Primitive types must not be escaped.
+	 * Primitive types are returned as is, all others are put into "'".
+	 * @param value to be escaped
+	 * @return correct escaped value
+	 */
+	private String getFieldValueStringWithExternal(Object value) {
+		if (value.getClass().isPrimitive()){
+			return value.toString();
+		} else {
+			if (!useExternalValues){
+				return "'"+value.toString()+"'"; //$NON-NLS-1$ //$NON-NLS-2$
+			} else {
+				// the value is of type String and of the form object.getValue()
+				String result ="'\"+"+value.toString()+"+\"'";//$NON-NLS-1$ //$NON-NLS-2$
+				return result;
+			}
+		}
+	}
+	/**
+	 * Get the value correct escaped. Primitive types must not be escaped.
+	 * Primitive types are returned as is, all others are put into "'".
+	 * @param value to be escaped
+	 * @return correct escaped value
+	 */
+	private String getFieldValueStringWithExternalMethodCall(Object value) {
+		if (value.getClass().isPrimitive()){
+			return value.toString();
+		} else if ( !(value instanceof String)){
+			return "'"+value.toString()+"'"; //$NON-NLS-1$ //$NON-NLS-2$
+		}else {
+			if (!useExternalValues && ((String)value).indexOf("()")==-1){
+				return "'"+value.toString()+"'"; //$NON-NLS-1$ //$NON-NLS-2$
+			} else if (((String)value).indexOf("()")>-1){
+				// the value is of type String and of the form object.getValue()
+				String result ="'\"+"+value.toString()+"+\"'";//$NON-NLS-1$ //$NON-NLS-2$
+				return result;
+			} else {
+				return "'"+value.toString()+"'"; //$NON-NLS-1$ //$NON-NLS-2$
+			}
 		}
 	}
 	/**
@@ -222,7 +278,7 @@
 			Pair pair = (Pair) iterator.next();
 			String fieldName = pair.getKey();
 			if (index.contains(fieldName)){
-				result +=fieldName+"="+getFieldValueString(pair.getValue())+" AND "; //$NON-NLS-1$ //$NON-NLS-2$
+				result +=fieldName+"="+getFieldValueStringWithExternal(pair.getValue())+" AND "; //$NON-NLS-1$ //$NON-NLS-2$
 			}
 		}
 		return result.substring(0,result.lastIndexOf(" AND ")); //$NON-NLS-1$
@@ -251,7 +307,7 @@
 			String result=""; //$NON-NLS-1$
 			for (Iterator iterator = values.iterator(); iterator.hasNext();) {
 				Pair pair = (Pair) iterator.next();
-				result +=getFieldValueString(pair.getValue())+", "; //$NON-NLS-1$
+				result +=getFieldValueStringWithExternalMethodCall(pair.getValue())+", "; //$NON-NLS-1$
 			}
 			return "("+result.substring(0,result.lastIndexOf(","))+") "; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 	}
/trunk/ch.sahits.codegen.test/src/ch/sahits/codegen/java/model/util/SQLProviderTest.java Diff Switch to side-by-side view
Loading...
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.