diff options
author | James Moger <james.moger@gitblit.com> | 2014-11-03 11:05:52 -0500 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2014-11-03 12:51:16 -0500 |
commit | 6f486197c5aae3246e8175b828fe3e4c8de3906d (patch) | |
tree | 46de7bfadab22efb3293364970bc10c937bcb45d /src/main/java/com | |
parent | e4e9fdae539ec85552755b3a7799b75dbb7c4812 (diff) | |
download | iciql-6f486197c5aae3246e8175b828fe3e4c8de3906d.tar.gz iciql-6f486197c5aae3246e8175b828fe3e4c8de3906d.zip |
Add support for SQLite 3.8.7
Diffstat (limited to 'src/main/java/com')
-rw-r--r-- | src/main/java/com/iciql/Db.java | 1 | ||||
-rw-r--r-- | src/main/java/com/iciql/IciqlException.java | 20 | ||||
-rw-r--r-- | src/main/java/com/iciql/SQLDialectSQLite.java | 115 |
3 files changed, 135 insertions, 1 deletions
diff --git a/src/main/java/com/iciql/Db.java b/src/main/java/com/iciql/Db.java index 67e73b0..23001d8 100644 --- a/src/main/java/com/iciql/Db.java +++ b/src/main/java/com/iciql/Db.java @@ -85,6 +85,7 @@ public class Db implements AutoCloseable { DIALECTS.put("MySQL", SQLDialectMySQL.class);
DIALECTS.put("PostgreSQL", SQLDialectPostgreSQL.class);
DIALECTS.put("Microsoft SQL Server", SQLDialectMSSQL.class);
+ DIALECTS.put("SQLite", SQLDialectSQLite.class);
}
private Db(Connection conn) {
diff --git a/src/main/java/com/iciql/IciqlException.java b/src/main/java/com/iciql/IciqlException.java index 3f27b73..3db62cf 100644 --- a/src/main/java/com/iciql/IciqlException.java +++ b/src/main/java/com/iciql/IciqlException.java @@ -101,7 +101,7 @@ public class IciqlException extends RuntimeException { // MySQL duplicate primary key on insert
iciqlCode = CODE_DUPLICATE_KEY;
if (s.getErrorCode() == 1217) {
- iciqlCode = CODE_CONSTRAINT_VIOLATION;
+ iciqlCode = CODE_CONSTRAINT_VIOLATION;
}
} else if ("23505".equals(state)) {
// Derby duplicate primary key on insert
@@ -154,6 +154,24 @@ public class IciqlException extends RuntimeException { } else if ("X0Y25".equals(state)) {
// Derby constraint violation
iciqlCode = CODE_CONSTRAINT_VIOLATION;
+ } else if (s.getMessage().startsWith("[SQLITE")) {
+ // SQLite error codes
+ final String msg = s.getMessage();
+ switch (s.getErrorCode()) {
+ case 1:
+ iciqlCode = CODE_OBJECT_NOT_FOUND;
+ break;
+ case 19:
+ if (msg.contains("UNIQUE")) {
+ iciqlCode = CODE_DUPLICATE_KEY;
+ } else {
+ iciqlCode = CODE_CONSTRAINT_VIOLATION;
+ }
+ break;
+ default:
+ iciqlCode = s.getErrorCode();
+ break;
+ }
} else {
// uncharacterized SQL code, we can always rely on iciqlCode != 0 in IciqlException
iciqlCode = s.getErrorCode() == 0 ? CODE_UNCHARACTERIZED : s.getErrorCode();
diff --git a/src/main/java/com/iciql/SQLDialectSQLite.java b/src/main/java/com/iciql/SQLDialectSQLite.java new file mode 100644 index 0000000..c9bf8ec --- /dev/null +++ b/src/main/java/com/iciql/SQLDialectSQLite.java @@ -0,0 +1,115 @@ +/*
+ * Copyright 2014 James Moger.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.iciql;
+
+import com.iciql.TableDefinition.FieldDefinition;
+import com.iciql.TableDefinition.IndexDefinition;
+import com.iciql.util.IciqlLogger;
+import com.iciql.util.StatementBuilder;
+
+
+/**
+ * SQLite database dialect.
+ */
+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;");
+ }
+
+ @Override
+ protected <T> String prepareCreateTable(TableDefinition<T> def) {
+ return "CREATE TABLE IF NOT EXISTS";
+ }
+
+ @Override
+ protected <T> String prepareCreateView(TableDefinition<T> def) {
+ return "CREATE VIEW IF NOT EXISTS";
+ }
+
+ @Override
+ public <T> void prepareDropView(SQLStatement stat, TableDefinition<T> def) {
+ StatementBuilder buff = new StatementBuilder("DROP VIEW IF EXISTS "
+ + prepareTableName(def.schemaName, def.tableName));
+ stat.setSQL(buff.toString());
+ return;
+ }
+
+ @Override
+ public void prepareCreateIndex(SQLStatement stat, String schemaName, String tableName,
+ IndexDefinition index) {
+ StatementBuilder buff = new StatementBuilder();
+ buff.append("CREATE ");
+ switch (index.type) {
+ case UNIQUE:
+ buff.append("UNIQUE ");
+ break;
+ case UNIQUE_HASH:
+ buff.append("UNIQUE ");
+ break;
+ default:
+ IciqlLogger.warn("{0} does not support hash indexes", getClass().getSimpleName());
+ }
+ 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) {
+ buff.appendExceptFirst(", ");
+ buff.append(prepareColumnName(col));
+ }
+ buff.append(") ");
+
+ stat.setSQL(buff.toString().trim());
+ }
+
+ @Override
+ public <T> void prepareMerge(SQLStatement stat, String schemaName, String tableName,
+ TableDefinition<T> def, Object obj) {
+ StatementBuilder buff = new StatementBuilder("INSERT OR REPLACE INTO ");
+ buff.append(prepareTableName(schemaName, tableName)).append(" (");
+ buff.resetCount();
+ for (FieldDefinition field : def.fields) {
+ buff.appendExceptFirst(", ");
+ buff.append(field.columnName);
+ }
+ buff.append(") ");
+ buff.resetCount();
+ buff.append("VALUES (");
+ for (FieldDefinition field : def.fields) {
+ buff.appendExceptFirst(", ");
+ buff.append('?');
+ Object value = def.getValue(obj, field);
+ Object parameter = serialize(value, field.typeAdapter);
+ stat.addParameter(parameter);
+ }
+ buff.append(')');
+ stat.setSQL(buff.toString());
+ }
+
+}
\ No newline at end of file |