wiki.xsx.jg.core.OracleService 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.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.util.Set;
/**
* Oracle实现类
*/
public class OracleService implements DatabaseService{
private Database database;
public OracleService(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("DECLARE ")
.append(" num number; ")
.append("BEGIN ")
.append(" SELECT COUNT(1) INTO num FROM user_tables WHERE table_name = ('").append(className.toUpperCase()).append("'); ")
.append(" IF num > 0 THEN ")
.append(" EXECUTE IMMEDIATE 'DROP TABLE ").append(className.toUpperCase()).append("'; ")
.append(" END IF; ")
.append("END;");
return sql.toString();
}
@Override
public String getCreateTableSQL(Table table) {
StringBuilder pk = new StringBuilder();
StringBuilder sql = new StringBuilder();
sql.append("CREATE TABLE ").append(table.getName().toUpperCase()).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.getDefaultValue()!=null){
sql.append(" DEFAULT ").append(column.getDefaultValue());
}
if (column.getIsNotNull()){
sql.append(" NOT NULL");
}
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|VARCHAR|VARCHAR2|LONG")){
return String.class;
}else if(dataTypeName.matches("NUMBER")) {
if (scale==0){
if (length==1){
return Short.class;
}else if (2<=length&&length<=9){
return Integer.class;
}else if (10<=length&&length<=18){
return Long.class;
}else {
return BigDecimal.class;
}
}else if (scale==1){
return Float.class;
}else{
return Double.class;
}
}else if(dataTypeName.matches("DATE|TIME|TIMESTAMP")) {
return Date.class;
}else if(dataTypeName.matches("BLOB")) {
return Blob.class;
}else if(dataTypeName.matches("CLOB")) {
return Clob.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")||
type.getSimpleName().equalsIgnoreCase("Long")||
type.getSimpleName().equalsIgnoreCase("Float")||
type.getSimpleName().equalsIgnoreCase("Double")||
type.getSimpleName().equalsIgnoreCase("BigDecimal")||
type.getSimpleName().equalsIgnoreCase("Boolean")||
type.getSimpleName().equalsIgnoreCase("Byte")||
type.getSimpleName().equalsIgnoreCase("Short")
){
return "NUMBER";
}else if(
type.getSimpleName().equalsIgnoreCase("Date")||
type.getSimpleName().equalsIgnoreCase("Time")
){
return "Date";
}else if(type.getSimpleName().equalsIgnoreCase("Timestamp")){
return "TIMESTAMP";
}else if(type.getSimpleName().equalsIgnoreCase("Blob")){
return "BLOB";
}else if(type.getSimpleName().equalsIgnoreCase("Clob")){
return "CLOB";
}else if(type.getSimpleName().equalsIgnoreCase("byte[]")){
return "RAW";
}else{
return "VARCHAR(255)";
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy