private Optional<Long> selectMaxSnapshotPrimaryKey(long globalIdPk, Session session) { Optional<Long> maxPrimaryKey = session .select("MAX(" + SNAPSHOT_PK + ")") .from(tableNameProvider.getSnapshotTableNameWithSchema()) .and(SNAPSHOT_GLOBAL_ID_FK, globalIdPk) .queryName("select max snapshot's PK") .queryForOptionalLong(); if (maxPrimaryKey.isPresent() && maxPrimaryKey.get() == 0){ return Optional.empty(); } return maxPrimaryKey; }
private Optional<Long> selectMaxSnapshotPrimaryKey(long globalIdPk, Session session) { Optional<Long> maxPrimaryKey = session .select("MAX(" + SNAPSHOT_PK + ")") .from(tableNameProvider.getSnapshotTableNameWithSchema()) .and(SNAPSHOT_GLOBAL_ID_FK, globalIdPk) .queryName("select max snapshot's PK") .queryForOptionalLong(); if (maxPrimaryKey.isPresent() && maxPrimaryKey.get() == 0){ return Optional.empty(); } return maxPrimaryKey; }
private Optional<Long> findGlobalIdPkInDB(GlobalId globalId, Session session) { SelectBuilder select = session.select(GLOBAL_ID_PK) .from(getGlobalIdTableNameWithSchema()); if (globalId instanceof ValueObjectId) { final ValueObjectId valueObjectId = (ValueObjectId) globalId; Optional<Long> ownerFk = findGlobalIdPk(valueObjectId.getOwnerId(), session); if (!ownerFk.isPresent()){ return Optional.empty(); } select.and(GLOBAL_ID_FRAGMENT, valueObjectId.getFragment()) .and(GLOBAL_ID_OWNER_ID_FK, ownerFk.get()) .queryName("find PK of valueObjectId"); } else if (globalId instanceof InstanceId){ select.and(GLOBAL_ID_LOCAL_ID, jsonConverter.toJson(((InstanceId)globalId).getCdoId())) .and(GLOBAL_ID_TYPE_NAME, globalId.getTypeName()) .queryName("find PK of InstanceId"); } else if (globalId instanceof UnboundedValueObjectId){ select.and(GLOBAL_ID_TYPE_NAME, globalId.getTypeName()) .queryName("find PK of UnboundedValueObjectId"); } return select.queryForOptionalLong(); }
private Optional<Long> findGlobalIdPkInDB(GlobalId globalId, Session session) { SelectBuilder select = session.select(GLOBAL_ID_PK) .from(getGlobalIdTableNameWithSchema()); if (globalId instanceof ValueObjectId) { final ValueObjectId valueObjectId = (ValueObjectId) globalId; Optional<Long> ownerFk = findGlobalIdPk(valueObjectId.getOwnerId(), session); if (!ownerFk.isPresent()){ return Optional.empty(); } select.and(GLOBAL_ID_FRAGMENT, valueObjectId.getFragment()) .and(GLOBAL_ID_OWNER_ID_FK, ownerFk.get()) .queryName("find PK of valueObjectId"); } else if (globalId instanceof InstanceId){ select.and(GLOBAL_ID_LOCAL_ID, jsonConverter.toJson(((InstanceId)globalId).getCdoId())) .and(GLOBAL_ID_TYPE_NAME, globalId.getTypeName()) .queryName("find PK of InstanceId"); } else if (globalId instanceof UnboundedValueObjectId){ select.and(GLOBAL_ID_TYPE_NAME, globalId.getTypeName()) .queryName("find PK of UnboundedValueObjectId"); } return select.queryForOptionalLong(); }