package org.nobject.common.db;
/**
* TODO
*
* @author gaozhengxing
* @date 2023-04-08 11:55
*/
import javassist.ClassPool;
import org.nobject.common.db.model.ORMPOJO;
import org.nobject.common.exception.DBException;
import org.nobject.common.lang.CollectionUtils;
import org.nobject.common.lang.MapUtils;
import org.nobject.common.log.Logger;
import java.lang.reflect.Field;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class DBFactory {
private Logger logger = Logger.getLogger(DBFactory.class);
protected ConnectionPool pool;
private boolean showsql = true;
private List path = new LinkedList();
public List<ORMPOJO> orms = new LinkedList();
public boolean autoSharding = true;
private boolean optimise = false;
private SQLOptimiser sqlOptimiser;
private boolean validateOnSave = false;
private boolean validateOnUpdate = false;
private ResultHandler resultHandler_mapping = new ResultHandlerMapping();
private ResultHandler resultHandler_orm = new ResultHandlerORM();
private Boolean isSqlServer;
public String key_updateProps = "__updateProps__";
public boolean genAble = true;
public ClassPool classPool = ClassPool.getDefault();
public DBFactory() {
}
public Connection getConnection() throws DBException {
return this.pool.getConnection(this);
}
public void saves(List list, String tableName) throws DBException {
if (CollectionUtils.isEmpty(list)) {
return;
}
Connection conn = this.getConnection();
Field[] fields = list.get(0).getClass().getDeclaredFields();
StringBuffer sql = new StringBuffer("insert into " + tableName + "(");
StringBuffer questionMarkSql = new StringBuffer("(");
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
sql.append(field.getName());
questionMarkSql.append("?");
if (i != fields.length - 1) {
sql.append(",");
questionMarkSql.append("?");
}
}
sql.append(") VALUES ");
questionMarkSql.append(")");
for (int i = 0; i < list.size(); i++) {
sql.append(questionMarkSql);
if (i != list.size() - 1) {
sql.append(",");
}
}
try {
//第二部:取得数据库连接对象
PreparedStatement preparedStatement = conn.conn.prepareStatement(sql.toString());
for (int i = 0; i < list.size(); i++) {
Map<String, Object> beanMap = MapUtils.toMap0(list.get(i));
for (int j = 0; j < fields.length; j++) {
Field field = fields[j];
setValue(i * fields.length + j, preparedStatement, field, beanMap);
}
}
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
}
}
void setValue(int index, PreparedStatement preparedStatement, Field field, Map<String, Object> beanMap) throws SQLException {
if (field.getType().getName().equals("java.lang.String")) {
preparedStatement.setString(index, (String) beanMap.get(field.getName()));
} else if (field.getType().getName().equals("java.lang.Integer")) {
preparedStatement.setInt(index, (Integer) beanMap.get(field.getName()));
} else {
preparedStatement.setObject(index, beanMap.get(field.getName()));
}
}
private void saves0(Object[] os, ORMPOJO model, Connection conn, String tableName) throws DBException {
}
}
/**
* TODO
*
* @author gaozhengxing
* @date 2023-04-08 11:55
*/
import javassist.ClassPool;
import org.nobject.common.db.model.ORMPOJO;
import org.nobject.common.exception.DBException;
import org.nobject.common.lang.CollectionUtils;
import org.nobject.common.lang.MapUtils;
import org.nobject.common.log.Logger;
import java.lang.reflect.Field;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class DBFactory {
private Logger logger = Logger.getLogger(DBFactory.class);
protected ConnectionPool pool;
private boolean showsql = true;
private List path = new LinkedList();
public List<ORMPOJO> orms = new LinkedList();
public boolean autoSharding = true;
private boolean optimise = false;
private SQLOptimiser sqlOptimiser;
private boolean validateOnSave = false;
private boolean validateOnUpdate = false;
private ResultHandler resultHandler_mapping = new ResultHandlerMapping();
private ResultHandler resultHandler_orm = new ResultHandlerORM();
private Boolean isSqlServer;
public String key_updateProps = "__updateProps__";
public boolean genAble = true;
public ClassPool classPool = ClassPool.getDefault();
public DBFactory() {
}
public Connection getConnection() throws DBException {
return this.pool.getConnection(this);
}
public void saves(List list, String tableName) throws DBException {
if (CollectionUtils.isEmpty(list)) {
return;
}
Connection conn = this.getConnection();
Field[] fields = list.get(0).getClass().getDeclaredFields();
StringBuffer sql = new StringBuffer("insert into " + tableName + "(");
StringBuffer questionMarkSql = new StringBuffer("(");
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
sql.append(field.getName());
questionMarkSql.append("?");
if (i != fields.length - 1) {
sql.append(",");
questionMarkSql.append("?");
}
}
sql.append(") VALUES ");
questionMarkSql.append(")");
for (int i = 0; i < list.size(); i++) {
sql.append(questionMarkSql);
if (i != list.size() - 1) {
sql.append(",");
}
}
try {
//第二部:取得数据库连接对象
PreparedStatement preparedStatement = conn.conn.prepareStatement(sql.toString());
for (int i = 0; i < list.size(); i++) {
Map<String, Object> beanMap = MapUtils.toMap0(list.get(i));
for (int j = 0; j < fields.length; j++) {
Field field = fields[j];
setValue(i * fields.length + j, preparedStatement, field, beanMap);
}
}
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
}
}
void setValue(int index, PreparedStatement preparedStatement, Field field, Map<String, Object> beanMap) throws SQLException {
if (field.getType().getName().equals("java.lang.String")) {
preparedStatement.setString(index, (String) beanMap.get(field.getName()));
} else if (field.getType().getName().equals("java.lang.Integer")) {
preparedStatement.setInt(index, (Integer) beanMap.get(field.getName()));
} else {
preparedStatement.setObject(index, beanMap.get(field.getName()));
}
}
private void saves0(Object[] os, ORMPOJO model, Connection conn, String tableName) throws DBException {
}
}