Supported Databases\r
-------\r
- [H2](http://h2database.com)\r
+- [MySQL](http://mysql.com)\r
- Support for others is planned and should only require creating a simple "dialect" class.\r
\r
License\r
</table>\r
\r
### Supported Databases\r
-[H2](http://h2database.com)\r
+[H2](http://h2database.com), [MySQL](http://mysql.com)\r
\r
Support for others is planned and should only require creating a simple "dialect" class.\r
\r
**%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%*\r
\r
- api change release (API v4)\r
+- moved dialects back to main package\r
+- refined dialect loading for pooled connections\r
+- added a MySQL dialect\r
- @IQColumn(allowNull=true) -> @IQColumn(nullable=true)\r
- All columns are assumed NULLABLE unless explicitly set *@IQColumn(nullable = false)*\r
- allow using objects to assign default values<br/>\r
import com.iciql.DbUpgrader.DefaultDbUpgrader;\r
import com.iciql.Iciql.IQTable;\r
import com.iciql.Iciql.IQVersion;\r
-import com.iciql.dialect.DefaultSQLDialect;\r
-import com.iciql.dialect.H2Dialect;\r
import com.iciql.util.JdbcUtils;\r
import com.iciql.util.StringUtils;\r
import com.iciql.util.Utils;\r
// can register by...\r
// 1. Connection class name\r
// 2. DatabaseMetaData.getDatabaseProductName()\r
- DIALECTS.put("h2", H2Dialect.class);\r
+ DIALECTS.put("H2", SQLDialectH2.class);\r
+ DIALECTS.put("MySQL", SQLDialectMySQL.class);\r
}\r
\r
private Db(Connection conn) {\r
dialectClass = DIALECTS.get(databaseName);\r
} else {\r
// did not find a match, use default\r
- dialectClass = DefaultSQLDialect.class;\r
+ dialectClass = SQLDialectDefault.class;\r
}\r
return instance(dialectClass);\r
}\r
--- /dev/null
+/*\r
+ * Copyright 2004-2011 H2 Group.\r
+ * Copyright 2011 James Moger.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package com.iciql;\r
+\r
+import java.sql.DatabaseMetaData;\r
+import java.sql.SQLException;\r
+\r
+import com.iciql.TableDefinition.IndexDefinition;\r
+import com.iciql.util.StringUtils;\r
+\r
+/**\r
+ * Default implementation of an SQL dialect. Does not support merge nor index\r
+ * creation.\r
+ */\r
+public class SQLDialectDefault implements SQLDialect {\r
+ float databaseVersion;\r
+ String databaseName;\r
+ String productVersion;\r
+\r
+ @Override\r
+ public String toString() {\r
+ return getClass().getName() + ": " + databaseName + " " + productVersion;\r
+ }\r
+\r
+ @Override\r
+ public void configureDialect(String databaseName, DatabaseMetaData data) {\r
+ this.databaseName = databaseName;\r
+ try {\r
+ databaseVersion = Float.parseFloat(data.getDatabaseMajorVersion() + "."\r
+ + data.getDatabaseMinorVersion());\r
+ productVersion = data.getDatabaseProductVersion();\r
+ } catch (SQLException e) {\r
+ throw new IciqlException(e);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsMemoryTables() {\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsMerge() {\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsLimitOffset() {\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public String prepareTableName(String schemaName, String tableName) {\r
+ if (StringUtils.isNullOrEmpty(schemaName)) {\r
+ return tableName;\r
+ }\r
+ return schemaName + "." + tableName;\r
+ }\r
+\r
+ @Override\r
+ public String prepareColumnName(String name) {\r
+ return name;\r
+ }\r
+\r
+ @Override\r
+ public String prepareCreateIndex(String schemaName, String tableName, IndexDefinition index) {\r
+ throw new IciqlException("Dialect does not support index creation!");\r
+ }\r
+\r
+ @Override\r
+ public <T> void prepareMerge(SQLStatement stat, String schemaName, String tableName, TableDefinition<T> def, Object obj) {\r
+ throw new IciqlException("Dialect does not support merge statements!");\r
+ }\r
+\r
+ @Override\r
+ public void appendLimit(SQLStatement stat, long limit) {\r
+ stat.appendSQL(" LIMIT " + limit);\r
+ }\r
+\r
+ @Override\r
+ public void appendOffset(SQLStatement stat, long offset) {\r
+ stat.appendSQL(" OFFSET " + offset);\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * Copyright 2011 James Moger.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package com.iciql;\r
+\r
+import com.iciql.TableDefinition.FieldDefinition;\r
+import com.iciql.TableDefinition.IndexDefinition;\r
+import com.iciql.util.StatementBuilder;\r
+\r
+/**\r
+ * H2 database dialect.\r
+ */\r
+public class SQLDialectH2 extends SQLDialectDefault {\r
+\r
+ @Override\r
+ public boolean supportsMemoryTables() {\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsMerge() {\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public String prepareCreateIndex(String schema, String table, IndexDefinition index) {\r
+ StatementBuilder buff = new StatementBuilder();\r
+ buff.append("CREATE ");\r
+ switch (index.type) {\r
+ case STANDARD:\r
+ break;\r
+ case UNIQUE:\r
+ buff.append("UNIQUE ");\r
+ break;\r
+ case HASH:\r
+ buff.append("HASH ");\r
+ break;\r
+ case UNIQUE_HASH:\r
+ buff.append("UNIQUE HASH ");\r
+ break;\r
+ }\r
+ buff.append("INDEX IF NOT EXISTS ");\r
+ buff.append(index.indexName);\r
+ buff.append(" ON ");\r
+ buff.append(table);\r
+ buff.append("(");\r
+ for (String col : index.columnNames) {\r
+ buff.appendExceptFirst(", ");\r
+ buff.append(col);\r
+ }\r
+ buff.append(")");\r
+ return buff.toString();\r
+ }\r
+ \r
+ @Override\r
+ public <T> void prepareMerge(SQLStatement stat, String schemaName, String tableName, TableDefinition<T> def, Object obj) {\r
+ StatementBuilder buff = new StatementBuilder("MERGE INTO ");\r
+ buff.append(prepareTableName(schemaName, tableName)).append(" (");\r
+ buff.resetCount();\r
+ for (FieldDefinition field : def.fields) {\r
+ buff.appendExceptFirst(", ");\r
+ buff.append(field.columnName);\r
+ }\r
+ buff.append(") KEY(");\r
+ buff.resetCount();\r
+ for (FieldDefinition field : def.fields) {\r
+ if (field.isPrimaryKey) {\r
+ buff.appendExceptFirst(", ");\r
+ buff.append(field.columnName);\r
+ }\r
+ }\r
+ buff.append(") ");\r
+ buff.resetCount();\r
+ buff.append("VALUES (");\r
+ for (FieldDefinition field : def.fields) {\r
+ buff.appendExceptFirst(", ");\r
+ buff.append('?');\r
+ Object value = def.getValue(obj, field);\r
+ stat.addParameter(value);\r
+ }\r
+ buff.append(')');\r
+ stat.setSQL(buff.toString());\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * Copyright 2011 James Moger.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package com.iciql;\r
+\r
+import com.iciql.TableDefinition.FieldDefinition;\r
+import com.iciql.TableDefinition.IndexDefinition;\r
+import com.iciql.util.StatementBuilder;\r
+\r
+/**\r
+ * MySQL database dialect.\r
+ */\r
+public class SQLDialectMySQL extends SQLDialectDefault {\r
+\r
+ @Override\r
+ public boolean supportsMemoryTables() {\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsMerge() {\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public String prepareColumnName(String name) {\r
+ return "`" + name + "`";\r
+ }\r
+ \r
+ @Override\r
+ public String prepareCreateIndex(String schema, String table, IndexDefinition index) {\r
+ StatementBuilder buff = new StatementBuilder();\r
+ buff.append("CREATE ");\r
+ switch (index.type) {\r
+ case STANDARD:\r
+ break;\r
+ case UNIQUE:\r
+ buff.append("UNIQUE ");\r
+ break;\r
+ case UNIQUE_HASH:\r
+ buff.append("UNIQUE ");\r
+ break;\r
+ }\r
+ buff.append("INDEX ");\r
+ buff.append(index.indexName);\r
+ buff.append(" ON ");\r
+ buff.append(table);\r
+ buff.append("(");\r
+ for (String col : index.columnNames) {\r
+ buff.appendExceptFirst(", ");\r
+ buff.append(prepareColumnName(col));\r
+ }\r
+ buff.append(") ");\r
+ \r
+ // USING\r
+ switch (index.type) {\r
+ case HASH:\r
+ buff.append("USING HASH");\r
+ break;\r
+ case UNIQUE_HASH:\r
+ buff.append("USING HASH");\r
+ break;\r
+ }\r
+ return buff.toString().trim();\r
+ }\r
+ \r
+ @Override\r
+ public <T> void prepareMerge(SQLStatement stat, String schemaName, String tableName, TableDefinition<T> def, Object obj) { \r
+ StatementBuilder buff = new StatementBuilder("REPLACE INTO ");\r
+ buff.append(prepareTableName(schemaName, tableName)).append('(');\r
+ for (FieldDefinition field : def.fields) {\r
+ buff.appendExceptFirst(", ");\r
+ buff.append(prepareColumnName(field.columnName));\r
+ }\r
+ buff.append(") VALUES(");\r
+ buff.resetCount();\r
+ for (FieldDefinition field : def.fields) {\r
+ buff.appendExceptFirst(", ");\r
+ buff.append('?');\r
+ Object value = def.getValue(obj, field);\r
+ stat.addParameter(value);\r
+ }\r
+ buff.append(')');\r
+ stat.setSQL(buff.toString());\r
+ }\r
+}
\ No newline at end of file
* The meta data of a field.\r
*/\r
\r
- public static class FieldDefinition {\r
- public String columnName;\r
+ static class FieldDefinition {\r
+ String columnName;\r
Field field;\r
String dataType;\r
int maxLength;\r
- public boolean isPrimaryKey;\r
+ boolean isPrimaryKey;\r
boolean isAutoIncrement;\r
boolean trimString;\r
boolean nullable;\r
* Optionally truncates strings to the maximum length and converts\r
* java.lang.Enum types to Strings or Integers.\r
*/\r
- public Object getValue(Object obj, FieldDefinition field) {\r
+ Object getValue(Object obj, FieldDefinition field) {\r
Object value = field.getValue(obj);\r
if (value == null) {\r
return value;\r
+++ /dev/null
-package com.iciql.dialect;\r
-\r
-import java.sql.DatabaseMetaData;\r
-import java.sql.SQLException;\r
-\r
-import com.iciql.IciqlException;\r
-import com.iciql.SQLDialect;\r
-import com.iciql.SQLStatement;\r
-import com.iciql.TableDefinition;\r
-import com.iciql.TableDefinition.IndexDefinition;\r
-import com.iciql.util.StringUtils;\r
-\r
-/**\r
- * Default implementation of an SQL dialect. Does not support merge nor index\r
- * creation.\r
- */\r
-public class DefaultSQLDialect implements SQLDialect {\r
- float databaseVersion;\r
- String databaseName;\r
- String productVersion;\r
-\r
- @Override\r
- public String toString() {\r
- return getClass().getName() + ": " + databaseName + " " + productVersion;\r
- }\r
-\r
- @Override\r
- public void configureDialect(String databaseName, DatabaseMetaData data) {\r
- this.databaseName = databaseName;\r
- try {\r
- databaseVersion = Float.parseFloat(data.getDatabaseMajorVersion() + "."\r
- + data.getDatabaseMinorVersion());\r
- productVersion = data.getDatabaseProductVersion();\r
- } catch (SQLException e) {\r
- throw new IciqlException(e);\r
- }\r
- }\r
-\r
- @Override\r
- public boolean supportsMemoryTables() {\r
- return false;\r
- }\r
-\r
- @Override\r
- public boolean supportsMerge() {\r
- return false;\r
- }\r
-\r
- @Override\r
- public boolean supportsLimitOffset() {\r
- return true;\r
- }\r
-\r
- @Override\r
- public String prepareTableName(String schemaName, String tableName) {\r
- if (StringUtils.isNullOrEmpty(schemaName)) {\r
- return tableName;\r
- }\r
- return schemaName + "." + tableName;\r
- }\r
-\r
- @Override\r
- public String prepareColumnName(String name) {\r
- return name;\r
- }\r
-\r
- @Override\r
- public String prepareCreateIndex(String schemaName, String tableName, IndexDefinition index) {\r
- throw new IciqlException("Dialect does not support index creation!");\r
- }\r
-\r
- @Override\r
- public <T> void prepareMerge(SQLStatement stat, String schemaName, String tableName, TableDefinition<T> def, Object obj) {\r
- throw new IciqlException("Dialect does not support merge statements!");\r
- }\r
-\r
- @Override\r
- public void appendLimit(SQLStatement stat, long limit) {\r
- stat.appendSQL(" LIMIT " + limit);\r
- }\r
-\r
- @Override\r
- public void appendOffset(SQLStatement stat, long offset) {\r
- stat.appendSQL(" OFFSET " + offset);\r
- }\r
-}
\ No newline at end of file
+++ /dev/null
-package com.iciql.dialect;\r
-\r
-import com.iciql.SQLStatement;\r
-import com.iciql.TableDefinition;\r
-import com.iciql.TableDefinition.FieldDefinition;\r
-import com.iciql.TableDefinition.IndexDefinition;\r
-import com.iciql.util.StatementBuilder;\r
-\r
-/**\r
- * H2 database dialect.\r
- */\r
-public class H2Dialect extends DefaultSQLDialect {\r
-\r
- @Override\r
- public boolean supportsMemoryTables() {\r
- return true;\r
- }\r
-\r
- @Override\r
- public boolean supportsMerge() {\r
- return true;\r
- }\r
-\r
- @Override\r
- public String prepareCreateIndex(String schema, String table, IndexDefinition index) {\r
- StatementBuilder buff = new StatementBuilder();\r
- buff.append("CREATE ");\r
- switch (index.type) {\r
- case STANDARD:\r
- break;\r
- case UNIQUE:\r
- buff.append("UNIQUE ");\r
- break;\r
- case HASH:\r
- buff.append("HASH ");\r
- break;\r
- case UNIQUE_HASH:\r
- buff.append("UNIQUE HASH ");\r
- break;\r
- }\r
- buff.append("INDEX IF NOT EXISTS ");\r
- buff.append(index.indexName);\r
- buff.append(" ON ");\r
- buff.append(table);\r
- buff.append("(");\r
- for (String col : index.columnNames) {\r
- buff.appendExceptFirst(", ");\r
- buff.append(col);\r
- }\r
- buff.append(")");\r
- return buff.toString();\r
- }\r
- \r
- @Override\r
- public <T> void prepareMerge(SQLStatement stat, String schemaName, String tableName, TableDefinition<T> def, Object obj) {\r
- StatementBuilder buff = new StatementBuilder("MERGE INTO ");\r
- buff.append(prepareTableName(schemaName, tableName)).append(" (");\r
- buff.resetCount();\r
- for (FieldDefinition field : def.fields) {\r
- buff.appendExceptFirst(", ");\r
- buff.append(field.columnName);\r
- }\r
- buff.append(") KEY(");\r
- buff.resetCount();\r
- for (FieldDefinition field : def.fields) {\r
- if (field.isPrimaryKey) {\r
- buff.appendExceptFirst(", ");\r
- buff.append(field.columnName);\r
- }\r
- }\r
- buff.append(") ");\r
- buff.resetCount();\r
- buff.append("VALUES (");\r
- for (FieldDefinition field : def.fields) {\r
- buff.appendExceptFirst(", ");\r
- buff.append('?');\r
- Object value = def.getValue(obj, field);\r
- stat.addParameter(value);\r
- }\r
- buff.append(')');\r
- stat.setSQL(buff.toString());\r
- }\r
-}
\ No newline at end of file
+++ /dev/null
-package com.iciql.dialect;\r
-\r
-import com.iciql.SQLStatement;\r
-import com.iciql.TableDefinition;\r
-import com.iciql.TableDefinition.FieldDefinition;\r
-import com.iciql.TableDefinition.IndexDefinition;\r
-import com.iciql.util.StatementBuilder;\r
-\r
-/**\r
- * H2 database dialect.\r
- */\r
-public class MySQLDialect extends DefaultSQLDialect {\r
-\r
- @Override\r
- public boolean supportsMemoryTables() {\r
- return false;\r
- }\r
-\r
- @Override\r
- public boolean supportsMerge() {\r
- return true;\r
- }\r
-\r
- @Override\r
- public String prepareColumnName(String name) {\r
- return "`" + name + "`";\r
- }\r
- \r
- @Override\r
- public String prepareCreateIndex(String schema, String table, IndexDefinition index) {\r
- StatementBuilder buff = new StatementBuilder();\r
- buff.append("CREATE ");\r
- switch (index.type) {\r
- case STANDARD:\r
- break;\r
- case UNIQUE:\r
- buff.append("UNIQUE ");\r
- break;\r
- case UNIQUE_HASH:\r
- buff.append("UNIQUE ");\r
- break;\r
- }\r
- buff.append("INDEX ");\r
- buff.append(index.indexName);\r
- buff.append(" ON ");\r
- buff.append(table);\r
- buff.append("(");\r
- for (String col : index.columnNames) {\r
- buff.appendExceptFirst(", ");\r
- buff.append(prepareColumnName(col));\r
- }\r
- buff.append(") ");\r
- \r
- // USING\r
- switch (index.type) {\r
- case HASH:\r
- buff.append("USING HASH");\r
- break;\r
- case UNIQUE_HASH:\r
- buff.append("USING HASH");\r
- break;\r
- }\r
- return buff.toString().trim();\r
- }\r
- \r
- @Override\r
- public <T> void prepareMerge(SQLStatement stat, String schemaName, String tableName, TableDefinition<T> def, Object obj) { \r
- StatementBuilder buff = new StatementBuilder("REPLACE INTO ");\r
- buff.append(prepareTableName(schemaName, tableName)).append('(');\r
- for (FieldDefinition field : def.fields) {\r
- buff.appendExceptFirst(", ");\r
- buff.append(prepareColumnName(field.columnName));\r
- }\r
- buff.append(") VALUES(");\r
- buff.resetCount();\r
- for (FieldDefinition field : def.fields) {\r
- buff.appendExceptFirst(", ");\r
- buff.append('?');\r
- Object value = def.getValue(obj, field);\r
- stat.addParameter(value);\r
- }\r
- buff.append(')');\r
- stat.setSQL(buff.toString());\r
- }\r
-}
\ No newline at end of file
+++ /dev/null
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<!--
- 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.
--->
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
-<title>Javadoc package documentation</title>
-</head>
-<body>
-The dialect classes for iciql.
-</body>
-</html>
\ No newline at end of file