<3>createTableIfNotExist

来源:互联网 发布:中国人民大学网络登录 编辑:程序博客网 时间:2024/06/07 05:41

1. createTableIfNotExit


public classDbUtils

 

public voidcreateTableIfNotExist(Class<?> entityType)throws DbException {
    if(!tableIsExist(entityType)) {
        SqlInfo sqlInfo = SqlInfoBuilder.buildCreateTableSqlInfo(this,entityType);
        execNonQuery(sqlInfo);
        String execAfterTableCreated = TableUtils.getExecAfterTableCreated(entityType);
        if (!TextUtils.isEmpty(execAfterTableCreated)) {
            execNonQuery(execAfterTableCreated);
        }
    }
}

 

① 先判断表是否存在

② 获取创建表的sql语句,执行语句

③ 类似钩子的操作,解析@Table注释中execAfterTableCreated内容,成为sql

并在表创建成功后执行,如果,sql为空,则跳过此步骤

 

2. 获取创建表的sql语句

public classSqlInfoBuilder

 

public staticSqlInfo buildCreateTableSqlInfo(DbUtils db,Class<?> entityType) throwsDbException {
    Table table = Table.get(db,entityType);
    Id id = table.id;

    StringBuffer sqlBuffer =new StringBuffer();
    sqlBuffer.append("CREATE TABLE IF NOT EXISTS ");
    sqlBuffer.append(table.tableName);
    sqlBuffer.append(" ( ");

    if (id.isAutoIncrement()) {
        sqlBuffer.append("\"").append(id.getColumnName()).append("\"  ").append("INTEGER PRIMARY KEY AUTOINCREMENT,");
    }else {
        sqlBuffer.append("\"").append(id.getColumnName()).append("\"  ").append(id.getColumnDbType()).append(" PRIMARY KEY,");
    }

    Collection<Column> columns = table.columnMap.values();
    for (Column column : columns) {
        if(column instanceofFinder) {
            continue;
        }
        sqlBuffer.append("\"").append(column.getColumnName()).append("\"  ");
        sqlBuffer.append(column.getColumnDbType());
        if (ColumnUtils.isUnique(column.getColumnField())) {
            sqlBuffer.append(" UNIQUE");
        }
        if(ColumnUtils.isNotNull(column.getColumnField())) {
            sqlBuffer.append(" NOT NULL");
        }
        String check = ColumnUtils.getCheck(column.getColumnField());
        if (check !=null) {
            sqlBuffer.append(" CHECK(").append(check).append(")");
        }
        sqlBuffer.append(",");
    }

    sqlBuffer.deleteCharAt(sqlBuffer.length() -1);
    sqlBuffer.append(" )");
    return new SqlInfo(sqlBuffer.toString());
}

 

① table对象的获取,tableIsExist()函数中有提及过程,大体就是获取主键@id以及@column数组信息,放入table对象

② 下面就是一个字符串拼接的活动了(\”java转义字符==双引号字符)

+ "CREATE TABLE IF NOT EXISTS "

 

+ entityclassName

 

+ " ( "

 

+ "\”" + @id(主键名称)  + "\”"  + "INTEGER PRIMARY KEY AUTOINCREMENT,"(默认自增)

 

For。。。循环

+ @clumn(列名称)+(列类型)+" UNIQUE" + " NOT NULL" + " CHECK(" + ")" + ","

结束循环

 

+ 删掉最后一个","

 

+ " )"

③ 打包成SqlInfo对象,其实就是把生成的sqlBuffer存入SqlInfo对象的sql变量中

 

1) 默认自增(isAutoIncrement

public classId extendsColumn

 

public booleanisAutoIncrement() {
    if(!isAutoIncrementChecked) {
        isAutoIncrementChecked= true;
        isAutoIncrement= columnField.getAnnotation(NoAutoIncrement.class) == null
                &&AUTO_INCREMENT_TYPES.contains(columnFieldClassName);
    }
    returnisAutoIncrement;
}

 

private static finalHashSet<String> INTEGER_TYPES= newHashSet<String>(2);
private static final HashSet<String>AUTO_INCREMENT_TYPES =new HashSet<String>(4);

static {
    INTEGER_TYPES.add(int.class.getName());
    INTEGER_TYPES.add(Integer.class.getName());

    AUTO_INCREMENT_TYPES.addAll(INTEGER_TYPES);
    AUTO_INCREMENT_TYPES.add(long.class.getName());
    AUTO_INCREMENT_TYPES.add(Long.class.getName());
}

① isAutoIncrementChecked默认false,所以进入if语句

② 可以看见isAutoIncrementChecked = true

③ 接下来就是真正的判断条件:

True = 没有加入注释NoAutoIncrement并且 是int或者long类型 会自增

False = 除此之外

 

1) 列类型

回到初始化Column哪里

 Column(Class<?> entityType,Field field) {
    this.columnField= field;
    this.columnConverter = ColumnConverterFactory.getColumnConverter(field.getType());
    this.columnName= ColumnUtils.getColumnNameByField(field);
    if (this.columnConverter!= null) {
        this.defaultValue= this.columnConverter.getFieldValue(ColumnUtils.getColumnDefaultValue(field));
    }else {
        this.defaultValue= null;
    }
    this.getMethod= ColumnUtils.getColumnGetMethod(entityType,field);
    this.setMethod= ColumnUtils.getColumnSetMethod(entityType,field);
}

 

