Example usage for java.sql DatabaseMetaData bestRowSession

List of usage examples for java.sql DatabaseMetaData bestRowSession

Introduction

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

Prototype

int bestRowSession

To view the source code for java.sql DatabaseMetaData bestRowSession.

Click Source Link

Document

Indicates that the scope of the best row identifier is the remainder of the current session.

Usage

From source file:io.vitess.jdbc.VitessMySQLDatabaseMetadata.java

@SuppressWarnings("StringBufferReplaceableByString")
public ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable)
        throws SQLException {
    ResultSet resultSet = null;/* w  w w.j a  va  2s. c o  m*/
    VitessStatement vitessStatement = new VitessStatement(this.connection);

    if (null == table) {
        throw new SQLException("Table Parameter cannot be null in getBestRowIdentifier");
    }

    String[] columnName = new String[] { "SCOPE", "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME", "COLUMN_SIZE",
            "BUFFER_LENGTH", "DECIMAL_DIGITS", "PSEUDO_COLUMN" };

    Query.Type[] columnType = new Query.Type[] { Query.Type.INT16, Query.Type.CHAR, Query.Type.INT32,
            Query.Type.CHAR, Query.Type.INT32, Query.Type.INT32, Query.Type.INT16, Query.Type.INT16 };

    ArrayList<ArrayList<String>> data = new ArrayList<>();

    try {
        resultSet = vitessStatement.executeQuery("SHOW COLUMNS FROM " + this.quotedId + table + this.quotedId
                + "" + " FROM " + this.quotedId + catalog + this.quotedId);

        while (resultSet.next()) {
            ArrayList<String> row = new ArrayList<>();
            String keyType = resultSet.getString("Key");
            if (keyType != null && StringUtils.startsWithIgnoreCase(keyType, "PRI")) {
                row.add(Integer.toString(DatabaseMetaData.bestRowSession));
                row.add(resultSet.getString("Field"));
                String type = resultSet.getString("Type");
                int size = this.maxBufferSize;
                int decimals = 0;
                if (type.indexOf("enum") != -1) {
                    String temp = type.substring(type.indexOf("("), type.indexOf(")"));
                    StringTokenizer tokenizer = new StringTokenizer(temp, ",");

                    int maxLength;
                    for (maxLength = 0; tokenizer.hasMoreTokens(); maxLength = Math.max(maxLength,
                            tokenizer.nextToken().length() - 2))
                        ;

                    size = maxLength;
                    decimals = 0;
                    type = "enum";
                } else if (type.indexOf("(") != -1) {
                    if (type.indexOf(",") != -1) {
                        size = Integer.parseInt(type.substring(type.indexOf("(") + 1, type.indexOf(",")));
                        decimals = Integer.parseInt(type.substring(type.indexOf(",") + 1, type.indexOf(")")));
                    } else {
                        size = Integer.parseInt(type.substring(type.indexOf("(") + 1, type.indexOf(")")));
                    }

                    type = type.substring(0, type.indexOf("("));
                }

                row.add(Integer.toString(MysqlDefs.mysqlToJavaType(type)));
                row.add(type);
                row.add(Integer.toString(size + decimals));
                row.add(Integer.toString(size + decimals));
                row.add(Integer.toString(decimals));
                row.add(Integer.toString(DatabaseMetaData.bestRowNotPseudo));
                data.add(row);
            }
        }
    } finally {
        if (resultSet != null) {
            resultSet.close();
        }
        vitessStatement.close();
    }
    return new VitessResultSet(columnName, columnType, data, this.connection);
}