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
--- a/trunk/ch.sahits.codegen.test/src/ch/sahits/codegen/java/model/util/SQLProviderTest.java
+++ b/trunk/ch.sahits.codegen.test/src/ch/sahits/codegen/java/model/util/SQLProviderTest.java
@@ -19,11 +19,12 @@
 @RunAs(ERunAsType.JUNIT)
 public class SQLProviderTest {
 	private DataBaseTable dbt;
-	private SQLProvider provider;
 	private DataBaseTableIndex pk;
 	private DataBaseTableIndex unique;
 	private DataBaseTableIndex sk2;
 	private DataBaseTableIndex sk3;
+	private SQLProvider provider;
+	private SQLProvider providerExternal;
 
 	@Before
 	public void setUp() throws Exception {
@@ -61,6 +62,7 @@
 		dbt.add(sk2);
 		dbt.add(sk3);
 		provider=new SQLProvider(dbt);
+		providerExternal=new SQLProvider(dbt, true);
 	}
 	@Test
 	public void testGetWhereClause() {
@@ -80,6 +82,13 @@
 		expected = "WHERE NAME='name' AND SURNAME='surname'";
 		actual = provider.getWhereClause(sk3, list);
 		Assert.assertEquals(expected, actual);
+		list.clear();
+		list = new Vector<Pair>();
+		list.add(new Pair("ID","bo.getId()"));
+		expected = "WHERE ID='\"+bo.getId()+\"'";
+		actual = providerExternal.getWhereClause(pk, list);
+		Assert.assertEquals(expected, actual);
+
 	}
 
 
@@ -100,7 +109,26 @@
 		expected = "UPDATE USER SET ID='1', EMAIL='email', BIRTH_DATE='birthdate' WHERE NAME='name' AND SURNAME='surname'";
 		actual = provider.getUpdateSQL(list, "SK_USER3");
 		Assert.assertEquals(expected, actual);
-		
+		list.clear();
+		list = new Vector<Pair>();
+		list.add(new Pair("ID","bo.getId()"));
+		list.add(new Pair("NAME","name"));
+		list.add(new Pair("SURNAME","surname"));
+		list.add(new Pair("EMAIL","email"));
+		list.add(new Pair("BIRTH_DATE","birthdate"));
+		expected = "UPDATE USER SET NAME='name', SURNAME='surname', EMAIL='email', BIRTH_DATE='birthdate' WHERE ID='\"+bo.getId()+\"'";
+		actual = providerExternal.getUpdateSQL(list, "PK_USER");
+		Assert.assertEquals(expected, actual);
+		list.clear();
+		list = new Vector<Pair>();
+		list.add(new Pair("ID","bo.getId()"));
+		list.add(new Pair("NAME","name"));
+		list.add(new Pair("SURNAME","surname"));
+		list.add(new Pair("EMAIL","bo.getEmail()"));
+		list.add(new Pair("BIRTH_DATE","birthdate"));
+		expected = "UPDATE USER SET NAME='name', SURNAME='surname', EMAIL='\"+bo.getEmail()+\"', BIRTH_DATE='birthdate' WHERE ID='\"+bo.getId()+\"'";
+		actual = providerExternal.getUpdateSQL(list, "PK_USER");
+		Assert.assertEquals(expected, actual);
 	}
 	@Test
 	public void testGetUpdateSQLListOfPair() {
@@ -113,6 +141,16 @@
 		String expected = "UPDATE USER SET NAME='name', SURNAME='surname', EMAIL='email', BIRTH_DATE='birthdate' WHERE ID='1'";
 		String actual = provider.getUpdateSQL(list);
 		Assert.assertEquals(expected, actual);
+		list.clear();
+		list = new Vector<Pair>();
+		list.add(new Pair("ID","bo.getId()"));
+		list.add(new Pair("NAME","name"));
+		list.add(new Pair("SURNAME","surname"));
+		list.add(new Pair("EMAIL","email"));
+		list.add(new Pair("BIRTH_DATE","birthdate"));
+		expected = "UPDATE USER SET NAME='name', SURNAME='surname', EMAIL='email', BIRTH_DATE='birthdate' WHERE ID='\"+bo.getId()+\"'";
+		actual = providerExternal.getUpdateSQL(list);
+		Assert.assertEquals(expected, actual);
 	}
 
 	@Test
@@ -138,6 +176,16 @@
 		list.add(new Pair("BIRTH_DATE","birthdate"));
 		String expected = "INSERT INTO USER (ID, NAME, SURNAME, EMAIL, BIRTH_DATE) VALUES ('1', 'name', 'surname', 'email', 'birthdate')";
 		String actual = provider.getInsertSQL(list);
+		Assert.assertEquals(expected, actual);
+		list.clear();
+		list = new Vector<Pair>();
+		list.add(new Pair("ID","bo.getId()"));
+		list.add(new Pair("NAME","name"));
+		list.add(new Pair("SURNAME","surname"));
+		list.add(new Pair("EMAIL","email"));
+		list.add(new Pair("BIRTH_DATE","birthdate"));
+		expected = "INSERT INTO USER (ID, NAME, SURNAME, EMAIL, BIRTH_DATE) VALUES ('\"+bo.getId()+\"', 'name', 'surname', 'email', 'birthdate')";
+		actual = providerExternal.getInsertSQL(list);
 		Assert.assertEquals(expected, actual);
 	}
 
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.