Search in sources :

Example 6 with TableModel

use of org.litepal.tablemanager.model.TableModel in project LitePal by LitePalFramework.

the class DBUtility method findPragmaTableInfo.

/**
 * Look from the database to find a table named same as the table name in
 * table model. Then iterate the columns and types of this table to create a
 * new instance of table model. If there's no such a table in the database,
 * then throw DatabaseGenerateException.
 *
 * @param tableName
 *            Table name.
 * @param db
 *            Instance of SQLiteDatabase.
 * @return A table model object with values from database table.
 * @throws org.litepal.exceptions.DatabaseGenerateException
 */
public static TableModel findPragmaTableInfo(String tableName, SQLiteDatabase db) {
    if (isTableExists(tableName, db)) {
        List<String> uniqueColumns = findUniqueColumns(tableName, db);
        TableModel tableModelDB = new TableModel();
        tableModelDB.setTableName(tableName);
        String checkingColumnSQL = "pragma table_info(" + tableName + ")";
        Cursor cursor = null;
        try {
            cursor = db.rawQuery(checkingColumnSQL, null);
            if (cursor.moveToFirst()) {
                do {
                    ColumnModel columnModel = new ColumnModel();
                    String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
                    String type = cursor.getString(cursor.getColumnIndexOrThrow("type"));
                    boolean nullable = cursor.getInt(cursor.getColumnIndexOrThrow("notnull")) != 1;
                    boolean unique = uniqueColumns.contains(name);
                    String defaultValue = cursor.getString(cursor.getColumnIndexOrThrow("dflt_value"));
                    columnModel.setColumnName(name);
                    columnModel.setColumnType(type);
                    columnModel.setNullable(nullable);
                    columnModel.setUnique(unique);
                    if (defaultValue != null) {
                        defaultValue = defaultValue.replace("'", "");
                    } else {
                        defaultValue = "";
                    }
                    columnModel.setDefaultValue(defaultValue);
                    tableModelDB.addColumnModel(columnModel);
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new DatabaseGenerateException(e.getMessage());
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return tableModelDB;
    } else {
        throw new DatabaseGenerateException(DatabaseGenerateException.TABLE_DOES_NOT_EXIST_WHEN_EXECUTING + tableName);
    }
}
Also used : DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) ColumnModel(org.litepal.tablemanager.model.ColumnModel) Cursor(android.database.Cursor) TableModel(org.litepal.tablemanager.model.TableModel) DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException)

Aggregations

TableModel (org.litepal.tablemanager.model.TableModel)6 ColumnModel (org.litepal.tablemanager.model.ColumnModel)3 Cursor (android.database.Cursor)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 DatabaseGenerateException (org.litepal.exceptions.DatabaseGenerateException)1