use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.
the class NodePreparedSqlBuilder method visitForBlockNode.
@Override
public Void visitForBlockNode(ForBlockNode node, Context p) {
ForNode forNode = node.getForNode();
SqlLocation location = forNode.getLocation();
EvaluationResult expressionResult = p.evaluate(location, forNode.getExpression());
Object expressionValue = expressionResult.getValue();
Class<?> expressionValueClass = expressionResult.getValueClass();
Iterable<?> iterable;
if (Iterable.class.isAssignableFrom(expressionValueClass)) {
iterable = (Iterable<?>) expressionValue;
} else if (expressionValueClass.isArray()) {
iterable = Arrays.asList((Object[]) expressionValue);
} else {
throw new JdbcException(Message.DOMA2129, location.getSql(), location.getLineNumber(), location.getPosition(), forNode.getExpression(), expressionValueClass);
}
String identifier = forNode.getIdentifier();
Value originalIdentifierValue = p.removeValue(identifier);
String hasNextVariable = identifier + ForBlockNode.HAS_NEXT_SUFFIX;
Value originalHasNextValue = p.removeValue(hasNextVariable);
String indexVariable = identifier + ForBlockNode.INDEX_SUFFIX;
Value originalIndexValue = p.removeValue(indexVariable);
int index = 0;
for (Iterator<?> it = iterable.iterator(); it.hasNext(); ) {
Object each = it.next();
Value value = each == null ? new Value(void.class, null) : new Value(each.getClass(), each);
p.putValue(identifier, value);
p.putValue(hasNextVariable, new Value(boolean.class, it.hasNext()));
p.putValue(indexVariable, new Value(int.class, index));
for (SqlNode child : forNode.getChildren()) {
child.accept(this, p);
}
index++;
}
if (originalIdentifierValue == null) {
p.removeValue(identifier);
} else {
p.putValue(identifier, originalIdentifierValue);
}
if (originalHasNextValue == null) {
p.removeValue(hasNextVariable);
} else {
p.putValue(hasNextVariable, originalHasNextValue);
}
if (originalIndexValue == null) {
p.removeValue(indexVariable);
} else {
p.putValue(indexVariable, originalIndexValue);
}
EndNode endNode = node.getEndNode();
endNode.accept(this, p);
return null;
}
use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.
the class NodePreparedSqlBuilder method handleIterableValueNode.
protected void handleIterableValueNode(ValueNode node, Context p, Iterable<?> values, Class<?> valueClass, Consumer<Scalar<?, ?>> consumer) {
int index = 0;
for (Object v : values) {
if (v == null) {
SqlLocation location = node.getLocation();
throw new JdbcException(Message.DOMA2115, location.getSql(), location.getLineNumber(), location.getPosition(), node.getVariableName(), index);
}
Supplier<Scalar<?, ?>> supplier = wrap(node.getLocation(), node.getVariableName(), v, v.getClass());
consumer.accept(supplier.get());
p.appendRawSql(", ");
p.appendFormattedSql(", ");
index++;
}
if (index == 0) {
p.appendRawSql("null");
p.appendFormattedSql("null");
} else {
p.cutBackSqlBuf(2);
p.cutBackFormattedSqlBuf(2);
}
}
use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.
the class BuiltinTableIdGenerator method selectId.
protected long selectId(IdGenerationConfig config, PreparedSql sql) {
JdbcLogger logger = config.getJdbcLogger();
Connection connection = JdbcUtil.getConnection(config.getDataSource());
try {
PreparedStatement preparedStatement = JdbcUtil.prepareStatement(connection, sql);
try {
logger.logSql(getClass().getName(), "selectId", sql);
setupOptions(config, preparedStatement);
preparedStatement.setString(1, pkColumnValue);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
Object result = resultSet.getObject(1);
if (result instanceof Number) {
return ((Number) result).longValue();
}
}
throw new JdbcException(Message.DOMA2017, config.getEntityType().getName());
} catch (SQLException e) {
throw new JdbcException(Message.DOMA2018, e, config.getEntityType().getName(), e);
} finally {
JdbcUtil.close(preparedStatement, logger);
}
} finally {
JdbcUtil.close(connection, logger);
}
}
use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.
the class BuiltinTableIdGenerator method initialize.
@Override
public void initialize() {
if (qualifiedTableName == null) {
throw new JdbcException(Message.DOMA2033, "qualifiedTableName");
}
if (pkColumnName == null) {
throw new JdbcException(Message.DOMA2033, "pkColumnName");
}
if (pkColumnValue == null) {
throw new JdbcException(Message.DOMA2033, "pkColumnValue");
}
if (valueColumnName == null) {
throw new JdbcException(Message.DOMA2033, "valueColumnName");
}
LongWrapper allocationSizeWrapper = new LongWrapper();
allocationSizeWrapper.set(allocationSize);
StringWrapper pkColumnValueWrapper = new StringWrapper();
pkColumnValueWrapper.set(pkColumnValue);
updateSql = new PreparedSql(SqlKind.UPDATE, createUpdateRawSql(), createUpdateFormattedSql(), null, Arrays.asList(new BasicInParameter<>(() -> allocationSizeWrapper), new BasicInParameter<>(() -> pkColumnValueWrapper)), SqlLogType.FORMATTED);
selectSql = new PreparedSql(SqlKind.SELECT, createSelectRawSql(), createSelectFormattedSql(), null, Collections.singletonList(new BasicInParameter<>(() -> pkColumnValueWrapper)), SqlLogType.FORMATTED);
}
use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.
the class BuiltinTableIdGenerator method updateId.
protected void updateId(IdGenerationConfig config, PreparedSql sql) {
JdbcLogger logger = config.getJdbcLogger();
Connection connection = JdbcUtil.getConnection(config.getDataSource());
try {
PreparedStatement preparedStatement = JdbcUtil.prepareStatement(connection, sql);
try {
logger.logSql(getClass().getName(), "updateId", sql);
setupOptions(config, preparedStatement);
preparedStatement.setLong(1, allocationSize);
preparedStatement.setString(2, pkColumnValue);
int rows = preparedStatement.executeUpdate();
if (rows != 1) {
throw new JdbcException(Message.DOMA2017, config.getEntityType().getName());
}
} catch (SQLException e) {
throw new JdbcException(Message.DOMA2018, e, config.getEntityType().getName(), e);
} finally {
JdbcUtil.close(preparedStatement, logger);
}
} finally {
JdbcUtil.close(connection, logger);
}
}
Aggregations