Search in sources :

Example 1 with ParameterMetaData

use of java.sql.ParameterMetaData in project Core by iConomy.

the class QueryRunner method fillStatement.

/**
     * Fill the <code>PreparedStatement</code> replacement parameters with 
     * the given objects.
     * @param stmt PreparedStatement to fill
     * @param params Query replacement parameters; <code>null</code> is a valid
     * value to pass in.
     * @throws SQLException if a database access error occurs
     */
public void fillStatement(PreparedStatement stmt, Object... params) throws SQLException {
    if (params == null) {
        return;
    }
    ParameterMetaData pmd = null;
    if (!pmdKnownBroken) {
        pmd = stmt.getParameterMetaData();
        if (pmd.getParameterCount() < params.length) {
            throw new SQLException("Too many parameters: expected " + pmd.getParameterCount() + ", was given " + params.length);
        }
    }
    for (int i = 0; i < params.length; i++) {
        if (params[i] != null) {
            stmt.setObject(i + 1, params[i]);
        } else {
            // VARCHAR works with many drivers regardless
            // of the actual column type.  Oddly, NULL and 
            // OTHER don't work with Oracle's drivers.
            int sqlType = Types.VARCHAR;
            if (!pmdKnownBroken) {
                try {
                    sqlType = pmd.getParameterType(i + 1);
                } catch (SQLException e) {
                    pmdKnownBroken = true;
                }
            }
            stmt.setNull(i + 1, sqlType);
        }
    }
}
Also used : SQLException(java.sql.SQLException) ParameterMetaData(java.sql.ParameterMetaData)

Example 2 with ParameterMetaData

use of java.sql.ParameterMetaData in project cubrid-manager by CUBRID.

the class PstmtDataDialog method analyzeSql.

protected void analyzeSql() {
    if (!validSql()) {
        return;
    }
    String sql = sqlTxt.getText();
    int count = SqlParser.getStrCount(sql, "?");
    parameterTable.removeAll();
    ParameterMetaData metaData = TableUtil.getParameterMetaData(database, sqlTxt.getText());
    for (int i = 0; i < count; i++) {
        TableItem item = new TableItem(parameterTable, SWT.NONE);
        item.setText(0, String.valueOf(i + 1));
        if (metaData != null) {
            try {
                String type = metaData.getParameterTypeName(i + 1);
                if (type == null || type.trim().length() == 0) {
                    continue;
                }
                int precision = metaData.getPrecision(i + 1);
                int scale = metaData.getScale(i + 1);
                type = FieldHandlerUtils.getComleteType(type, null, precision, scale);
                item.setText(1, type);
            } catch (SQLException e) {
                continue;
            }
        }
    }
    validate();
    packTable();
}
Also used : SQLException(java.sql.SQLException) TableItem(org.eclipse.swt.widgets.TableItem) Point(org.eclipse.swt.graphics.Point) ParameterMetaData(java.sql.ParameterMetaData)

Example 3 with ParameterMetaData

use of java.sql.ParameterMetaData in project phoenix by apache.

the class PhoenixConnection method executeStatements.

