From 1fa834f55926b623575aef5b0f46e9cc8555c16c Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Mon, 16 Apr 2018 21:51:21 +0800 Subject: [PATCH] --- src/org/redkale/source/EntityInfo.java | 188 +++++++++++++++++++++++-- 1 file changed, 178 insertions(+), 10 deletions(-) diff --git a/src/org/redkale/source/EntityInfo.java b/src/org/redkale/source/EntityInfo.java index 0779eeba6..ef1231083 100644 --- a/src/org/redkale/source/EntityInfo.java +++ b/src/org/redkale/source/EntityInfo.java @@ -91,7 +91,13 @@ public final class EntityInfo { final DistributeTableStrategy tableStrategy; //根据主键查找单个对象的SQL, 含 ? - final String queryPrepareSQL; + private final String queryPrepareSQL; + + //根据主键查找单个对象的SQL, 含 $ + private final String queryDollarPrepareSQL; + + //根据主键查找单个对象的SQL, 含 :name + private final String queryNamesPrepareSQL; //数据库中所有字段 private final Attribute[] queryAttributes; @@ -99,18 +105,36 @@ public final class EntityInfo { //新增SQL, 含 ?,即排除了自增长主键和标记为@Column(insertable=false)的字段 private final String insertPrepareSQL; + //新增SQL, 含 $,即排除了自增长主键和标记为@Column(insertable=false)的字段 + private final String insertDollarPrepareSQL; + + //新增SQL, 含 :name,即排除了自增长主键和标记为@Column(insertable=false)的字段 + private final String insertNamesPrepareSQL; + //数据库中所有可新增字段 final Attribute[] insertAttributes; //根据主键更新所有可更新字段的SQL,含 ? private final String updatePrepareSQL; + //根据主键更新所有可更新字段的SQL,含 $ + private final String updateDollarPrepareSQL; + + //根据主键更新所有可更新字段的SQL,含 :name + private final String updateNamesPrepareSQL; + //数据库中所有可更新字段 final Attribute[] updateAttributes; //根据主键删除记录的SQL,含 ? private final String deletePrepareSQL; + //根据主键删除记录的SQL,含 $ + private final String deleteDollarPrepareSQL; + + //根据主键删除记录的SQL,含 :name + private final String deleteNamesPrepareSQL; + //日志级别,从LogLevel获取 private final int logLevel; @@ -312,27 +336,63 @@ public final class EntityInfo { } if (table != null) { StringBuilder insertsb = new StringBuilder(); - StringBuilder insertsb2 = new StringBuilder(); + StringBuilder insertsbjdbc = new StringBuilder(); + StringBuilder insertsbdollar = new StringBuilder(); + StringBuilder insertsbnames = new StringBuilder(); + int index = 0; for (String col : insertcols) { if (insertsb.length() > 0) insertsb.append(','); insertsb.append(col); - if (insertsb2.length() > 0) insertsb2.append(','); - insertsb2.append('?'); + if (index > 0) { + insertsbjdbc.append(','); + insertsbdollar.append(','); + insertsbnames.append(','); + } + insertsbjdbc.append('?'); + insertsbdollar.append("$").append(++index); + insertsbnames.append(":").append(col); } - this.insertPrepareSQL = "INSERT INTO " + (this.tableStrategy == null ? table : "${newtable}") + "(" + insertsb + ") VALUES(" + insertsb2 + ")"; + this.insertPrepareSQL = "INSERT INTO " + (this.tableStrategy == null ? table : "${newtable}") + "(" + insertsb + ") VALUES(" + insertsbjdbc + ")"; + this.insertDollarPrepareSQL = "INSERT INTO " + (this.tableStrategy == null ? table : "${newtable}") + "(" + insertsb + ") VALUES(" + insertsbdollar + ")"; + this.insertNamesPrepareSQL = "INSERT INTO " + (this.tableStrategy == null ? table : "${newtable}") + "(" + insertsb + ") VALUES(" + insertsbnames + ")"; StringBuilder updatesb = new StringBuilder(); + StringBuilder updatesbdollar = new StringBuilder(); + StringBuilder updatesbnames = new StringBuilder(); + index = 0; for (String col : updatecols) { - if (updatesb.length() > 0) updatesb.append(", "); - updatesb.append(col).append(" = ?"); + if (updatesb.length() > 0) { + updatesb.append(','); + updatesbdollar.append(','); + updatesbnames.append(','); + } + updatesb.append(col).append("=?"); + updatesbdollar.append(col).append("=").append("$").append(++index); + updatesbnames.append(col).append("=:").append(col); } - this.updatePrepareSQL = "UPDATE " + (this.tableStrategy == null ? table : "${newtable}") + " SET " + updatesb + " WHERE " + getPrimarySQLColumn(null) + " = ?"; - this.deletePrepareSQL = "DELETE FROM " + (this.tableStrategy == null ? table : "${newtable}") + " WHERE " + getPrimarySQLColumn(null) + " = ?"; - this.queryPrepareSQL = "SELECT * FROM " + table + " WHERE " + getPrimarySQLColumn(null) + " = ?"; + this.updatePrepareSQL = "UPDATE " + (this.tableStrategy == null ? table : "${newtable}") + " SET " + updatesb + " WHERE " + getPrimarySQLColumn(null) + "=?"; + this.updateDollarPrepareSQL = "UPDATE " + (this.tableStrategy == null ? table : "${newtable}") + " SET " + updatesbdollar + " WHERE " + getPrimarySQLColumn(null) + "=$" + (++index); + this.updateNamesPrepareSQL = "UPDATE " + (this.tableStrategy == null ? table : "${newtable}") + " SET " + updatesbnames + " WHERE " + getPrimarySQLColumn(null) + "=:" + getPrimarySQLColumn(null); + this.deletePrepareSQL = "DELETE FROM " + (this.tableStrategy == null ? table : "${newtable}") + " WHERE " + getPrimarySQLColumn(null) + "=?"; + this.deleteDollarPrepareSQL = "DELETE FROM " + (this.tableStrategy == null ? table : "${newtable}") + " WHERE " + getPrimarySQLColumn(null) + "=$1"; + this.deleteNamesPrepareSQL = "DELETE FROM " + (this.tableStrategy == null ? table : "${newtable}") + " WHERE " + getPrimarySQLColumn(null) + "=:" + getPrimarySQLColumn(null); + this.queryPrepareSQL = "SELECT * FROM " + table + " WHERE " + getPrimarySQLColumn(null) + "=?"; + this.queryDollarPrepareSQL = "SELECT * FROM " + table + " WHERE " + getPrimarySQLColumn(null) + "=$1"; + this.queryNamesPrepareSQL = "SELECT * FROM " + table + " WHERE " + getPrimarySQLColumn(null) + "=:" + getPrimarySQLColumn(null); } else { this.insertPrepareSQL = null; this.updatePrepareSQL = null; this.deletePrepareSQL = null; this.queryPrepareSQL = null; + + this.insertDollarPrepareSQL = null; + this.updateDollarPrepareSQL = null; + this.deleteDollarPrepareSQL = null; + this.queryDollarPrepareSQL = null; + + this.insertNamesPrepareSQL = null; + this.updateNamesPrepareSQL = null; + this.deleteNamesPrepareSQL = null; + this.queryNamesPrepareSQL = null; } this.autoGenerated = auto; this.autouuid = uuid; @@ -405,6 +465,42 @@ public final class EntityInfo { return table == null; } + /** + * 获取Entity的QUERY SQL + * + * @param bean Entity对象 + * + * @return String + */ + public String getQueryPrepareSQL(T bean) { + if (this.tableStrategy == null) return queryPrepareSQL; + return queryPrepareSQL.replace("${newtable}", getTable(bean)); + } + + /** + * 获取Entity的QUERY SQL + * + * @param bean Entity对象 + * + * @return String + */ + public String getQueryDollarPrepareSQL(T bean) { + if (this.tableStrategy == null) return queryDollarPrepareSQL; + return queryDollarPrepareSQL.replace("${newtable}", getTable(bean)); + } + + /** + * 获取Entity的QUERY SQL + * + * @param bean Entity对象 + * + * @return String + */ + public String getQueryNamesPrepareSQL(T bean) { + if (this.tableStrategy == null) return queryNamesPrepareSQL; + return queryNamesPrepareSQL.replace("${newtable}", getTable(bean)); + } + /** * 获取Entity的INSERT SQL * @@ -417,6 +513,30 @@ public final class EntityInfo { return insertPrepareSQL.replace("${newtable}", getTable(bean)); } + /** + * 获取Entity的INSERT SQL + * + * @param bean Entity对象 + * + * @return String + */ + public String getInsertDollarPrepareSQL(T bean) { + if (this.tableStrategy == null) return insertDollarPrepareSQL; + return insertDollarPrepareSQL.replace("${newtable}", getTable(bean)); + } + + /** + * 获取Entity的INSERT SQL + * + * @param bean Entity对象 + * + * @return String + */ + public String getInsertNamesPrepareSQL(T bean) { + if (this.tableStrategy == null) return insertNamesPrepareSQL; + return insertNamesPrepareSQL.replace("${newtable}", getTable(bean)); + } + /** * 获取Entity的UPDATE SQL * @@ -429,6 +549,30 @@ public final class EntityInfo { return updatePrepareSQL.replace("${newtable}", getTable(bean)); } + /** + * 获取Entity的UPDATE SQL + * + * @param bean Entity对象 + * + * @return String + */ + public String getUpdateDollarPrepareSQL(T bean) { + if (this.tableStrategy == null) return updateDollarPrepareSQL; + return updateDollarPrepareSQL.replace("${newtable}", getTable(bean)); + } + + /** + * 获取Entity的UPDATE SQL + * + * @param bean Entity对象 + * + * @return String + */ + public String getUpdateNamesPrepareSQL(T bean) { + if (this.tableStrategy == null) return updateNamesPrepareSQL; + return updateNamesPrepareSQL.replace("${newtable}", getTable(bean)); + } + /** * 获取Entity的DELETE SQL * @@ -441,6 +585,30 @@ public final class EntityInfo { return deletePrepareSQL.replace("${newtable}", getTable(bean)); } + /** + * 获取Entity的DELETE SQL + * + * @param bean Entity对象 + * + * @return String + */ + public String getDeleteDollarPrepareSQL(T bean) { + if (this.tableStrategy == null) return deleteDollarPrepareSQL; + return deleteDollarPrepareSQL.replace("${newtable}", getTable(bean)); + } + + /** + * 获取Entity的DELETE SQL + * + * @param bean Entity对象 + * + * @return String + */ + public String getDeleteNamesPrepareSQL(T bean) { + if (this.tableStrategy == null) return deleteNamesPrepareSQL; + return deleteNamesPrepareSQL.replace("${newtable}", getTable(bean)); + } + /** * 获取查询字段列表 *