public staticColumnConverter getColumnConverter(Class columnType) {
    if(columnType_columnConverter_map.containsKey(columnType.getName())) {
        returncolumnType_columnConverter_map.get(columnType.getName());
    }else if (ColumnConverter.class.isAssignableFrom(columnType)) {
        try{
            ColumnConverter columnConverter = (ColumnConverter) columnType.newInstance();
            if (columnConverter !=null) {
                columnType_columnConverter_map.put(columnType.getName(),columnConverter);
            }
            returncolumnConverter;
        }catch (Throwable e) {
        }
    }
    return null;
}

 

private static finalConcurrentHashMap<String,ColumnConverter> columnType_columnConverter_map;

static {
    columnType_columnConverter_map= newConcurrentHashMap<String,ColumnConverter>();

    BooleanColumnConverter booleanColumnConverter =new BooleanColumnConverter();
    columnType_columnConverter_map.put(boolean.class.getName(),booleanColumnConverter);
    columnType_columnConverter_map.put(Boolean.class.getName(),booleanColumnConverter);

    ByteArrayColumnConverter byteArrayColumnConverter =new ByteArrayColumnConverter();
    columnType_columnConverter_map.put(byte[].class.getName(),byteArrayColumnConverter);

    ByteColumnConverter byteColumnConverter =new ByteColumnConverter();
    columnType_columnConverter_map.put(byte.class.getName(),byteColumnConverter);
    columnType_columnConverter_map.put(Byte.class.getName(),byteColumnConverter);

    CharColumnConverter charColumnConverter =new CharColumnConverter();
    columnType_columnConverter_map.put(char.class.getName(),charColumnConverter);
    columnType_columnConverter_map.put(Character.class.getName(),charColumnConverter);

    DateColumnConverter dateColumnConverter =new DateColumnConverter();
    columnType_columnConverter_map.put(Date.class.getName(),dateColumnConverter);

    DoubleColumnConverter doubleColumnConverter =new DoubleColumnConverter();
    columnType_columnConverter_map.put(double.class.getName(),doubleColumnConverter);
    columnType_columnConverter_map.put(Double.class.getName(),doubleColumnConverter);

    FloatColumnConverter floatColumnConverter =new FloatColumnConverter();
    columnType_columnConverter_map.put(float.class.getName(),floatColumnConverter);
    columnType_columnConverter_map.put(Float.class.getName(),floatColumnConverter);

    IntegerColumnConverter integerColumnConverter =new IntegerColumnConverter();
    columnType_columnConverter_map.put(int.class.getName(),integerColumnConverter);
    columnType_columnConverter_map.put(Integer.class.getName(),integerColumnConverter);

    LongColumnConverter longColumnConverter =new LongColumnConverter();
    columnType_columnConverter_map.put(long.class.getName(),longColumnConverter);
    columnType_columnConverter_map.put(Long.class.getName(),longColumnConverter);

    ShortColumnConverter shortColumnConverter =new ShortColumnConverter();
    columnType_columnConverter_map.put(short.class.getName(),shortColumnConverter);
    columnType_columnConverter_map.put(Short.class.getName(),shortColumnConverter);

    SqlDateColumnConverter sqlDateColumnConverter =new SqlDateColumnConverter();
    columnType_columnConverter_map.put(java.sql.Date.class.getName(),sqlDateColumnConverter);

    StringColumnConverter stringColumnConverter =new StringColumnConverter();
    columnType_columnConverter_map.put(String.class.getName(),stringColumnConverter);
}

 

 

① 可以看到最终是从columnType_columnConverter_map缓存中,获取一个IntegerColumnConverter对象(这里以int类型举例)

 

sqlBuffer.append(column.getColumnDbType());

 

public ColumnDbType getColumnDbType() {
    returncolumnConverter.getColumnDbType();
}

 

public ColumnDbType getColumnDbType() {
    returnColumnDbType.INTEGER;
}

 

public enumColumnDbType {

    INTEGER("INTEGER"),REAL("REAL"),TEXT("TEXT"),BLOB("BLOB");

    private Stringvalue;

    ColumnDbType(String value) {
        this.value= value;
    }

    @Override
    publicString toString() {
        returnvalue;
    }
}

② 看到最终拿到的是一个枚举,值为"INTEGER"

 

3. Database执行sql语句创建表

public voidcreateTableIfNotExist(Class<?> entityType)throws DbException {
    if(!tableIsExist(entityType)) {
        SqlInfo sqlInfo = SqlInfoBuilder.buildCreateTableSqlInfo(this,entityType);
        execNonQuery(sqlInfo);
        String execAfterTableCreated = TableUtils.getExecAfterTableCreated(entityType);
        if (!TextUtils.isEmpty(execAfterTableCreated)) {
            execNonQuery(execAfterTableCreated);
        }
    }
}

public voidexecNonQuery(SqlInfo sqlInfo)throws DbException {
    debugSql(sqlInfo.getSql());
    try {
        if(sqlInfo.getBindArgs() != null) {
            database.execSQL(sqlInfo.getSql(),sqlInfo.getBindArgsAsArray());
        }else {
            database.execSQL(sqlInfo.getSql());
        }
    } catch(Throwable e) {
        throw newDbException(e);
    }
}

 

Ok,一个简单的语句执行,创建完毕

0 0