Search in sources :

Example 46 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class PreparedRoutingExecutor method invokeUpdateRow.

/**
     * @param result
     * @param row
     * @return
     */
private int invokeUpdateRow(RoutingResult result, Row row) {
    List<JdbcWorker<Integer>> workers = New.arrayList(result.tableNodeCount());
    TableNode[] selectNodes = result.getSelectNodes();
    for (TableNode node : selectNodes) {
        StatementBuilder sqlBuff = new StatementBuilder();
        List<Value> params = doTranslate(node, row, sqlBuff);
        workers.add(createUpdateWorker(node.getShardName(), sqlBuff.toString(), params));
    }
    try {
        addRuningJdbcWorkers(workers);
        int affectRows = 0;
        if (workers.size() > 1) {
            //MILLISECONDS
            int queryTimeout = getQueryTimeout();
            List<Future<Integer>> invokeAll;
            if (queryTimeout > 0) {
                invokeAll = jdbcExecutor.invokeAll(workers, queryTimeout, TimeUnit.MILLISECONDS);
            } else {
                invokeAll = jdbcExecutor.invokeAll(workers);
            }
            for (Future<Integer> future : invokeAll) {
                affectRows += future.get();
            }
        } else if (workers.size() == 1) {
            affectRows = workers.get(0).doWork();
        }
        return affectRows;
    } catch (InterruptedException e) {
        throw DbException.convert(e);
    } catch (ExecutionException e) {
        throw DbException.convert(e.getCause());
    } finally {
        removeRuningJdbcWorkers(workers);
        for (JdbcWorker<Integer> jdbcWorker : workers) {
            jdbcWorker.closeResource();
        }
    }
}
Also used : JdbcWorker(com.wplatform.ddal.excutor.JdbcWorker) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) Value(com.wplatform.ddal.value.Value) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 47 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class JdbcDatabaseMetaData method getFunctions.

