use of org.jooq.Converter in project torodb by torodb.
the class SqlHelper method setPreparedStatementValue.
@SuppressWarnings({ "rawtypes", "unchecked" })
public void setPreparedStatementValue(PreparedStatement preparedStatement, int parameterIndex, FieldType fieldType, KvValue<?> value) throws SQLException {
DataTypeForKv dataType = dataTypeProvider.getDataType(fieldType);
KvValueConverter valueConverter = dataType.getKvValueConverter();
Converter converter = dataType.getConverter();
SqlBinding sqlBinding = valueConverter.getSqlBinding();
sqlBinding.set(preparedStatement, parameterIndex, converter.to(value));
}
use of org.jooq.Converter in project torodb by torodb.
the class SqlHelper method setPreparedStatementNullableValue.
@SuppressWarnings({ "rawtypes", "unchecked" })
public void setPreparedStatementNullableValue(PreparedStatement preparedStatement, int parameterIndex, FieldType fieldType, KvValue<?> value) throws SQLException {
DataTypeForKv dataType = dataTypeProvider.getDataType(fieldType);
if (value != null) {
KvValueConverter valueConverter = dataType.getKvValueConverter();
SqlBinding sqlBinding = valueConverter.getSqlBinding();
Converter converter = dataType.getConverter();
sqlBinding.set(preparedStatement, parameterIndex, converter.to(value));
} else {
preparedStatement.setNull(parameterIndex, dataType.getSQLType());
}
}
use of org.jooq.Converter in project jOOQ by jOOQ.
the class AbstractTypedElementDefinition method mapDefinedType.
public static final DataTypeDefinition mapDefinedType(Definition container, Definition child, DataTypeDefinition definedType, JavaTypeResolver resolver) {
DataTypeDefinition result = definedType;
Database db = container.getDatabase();
log.debug("Type mapping", child + " with type " + definedType.getType());
// [#976] Mapping DATE as TIMESTAMP
if (db.dateAsTimestamp()) {
DataType<?> dataType = null;
try {
dataType = getDataType(db, result.getType(), 0, 0);
} catch (SQLDialectNotSupportedException ignore) {
}
if (dataType != null) {
// [#5239] [#5762] [#6453] Don't rely on getSQLType()
if (SQLDataType.DATE.equals(dataType.getSQLDataType())) {
DataType<?> forcedDataType = getDataType(db, SQLDataType.TIMESTAMP.getTypeName(), 0, 0);
String binding = DateAsTimestampBinding.class.getName();
if (db.javaTimeTypes())
binding = org.jooq.impl.LocalDateAsLocalDateTimeBinding.class.getName();
result = new DefaultDataTypeDefinition(db, child.getSchema(), forcedDataType.getTypeName(), 0, 0, 0, result.isNullable(), result.getDefaultValue(), result.isIdentity(), (Name) null, null, binding, null);
}
}
}
// [#677] Forced types for matching regular expressions
ForcedType forcedType = db.getConfiguredForcedType(child, definedType);
if (forcedType != null) {
String uType = forcedType.getName();
String converter = null;
String binding = result.getBinding();
CustomType customType = customType(db, forcedType);
if (customType != null) {
uType = (!StringUtils.isBlank(customType.getType())) ? customType.getType() : customType.getName();
// [#5877] [#6567] EnumConverters profit from simplified configuration
if (Boolean.TRUE.equals(customType.isEnumConverter()) || EnumConverter.class.getName().equals(customType.getConverter())) {
String tType = tType(db, resolver, definedType);
converter = resolver.constructorCall(EnumConverter.class.getName() + "<" + resolver.ref(tType) + ", " + resolver.ref(uType) + ">") + "(" + resolver.classLiteral(tType) + ", " + resolver.classLiteral(uType) + ")";
} else if (customType.getLambdaConverter() != null) {
LambdaConverter c = customType.getLambdaConverter();
String tType = tType(db, resolver, definedType);
converter = resolver.ref(Converter.class) + ".of" + (!FALSE.equals(c.isNullable()) ? "Nullable" : "") + "(" + resolver.classLiteral(tType) + ", " + resolver.classLiteral(uType) + ", " + c.getFrom() + ", " + c.getTo() + ")";
} else if (!StringUtils.isBlank(customType.getConverter())) {
converter = customType.getConverter();
}
if (!StringUtils.isBlank(customType.getBinding()))
binding = customType.getBinding();
}
if (uType != null) {
db.markUsed(forcedType);
log.info("Forcing type", child + " to " + forcedType);
DataType<?> forcedDataType = null;
boolean n = result.isNullable();
String d = result.getDefaultValue();
boolean i = result.isIdentity();
boolean r = result.isReadonly();
String g = result.getGeneratedAlwaysAs();
int l = 0;
int p = 0;
int s = 0;
// [#2486] Allow users to override length, precision, and scale
Matcher matcher = LENGTH_PRECISION_SCALE_PATTERN.matcher(uType);
if (matcher.find()) {
if (!isEmpty(matcher.group(1))) {
l = p = convert(matcher.group(1), int.class);
} else {
p = convert(matcher.group(2), int.class);
s = convert(matcher.group(3), int.class);
}
}
try {
forcedDataType = getDataType(db, uType, p, s);
} catch (SQLDialectNotSupportedException ignore) {
}
// [#677] SQLDataType matches are actual type-rewrites
if (forcedDataType != null) {
// [#3704] When <forcedType/> matches a custom type AND a data type rewrite, the rewrite was usually accidental.
if (customType != null)
log.warn("Custom type conflict", child + " has custom type " + customType + " forced by " + forcedType + " but a data type rewrite applies");
result = new DefaultDataTypeDefinition(db, child.getSchema(), uType, l, p, s, n, r, g, d, i, (Name) null, converter, binding, null);
} else // Other forced types are UDT's, enums, etc.
if (customType != null) {
l = result.getLength();
p = result.getPrecision();
s = result.getScale();
String t = result.getType();
Name u = result.getQualifiedUserType();
result = new DefaultDataTypeDefinition(db, definedType.getSchema(), t, l, p, s, n, r, g, d, i, u, converter, binding, uType);
} else // [#4597] If we don't have a type-rewrite (forcedDataType) or a
// matching customType, the user probably malconfigured
// their <forcedTypes/> or <customTypes/>
{
// [#7373] [#10944] Refer to <customType/> only if someone is still using the feature
if (db.getConfiguredCustomTypes().isEmpty())
log.warn("Bad configuration for <forcedType/> " + forcedType.getName() + ". No matching SQLDataType found: " + forcedType);
else
log.warn("Bad configuration for <forcedType/> " + forcedType.getName() + ". No matching <customType/> found, and no matching SQLDataType found: " + forcedType);
}
}
}
return result;
}
use of org.jooq.Converter in project jOOQ by jOOQ.
the class PostgresUtils method toPGString.
/**
* Create a PostgreSQL string representation of a record.
*/
public static String toPGString(Record r) {
StringBuilder sb = new StringBuilder();
sb.append("(");
String separator = "";
for (int i = 0; i < r.size(); i++) {
@SuppressWarnings({ "unchecked", "rawtypes" }) Object a = ((Converter) r.field(i).getConverter()).to(r.get(i));
sb.append(separator);
// [#753] null must not be set as a literal
if (a != null) {
if (a instanceof byte[])
sb.append(toPGString((byte[]) a));
else
sb.append("\"").append(StringUtils.replace(StringUtils.replace(toPGString(a), "\\", "\\\\"), "\"", "\\\"")).append("\"");
}
separator = ",";
}
sb.append(")");
return sb.toString();
}
Aggregations