Search in sources :

Example 1 with SQLDialect

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);
        }
    };
}
Also used : Require(org.jooq.Require) Support(org.jooq.Support) SourceVisitor(org.checkerframework.framework.source.SourceVisitor) ExecutableElement(javax.lang.model.element.ExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) Allow(org.jooq.Allow) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) SQLDialect(org.jooq.SQLDialect) PrintWriter(java.io.PrintWriter)

Example 2 with SQLDialect

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());
    }
}
Also used : SQLDialect(org.jooq.SQLDialect) PostgresUtils.toPGArrayString(org.jooq.util.postgres.PostgresUtils.toPGArrayString) BigDecimal(java.math.BigDecimal)

Example 3 with SQLDialect

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;
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) DSL(org.jooq.impl.DSL) DSL.field(org.jooq.impl.DSL.field) CheckAndSetRequest(com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest) Condition(org.jooq.Condition) Record1(org.jooq.Record1) SelectField(org.jooq.SelectField) Map(java.util.Map) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) SQLDialect(org.jooq.SQLDialect) CandidateCellForSweeping(com.palantir.atlasdb.keyvalue.api.CandidateCellForSweeping) Select(org.jooq.Select) AtlasDbConstants(com.palantir.atlasdb.AtlasDbConstants) Cell(com.palantir.atlasdb.keyvalue.api.Cell) T1_TIMESTAMP(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.T1_TIMESTAMP) Set(java.util.Set) PutBatch(com.palantir.atlasdb.keyvalue.jdbc.impl.PutBatch) T1_COL_NAME(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.T1_COL_NAME) Result(org.jooq.Result) StandardCharsets(java.nio.charset.StandardCharsets) InsufficientConsistencyException(com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) AbstractKeyValueService(com.palantir.atlasdb.keyvalue.impl.AbstractKeyValueService) ColumnRangeSelection(com.palantir.atlasdb.keyvalue.api.ColumnRangeSelection) Iterables(com.google.common.collect.Iterables) METADATA(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.METADATA) DSL.row(org.jooq.impl.DSL.row) ClusterAvailabilityStatus(com.palantir.atlasdb.keyvalue.api.ClusterAvailabilityStatus) R_TIMESTAMP(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.R_TIMESTAMP) SQLException(java.sql.SQLException) T2_MAX_TIMESTAMP(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.T2_MAX_TIMESTAMP) Lists(com.google.common.collect.Lists) VARBINARY(org.jooq.impl.SQLDataType.VARBINARY) RANGE_TABLE(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.RANGE_TABLE) VARCHAR(org.jooq.impl.SQLDataType.VARCHAR) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Record(org.jooq.Record) UnsignedBytes(com.google.common.primitives.UnsignedBytes) JdbcDataSourceConfiguration(com.palantir.atlasdb.jdbc.config.JdbcDataSourceConfiguration) T1_ROW_NAME(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.T1_ROW_NAME) BaseEncoding(com.google.common.io.BaseEncoding) COL_NAME(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.COL_NAME) MoreObjects(com.google.common.base.MoreObjects) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) BLOB(org.jooq.impl.SQLDataType.BLOB) MultiTimestampPutBatch(com.palantir.atlasdb.keyvalue.jdbc.impl.MultiTimestampPutBatch) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) DSL.table(org.jooq.impl.DSL.table) VALUE(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.VALUE) GetCandidateCellsForSweepingShim(com.palantir.atlasdb.keyvalue.impl.GetCandidateCellsForSweepingShim) TEMP_TABLE_1(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.TEMP_TABLE_1) RowN(org.jooq.RowN) RangeRequests(com.palantir.atlasdb.keyvalue.api.RangeRequests) TEMP_TABLE_2(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.TEMP_TABLE_2) Row(org.jooq.Row) Connection(java.sql.Connection) TABLE_VALUES(org.jooq.Clause.TABLE_VALUES) ATLAS_TABLE(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.ATLAS_TABLE) BatchColumnRangeSelection(com.palantir.atlasdb.keyvalue.api.BatchColumnRangeSelection) ClosableIterator(com.palantir.common.base.ClosableIterator) Table(org.jooq.Table) TABLE_NAME(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.TABLE_NAME) SelectOffsetStep(org.jooq.SelectOffsetStep) RowColumnRangeIterator(com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator) KeyValueServices(com.palantir.atlasdb.keyvalue.impl.KeyValueServices) A_TIMESTAMP(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.A_TIMESTAMP) RenderContext(org.jooq.RenderContext) HashMultimap(com.google.common.collect.HashMultimap) DSLContext(org.jooq.DSLContext) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) AbstractPagingIterable(com.palantir.util.paging.AbstractPagingIterable) Row3(org.jooq.Row3) Collection(java.util.Collection) SingleTimestampPutBatch(com.palantir.atlasdb.keyvalue.jdbc.impl.SingleTimestampPutBatch) ROW_NAME(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.ROW_NAME) T1_VALUE(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.T1_VALUE) NavigableMap(java.util.NavigableMap) Sets(com.google.common.collect.Sets) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) List(java.util.List) InsertValuesStep4(org.jooq.InsertValuesStep4) CandidateCellForSweepingRequest(com.palantir.atlasdb.keyvalue.api.CandidateCellForSweepingRequest) Entry(java.util.Map.Entry) Query(org.jooq.Query) SortedMap(java.util.SortedMap) T2_COL_NAME(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.T2_COL_NAME) A_COL_NAME(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.A_COL_NAME) HashMap(java.util.HashMap) Hashing(com.google.common.hash.Hashing) Multimap(com.google.common.collect.Multimap) BIGINT(org.jooq.impl.SQLDataType.BIGINT) TableLike(org.jooq.TableLike) ImmutableList(com.google.common.collect.ImmutableList) A_VALUE(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.A_VALUE) DataSource(javax.sql.DataSource) R_ROW_NAME(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.R_ROW_NAME) ClosableIterators(com.palantir.common.base.ClosableIterators) DataAccessException(org.jooq.exception.DataAccessException) Value(com.palantir.atlasdb.keyvalue.api.Value) Settings(org.jooq.conf.Settings) MAX_TIMESTAMP(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.MAX_TIMESTAMP) TIMESTAMP(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.TIMESTAMP) A_ROW_NAME(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.A_ROW_NAME) TokenBackedBasicResultsPage(com.palantir.util.paging.TokenBackedBasicResultsPage) Maps(com.google.common.collect.Maps) T2_ROW_NAME(com.palantir.atlasdb.keyvalue.jdbc.impl.JdbcConstants.T2_ROW_NAME) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) Closeable(java.io.Closeable) SimpleTokenBackedResultsPage(com.palantir.util.paging.SimpleTokenBackedResultsPage) BatchBindStep(org.jooq.BatchBindStep) RenderNameStyle(org.jooq.conf.RenderNameStyle) SQLDialect(org.jooq.SQLDialect) DSLContext(org.jooq.DSLContext) JdbcDataSourceConfiguration(com.palantir.atlasdb.jdbc.config.JdbcDataSourceConfiguration) Settings(org.jooq.conf.Settings) DataAccessException(org.jooq.exception.DataAccessException) DataSource(javax.sql.DataSource)

Example 4 with SQLDialect

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()));
}
Also used : SQLErrorCodeSQLExceptionTranslator(org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator) SQLExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator) SQLErrorCodeSQLExceptionTranslator(org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator) SQLDialect(org.jooq.SQLDialect)

Example 5 with SQLDialect

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;
}
Also used : RdbDialect(org.openforis.collect.relational.print.RDBPrintJob.RdbDialect) SQLDialect(org.jooq.SQLDialect)

Aggregations

SQLDialect (org.jooq.SQLDialect)36 List (java.util.List)7 Select (org.jooq.Select)7 Condition (org.jooq.Condition)6 Table (org.jooq.Table)6 Set (java.util.Set)5 Configuration (org.jooq.Configuration)5 QueryPart (org.jooq.QueryPart)5 Record (org.jooq.Record)5 Connection (java.sql.Connection)4 Collection (java.util.Collection)4 Field (org.jooq.Field)4 TRUE (java.lang.Boolean.TRUE)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Arrays.asList (java.util.Arrays.asList)3 Clause (org.jooq.Clause)3 Comparator (org.jooq.Comparator)3 Context (org.jooq.Context)3 Name (org.jooq.Name)3