@SuppressWarnings("unchecked") @Override public <T> List<? extends T> findNonRemoved( Class<T> mapType, Class<?> resourceType, long resourceId) { String type = schemaFactory.getSchemaName(mapType); Table<?> table = getTable(mapType); Relationship reference = getRelationship(mapType, resourceType); TableField<?, Object> removed = JooqUtils.getTableField(metaDataManager, type, ObjectMetaDataManager.REMOVED_FIELD); TableField<?, Object> referenceField = JooqUtils.getTableField(metaDataManager, type, reference.getPropertyName()); if (removed == null || referenceField == null) { throw new IllegalArgumentException( "Type [" + mapType + "] is missing required removed or reference column"); } return (List<? extends T>) create() .selectFrom(table) .where(removed.isNull().and(referenceField.eq(resourceId))) .fetch(); }
@Override public boolean equals(Object that) { // [#2144] TableFieldImpl equality can be decided without executing the // rather expensive implementation of AbstractQueryPart.equals() if (that instanceof TableField) { TableField<?, ?> other = (TableField<?, ?>) that; return StringUtils.equals(getTable(), other.getTable()) && StringUtils.equals(getName(), other.getName()); } return super.equals(that); }
protected static org.jooq.Condition listToCondition(TableField<?, Object> field, List<?> list) { org.jooq.Condition condition = null; for (Object value : list) { if (value instanceof Condition) { org.jooq.Condition newCondition = toCondition(field, (Condition) value); condition = condition == null ? newCondition : condition.and(newCondition); } else { condition = condition == null ? field.eq(value) : condition.and(field.eq(value)); } } return condition; }
public static org.jooq.Condition toConditions( ObjectMetaDataManager metaData, String type, Map<Object, Object> criteria) { org.jooq.Condition existingCondition = null; for (Map.Entry<Object, Object> entry : criteria.entrySet()) { Object value = entry.getValue(); Object key = entry.getKey(); TableField<?, Object> field = null; if (key == org.jooq.Condition.class) { if (!(value instanceof org.jooq.Condition)) { throw new IllegalArgumentException( "If key is Condition, value must be an instanceof Condition got key [" + key + "] value [" + value + "]"); } } else { field = getTableField(metaData, type, key); if (field == null) { continue; } } org.jooq.Condition newCondition = null; if (value instanceof org.jooq.Condition) { newCondition = (org.jooq.Condition) value; } else if (value instanceof Condition) { newCondition = toCondition(field, (Condition) value); } else if (value instanceof List) { newCondition = listToCondition(field, (List<?>) value); } else if (value == null) { newCondition = field.isNull(); } else { newCondition = field.eq(value); } if (existingCondition == null) { existingCondition = newCondition; } else { existingCondition = existingCondition.and(newCondition); } } return existingCondition; }
@SuppressWarnings("unchecked") public static <T extends UpdatableRecord<?>> T findById( DSLContext context, Class<T> clz, Object id) { if (id == null) return null; Table<?> table = getTableFromRecordClass(clz); if (table == null) return null; UniqueKey<?> key = table.getPrimaryKey(); if (key == null || key.getFieldsArray().length != 1) return null; TableField<?, Object> keyField = (TableField<?, Object>) key.getFieldsArray()[0]; /* Convert object because we are abusing type safety here */ Object converted = keyField.getDataType().convert(id); return (T) context.selectFrom(table).where(keyField.eq(converted)).fetchOne(); }
@SuppressWarnings("unchecked") @Override public <T> T findToRemove( Class<T> mapType, Class<?> leftResourceType, long leftResourceId, Class<?> rightResourceType, long rightResourceId) { String type = schemaFactory.getSchemaName(mapType); Table<?> table = getTable(mapType); Relationship leftReference = getRelationship(mapType, leftResourceType); Relationship rightReference = getRelationship(mapType, rightResourceType); TableField<?, Object> removed = JooqUtils.getTableField(metaDataManager, type, ObjectMetaDataManager.REMOVED_FIELD); TableField<?, Object> state = JooqUtils.getTableField(metaDataManager, type, ObjectMetaDataManager.STATE_FIELD); TableField<?, Object> leftReferenceField = JooqUtils.getTableField(metaDataManager, type, leftReference.getPropertyName()); TableField<?, Object> rightReferenceField = JooqUtils.getTableField(metaDataManager, type, rightReference.getPropertyName()); if (removed == null || leftReferenceField == null || rightReferenceField == null) { throw new IllegalArgumentException( "Type [" + mapType + "] is missing required removed or references column"); } return (T) create() .selectFrom(table) .where( (removed.isNull().or(state.eq(CommonStatesConstants.REMOVING))) .and(leftReferenceField.eq(leftResourceId)) .and(rightReferenceField.eq(rightResourceId))) .fetchOne(); }
private <R extends UpdatableRecord<R>> void testStoreWithOptimisticLock0( UpdatableTable<R> table, TableField<R, Integer> id, TableField<R, String> string) throws Exception { Factory create = create(new Settings().withExecuteWithOptimisticLocking(true)); // Storing without changing shouldn't execute any queries R record1 = create.fetchOne(table, id.equal(1)); assertEquals(0, record1.store()); assertEquals(0, record1.store()); // Succeed if there are no concurrency issues record1.setValue(string, "New Title 1"); assertEquals(1, record1.store()); assertEquals("New Title 1", create.fetchOne(table, id.equal(1)).getValue(string)); // Get new books R record2 = create.fetchOne(table, id.equal(1)); R record3 = create.fetchOne(table, id.equal(1)); // Still won't fail, but this will cause record3 to be stale record2.setValue(string, "New Title 2"); assertEquals(1, record2.store()); assertEquals("New Title 2", create.fetchOne(table, id.equal(1)).getValue(string)); // Storing without changing shouldn't execute any queries assertEquals(0, record3.store()); // This should fail as record3 is stale record3.setValue(string, "New Title 3"); try { record3.store(); fail(); } catch (DataChangedException expected) {} assertEquals("New Title 2", create.fetchOne(table, id.equal(1)).getValue(string)); // Refreshing first will work, though record3.refresh(); record3.setValue(string, "New Title 3"); assertEquals(1, record3.store()); assertEquals("New Title 3", create.fetchOne(table, id.equal(1)).getValue(string)); // Get new books R record4 = create.fetchOne(table, id.equal(1)); R record5 = create.fetchOne(table, id.equal(1)); // Delete the book assertEquals(1, record4.delete()); // Storing without changing shouldn't execute any queries assertEquals(0, record5.store()); // This should fail, as the database record no longer exists record5.setValue(string, "New Title 5"); try { record5.store(); fail(); } catch (DataChangedException expected) {} // Restore the book, refresh the copy, then it should work assertEquals(1, record4.store()); record5.refresh(); record5.setValue(string, "New Title 5"); assertEquals(1, record5.store()); assertEquals("New Title 5", create.fetchOne(table, id.equal(1)).getValue(string)); // Deleting the original should no longer be possible try { record4.delete(); fail(); } catch (DataChangedException expected) {} // Refreshing and deleting should work record4.refresh(); assertEquals(1, record4.delete()); // Now the other record cannot be deleted anymore try { record5.delete(); fail(); } catch (DataChangedException expected) {} }
protected static org.jooq.Condition toCondition(TableField<?, Object> field, Condition value) { Condition condition = value; switch (condition.getConditionType()) { case EQ: return field.eq(condition.getValue()); case GT: return field.gt(condition.getValue()); case GTE: return field.ge(condition.getValue()); case IN: List<Object> values = condition.getValues(); if (values.size() == 1) { return field.eq(values.get(0)); } else { return field.in(condition.getValues()); } case NOTIN: List<Object> vals = condition.getValues(); if (vals.size() == 1) { return field.ne(vals.get(0)); } else { return field.notIn(condition.getValues()); } case LIKE: return field.like(condition.getValue().toString()); case LT: return field.lt(condition.getValue()); case LTE: return field.le(condition.getValue()); case NE: return field.ne(condition.getValue()); case NOTLIKE: return field.notLike(condition.getValue().toString()); case NOTNULL: return field.isNotNull(); case NULL: return field.isNull(); case PREFIX: return field.like(condition.getValue() + "%"); case OR: return toCondition(field, condition.getLeft()).or(toCondition(field, condition.getRight())); default: throw new IllegalArgumentException( "Invalid condition type [" + condition.getConditionType() + "]"); } }