Author: tfischer
Date: Sat Feb 12 07:50:05 2011
New Revision: 1070041
URL: http://svn.apache.org/viewvc?rev=1070041&view=rev
Log:
- Fixed "In" queries for Lists and added test
- added date key in test for multiPks
- changed local variable name in SqlExpression
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SqlExpression.java
db/torque/torque4/trunk/torque-test/src/main/schema/test-schema.xml
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java
Modified:
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SqlExpression.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SqlExpression.java?rev=1070041&r1=1070040&r2=1070041&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SqlExpression.java
(original)
+++
db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/SqlExpression.java
Sat Feb 12 07:50:05 2011
@@ -131,7 +131,7 @@ public final class SqlExpression
}
}
- String sqlValue;
+ String sqlSnippet;
Object parameter;
if (value instanceof Criteria)
{
@@ -157,16 +157,16 @@ public final class SqlExpression
|| comparison.equals(Criteria.NOT_IN))
{
// code below takes care of adding brackets
- sqlValue = subquery.toString();
+ sqlSnippet = subquery.toString();
}
else
{
- sqlValue = "(" + subquery.toString() + ")";
+ sqlSnippet = "(" + subquery.toString() + ")";
}
}
else
{
- sqlValue = "?";
+ sqlSnippet = "?";
parameter = value;
}
@@ -175,13 +175,13 @@ public final class SqlExpression
|| comparison.equals(Criteria.ILIKE)
|| comparison.equals(Criteria.NOT_ILIKE))
{
- buildPsLike(columnName, (String) parameter, sqlValue, comparison,
+ buildPsLike(columnName, (String) parameter, sqlSnippet, comparison,
ignoreCase, db, whereClause, params);
}
else if (comparison.equals(Criteria.IN)
|| comparison.equals(Criteria.NOT_IN))
{
- buildPsIn(columnName, parameter, comparison, sqlValue,
+ buildPsIn(columnName, parameter, comparison, sqlSnippet,
ignoreCase, db, whereClause, params);
}
else
@@ -200,11 +200,11 @@ public final class SqlExpression
if (ignoreCase)
{
columnName = db.ignoreCase(columnName);
- sqlValue = db.ignoreCase(sqlValue);
+ sqlSnippet = db.ignoreCase(sqlSnippet);
}
whereClause.append(columnName)
.append(comparison)
- .append(sqlValue);
+ .append(sqlSnippet);
}
}
}
@@ -363,19 +363,22 @@ public final class SqlExpression
}
/**
- * Takes a columnName and criteria (which must be an array) and
+ * Takes a columnName and criteria and
* builds a SQL 'IN' expression taking into account the ignoreCase
* flag.
*
- * @param columnName A column.
- * @param criteria The value to compare the column against.
+ * @param columnName The name of the column to add.
+ * @param parameter The value to compare the column against.
* @param comparison Either " IN " or " NOT IN ".
+ * @param sqlValue The value which should be appended to the sql query.
+ * May be replaced for certain classes of parameter.
* @param ignoreCase If true and columns represent Strings, the
- * appropriate function defined for the database will be used to
- * ignore differences in case.
+ * appropriate function defined for the database will be used to
+ * ignore differences in case.
* @param db Represents the database in use, for vendor specific functions.
- * @param whereClause A StringBuffer to which the sql expression
- * will be appended.
+ * @param whereClause A StringBuffer to which the SQL expression
+ * will be appended.
+ * @param params the list of prepared statement parameters.
*/
static void buildPsIn(String columnName,
Object parameter,
@@ -399,24 +402,16 @@ public final class SqlExpression
List<String> inClause = new ArrayList<String>();
if (parameter instanceof Iterable)
{
- if (!ignoreCase)
+ for (Object listValue : (Iterable<?>) parameter)
{
- inClause.add(sqlValue);
- params.add(parameter);
- }
- else
- {
- for (Object listValue : (Iterable<?>) parameter)
+ params.add(listValue);
+ if (ignoreCase && listValue instanceof String)
{
- params.add(listValue);
- if (listValue instanceof String)
- {
- inClause.add(db.ignoreCase("?"));
- }
- else
- {
- inClause.add("?");
- }
+ inClause.add(db.ignoreCase("?"));
+ }
+ else
+ {
+ inClause.add("?");
}
}
}
@@ -428,27 +423,19 @@ public final class SqlExpression
}
else if (parameter.getClass().isArray())
{
- if (!ignoreCase)
- {
- inClause.add(sqlValue);
- params.add(parameter);
- }
- else
+ for (Object listValue : (Object[]) parameter)
{
- for (Object listValue : (Object[]) parameter)
+ params.add(listValue);
+ if (ignoreCase && listValue instanceof String)
+ {
+ inClause.add(db.ignoreCase("?"));
+ }
+ else
{
- params.add(listValue);
- if (listValue instanceof String)
- {
- inClause.add(db.ignoreCase("?"));
- }
- else
- {
- inClause.add("?");
- }
+ inClause.add("?");
}
}
- }
+ }
else
{
throw new IllegalArgumentException(
Modified: db/torque/torque4/trunk/torque-test/src/main/schema/test-schema.xml
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/main/schema/test-schema.xml?rev=1070041&r1=1070040&r2=1070041&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/main/schema/test-schema.xml
(original)
+++ db/torque/torque4/trunk/torque-test/src/main/schema/test-schema.xml Sat Feb
12 07:50:05 2011
@@ -61,6 +61,7 @@
<column name="pk3" required="true" primaryKey="true" type="CHAR" size="5"
/>
<column name="pk4" required="true" primaryKey="true" type="INTEGER"
javaType="object" />
<column name="pk5" required="true" primaryKey="true" type="TINYINT"
javaType="object" />
+ <column name="pk6" required="true" primaryKey="true" type="TIMESTAMP" />
<column name="col_a" type="BOOLEANINT" />
<column name="col_b" type="BOOLEANINT" javaType="object" />
</table>
Modified:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java?rev=1070041&r1=1070040&r2=1070041&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java
(original)
+++
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/DataTest.java
Sat Feb 12 07:50:05 2011
@@ -23,6 +23,7 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
@@ -103,7 +104,6 @@ import org.apache.torque.test.RAbPeer;
import org.apache.torque.util.BasePeer;
import org.apache.torque.util.CountHelper;
import org.apache.torque.util.Criteria;
-import org.apache.torque.util.Query;
import org.apache.torque.util.Transaction;
/**
@@ -230,8 +230,9 @@ public class DataTest extends BaseRuntim
// do test
MultiPk mpk = new MultiPk();
- mpk.setPrimaryKey("Svarchar:N5:Schar:N3:N42:");
+ mpk.setPrimaryKey("Svarchar:N5:Schar:N3:N-42:D9999999999:");
mpk.save();
+ // TODO assert saved values
}
private static final String[] validTitles = {
@@ -1480,6 +1481,19 @@ public class DataTest extends BaseRuntim
assertTrue("Size of result is not 1, but " + result.size(),
result.size() == 1);
+ // ignore case in "in" query
+ {
+ criteria = new Criteria();
+ Set<String> names = new HashSet<String>();
+ names.add(author.getName().toLowerCase());
+ criteria.add(AuthorPeer.NAME, names, Criteria.IN);
+ criteria.setIgnoreCase(true);
+
+ result = AuthorPeer.doSelect(criteria);
+ assertEquals("Expected result of size 1 but got " + result.size(),
+ result.size(),
+ 1);
+ }
// Check that case is not ignored if ignoreCase is not set
// This is known not to work for mysql
@@ -1538,6 +1552,19 @@ public class DataTest extends BaseRuntim
result = AuthorPeer.doSelect(criteria);
assertTrue("Size of result is not 1, but " + result.size(),
result.size() == 1);
+
+ // ignore case in "in" query
+ {
+ criteria = new Criteria();
+ Set<String> names = new HashSet<String>();
+ names.add(author.getName());
+ criteria.add(AuthorPeer.NAME, names, Criteria.IN);
+
+ result = AuthorPeer.doSelect(criteria);
+ assertEquals("Expected result of size 1 but got " +
result.size(),
+ result.size(),
+ 1);
+ }
}
// check ignore case in order by
@@ -2103,11 +2130,11 @@ public class DataTest extends BaseRuntim
Criteria criteria = new Criteria();
criteria.add(AuthorPeer.AUTHOR_ID, subquery);
- List result = AuthorPeer.doSelect(criteria);
+ List<Author> result = AuthorPeer.doSelect(criteria);
assertEquals("Expected result of size 1 but got " + result.size(),
result.size(),
1);
- Author author = (Author) result.get(0);
+ Author author = result.get(0);
assertEquals("Expected author with Id "
+ author1.getAuthorId()
+ " but got "
@@ -2118,6 +2145,80 @@ public class DataTest extends BaseRuntim
}
/**
+ * Tests whether we can in statements.
+ * @throws Exception if the test fails
+ */
+ public void testIn() throws Exception
+ {
+ cleanBookstore();
+ Author author1 = new Author();
+ author1.setName("author1");
+ author1.save();
+ Author author2 = new Author();
+ author2.setName("author2");
+ author2.save();
+ Author author3 = new Author();
+ author3.setName("author3");
+ author3.save();
+
+ // "in" query with list
+ {
+ Criteria criteria = new Criteria();
+ List<String> nameList = new ArrayList<String>();
+ nameList.add("author1");
+ nameList.add("author2");
+ criteria.add(AuthorPeer.NAME, nameList, Criteria.IN);
+ criteria.addDescendingOrderByColumn(AuthorPeer.AUTHOR_ID);
+
+ List<Author> result = AuthorPeer.doSelect(criteria);
+ assertEquals("Expected result of size 2 but got " + result.size(),
+ 2,
+ result.size());
+ Author author = result.get(0);
+ assertEquals("Expected author with Id "
+ + author2.getAuthorId()
+ + " at first position but got "
+ + author.getAuthorId(),
+ author2.getAuthorId(),
+ author.getAuthorId());
+ author = result.get(1);
+ assertEquals("Expected author with Id "
+ + author1.getAuthorId()
+ + " at second position but got "
+ + author.getAuthorId(),
+ author1.getAuthorId(),
+ author.getAuthorId());
+ }
+
+ // "in" query with array
+ {
+ Criteria criteria = new Criteria();
+ String[] nameArray = new String[] {"author1", "author3"};
+ criteria.add(AuthorPeer.NAME, nameArray, Criteria.IN);
+ criteria.addDescendingOrderByColumn(AuthorPeer.AUTHOR_ID);
+
+ List<Author> result = AuthorPeer.doSelect(criteria);
+ assertEquals("Expected result of size 2 but got " + result.size(),
+ result.size(),
+ 2);
+ Author author = result.get(0);
+ assertEquals("Expected author with Id "
+ + author3.getAuthorId()
+ + " at first position but got "
+ + author.getAuthorId(),
+ author3.getAuthorId(),
+ author.getAuthorId());
+ author = result.get(1);
+ assertEquals("Expected author with Id "
+ + author1.getAuthorId()
+ + " at second position but got "
+ + author.getAuthorId(),
+ author1.getAuthorId(),
+ author.getAuthorId());
+ }
+ }
+
+ /**
* Tests whether a table implementing an interface actually
* returns an instance of this interface
* @throws Exception if the test fails
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]