package com.cnbbx.mybatis.mysql;
import com.cnbbx.mybatis.definition.*;
import com.cnbbx.mybatis.query.builder.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class MysqlBaseGen {
public static Map<Class, String> typeMappings = new HashMap();
private static Logger logger = LoggerFactory.getLogger(MysqlBaseGen.class);
static {
typeMappings.put(Integer.class, "INT");
typeMappings.put(Long.class, "BIGINT");
typeMappings.put(Float.class, "FLOAT");
typeMappings.put(String.class, "VARCHAR");
typeMappings.put(Double.class, "DOUBLE");
}
public static String createTable(Class clazz) {
try {
EntityDefinition entityDefinition = BaseQuery.resolve(clazz);
String table = entityDefinition.getTable();
StringBuffer stringBuffer = new StringBuffer("DROP TABLE IF EXISTS `");
stringBuffer.append(table);
stringBuffer.append("`; \nCREATE TABLE ");
stringBuffer.append("`");
stringBuffer.append(table);
stringBuffer.append("`");
stringBuffer.append(" ( \n");
for (EntityColumnDefinition entityColumnDefinition : entityDefinition.getColumnDefinitions().values()) {
stringBuffer.append(" `");
stringBuffer.append(entityColumnDefinition.getColumn());
stringBuffer.append("`");
stringBuffer.append(" ");
stringBuffer.append(typeMappings.get(entityColumnDefinition.getType()));
if (String.class.equals(entityColumnDefinition.getType())) {
stringBuffer.append("(100)");
} else {
stringBuffer.append("(");
stringBuffer.append(entityColumnDefinition.getLength());
stringBuffer.append(")");
}
stringBuffer.append(" ");
if (entityColumnDefinition.getId()) {
stringBuffer.append("NOT NULL DEFAULT ''");
} else {
stringBuffer.append("DEFAULT NULL");
}
if (StringUtils.isNotBlank(entityColumnDefinition.getDesc())) {
stringBuffer.append(" COMMENT ");
stringBuffer.append("'");
stringBuffer.append(entityColumnDefinition.getDesc());
stringBuffer.append("'");
}
stringBuffer.append(",\n");
}
stringBuffer.append(" PRIMARY KEY (`" + entityDefinition.getId() + "`)\n)ENGINE=InnoDB DEFAULT CHARSET=utf8;\n\n");
return stringBuffer.toString();
} catch (Exception ex) {
if (logger.isInfoEnabled()) {
logger.error("list", ex);
}
throw new RuntimeException(ex);
}
}
public void orderBy(QueryBuilder queryBuilder, MYSQL sql) {
EntityDefinition entityDefinition = queryBuilder.findEntityDefinition();
List<Order> orders = queryBuilder.getOrders();
if (orders == null || orders.isEmpty()) {
return;
}
// Collections.reverse(orders);
for (Order order : orders) {
EntityColumnDefinition entityColumnDefinition = entityDefinition.getColumnDefinitions().get(order.getPropertyName());
if (entityColumnDefinition == null) {
throw new RuntimeException("this PropertyName " + order.getPropertyName() + " not register");
}
sql.ORDER_BY(entityColumnDefinition.orderBy(order));
}
}
public void where(QueryBuilder queryBuilder, MYSQL sql) {
EntityDefinition entityDefinition = queryBuilder.findEntityDefinition();
List<Query> querySet = queryBuilder.getQueries();
if (querySet == null || querySet.isEmpty()) {
return;
}
for (Query query : querySet) {
EntityColumnDefinition entityColumnDefinition = entityDefinition.getColumnDefinitions().get(query.getProperty());
if (entityColumnDefinition == null) {
throw new RuntimeException("this PropertyName " + query.getProperty() + " not register");
}
if (Where.kv.contains(query.getWhere())) {
sql.WHERE(entityColumnDefinition.where(query, queryBuilder));
}
if (Where.single.contains(query.getWhere())) {
sql.WHERE(entityColumnDefinition.whereSingle(query));
}
if (Where.array.contains(query.getWhere())) {
sql.WHERE(entityColumnDefinition.in(query, queryBuilder));
}
}
}
public void from(QueryBuilder queryBuilder, MYSQL sql) {
EntityDefinition entityDefinition = queryBuilder.findEntityDefinition();
if (entityDefinition == null) {
throw new RuntimeException("entityDefinition not register");
}
if (entityDefinition.table() == "more") {
String[] table = entityDefinition.getMoreTable().split(",");
sql.FROM(table[0]);
if (table.length > 1) {
for (int i = 1; i < table.length; i++) {
sql.INNER_JOIN(table[i]);
}
}
String[] on = entityDefinition.getMoreOn().split(",");
if (on.length >= 1) {
for (int i = 0; i < on.length; i++) {
sql.ON(on[i]);
}
}
} else {
sql.FROM(entityDefinition.table());
if (JoinType.inner.equals(entityDefinition.getJoinType())) {
sql.INNER_JOIN(entityDefinition.joinTable());
sql.ON(entityDefinition.on());
} else if (JoinType.left.equals(entityDefinition.getJoinType())) {
sql.LEFT_JOIN(entityDefinition.joinTable());
sql.ON(entityDefinition.on());
} else if (JoinType.right.equals(entityDefinition.getJoinType())) {
sql.RIGHT_JOIN(entityDefinition.joinTable());
sql.ON(entityDefinition.on());
} else if (JoinType.outer.equals(entityDefinition.getJoinType())) {
sql.OUTER_JOIN(entityDefinition.joinTable());
sql.ON(entityDefinition.on());
} else if (JoinType.right_outer.equals(entityDefinition.getJoinType())) {
sql.RIGHT_OUTER_JOIN(entityDefinition.joinTable());
sql.ON(entityDefinition.on());
} else if (JoinType.left_outer.equals(entityDefinition.getJoinType())) {
sql.LEFT_OUTER_JOIN(entityDefinition.joinTable());
sql.ON(entityDefinition.on());
}
}
}
public void column(QueryBuilder queryBuilder, MYSQL sql) {
EntityDefinition entityDefinition = queryBuilder.findEntityDefinition();
Collection<EntityColumnDefinition> columns = entityDefinition.getColumnDefinitions().values();
if (columns == null || columns.isEmpty()) {
throw new RuntimeException("columns is null");
}
for (EntityColumnDefinition column : columns) {
sql.SELECT(column.column());
}
}
public String list(QueryBuilder queryBuilder) {
try {
MYSQL sql = new MYSQL();
column(queryBuilder, sql);
from(queryBuilder, sql);
where(queryBuilder, sql);
orderBy(queryBuilder, sql);
String sqlStr = sql.toString() + " LIMIT " + queryBuilder.begin() + "," + queryBuilder.getSize();
if (logger.isInfoEnabled()) {
logger.info("MysqlCURDTemple.list.sql:" + sqlStr);
}
return sqlStr;
} catch (Exception ex) {