Search in sources :

Example 1 with ParserException

use of org.jooq.impl.ParserException in project jOOQ by jOOQ.

the class SQLiteDatabase method loadCheckConstraints.

@Override
protected void loadCheckConstraints(DefaultRelations r) throws SQLException {
    DSLContext ctx = create().configuration().deriveSettings(s -> s.withInterpreterDelayForeignKeyDeclarations(true)).dsl();
    SchemaDefinition schema = getSchemata().get(0);
    for (Record record : ctx.select(SQLiteMaster.TBL_NAME, SQLiteMaster.SQL).from(SQLITE_MASTER).where(SQLiteMaster.SQL.likeIgnoreCase("%CHECK%")).orderBy(SQLiteMaster.TBL_NAME)) {
        TableDefinition table = getTable(schema, record.get(SQLiteMaster.TBL_NAME));
        if (table != null) {
            String sql = record.get(SQLiteMaster.SQL);
            try {
                Query query = ctx.parser().parseQuery(sql);
                for (Table<?> t : ctx.meta(query).getTables(table.getName())) {
                    for (Check<?> check : t.getChecks()) {
                        r.addCheckConstraint(table, new DefaultCheckConstraintDefinition(schema, table, check.getName(), ctx.renderInlined(check.condition()), check.enforced()));
                    }
                }
            } catch (ParserException e) {
                log.info("Cannot parse SQL: " + sql, e);
            } catch (DataDefinitionException e) {
                log.info("Cannot interpret SQL: " + sql, e);
            }
        }
    }
}
Also used : UniqueKey(org.jooq.UniqueKey) DSL(org.jooq.impl.DSL) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) DSL.field(org.jooq.impl.DSL.field) Table(org.jooq.Table) DSL.all(org.jooq.impl.DSL.all) DSL.when(org.jooq.impl.DSL.when) RoutineDefinition(org.jooq.meta.RoutineDefinition) PackageDefinition(org.jooq.meta.PackageDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) Record1(org.jooq.Record1) Map(java.util.Map) DSLContext(org.jooq.DSLContext) DSL.coalesce(org.jooq.impl.DSL.coalesce) SQLDialect(org.jooq.SQLDialect) SortOrder(org.jooq.SortOrder) Select(org.jooq.Select) DSL.name(org.jooq.impl.DSL.name) JooqLogger(org.jooq.tools.JooqLogger) Check(org.jooq.Check) Field(org.jooq.Field) DSL.select(org.jooq.impl.DSL.select) Meta(org.jooq.Meta) Result(org.jooq.Result) TableType(org.jooq.TableOptions.TableType) DomainDefinition(org.jooq.meta.DomainDefinition) List(java.util.List) SQLITE_MASTER(org.jooq.meta.sqlite.sqlite_master.SQLiteMaster.SQLITE_MASTER) TableField(org.jooq.TableField) DSL.noCondition(org.jooq.impl.DSL.noCondition) Entry(java.util.Map.Entry) ParserException(org.jooq.impl.ParserException) SchemaMappingType(org.jooq.meta.jaxb.SchemaMappingType) Query(org.jooq.Query) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultRelations(org.jooq.meta.DefaultRelations) SequenceDefinition(org.jooq.meta.SequenceDefinition) TableDefinition(org.jooq.meta.TableDefinition) HashMap(java.util.HashMap) DSL.replace(org.jooq.impl.DSL.replace) ForeignKey(org.jooq.ForeignKey) IndexDefinition(org.jooq.meta.IndexDefinition) ArrayList(java.util.ArrayList) SelectConditionStep(org.jooq.SelectConditionStep) SQLException(java.sql.SQLException) DSL.selectFrom(org.jooq.impl.DSL.selectFrom) VARCHAR(org.jooq.impl.SQLDataType.VARCHAR) ArrayDefinition(org.jooq.meta.ArrayDefinition) UDTDefinition(org.jooq.meta.UDTDefinition) DSL.inline(org.jooq.impl.DSL.inline) Record(org.jooq.Record) SettingsTools(org.jooq.conf.SettingsTools) SQLiteMaster(org.jooq.meta.sqlite.sqlite_master.SQLiteMaster) StringUtils(org.jooq.tools.StringUtils) DSL.one(org.jooq.impl.DSL.one) DataDefinitionException(org.jooq.exception.DataDefinitionException) SchemaDefinition(org.jooq.meta.SchemaDefinition) DSL.any(org.jooq.impl.DSL.any) EnumDefinition(org.jooq.meta.EnumDefinition) DSL.table(org.jooq.impl.DSL.table) CommonTableExpression(org.jooq.CommonTableExpression) AbstractDatabase(org.jooq.meta.AbstractDatabase) CatalogDefinition(org.jooq.meta.CatalogDefinition) ParserException(org.jooq.impl.ParserException) SchemaDefinition(org.jooq.meta.SchemaDefinition) DataDefinitionException(org.jooq.exception.DataDefinitionException) Query(org.jooq.Query) DSLContext(org.jooq.DSLContext) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition)

Example 2 with ParserException

use of org.jooq.impl.ParserException in project jOOQ by jOOQ.

