Search in sources :

Example 6 with DatabaseGenerateException

use of org.litepal.exceptions.DatabaseGenerateException in project LitePal by LitePalFramework.

the class LitePalBase method getSupportedFields.

/**
 * Find all the fields in the class. But not each field is supported to add
 * a column to the table. Only the basic data types and String are
 * supported. This method will intercept all the types which are not
 * supported and return a new list of supported fields.
 *
 * @param className
 *            The full name of the class.
 * @return A list of supported fields.
 */
protected List<Field> getSupportedFields(String className) {
    List<Field> fieldList = classFieldsMap.get(className);
    if (fieldList == null) {
        List<Field> supportedFields = new ArrayList<Field>();
        Class<?> clazz;
        try {
            clazz = Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw new DatabaseGenerateException(DatabaseGenerateException.CLASS_NOT_FOUND + className);
        }
        recursiveSupportedFields(clazz, supportedFields);
        classFieldsMap.put(className, supportedFields);
        return supportedFields;
    }
    return fieldList;
}
Also used : DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList)

Example 7 with DatabaseGenerateException

use of org.litepal.exceptions.DatabaseGenerateException in project LitePal by LitePalFramework.

the class LitePalBase method getSupportedGenericFields.

/**
 * Find all supported generic fields in the class. Supporting rule is in {@link BaseUtility#isGenericTypeSupported(String)}.
 * @param className
 *           The full name of the class.
 * @return A list of supported generic fields.
 */
protected List<Field> getSupportedGenericFields(String className) {
    List<Field> genericFieldList = classGenericFieldsMap.get(className);
    if (genericFieldList == null) {
        List<Field> supportedGenericFields = new ArrayList<Field>();
        Class<?> clazz;
        try {
            clazz = Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw new DatabaseGenerateException(DatabaseGenerateException.CLASS_NOT_FOUND + className);
        }
        recursiveSupportedGenericFields(clazz, supportedGenericFields);
        classGenericFieldsMap.put(className, supportedGenericFields);
        return supportedGenericFields;
    }
    return genericFieldList;
}
Also used : DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList)

Example 8 with DatabaseGenerateException

use of org.litepal.exceptions.DatabaseGenerateException in project LitePal by LitePalFramework.

the class DataHandler method getEmptyModel.

/**
 * Create an empty instance of baseObj if it hasn't created one yet. If
 * there's already an empty model existed in {@link #tempEmptyModel}, no
 * need to create a new one.
 *
 * @param baseObj
 *            Current model to update.
 * @return An empty instance of baseObj.
 */
protected DataSupport getEmptyModel(DataSupport baseObj) {
    if (tempEmptyModel != null) {
        return tempEmptyModel;
    }
    String className = null;
    try {
        className = baseObj.getClassName();
        Class<?> modelClass = Class.forName(className);
        tempEmptyModel = (DataSupport) modelClass.newInstance();
        return tempEmptyModel;
    } catch (ClassNotFoundException e) {
        throw new DatabaseGenerateException(DatabaseGenerateException.CLASS_NOT_FOUND + className);
    } catch (InstantiationException e) {
        throw new DataSupportException(className + DataSupportException.INSTANTIATION_EXCEPTION, e);
    } catch (Exception e) {
        throw new DataSupportException(e.getMessage(), e);
    }
}
Also used : DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) DataSupportException(org.litepal.exceptions.DataSupportException) DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) DataSupportException(org.litepal.exceptions.DataSupportException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 9 with DatabaseGenerateException

use of org.litepal.exceptions.DatabaseGenerateException in project LitePal by LitePalFramework.

the class Connector method buildConnection.

/**
 * Build a connection to the database. This progress will analysis the
 * litepal.xml file, and will check if the fields in LitePalAttr are valid,
 * and it will open a SQLiteOpenHelper to decide to create tables or update
 * tables or doing nothing depends on the version attributes.
 *
 * After all the stuffs above are finished. This method will return a
 * LitePalHelper object.Notes this method could throw a lot of exceptions.
 *
 * @return LitePalHelper object.
 *
 * @throws org.litepal.exceptions.InvalidAttributesException
 */
private static LitePalOpenHelper buildConnection() {
    LitePalAttr litePalAttr = LitePalAttr.getInstance();
    litePalAttr.checkSelfValid();
    if (mLitePalHelper == null) {
        String dbName = litePalAttr.getDbName();
        if ("external".equalsIgnoreCase(litePalAttr.getStorage())) {
            dbName = LitePalApplication.getContext().getExternalFilesDir("") + "/databases/" + dbName;
        } else if (!"internal".equalsIgnoreCase(litePalAttr.getStorage()) && !TextUtils.isEmpty(litePalAttr.getStorage())) {
            // internal or empty means internal storage, neither or them means sdcard storage
            String dbPath = Environment.getExternalStorageDirectory().getPath() + "/" + litePalAttr.getStorage();
            dbPath = dbPath.replace("//", "/");
            if (BaseUtility.isClassAndMethodExist("android.support.v4.content.ContextCompat", "checkSelfPermission") && ContextCompat.checkSelfPermission(LitePalApplication.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                throw new DatabaseGenerateException(String.format(DatabaseGenerateException.EXTERNAL_STORAGE_PERMISSION_DENIED, dbPath));
            }
            File path = new File(dbPath);
            if (!path.exists()) {
                path.mkdirs();
            }
            dbName = dbPath + "/" + dbName;
        }
        mLitePalHelper = new LitePalOpenHelper(dbName, litePalAttr.getVersion());
    }
    return mLitePalHelper;
}
Also used : DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) LitePalAttr(org.litepal.parser.LitePalAttr) File(java.io.File)

Example 10 with DatabaseGenerateException

use of org.litepal.exceptions.DatabaseGenerateException 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

DatabaseGenerateException (org.litepal.exceptions.DatabaseGenerateException)10 ArrayList (java.util.ArrayList)5 Cursor (android.database.Cursor)3 Field (java.lang.reflect.Field)3 ColumnModel (org.litepal.tablemanager.model.ColumnModel)2 SQLException (android.database.SQLException)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Column (org.litepal.annotation.Column)1 DataSupportException (org.litepal.exceptions.DataSupportException)1 LitePalAttr (org.litepal.parser.LitePalAttr)1 TableModel (org.litepal.tablemanager.model.TableModel)1