/** * 根据条件更新,自行set更新内容 * * @param setter update * @param predicate 条件 * @return 更新条数 */ default long update(Consumer<SQLUpdateClause> setter, @Nullable Predicate... predicate) { Assert.ok("更新时条件不能为空", null != predicate && predicate.length > 0); SQLUpdateClause update = sql().update(root()); setter.accept(update); return update.where(predicate).execute(); }
protected void storeState(PostgreSQLQueryFactory queryFactory, String addonKey, PluginState state) { final QAtlassianConnectAddonData atlassianConnectAddonData = new QAtlassianConnectAddonData(rdbmsSchemaService.get().getSandbox()); Preconditions.checkState(state == PluginState.ENABLED || state == PluginState.DISABLED, "Unexpected add-on state to store: " + state.name()); final boolean isEnabled = state == PluginState.ENABLED; final long rowsUpdated = queryFactory.update(atlassianConnectAddonData) .set(atlassianConnectAddonData.IS_ENABLED, isEnabled) .set(atlassianConnectAddonData.LAST_UPDATED, new Timestamp(DateTime.now(DateTimeZone.UTC).getMillis())) .where(atlassianConnectAddonData.ADDON_KEY.eq(addonKey)) .execute(); if (rowsUpdated != 1) { log.error("Failed to update an add-on '{}' state entry: rowsUpdated: {}, expected: {}. THIS ERROR is OK only until addon migration is released", addonKey, rowsUpdated, 1); } }
private void squashVersions(List<Revision> revisions) { long count = options.queryFactory .update(options.version) .set(options.version.status, SQUASHED) .where(options.version.revision.in(revisions), options.version.status.ne(SQUASHED)) .execute(); if (count != revisions.size()) { throw new ConcurrentMaintenanceException("Expected to squash " + revisions.size() + " revisions. Got " + count); } }
/** * 根据条件更新,自行set更新内容 * * @param entity 实体类 * @param setter update * @param predicate 条件 * @return 更新条数 */ default long update(@Nonnull T entity, Consumer<SQLUpdateClause> setter, @Nullable Predicate... predicate) { Assert.ok("更新时条件不能为空", null != predicate && predicate.length > 0); // 强制不更新ID entity.setId(null); SQLUpdateClause update = sql().update(root()).populate(entity); setter.accept(update); return update.where(predicate).execute(); }
private void squashParent(Revision revision, Revision parentRevision) { options.queryFactory .update(options.parent) .set(options.parent.status, SQUASHED) .where(options.parent.revision.eq(revision), options.parent.parentRevision.eq(parentRevision)) .execute(); }
/** * 根据条件更新,自行set更新内容 * * @param entity 实体类 * @param setter update * @return 更新条数 */ default long update(@Nonnull T entity, Consumer<SQLUpdateClause> setter) { Assert.notNull(HuiCheError.UPDATE_MUST_HAVE_ID, entity.getId()); SQLUpdateClause update = sql().update(root()).populate(entity); setter.accept(update); return update.where(pk().eq(entity.getId())).execute(); }
/** * 根据条件更新实体,完全根据条件更新,忽略ID * * @param entity 要更新的内容 * @param predicate 条件 * @return 变更条数 */ default long update(@Nonnull T entity, @Nullable Predicate... predicate) { Assert.ok("更新时条件不能为空", null != predicate && predicate.length > 0); // 强制不更新ID entity.setId(null); validRegular(entity); return sql().update(root()).populate(entity).where(predicate).execute(); }
private void squashProperty(Revision revision, PropertyPath path) { options.queryFactory .update(options.property) .set(options.property.status, SQUASHED) .where(options.property.revision.eq(revision), options.property.path.eq(path.toString())) .execute(); }
@Override protected void executeAfterBlobStatementClosedButBeforeConnectionClose() throws SQLException { super.executeAfterBlobStatementClosedButBeforeConnectionClose(); if (!this.updateBlobContentInUpdateSQLNecessary && getVersion() == this.newVersion) { return; } QBlobstoreBlob qBlob = QBlobstoreBlob.blobstoreBlob; SQLUpdateClause updateClause = new SQLUpdateClause(this.connection, this.querydslConfiguration, qBlob) .set(qBlob.version, getNewVersion()) .where(qBlob.blobId.eq(this.connectedBlob.blobId)); if (this.updateBlobContentInUpdateSQLNecessary) { updateClause.set(qBlob.blob, this.connectedBlob.blobChannel.getBlobExpression()); } updateClause.execute(); }
@Test public void update_where() { SQLUpdateClause updateClause = new SQLUpdateClause(connection,SQLTemplates.DEFAULT,survey); updateClause.set(survey.id, 1); updateClause.set(survey.name, (String) null); updateClause.where(survey.name.eq("XXX")); assertEquals("update SURVEY\nset ID = ?, NAME = ?\nwhere SURVEY.NAME = ?", updateClause.toString()); }
@Test public void batch_templates() throws SQLException { assertEquals(1, insert(survey).values(2, "A","B").execute()); assertEquals(1, insert(survey).values(3, "B","C").execute()); SQLUpdateClause update = update(survey); update.set(survey.name, "AA").where(survey.name.eq(Expressions.stringTemplate("'A'"))).addBatch(); update.set(survey.name, "BB").where(survey.name.eq(Expressions.stringTemplate("'B'"))).addBatch(); assertEquals(2, update.execute()); }
@Test public void update5() { assertEquals(1, insert(survey).values(3, "B","C").execute()); assertEquals(1, update(survey).set(survey.name, "BB").where(survey.name.eq("B")).execute()); }
@Test public void update4() { assertEquals(1, insert(survey).values(2, "A","B").execute()); assertEquals(1, update(survey).set(survey.name, "AA").where(survey.name.eq("A")).execute()); }
@Test public void batch() throws SQLException { assertEquals(1, insert(survey).values(2, "A","B").execute()); assertEquals(1, insert(survey).values(3, "B","C").execute()); SQLUpdateClause update = update(survey); update.set(survey.name, "AA").where(survey.name.eq("A")).addBatch(); assertEquals(1, update.getBatchCount()); update.set(survey.name, "BB").where(survey.name.eq("B")).addBatch(); assertEquals(2, update.getBatchCount()); assertEquals(2, update.execute()); }
@Test public void update_with_subQuery_exists() { QSurvey survey1 = new QSurvey("s1"); QEmployee employee = new QEmployee("e"); SQLUpdateClause update = update(survey1); update.set(survey1.name, "AA"); update.where(selectOne().from(employee).where(survey1.id.eq(employee.id)).exists()); assertEquals(1, update.execute()); }
@Test public void update_with_subQuery_exists2() { QSurvey survey1 = new QSurvey("s1"); QEmployee employee = new QEmployee("e"); SQLUpdateClause update = update(survey1); update.set(survey1.name, "AA"); update.where(selectOne().from(employee).where(survey1.name.eq(employee.lastname)).exists()); assertEquals(0, update.execute()); }
@Test public void update_with_subQuery_notExists() { QSurvey survey1 = new QSurvey("s1"); QEmployee employee = new QEmployee("e"); SQLUpdateClause update = update(survey1); update.set(survey1.name, "AA"); update.where(query().from(employee).where(survey1.id.eq(employee.id)).notExists()); assertEquals(0, update.execute()); }
@Test public void update_with_subQuery_exists_Params() { QSurvey survey1 = new QSurvey("s1"); QEmployee employee = new QEmployee("e"); Param<Integer> param = new Param<Integer>(Integer.class, "param"); SQLQuery<?> sq = query().from(employee).where(employee.id.eq(param)); sq.set(param, -12478923); SQLUpdateClause update = update(survey1); update.set(survey1.name, "AA"); update.where(sq.exists()); assertEquals(0, update.execute()); }
@Test public void update2() throws SQLException { List<Path<?>> paths = Collections.<Path<?>>singletonList(survey.name); List<?> values = Collections.singletonList("S"); // original state long count = query().from(survey).fetchCount(); assertEquals(0, query().from(survey).where(survey.name.eq("S")).fetchCount()); // update call with 0 update count assertEquals(0, update(survey).where(survey.name.eq("XXX")).set(paths, values).execute()); assertEquals(0, query().from(survey).where(survey.name.eq("S")).fetchCount()); // update call with full update count assertEquals(count, update(survey).set(paths, values).execute()); assertEquals(count, query().from(survey).where(survey.name.eq("S")).fetchCount()); }
@Test public void update() throws SQLException { // original state long count = query().from(survey).fetchCount(); assertEquals(0, query().from(survey).where(survey.name.eq("S")).fetchCount()); // update call with 0 update count assertEquals(0, update(survey).where(survey.name.eq("XXX")).set(survey.name, "S").execute()); assertEquals(0, query().from(survey).where(survey.name.eq("S")).fetchCount()); // update call with full update count assertEquals(count, update(survey).set(survey.name, "S").execute()); assertEquals(count, query().from(survey).where(survey.name.eq("S")).fetchCount()); }