package com..utils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;
public class JDBCTools {
public static String mapToInertSQL(Map map, String insertTableName, String[] aesdecode,
String... exclude) {
String insertSql ="insert into `" +escapeSql(insertTableName) +"` (%s) values (%s);";
StringBuffer columsString =new StringBuffer();
StringBuffer valuesString =new StringBuffer();
Set> entry = map.entrySet();
for (Entry en : entry) {
if (exclude !=null && ArrayUtils.contains(exclude, en.getKey())) {
continue;
}
if (en.getValue() !=null) {
columsString.append('`').append(escapeSql(en.getKey())).append('`').append(',');
if (en.getValue()instanceof Integer) {
String v = en.getValue().toString();
if (aesdecode !=null && ArrayUtils.contains(aesdecode, en.getKey())) {
v = v;
}
valuesString.append(v).append(',');
}else if (en.getValue()instanceof Long) {
String v = en.getValue().toString();
if (aesdecode !=null && ArrayUtils.contains(aesdecode, en.getKey())) {
v = v;
}
valuesString.append(v).append(',');
}else if (en.getValue()instanceof BigDecimal) {
String v = en.getValue().toString();
if (aesdecode !=null && ArrayUtils.contains(aesdecode, en.getKey())) {
v = v;
}
valuesString.append(v).append(',');
}else if (en.getValue()instanceof Boolean) {
String v = en.getValue().toString();
if (aesdecode !=null && ArrayUtils.contains(aesdecode, en.getKey())) {
v = v;
}
valuesString.append(v).append(',');
}else if (en.getValue()instanceof Date) {
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String v = sdf.format((Date) en.getValue());
if (aesdecode !=null && ArrayUtils.contains(aesdecode, en.getKey())) {
v = v;
}
valuesString.append('\'').append(escapeSql(v)).append('\'').append(',');
}else {
String v = en.getValue().toString();
if (aesdecode !=null && ArrayUtils.contains(aesdecode, en.getKey())) {
v = v;
}
valuesString.append('\'').append(escapeSql(v)).append('\'').append(',');
}
}
}
return String.format(insertSql, columsString.substring(0, columsString.length() -1),
valuesString.substring(0, valuesString.length() -1));
}
public static String mapToUpdateSQL(Map map, String updateTableName, String whereColums,
String... exclude) {
String updateSql ="update " +escapeSql(updateTableName) +" set %s where %s;";
StringBuffer columsString =new StringBuffer();
StringBuffer valueString =new StringBuffer();
StringBuffer whereString =new StringBuffer();
Set> entry = map.entrySet();
for (Entry en : entry) {
if (exclude !=null && ArrayUtils.contains(exclude, en.getKey())) {
continue;
}
columsString.append('`').append(escapeSql(en.getKey())).append('`');
valueString =new StringBuffer();
if (en.getValue() ==null) {
valueString.append("null");
}else if (en.getValue()instanceof Integer) {
valueString.append(escapeSql(en.getValue().toString()));
}else if (en.getValue()instanceof Long) {
valueString.append(escapeSql(en.getValue().toString()));
}else if (en.getValue()instanceof BigDecimal) {
valueString.append(escapeSql(en.getValue().toString()));
}else if (en.getValue()instanceof Boolean) {
valueString.append(escapeSql(en.getValue().toString()));
}else {
valueString.append('\'').append(escapeSql(en.getValue().toString())).append('\'');
}
if (whereColums !=null && whereColums.equals(en.getKey())) {
whereString.append(escapeSql(en.getKey())).append('=').append(valueString);
}
columsString.append('=').append(valueString).append(',');
}
return String.format(updateSql, columsString.substring(0, columsString.length() -1), whereString);
}
public static String listToBatchInertSQL(List> list, String insertTableName, String[] aesdecode,
String... exclude) {
String insertSql ="insert into " +escapeSql(insertTableName) +" (%s) values %s;";
StringBuffer columsString =new StringBuffer();
StringBuffer valuesString =new StringBuffer();
String lastKey =null;
Set keysSet =new TreeSet();
if (list.size() >0) {
Map map = list.get(0);
Set> entry = map.entrySet();
for (Entry en : entry) {
System.out.println(en.getKey());
if (exclude !=null && ArrayUtils.contains(exclude, en.getKey())) {
continue;
}
columsString.append('`').append(escapeSql(en.getKey())).append('`').append(',');
lastKey = en.getKey();
System.out.println("lastKey:"+lastKey);
keysSet.add(lastKey);
}
}
for (Map map : list) {
Set> entry = map.entrySet();
valuesString.append('(');
for (Entry en : entry) {
if (exclude !=null && ArrayUtils.contains(exclude, en.getKey())) {
continue;
}
if (en.getValue() !=null) {
if (en.getValue()instanceof Integer) {
String v = en.getValue().toString();
if (aesdecode !=null && ArrayUtils.contains(aesdecode, en.getKey())) {
v = v;
}
valuesString.append(v);
}else if (en.getValue()instanceof Long) {
String v = en.getValue().toString();
if (aesdecode !=null && ArrayUtils.contains(aesdecode, en.getKey())) {
v = v;
}
valuesString.append(v);
}else if (en.getValue()instanceof Date) {
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String v = sdf.format((Date) en.getValue());
if (aesdecode !=null && ArrayUtils.contains(aesdecode, en.getKey())) {
v = v;
}
valuesString.append('\'').append(escapeSql(v)).append('\'');
}else if (en.getValue()instanceof BigDecimal) {
String v = en.getValue().toString();
if (aesdecode !=null && ArrayUtils.contains(aesdecode, en.getKey())) {
v = v;
}
valuesString.append(v);
}else if (en.getValue()instanceof Boolean) {
String v = en.getValue().toString();
if (aesdecode !=null && ArrayUtils.contains(aesdecode, en.getKey())) {
v = v;
}
valuesString.append(v);
}else {
String v = en.getValue().toString();
if (aesdecode !=null && ArrayUtils.contains(aesdecode, en.getKey())) {
v = v;
}
valuesString.append('\'').append(escapeSql(v)).append('\'');
}
}else {
if (keysSet.contains(en.getKey())) {
valuesString.append("null");
}
}
if (!en.getKey().equals(lastKey)) {
valuesString.append(',');
}
}
valuesString.append(')').append(',');
}
return insertSql.format(insertSql, columsString.substring(0, columsString.length() -1),
valuesString.substring(0, valuesString.length() -1));
}
/**
* 获取表中最大id
* @param selectTableName
* @return
*/
public static String getMaxId(String selectTableName){
return "select max(id) from " +escapeSql(selectTableName);
}
public static String escapeSql(String str) {
if (str ==null) {
return null;
}
String a = StringUtils.replace(str,"'","''");
return StringUtils.replace(a,"\\","\\\\");
}
public static List batchInsert(JdbcTemplate jdbcTemplate, List sqlList) {
if (sqlList ==null || sqlList.isEmpty()) {
return null;
}
List rList =new ArrayList<>();
String[] sqlstrings = sqlList.toArray(new String[sqlList.size()]);
for (int i =0; i < sqlstrings.length; i = i +5000) {
String[] sqltemp =null;
if (sqlstrings.length - i >=5000) {
sqltemp =new String[5000];
System.arraycopy(sqlstrings, i, sqltemp,0,5000);
}else {
sqltemp =new String[sqlstrings.length - i];
System.arraycopy(sqlstrings, i, sqltemp,0, sqlstrings.length - i);
}
int[] r = jdbcTemplate.batchUpdate(sqltemp);
Integer[] rI = ArrayUtils.toObject(r);
rList.addAll(Arrays.asList(rI));
}
return rList;
}
public static List batchUpdate(JdbcTemplate jdbcTemplate, List sqlList) {
if (sqlList ==null || sqlList.isEmpty()) {
return null;
}
List rList =new ArrayList<>();
String[] sqlstrings = sqlList.toArray(new String[sqlList.size()]);
for (int i =0; i < sqlstrings.length; i = i +5000) {
String[] sqltemp =null;
if (sqlstrings.length - i >=5000) {
sqltemp =new String[5000];
System.arraycopy(sqlstrings, i, sqltemp,0,5000);
}else {
sqltemp =new String[sqlstrings.length - i];
System.arraycopy(sqlstrings, i, sqltemp,0, sqlstrings.length - i);
}
int[] r = jdbcTemplate.batchUpdate(sqltemp);
Integer[] rI = ArrayUtils.toObject(r);
rList.addAll(Arrays.asList(rI));
}
return rList;
}
public static String camel2Underline(String line) {
if ("name".equals(line)) {
line ="user_name";
}
StringBuilder sb =new StringBuilder();
for (int i =0; i < line.length(); ++i) {
char c = line.charAt(i);
if (Character.isUpperCase(c)) {
sb.append('_').append(Character.toLowerCase(c));
}else {
sb.append(c);
}
}
return sb.toString();
}
public static String underline2Camel(String line,boolean... firstIsUpperCase) {
String str ="";
if (StringUtils.isBlank(line)) {
return str;
}else {
StringBuilder sb =new StringBuilder();
String[] strArr;
// 不包含下划线,且第二个参数是空的
if (!line.contains("_") && firstIsUpperCase.length ==0) {
sb.append(line.substring(0,1).toLowerCase()).append(line.substring(1));
str = sb.toString();
}else if (!line.contains("_") && firstIsUpperCase.length !=0) {
if (!firstIsUpperCase[0]) {
sb.append(line.substring(0,1).toLowerCase()).append(line.substring(1));
str = sb.toString();
}else {
sb.append(line.substring(0,1).toUpperCase()).append(line.substring(1));
str = sb.toString();
}
}else if (line.contains("_") && firstIsUpperCase.length ==0) {
strArr = line.split("_");
for (String s : strArr) {
sb.append(s.substring(0,1).toUpperCase()).append(s.substring(1));
}
str = sb.toString();
str = str.substring(0,1).toLowerCase() + str.substring(1);
}else if (line.contains("_") && firstIsUpperCase.length !=0) {
strArr = line.split("_");
for (String s : strArr) {
sb.append(s.substring(0,1).toUpperCase()).append(s.substring(1));
}
if (!firstIsUpperCase[0]) {
str = sb.toString();
str = str.substring(0,1).toLowerCase() + str.substring(1);
}else {
str = sb.toString();
}
}
}
return str;
}
}