Example usage for java.sql DatabaseMetaData getSQLKeywords

List of usage examples for java.sql DatabaseMetaData getSQLKeywords

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData getSQLKeywords.

Prototype

String getSQLKeywords() throws SQLException;

Source Link

Document

Retrieves a comma-separated list of all of this database's SQL keywords that are NOT also SQL:2003 keywords.

Usage

From source file:org.apache.zeppelin.postgresql.SqlCompleter.java

public static Set<String> getSqlKeywordsCompletions(Connection connection) throws IOException, SQLException {

    // Add the default SQL completions
    String keywords = new BufferedReader(
            new InputStreamReader(SqlCompleter.class.getResourceAsStream("/ansi.sql.keywords"))).readLine();

    DatabaseMetaData metaData = connection.getMetaData();

    // Add the driver specific SQL completions
    String driverSpecificKeywords = "/" + metaData.getDriverName().replace(" ", "-").toLowerCase()
            + "-sql.keywords";

    logger.info("JDBC DriverName:" + driverSpecificKeywords);

    if (SqlCompleter.class.getResource(driverSpecificKeywords) != null) {
        String driverKeywords = new BufferedReader(
                new InputStreamReader(SqlCompleter.class.getResourceAsStream(driverSpecificKeywords)))
                        .readLine();//w ww.ja  v  a 2  s . co m
        keywords += "," + driverKeywords.toUpperCase();
    }

    Set<String> completions = new TreeSet<String>();

    // Add the keywords from the current JDBC connection
    try {
        keywords += "," + metaData.getSQLKeywords();
    } catch (Exception e) {
        logger.debug("fail to get SQL key words from database metadata: " + e, e);
    }
    try {
        keywords += "," + metaData.getStringFunctions();
    } catch (Exception e) {
        logger.debug("fail to get string function names from database metadata: " + e, e);
    }
    try {
        keywords += "," + metaData.getNumericFunctions();
    } catch (Exception e) {
        logger.debug("fail to get numeric function names from database metadata: " + e, e);
    }
    try {
        keywords += "," + metaData.getSystemFunctions();
    } catch (Exception e) {
        logger.debug("fail to get system function names from database metadata: " + e, e);
    }
    try {
        keywords += "," + metaData.getTimeDateFunctions();
    } catch (Exception e) {
        logger.debug("fail to get time date function names from database metadata: " + e, e);
    }

    // Also allow lower-case versions of all the keywords
    keywords += "," + keywords.toLowerCase();

    StringTokenizer tok = new StringTokenizer(keywords, ", ");
    while (tok.hasMoreTokens()) {
        completions.add(tok.nextToken());
    }

    return completions;
}

From source file:org.jumpmind.db.sql.JdbcSqlTemplate.java

public Set<String> getSqlKeywords() {
    return execute(new IConnectionCallback<Set<String>>() {
        public Set<String> execute(Connection con) throws SQLException {
            DatabaseMetaData sqlTemplateData = con.getMetaData();
            return new HashSet<String>(Arrays.asList(sqlTemplateData.getSQLKeywords().split(",")));
        }/*w w  w. j  av a2  s  . c o m*/
    });
}

From source file:ro.nextreports.designer.dbviewer.DefaultDBViewer.java

public DBInfo getDBInfo(String schemaName, int mask, Connection con) throws NextSqlException {

    String info = "";
    List<String> keywords = new ArrayList<String>();
    List<DBTable> tables = new ArrayList<DBTable>();
    List<DBProcedure> procedures = new ArrayList<DBProcedure>();
    Dialect dialect;/*from  w  w  w. ja  va2s .  c o  m*/

    try {
        dialect = DialectUtil.getDialect(con);
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new NextSqlException("Could not get Dialect.", ex);
    }

    try {
        DatabaseMetaData dbmd = con.getMetaData();

        if ((mask & DBInfo.INFO) == DBInfo.INFO) {
            StringBuffer sb = new StringBuffer();
            sb.append(I18NSupport.getString("database.product")).append(dbmd.getDatabaseProductName())
                    .append("\r\n");
            sb.append(I18NSupport.getString("database.product.version"))
                    .append(dbmd.getDatabaseProductVersion()).append("\r\n");
            sb.append(I18NSupport.getString("database.driver.name")).append(dbmd.getDriverName())
                    .append("\r\n");
            sb.append(I18NSupport.getString("database.driver.version")).append(dbmd.getDriverVersion())
                    .append("\r\n");
            info = sb.toString();
        }

        if ((mask & DBInfo.SUPPORTED_KEYWORDS) == DBInfo.SUPPORTED_KEYWORDS) {
            StringTokenizer st = new StringTokenizer(dbmd.getSQLKeywords(), ",");
            while (st.hasMoreTokens()) {
                keywords.add(st.nextToken());
            }
        }

        // Get a ResultSet that contains all of the tables in this database
        // We specify a table_type of "TABLE" to prevent seeing system tables,
        // views and so forth
        boolean tableMask = ((mask & DBInfo.TABLES) == DBInfo.TABLES);
        boolean viewMask = ((mask & DBInfo.VIEWS) == DBInfo.VIEWS);
        if (tableMask || viewMask) {
            String[] tableTypes;
            if (tableMask && viewMask) {
                tableTypes = new String[] { "TABLE", "VIEW" };
            } else if (tableMask) {
                tableTypes = new String[] { "TABLE" };
            } else {
                tableTypes = new String[] { "VIEW" };
            }

            String pattern = tableMask ? Globals.getTableNamePattern() : Globals.getViewNamePattern();
            ResultSet allTables = dbmd.getTables(null, schemaName, pattern, tableTypes);
            try {
                while (allTables.next()) {
                    String table_name = allTables.getString("TABLE_NAME");
                    String table_type = allTables.getString("TABLE_TYPE");

                    // discard recycle bin tables
                    String ignoreTablePrefix = dialect.getRecycleBinTablePrefix();
                    if ((table_name == null)
                            || ((ignoreTablePrefix != null) && table_name.startsWith(ignoreTablePrefix))) {
                        continue;
                    }

                    if ((mask & DBInfo.INDEXES) == DBInfo.INDEXES) {
                        ResultSet indexList = null;
                        try {
                            // Get a list of all the indexes for this table
                            indexList = dbmd.getIndexInfo(null, schemaName, table_name, false, false);
                            List<DBIndex> indexes = new ArrayList<DBIndex>();
                            while (indexList.next()) {
                                String index_name = indexList.getString("INDEX_NAME");
                                String column_name = indexList.getString("COLUMN_NAME");
                                if (!index_name.equals("null")) {
                                    DBIndex index = new DBIndex(index_name, column_name);
                                    indexes.add(index);
                                }
                            }
                            DBTable table = new DBTable(schemaName, table_name, table_type, indexes);
                            tables.add(table);

                        } catch (SQLException e) {
                            throw new NextSqlException("SQL Exception: " + e.getMessage(), e);
                        } finally {
                            closeResultSet(indexList);
                        }

                    } else {
                        DBTable table = new DBTable(schemaName, table_name, table_type);
                        tables.add(table);
                    }
                }
            } catch (SQLException e) {
                throw new NextSqlException("SQL Exception: " + e.getMessage(), e);
            } finally {
                closeResultSet(allTables);
            }

        }

        boolean procedureMask = ((mask & DBInfo.PROCEDURES) == DBInfo.PROCEDURES);
        if (procedureMask) {
            String pattern = Globals.getProcedureNamePattern();
            if (pattern == null) {
                pattern = "%";
            }
            ResultSet rs = dbmd.getProcedures(null, schemaName, pattern);
            try {
                while (rs.next()) {
                    String spName = rs.getString("PROCEDURE_NAME");
                    int spType = rs.getInt("PROCEDURE_TYPE");
                    String catalog = rs.getString("PROCEDURE_CAT");
                    //                        System.out.println("Stored Procedure Name: " + spName);
                    //                        if (spType == DatabaseMetaData.procedureReturnsResult) {
                    //                            System.out.println("procedure Returns Result");
                    //                        } else if (spType == DatabaseMetaData.procedureNoResult) {
                    //                            System.out.println("procedure No Result");
                    //                        } else {
                    //                            System.out.println("procedure Result unknown");
                    //                        }
                    procedures.add(new DBProcedure(schemaName, catalog, spName, spType));
                }
            } catch (SQLException e) {
                throw new NextSqlException("SQL Exception: " + e.getMessage(), e);
            } finally {
                closeResultSet(rs);
            }
        }

    } catch (SQLException e) {
        LOG.error(e.getMessage(), e);
        e.printStackTrace();
        throw new NextSqlException("SQL Exception: " + e.getMessage(), e);
    }

    return new DBInfo(info, tables, procedures, keywords);
}