/** * Create a SQL INSERT string. Returned values are then bound via * JDBC to facilitate various data types. * * @param table table name * @param values column name/value pairs * @param conflictAlgorithm the conflict algorithm to use * @return insert string */ public static SQLStringAndBindings buildInsertString(String table, ContentValues values, int conflictAlgorithm) throws SQLException { StringBuilder sb = new StringBuilder(); sb.append("INSERT "); sb.append(CONFLICT_VALUES[conflictAlgorithm]); sb.append("INTO "); sb.append(table); sb.append(" "); SQLStringAndBindings columnsValueClause = buildColumnValuesClause(values); sb.append(columnsValueClause.sql); sb.append(";"); String sql = DatabaseConfig.getScrubSQL(sb.toString()); return new SQLStringAndBindings(sql, columnsValueClause.columnValues); }
@Implementation public void execSQL(String sql) throws android.database.SQLException { if (!isOpen()) { throw new IllegalStateException("database not open"); } try { String scrubbedSql= DatabaseConfig.getScrubSQL(sql); connection.createStatement().execute(scrubbedSql); } catch (java.sql.SQLException e) { android.database.SQLException ase = new android.database.SQLException(); ase.initCause(e); throw ase; } }
@Implementation public SQLiteStatement compileStatement(String sql) throws SQLException { lock(); String scrubbedSql= DatabaseConfig.getScrubSQL(sql); try { SQLiteStatement stmt = Robolectric.newInstanceOf(SQLiteStatement.class); Robolectric.shadowOf(stmt).init(realSQLiteDatabase, scrubbedSql); return stmt; } catch (Exception e){ throw new RuntimeException(e); } finally { unlock(); } }
@Implementation public void execSQL(String sql, Object[] bindArgs) throws SQLException { if (bindArgs == null) { throw new IllegalArgumentException("Empty bindArgs"); } String scrubbedSql= DatabaseConfig.getScrubSQL(sql); SQLiteStatement statement = null; try { statement =compileStatement(scrubbedSql); if (bindArgs != null) { int numArgs = bindArgs.length; for (int i = 0; i < numArgs; i++) { DatabaseUtils.bindObjectToProgram(statement, i + 1, bindArgs[i]); } } statement.execute(); } catch (SQLiteDatabaseCorruptException e) { throw e; } finally { if (statement != null) { statement.close(); } } }