Commit 59d59db7 authored by 张永's avatar 张永

Merge branch 'master' of http://gitlab.egolm.com/key/common.git

parents 4a9692aa 4152889d
package com.egolm.common; package com.egolm.common;
import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
......
package com.egolm.common.jdbc.dialect;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.egolm.common.ReflexUtil;
import com.egolm.common.StringUtil;
import com.egolm.common.jdbc.dialect.bean.Desc;
import com.egolm.common.jdbc.dialect.bean.Sql;
/**
* JDBC方言抽象类
* @author 曲欣亮
* @since 2014-03-14
*/
public abstract class AbstractDialect implements Dialect {
public Sql argsInsert(Object obj) {
Class<?> clz = obj.getClass();
Desc i_table = new Desc(clz);
String table_name = i_table.getName();
String[] fields = i_table.getFieldNames();
String[] columns = i_table.getColumnNames();
List<String> names = new ArrayList<String>();
List<Object> args = new ArrayList<Object>();
for(int i = 0; i < fields.length; i++) {
Object objArg = ReflexUtil.readField(fields[i], obj);
if(objArg != null) {
names.add(columns[i]);
if(objArg != null && objArg instanceof Date) {
objArg = new Timestamp(((Date)objArg).getTime());
}
args.add(objArg);
}
}
String sql = "insert into " + table_name + StringUtil.join(getColumnRight() + ", " + getColumnLeft(), " (" + getColumnLeft(), getColumnRight() + ") ", names) + "values" + StringUtil.join("?", ", ", names.size(), " (", ")");
return new Sql(sql, args.toArray());
}
public Sql batchInsert(Object[] objs) {
if(objs.length > 0) {
Class<?> clz = objs[0].getClass();
Desc i_table = new Desc(clz);
String table_name = i_table.getName();
String[] fields = i_table.getFieldNames();
String[] columns = i_table.getColumnNames();
String[] pks = i_table.getFieldPkNames();
List<String> names = new ArrayList<String>();
Object[][] vals = new Object[objs.length][];
String expk = null;
for(String pk : pks) {
Object objArg = ReflexUtil.readField(pk, objs[0]);
if(objArg == null) {
expk = pk;
}
}
for(int k = 0; k < objs.length; k++) {
List<Object> args = new ArrayList<Object>();
for(int i = 0; i < fields.length; i++) {
if(!fields[i].equals(expk)) {
if(k == 0) {
names.add(columns[i]);
}
Object objArg = ReflexUtil.readField(fields[i], objs[k]);
if(objArg != null && objArg instanceof Date) {
objArg = new Timestamp(((Date)objArg).getTime());
}
args.add(objArg);
}
}
vals[k] = args.toArray();
}
String sql = "insert into " + table_name + StringUtil.join(getColumnRight() + ", " + getColumnLeft(), " (" + getColumnLeft(), getColumnRight() + ") ", names) + " values" + StringUtil.join("?", ", ", names.size(), " (", ")");
return new Sql(sql, vals);
} else {
throw new RuntimeException("DataArray can't be null");
}
}
public Sql batchInsert(String table_name, List<Map<String, Object>> objs) {
List<String> names = new ArrayList<String>();
Object[][] vals = new Object[objs.size()][];
for(int i = 0; i < objs.size(); i++) {
Map<String, Object> map = objs.get(i);
List<Object> valsAry = new ArrayList<Object>();
for(String key : map.keySet()) {
if(i == 0) {
names.add(key);
}
valsAry.add(map.get(key));
}
vals[i] = valsAry.toArray();
}
String sql = "insert into " + table_name + StringUtil.join(getColumnRight() + ", " + getColumnLeft(), " (" + getColumnLeft(), getColumnRight() + ") ", names) + " values" + StringUtil.join("?", ", ", names.size(), " (", ")");
return new Sql(sql, vals);
}
public Sql argsDelete(Object obj) {
Class<?> clz = obj.getClass();
Desc i_table = new Desc(clz);
String table_name = i_table.getName();
String[] pk_fields = i_table.getFieldPkNames();
String[] pk_columns = i_table.getColumnPkNames();
String[] strs = new String[pk_fields.length];
List<Object> args = new ArrayList<Object>();
for(int i = 0; i < pk_fields.length; i++) {
Object arg = ReflexUtil.readField(pk_fields[i], obj);
if(arg != null && arg instanceof Date) {
arg = new Timestamp(((Date)arg).getTime());
}
if(arg == null) {
strs[i] = getColumnLeft() + pk_columns[i] + getColumnRight() + " is null";
} else {
strs[i] = getColumnLeft() + pk_columns[i] + getColumnRight() + " = ?";
args.add(arg);
}
}
String sql = "delete from " + table_name + StringUtil.join(" and ", " where ", "", strs);
return new Sql(sql, args.toArray());
}
public Sql batchDelete(Object[] objs) {
if(objs.length > 0) {
Class<?> clz = objs[0].getClass();
Desc i_table = new Desc(clz);
String table_name = i_table.getName();
String[] pk_fields = i_table.getFieldPkNames();
String[] pk_columns = i_table.getColumnPkNames();
Object[][] vals = new Object[objs.length][];
for(int k = 0; k < objs.length; k++) {
Object obj = objs[k];
List<Object> args = new ArrayList<Object>();
for(int i = 0; i < pk_fields.length; i++) {
Object arg = ReflexUtil.readField(pk_fields[i], obj);
if(arg != null && arg instanceof Date) {
arg = new Timestamp(((Date)arg).getTime());
}
args.add(arg);
}
vals[k] = args.toArray();
}
String sql = "delete from " + table_name + StringUtil.join(getColumnRight() + " = ? and " + getColumnLeft(), " where " + getColumnLeft(), getColumnRight() + " = ?", pk_columns);
return new Sql(sql, vals);
} else {
throw new RuntimeException("DataArray can't be null");
}
}
public Sql argsUpdate(Object obj) {
Class<?> clz = obj.getClass();
Desc i_table = new Desc(clz);
String table_name = i_table.getName();
String[] pk_fields = i_table.getFieldPkNames();
String[] pk_columns = i_table.getColumnPkNames();
String[] fields = i_table.getFieldNames();
String[] columns = i_table.getColumnNames();
List<Object> args = new ArrayList<Object>();
List<String> strs = new ArrayList<String>();
List<String> vals = new ArrayList<String>();
for (int i = 0; i < fields.length; i++) {
Object arg = ReflexUtil.readField(fields[i], obj);
if(arg != null && arg instanceof Date) {
arg = new Timestamp(((Date)arg).getTime());
}
if (!StringUtil.contains(pk_columns, columns[i])) {
vals.add(getColumnLeft() + columns[i] + getColumnRight() + " = ?");
args.add(arg);
}
}
for (int i = 0; i < pk_fields.length; i++) {
Object arg = ReflexUtil.readField(pk_fields[i], obj);
if(arg != null && arg instanceof Date) {
arg = new Timestamp(((Date)arg).getTime());
}
if (arg == null) {
strs.add(getColumnLeft() + pk_columns[i] + getColumnRight() + " is null");
} else {
strs.add(getColumnLeft() + pk_columns[i] + getColumnRight() + " = ?");
args.add(arg);
}
}
String sql = "update " + table_name + StringUtil.join(", ", " set ", vals) + StringUtil.join(" and ", " where ", strs);
return new Sql(sql, args.toArray());
}
public Sql argsInsert(String tableName, Map<String, Object> objMap) {
String table_name = tableName;
Set<String> keySet = objMap.keySet();
String[] setArray = keySet.toArray(new String[keySet.size()]);
String[] fields = setArray;
String[] columns = setArray;
List<String> names = new ArrayList<String>();
List<Object> args = new ArrayList<Object>();
for(int i = 0; i < fields.length; i++) {
Object objArg = objMap.get(fields[i]);
if(objArg != null && objArg instanceof Date) {
objArg = new Timestamp(((Date)objArg).getTime());
}
if(objArg != null) {
names.add(columns[i]);
args.add(objArg);
}
}
String sql = "insert into " + table_name + StringUtil.join(getColumnRight() + ", " + getColumnLeft(), " (" + getColumnLeft(), getColumnRight() + ") ", names) + "values" + StringUtil.join("?", ", ", names.size(), " (", ")");
return new Sql(sql, args.toArray());
}
public Sql argsDelete(String tableName, String columnName, Object columnValue) {
if(columnValue != null && columnValue instanceof Date) {
columnValue = new Timestamp(((Date)columnValue).getTime());
}
String sql = "delete from " + tableName + " where " + getColumnLeft() + columnName + getColumnRight() + " = ?";
return new Sql(sql, new Object[]{columnValue});
}
public Sql argsDelete(String tableName, Map<String, Object> objMap) {
Set<String> keySet = objMap.keySet();
String[] setArray = keySet.toArray(new String[keySet.size()]);
String[] columns = setArray;
String[] args = new String[columns.length];
Object[] objs = new Object[columns.length];
for(int i = 0; i < columns.length; i++) {
args[i] = getColumnLeft() + columns[i] + getColumnRight() + " = ?";
Object objArg = objMap.get(columns[i]);
if(objArg != null && objArg instanceof Date) {
objArg = new Timestamp(((Date)objArg).getTime());
}
objs[i] = objArg;
}
String sql = "delete from " + tableName + StringUtil.join(" and ", " where ", "", args);
return new Sql(sql, objs);
}
public Sql argsUpdate(String tableName, String[] columnNames, Map<String, Object> objMap) {
String table_name = tableName;
String[] pk_fields = columnNames;
String[] pk_columns = columnNames;
Set<String> keySet = objMap.keySet();
String[] setArray = keySet.toArray(new String[keySet.size()]);
String[] fields = setArray;
String[] columns = setArray;
List<Object> args = new ArrayList<Object>();
List<String> strs = new ArrayList<String>();
List<String> vals = new ArrayList<String>();
for(int i = 0; i < fields.length; i++) {
Object arg = objMap.get(fields[i]);
if(arg != null && arg instanceof Date) {
arg = new Timestamp(((Date)arg).getTime());
}
if(!StringUtil.contains(pk_columns, columns[i])) {
vals.add(getColumnLeft() + columns[i] + getColumnRight() + " = ?");
args.add(arg);
}
}
for(int i = 0; i < pk_fields.length; i++) {
Object arg = objMap.get(pk_fields[i]);
if(arg != null && arg instanceof Date) {
arg = new Timestamp(((Date)arg).getTime());
}
if(arg == null) {
strs.add(getColumnLeft() + pk_columns[i] + getColumnRight() + " is null");
} else {
strs.add(getColumnLeft() + pk_columns[i] + getColumnRight() + " = ?");
args.add(arg);
}
}
String sql = "update " + table_name + StringUtil.join(", ", " set ", vals) + StringUtil.join(" and ", " where ", strs);
return new Sql(sql, args.toArray());
}
public boolean hasPkValue(Object obj) {
Class<?> clz = obj.getClass();
Desc i_table = new Desc(clz);
String[] pks = i_table.getFieldPkNames();
for(String pk : pks) {
if(ReflexUtil.readField(pk, obj) == null) {
return false;
}
}
return true;
}
public Map<String, Object> getPkValue(Object obj) {
Class<?> clz = obj.getClass();
Desc desc = new Desc(clz);
return getFieldValue(desc.getFieldPkNames());
}
public Map<String, Object> getFieldValue(Object obj, String... fields) {
Map<String, Object> pkMap = new HashMap<String, Object>();
for(String field : fields) {
Object value = ReflexUtil.readField(field, obj);
pkMap.put(field, value);
}
return pkMap;
}
public Map<String, Object> getColumnValueByField(Object obj, String... fields) {
Desc i_table = new Desc(obj.getClass());
String[] columns = i_table.getColumnsByFidlds(fields);
Map<String, Object> pkMap = new HashMap<String, Object>();
for(int i = 0; i < fields.length; i++) {
Object value = ReflexUtil.readField(fields[i], obj);
pkMap.put(columns[i], value);
}
return pkMap;
}
public Desc getDesc(Class<?> clz) {
return new Desc(clz);
}
public Sql argsQuery(String tableName, Map<String, Object> objMap) {
List<String> wheres = new ArrayList<String>();
List<Object> args = new ArrayList<Object>();
for(String key : objMap.keySet()) {
wheres.add(getColumnLeft() + key + getColumnRight() + " = ?");
args.add(objMap.get(key));
}
String sql = "select * from " + tableName + StringUtil.join(" and ", " where ", "", wheres);
return new Sql(sql, args.toArray());
}
public Map<String, Object> fieldsToColumns(Class<?> type, Map<String, Object> byFields) {
Desc desc = new Desc(type);
String[] fieldNames = desc.getFieldNames();
String[] columnNames = desc.getColumnNames();
Map<String, Object> byColumns = new HashMap<String, Object>();
for(int i = 0; i < fieldNames.length; i++) {
String fieldName = fieldNames[i];
String columnName = columnNames[i];
if(byFields.containsKey(fieldName)) {
byColumns.put(columnName, byFields.get(fieldName));
}
}
return byColumns;
}
}
package com.egolm.common.jdbc.dialect; package com.egolm.common.jdbc.dialect;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import com.egolm.common.ReflexUtil;
import com.egolm.common.StringUtil;
import com.egolm.common.jdbc.Page; import com.egolm.common.jdbc.Page;
import com.egolm.common.jdbc.dialect.bean.Desc; import com.egolm.common.jdbc.dialect.bean.Desc;
import com.egolm.common.jdbc.dialect.bean.Sql; import com.egolm.common.jdbc.dialect.bean.Sql;
...@@ -14,127 +21,339 @@ import com.egolm.common.jdbc.dialect.bean.Sql; ...@@ -14,127 +21,339 @@ import com.egolm.common.jdbc.dialect.bean.Sql;
*/ */
public interface Dialect { public interface Dialect {
/**
* 生成分页查询SQL语句
* @param sql 原SQL
* @param page 分页工具类
* @return
*/
public String limit(String sql, Page page); public String limit(String sql, Page page);
/**
* 生成统计数据条数的SQL
* @param sql 原SQL语句(要统计数据条数的SQL语句)
* @return
*/
public String count(String sql); public String count(String sql);
/**
* 生成插入SQL语句
* @param obj 要插入的对象
* @return
*/
public Sql argsInsert(Object obj);
/**
* 生成插入SQL语句
* @param tableName 表名
* @param objMap 要插入的对象
* @return
*/
public Sql argsInsert(String tableName, Map<String, Object> objMap);
/**
* 生成插入SQL语句
* @param obj
* @return
*/
public Sql batchInsert(Object[] obj);
public Sql batchInsert(String tableName, List<Map<String, Object>> objs);
/**
* 生成删除SQL语句
* @param obj 要删除的对象
* @return
*/
public Sql argsDelete(Object obj);
public Sql batchDelete(Object[] obj);
/**
* 生成删除SQL语句
* @param tableName
* @param columnName
* @param columnValue
* @return
*/
public Sql argsDelete(String tableName, String columnName, Object columnValue);
/**
* 生成删除SQL语句
* @param tableName
* @param objMap
* @return
*/
public Sql argsDelete(String tableName, Map<String, Object> objMap);
/**
* 生成更新SQL语句(根据ID更新)
* @param obj 要更新的对象
* @return
*/
public Sql argsUpdate(Object obj);
/**
* 生成更新SQL语句
* @param tableName
* @param columnNames
* @param objMap
* @return
*/
public Sql argsUpdate(String tableName, String[] columnNames, Map<String, Object> objMap);
public Sql argsQuery(String tableName, Map<String, Object> objMap);
public String ifExists(String tableName); public String ifExists(String tableName);
public String ifNotExists(String tableName); public String ifNotExists(String tableName);
/** public String selectDate();
* 生成建表SQL语句
* @param clzz
* @return
*/
public String[] sqlTable(Class<?>... claz);
/**
* 判断主键字段是否有值
* @Description
* @param obj
* @return
* @author 曲欣亮
* @date 2016年7月13日
* @since 2016年7月13日
* @return boolean
* @throws
*/
public boolean hasPkValue(Object obj);
public Map<String, Object> getPkValue(Object obj); public String getColumnLeft();
public String getColumnRight();
default Sql argsInsert(Object obj) {
Class<?> clz = obj.getClass();
Desc i_table = new Desc(clz);
String table_name = i_table.getName();
String[] fields = i_table.getFieldNames();
String[] columns = i_table.getColumnNames();
List<String> names = new ArrayList<String>();
List<Object> args = new ArrayList<Object>();
for(int i = 0; i < fields.length; i++) {
Object objArg = ReflexUtil.readField(fields[i], obj);
if(objArg != null) {
names.add(columns[i]);
if(objArg != null && objArg instanceof Date) {
objArg = new Timestamp(((Date)objArg).getTime());
}
args.add(objArg);
}
}
String sql = "insert into " + table_name + StringUtil.join(getColumnRight() + ", " + getColumnLeft(), " (" + getColumnLeft(), getColumnRight() + ") ", names) + "values" + StringUtil.join("?", ", ", names.size(), " (", ")");
return new Sql(sql, args.toArray());
}
default Sql batchInsert(Object[] objs) {
if(objs.length > 0) {
Class<?> clz = objs[0].getClass();
Desc i_table = new Desc(clz);
String table_name = i_table.getName();
String[] fields = i_table.getFieldNames();
String[] columns = i_table.getColumnNames();
String[] pks = i_table.getFieldPkNames();
List<String> names = new ArrayList<String>();
Object[][] vals = new Object[objs.length][];
String expk = null;
for(String pk : pks) {
Object objArg = ReflexUtil.readField(pk, objs[0]);
if(objArg == null) {
expk = pk;
}
}
for(int k = 0; k < objs.length; k++) {
List<Object> args = new ArrayList<Object>();
for(int i = 0; i < fields.length; i++) {
if(!fields[i].equals(expk)) {
if(k == 0) {
names.add(columns[i]);
}
Object objArg = ReflexUtil.readField(fields[i], objs[k]);
if(objArg != null && objArg instanceof Date) {
objArg = new Timestamp(((Date)objArg).getTime());
}
args.add(objArg);
}
}
vals[k] = args.toArray();
}
String sql = "insert into " + table_name + StringUtil.join(getColumnRight() + ", " + getColumnLeft(), " (" + getColumnLeft(), getColumnRight() + ") ", names) + " values" + StringUtil.join("?", ", ", names.size(), " (", ")");
return new Sql(sql, vals);
} else {
throw new RuntimeException("DataArray can't be null");
}
}
default Sql batchInsert(String table_name, List<Map<String, Object>> objs) {
List<String> names = new ArrayList<String>();
Object[][] vals = new Object[objs.size()][];
for(int i = 0; i < objs.size(); i++) {
Map<String, Object> map = objs.get(i);
List<Object> valsAry = new ArrayList<Object>();
for(String key : map.keySet()) {
if(i == 0) {
names.add(key);
}
valsAry.add(map.get(key));
}
vals[i] = valsAry.toArray();
}
String sql = "insert into " + table_name + StringUtil.join(getColumnRight() + ", " + getColumnLeft(), " (" + getColumnLeft(), getColumnRight() + ") ", names) + " values" + StringUtil.join("?", ", ", names.size(), " (", ")");
return new Sql(sql, vals);
}
public Map<String, Object> getFieldValue(Object obj, String... field); default Sql argsDelete(Object obj) {
Class<?> clz = obj.getClass();
Desc i_table = new Desc(clz);
String table_name = i_table.getName();
String[] pk_fields = i_table.getFieldPkNames();
String[] pk_columns = i_table.getColumnPkNames();
String[] strs = new String[pk_fields.length];
List<Object> args = new ArrayList<Object>();
for(int i = 0; i < pk_fields.length; i++) {
Object arg = ReflexUtil.readField(pk_fields[i], obj);
if(arg != null && arg instanceof Date) {
arg = new Timestamp(((Date)arg).getTime());
}
if(arg == null) {
strs[i] = getColumnLeft() + pk_columns[i] + getColumnRight() + " is null";
} else {
strs[i] = getColumnLeft() + pk_columns[i] + getColumnRight() + " = ?";
args.add(arg);
}
}
String sql = "delete from " + table_name + StringUtil.join(" and ", " where ", "", strs);
return new Sql(sql, args.toArray());
}
public Map<String, Object> getColumnValueByField(Object obj, String... field); default Sql batchDelete(Object[] objs) {
if(objs.length > 0) {
Class<?> clz = objs[0].getClass();
Desc i_table = new Desc(clz);
String table_name = i_table.getName();
String[] pk_fields = i_table.getFieldPkNames();
String[] pk_columns = i_table.getColumnPkNames();
Object[][] vals = new Object[objs.length][];
for(int k = 0; k < objs.length; k++) {
Object obj = objs[k];
List<Object> args = new ArrayList<Object>();
for(int i = 0; i < pk_fields.length; i++) {
Object arg = ReflexUtil.readField(pk_fields[i], obj);
if(arg != null && arg instanceof Date) {
arg = new Timestamp(((Date)arg).getTime());
}
args.add(arg);
}
vals[k] = args.toArray();
}
String sql = "delete from " + table_name + StringUtil.join(getColumnRight() + " = ? and " + getColumnLeft(), " where " + getColumnLeft(), getColumnRight() + " = ?", pk_columns);
return new Sql(sql, vals);
} else {
throw new RuntimeException("DataArray can't be null");
}
}
public Desc getDesc(Class<?> clz); default Sql argsUpdate(Object obj) {
Class<?> clz = obj.getClass();
Desc i_table = new Desc(clz);
String table_name = i_table.getName();
String[] pk_fields = i_table.getFieldPkNames();
String[] pk_columns = i_table.getColumnPkNames();
String[] fields = i_table.getFieldNames();
String[] columns = i_table.getColumnNames();
List<Object> args = new ArrayList<Object>();
List<String> strs = new ArrayList<String>();
List<String> vals = new ArrayList<String>();
for (int i = 0; i < fields.length; i++) {
Object arg = ReflexUtil.readField(fields[i], obj);
if(arg != null && arg instanceof Date) {
arg = new Timestamp(((Date)arg).getTime());
}
if (!StringUtil.contains(pk_columns, columns[i])) {
vals.add(getColumnLeft() + columns[i] + getColumnRight() + " = ?");
args.add(arg);
}
}
for (int i = 0; i < pk_fields.length; i++) {
Object arg = ReflexUtil.readField(pk_fields[i], obj);
if(arg != null && arg instanceof Date) {
arg = new Timestamp(((Date)arg).getTime());
}
if (arg == null) {
strs.add(getColumnLeft() + pk_columns[i] + getColumnRight() + " is null");
} else {
strs.add(getColumnLeft() + pk_columns[i] + getColumnRight() + " = ?");
args.add(arg);
}
}
String sql = "update " + table_name + StringUtil.join(", ", " set ", vals) + StringUtil.join(" and ", " where ", strs);
return new Sql(sql, args.toArray());
}
public Map<String, Object> fieldsToColumns(Class<?> type, Map<String, Object> byFields); default Sql argsInsert(String tableName, Map<String, Object> objMap) {
String table_name = tableName;
Set<String> keySet = objMap.keySet();
String[] setArray = keySet.toArray(new String[keySet.size()]);
String[] fields = setArray;
String[] columns = setArray;
List<String> names = new ArrayList<String>();
List<Object> args = new ArrayList<Object>();
for(int i = 0; i < fields.length; i++) {
Object objArg = objMap.get(fields[i]);
if(objArg != null && objArg instanceof Date) {
objArg = new Timestamp(((Date)objArg).getTime());
}
if(objArg != null) {
names.add(columns[i]);
args.add(objArg);
}
}
String sql = "insert into " + table_name + StringUtil.join(getColumnRight() + ", " + getColumnLeft(), " (" + getColumnLeft(), getColumnRight() + ") ", names) + "values" + StringUtil.join("?", ", ", names.size(), " (", ")");
return new Sql(sql, args.toArray());
}
public String selectDate(); default Sql argsDelete(String tableName, String columnName, Object columnValue) {
if(columnValue != null && columnValue instanceof Date) {
columnValue = new Timestamp(((Date)columnValue).getTime());
}
String sql = "delete from " + tableName + " where " + getColumnLeft() + columnName + getColumnRight() + " = ?";
return new Sql(sql, new Object[]{columnValue});
}
default Sql argsDelete(String tableName, Map<String, Object> objMap) {
Set<String> keySet = objMap.keySet();
String[] setArray = keySet.toArray(new String[keySet.size()]);
String[] columns = setArray;
String[] args = new String[columns.length];
Object[] objs = new Object[columns.length];
for(int i = 0; i < columns.length; i++) {
args[i] = getColumnLeft() + columns[i] + getColumnRight() + " = ?";
Object objArg = objMap.get(columns[i]);
if(objArg != null && objArg instanceof Date) {
objArg = new Timestamp(((Date)objArg).getTime());
}
objs[i] = objArg;
}
String sql = "delete from " + tableName + StringUtil.join(" and ", " where ", "", args);
return new Sql(sql, objs);
}
public String getColumnLeft(); default Sql argsUpdate(String tableName, String[] columnNames, Map<String, Object> objMap) {
String table_name = tableName;
String[] pk_fields = columnNames;
String[] pk_columns = columnNames;
Set<String> keySet = objMap.keySet();
String[] setArray = keySet.toArray(new String[keySet.size()]);
String[] fields = setArray;
String[] columns = setArray;
List<Object> args = new ArrayList<Object>();
List<String> strs = new ArrayList<String>();
List<String> vals = new ArrayList<String>();
for(int i = 0; i < fields.length; i++) {
Object arg = objMap.get(fields[i]);
if(arg != null && arg instanceof Date) {
arg = new Timestamp(((Date)arg).getTime());
}
if(!StringUtil.contains(pk_columns, columns[i])) {
vals.add(getColumnLeft() + columns[i] + getColumnRight() + " = ?");
args.add(arg);
}
}
for(int i = 0; i < pk_fields.length; i++) {
Object arg = objMap.get(pk_fields[i]);
if(arg != null && arg instanceof Date) {
arg = new Timestamp(((Date)arg).getTime());
}
if(arg == null) {
strs.add(getColumnLeft() + pk_columns[i] + getColumnRight() + " is null");
} else {
strs.add(getColumnLeft() + pk_columns[i] + getColumnRight() + " = ?");
args.add(arg);
}
}
String sql = "update " + table_name + StringUtil.join(", ", " set ", vals) + StringUtil.join(" and ", " where ", strs);
return new Sql(sql, args.toArray());
}
default boolean hasPkValue(Object obj) {
Class<?> clz = obj.getClass();
Desc i_table = new Desc(clz);
String[] pks = i_table.getFieldPkNames();
for(String pk : pks) {
if(ReflexUtil.readField(pk, obj) == null) {
return false;
}
}
return true;
}
default Map<String, Object> getPkValue(Object obj) {
Class<?> clz = obj.getClass();
Desc desc = new Desc(clz);
return getFieldValue(desc.getFieldPkNames());
}
public String getColumnRight(); default Map<String, Object> getFieldValue(Object obj, String... fields) {
Map<String, Object> pkMap = new HashMap<String, Object>();
for(String field : fields) {
Object value = ReflexUtil.readField(field, obj);
pkMap.put(field, value);
}
return pkMap;
}
default Map<String, Object> getColumnValueByField(Object obj, String... fields) {
Desc i_table = new Desc(obj.getClass());
String[] columns = i_table.getColumnsByFidlds(fields);
Map<String, Object> pkMap = new HashMap<String, Object>();
for(int i = 0; i < fields.length; i++) {
Object value = ReflexUtil.readField(fields[i], obj);
pkMap.put(columns[i], value);
}
return pkMap;
}
default Desc getDesc(Class<?> clz) {
return new Desc(clz);
}
default Sql argsQuery(String tableName, Map<String, Object> objMap) {
List<String> wheres = new ArrayList<String>();
List<Object> args = new ArrayList<Object>();
for(String key : objMap.keySet()) {
wheres.add(getColumnLeft() + key + getColumnRight() + " = ?");
args.add(objMap.get(key));
}
String sql = "select * from " + tableName + StringUtil.join(" and ", " where ", "", wheres);
return new Sql(sql, args.toArray());
}
default Map<String, Object> fieldsToColumns(Class<?> type, Map<String, Object> byFields) {
Desc desc = new Desc(type);
String[] fieldNames = desc.getFieldNames();
String[] columnNames = desc.getColumnNames();
Map<String, Object> byColumns = new HashMap<String, Object>();
for(int i = 0; i < fieldNames.length; i++) {
String fieldName = fieldNames[i];
String columnName = columnNames[i];
if(byFields.containsKey(fieldName)) {
byColumns.put(columnName, byFields.get(fieldName));
}
}
return byColumns;
}
} }
...@@ -14,7 +14,7 @@ import com.egolm.common.jdbc.dialect.bean.Desc; ...@@ -14,7 +14,7 @@ import com.egolm.common.jdbc.dialect.bean.Desc;
* @author 曲欣亮 * @author 曲欣亮
* @since 2012-12-14 * @since 2012-12-14
*/ */
public class MySqlDialect extends AbstractDialect { public class MySqlDialect implements Dialect {
public static final String FMT_MYSQL_TIME = "%Y-%m-%d %H:%i:%s"; public static final String FMT_MYSQL_TIME = "%Y-%m-%d %H:%i:%s";
public static final String FMT_MYSQL_DATE = "%Y-%m-%d"; public static final String FMT_MYSQL_DATE = "%Y-%m-%d";
......
...@@ -13,7 +13,7 @@ import com.egolm.common.jdbc.dialect.bean.Desc; ...@@ -13,7 +13,7 @@ import com.egolm.common.jdbc.dialect.bean.Desc;
* @author 曲欣亮 * @author 曲欣亮
* @since 2012-12-14 * @since 2012-12-14
*/ */
public class OracleDialect extends AbstractDialect { public class OracleDialect implements Dialect {
public static final String FMT_ORACLE_TIME = "yyyy-mm-dd hh24:mi:ss"; public static final String FMT_ORACLE_TIME = "yyyy-mm-dd hh24:mi:ss";
public static final String FMT_ORACLE_DATE = "yyyy-mm-dd"; public static final String FMT_ORACLE_DATE = "yyyy-mm-dd";
......
...@@ -14,7 +14,7 @@ import com.egolm.common.jdbc.dialect.bean.Desc; ...@@ -14,7 +14,7 @@ import com.egolm.common.jdbc.dialect.bean.Desc;
* @author 曲欣亮 * @author 曲欣亮
* @since 2012-12-14 * @since 2012-12-14
*/ */
public class SqlServerDialect extends AbstractDialect { public class SqlServerDialect implements Dialect {
public static final String FMT_SQLSERVER_TIME = "120"; public static final String FMT_SQLSERVER_TIME = "120";
public static final String FMT_SQLSERVER_DATE = "23"; public static final String FMT_SQLSERVER_DATE = "23";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment