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);
}
}
}
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();
}
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;
}
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));
}
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));
}
Aggregations