public int executeStatements(Reader reader, List<Object> binds, PrintStream out) throws IOException, SQLException {
    int bindsOffset = 0;
    int nStatements = 0;
    PhoenixStatementParser parser = new PhoenixStatementParser(reader);
    try {
        while (true) {
            PhoenixPreparedStatement stmt = null;
            try {
                stmt = new PhoenixPreparedStatement(this, parser);
                ParameterMetaData paramMetaData = stmt.getParameterMetaData();
                for (int i = 0; i < paramMetaData.getParameterCount(); i++) {
                    stmt.setObject(i + 1, binds.get(bindsOffset + i));
                }
                long start = System.currentTimeMillis();
                boolean isQuery = stmt.execute();
                if (isQuery) {
                    ResultSet rs = stmt.getResultSet();
                    if (!rs.next()) {
                        if (out != null) {
                            out.println("no rows selected");
                        }
                    } else {
                        int columnCount = 0;
                        if (out != null) {
                            ResultSetMetaData md = rs.getMetaData();
                            columnCount = md.getColumnCount();
                            for (int i = 1; i <= columnCount; i++) {
                                int displayWidth = md.getColumnDisplaySize(i);
                                String label = md.getColumnLabel(i);
                                if (md.isSigned(i)) {
                                    out.print(displayWidth < label.length() ? label.substring(0, displayWidth) : Strings.padStart(label, displayWidth, ' '));
                                    out.print(' ');
                                } else {
                                    out.print(displayWidth < label.length() ? label.substring(0, displayWidth) : Strings.padEnd(md.getColumnLabel(i), displayWidth, ' '));
                                    out.print(' ');
                                }
                            }
                            out.println();
                            for (int i = 1; i <= columnCount; i++) {
                                int displayWidth = md.getColumnDisplaySize(i);
                                out.print(Strings.padStart("", displayWidth, '-'));
                                out.print(' ');
                            }
                            out.println();
                        }
                        do {
                            if (out != null) {
                                ResultSetMetaData md = rs.getMetaData();
                                for (int i = 1; i <= columnCount; i++) {
                                    int displayWidth = md.getColumnDisplaySize(i);
                                    String value = rs.getString(i);
                                    String valueString = value == null ? QueryConstants.NULL_DISPLAY_TEXT : value;
                                    if (md.isSigned(i)) {
                                        out.print(Strings.padStart(valueString, displayWidth, ' '));
                                    } else {
                                        out.print(Strings.padEnd(valueString, displayWidth, ' '));
                                    }
                                    out.print(' ');
                                }
                                out.println();
                            }
                        } while (rs.next());
                    }
                } else if (out != null) {
                    int updateCount = stmt.getUpdateCount();
                    if (updateCount >= 0) {
                        out.println((updateCount == 0 ? "no" : updateCount) + (updateCount == 1 ? " row " : " rows ") + stmt.getUpdateOperation().toString());
                    }
                }
                bindsOffset += paramMetaData.getParameterCount();
                double elapsedDuration = ((System.currentTimeMillis() - start) / 1000.0);
                out.println("Time: " + elapsedDuration + " sec(s)\n");
                nStatements++;
            } finally {
                if (stmt != null) {
                    stmt.close();
                }
            }
        }
    } catch (EOFException e) {
    }
    return nStatements;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PhoenixStatementParser(org.apache.phoenix.jdbc.PhoenixStatement.PhoenixStatementParser) ResultSet(java.sql.ResultSet) EOFException(java.io.EOFException) Savepoint(java.sql.Savepoint) ParameterMetaData(java.sql.ParameterMetaData)

Example 4 with ParameterMetaData

use of java.sql.ParameterMetaData in project phoenix by apache.

the class QueryMetaDataTest method testNonEqualityRowValueConstructorBindParamMetaDataWithBindArgsOnRHSAndLiteralExprOnLHS.

@Test
public void testNonEqualityRowValueConstructorBindParamMetaDataWithBindArgsOnRHSAndLiteralExprOnLHS() throws Exception {
    String query = "SELECT a_integer, x_integer FROM aTable WHERE 7 >= (?, ?)";
    Connection conn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES));
    PreparedStatement statement = conn.prepareStatement(query);
    ParameterMetaData pmd = statement.getParameterMetaData();
    assertEquals(2, pmd.getParameterCount());
    assertEquals(Integer.class.getName(), pmd.getParameterClassName(1));
    assertEquals(null, pmd.getParameterClassName(2));
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ParameterMetaData(java.sql.ParameterMetaData) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 5 with ParameterMetaData

use of java.sql.ParameterMetaData in project phoenix by apache.

the class QueryMetaDataTest method testUpsertParameterMetaData.

@Test
public void testUpsertParameterMetaData() throws Exception {
    String query = "UPSERT INTO atable VALUES (?, ?, ?, ?, ?)";
    Connection conn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES));
    PreparedStatement statement = conn.prepareStatement(query);
    ParameterMetaData pmd = statement.getParameterMetaData();
    assertEquals(5, pmd.getParameterCount());
    assertEquals(String.class.getName(), pmd.getParameterClassName(1));
    assertEquals(String.class.getName(), pmd.getParameterClassName(2));
    assertEquals(String.class.getName(), pmd.getParameterClassName(3));
    assertEquals(String.class.getName(), pmd.getParameterClassName(4));
    assertEquals(Integer.class.getName(), pmd.getParameterClassName(5));
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ParameterMetaData(java.sql.ParameterMetaData) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Aggregations

ParameterMetaData (java.sql.ParameterMetaData)136 PreparedStatement (java.sql.PreparedStatement)82 Test (org.junit.Test)55 Connection (java.sql.Connection)45 SQLException (java.sql.SQLException)35 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)31 ResultSet (java.sql.ResultSet)22 Statement (java.sql.Statement)20 ResultSetMetaData (java.sql.ResultSetMetaData)18 SQLServerParameterMetaData (com.microsoft.sqlserver.jdbc.SQLServerParameterMetaData)15 BigDecimal (java.math.BigDecimal)12 CallableStatement (java.sql.CallableStatement)12 Test (org.junit.jupiter.api.Test)12 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)11 UnitTest (nl.topicus.jdbc.test.category.UnitTest)9 StringContains.containsString (org.hamcrest.core.StringContains.containsString)9 Date (java.sql.Date)7 DisplayName (org.junit.jupiter.api.DisplayName)7 ArrayList (java.util.ArrayList)5 List (java.util.List)5