|
@@ -0,0 +1,109 @@
|
|
|
+package com.jzdsh.common.database;
|
|
|
+
|
|
|
+import com.jfinal.plugin.activerecord.Record;
|
|
|
+import com.jfinal.plugin.activerecord.Table;
|
|
|
+import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 重写增加和保存的
|
|
|
+ */
|
|
|
+public class JFMysqlDialect extends MysqlDialect {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Do not delete the String[] pKeys parameter, the element of pKeys needs to trim()
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void forDbSave(String tableName, String[] pKeys, Record record, StringBuilder sql, List<Object> paras) {
|
|
|
+ tableName = tableName.trim();
|
|
|
+ trimPrimaryKeys(pKeys); // important
|
|
|
+
|
|
|
+ sql.append("insert into ");
|
|
|
+ sql.append(tableName).append("(");
|
|
|
+ StringBuilder temp = new StringBuilder();
|
|
|
+ temp.append(") values(");
|
|
|
+
|
|
|
+ for (Map.Entry<String, Object> e: record.getColumns().entrySet()) {
|
|
|
+ if (paras.size() > 0) {
|
|
|
+ sql.append(", ");
|
|
|
+ temp.append(", ");
|
|
|
+ }
|
|
|
+ sql.append("`").append(e.getKey()).append("`");
|
|
|
+ temp.append("?");
|
|
|
+ paras.add(e.getValue());
|
|
|
+ }
|
|
|
+ sql.append(temp.toString()).append(")");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void forDbUpdate(String tableName, String[] pKeys, Object[] ids, Record record, StringBuilder sql, List<Object> paras) {
|
|
|
+ tableName = tableName.trim();
|
|
|
+ trimPrimaryKeys(pKeys);
|
|
|
+
|
|
|
+ sql.append("update ").append(tableName).append(" set ");
|
|
|
+ for (Map.Entry<String, Object> e: record.getColumns().entrySet()) {
|
|
|
+ String colName = e.getKey();
|
|
|
+ if (!isPrimaryKey(colName, pKeys)) {
|
|
|
+ if (paras.size() > 0) {
|
|
|
+ sql.append(", ");
|
|
|
+ }
|
|
|
+ sql.append("`").append(colName).append("` = ? ");
|
|
|
+ paras.add(e.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sql.append(" where ");
|
|
|
+ for (int i=0; i<pKeys.length; i++) {
|
|
|
+ if (i > 0) {
|
|
|
+ sql.append(" and ");
|
|
|
+ }
|
|
|
+ sql.append("`").append(pKeys[i]).append("` = ?");
|
|
|
+ paras.add(ids[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void forModelSave(Table table, Map<String, Object> attrs, StringBuilder sql, List<Object> paras) {
|
|
|
+ sql.append("insert into ").append(table.getName()).append("(");
|
|
|
+ StringBuilder temp = new StringBuilder(") values(");
|
|
|
+ for (Map.Entry<String, Object> e: attrs.entrySet()) {
|
|
|
+ String colName = e.getKey();
|
|
|
+ if (table.hasColumnLabel(colName)) {
|
|
|
+ if (paras.size() > 0) {
|
|
|
+ sql.append(", ");
|
|
|
+ temp.append(", ");
|
|
|
+ }
|
|
|
+ sql.append("`").append(colName).append("`");
|
|
|
+ temp.append("?");
|
|
|
+ paras.add(e.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sql.append(temp.toString()).append(")");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void forModelUpdate(Table table, Map<String, Object> attrs, Set<String> modifyFlag, StringBuilder sql, List<Object> paras) {
|
|
|
+ sql.append("update ").append(table.getName()).append(" set ");
|
|
|
+ String[] pKeys = table.getPrimaryKey();
|
|
|
+ for (Map.Entry<String, Object> e : attrs.entrySet()) {
|
|
|
+ String colName = e.getKey();
|
|
|
+ if (modifyFlag.contains(colName) && !isPrimaryKey(colName, pKeys) && table.hasColumnLabel(colName)) {
|
|
|
+ if (paras.size() > 0) {
|
|
|
+ sql.append(", ");
|
|
|
+ }
|
|
|
+ sql.append("`").append(colName).append("` = ? ");
|
|
|
+ paras.add(e.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sql.append(" where ");
|
|
|
+ for (int i=0; i<pKeys.length; i++) {
|
|
|
+ if (i > 0) {
|
|
|
+ sql.append(" and ");
|
|
|
+ }
|
|
|
+ sql.append("`").append(pKeys[i]).append("` = ?");
|
|
|
+ paras.add(attrs.get(pKeys[i]));
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|