private long insertProvider() { return querydslSupport.execute((connection, configuration) -> { QOAuth2Provider qoAuth2Provider = QOAuth2Provider.oAuth2Provider; return new SQLInsertClause(connection, configuration, qoAuth2Provider) .set(qoAuth2Provider.providerName, providerName) .executeWithKey(qoAuth2Provider.oauth2ProviderId); }); }
@Test public void test() throws SQLException { stmt.execute("drop table GENERATED_KEYS if exists"); stmt.execute("create table GENERATED_KEYS(" + "ID int AUTO_INCREMENT PRIMARY KEY, " + "NAME varchar(30))"); QGeneratedKeysEntity entity = new QGeneratedKeysEntity("entity"); SQLInsertClause insertClause = new SQLInsertClause(conn, new H2Templates(), entity); ResultSet rs = insertClause.set(entity.name, "Hello").executeWithKeys(); ResultSetMetaData md = rs.getMetaData(); System.out.println(md.getColumnName(1)); assertTrue(rs.next()); assertEquals(1, rs.getInt(1)); assertFalse(rs.next()); insertClause = new SQLInsertClause(conn, new H2Templates(), entity); rs = insertClause.set(entity.name, "World").executeWithKeys(); assertTrue(rs.next()); assertEquals(2, rs.getInt(1)); assertFalse(rs.next()); insertClause = new SQLInsertClause(conn, new H2Templates(), entity); assertEquals(3, insertClause.set(entity.name, "World").executeWithKey(entity.id).intValue()); insertClause = new SQLInsertClause(conn, new H2Templates(), entity); assertEquals(Collections.singletonList(4), insertClause.set(entity.name, "World").executeWithKeys(entity.id)); }
@Override public long createResource() { return querydslSupport.execute((connection, configuration) -> { QResource qResource = QResource.resource; new SQLInsertClause(connection, configuration, qResource); return new SQLInsertClause(connection, configuration, qResource) .executeWithKey(qResource.resourceId); }); }
@Override public BlobAccessor createBlob() { Connection connection = createDatabaseConnection(); QBlobstoreBlob qBlob = QBlobstoreBlob.blobstoreBlob; Long blobId; try { blobId = new SQLInsertClause(connection, this.querydslConfiguration, qBlob) .set(qBlob.version, 0L).set(qBlob.blob, this.emptyBlobExpression) .executeWithKey(qBlob.blobId); } catch (RuntimeException | Error e) { closeCloseableDueToThrowable(connection, e); throw new RuntimeException(e); } return updateBlob(blobId, connection, false); }
.set(addonProperties.VALUE, value) .set(addonProperties.UNUSED_FIELD_PRIMARY_KEY, QAddonProperty.getPrimaryKeyForProperty(addonKey, propertyKey)) .executeWithKey(addonProperties.ID);
@Transactional public Pizza save(Pizza pizza) { Long pizzaId = sqlQueryFactory.insert(qPizza) .columns(qPizza.baseId, qPizza.name, qPizza.price) .values(pizza.getBase().getId(), pizza.getName(), pizza.getPrice()) .executeWithKey(Long.class); SQLInsertClause insert = sqlQueryFactory.insert(qPizzaToppings); pizza.getToppings().forEach(tpg -> insert.values(pizzaId, tpg.getId()).addBatch()); insert.execute(); pizza.setId(pizzaId); return pizza; } }
@Test @ExcludeIn({CUBRID, SQLSERVER}) public void insert_with_keys_Projected() throws SQLException { assertNotNull(insert(survey).set(survey.name, "Hello you").executeWithKey(survey.id)); }
@Test @ExcludeIn({CUBRID, SQLSERVER}) public void insert_with_keys_Projected2() throws SQLException { Path<Object> idPath = ExpressionUtils.path(Object.class, "id"); Object id = insert(survey).set(survey.name, "Hello you").executeWithKey(idPath); assertNotNull(id); }
/** * 新增数据 * * @param entity 实体 * @param setId 是否设置ID * @return ID */ default long create(@Nonnull T entity, boolean setId) { beforeCreate(entity); validOnCreate(entity); Long id = entity.getId(); if (null == id) { id = sql().insert(root()) .populate(entity) .executeWithKey(pk()); } else { sql().insert(root()) .populate(entity) .execute(); } if (null == id) { throw new HuiCheException("新增数据失败"); } else { if (setId) { entity.setId(id); } return id; } }
@Test public void insert_update_query_and_delete() { // Insert Employee employee = new Employee(); employee.setFirstname("John"); Integer id = insert(e).populate(employee).executeWithKey(e.id); assertNotNull(id); employee.setId(id); // Update employee.setLastname("S"); assertEquals(1L, update(e).populate(employee).where(e.id.eq(employee.getId())).execute()); // Query Employee smith = query().from(e).where(e.lastname.eq("S")).limit(1).select(e).fetchFirst(); assertEquals("John", smith.getFirstname()); // Delete (no changes needed) assertEquals(1L, delete(e).where(e.id.eq(employee.getId())).execute()); }
Integer id = insert(e).populate(employee).executeWithKey(e.id); employee.setId(id);