Search in sources :

Example 1 with RuleList

use of org.h2.bnf.RuleList in project h2database by h2database.

the class DbContextRule method autoCompleteProcedure.

private void autoCompleteProcedure(Sentence sentence) {
    DbSchema schema = sentence.getLastMatchedSchema();
    if (schema == null) {
        schema = contents.getDefaultSchema();
    }
    String incompleteSentence = sentence.getQueryUpper();
    String incompleteFunctionName = incompleteSentence;
    if (incompleteSentence.contains("(")) {
        incompleteFunctionName = incompleteSentence.substring(0, incompleteSentence.indexOf('(')).trim();
    }
    // Common elements
    RuleElement openBracket = new RuleElement("(", "Function");
    RuleElement closeBracket = new RuleElement(")", "Function");
    RuleElement comma = new RuleElement(",", "Function");
    // Fetch all elements
    for (DbProcedure procedure : schema.getProcedures()) {
        final String procName = procedure.getName();
        if (procName.startsWith(incompleteFunctionName)) {
            // That's it, build a RuleList from this function
            RuleElement procedureElement = new RuleElement(procName, "Function");
            RuleList rl = new RuleList(procedureElement, openBracket, false);
            // Go further only if the user use open bracket
            if (incompleteSentence.contains("(")) {
                for (DbColumn parameter : procedure.getParameters()) {
                    if (parameter.getPosition() > 1) {
                        rl = new RuleList(rl, comma, false);
                    }
                    DbContextRule columnRule = new DbContextRule(contents, COLUMN);
                    String parameterType = parameter.getDataType();
                    // Remove precision
                    if (parameterType.contains("(")) {
                        parameterType = parameterType.substring(0, parameterType.indexOf('('));
                    }
                    columnRule.setColumnType(parameterType);
                    rl = new RuleList(rl, columnRule, false);
                }
                rl = new RuleList(rl, closeBracket, false);
            }
            rl.autoComplete(sentence);
        }
    }
}
Also used : RuleList(org.h2.bnf.RuleList) RuleElement(org.h2.bnf.RuleElement)

Example 2 with RuleList

use of org.h2.bnf.RuleList in project h2database by h2database.

the class Bnf method parse.

private void parse(Reader reader) throws SQLException, IOException {
    Rule functions = null;
    statements = New.arrayList();
    Csv csv = new Csv();
    csv.setLineCommentCharacter('#');
    ResultSet rs = csv.read(reader, null);
    while (rs.next()) {
        String section = rs.getString("SECTION").trim();
        if (section.startsWith("System")) {
            continue;
        }
        String topic = rs.getString("TOPIC");
        syntax = rs.getString("SYNTAX").trim();
        currentTopic = section;
        tokens = tokenize();
        index = 0;
        Rule rule = parseRule();
        if (section.startsWith("Command")) {
            rule = new RuleList(rule, new RuleElement(";\n\n", currentTopic), false);
        }
        RuleHead head = addRule(topic, section, rule);
        if (section.startsWith("Function")) {
            if (functions == null) {
                functions = rule;
            } else {
                functions = new RuleList(rule, functions, true);
            }
        } else if (section.startsWith("Commands")) {
            statements.add(head);
        }
    }
    addRule("@func@", "Function", functions);
    addFixedRule("@ymd@", RuleFixed.YMD);
    addFixedRule("@hms@", RuleFixed.HMS);
    addFixedRule("@nanos@", RuleFixed.NANOS);
    addFixedRule("anything_except_single_quote", RuleFixed.ANY_EXCEPT_SINGLE_QUOTE);
    addFixedRule("anything_except_double_quote", RuleFixed.ANY_EXCEPT_DOUBLE_QUOTE);
    addFixedRule("anything_until_end_of_line", RuleFixed.ANY_UNTIL_EOL);
    addFixedRule("anything_until_end_comment", RuleFixed.ANY_UNTIL_END);
    addFixedRule("anything_except_two_dollar_signs", RuleFixed.ANY_EXCEPT_2_DOLLAR);
    addFixedRule("anything", RuleFixed.ANY_WORD);
    addFixedRule("@hex_start@", RuleFixed.HEX_START);
    addFixedRule("@concat@", RuleFixed.CONCAT);
    addFixedRule("@az_@", RuleFixed.AZ_UNDERSCORE);
    addFixedRule("@af@", RuleFixed.AF);
    addFixedRule("@digit@", RuleFixed.DIGIT);
    addFixedRule("@open_bracket@", RuleFixed.OPEN_BRACKET);
    addFixedRule("@close_bracket@", RuleFixed.CLOSE_BRACKET);
}
Also used : Csv(org.h2.tools.Csv) ResultSet(java.sql.ResultSet) DbContextRule(org.h2.bnf.context.DbContextRule)

Example 3 with RuleList

use of org.h2.bnf.RuleList in project h2database by h2database.

the class Bnf method parseToken.

private Rule parseToken() {
    Rule r;
    if ((firstChar >= 'A' && firstChar <= 'Z') || (firstChar >= 'a' && firstChar <= 'z')) {
        // r = new RuleElement(currentToken+ " syntax:" + syntax);
        r = new RuleElement(currentToken, currentTopic);
    } else if (firstChar == '[') {
        read();
        Rule r2 = parseOr();
        r = new RuleOptional(r2);
        if (firstChar != ']') {
            throw new AssertionError("expected ], got " + currentToken + " syntax:" + syntax);
        }
    } else if (firstChar == '{') {
        read();
        r = parseOr();
        if (firstChar != '}') {
            throw new AssertionError("expected }, got " + currentToken + " syntax:" + syntax);
        }
    } else if ("@commaDots@".equals(currentToken)) {
        r = new RuleList(new RuleElement(",", currentTopic), lastRepeat, false);
        r = new RuleRepeat(r, true);
    } else if ("@dots@".equals(currentToken)) {
        r = new RuleRepeat(lastRepeat, false);
    } else {
        r = new RuleElement(currentToken, currentTopic);
    }
    lastRepeat = r;
    read();
    return r;
}
Also used : DbContextRule(org.h2.bnf.context.DbContextRule)

Example 4 with RuleList

use of org.h2.bnf.RuleList in project h2database by h2database.

the class Bnf method parseOr.

private Rule parseOr() {
    Rule r = parseList();
    if (firstChar == '|') {
        read();
        r = new RuleList(r, parseOr(), true);
    }
    lastRepeat = r;
    return r;
}
Also used : DbContextRule(org.h2.bnf.context.DbContextRule)

Example 5 with RuleList

use of org.h2.bnf.RuleList in project h2database by h2database.

the class Bnf method parseList.

private Rule parseList() {
    Rule r = parseToken();
    if (firstChar != '|' && firstChar != ']' && firstChar != '}' && firstChar != 0) {
        r = new RuleList(r, parseList(), false);
    }
    lastRepeat = r;
    return r;
}
Also used : DbContextRule(org.h2.bnf.context.DbContextRule)

Aggregations

DbContextRule (org.h2.bnf.context.DbContextRule)4 ResultSet (java.sql.ResultSet)1 RuleElement (org.h2.bnf.RuleElement)1 RuleList (org.h2.bnf.RuleList)1 Csv (org.h2.tools.Csv)1