private String getFunctions(String section) throws SQLException {
    try {
        checkClosed();
        PreparedStatement prep = conn.prepareAutoCloseStatement("SELECT TOPIC " + "FROM INFORMATION_SCHEMA.HELP WHERE SECTION = ?");
        prep.setString(1, section);
        ResultSet rs = prep.executeQuery();
        StatementBuilder buff = new StatementBuilder();
        while (rs.next()) {
            String s = rs.getString(1).trim();
            String[] array = StringUtils.arraySplit(s, ',', true);
            for (String a : array) {
                buff.appendExceptFirst(",");
                String f = a.trim();
                if (f.indexOf(' ') >= 0) {
                    // remove 'Function' from 'INSERT Function'
                    f = f.substring(0, f.indexOf(' ')).trim();
                }
                buff.append(f);
            }
        }
        rs.close();
        prep.close();
        return buff.toString();
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder) SimpleResultSet(com.wplatform.ddal.result.SimpleResultSet) DbException(com.wplatform.ddal.message.DbException)

Example 48 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class JdbcDatabaseMetaData method getTables.

/**
     * Gets the list of tables in the database. The result set is sorted by
     * TABLE_TYPE, TABLE_SCHEM, and TABLE_NAME.
     * <p>
     * <ul>
     * <li>1 TABLE_CAT (String) table catalog </li>
     * <li>2 TABLE_SCHEM (String) table schema </li>
     * <li>3 TABLE_NAME (String) table name </li>
     * <li>4 TABLE_TYPE (String) table type </li>
     * <li>5 REMARKS (String) comment </li>
     * <li>6 TYPE_CAT (String) always null </li>
     * <li>7 TYPE_SCHEM (String) always null </li>
     * <li>8 TYPE_NAME (String) always null </li>
     * <li>9 SELF_REFERENCING_COL_NAME (String) always null </li>
     * <li>10 REF_GENERATION (String) always null </li>
     * <li>11 SQL (String) the create table statement or NULL for systems tables
     * </li>
     * </ul>
     *
     * @param catalogPattern   null (to get all objects) or the catalog name
     * @param schemaPattern    null (to get all objects) or a schema name
     *                         (uppercase for unquoted names)
     * @param tableNamePattern null (to get all objects) or a table name
     *                         (uppercase for unquoted names)
     * @param types            null or a list of table types
     * @return the list of columns
     * @throws SQLException if the connection is closed
     */
@Override
public ResultSet getTables(String catalogPattern, String schemaPattern, String tableNamePattern, String[] types) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("getTables(" + quote(catalogPattern) + ", " + quote(schemaPattern) + ", " + quote(tableNamePattern) + ", " + quoteArray(types) + ");");
        }
        checkClosed();
        String tableType;
        if (types != null && types.length > 0) {
            StatementBuilder buff = new StatementBuilder("TABLE_TYPE IN(");
            for (int i = 0; i < types.length; i++) {
                buff.appendExceptFirst(", ");
                buff.append('?');
            }
            tableType = buff.append(')').toString();
        } else {
            tableType = "TRUE";
        }
        PreparedStatement prep = conn.prepareAutoCloseStatement("SELECT " + "TABLE_CATALOG TABLE_CAT, " + "TABLE_SCHEMA TABLE_SCHEM, " + "TABLE_NAME, " + "TABLE_TYPE, " + "REMARKS, " + "TYPE_NAME TYPE_CAT, " + "TYPE_NAME TYPE_SCHEM, " + "TYPE_NAME, " + "TYPE_NAME SELF_REFERENCING_COL_NAME, " + "TYPE_NAME REF_GENERATION, " + "SQL " + "FROM INFORMATION_SCHEMA.TABLES " + "WHERE TABLE_CATALOG LIKE ? ESCAPE ? " + "AND TABLE_SCHEMA LIKE ? ESCAPE ? " + "AND TABLE_NAME LIKE ? ESCAPE ? " + "AND (" + tableType + ") " + "ORDER BY TABLE_TYPE, TABLE_SCHEMA, TABLE_NAME");
        prep.setString(1, getCatalogPattern(catalogPattern));
        prep.setString(2, "\\");
        prep.setString(3, getSchemaPattern(schemaPattern));
        prep.setString(4, "\\");
        prep.setString(5, getPattern(tableNamePattern));
        prep.setString(6, "\\");
        for (int i = 0; types != null && i < types.length; i++) {
            prep.setString(7 + i, types[i]);
        }
        return prep.executeQuery();
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder) DbException(com.wplatform.ddal.message.DbException)

Example 49 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class Trace method formatParams.

/**
     * Format the parameter list.
     *
     * @param parameters the parameter list
     * @return the formatted text
     */
public static String formatParams(ArrayList<? extends ParameterInterface> parameters) {
    if (parameters.size() == 0) {
        return "";
    }
    StatementBuilder buff = new StatementBuilder();
    int i = 0;
    boolean params = false;
    for (ParameterInterface p : parameters) {
        if (p.isValueSet()) {
            if (!params) {
                buff.append(" {");
                params = true;
            }
            buff.appendExceptFirst(", ");
            Value v = p.getParamValue();
            buff.append(++i).append(": ").append(v.getTraceSQL());
        }
    }
    if (params) {
        buff.append('}');
    }
    return buff.toString();
}
Also used : ParameterInterface(com.wplatform.ddal.command.expression.ParameterInterface) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Value(com.wplatform.ddal.value.Value)

Aggregations

StatementBuilder (com.wplatform.ddal.util.StatementBuilder)49 Value (com.wplatform.ddal.value.Value)13 Expression (com.wplatform.ddal.command.expression.Expression)9 Column (com.wplatform.ddal.dbobject.table.Column)8 PreparedStatement (java.sql.PreparedStatement)7 SQLException (java.sql.SQLException)4 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 Connection (java.sql.Connection)3 DataSource (javax.sql.DataSource)3 Index (com.wplatform.ddal.dbobject.index.Index)2 JdbcWorker (com.wplatform.ddal.excutor.JdbcWorker)2 DbException (com.wplatform.ddal.message.DbException)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)1 AlterTableAlterColumn (com.wplatform.ddal.command.ddl.AlterTableAlterColumn)1