wiki.xsx.jg.core.MysqlService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JG-All Show documentation
Show all versions of JG-All Show documentation
a generator, from the database tables convert to the Java classes or from the Java classes convert to the database tables.
support mysql, oracle, sqlserver and postgresql.
package wiki.xsx.jg.core;
import java.math.BigDecimal;
import java.sql.Time;
import java.util.Date;
import java.util.Set;
/**
* mysql实现类
*/
public class MysqlService implements DatabaseService {
private Database database;
public MysqlService(Database database) {
this.database = database;
}
@Override
public void setTableInfo() throws Exception {
CommonService.setTableInfo(this);
}
@Override
public void createTables(Set tableSet) throws Exception {
CommonService.createTables(tableSet, this);
}
@Override
public Database getDatabase() {
return this.database;
}
@Override
public String testQuery() {
return "SELECT 1";
}
@Override
public String getDropTableSQL(String className) {
StringBuilder sql = new StringBuilder();
sql.append("DROP TABLE IF EXISTS `").append(this.database.getName()).append("`.").append(className);
return sql.toString();
}
@Override
public String getCreateTableSQL(Table table) {
StringBuilder pk = new StringBuilder();
StringBuilder sql = new StringBuilder();
sql.append("CREATE TABLE ").append(table.getName()).append("(");
for (Column column : table.getColumnSet()){
sql.append("`").append(column.getName()).append("` ")
.append(getDataType(column.getJavaType()));
if (column.getIsNotNull()){
sql.append(" NOT NULL,");
}else{
sql.append(",");
}
if (column.getIsPrimaryKey()){
pk.append("`").append(column.getName()).append("`,");
}
}
if (pk.length()>0){
sql.append("PRIMARY KEY (").append(pk.substring(0, pk.length()-1)).append(")");
}else{
sql = sql.deleteCharAt(sql.length()-1);
}
sql.append(")");
return sql.toString();
}
@Override
public Class> getTypeClass(String dataTypeName, int length, int scale) {
if (dataTypeName.matches("CHAR|VARCHAR")){
return String.class;
}else if(dataTypeName.matches("TINYINT|SMALLINT|MEDIUMINT|INT|INTEGER|YEAR")) {
return Integer.class;
}else if(dataTypeName.matches("BIGINT")) {
return Long.class;
}else if(dataTypeName.matches("FLOAT")) {
return Float.class;
}else if(dataTypeName.matches("DOUBLE")) {
return Double.class;
}else if(dataTypeName.matches("DECIMAL")) {
return BigDecimal.class;
}else if(dataTypeName.matches("DATE|DATETIME|TIMESTAMP")) {
return Date.class;
}else if(dataTypeName.matches("TIME")) {
return Time.class;
}else if(dataTypeName.matches("BINARY|VARBINARY|TINYBLOB|BLOB|MEDIUMBLOB|LONGBLOB")) {
return byte[].class;
}else{
return Object.class;
}
}
@Override
public String getDataType(Class> type) {
if (type.getSimpleName().equalsIgnoreCase("String")){
return "VARCHAR(255)";
}else if(type.getSimpleName().equalsIgnoreCase("Integer")){
return "INT";
}else if(type.getSimpleName().equalsIgnoreCase("Long")){
return "BIGINT";
}else if(type.getSimpleName().equalsIgnoreCase("Float")){
return "FLOAT";
}else if(type.getSimpleName().equalsIgnoreCase("Double")){
return "DOUBLE";
}else if(type.getSimpleName().equalsIgnoreCase("BigDecimal")){
return "DECIMAL";
}else if(type.getSimpleName().equalsIgnoreCase("Date")){
return "TIMESTAMP";
}else if(type.getSimpleName().equalsIgnoreCase("Time")){
return "TIME";
}else if(type.getSimpleName().equalsIgnoreCase("byte[]")){
return "BLOB";
}else{
return "VARCHAR(255)";
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy