pg.ddx.io pgsql-odbc@postgresql.org mailing list archive
help / color / mirror / Atom feed From: Heikki Linnakangas <hlinnakangas@vmware.com>
To: Hiroshi Inoue <inoue@tpf.co.jp>
Cc: pgsql-odbc@postgresql.org
Subject: Re: SQLExecute returns 0 rows with BoolsAsChar=1
Date: Mon, 25 Mar 2013 13:44:05 +0200
Message-ID: <51503885.5090001@vmware.com> (raw )
In-Reply-To: <514ED061.10906@tpf.co.jp >
References: <51479445.7020905@vmware.com >
<514AFABC.7000808@tpf.co.jp >
<514B3870.2060208@vmware.com >
<514ED061.10906@tpf.co.jp >
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-odbc>
On 24.03.2013 12:07, Hiroshi Inoue wrote:
> I can reproduce it now when I turn off the *Server side prepare* option
> or turn on the *Use Declare/Fetch* option.
Ah yeah, UseServerSidePrepare hides it. I'm also seeing the problem
without UseDeclareFetch, though.
> Attached is a patch to fix the bug.
Thanks. I must admit I don't understand how this works. Also, the 2nd
test case in attached file now fails. It does a DELETE ... RETURNING,
binding a parameter array with a VARCHAR(5) column, and fetches the
results. With the patch, it can't fetch the result sets.
- Heikki
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
Attachments:
arraybinding.out (text/plain, 497B) download
\! ./src/arraybinding-test
connected
Parameter Status
Result set:
10000
Result set:
0 columnwise 0
1 columnwise 1
100 columnwise 100
9999 columnwise 9999
Parameter Status
Fetching result sets for array bound (5 results expected)
1: Result set:
columnwise 100
2: Result set:
columnwise 101
3: Result set:
columnwise 102
4: Result set:
columnwise 103
5: Result set:
columnwise 104
Number of rows in table:
Result set:
9995
Result set:
0 columnwise 0
1 columnwise 1
9999 columnwise 9999
disconnecting
arraybinding-test.c (text/x-csrc, 6.5K) download
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
#define ARRAY_SIZE 10000
#define ARRAY_SIZE_SMALL 5
int main(int argc, char **argv)
{
SQLRETURN rc;
HSTMT hstmt = SQL_NULL_HSTMT;
char *sql;
int i;
SQLUINTEGER int_array[ARRAY_SIZE];
SQLCHAR str_array[ARRAY_SIZE][30];
SQLCHAR str_array2[ARRAY_SIZE][6];
SQLLEN int_ind_array[ARRAY_SIZE];
SQLLEN str_ind_array[ARRAY_SIZE];
SQLUSMALLINT status_array[ARRAY_SIZE];
SQLULEN nprocessed;
test_connect();
rc = SQLAllocStmt(conn, &hstmt);
if (!SQL_SUCCEEDED(rc))
{
print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
exit(1);
}
sql = "CREATE TEMPORARY TABLE tmptable (i int4, t text)";
rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLExecDirect failed while creating temp table", hstmt);
rc = SQLFreeStmt(hstmt, SQL_CLOSE);
CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
/****
* 1. Test column-wise binding
*/
for (i = 0; i < ARRAY_SIZE; i++)
{
int_array[i] = i;
int_ind_array[i] = 0;
sprintf(str_array[i], "columnwise %d", i);
str_ind_array[i] = SQL_NTS;
}
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_BIND_TYPE, SQL_PARAM_BIND_BY_COLUMN, 0);
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_STATUS_PTR, status_array, 0);
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMS_PROCESSED_PTR, &nprocessed, 0);
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER) ARRAY_SIZE, 0);
/* Bind the parameter arrays. */
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 5, 0,
int_array, 0, int_ind_array);
SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 29, 0,
str_array, 30, str_ind_array);
/* Execute */
sql = "INSERT INTO tmptable VALUES (?, ?)";
rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
/* Fetch results */
printf("Parameter Status\n");
for (i = 0; i < nprocessed; i++)
{
switch (status_array[i])
{
case SQL_PARAM_SUCCESS:
case SQL_PARAM_SUCCESS_WITH_INFO:
break;
case SQL_PARAM_ERROR:
printf("%d\tError\n", i);
break;
case SQL_PARAM_UNUSED:
printf("%d\tUnused\n", i);
break;
case SQL_PARAM_DIAG_UNAVAILABLE:
printf("%d\tDiag unavailable\n", i);
break;
}
}
rc = SQLFreeStmt(hstmt, SQL_CLOSE);
CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
/*
* Free and allocate a new handle for the next SELECT statement, as we don't
* want to array bind that one. The parameters set with SQLSetStmtAttr
* survive SQLFreeStmt.
*/
rc = SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
CHECK_STMT_RESULT(rc, "SQLFreeHandle failed", hstmt);
rc = SQLAllocStmt(conn, &hstmt);
if (!SQL_SUCCEEDED(rc))
{
print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
exit(1);
}
/* Check that all the rows were inserted */
sql = "SELECT COUNT(*) FROM tmptable";
rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
print_result(hstmt);
rc = SQLFreeStmt(hstmt, SQL_CLOSE);
CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
/* Check the contents of a few rows */
sql = "SELECT * FROM tmptable WHERE i IN (0, 1, 100, 9999, 10000) ORDER BY i";
rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
print_result(hstmt);
rc = SQLFreeStmt(hstmt, SQL_CLOSE);
CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
/****
* 2. Test column-wise binding. With a column_size=5 VARCHAR param - that
* causes the driver to do a server-side prepare, assuming BoolsAsChar=1.
*/
/* a small array will do for this test */
for (i = 0; i < ARRAY_SIZE_SMALL; i++)
{
sprintf(str_array2[i], "%d", 100+i);
str_ind_array[i] = SQL_NTS;
}
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_BIND_TYPE, SQL_PARAM_BIND_BY_COLUMN, 0);
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_STATUS_PTR, status_array, 0);
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMS_PROCESSED_PTR, &nprocessed, 0);
SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER) ARRAY_SIZE_SMALL, 0);
/* Bind the parameter array. */
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 5, 0,
str_array2, 6, str_ind_array);
/* Execute */
sql = "DELETE FROM tmptable WHERE i = ? RETURNING (t)";
rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
/* Fetch results */
printf("Parameter Status\n");
for (i = 0; i < nprocessed; i++)
{
switch (status_array[i])
{
case SQL_PARAM_SUCCESS:
case SQL_PARAM_SUCCESS_WITH_INFO:
break;
case SQL_PARAM_ERROR:
printf("%d\tError\n", i);
break;
case SQL_PARAM_UNUSED:
printf("%d\tUnused\n", i);
break;
case SQL_PARAM_DIAG_UNAVAILABLE:
printf("%d\tDiag unavailable\n", i);
break;
}
}
printf ("Fetching result sets for array bound (%d results expected)\n",
nprocessed);
for (i = 1; rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO; i++)
{
printf("%d: ", i);
print_result(hstmt);
rc = SQLMoreResults(hstmt);
}
if (rc != SQL_NO_DATA)
CHECK_STMT_RESULT(rc, "SQLMoreResults failed", hstmt);
rc = SQLFreeStmt(hstmt, SQL_CLOSE);
CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
/*
* Free and allocate a new handle for the next SELECT statement, as we don't
* want to array bind that one. The parameters set with SQLSetStmtAttr
* survive SQLFreeStmt.
*/
rc = SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
CHECK_STMT_RESULT(rc, "SQLFreeHandle failed", hstmt);
rc = SQLAllocStmt(conn, &hstmt);
if (!SQL_SUCCEEDED(rc))
{
print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
exit(1);
}
/* Check that all the rows were inserted */
printf("Number of rows in table:\n");
sql = "SELECT COUNT(*) FROM tmptable";
rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
print_result(hstmt);
rc = SQLFreeStmt(hstmt, SQL_CLOSE);
CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
/* Check the contents of a few rows */
sql = "SELECT * FROM tmptable WHERE i IN (0, 1, 100, 9999, 10000) ORDER BY i";
rc = SQLExecDirect(hstmt, (SQLCHAR *) sql, SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
print_result(hstmt);
rc = SQLFreeStmt(hstmt, SQL_CLOSE);
CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
/* Clean up */
test_disconnect();
return 0;
}
common.h (text/x-chdr, 5.3K) download
#include <stdio.h>
#include <string.h>
#include <sql.h>
#include <sqlext.h>
static SQLHENV env;
static SQLHDBC conn;
#define CHECK_STMT_RESULT(rc, msg, hstmt) \
if (!SQL_SUCCEEDED(rc)) \
{ \
print_diag(msg, SQL_HANDLE_STMT, hstmt); \
exit(1); \
}
static void
print_diag(char *msg, SQLSMALLINT htype, SQLHANDLE handle)
{
char sqlstate[32];
char message[1000];
SQLINTEGER nativeerror;
SQLSMALLINT textlen;
SQLRETURN ret;
if (msg)
printf("%s\n", msg);
ret = SQLGetDiagRec(htype, handle, 1,
sqlstate, &nativeerror, message, 256, &textlen);
if (ret != SQL_ERROR)
printf("%s=%s\n", (CHAR *)sqlstate, (CHAR *)message);
}
static void
test_connect_ext(char *extraparams)
{
SQLRETURN ret;
SQLCHAR str[1024];
SQLSMALLINT strl;
SQLCHAR dsn[1024];
snprintf(dsn, sizeof(dsn), "DSN=psqlodbc_test_dsn;%s",
extraparams ? extraparams : "");
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, env, &conn);
ret = SQLDriverConnect(conn, NULL, dsn, SQL_NTS,
str, sizeof(str), &strl,
SQL_DRIVER_COMPLETE);
if (SQL_SUCCEEDED(ret)) {
printf("connected\n");
} else {
print_diag("SQLDriverConnect failed.", SQL_HANDLE_DBC, conn);
exit(1);
}
}
static void
test_connect(void)
{
test_connect_ext(NULL);
}
static void
test_disconnect(void)
{
SQLRETURN rc;
printf("disconnecting\n");
rc = SQLDisconnect(conn);
if (!SQL_SUCCEEDED(rc))
{
print_diag("SQLDisconnect failed", SQL_HANDLE_DBC, conn);
exit(1);
}
rc = SQLFreeConnect(conn);
if (!SQL_SUCCEEDED(rc))
{
print_diag("SQLFreeConnect failed", SQL_HANDLE_DBC, conn);
exit(1);
}
conn = NULL;
rc = SQLFreeEnv(env);
if (!SQL_SUCCEEDED(rc))
{
print_diag("SQLFreeEnv failed", SQL_HANDLE_ENV, env);
exit(1);
}
env = NULL;
}
static const char *
datatype_str(SQLSMALLINT datatype)
{
static char buf[100];
switch (datatype)
{
case SQL_CHAR:
return "CHAR";
case SQL_VARCHAR:
return "VARCHAR";
case SQL_LONGVARCHAR:
return "LONGVARCHAR";
case SQL_WCHAR:
return "WCHAR";
case SQL_WVARCHAR:
return "WVARCHAR";
case SQL_WLONGVARCHAR:
return "WLONGVARCHAR";
case SQL_DECIMAL:
return "DECIMAL";
case SQL_NUMERIC:
return "NUMERIC";
case SQL_SMALLINT:
return "SMALLINT";
case SQL_INTEGER:
return "INTEGER";
case SQL_REAL:
return "REAL";
case SQL_FLOAT:
return "FLOAT";
case SQL_DOUBLE:
return "DOUBLE";
case SQL_BIT:
return "BIT";
case SQL_TINYINT:
return "TINYINT";
case SQL_BIGINT:
return "BIGINT";
case SQL_BINARY:
return "BINARY";
case SQL_VARBINARY:
return "VARBINARY";
case SQL_LONGVARBINARY:
return "LONGVARBINARY";
case SQL_TYPE_DATE:
return "TYPE_DATE";
case SQL_TYPE_TIME:
return "TYPE_TIME";
case SQL_TYPE_TIMESTAMP:
return "TYPE_TIMESTAMP";
case SQL_GUID:
return "GUID";
default:
snprintf(buf, sizeof(buf), "unknown sql type %d", datatype);
return buf;
}
}
static const char *nullable_str(SQLSMALLINT nullable)
{
static char buf[100];
switch(nullable)
{
case SQL_NO_NULLS:
return "not nullable";
case SQL_NULLABLE:
return "nullable";
case SQL_NULLABLE_UNKNOWN:
return "nullable_unknown";
default:
snprintf(buf, sizeof(buf), "unknown nullable value %d", nullable);
return buf;
}
}
static void
print_result_meta(HSTMT hstmt)
{
SQLRETURN rc;
SQLSMALLINT numcols;
int i;
rc = SQLNumResultCols(hstmt, &numcols);
if (!SQL_SUCCEEDED(rc))
{
print_diag("SQLNumResultCols failed", SQL_HANDLE_STMT, hstmt);
return;
}
printf("Result set metadata:\n");
for (i = 1; i <= numcols; i++)
{
SQLCHAR colname[50];
SQLSMALLINT colnamelen;
SQLSMALLINT datatype;
SQLULEN colsize;
SQLSMALLINT decdigits;
SQLSMALLINT nullable;
rc = SQLDescribeCol(hstmt, i,
colname, sizeof(colname),
&colnamelen,
&datatype,
&colsize,
&decdigits,
&nullable);
if (!SQL_SUCCEEDED(rc))
{
print_diag("SQLDescribeCol failed", SQL_HANDLE_STMT, hstmt);
return;
}
printf("%s: %s(%d) digits: %d, %s\n",
colname, datatype_str(datatype), colsize,
decdigits, nullable_str(nullable));
}
}
static void
print_result(HSTMT hstmt)
{
SQLRETURN rc;
SQLSMALLINT numcols;
rc = SQLNumResultCols(hstmt, &numcols);
if (!SQL_SUCCEEDED(rc))
{
print_diag("SQLNumResultCols failed", SQL_HANDLE_STMT, hstmt);
return;
}
printf("Result set:\n");
while(1)
{
rc = SQLFetch(hstmt);
if (rc == SQL_NO_DATA)
break;
if (rc == SQL_SUCCESS)
{
char buf[40];
int i;
SQLLEN ind;
for (i = 1; i <= numcols; i++)
{
rc = SQLGetData(hstmt,i, SQL_C_CHAR, buf, sizeof(buf), &ind);
if (!SQL_SUCCEEDED(rc))
{
print_diag("SQLGetData failed", SQL_HANDLE_STMT, hstmt);
return;
}
if (ind == SQL_NULL_DATA)
strcpy(buf, "NULL");
printf("%s%s", (i > 1) ? "\t" : "", buf);
}
printf("\n");
}
else
{
print_diag("SQLFetch failed", SQL_HANDLE_STMT, hstmt);
exit(1);
}
}
}
view thread (12+ messages) latest in thread
Message-ID: <51503885.5090001@vmware.com>
Permalink: ../51503885.5090001@vmware.com/
Also on: postgresql.org/message-id/51503885.5090001@vmware.com
copy link · copy postgr.es
reply Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-odbc@postgresql.org
Cc: hlinnakangas@vmware.com, inoue@tpf.co.jp
Subject: Re: SQLExecute returns 0 rows with BoolsAsChar=1
In-Reply-To: <51503885.5090001@vmware.com>
References: <51479445.7020905@vmware.com> <514AFABC.7000808@tpf.co.jp> <514B3870.2060208@vmware.com> <514ED061.10906@tpf.co.jp> <51503885.5090001@vmware.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox