Search in sources :

Example 1 with Bnf

use of org.h2.bnf.Bnf in project jackrabbit-oak by apache.

the class BnfSyntax method getLink.

/**
 * Get the HTML link to the given token.
 *
 * @param bnf the BNF
 * @param token the token
 * @return the HTML link
 */
String getLink(Bnf bnf, String token) {
    RuleHead found = null;
    String key = Bnf.getRuleMapKey(token);
    for (int i = 0; i < token.length(); i++) {
        String test = StringUtils.toLowerEnglish(key.substring(i));
        RuleHead r = bnf.getRuleHead(test);
        if (r != null) {
            found = r;
            break;
        }
    }
    if (found == null) {
        return token;
    }
    if (found.getRule() instanceof RuleFixed) {
        found.getRule().accept(this);
        return html;
    }
    String link = found.getTopic().toLowerCase().replace(' ', '_');
    link = "#" + StringUtils.urlEncode(link);
    return "<a href=\"" + link + "\">" + token + "</a>";
}
Also used : RuleFixed(org.h2.bnf.RuleFixed) RuleHead(org.h2.bnf.RuleHead)

Example 2 with Bnf

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

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

the class Railroads method map.

private void map(String key, ResultSet rs, boolean railroads) throws Exception {
    ArrayList<HashMap<String, String>> list;
    list = new ArrayList<>();
    while (rs.next()) {
        HashMap<String, String> map = new HashMap<>();
        ResultSetMetaData meta = rs.getMetaData();
        for (int i = 0; i < meta.getColumnCount(); i++) {
            String k = StringUtils.toLowerEnglish(meta.getColumnLabel(i + 1));
            String value = rs.getString(i + 1);
            value = value.trim();
            map.put(k, PageParser.escapeHtml(value));
        }
        String topic = rs.getString("TOPIC");
        String syntax = rs.getString("SYNTAX").trim();
        if (railroads) {
            BnfRailroad r = new BnfRailroad();
            String railroad = r.getHtml(bnf, syntax);
            map.put("railroad", railroad);
        }
        BnfSyntax visitor = new BnfSyntax();
        String syntaxHtml = visitor.getHtml(bnf, syntax);
        map.put("syntax", syntaxHtml);
        // remove newlines in the regular text
        String text = map.get("text");
        if (text != null) {
            // text is enclosed in <p> .. </p> so this works.
            text = StringUtils.replaceAll(text, "<br /><br />", "</p><p>");
            text = StringUtils.replaceAll(text, "<br />", " ");
            map.put("text", text);
        }
        String link = topic.toLowerCase();
        link = link.replace(' ', '_');
        // link = StringUtils.replaceAll(link, "_", "");
        link = link.replace('@', '_');
        map.put("link", StringUtils.urlEncode(link));
        list.add(map);
    }
    session.put(key, list);
    int div = 3;
    int part = (list.size() + div - 1) / div;
    for (int i = 0, start = 0; i < div; i++, start += part) {
        List<HashMap<String, String>> listThird = list.subList(start, Math.min(start + part, list.size()));
        session.put(key + "-" + i, listThird);
    }
    rs.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) HashMap(java.util.HashMap) BnfRailroad(org.h2.build.doc.BnfRailroad) BnfSyntax(org.h2.build.doc.BnfSyntax)

Example 4 with Bnf

use of org.h2.bnf.Bnf 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 5 with Bnf

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

the class BnfSyntax method getLink.

/**
 * Get the HTML link to the given token.
 *
 * @param bnf the BNF
 * @param token the token
 * @return the HTML link
 */
String getLink(Bnf bnf, String token) {
    RuleHead found = null;
    String key = Bnf.getRuleMapKey(token);
    for (int i = 0; i < token.length(); i++) {
        String test = StringUtils.toLowerEnglish(key.substring(i));
        RuleHead r = bnf.getRuleHead(test);
        if (r != null) {
            found = r;
            break;
        }
    }
    if (found == null) {
        return token;
    }
    String page = "grammar.html";
    if (found.getSection().startsWith("Data Types")) {
        page = "datatypes.html";
    } else if (found.getSection().startsWith("Functions")) {
        page = "functions.html";
    } else if (token.equals("@func@")) {
        return "<a href=\"functions.html\">Function</a>";
    } else if (found.getRule() instanceof RuleFixed) {
        found.getRule().accept(this);
        return html;
    }
    String link = found.getTopic().toLowerCase().replace(' ', '_');
    link = page + "#" + StringUtils.urlEncode(link);
    return "<a href=\"" + link + "\">" + token + "</a>";
}
Also used : RuleFixed(org.h2.bnf.RuleFixed) RuleHead(org.h2.bnf.RuleHead)

Aggregations

Bnf (org.h2.bnf.Bnf)3 HashMap (java.util.HashMap)2 RuleFixed (org.h2.bnf.RuleFixed)2 RuleHead (org.h2.bnf.RuleHead)2 DbContextRule (org.h2.bnf.context.DbContextRule)2 StringReader (java.io.StringReader)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 DbContents (org.h2.bnf.context.DbContents)1 DbProcedure (org.h2.bnf.context.DbProcedure)1 DbSchema (org.h2.bnf.context.DbSchema)1 BnfRailroad (org.h2.build.doc.BnfRailroad)1 BnfSyntax (org.h2.build.doc.BnfSyntax)1 DbException (org.h2.message.DbException)1 Csv (org.h2.tools.Csv)1 ScriptReader (org.h2.util.ScriptReader)1