From: James Moger Date: Wed, 10 Aug 2011 13:18:43 +0000 (-0400) Subject: Moved dialects back to main package. Registered MySQL dialect. X-Git-Tag: v0.6.4~8 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f3faeb5d1ea631b0074441f97080e1f2a9145f4b;p=iciql.git Moved dialects back to main package. Registered MySQL dialect. --- diff --git a/README.markdown b/README.markdown index c212bea..416bba2 100644 --- a/README.markdown +++ b/README.markdown @@ -18,6 +18,7 @@ iciql **is not**... Supported Databases ------- - [H2](http://h2database.com) +- [MySQL](http://mysql.com) - Support for others is planned and should only require creating a simple "dialect" class. License diff --git a/docs/00_index.mkd b/docs/00_index.mkd index 06bec5b..70048e7 100644 --- a/docs/00_index.mkd +++ b/docs/00_index.mkd @@ -36,7 +36,7 @@ select * from products ### Supported Databases -[H2](http://h2database.com) +[H2](http://h2database.com), [MySQL](http://mysql.com) Support for others is planned and should only require creating a simple "dialect" class. diff --git a/docs/05_releases.mkd b/docs/05_releases.mkd index e889f2f..7fb0113 100644 --- a/docs/05_releases.mkd +++ b/docs/05_releases.mkd @@ -7,6 +7,9 @@ **%VERSION%** ([zip](http://code.google.com/p/iciql/downloads/detail?name=%ZIP%)|[jar](http://code.google.com/p/iciql/downloads/detail?name=%JAR%))   *released %BUILDDATE%* - api change release (API v4) +- moved dialects back to main package +- refined dialect loading for pooled connections +- added a MySQL dialect - @IQColumn(allowNull=true) -> @IQColumn(nullable=true) - All columns are assumed NULLABLE unless explicitly set *@IQColumn(nullable = false)* - allow using objects to assign default values
diff --git a/src/com/iciql/Db.java b/src/com/iciql/Db.java index d6f70d1..c7801c2 100644 --- a/src/com/iciql/Db.java +++ b/src/com/iciql/Db.java @@ -37,8 +37,6 @@ import javax.sql.DataSource; import com.iciql.DbUpgrader.DefaultDbUpgrader; import com.iciql.Iciql.IQTable; import com.iciql.Iciql.IQVersion; -import com.iciql.dialect.DefaultSQLDialect; -import com.iciql.dialect.H2Dialect; import com.iciql.util.JdbcUtils; import com.iciql.util.StringUtils; import com.iciql.util.Utils; @@ -73,7 +71,8 @@ public class Db { // can register by... // 1. Connection class name // 2. DatabaseMetaData.getDatabaseProductName() - DIALECTS.put("h2", H2Dialect.class); + DIALECTS.put("H2", SQLDialectH2.class); + DIALECTS.put("MySQL", SQLDialectMySQL.class); } private Db(Connection conn) { @@ -114,7 +113,7 @@ public class Db { dialectClass = DIALECTS.get(databaseName); } else { // did not find a match, use default - dialectClass = DefaultSQLDialect.class; + dialectClass = SQLDialectDefault.class; } return instance(dialectClass); } diff --git a/src/com/iciql/SQLDialectDefault.java b/src/com/iciql/SQLDialectDefault.java new file mode 100644 index 0000000..760a1f4 --- /dev/null +++ b/src/com/iciql/SQLDialectDefault.java @@ -0,0 +1,99 @@ +/* + * Copyright 2004-2011 H2 Group. + * Copyright 2011 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 java.sql.DatabaseMetaData; +import java.sql.SQLException; + +import com.iciql.TableDefinition.IndexDefinition; +import com.iciql.util.StringUtils; + +/** + * Default implementation of an SQL dialect. Does not support merge nor index + * creation. + */ +public class SQLDialectDefault implements SQLDialect { + float databaseVersion; + String databaseName; + String productVersion; + + @Override + public String toString() { + return getClass().getName() + ": " + databaseName + " " + productVersion; + } + + @Override + public void configureDialect(String databaseName, DatabaseMetaData data) { + this.databaseName = databaseName; + try { + databaseVersion = Float.parseFloat(data.getDatabaseMajorVersion() + "." + + data.getDatabaseMinorVersion()); + productVersion = data.getDatabaseProductVersion(); + } catch (SQLException e) { + throw new IciqlException(e); + } + } + + @Override + public boolean supportsMemoryTables() { + return false; + } + + @Override + public boolean supportsMerge() { + return false; + } + + @Override + public boolean supportsLimitOffset() { + return true; + } + + @Override + public String prepareTableName(String schemaName, String tableName) { + if (StringUtils.isNullOrEmpty(schemaName)) { + return tableName; + } + return schemaName + "." + tableName; + } + + @Override + public String prepareColumnName(String name) { + return name; + } + + @Override + public String prepareCreateIndex(String schemaName, String tableName, IndexDefinition index) { + throw new IciqlException("Dialect does not support index creation!"); + } + + @Override + public void prepareMerge(SQLStatement stat, String schemaName, String tableName, TableDefinition def, Object obj) { + throw new IciqlException("Dialect does not support merge statements!"); + } + + @Override + public void appendLimit(SQLStatement stat, long limit) { + stat.appendSQL(" LIMIT " + limit); + } + + @Override + public void appendOffset(SQLStatement stat, long offset) { + stat.appendSQL(" OFFSET " + offset); + } +} \ No newline at end of file diff --git a/src/com/iciql/SQLDialectH2.java b/src/com/iciql/SQLDialectH2.java new file mode 100644 index 0000000..c65c277 --- /dev/null +++ b/src/com/iciql/SQLDialectH2.java @@ -0,0 +1,97 @@ +/* + * Copyright 2011 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.StatementBuilder; + +/** + * H2 database dialect. + */ +public class SQLDialectH2 extends SQLDialectDefault { + + @Override + public boolean supportsMemoryTables() { + return true; + } + + @Override + public boolean supportsMerge() { + return true; + } + + @Override + public String prepareCreateIndex(String schema, String table, IndexDefinition index) { + StatementBuilder buff = new StatementBuilder(); + buff.append("CREATE "); + switch (index.type) { + case STANDARD: + break; + case UNIQUE: + buff.append("UNIQUE "); + break; + case HASH: + buff.append("HASH "); + break; + case UNIQUE_HASH: + buff.append("UNIQUE HASH "); + break; + } + buff.append("INDEX IF NOT EXISTS "); + buff.append(index.indexName); + buff.append(" ON "); + buff.append(table); + buff.append("("); + for (String col : index.columnNames) { + buff.appendExceptFirst(", "); + buff.append(col); + } + buff.append(")"); + return buff.toString(); + } + + @Override + public void prepareMerge(SQLStatement stat, String schemaName, String tableName, TableDefinition def, Object obj) { + StatementBuilder buff = new StatementBuilder("MERGE INTO "); + buff.append(prepareTableName(schemaName, tableName)).append(" ("); + buff.resetCount(); + for (FieldDefinition field : def.fields) { + buff.appendExceptFirst(", "); + buff.append(field.columnName); + } + buff.append(") KEY("); + buff.resetCount(); + for (FieldDefinition field : def.fields) { + if (field.isPrimaryKey) { + 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); + stat.addParameter(value); + } + buff.append(')'); + stat.setSQL(buff.toString()); + } +} \ No newline at end of file diff --git a/src/com/iciql/SQLDialectMySQL.java b/src/com/iciql/SQLDialectMySQL.java new file mode 100644 index 0000000..837d77b --- /dev/null +++ b/src/com/iciql/SQLDialectMySQL.java @@ -0,0 +1,99 @@ +/* + * Copyright 2011 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.StatementBuilder; + +/** + * MySQL database dialect. + */ +public class SQLDialectMySQL extends SQLDialectDefault { + + @Override + public boolean supportsMemoryTables() { + return false; + } + + @Override + public boolean supportsMerge() { + return true; + } + + @Override + public String prepareColumnName(String name) { + return "`" + name + "`"; + } + + @Override + public String prepareCreateIndex(String schema, String table, IndexDefinition index) { + StatementBuilder buff = new StatementBuilder(); + buff.append("CREATE "); + switch (index.type) { + case STANDARD: + break; + case UNIQUE: + buff.append("UNIQUE "); + break; + case UNIQUE_HASH: + buff.append("UNIQUE "); + break; + } + buff.append("INDEX "); + buff.append(index.indexName); + buff.append(" ON "); + buff.append(table); + buff.append("("); + for (String col : index.columnNames) { + buff.appendExceptFirst(", "); + buff.append(prepareColumnName(col)); + } + buff.append(") "); + + // USING + switch (index.type) { + case HASH: + buff.append("USING HASH"); + break; + case UNIQUE_HASH: + buff.append("USING HASH"); + break; + } + return buff.toString().trim(); + } + + @Override + public void prepareMerge(SQLStatement stat, String schemaName, String tableName, TableDefinition def, Object obj) { + StatementBuilder buff = new StatementBuilder("REPLACE INTO "); + buff.append(prepareTableName(schemaName, tableName)).append('('); + for (FieldDefinition field : def.fields) { + buff.appendExceptFirst(", "); + buff.append(prepareColumnName(field.columnName)); + } + buff.append(") VALUES("); + buff.resetCount(); + for (FieldDefinition field : def.fields) { + buff.appendExceptFirst(", "); + buff.append('?'); + Object value = def.getValue(obj, field); + stat.addParameter(value); + } + buff.append(')'); + stat.setSQL(buff.toString()); + } +} \ No newline at end of file diff --git a/src/com/iciql/TableDefinition.java b/src/com/iciql/TableDefinition.java index 5d934a5..c08a032 100644 --- a/src/com/iciql/TableDefinition.java +++ b/src/com/iciql/TableDefinition.java @@ -67,12 +67,12 @@ public class TableDefinition { * The meta data of a field. */ - public static class FieldDefinition { - public String columnName; + static class FieldDefinition { + String columnName; Field field; String dataType; int maxLength; - public boolean isPrimaryKey; + boolean isPrimaryKey; boolean isAutoIncrement; boolean trimString; boolean nullable; @@ -362,7 +362,7 @@ public class TableDefinition { * Optionally truncates strings to the maximum length and converts * java.lang.Enum types to Strings or Integers. */ - public Object getValue(Object obj, FieldDefinition field) { + Object getValue(Object obj, FieldDefinition field) { Object value = field.getValue(obj); if (value == null) { return value; diff --git a/src/com/iciql/dialect/DefaultSQLDialect.java b/src/com/iciql/dialect/DefaultSQLDialect.java deleted file mode 100644 index f3f222e..0000000 --- a/src/com/iciql/dialect/DefaultSQLDialect.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.iciql.dialect; - -import java.sql.DatabaseMetaData; -import java.sql.SQLException; - -import com.iciql.IciqlException; -import com.iciql.SQLDialect; -import com.iciql.SQLStatement; -import com.iciql.TableDefinition; -import com.iciql.TableDefinition.IndexDefinition; -import com.iciql.util.StringUtils; - -/** - * Default implementation of an SQL dialect. Does not support merge nor index - * creation. - */ -public class DefaultSQLDialect implements SQLDialect { - float databaseVersion; - String databaseName; - String productVersion; - - @Override - public String toString() { - return getClass().getName() + ": " + databaseName + " " + productVersion; - } - - @Override - public void configureDialect(String databaseName, DatabaseMetaData data) { - this.databaseName = databaseName; - try { - databaseVersion = Float.parseFloat(data.getDatabaseMajorVersion() + "." - + data.getDatabaseMinorVersion()); - productVersion = data.getDatabaseProductVersion(); - } catch (SQLException e) { - throw new IciqlException(e); - } - } - - @Override - public boolean supportsMemoryTables() { - return false; - } - - @Override - public boolean supportsMerge() { - return false; - } - - @Override - public boolean supportsLimitOffset() { - return true; - } - - @Override - public String prepareTableName(String schemaName, String tableName) { - if (StringUtils.isNullOrEmpty(schemaName)) { - return tableName; - } - return schemaName + "." + tableName; - } - - @Override - public String prepareColumnName(String name) { - return name; - } - - @Override - public String prepareCreateIndex(String schemaName, String tableName, IndexDefinition index) { - throw new IciqlException("Dialect does not support index creation!"); - } - - @Override - public void prepareMerge(SQLStatement stat, String schemaName, String tableName, TableDefinition def, Object obj) { - throw new IciqlException("Dialect does not support merge statements!"); - } - - @Override - public void appendLimit(SQLStatement stat, long limit) { - stat.appendSQL(" LIMIT " + limit); - } - - @Override - public void appendOffset(SQLStatement stat, long offset) { - stat.appendSQL(" OFFSET " + offset); - } -} \ No newline at end of file diff --git a/src/com/iciql/dialect/H2Dialect.java b/src/com/iciql/dialect/H2Dialect.java deleted file mode 100644 index 61babd7..0000000 --- a/src/com/iciql/dialect/H2Dialect.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.iciql.dialect; - -import com.iciql.SQLStatement; -import com.iciql.TableDefinition; -import com.iciql.TableDefinition.FieldDefinition; -import com.iciql.TableDefinition.IndexDefinition; -import com.iciql.util.StatementBuilder; - -/** - * H2 database dialect. - */ -public class H2Dialect extends DefaultSQLDialect { - - @Override - public boolean supportsMemoryTables() { - return true; - } - - @Override - public boolean supportsMerge() { - return true; - } - - @Override - public String prepareCreateIndex(String schema, String table, IndexDefinition index) { - StatementBuilder buff = new StatementBuilder(); - buff.append("CREATE "); - switch (index.type) { - case STANDARD: - break; - case UNIQUE: - buff.append("UNIQUE "); - break; - case HASH: - buff.append("HASH "); - break; - case UNIQUE_HASH: - buff.append("UNIQUE HASH "); - break; - } - buff.append("INDEX IF NOT EXISTS "); - buff.append(index.indexName); - buff.append(" ON "); - buff.append(table); - buff.append("("); - for (String col : index.columnNames) { - buff.appendExceptFirst(", "); - buff.append(col); - } - buff.append(")"); - return buff.toString(); - } - - @Override - public void prepareMerge(SQLStatement stat, String schemaName, String tableName, TableDefinition def, Object obj) { - StatementBuilder buff = new StatementBuilder("MERGE INTO "); - buff.append(prepareTableName(schemaName, tableName)).append(" ("); - buff.resetCount(); - for (FieldDefinition field : def.fields) { - buff.appendExceptFirst(", "); - buff.append(field.columnName); - } - buff.append(") KEY("); - buff.resetCount(); - for (FieldDefinition field : def.fields) { - if (field.isPrimaryKey) { - 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); - stat.addParameter(value); - } - buff.append(')'); - stat.setSQL(buff.toString()); - } -} \ No newline at end of file diff --git a/src/com/iciql/dialect/MySQLDialect.java b/src/com/iciql/dialect/MySQLDialect.java deleted file mode 100644 index e1ccaf3..0000000 --- a/src/com/iciql/dialect/MySQLDialect.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.iciql.dialect; - -import com.iciql.SQLStatement; -import com.iciql.TableDefinition; -import com.iciql.TableDefinition.FieldDefinition; -import com.iciql.TableDefinition.IndexDefinition; -import com.iciql.util.StatementBuilder; - -/** - * H2 database dialect. - */ -public class MySQLDialect extends DefaultSQLDialect { - - @Override - public boolean supportsMemoryTables() { - return false; - } - - @Override - public boolean supportsMerge() { - return true; - } - - @Override - public String prepareColumnName(String name) { - return "`" + name + "`"; - } - - @Override - public String prepareCreateIndex(String schema, String table, IndexDefinition index) { - StatementBuilder buff = new StatementBuilder(); - buff.append("CREATE "); - switch (index.type) { - case STANDARD: - break; - case UNIQUE: - buff.append("UNIQUE "); - break; - case UNIQUE_HASH: - buff.append("UNIQUE "); - break; - } - buff.append("INDEX "); - buff.append(index.indexName); - buff.append(" ON "); - buff.append(table); - buff.append("("); - for (String col : index.columnNames) { - buff.appendExceptFirst(", "); - buff.append(prepareColumnName(col)); - } - buff.append(") "); - - // USING - switch (index.type) { - case HASH: - buff.append("USING HASH"); - break; - case UNIQUE_HASH: - buff.append("USING HASH"); - break; - } - return buff.toString().trim(); - } - - @Override - public void prepareMerge(SQLStatement stat, String schemaName, String tableName, TableDefinition def, Object obj) { - StatementBuilder buff = new StatementBuilder("REPLACE INTO "); - buff.append(prepareTableName(schemaName, tableName)).append('('); - for (FieldDefinition field : def.fields) { - buff.appendExceptFirst(", "); - buff.append(prepareColumnName(field.columnName)); - } - buff.append(") VALUES("); - buff.resetCount(); - for (FieldDefinition field : def.fields) { - buff.appendExceptFirst(", "); - buff.append('?'); - Object value = def.getValue(obj, field); - stat.addParameter(value); - } - buff.append(')'); - stat.setSQL(buff.toString()); - } -} \ No newline at end of file diff --git a/src/com/iciql/dialect/package.html b/src/com/iciql/dialect/package.html deleted file mode 100644 index ad91873..0000000 --- a/src/com/iciql/dialect/package.html +++ /dev/null @@ -1,25 +0,0 @@ - - - - -Javadoc package documentation - - -The dialect classes for iciql. - - \ No newline at end of file