use of java.sql.ParameterMetaData in project derby by apache.
the class DRDAConnThread method writeQRYDSC.
/**
* Write QRYDSC - Query Answer Set Description
*
* @param stmt DRDAStatement we are working on
* @param FDODSConly simply the FDODSC, without the wrap
*
* Instance Variables
* SQLDTAGRP - required
*
* Only 84 columns can be sent in a single QRYDSC. If there are more columns
* they must be sent in subsequent QRYDSC.
* If the QRYDSC will not fit into the current block, as many columns as can
* fit are sent and then the remaining are sent in the following blocks.
*
* @throws DRDAProtocolException
* @throws SQLException
*/
private void writeQRYDSC(DRDAStatement stmt, boolean FDODSConly) throws DRDAProtocolException, SQLException {
ResultSet rs = null;
ResultSetMetaData rsmeta = null;
ParameterMetaData pmeta = null;
if (!stmt.needsToSendParamData) {
rs = stmt.getResultSet();
}
if (rs == null) {
// this is a CallableStatement, use parameter meta data
pmeta = stmt.getParameterMetaData();
} else {
rsmeta = rs.getMetaData();
}
int numCols = (rsmeta != null ? rsmeta.getColumnCount() : pmeta.getParameterCount());
int numGroups = 1;
int colStart = 1;
int colEnd = numCols;
int blksize = stmt.getBlksize() > 0 ? stmt.getBlksize() : CodePoint.QRYBLKSZ_MAX;
// check for remaining space in current query block
// Need to mod with blksize so remaining doesn't go negative. 4868
int remaining = blksize - (writer.getDSSLength() % blksize) - (3 + FdocaConstants.SQLCADTA_SQLDTARD_RLO_SIZE);
// calcuate how may columns can be sent in the current query block
int firstcols = remaining / FdocaConstants.SQLDTAGRP_COL_DSC_SIZE;
// under FdocaConstants.MAX_VARS_IN_NGDA
if (firstcols < numCols || numCols > FdocaConstants.MAX_VARS_IN_NGDA) {
// we are limited to FdocaConstants.MAX_VARS_IN_NGDA
if (firstcols > FdocaConstants.MAX_VARS_IN_NGDA) {
if (SanityManager.DEBUG) {
SanityManager.ASSERT(numCols > FdocaConstants.MAX_VARS_IN_NGDA, "Number of columns " + numCols + " is less than MAX_VARS_IN_NGDA");
}
numGroups = numCols / FdocaConstants.MAX_VARS_IN_NGDA;
// some left over
if (FdocaConstants.MAX_VARS_IN_NGDA * numGroups < numCols) {
numGroups++;
}
colEnd = FdocaConstants.MAX_VARS_IN_NGDA;
} else {
colEnd = firstcols;
numGroups += (numCols - firstcols) / FdocaConstants.MAX_VARS_IN_NGDA;
if (FdocaConstants.MAX_VARS_IN_NGDA * numGroups < numCols) {
numGroups++;
}
}
}
if (!FDODSConly) {
writer.createDssObject();
writer.startDdm(CodePoint.QRYDSC);
}
for (int i = 0; i < numGroups; i++) {
writeSQLDTAGRP(stmt, rsmeta, pmeta, colStart, colEnd, (i == 0 ? true : false));
colStart = colEnd + 1;
// 4868 - Limit range to MAX_VARS_IN_NGDA (used to have extra col)
colEnd = Math.min(colEnd + FdocaConstants.MAX_VARS_IN_NGDA, numCols);
}
writer.writeBytes(FdocaConstants.SQLCADTA_SQLDTARD_RLO);
if (!FDODSConly) {
writer.endDdmAndDss();
}
}
use of java.sql.ParameterMetaData in project derby by apache.
the class DRDAStatement method setupCallableStatementParams.
private void setupCallableStatementParams(CallableStatement cs) throws SQLException {
ParameterMetaData pmeta = getParameterMetaData();
int numElems = pmeta.getParameterCount();
for (int i = 0; i < numElems; i++) {
boolean outputFlag = false;
int parameterMode = pmeta.getParameterMode(i + 1);
int parameterType = pmeta.getParameterType(i + 1);
int parameterPrecision = pmeta.getPrecision(i + 1);
int parameterScale = pmeta.getScale(i + 1);
switch(parameterMode) {
case (ParameterMetaData.parameterModeIn):
break;
case (ParameterMetaData.parameterModeOut):
case (ParameterMetaData.parameterModeInOut):
outputFlag = true;
break;
case (ParameterMetaData.parameterModeUnknown):
// It's only unknown if array
String objectType = pmeta.getParameterClassName(i + 1);
parameterType = getOutputParameterTypeFromClassName(objectType);
if (parameterType != NOT_OUTPUT_PARAM)
outputFlag = true;
}
if (outputFlag) {
if (// not initialized yet, since previously none output
outputTypes == null) {
outputTypes = new int[numElems];
outputPrecision = new int[numElems];
outputScale = new int[numElems];
for (int j = 0; j < numElems; j++) {
// default init value
outputTypes[j] = NOT_OUTPUT_PARAM;
outputPrecision[j] = NOT_OUTPUT_PARAM;
outputScale[j] = NOT_OUTPUT_PARAM;
}
}
// save the output type so we can register when we parse
// the SQLDTA
outputTypes[i] = parameterType;
outputPrecision[i] = parameterPrecision;
outputScale[i] = parameterScale;
}
}
}
use of java.sql.ParameterMetaData in project sqlite-jna by gwenn.
the class SqliteParameterMetadataTest method testCount.
@Test
public void testCount() throws Exception {
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM test_table")) {
ParameterMetaData pmd = ps.getParameterMetaData();
assertEquals(0, pmd.getParameterCount());
}
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM test_table WHERE name = ?")) {
ParameterMetaData pmd = ps.getParameterMetaData();
assertEquals(1, pmd.getParameterCount());
}
}
use of java.sql.ParameterMetaData in project h2database by h2database.
the class PgServerThread method process.
private void process() throws IOException {
int x;
if (initDone) {
x = dataInRaw.read();
if (x < 0) {
stop = true;
return;
}
} else {
x = 0;
}
int len = dataInRaw.readInt();
len -= 4;
byte[] data = Utils.newBytes(len);
dataInRaw.readFully(data, 0, len);
dataIn = new DataInputStream(new ByteArrayInputStream(data, 0, len));
switch(x) {
case 0:
server.trace("Init");
int version = readInt();
if (version == 80877102) {
server.trace("CancelRequest");
int pid = readInt();
int key = readInt();
PgServerThread c = server.getThread(pid);
if (c != null && key == c.secret) {
c.cancelRequest();
} else {
// According to the PostgreSQL documentation, when canceling
// a request, if an invalid secret is provided then no
// exception should be sent back to the client.
server.trace("Invalid CancelRequest: pid=" + pid + ", key=" + key);
}
close();
} else if (version == 80877103) {
server.trace("SSLRequest");
out.write('N');
} else {
server.trace("StartupMessage");
server.trace(" version " + version + " (" + (version >> 16) + "." + (version & 0xff) + ")");
while (true) {
String param = readString();
if (param.length() == 0) {
break;
}
String value = readString();
if ("user".equals(param)) {
this.userName = value;
} else if ("database".equals(param)) {
this.databaseName = server.checkKeyAndGetDatabaseName(value);
} else if ("client_encoding".equals(param)) {
// UTF8
clientEncoding = value;
} else if ("DateStyle".equals(param)) {
if (value.indexOf(',') < 0) {
value += ", MDY";
}
dateStyle = value;
}
// extra_float_digits 2
// geqo on (Genetic Query Optimization)
server.trace(" param " + param + "=" + value);
}
sendAuthenticationCleartextPassword();
initDone = true;
}
break;
case 'p':
{
server.trace("PasswordMessage");
String password = readString();
try {
Properties info = new Properties();
info.put("MODE", "PostgreSQL");
info.put("USER", userName);
info.put("PASSWORD", password);
String url = "jdbc:h2:" + databaseName;
ConnectionInfo ci = new ConnectionInfo(url, info);
String baseDir = server.getBaseDir();
if (baseDir == null) {
baseDir = SysProperties.getBaseDir();
}
if (baseDir != null) {
ci.setBaseDir(baseDir);
}
if (server.getIfExists()) {
ci.setProperty("IFEXISTS", "TRUE");
}
conn = new JdbcConnection(ci, false);
// can not do this because when called inside
// DriverManager.getConnection, a deadlock occurs
// conn = DriverManager.getConnection(url, userName, password);
initDb();
sendAuthenticationOk();
} catch (Exception e) {
e.printStackTrace();
stop = true;
}
break;
}
case 'P':
{
server.trace("Parse");
Prepared p = new Prepared();
p.name = readString();
p.sql = getSQL(readString());
int paramTypesCount = readShort();
int[] paramTypes = null;
if (paramTypesCount > 0) {
paramTypes = new int[paramTypesCount];
for (int i = 0; i < paramTypesCount; i++) {
paramTypes[i] = readInt();
}
}
try {
p.prep = (JdbcPreparedStatement) conn.prepareStatement(p.sql);
ParameterMetaData meta = p.prep.getParameterMetaData();
p.paramType = new int[meta.getParameterCount()];
for (int i = 0; i < p.paramType.length; i++) {
int type;
if (i < paramTypesCount && paramTypes[i] != 0) {
type = paramTypes[i];
server.checkType(type);
} else {
type = PgServer.convertType(meta.getParameterType(i + 1));
}
p.paramType[i] = type;
}
prepared.put(p.name, p);
sendParseComplete();
} catch (Exception e) {
sendErrorResponse(e);
}
break;
}
case 'B':
{
server.trace("Bind");
Portal portal = new Portal();
portal.name = readString();
String prepName = readString();
Prepared prep = prepared.get(prepName);
if (prep == null) {
sendErrorResponse("Prepared not found");
break;
}
portal.prep = prep;
portals.put(portal.name, portal);
int formatCodeCount = readShort();
int[] formatCodes = new int[formatCodeCount];
for (int i = 0; i < formatCodeCount; i++) {
formatCodes[i] = readShort();
}
int paramCount = readShort();
try {
for (int i = 0; i < paramCount; i++) {
setParameter(prep.prep, prep.paramType[i], i, formatCodes);
}
} catch (Exception e) {
sendErrorResponse(e);
break;
}
int resultCodeCount = readShort();
portal.resultColumnFormat = new int[resultCodeCount];
for (int i = 0; i < resultCodeCount; i++) {
portal.resultColumnFormat[i] = readShort();
}
sendBindComplete();
break;
}
case 'C':
{
char type = (char) readByte();
String name = readString();
server.trace("Close");
if (type == 'S') {
Prepared p = prepared.remove(name);
if (p != null) {
JdbcUtils.closeSilently(p.prep);
}
} else if (type == 'P') {
portals.remove(name);
} else {
server.trace("expected S or P, got " + type);
sendErrorResponse("expected S or P");
break;
}
sendCloseComplete();
break;
}
case 'D':
{
char type = (char) readByte();
String name = readString();
server.trace("Describe");
if (type == 'S') {
Prepared p = prepared.get(name);
if (p == null) {
sendErrorResponse("Prepared not found: " + name);
} else {
try {
sendParameterDescription(p.prep.getParameterMetaData(), p.paramType);
sendRowDescription(p.prep.getMetaData());
} catch (Exception e) {
sendErrorResponse(e);
}
}
} else if (type == 'P') {
Portal p = portals.get(name);
if (p == null) {
sendErrorResponse("Portal not found: " + name);
} else {
PreparedStatement prep = p.prep.prep;
try {
ResultSetMetaData meta = prep.getMetaData();
sendRowDescription(meta);
} catch (Exception e) {
sendErrorResponse(e);
}
}
} else {
server.trace("expected S or P, got " + type);
sendErrorResponse("expected S or P");
}
break;
}
case 'E':
{
String name = readString();
server.trace("Execute");
Portal p = portals.get(name);
if (p == null) {
sendErrorResponse("Portal not found: " + name);
break;
}
int maxRows = readShort();
Prepared prepared = p.prep;
JdbcPreparedStatement prep = prepared.prep;
server.trace(prepared.sql);
try {
prep.setMaxRows(maxRows);
setActiveRequest(prep);
boolean result = prep.execute();
if (result) {
try {
ResultSet rs = prep.getResultSet();
// the meta-data is sent in the prior 'Describe'
while (rs.next()) {
sendDataRow(rs, p.resultColumnFormat);
}
sendCommandComplete(prep, 0);
} catch (Exception e) {
sendErrorResponse(e);
}
} else {
sendCommandComplete(prep, prep.getUpdateCount());
}
} catch (Exception e) {
if (prep.isCancelled()) {
sendCancelQueryResponse();
} else {
sendErrorResponse(e);
}
} finally {
setActiveRequest(null);
}
break;
}
case 'S':
{
server.trace("Sync");
sendReadyForQuery();
break;
}
case 'Q':
{
server.trace("Query");
String query = readString();
ScriptReader reader = new ScriptReader(new StringReader(query));
while (true) {
JdbcStatement stat = null;
try {
String s = reader.readStatement();
if (s == null) {
break;
}
s = getSQL(s);
stat = (JdbcStatement) conn.createStatement();
setActiveRequest(stat);
boolean result = stat.execute(s);
if (result) {
ResultSet rs = stat.getResultSet();
ResultSetMetaData meta = rs.getMetaData();
try {
sendRowDescription(meta);
while (rs.next()) {
sendDataRow(rs, null);
}
sendCommandComplete(stat, 0);
} catch (Exception e) {
sendErrorResponse(e);
break;
}
} else {
sendCommandComplete(stat, stat.getUpdateCount());
}
} catch (SQLException e) {
if (stat != null && stat.isCancelled()) {
sendCancelQueryResponse();
} else {
sendErrorResponse(e);
}
break;
} finally {
JdbcUtils.closeSilently(stat);
setActiveRequest(null);
}
}
sendReadyForQuery();
break;
}
case 'X':
{
server.trace("Terminate");
close();
break;
}
default:
server.trace("Unsupported: " + x + " (" + (char) x + ")");
break;
}
}
use of java.sql.ParameterMetaData in project mssql-jdbc by Microsoft.
the class SQLServerSpatialDatatypeTest method testCheckGeogMetaData.
@Test
public void testCheckGeogMetaData() throws SQLException {
beforeEachSetup();
pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + geogTableName + " (c1) VALUES (?)");
ParameterMetaData paramMetaData = pstmt.getParameterMetaData();
Geography g = Geography.STGeomFromText("POINT (1 2 3 4)", 4326);
pstmt.setGeography(1, g);
pstmt.execute();
int sqlType = paramMetaData.getParameterType(1);
String sqlTypeName = paramMetaData.getParameterTypeName(1);
assertEquals(sqlType, -158);
assertEquals(sqlTypeName, "geography");
SQLServerResultSet rs = (SQLServerResultSet) stmt.executeQuery("select * from " + geogTableName);
ResultSetMetaData rsmd = rs.getMetaData();
assertEquals(rsmd.getColumnType(1), -158);
}
Aggregations