Search in sources :

Example 6 with Function

use of org.h2.expression.Function 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 7 with Function

use of org.h2.expression.Function 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 8 with Function

use of org.h2.expression.Function in project h2database by h2database.

the class FullText method init.

/**
 * Initializes full text search functionality for this database. This adds
 * the following Java functions to the database:
 * <ul>
 * <li>FT_CREATE_INDEX(schemaNameString, tableNameString,
 * columnListString)</li>
 * <li>FT_SEARCH(queryString, limitInt, offsetInt): result set</li>
 * <li>FT_REINDEX()</li>
 * <li>FT_DROP_ALL()</li>
 * </ul>
 * It also adds a schema FT to the database where bookkeeping information
 * is stored. This function may be called from a Java application, or by
 * using the SQL statements:
 *
 * <pre>
 * CREATE ALIAS IF NOT EXISTS FT_INIT FOR
 *      &quot;org.h2.fulltext.FullText.init&quot;;
 * CALL FT_INIT();
 * </pre>
 *
 * @param conn the connection
 */
public static void init(Connection conn) throws SQLException {
    Statement stat = conn.createStatement();
    stat.execute("CREATE SCHEMA IF NOT EXISTS " + SCHEMA);
    stat.execute("CREATE TABLE IF NOT EXISTS " + SCHEMA + ".INDEXES(ID INT AUTO_INCREMENT PRIMARY KEY, " + "SCHEMA VARCHAR, TABLE VARCHAR, COLUMNS VARCHAR, " + "UNIQUE(SCHEMA, TABLE))");
    stat.execute("CREATE TABLE IF NOT EXISTS " + SCHEMA + ".WORDS(ID INT AUTO_INCREMENT PRIMARY KEY, " + "NAME VARCHAR, UNIQUE(NAME))");
    stat.execute("CREATE TABLE IF NOT EXISTS " + SCHEMA + ".ROWS(ID IDENTITY, HASH INT, INDEXID INT, " + "KEY VARCHAR, UNIQUE(HASH, INDEXID, KEY))");
    stat.execute("CREATE TABLE IF NOT EXISTS " + SCHEMA + ".MAP(ROWID INT, WORDID INT, PRIMARY KEY(WORDID, ROWID))");
    stat.execute("CREATE TABLE IF NOT EXISTS " + SCHEMA + ".IGNORELIST(LIST VARCHAR)");
    stat.execute("CREATE TABLE IF NOT EXISTS " + SCHEMA + ".SETTINGS(KEY VARCHAR PRIMARY KEY, VALUE VARCHAR)");
    stat.execute("CREATE ALIAS IF NOT EXISTS FT_CREATE_INDEX FOR \"" + FullText.class.getName() + ".createIndex\"");
    stat.execute("CREATE ALIAS IF NOT EXISTS FT_DROP_INDEX FOR \"" + FullText.class.getName() + ".dropIndex\"");
    stat.execute("CREATE ALIAS IF NOT EXISTS FT_SEARCH FOR \"" + FullText.class.getName() + ".search\"");
    stat.execute("CREATE ALIAS IF NOT EXISTS FT_SEARCH_DATA FOR \"" + FullText.class.getName() + ".searchData\"");
    stat.execute("CREATE ALIAS IF NOT EXISTS FT_REINDEX FOR \"" + FullText.class.getName() + ".reindex\"");
    stat.execute("CREATE ALIAS IF NOT EXISTS FT_DROP_ALL FOR \"" + FullText.class.getName() + ".dropAll\"");
    FullTextSettings setting = FullTextSettings.getInstance(conn);
    ResultSet rs = stat.executeQuery("SELECT * FROM " + SCHEMA + ".IGNORELIST");
    while (rs.next()) {
        String commaSeparatedList = rs.getString(1);
        setIgnoreList(setting, commaSeparatedList);
    }
    rs = stat.executeQuery("SELECT * FROM " + SCHEMA + ".SETTINGS");
    while (rs.next()) {
        String key = rs.getString(1);
        if ("whitespaceChars".equals(key)) {
            String value = rs.getString(2);
            setting.setWhitespaceChars(value);
        }
    }
    rs = stat.executeQuery("SELECT * FROM " + SCHEMA + ".WORDS");
    while (rs.next()) {
        String word = rs.getString("NAME");
        int id = rs.getInt("ID");
        word = setting.convertWord(word);
        if (word != null) {
            setting.addWord(word, id);
        }
    }
    setting.setInitialized(true);
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 9 with Function

use of org.h2.expression.Function in project h2database by h2database.

the class FunctionMultiReturn method polar2Cartesian.

/**
 * Convert polar coordinates to cartesian coordinates. The function may be
 * called twice, once to retrieve the result columns (with null parameters),
 * and the second time to return the data.
 *
 * @param r the distance from the point 0/0
 * @param alpha the angle
 * @return a result set with two columns: x and y
 */
public static ResultSet polar2Cartesian(Double r, Double alpha) {
    SimpleResultSet rs = new SimpleResultSet();
    rs.addColumn("X", Types.DOUBLE, 0, 0);
    rs.addColumn("Y", Types.DOUBLE, 0, 0);
    if (r != null && alpha != null) {
        double x = r.doubleValue() * Math.cos(alpha.doubleValue());
        double y = r.doubleValue() * Math.sin(alpha.doubleValue());
        rs.addRow(x, y);
    }
    return rs;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet)

Example 10 with Function

use of org.h2.expression.Function in project h2database by h2database.

the class FunctionMultiReturn method polar2CartesianSet.

/**
 * Convert a set of polar coordinates to cartesian coordinates. The function
 * may be called twice, once to retrieve the result columns (with null
 * parameters), and the second time to return the data.
 *
 * @param conn the connection
 * @param query the query
 * @return a result set with the coordinates
 */
public static ResultSet polar2CartesianSet(Connection conn, String query) throws SQLException {
    SimpleResultSet result = new SimpleResultSet();
    result.addColumn("R", Types.DOUBLE, 0, 0);
    result.addColumn("A", Types.DOUBLE, 0, 0);
    result.addColumn("X", Types.DOUBLE, 0, 0);
    result.addColumn("Y", Types.DOUBLE, 0, 0);
    if (query != null) {
        ResultSet rs = conn.createStatement().executeQuery(query);
        while (rs.next()) {
            double r = rs.getDouble("R");
            double alpha = rs.getDouble("A");
            double x = r * Math.cos(alpha);
            double y = r * Math.sin(alpha);
            result.addRow(r, alpha, x, y);
        }
    }
    return result;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet)

Aggregations

SimpleResultSet (org.h2.tools.SimpleResultSet)10 ResultSet (java.sql.ResultSet)9 Function (org.h2.expression.Function)8 JavaFunction (org.h2.expression.JavaFunction)8 TableFunction (org.h2.expression.TableFunction)8 Expression (org.h2.expression.Expression)7 ValueExpression (org.h2.expression.ValueExpression)7 Column (org.h2.table.Column)7 Statement (java.sql.Statement)6 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)6 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 IndexColumn (org.h2.table.IndexColumn)6 SQLException (java.sql.SQLException)5 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)5 PreparedStatement (java.sql.PreparedStatement)4 ValueString (org.h2.value.ValueString)4 Connection (java.sql.Connection)3 ArrayList (java.util.ArrayList)3 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)3