Search in sources :

Example 1 with DbColumn

use of org.h2.bnf.context.DbColumn 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 DbColumn

use of org.h2.bnf.context.DbColumn in project elastic-core-maven by OrdinaryDude.

the class FullTextTrigger method reindexTable.

/**
 * Reindex the table
 *
 * @param   conn                SQL connection
 * @throws  SQLException        Unable to reindex table
 */
private void reindexTable(Connection conn) throws SQLException {
    if (indexColumns.isEmpty()) {
        return;
    }
    // 
    // Build the SELECT statement for just the indexed columns
    // 
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT DB_ID");
    for (int index : indexColumns) {
        sb.append(", ").append(columnNames.get(index));
    }
    sb.append(" FROM ").append(tableName);
    Object[] row = new Object[columnNames.size()];
    // 
    try (Statement qstmt = conn.createStatement();
        ResultSet rs = qstmt.executeQuery(sb.toString())) {
        while (rs.next()) {
            row[dbColumn] = rs.getObject(1);
            int i = 2;
            for (int index : indexColumns) {
                row[index] = rs.getObject(i++);
            }
            indexRow(row);
        }
    }
    // 
    // Commit the index updates
    // 
    commitIndex();
}
Also used : Statement(java.sql.Statement) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 3 with DbColumn

use of org.h2.bnf.context.DbColumn in project elastic-core-maven by OrdinaryDude.

the class FullTextTrigger method init.

/**
 * Initialize the trigger (Trigger interface)
 *
 * @param   conn                Database connection
 * @param   schema              Database schema name
 * @param   trigger             Database trigger name
 * @param   table               Database table name
 * @param   before              TRUE if trigger is called before database operation
 * @param   type                Trigger type
 * @throws  SQLException        A SQL error occurred
 */
@Override
public void init(Connection conn, String schema, String trigger, String table, boolean before, int type) throws SQLException {
    // 
    if (!isActive || table.contains("_COPY_")) {
        return;
    }
    // 
    // Access the Lucene index
    // 
    // We need to get the access just once, either in a trigger or in a function alias
    // 
    getIndexAccess(conn);
    // 
    // Get table and index information
    // 
    tableName = schema + "." + table;
    try (Statement stmt = conn.createStatement()) {
        // 
        try (ResultSet rs = stmt.executeQuery("SHOW COLUMNS FROM " + table + " FROM " + schema)) {
            int index = 0;
            while (rs.next()) {
                String columnName = rs.getString("FIELD");
                String columnType = rs.getString("TYPE");
                columnType = columnType.substring(0, columnType.indexOf('('));
                columnNames.add(columnName);
                columnTypes.add(columnType);
                if (columnName.equals("DB_ID")) {
                    dbColumn = index;
                }
                index++;
            }
        }
        if (dbColumn < 0) {
            Logger.logErrorMessage("DB_ID column not found for table " + tableName);
            return;
        }
        // 
        try (ResultSet rs = stmt.executeQuery(String.format("SELECT COLUMNS FROM FTL.INDEXES WHERE SCHEMA = '%s' AND TABLE = '%s'", schema, table))) {
            if (rs.next()) {
                String[] columns = rs.getString(1).split(",");
                for (String column : columns) {
                    int pos = columnNames.indexOf(column);
                    if (pos >= 0) {
                        if (columnTypes.get(pos).equals("VARCHAR")) {
                            indexColumns.add(pos);
                        } else {
                            Logger.logErrorMessage("Indexed column " + column + " in table " + tableName + " is not a string");
                        }
                    } else {
                        Logger.logErrorMessage("Indexed column " + column + " not found in table " + tableName);
                    }
                }
            }
        }
        if (indexColumns.isEmpty()) {
            Logger.logErrorMessage("No indexed columns found for table " + tableName);
            return;
        }
        // 
        // Trigger is enabled
        // 
        isEnabled = true;
        indexTriggers.put(tableName, this);
    } catch (SQLException exc) {
        Logger.logErrorMessage("Unable to get table information", exc);
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 4 with DbColumn

use of org.h2.bnf.context.DbColumn in project h2database by h2database.

the class WebApp method addColumns.

private static int addColumns(boolean mainSchema, DbTableOrView table, StringBuilder buff, int treeIndex, boolean showColumnTypes, StringBuilder columnsBuffer) {
    DbColumn[] columns = table.getColumns();
    for (int i = 0; columns != null && i < columns.length; i++) {
        DbColumn column = columns[i];
        if (columnsBuffer.length() > 0) {
            columnsBuffer.append(' ');
        }
        columnsBuffer.append(column.getName());
        String col = escapeIdentifier(column.getName());
        String level = mainSchema ? ", 1, 1" : ", 2, 2";
        buff.append("setNode(").append(treeIndex).append(level).append(", 'column', '").append(PageParser.escapeJavaScript(column.getName())).append("', 'javascript:ins(\\'").append(col).append("\\')');\n");
        treeIndex++;
        if (mainSchema && showColumnTypes) {
            buff.append("setNode(").append(treeIndex).append(", 2, 2, 'type', '").append(PageParser.escapeJavaScript(column.getDataType())).append("', null);\n");
            treeIndex++;
        }
    }
    return treeIndex;
}
Also used : DbColumn(org.h2.bnf.context.DbColumn)

Aggregations

ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 SimpleResultSet (org.h2.tools.SimpleResultSet)2 SQLException (java.sql.SQLException)1 RuleElement (org.h2.bnf.RuleElement)1 RuleList (org.h2.bnf.RuleList)1 DbColumn (org.h2.bnf.context.DbColumn)1