\! "./src/bindcol-test"
connected
Result set:
-1 foo
-2 bar
-3 foobar
+1 foo1
+2 foo2
+3 foo3
+4 foo3
+5 foo3
+6 foo6
+7 foo7
+7 foo7
+7 foo7
+7 foo10
disconnecting
fetched: foo111
Testing SQL_FETCH_BOOKMARK, middle (-10)...
fetched: foo91
+Getting bookmark with SQLBindCol...
+fetched: foo100
+Unbinding boomark column...
+Moving +100...
+fetched: foo200
+Goind back to bookmark acquired with SQLBindCol...
+fetched: foo100
disconnecting
Query: SELECT 'x' || {fn SPACE(10) } || 'x'
Result set:
x x
+
+Query: { call length(?) }
+Param 1: foobar
+Result set:
+6
+
+Query: { call right(?, ?) }
+Param 1: foobar
+Param 2: 3
+Result set:
+bar
+
+Query: SELECT {d '2014-12-21' } + '1 day'::interval
+Result set:
+2014-12-22 00:00:00
+
+Query: SELECT {t '20:30:40' } + '1 hour 1 minute 1 second'::interval
+Result set:
+21:31:41
+
+Query: SELECT {ts '2014-12-21 20:30:40' } + '1 day 1 hour 1 minute 1 second'::interval
+Result set:
+2014-12-22 21:31:41
disconnecting
int main(int argc, char **argv)
{
- int rc;
- HSTMT hstmt = SQL_NULL_HSTMT;
+ int rc;
+ HSTMT hstmt = SQL_NULL_HSTMT;
/*
* NOTE: in the psqlodbc, we assume that SQL_C_LONG actually means a
* variable of type SQLINTEGER. They are not the same on platforms where
* (on little-endian systems, you won't notice the difference if you reset
* the high bits to zero before calling SQLBindCol.)
*/
- SQLINTEGER longvalue;
+ SQLINTEGER longvalue;
SQLLEN indLongvalue;
- char charvalue[100];
- SQLLEN indCharvalue;
+ char charvalue[100];
+ SQLLEN indCharvalue;
+ int rowno;
test_connect();
rc = SQLBindCol(hstmt, 2, SQL_C_CHAR, &charvalue, sizeof(charvalue), &indCharvalue);
CHECK_STMT_RESULT(rc, "SQLBindCol failed", hstmt);
- rc = SQLExecDirect(hstmt, (SQLCHAR *) "SELECT * FROM testtab1 ORDER BY id", SQL_NTS);
+ rc = SQLExecDirect(hstmt, (SQLCHAR *)
+ "SELECT id, 'foo' || id FROM generate_series(1, 10) id", SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);
printf("Result set:\n");
+ rowno = 0;
while(1)
{
rc = SQLFetch(hstmt);
print_diag("SQLFetch failed", SQL_HANDLE_STMT, hstmt);
exit(1);
}
+
+ /*
+ * At row 3, unbind the text field. At row 5, bind it again.
+ * At row 7, unbind both columns with SQLFreeStmt(SQL_UNBIND).
+ * At row 9, bind text field again.
+ */
+ rowno++;
+ if (rowno == 3)
+ {
+ rc = SQLBindCol(hstmt, 2, SQL_C_CHAR, NULL, 0, NULL);
+ CHECK_STMT_RESULT(rc, "SQLBindCol failed", hstmt);
+ }
+ if (rowno == 7)
+ {
+ rc = SQLFreeStmt(hstmt, SQL_UNBIND);
+ CHECK_STMT_RESULT(rc, "SQLFreeStmt(SQL_UNBIND) failed", hstmt);
+ }
+ if (rowno == 5 || rowno == 9)
+ {
+ rc = SQLBindCol(hstmt, 2, SQL_C_CHAR, &charvalue, sizeof(charvalue), &indCharvalue);
+ CHECK_STMT_RESULT(rc, "SQLBindCol failed", hstmt);
+ }
}
rc = SQLFreeStmt(hstmt, SQL_CLOSE);
rc = SQLFetchScroll(hstmt, SQL_FETCH_BOOKMARK, -10);
printFetchResult(hstmt, rc);
+ /*** Test getting a bookmark with SQLBindCol */
+ printf("Getting bookmark with SQLBindCol...\n");
+ rc = SQLBindCol(hstmt, 0, SQL_C_VARBOOKMARK, book_middle, sizeof(book_middle), &book_middle_ind);
+ CHECK_STMT_RESULT(rc, "SQLBindCol failed", hstmt);
+
+ rc = SQLFetchScroll(hstmt, SQL_FETCH_ABSOLUTE, 100);
+ printFetchResult(hstmt, rc);
+
+ printf("Unbinding boomark column...\n");
+ rc = SQLBindCol(hstmt, 0, SQL_C_VARBOOKMARK, NULL, 0, NULL);
+ CHECK_STMT_RESULT(rc, "SQLBindCol failed", hstmt);
+
+ printf("Moving +100...\n");
+ rc = SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, 100);
+ printFetchResult(hstmt, rc);
+
+ printf("Goind back to bookmark acquired with SQLBindCol...\n");
+ rc = SQLSetStmtAttr(hstmt, SQL_ATTR_FETCH_BOOKMARK_PTR,
+ (SQLPOINTER) book_middle, SQL_IS_POINTER);
+ CHECK_STMT_RESULT(rc, "SQLSetStmtAttr failed", hstmt);
+
+ rc = SQLFetchScroll(hstmt, SQL_FETCH_BOOKMARK, 0);
+ printFetchResult(hstmt, rc);
+
rc = SQLFreeStmt(hstmt, SQL_CLOSE);
CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
}
prepareQuery(hstmt, "SELECT 'x' || {fn SPACE(10) } || 'x'");
executeQuery(hstmt);
+ /**** CALL escapes ****/
+
+ prepareQuery(hstmt, "{ call length(?) }");
+ bindParamString(hstmt, 1, "foobar");
+ executeQuery(hstmt);
+
+ prepareQuery(hstmt, "{ call right(?, ?) }");
+ bindParamString(hstmt, 1, "foobar");
+ bindParamString(hstmt, 2, "3");
+ executeQuery(hstmt);
+
+ /* TODO: This doesn't currently work.
+ prepareQuery(hstmt, "{ ? = call concat(?, ?) }");
+ */
+
+ /**** Date, Time, and Timestamp literals ****/
+
+ prepareQuery(hstmt, "SELECT {d '2014-12-21' } + '1 day'::interval");
+ executeQuery(hstmt);
+
+ prepareQuery(hstmt, "SELECT {t '20:30:40' } + '1 hour 1 minute 1 second'::interval");
+ executeQuery(hstmt);
+
+ prepareQuery(hstmt, "SELECT {ts '2014-12-21 20:30:40' } + '1 day 1 hour 1 minute 1 second'::interval");
+ executeQuery(hstmt);
+
/* Clean up */
test_disconnect();