@Implementation public Cursor query( boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) { String where = selection; if (selection != null && selectionArgs != null) { where = buildWhereClause(selection, selectionArgs); } String sql = SQLiteQueryBuilder.buildQueryString( distinct, table, columns, where, groupBy, having, orderBy, limit); ResultSet resultSet; try { Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); resultSet = statement.executeQuery(sql); } catch (SQLException e) { throw new RuntimeException("SQL exception in query", e); } SQLiteCursor cursor = new SQLiteCursor(null, null, null, null); shadowOf(cursor).setResultSet(resultSet); return cursor; }
@Implementation public int delete(String table, String whereClause, String[] whereArgs) { String sql = buildDeleteString(table, whereClause, whereArgs); try { return connection.prepareStatement(sql).executeUpdate(); } catch (SQLException e) { throw new RuntimeException("SQL exception in delete", e); } }
@Implementation public void close() { if (!isOpen()) { return; } try { connection.close(); connection = null; } catch (SQLException e) { throw new RuntimeException("SQL exception in close", e); } }
@Implementation public void execSQL(String sql) throws android.database.SQLException { if (!isOpen()) { throw new IllegalStateException("database not open"); } // Map 'autoincrement' (sqlite) to 'auto_increment' (h2). String scrubbedSQL = sql.replaceAll("(?i:autoincrement)", "auto_increment"); try { connection.createStatement().execute(scrubbedSQL); } catch (java.sql.SQLException e) { android.database.SQLException ase = new android.database.SQLException(); ase.initCause(e); throw ase; } }
@Implementation public int update(String table, ContentValues values, String whereClause, String[] whereArgs) { SQLStringAndBindings sqlUpdateString = buildUpdateString(table, values, whereClause, whereArgs); try { PreparedStatement statement = connection.prepareStatement(sqlUpdateString.sql); Iterator<Object> columns = sqlUpdateString.columnValues.iterator(); int i = 1; while (columns.hasNext()) { statement.setObject(i++, columns.next()); } return statement.executeUpdate(); } catch (SQLException e) { throw new RuntimeException("SQL exception in update", e); } }
@Implementation public long insert(String table, String nullColumnHack, ContentValues values) { SQLStringAndBindings sqlInsertString = buildInsertString(table, values); try { PreparedStatement statement = connection.prepareStatement(sqlInsertString.sql, Statement.RETURN_GENERATED_KEYS); Iterator<Object> columns = sqlInsertString.columnValues.iterator(); int i = 1; while (columns.hasNext()) { statement.setObject(i++, columns.next()); } statement.executeUpdate(); ResultSet resultSet = statement.getGeneratedKeys(); if (resultSet.first()) { return resultSet.getLong(1); } } catch (SQLException e) { throw new RuntimeException("SQL exception in insert", e); } return -1; }