wiki.xsx.jg.core.SQLServerService 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 and sqlserver.
package wiki.xsx.jg.core;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.Set;
/**
* SQLServer实现类
*/
public class SQLServerService implements DatabaseService{
private Database database;
public SQLServerService(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 getDropTableSQL(String className) {
StringBuilder sql = new StringBuilder();
sql.append("IF EXISTS(SELECT 1 FROM sysobjects WHERE name='").append(className).append("') ")
.append("DROP TABLE ").append(className);
return sql.toString();
}
@Override
public String getCreateTableSQL(Table table) {
StringBuilder pk = new StringBuilder();
StringBuilder sql = new StringBuilder();
sql.append("CREATE TABLE ").append(this.database.getName()).append(".dbo.").append(table.getName()).append("(");
for (Column column : table.getColumnSet()){
sql.append(column.getName()).append(" ");
if (column.getType()!=null){
sql.append(column.getType());
}else{
sql.append(getDataType(column.getJavaType()));
}
if (column.getIsNotNull()){
sql.append(" NOT NULL ");
}
if (column.getDefaultValue()!=null){
sql.append("DEFAULT ").append(column.getDefaultValue());
}
sql.append(",");
if (column.getIsPrimaryKey()){
pk.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|NCHAR|VARCHAR|NVARCHAR|TEXT|NTEXT|UNIQUEIDENTIFIER|XML")){
return String.class;
}else if(dataTypeName.matches("INT")) {
return Integer.class;
}else if(dataTypeName.matches("SMALLINT|TINYINT")) {
return Short.class;
}else if(dataTypeName.matches("BIGINT")) {
return Long.class;
}else if(dataTypeName.matches("REAL")) {
return Float.class;
}else if(dataTypeName.matches("FLOAT")) {
return Double.class;
}else if(dataTypeName.matches("DECIMAL|NUMERIC|SMALLMONEY|MONEY")) {
return BigDecimal.class;
}else if(dataTypeName.matches("DATE|TIME|DATETIME|DATETIME2|SMALLDATETIME|DATETIMEOFFSET")) {
return Date.class;
}else if(dataTypeName.matches("TIMESTAMP|BINARY|IMAGE|VARBINARY")) {
return byte[].class;
}else if(dataTypeName.matches("BIT")) {
return Boolean.class;
}else{
return Object.class;
}
}
@Override
public String getDataType(Class type) {
if (type.getSimpleName().equalsIgnoreCase("String")){
return "NVARCHAR(255)";
}else if(
type.getSimpleName().equalsIgnoreCase("Integer")){
return "INT";
}else if(type.getSimpleName().equalsIgnoreCase("Float")){
return "REAL";
}else if(type.getSimpleName().equalsIgnoreCase("Double")){
return "FLOAT";
}else if(type.getSimpleName().equalsIgnoreCase("Long")){
return "BIGINT";
}else if(type.getSimpleName().equalsIgnoreCase("Short")){
return "SMALLINT";
}else if(type.getSimpleName().equalsIgnoreCase("BigDecimal")){
return "DECIMAL";
}else if(
type.getSimpleName().equalsIgnoreCase("Date")||
type.getSimpleName().equalsIgnoreCase("Time")||
type.getSimpleName().equalsIgnoreCase("Timestamp")
){
return "DATETIME";
}else if(type.getSimpleName().equalsIgnoreCase("Boolean")){
return "BIT";
}else if(type.getSimpleName().equalsIgnoreCase("byte[]")){
return "VARBINARY";
}else{
return "NVARCHAR(255)";
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy