Search in sources :

Example 1 with Procedure

use of org.h2.engine.Procedure 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 Procedure

use of org.h2.engine.Procedure in project h2database by h2database.

the class WebSession method loadBnf.

/**
 * Load the SQL grammar BNF.
 */
void loadBnf() {
    try {
        Bnf newBnf = Bnf.getInstance(null);
        DbContextRule columnRule = new DbContextRule(contents, DbContextRule.COLUMN);
        DbContextRule newAliasRule = new DbContextRule(contents, DbContextRule.NEW_TABLE_ALIAS);
        DbContextRule aliasRule = new DbContextRule(contents, DbContextRule.TABLE_ALIAS);
        DbContextRule tableRule = new DbContextRule(contents, DbContextRule.TABLE);
        DbContextRule schemaRule = new DbContextRule(contents, DbContextRule.SCHEMA);
        DbContextRule columnAliasRule = new DbContextRule(contents, DbContextRule.COLUMN_ALIAS);
        DbContextRule procedure = new DbContextRule(contents, DbContextRule.PROCEDURE);
        newBnf.updateTopic("procedure", procedure);
        newBnf.updateTopic("column_name", columnRule);
        newBnf.updateTopic("new_table_alias", newAliasRule);
        newBnf.updateTopic("table_alias", aliasRule);
        newBnf.updateTopic("column_alias", columnAliasRule);
        newBnf.updateTopic("table_name", tableRule);
        newBnf.updateTopic("schema_name", schemaRule);
        newBnf.linkStatements();
        bnf = newBnf;
    } catch (Exception e) {
        // ok we don't have the bnf
        server.traceError(e);
    }
}
Also used : Bnf(org.h2.bnf.Bnf) DbContextRule(org.h2.bnf.context.DbContextRule) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 3 with Procedure

use of org.h2.engine.Procedure in project h2database by h2database.

the class TestBnf method testProcedures.

private void testProcedures(Connection conn, boolean isMySQLMode) throws Exception {
    // Register a procedure and check if it is present in DbContents
    conn.createStatement().execute("DROP ALIAS IF EXISTS CUSTOM_PRINT");
    conn.createStatement().execute("CREATE ALIAS CUSTOM_PRINT " + "AS $$ void print(String s) { System.out.println(s); } $$");
    conn.createStatement().execute("DROP TABLE IF EXISTS " + "TABLE_WITH_STRING_FIELD");
    conn.createStatement().execute("CREATE TABLE " + "TABLE_WITH_STRING_FIELD (STRING_FIELD VARCHAR(50), INT_FIELD integer)");
    DbContents dbContents = new DbContents();
    dbContents.readContents("jdbc:h2:test", conn);
    assertTrue(dbContents.isH2());
    assertFalse(dbContents.isDerby());
    assertFalse(dbContents.isFirebird());
    assertEquals(null, dbContents.quoteIdentifier(null));
    if (isMySQLMode) {
        assertTrue(dbContents.isH2ModeMySQL());
        assertEquals("TEST", dbContents.quoteIdentifier("TEST"));
        assertEquals("TEST", dbContents.quoteIdentifier("Test"));
        assertEquals("TEST", dbContents.quoteIdentifier("test"));
    } else {
        assertFalse(dbContents.isH2ModeMySQL());
        assertEquals("TEST", dbContents.quoteIdentifier("TEST"));
        assertEquals("\"Test\"", dbContents.quoteIdentifier("Test"));
        assertEquals("\"test\"", dbContents.quoteIdentifier("test"));
    }
    assertFalse(dbContents.isMSSQLServer());
    assertFalse(dbContents.isMySQL());
    assertFalse(dbContents.isOracle());
    assertFalse(dbContents.isPostgreSQL());
    assertFalse(dbContents.isSQLite());
    DbSchema defaultSchema = dbContents.getDefaultSchema();
    DbProcedure[] procedures = defaultSchema.getProcedures();
    Set<String> procedureName = new HashSet<>(procedures.length);
    for (DbProcedure procedure : procedures) {
        assertTrue(defaultSchema == procedure.getSchema());
        procedureName.add(procedure.getName());
    }
    if (isMySQLMode) {
        assertTrue(procedureName.contains("custom_print"));
    } else {
        assertTrue(procedureName.contains("CUSTOM_PRINT"));
    }
    if (isMySQLMode) {
        return;
    }
    // Test completion
    Bnf bnf = Bnf.getInstance(null);
    DbContextRule columnRule = new DbContextRule(dbContents, DbContextRule.COLUMN);
    bnf.updateTopic("column_name", columnRule);
    bnf.updateTopic("user_defined_function_name", new DbContextRule(dbContents, DbContextRule.PROCEDURE));
    bnf.linkStatements();
    // Test partial
    Map<String, String> tokens;
    tokens = bnf.getNextTokenList("SELECT CUSTOM_PR");
    assertTrue(tokens.values().contains("INT"));
    // Test identifiers are working
    tokens = bnf.getNextTokenList("create table \"test\" as s" + "el");
    assertTrue(tokens.values().contains("E" + "CT"));
    tokens = bnf.getNextTokenList("create table test as s" + "el");
    assertTrue(tokens.values().contains("E" + "CT"));
    // Test || with and without spaces
    tokens = bnf.getNextTokenList("select 1||f");
    assertFalse(tokens.values().contains("R" + "OM"));
    tokens = bnf.getNextTokenList("select 1 || f");
    assertFalse(tokens.values().contains("R" + "OM"));
    tokens = bnf.getNextTokenList("select 1 || 2 ");
    assertTrue(tokens.values().contains("FROM"));
    tokens = bnf.getNextTokenList("select 1||2");
    assertTrue(tokens.values().contains("FROM"));
    tokens = bnf.getNextTokenList("select 1 || 2");
    assertTrue(tokens.values().contains("FROM"));
    // Test keyword
    tokens = bnf.getNextTokenList("SELECT LE" + "AS");
    assertTrue(tokens.values().contains("T"));
    // Test parameters
    tokens = bnf.getNextTokenList("SELECT CUSTOM_PRINT(");
    assertTrue(tokens.values().contains("STRING_FIELD"));
    assertFalse(tokens.values().contains("INT_FIELD"));
    // Test parameters with spaces
    tokens = bnf.getNextTokenList("SELECT CUSTOM_PRINT ( ");
    assertTrue(tokens.values().contains("STRING_FIELD"));
    assertFalse(tokens.values().contains("INT_FIELD"));
    // Test parameters with close bracket
    tokens = bnf.getNextTokenList("SELECT CUSTOM_PRINT ( STRING_FIELD");
    assertTrue(tokens.values().contains(")"));
}
Also used : DbProcedure(org.h2.bnf.context.DbProcedure) DbContents(org.h2.bnf.context.DbContents) DbSchema(org.h2.bnf.context.DbSchema) Bnf(org.h2.bnf.Bnf) DbContextRule(org.h2.bnf.context.DbContextRule) HashSet(java.util.HashSet)

Example 4 with Procedure

use of org.h2.engine.Procedure in project h2database by h2database.

the class Parser method parseExecute.

private Prepared parseExecute() {
    ExecuteProcedure command = new ExecuteProcedure(session);
    String procedureName = readAliasIdentifier();
    Procedure p = session.getProcedure(procedureName);
    if (p == null) {
        throw DbException.get(ErrorCode.FUNCTION_ALIAS_NOT_FOUND_1, procedureName);
    }
    command.setProcedure(p);
    if (readIf("(")) {
        for (int i = 0; ; i++) {
            command.setExpression(i, readExpression());
            if (readIf(")")) {
                break;
            }
            read(",");
        }
    }
    return command;
}
Also used : ExecuteProcedure(org.h2.command.dml.ExecuteProcedure) Procedure(org.h2.engine.Procedure) DeallocateProcedure(org.h2.command.ddl.DeallocateProcedure) ExecuteProcedure(org.h2.command.dml.ExecuteProcedure) PrepareProcedure(org.h2.command.ddl.PrepareProcedure) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 5 with Procedure

use of org.h2.engine.Procedure in project h2database by h2database.

the class PrepareProcedure method update.

@Override
public int update() {
    Procedure proc = new Procedure(procedureName, prepared);
    prepared.setParameterList(parameters);
    prepared.setPrepareAlways(prepareAlways);
    prepared.prepare();
    session.addProcedure(proc);
    return 0;
}
Also used : Procedure(org.h2.engine.Procedure)

Aggregations

SQLException (java.sql.SQLException)3 DbException (org.h2.message.DbException)3 PreparedStatement (java.sql.PreparedStatement)2 Bnf (org.h2.bnf.Bnf)2 DbContextRule (org.h2.bnf.context.DbContextRule)2 Procedure (org.h2.engine.Procedure)2 HashSet (java.util.HashSet)1 RuleElement (org.h2.bnf.RuleElement)1 RuleList (org.h2.bnf.RuleList)1 DbContents (org.h2.bnf.context.DbContents)1 DbProcedure (org.h2.bnf.context.DbProcedure)1 DbSchema (org.h2.bnf.context.DbSchema)1 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)1 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)1 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)1 DeallocateProcedure (org.h2.command.ddl.DeallocateProcedure)1 PrepareProcedure (org.h2.command.ddl.PrepareProcedure)1 ExecuteProcedure (org.h2.command.dml.ExecuteProcedure)1 ValueString (org.h2.value.ValueString)1