private ISQLitePreparedStatement prepareInsert(SquidDatabase db, Table table, TableStatement.ConflictAlgorithm conflictAlgorithm) { Object[] placeholders = new Object[table.getProperties().length]; Arrays.fill(placeholders, new Object()); Insert insert = Insert.into(table).columns(table.getProperties()) .values(placeholders).onConflict(conflictAlgorithm); CompiledStatement compiled = insert.compile(db.getCompileContext()); ISQLitePreparedStatement statement = db.prepareStatement(compiled.sql); dbStatementTracking.add(statement); return statement; } }
/** * Construct a new Insert statement on the specified {@link Table} or {@link VirtualTable} */ public static Insert into(Table table) { return new Insert(table); }
private void assertValues() { if (!valuesToInsert.isEmpty()) { if (columns.isEmpty()) { throw new IllegalStateException("No columns were specified to insert into."); } assertValueSetSizes(columns.size()); } else if (query != null) { if (columns.size() != query.getFields().size()) { throw new IllegalStateException("Number of properties being selected must match the number of columns " + "specified."); } } else if (!defaultValues) { throw new IllegalStateException("No values to insert were specified."); } }
@Override void appendToSqlBuilder(SqlBuilder builder, boolean forSqlValidation) { assertValues(); builder.sql.append("INSERT "); visitConflictAlgorithm(builder.sql); builder.sql.append("INTO ").append(table.getExpression()).append(" "); visitColumns(builder.sql); if (!valuesToInsert.isEmpty()) { visitValues(builder, forSqlValidation); } else if (query != null) { visitQuery(builder, forSqlValidation); } else { builder.sql.append("DEFAULT VALUES"); } }
private long insertRowLegacy(TableModel item, Table table, TableStatement.ConflictAlgorithm conflictAlgorithm) { ValuesStorage mergedValues = item.getMergedValues(); if (mergedValues.size() == 0) { return -1; } Insert insert = Insert.into(table).fromValues(mergedValues); if (conflictAlgorithm != null) { insert.onConflict(conflictAlgorithm); } return insertInternal(insert); }
@Override public void run() { // insert into testModels (firstName, lastName) select (firstName, lastName, creationDate) from // testModels where luckyNumber = 9; Query query = Query.select(TestModel.FIRST_NAME, TestModel.LAST_NAME, TestModel.BIRTHDAY) .from(TestModel.TABLE) .where(TestModel.LUCKY_NUMBER.eq(9)); Insert insert = Insert.into(TestModel.TABLE).columns(TestModel.FIRST_NAME, TestModel.LAST_NAME) .select(query); insert.compile(database.getCompileContext()); } }, IllegalStateException.class);
database2.beginTransaction(); try { database2.tryExecStatement(Insert.into(TestModel.TABLE).columns(TestModel.FIRST_NAME, TestModel.LAST_NAME) .select(Query.select(TestModel.FIRST_NAME, TestModel.LAST_NAME) .from(TestModel.TABLE.qualifiedFromDatabase(attachedAs)))); database2.setTransactionSuccessful();
@Override public void run() { // insert into testModels select luckyNumber from testModels where luckyNumber = 9; Query query = Query.select(TestModel.FIRST_NAME, TestModel.LAST_NAME, TestModel.BIRTHDAY) .from(TestModel.TABLE) .where(TestModel.LUCKY_NUMBER.eq(9)); Insert insert = Insert.into(TestModel.TABLE).select(query); insert.compile(database.getCompileContext()); } }, IllegalStateException.class);
public void testInsertWithDefaultValues() { // insert into things default values; Insert insert = Insert.into(Thing.TABLE).defaultValues(); CompiledStatement compiled = insert.compile(database.getCompileContext()); verifyCompiledSqlArgs(compiled, 0); int rowsBeforeInsert = database.countAll(Thing.class); assertEquals(3, database.insert(insert)); int rowsAfterInsert = database.countAll(Thing.class); assertEquals(rowsBeforeInsert + 1, rowsAfterInsert); // get the newest Thing newThing = null; SquidCursor<Thing> cursor = null; try { cursor = database.query(Thing.class, Query.select(Thing.PROPERTIES).orderBy(Order.desc(Thing.ID)).limit(1)); if (cursor.moveToFirst()) { newThing = new Thing(cursor); } } finally { if (cursor != null) { cursor.close(); } } assertNotNull(newThing); assertEquals(Thing.DEFAULT_FOO, newThing.getFoo()); assertEquals(Thing.DEFAULT_BAR, newThing.getBar().intValue()); assertEquals(Thing.DEFAULT_IS_ALIVE, newThing.isAlive().booleanValue()); }
/** * Executes an {@link Insert} statement. * <p> * Note: Generally speaking, you should prefer to use {@link #persist(TableModel) persist} or * {@link #createNew(TableModel) createNew} for inserting database rows. This is provided as a convenience in case * there exists a non-ORM case where a more traditional SQL insert statement is required. * * @param insert the statement to execute * @return the row id of the last row inserted on success, 0 on failure */ public long insert(Insert insert) { long result = insertInternal(insert); if (result > TableModel.NO_ID) { int numInserted = insert.getNumRows(); notifyForTable(DataChangedNotifier.DBOperation.INSERT, null, insert.getTable(), numInserted == 1 ? result : TableModel.NO_ID); } return result; }
/** * Execute a SQL {@link com.yahoo.squidb.sql.Insert} statement * * @return the row id of the last row inserted on success, -1 on failure */ private long insertInternal(Insert insert) { CompiledStatement compiled = insert.compile(getCompileContext()); acquireNonExclusiveLock(); try { return getDatabase().executeInsert(compiled.sql, compiled.sqlArgs); } finally { releaseNonExclusiveLock(); } }
@Override void appendToSqlBuilder(SqlBuilder builder, boolean forSqlValidation) { assertValues(); builder.sql.append("INSERT "); visitConflictAlgorithm(builder.sql); builder.sql.append("INTO ").append(table.getExpression()).append(" "); visitColumns(builder.sql); if (!valuesToInsert.isEmpty()) { visitValues(builder, forSqlValidation); } else if (query != null) { visitQuery(builder, forSqlValidation); } else { builder.sql.append("DEFAULT VALUES"); } }
public void testInsertWithQuery() { double pi = Math.PI; Criterion criterion = Thing.QUX.gt(pi); int numThingsMatching = database.count(Thing.class, criterion); // insert into testModels select foo, bar, isAlive from things where qux > 3.1415...; Query query = Query.select(Thing.FOO, Thing.BAR, Thing.IS_ALIVE).from(Thing.TABLE).where(criterion); Insert insert = Insert.into(TestModel.TABLE).columns(TestModel.LAST_NAME, TestModel.LUCKY_NUMBER, TestModel.IS_HAPPY).select(query); CompiledStatement compiled = insert.compile(database.getCompileContext()); verifyCompiledSqlArgs(compiled, 1, pi); int testModelsBeforeInsert = database.countAll(TestModel.class); assertEquals(3, database.insert(insert)); int testModelsAfterInsert = database.countAll(TestModel.class); assertEquals(testModelsBeforeInsert + numThingsMatching, testModelsAfterInsert); }
private long insertRowLegacy(TableModel item, Table table, TableStatement.ConflictAlgorithm conflictAlgorithm) { ValuesStorage mergedValues = item.getMergedValues(); if (mergedValues.size() == 0) { return -1; } Insert insert = Insert.into(table).fromValues(mergedValues); if (conflictAlgorithm != null) { insert.onConflict(conflictAlgorithm); } return insertInternal(insert); }