use of org.jooq.SQLDialect in project jOOQ by jOOQ.
the class SQLDialectChecker method createSourceVisitor.
@Override
protected SourceVisitor<Void, Void> createSourceVisitor() {
return new SourceVisitor<Void, Void>(getChecker()) {
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
try {
ExecutableElement elementFromUse = elementFromUse(node);
Support support = elementFromUse.getAnnotation(Support.class);
// all jOOQ API method calls will type check.
if (support != null && support.value().length > 0) {
Element enclosing = elementFromDeclaration(enclosingMethod(getPath(root, node)));
EnumSet<SQLDialect> supported = EnumSet.copyOf(asList(support.value()));
EnumSet<SQLDialect> allowed = EnumSet.noneOf(SQLDialect.class);
EnumSet<SQLDialect> required = EnumSet.noneOf(SQLDialect.class);
boolean evaluateRequire = true;
while (enclosing != null) {
Allow allow = enclosing.getAnnotation(Allow.class);
if (allow != null)
allowed.addAll(asList(allow.value()));
if (evaluateRequire) {
Require require = enclosing.getAnnotation(Require.class);
if (require != null) {
evaluateRequire = false;
required.clear();
required.addAll(asList(require.value()));
}
}
enclosing = enclosing.getEnclosingElement();
}
if (allowed.isEmpty())
error(node, "No jOOQ API usage is allowed at current scope. Use @Allow.");
if (required.isEmpty())
error(node, "No jOOQ API usage is allowed at current scope due to conflicting @Require specification.");
boolean allowedFail = true;
allowedLoop: for (SQLDialect a : allowed) {
for (SQLDialect s : supported) {
if (a.supports(s)) {
allowedFail = false;
break allowedLoop;
}
}
}
if (allowedFail)
error(node, "The allowed dialects in scope " + allowed + " do not include any of the supported dialects: " + supported);
boolean requiredFail = false;
requiredLoop: for (SQLDialect r : required) {
for (SQLDialect s : supported) if (r.supports(s))
continue requiredLoop;
requiredFail = true;
break requiredLoop;
}
if (requiredFail)
error(node, "Not all of the required dialects " + required + " from the current scope are supported " + supported);
}
} catch (final Exception e) {
print(new Printer() {
@Override
public void print(PrintWriter t) {
e.printStackTrace(t);
}
});
}
return super.visitMethodInvocation(node, p);
}
};
}
use of org.jooq.SQLDialect in project jOOQ by jOOQ.
the class DefaultBinding method toSQLCast.
/**
* Render the bind variable including a cast, if necessary
*/
private final void toSQLCast(BindingSQLContext<U> ctx, T converted) {
DataType<T> dataType = DefaultDataType.getDataType(ctx.dialect(), type);
DataType<T> sqlDataType = dataType.getSQLDataType();
SQLDialect family = ctx.family();
// [#822] Some RDBMS need precision / scale information on BigDecimals
if (converted != null && type == BigDecimal.class && asList(CUBRID, DERBY, FIREBIRD, HSQLDB).contains(family)) {
// Add precision / scale on BigDecimals
int scale = ((BigDecimal) converted).scale();
int precision = ((BigDecimal) converted).precision();
// [#5323] BigDecimal precision is always 1 for BigDecimals smaller than 1.0
if (scale >= precision)
precision = scale + 1;
toSQLCast(ctx, converted, dataType, 0, precision, scale);
} else // [#1028] Most databases don't know an OTHER type (except H2, HSQLDB).
if (SQLDataType.OTHER == sqlDataType) {
// If the bind value is set, it can be used to derive the cast type
if (converted != null) {
toSQLCast(ctx, converted, DefaultDataType.getDataType(family, converted.getClass()), 0, 0, 0);
} else // Sybase can do without casting in most cases.
if (asList().contains(family)) {
ctx.render().sql(ctx.variable());
} else // Derby and DB2 must have a type associated with NULL. Use VARCHAR
// as a workaround. That's probably not correct in all cases, though
{
toSQLCast(ctx, converted, DefaultDataType.getDataType(family, String.class), 0, 0, 0);
}
} else // [#1130] TODO type can be null for ARRAY types, etc.
if (asList(POSTGRES).contains(family) && (sqlDataType == null || !sqlDataType.isTemporal())) {
toSQL(ctx, converted);
} else // dialects
if ((sqlDataType == SQLDataType.VARCHAR || sqlDataType == SQLDataType.CHAR) && asList(FIREBIRD).contains(family)) {
toSQLCast(ctx, converted, dataType, getValueLength(converted), 0, 0);
} else // In all other cases, the bind variable can be cast normally
{
toSQLCast(ctx, converted, dataType, dataType.length(), dataType.precision(), dataType.scale());
}
}
use of org.jooq.SQLDialect in project atlasdb by palantir.
the class JdbcKeyValueService method create.
public static JdbcKeyValueService create(JdbcKeyValueConfiguration config) {
JdbcDataSourceConfiguration dataSourceConfig = config.getDataSourceConfig();
SQLDialect sqlDialect = SQLDialect.valueOf(dataSourceConfig.getSqlDialect());
DataSource dataSource = dataSourceConfig.createDataSource();
Settings settings = new Settings();
settings.setRenderNameStyle(RenderNameStyle.AS_IS);
final JdbcKeyValueService kvs = new JdbcKeyValueService(settings, sqlDialect, dataSource, config.getTablePrefix(), config.getRowBatchSize(), config.getBatchSizeForReads(), config.getBatchSizeForMutations());
kvs.run((Function<DSLContext, Void>) ctx -> {
String partialSql = ctx.createTable(kvs.METADATA_TABLE).column(TABLE_NAME, VARCHAR.nullable(false)).column(METADATA, BLOB.nullable(false)).getSQL();
int endIndex = partialSql.lastIndexOf(')');
String fullSql = partialSql.substring(0, endIndex) + "," + " CONSTRAINT pk_" + kvs.METADATA_TABLE.getName() + " PRIMARY KEY (" + TABLE_NAME.getName() + ")" + partialSql.substring(endIndex);
try {
ctx.execute(fullSql);
} catch (DataAccessException e) {
kvs.handleTableCreationException(e);
}
return null;
});
return kvs;
}
use of org.jooq.SQLDialect in project tutorials by eugenp.
the class ExceptionTranslator method exception.
@Override
public void exception(ExecuteContext context) {
SQLDialect dialect = context.configuration().dialect();
SQLExceptionTranslator translator = new SQLErrorCodeSQLExceptionTranslator(dialect.name());
context.exception(translator.translate("Access database using jOOQ", context.sql(), context.sqlException()));
}
use of org.jooq.SQLDialect in project collect by openforis.
the class SQLRelationalSchemaCreator method getRdbDialect.
private RdbDialect getRdbDialect(CollectDSLContext dsl) {
SQLDialect dialect = dsl.getDialect();
RdbDialect rdbDialect;
switch(dialect) {
case SQLITE:
rdbDialect = RdbDialect.SQLITE;
break;
default:
rdbDialect = RdbDialect.STANDARD;
}
return rdbDialect;
}
Aggregations