Search in sources :

Example 1 with DatabaseGenerateException

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

the class AssociationCreator method addForeignKeyColumn.

/**
	 * This method is used to add many to one association or one to one
	 * association on tables. It will automatically build a SQL to add foreign
	 * key to a table. If the passed in table name or associated table name
	 * doesn't exist, it will throw an exception.
	 * 
	 * @param tableName
	 *            The table name.
	 * @param associatedTableName
	 *            The associated table name.
	 * @param tableHoldsForeignKey
	 *            The table which holds the foreign key.
	 * @param db
	 *            Instance of SQLiteDatabase.
	 * 
	 * @throws org.litepal.exceptions.DatabaseGenerateException
	 */
protected void addForeignKeyColumn(String tableName, String associatedTableName, String tableHoldsForeignKey, SQLiteDatabase db) {
    if (DBUtility.isTableExists(tableName, db)) {
        if (DBUtility.isTableExists(associatedTableName, db)) {
            String foreignKeyColumn = null;
            if (tableName.equals(tableHoldsForeignKey)) {
                foreignKeyColumn = getForeignKeyColumnName(associatedTableName);
            } else if (associatedTableName.equals(tableHoldsForeignKey)) {
                foreignKeyColumn = getForeignKeyColumnName(tableName);
            }
            if (!DBUtility.isColumnExists(foreignKeyColumn, tableHoldsForeignKey, db)) {
                ColumnModel columnModel = new ColumnModel();
                columnModel.setColumnName(foreignKeyColumn);
                columnModel.setColumnType("integer");
                List<String> sqls = new ArrayList<String>();
                sqls.add(generateAddColumnSQL(tableHoldsForeignKey, columnModel));
                execute(sqls, db);
            } else {
                LogUtil.d(TAG, "column " + foreignKeyColumn + " is already exist, no need to add one");
            }
        } else {
            throw new DatabaseGenerateException(DatabaseGenerateException.TABLE_DOES_NOT_EXIST + associatedTableName);
        }
    } else {
        throw new DatabaseGenerateException(DatabaseGenerateException.TABLE_DOES_NOT_EXIST + tableName);
    }
}
Also used : DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) ArrayList(java.util.ArrayList) ColumnModel(org.litepal.tablemanager.model.ColumnModel)

Example 2 with DatabaseGenerateException

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

the class Generator method execute.

/**
	 * Use the parameter SQLiteDatabase to execute the passing SQLs. Subclasses
	 * can add their own logic when do the executing job by overriding this
	 * method.
	 * 
	 * @param sqls
	 *            SQLs that want to execute.
	 * @param db
	 *            instance of SQLiteDatabase
	 * 
	 * @throws org.litepal.exceptions.DatabaseGenerateException
	 */
protected void execute(List<String> sqls, SQLiteDatabase db) {
    String throwSQL = "";
    try {
        if (sqls != null && !sqls.isEmpty()) {
            for (String sql : sqls) {
                if (!TextUtils.isEmpty(sql)) {
                    throwSQL = BaseUtility.changeCase(sql);
                    db.execSQL(throwSQL);
                }
            }
        }
    } catch (SQLException e) {
        throw new DatabaseGenerateException(DatabaseGenerateException.SQL_ERROR + throwSQL);
    }
}
Also used : DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) SQLException(android.database.SQLException)

Example 3 with DatabaseGenerateException

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

the class DBUtility method findUniqueColumns.

/**
     * Find all unique column names of specified table.
     * @param tableName
     *          The table to find unique columns.
     * @param db
     *          Instance of SQLiteDatabase.
     * @return A list with all unique column names of specified table.
     */
public static List<String> findUniqueColumns(String tableName, SQLiteDatabase db) {
    List<String> columns = new ArrayList<String>();
    Cursor cursor = null;
    Cursor innerCursor = null;
    try {
        cursor = db.rawQuery("pragma index_list(" + tableName + ")", null);
        if (cursor.moveToFirst()) {
            do {
                int unique = cursor.getInt(cursor.getColumnIndexOrThrow("unique"));
                if (unique == 1) {
                    String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
                    innerCursor = db.rawQuery("pragma index_info(" + name + ")", null);
                    if (innerCursor.moveToFirst()) {
                        String columnName = innerCursor.getString(innerCursor.getColumnIndexOrThrow("name"));
                        columns.add(columnName);
                    }
                }
            } while (cursor.moveToNext());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new DatabaseGenerateException(e.getMessage());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
        if (innerCursor != null) {
            innerCursor.close();
        }
    }
    return columns;
}
Also used : DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException)

Example 4 with DatabaseGenerateException

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

the class DBUtility method findAllTableNames.

/**
	 * Find all table names in the database. If there's some wrong happens when
	 * finding tables, it will throw exceptions.
	 * 
	 * @param db
	 *            Instance of SQLiteDatabase.
	 * @return A list with all table names.
	 * @throws org.litepal.exceptions.DatabaseGenerateException
	 */
public static List<String> findAllTableNames(SQLiteDatabase db) {
    List<String> tableNames = new ArrayList<String>();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("select * from sqlite_master where type = ?", new String[] { "table" });
        if (cursor.moveToFirst()) {
            do {
                String tableName = cursor.getString(cursor.getColumnIndexOrThrow("tbl_name"));
                if (!tableNames.contains(tableName)) {
                    tableNames.add(tableName);
                }
            } while (cursor.moveToNext());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new DatabaseGenerateException(e.getMessage());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return tableNames;
}
Also used : DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) DatabaseGenerateException(org.litepal.exceptions.DatabaseGenerateException)

Example 5 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)

Aggregations

DatabaseGenerateException (org.litepal.exceptions.DatabaseGenerateException)9 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 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 DataSupportException (org.litepal.exceptions.DataSupportException)1 TableModel (org.litepal.tablemanager.model.TableModel)1