diff options
author | James Moger <james.moger@gmail.com> | 2012-01-24 08:19:31 -0500 |
---|---|---|
committer | James Moger <james.moger@gmail.com> | 2012-01-24 08:19:31 -0500 |
commit | 342840e8c3ce94aaf14dd71b3246058393096058 (patch) | |
tree | f7ea2b54f7468c789dec3b80adfb613280d31a43 /src/com | |
parent | 2e077918649d398dce0948cb3feeb61b925ee8a4 (diff) | |
download | iciql-342840e8c3ce94aaf14dd71b3246058393096058.tar.gz iciql-342840e8c3ce94aaf14dd71b3246058393096058.zip |
Added toParameter() to update/set commandsv0.7.9
Also allow generation of parameterized update statements for reuse.
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/iciql/Constants.java | 6 | ||||
-rw-r--r-- | src/com/iciql/Db.java | 17 | ||||
-rw-r--r-- | src/com/iciql/Query.java | 62 | ||||
-rw-r--r-- | src/com/iciql/UpdateColumnSet.java | 15 |
4 files changed, 68 insertions, 32 deletions
diff --git a/src/com/iciql/Constants.java b/src/com/iciql/Constants.java index 5fd1dec..3653cde 100644 --- a/src/com/iciql/Constants.java +++ b/src/com/iciql/Constants.java @@ -25,14 +25,14 @@ public class Constants { // The build script extracts this exact line so be careful editing it
// and only use A-Z a-z 0-9 .-_ in the string.
- public static final String VERSION = "0.7.8";
+ public static final String VERSION = "0.7.9";
// The build script extracts this exact line so be careful editing it
// and only use A-Z a-z 0-9 .-_ in the string.
- public static final String VERSION_DATE = "2012-01-11";
+ public static final String VERSION_DATE = "2012-01-24";
// The build script extracts this exact line so be careful editing it
// and only use A-Z a-z 0-9 .-_ in the string.
- public static final String API_CURRENT = "12";
+ public static final String API_CURRENT = "13";
}
diff --git a/src/com/iciql/Db.java b/src/com/iciql/Db.java index bfc3c73..16cf386 100644 --- a/src/com/iciql/Db.java +++ b/src/com/iciql/Db.java @@ -572,11 +572,22 @@ public class Db { * the SQL statement
* @return the update count
*/
- public int executeUpdate(String sql) {
+ public int executeUpdate(String sql, Object... args) {
Statement stat = null;
try {
- stat = conn.createStatement();
- int updateCount = stat.executeUpdate(sql);
+ int updateCount;
+ if (args.length == 0) {
+ stat = conn.createStatement();
+ updateCount = stat.executeUpdate(sql);
+ } else {
+ PreparedStatement ps = conn.prepareStatement(sql);
+ int i = 1;
+ for (Object arg : args) {
+ ps.setObject(i++, arg);
+ }
+ updateCount = ps.executeUpdate();
+ stat = ps;
+ }
return updateCount;
} catch (SQLException e) {
throw new IciqlException(e);
diff --git a/src/com/iciql/Query.java b/src/com/iciql/Query.java index fb193a0..aa0ce4b 100644 --- a/src/com/iciql/Query.java +++ b/src/com/iciql/Query.java @@ -160,32 +160,46 @@ public class Query<T> { */
public <K> String toSQL(boolean distinct, K k) {
SQLStatement stat = new SQLStatement(getDb());
- stat.appendSQL("SELECT ");
- if (distinct) {
- stat.appendSQL("DISTINCT ");
- }
- if (k != null) {
- SelectTable<?> sel = getSelectTable(k);
- if (sel == null) {
- // unknown alias, use wildcard
- IciqlLogger.warn("Alias {0} is not defined in the statement!", k.getClass());
- stat.appendSQL("*");
- } else if (isJoin()) {
- // join query, use AS alias
- String as = sel.getAs();
- stat.appendSQL(as + ".*");
- } else {
- // schema.table.*
- String schema = sel.getAliasDefinition().schemaName;
- String table = sel.getAliasDefinition().tableName;
- String as = getDb().getDialect().prepareTableName(schema, table);
- stat.appendSQL(as + ".*");
+ if (updateColumnDeclarations.size() > 0) {
+ stat.appendSQL("UPDATE ");
+ from.appendSQL(stat);
+ stat.appendSQL(" SET ");
+ int i = 0;
+ for (UpdateColumn declaration : updateColumnDeclarations) {
+ if (i++ > 0) {
+ stat.appendSQL(", ");
+ }
+ declaration.appendSQL(stat);
}
+ appendWhere(stat);
} else {
- // alias unspecified, use wildcard
- stat.appendSQL("*");
- }
- appendFromWhere(stat);
+ stat.appendSQL("SELECT ");
+ if (distinct) {
+ stat.appendSQL("DISTINCT ");
+ }
+ if (k != null) {
+ SelectTable<?> sel = getSelectTable(k);
+ if (sel == null) {
+ // unknown alias, use wildcard
+ IciqlLogger.warn("Alias {0} is not defined in the statement!", k.getClass());
+ stat.appendSQL("*");
+ } else if (isJoin()) {
+ // join query, use AS alias
+ String as = sel.getAs();
+ stat.appendSQL(as + ".*");
+ } else {
+ // schema.table.*
+ String schema = sel.getAliasDefinition().schemaName;
+ String table = sel.getAliasDefinition().tableName;
+ String as = getDb().getDialect().prepareTableName(schema, table);
+ stat.appendSQL(as + ".*");
+ }
+ } else {
+ // alias unspecified, use wildcard
+ stat.appendSQL("*");
+ }
+ appendFromWhere(stat);
+ }
return stat.toSQL().trim();
}
diff --git a/src/com/iciql/UpdateColumnSet.java b/src/com/iciql/UpdateColumnSet.java index 8c30982..a961480 100644 --- a/src/com/iciql/UpdateColumnSet.java +++ b/src/com/iciql/UpdateColumnSet.java @@ -31,6 +31,7 @@ public class UpdateColumnSet<T, A> implements UpdateColumn { private Query<T> query; private A x; private A y; + private boolean isParameter; UpdateColumnSet(Query<T> query, A x) { this.query = query; @@ -43,10 +44,20 @@ public class UpdateColumnSet<T, A> implements UpdateColumn { return query; } + public Query<T> toParameter() { + query.addUpdateColumnDeclaration(this); + isParameter = true; + return query; + } + public void appendSQL(SQLStatement stat) { query.appendSQL(stat, null, x); - stat.appendSQL("="); - query.appendSQL(stat, x, y); + stat.appendSQL(" = "); + if (isParameter) { + query.appendSQL(stat, x, RuntimeParameter.PARAMETER); + } else { + query.appendSQL(stat, x, y); + } } } |