the class SQLiteTableDefinition method interpretedTable.

Table<?> interpretedTable() {
    if (interpretedTable == null) {
        try {
            Configuration c = create().configuration().derive();
            c.settings().withParseWithMetaLookups(THROW_ON_FAILURE);
            Query query = create().parser().parseQuery(getSource());
            for (Table<?> t : create().meta(query).getTables(getInputName())) return interpretedTable = t;
        } catch (ParserException e) {
            log.info("Cannot parse SQL: " + getSource(), e);
        } catch (DataDefinitionException e) {
            log.info("Cannot interpret SQL: " + getSource(), e);
        }
    }
    return interpretedTable;
}
Also used : ParserException(org.jooq.impl.ParserException) DataDefinitionException(org.jooq.exception.DataDefinitionException) Configuration(org.jooq.Configuration) Query(org.jooq.Query)

Example 3 with ParserException

use of org.jooq.impl.ParserException in project jOOQ by jOOQ.

the class DDLDatabase method export.

@Override
protected void export() throws Exception {
    Settings defaultSettings = new Settings();
    String scripts = getProperties().getProperty("scripts");
    String encoding = getProperties().getProperty("encoding", "UTF-8");
    String sort = getProperties().getProperty("sort", "semantic").toLowerCase();
    final String defaultNameCase = getProperties().getProperty("defaultNameCase", "as_is").toUpperCase();
    boolean parseIgnoreComments = !"false".equalsIgnoreCase(getProperties().getProperty("parseIgnoreComments"));
    String parseIgnoreCommentStart = getProperties().getProperty("parseIgnoreCommentStart", defaultSettings.getParseIgnoreCommentStart());
    String parseIgnoreCommentStop = getProperties().getProperty("parseIgnoreCommentStop", defaultSettings.getParseIgnoreCommentStop());
    logExecutedQueries = !"false".equalsIgnoreCase(getProperties().getProperty("logExecutedQueries"));
    logExecutionResults = !"false".equalsIgnoreCase(getProperties().getProperty("logExecutionResults"));
    if (isBlank(scripts)) {
        scripts = "";
        log.warn("No scripts defined", "It is recommended that you provide an explicit script directory to scan");
    }
    try {
        final DSLContext ctx = DSL.using(connection(), new Settings().withParseIgnoreComments(parseIgnoreComments).withParseIgnoreCommentStart(parseIgnoreCommentStart).withParseIgnoreCommentStop(parseIgnoreCommentStop).withParseUnknownFunctions(ParseUnknownFunctions.IGNORE));
        // [#7771] [#8011] Ignore all parsed storage clauses when executing the statements
        ctx.data("org.jooq.ddl.ignore-storage-clauses", true);
        // [#8910] Parse things a bit differently for use with the DDLDatabase
        ctx.data("org.jooq.ddl.parse-for-ddldatabase", true);
        if (!"AS_IS".equals(defaultNameCase)) {
            ctx.configuration().set(new DefaultVisitListener() {

                @Override
                public void visitStart(VisitContext vc) {
                    if (vc.queryPart() instanceof Name) {
                        Name n = (Name) vc.queryPart();
                        Name[] parts = n.parts();
                        boolean changed = false;
                        for (int i = 0; i < parts.length; i++) {
                            // flag for DSL.systemName() names
                            if (parts[i].quoted() == Quoted.UNQUOTED) {
                                parts[i] = DSL.quotedName("UPPER".equals(defaultNameCase) ? parts[i].first().toUpperCase(renderLocale(ctx.settings())) : parts[i].first().toLowerCase(renderLocale(ctx.settings())));
                                changed = true;
                            }
                        }
                        if (changed)
                            vc.queryPart(DSL.name(parts));
                    }
                }
            });
        }
        new FilePattern().encoding(encoding).basedir(new File(getBasedir())).pattern(scripts).sort(Sort.of(sort)).load(source -> DDLDatabase.this.load(ctx, source));
    } catch (ParserException e) {
        log.error("An exception occurred while parsing script source : " + scripts + ". Please report this error to https://github.com/jOOQ/jOOQ/issues/new", e);
        throw e;
    }
}
Also used : ParserException(org.jooq.impl.ParserException) VisitContext(org.jooq.VisitContext) DSLContext(org.jooq.DSLContext) FilePattern(org.jooq.FilePattern) DefaultVisitListener(org.jooq.impl.DefaultVisitListener) File(java.io.File) Settings(org.jooq.conf.Settings) Name(org.jooq.Name)

Aggregations

ParserException (org.jooq.impl.ParserException)3 DSLContext (org.jooq.DSLContext)2 Query (org.jooq.Query)2 DataDefinitionException (org.jooq.exception.DataDefinitionException)2 File (java.io.File)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Check (org.jooq.Check)1 CommonTableExpression (org.jooq.CommonTableExpression)1 Configuration (org.jooq.Configuration)1 Field (org.jooq.Field)1 FilePattern (org.jooq.FilePattern)1 ForeignKey (org.jooq.ForeignKey)1 Meta (org.jooq.Meta)1 Name (org.jooq.Name)1 Record (org.jooq.Record)1