/** * Execute SQL. * * @param database * the database * @param fileInputStream * the file input stream */ public static void executeSQL(final SQLiteDatabase database, InputStream fileInputStream) { List<String> commands = readSQLFromFile(fileInputStream); executeSQL(database, commands); }
/** * Verify schema. * * @param database * the database * @param inputStream * the input stream */ public static void verifySchema(SQLiteDatabase database, InputStream inputStream) { List<String> ddl = extractCommands(database, inputStream); verifySchemaInternal(database, ddl); }
actualSql.addAll(SQLiteTestUtils.getAllTables(database).values()); actualSql.addAll(SQLiteTestUtils.getAllIndexes(database).values());
/** * Verify schema. * * @param database * the database * @param context * the context * @param rawId * the raw id */ public static void verifySchema(SQLiteDatabase database, Context context, int rawId) { verifySchema(database, context.getResources().openRawResource(rawId)); }
/** * Execute SQL. * * @param database * the database * @param commands * the commands */ public static void executeSQL(final SQLiteDatabase database, List<String> commands) { for (String command : commands) { executeSQL(database, command); } // commands.forEach(command -> { // executeSQL(database, command); // }); }
/** * Read SQL from file. * * @param fileName * the file name * @return the list */ public static List<String> readSQLFromFile(String fileName) { try { return readSQLFromFile(new FileInputStream(fileName)); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } }
/** * Add to all schema's table a specifix prefix. * * @param db * the db * @param prefix * the prefix */ public static void renameAllTablesWithPrefix(SQLiteDatabase db, final String prefix) { Logger.info("MASSIVE TABLE RENAME OPERATION: ADD PREFIX " + prefix); query(db, null, QueryType.TABLE, new OnResultListener() { @Override public void onRow(SQLiteDatabase db, String name, String sql) { sql = String.format("ALTER TABLE %s RENAME TO %s%s;", name, prefix, name); Logger.info(sql); db.execSQL(sql); } }); }
/** * Verify schema. * * @param <H> * the generic type * @param dataSource * the data source * @param inputStream * the input stream */ public static <H extends AbstractDataSource> void verifySchema(H dataSource, InputStream inputStream) { verifySchema(dataSource.openWritableDatabase(), inputStream); }
@Override public void onCreate(SQLiteDatabase database) { if (firstSchemaDefinitionInputStream != null) { Logger.info("Load DDL from input stream"); SQLiteTestUtils.executeSQL(database, firstSchemaDefinitionInputStream); } else { Logger.info("Load DDL from resourceId"); SQLiteTestUtils.executeSQL(database, context, firstSchemaDefinitionResourceId); } } };
/** * Execute SQL from file. * * @param database the database * @param fileName the sql definition file */ public static void executeSQLFromFile(SQLiteDatabase database, String fileName) { List<String> executionList = readSQLFromFile(fileName); for (String item : executionList) { Logger.info(item); database.execSQL(item); } }
/** * Drop all entity of particular type (table or index). If prefix is * specified, the drop operation is applied only to entity with prefix. * * @param db * the db * @param type * the type * @param prefix * the prefix */ private static void drop(SQLiteDatabase db, final QueryType type, String prefix) { String dropSQL = StringUtils.hasText(prefix) ? "name like '" + prefix + "' || '%'" : null; query(db, dropSQL, type, new OnResultListener() { @Override public void onRow(SQLiteDatabase db, String name, String sql) { String drop = "DROP " + type.toString().toUpperCase() + " " + name; Logger.info(drop); db.execSQL(drop); } }); }
/** * Verify schema. * * @param <H> * the generic type * @param dataSource * the data source * @param context * the context * @param rawId * the raw id */ public static <H extends AbstractDataSource> void verifySchema(H dataSource, Context context, int rawId) { verifySchema(dataSource.openWritableDatabase(), context, rawId); }
/** * Execute SQL contained in raw resource. * * @param database * the database * @param context * the context * @param rawResourceId * the raw resource id */ public static void executeSQL(final SQLiteDatabase database, Context context, int rawResourceId) { String[] c = IOUtils.readTextFile(context, rawResourceId).split(";"); List<String> commands = Arrays.asList(c); executeSQL(database, commands); }
/** * Drop all table with specific prefix. * * @param db * the db * @param prefix * the prefix */ public static void dropTablesWithPrefix(SQLiteDatabase db, String prefix) { Logger.info("MASSIVE TABLE DROP OPERATION%s", StringUtils.ifNotEmptyAppend(prefix, " WITH PREFIX ")); drop(db, QueryType.TABLE, prefix); }
/** * Allows to update database version to <i>version</i>. This method allows to specify the destination version schema and compare it with schema resulting by version update * applied. * * @param version * the version * @param schemaDefinitionInputStream * the schema definition input stream * @return the SQ lite update test database */ public SQLiteTestDatabase updateAndVerify(int version, final InputStream schemaDefinitionInputStream) { sqlite = new SQLiteOpenHelper(context, TEST_DATABASE, factory, version, errorHandler) { @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { List<SQLiteUpdateTask> task = findTask(oldVersion, newVersion); for (SQLiteUpdateTask item : task) { item.execute(db, oldVersion, oldVersion + 1); oldVersion++; } } @Override public void onCreate(SQLiteDatabase db) { throw (new KriptonRuntimeException("Unsupported situation")); } }; SQLiteTestUtils.verifySchema(sqlite.getWritableDatabase(), schemaDefinitionInputStream); return this; }