From 23eaf92995f7e2b087db19e03feffb733c0fd8f8 Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 30 Apr 2015 12:03:05 -0400 Subject: [PATCH] Improve SQLite dialect and unit-test conformance --- src/main/java/com/iciql/SQLDialectSQLite.java | 17 +++----- src/test/java/com/iciql/test/IciqlSuite.java | 3 ++ src/test/java/com/iciql/test/ModelsTest.java | 13 ++++-- .../com/iciql/test/NestedConditionsTest.java | 43 ++++++++++--------- .../test/models/ProductAnnotationOnly.java | 5 ++- 5 files changed, 45 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/iciql/SQLDialectSQLite.java b/src/main/java/com/iciql/SQLDialectSQLite.java index 28ae852..acb9b31 100644 --- a/src/main/java/com/iciql/SQLDialectSQLite.java +++ b/src/main/java/com/iciql/SQLDialectSQLite.java @@ -35,14 +35,13 @@ public class SQLDialectSQLite extends SQLDialectDefault { @Override public boolean supportsSavePoints() { - return false; - } - - @Override - public void configureDialect(Db db) { - super.configureDialect(db); - // enable foreign key constraint enforcement - db.executeUpdate("PRAGMA foreign_keys = ON;"); + // SAVEPOINT support was added after the 3.8.7 release + String [] chunks = productVersion.split("\\."); + if (Integer.parseInt(chunks[0]) > 3) { + return true; + } + float f = Float.parseFloat(chunks[1] + "." + chunks[2]); + return (f > 8.7); } @Override @@ -104,8 +103,6 @@ public class SQLDialectSQLite extends SQLDialectDefault { buff.append("INDEX IF NOT EXISTS "); buff.append(index.indexName); buff.append(" ON "); - // FIXME maybe we can use schemaName ? - // buff.append(prepareTableName(schemaName, tableName)); buff.append(tableName); buff.append("("); for (String col : index.columnNames) { diff --git a/src/test/java/com/iciql/test/IciqlSuite.java b/src/test/java/com/iciql/test/IciqlSuite.java index 6ec61df..19b3d60 100644 --- a/src/test/java/com/iciql/test/IciqlSuite.java +++ b/src/test/java/com/iciql/test/IciqlSuite.java @@ -290,6 +290,9 @@ public class IciqlSuite { } else if (isMySQL(db)) { // MySQL does not have schemas return null; + } else if (isSQLite(db)) { + // SQLite does not have schemas + return null; } return "PUBLIC"; diff --git a/src/test/java/com/iciql/test/ModelsTest.java b/src/test/java/com/iciql/test/ModelsTest.java index c5ba27a..683e494 100644 --- a/src/test/java/com/iciql/test/ModelsTest.java +++ b/src/test/java/com/iciql/test/ModelsTest.java @@ -17,9 +17,9 @@ package com.iciql.test; +import static com.iciql.test.IciqlSuite.assertEqualsIgnoreCase; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static com.iciql.test.IciqlSuite.assertEqualsIgnoreCase; import java.sql.SQLException; import java.text.MessageFormat; @@ -69,10 +69,11 @@ public class ModelsTest { @Test public void testValidateModels() { + // SQLite metadata mapping in the JDBC driver needs improvement String schemaName = IciqlSuite.getDefaultSchema(db); DbInspector inspector = new DbInspector(db); - validateModel(inspector, schemaName, new ProductAnnotationOnly(), 2); - validateModel(inspector, schemaName, new ProductMixedAnnotation(), 4); + validateModel(inspector, schemaName, new ProductAnnotationOnly(), IciqlSuite.isSQLite(db) ? 5 : 2); + validateModel(inspector, schemaName, new ProductMixedAnnotation(), IciqlSuite.isSQLite(db) ? 6 : 4); } private void validateModel(DbInspector inspector, String schemaName, Object o, int expected) { @@ -89,7 +90,9 @@ public class ModelsTest { } } - if (StringUtils.isNullOrEmpty(schemaName)) { + if (IciqlSuite.isSQLite(db)) { + assertEquals(sb.toString(), expected, remarks.size()); + } else if (StringUtils.isNullOrEmpty(schemaName)) { // no schema expected assertEquals(sb.toString(), expected - 1, remarks.size()); } else { @@ -137,6 +140,8 @@ public class ModelsTest { // MySQL uses timestamp default values like // 0000-00-00 00:00:00 and CURRENT_TIMESTAMP assertEquals(1673, models.get(0).length()); + } else if (dbName.equals("SQLite")) { + assertEquals(1566, models.get(0).length()); } else { // unknown database assertEquals(0, models.get(0).length()); diff --git a/src/test/java/com/iciql/test/NestedConditionsTest.java b/src/test/java/com/iciql/test/NestedConditionsTest.java index 7a20468..6676c9e 100644 --- a/src/test/java/com/iciql/test/NestedConditionsTest.java +++ b/src/test/java/com/iciql/test/NestedConditionsTest.java @@ -80,36 +80,37 @@ public class NestedConditionsTest { String Customer = db.getDialect().prepareTableName(null, "Customer"); String customerId = db.getDialect().prepareColumnName("customerId"); String region = db.getDialect().prepareColumnName("region"); + String trueValue = db.getDialect().prepareStringParameter(true); assertEquals( - String.format("SELECT * FROM %s WHERE (true)", Customer), + String.format("SELECT * FROM %s WHERE (%s)", Customer, trueValue), search(null, (String[]) null)); assertEquals( - String.format("SELECT * FROM %s WHERE (true)", Customer), + String.format("SELECT * FROM %s WHERE (%s)", Customer, trueValue), search(null, new String[0])); assertEquals( - String.format("SELECT * FROM %s WHERE (true) AND ( %s = '0001' )", - Customer, customerId), + String.format("SELECT * FROM %s WHERE (%s) AND ( %s = '0001' )", + Customer, trueValue, customerId), search(null, "0001")); assertEquals( - String.format("SELECT * FROM %s WHERE (true) AND ( %s = '0001' OR %s = '0002' )", - Customer, customerId, customerId), + String.format("SELECT * FROM %s WHERE (%s) AND ( %s = '0001' OR %s = '0002' )", + Customer, trueValue, customerId, customerId), search(null, "0001", "0002")); assertEquals( - String.format("SELECT * FROM %s WHERE (true) AND %s = 'JP'", - Customer, region), + String.format("SELECT * FROM %s WHERE (%s) AND %s = 'JP'", + Customer, trueValue, region), search(Region.JP, (String[]) null)); assertEquals( - String.format("SELECT * FROM %s WHERE (true) AND %s = 'JP'", - Customer, region), + String.format("SELECT * FROM %s WHERE (%s) AND %s = 'JP'", + Customer, trueValue, region), search(Region.JP, new String[0])); assertEquals( - String.format("SELECT * FROM %s WHERE (true) AND ( %s = '0001' ) AND %s = 'JP'", - Customer, customerId, region), + String.format("SELECT * FROM %s WHERE (%s) AND ( %s = '0001' ) AND %s = 'JP'", + Customer, trueValue, customerId, region), search(Region.JP, "0001")); assertEquals( - String.format("SELECT * FROM %s WHERE (true) AND ( %s = '0001' OR %s = '0002' ) AND %s = 'JP'", - Customer, customerId, customerId, region), + String.format("SELECT * FROM %s WHERE (%s) AND ( %s = '0001' OR %s = '0002' ) AND %s = 'JP'", + Customer, trueValue, customerId, customerId, region), search(Region.JP, "0001", "0002")); } @@ -165,12 +166,14 @@ public class NestedConditionsTest { String Customer = db.getDialect().prepareTableName(null, "Customer"); String customerId = db.getDialect().prepareColumnName("customerId"); String region = db.getDialect().prepareColumnName("region"); + String trueValue = db.getDialect().prepareStringParameter(true); + String falseValue = db.getDialect().prepareStringParameter(false); final Customer model = new Customer(); assertEquals( - String.format("SELECT * FROM %s WHERE (true) AND %s = '0001' AND ( %s = 'CA' OR %s = 'LA' )", - Customer, customerId, region, region), + String.format("SELECT * FROM %s WHERE (%s) AND %s = '0001' AND ( %s = 'CA' OR %s = 'LA' )", + Customer, trueValue, customerId, region, region), db.from(model).where(new And(db, model) {{ @@ -185,8 +188,8 @@ public class NestedConditionsTest { .toSQL()); assertEquals( - String.format("SELECT * FROM %s WHERE (false) OR %s = '0001' OR ( %s = '0002' AND %s = 'LA' )", - Customer, customerId, customerId, region), + String.format("SELECT * FROM %s WHERE (%s) OR %s = '0001' OR ( %s = '0002' AND %s = 'LA' )", + Customer, falseValue, customerId, customerId, region), db.from(model).where(new Or(db, model) {{ @@ -202,8 +205,8 @@ public class NestedConditionsTest { .toSQL()); assertEquals( - String.format("SELECT * FROM %s WHERE (false) OR ( %s = '0001' AND %s = 'WA' ) OR ( %s = '0002' AND %s = 'LA' )", - Customer, customerId, region, customerId, region), + String.format("SELECT * FROM %s WHERE (%s) OR ( %s = '0001' AND %s = 'WA' ) OR ( %s = '0002' AND %s = 'LA' )", + Customer, falseValue, customerId, region, customerId, region), db.from(model).where(new Or(db, model) {{ diff --git a/src/test/java/com/iciql/test/models/ProductAnnotationOnly.java b/src/test/java/com/iciql/test/models/ProductAnnotationOnly.java index 1f6b4e2..ea5856b 100644 --- a/src/test/java/com/iciql/test/models/ProductAnnotationOnly.java +++ b/src/test/java/com/iciql/test/models/ProductAnnotationOnly.java @@ -30,13 +30,13 @@ import com.iciql.Iciql.IndexType; * A table containing product data. */ -@IQTable(name = "AnnotatedProduct", primaryKey = "id") +@IQTable(name = "AnnotatedProduct") @IQIndexes({ @IQIndex({ "name", "cat" }), @IQIndex(name = "nameidx", type = IndexType.HASH, value = "name") }) public class ProductAnnotationOnly { public String unmappedField; - @IQColumn(name = "id", autoIncrement = true) + @IQColumn(name = "id", autoIncrement = true, primaryKey = true) public Long productId; @IQColumn(name = "cat", length = 15, trim = true) @@ -87,6 +87,7 @@ public class ProductAnnotationOnly { return Arrays.asList(list); } + @Override public String toString() { return productName + ": " + unitsInStock; } -- 2.39.5