Created
October 15, 2017 03:06
-
-
Save kubo/3d8cfb06a71454d137e3014475e62674 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <dpi.h> | |
static dpiContext *g_context; | |
#define chkerr(func) do { \ | |
if ((func) < 0) { \ | |
dpiErrorInfo err; \ | |
dpiContext_getError(g_context, &err); \ | |
printf("ERROR at line %d\n %s\n %s\n", __LINE__, #func, err.message); \ | |
exit(1); \ | |
} \ | |
} while (0) | |
static const char *env(const char *name, const char *default_value) | |
{ | |
const char *value = getenv(name); | |
if (value == NULL) { | |
value = default_value; | |
} | |
return value; | |
} | |
int main() | |
{ | |
const char *username = env("ODPIC_SAMPLES_MAIN_USER", "odpicdemo"); | |
const char *password = env("ODPIC_SAMPLES_MAIN_PASSWORD", "welcome"); | |
const char *connectString = env("ODPIC_SAMPLES_CONNECT_STRING", "localhost/orclpdb"); | |
const char *sqltext1 = "select to_timestamp_tz('2017-01-01 00:00:00 -3:30', 'YYYY-MM-DD HH24:MI:SS TZH:TZM') from dual"; | |
const char *sqltext2 = "select to_char(:1, 'YYYY-MM-DD HH24:MI:SS TZH:TZM') from dual"; | |
dpiErrorInfo err; | |
dpiConn *conn; | |
dpiStmt *stmt; | |
uint32_t numQueryColumns; | |
uint32_t bufferRowIndex; | |
int found; | |
dpiNativeTypeNum nativeTypeNum; | |
dpiVar *var; | |
dpiData *data; | |
dpiTimestamp ts; | |
if (dpiContext_create(DPI_MAJOR_VERSION, DPI_MINOR_VERSION, &g_context, &err) < 0) { | |
printf("ERROR\n dpiContext_create()\n %s\n", err.message); | |
exit(1); | |
} | |
chkerr(dpiConn_create(g_context, username, strlen(username), password, strlen(password), | |
connectString, strlen(connectString), NULL, NULL, &conn)); | |
// fetch timestamp with time zone whose offset is '-03:30'. | |
chkerr(dpiConn_prepareStmt(conn, 0, sqltext1, strlen(sqltext1), NULL, 0, &stmt)); | |
chkerr(dpiStmt_execute(stmt, 0, &numQueryColumns)); | |
chkerr(dpiStmt_fetch(stmt, &found, &bufferRowIndex)); | |
if (!found) { | |
printf("No rows are found.\n"); | |
exit(1); | |
} | |
chkerr(dpiStmt_getQueryValue(stmt, 1, &nativeTypeNum, &data)); | |
ts = *dpiData_getTimestamp(data); | |
printf("Timestamp value:\n" | |
" year: %d\n" | |
" month: %u\n" | |
" day: %u\n" | |
" hour: %u\n" | |
" minute: %u\n" | |
" second: %u\n" | |
" fsecond %u\n" | |
" tzHourOffset: %d\n" | |
" tzMinuteOffset: %d\n\n", | |
ts.year, ts.month, ts.day, | |
ts.hour, ts.minute, ts.second, ts.fsecond, | |
ts.tzHourOffset, ts.tzMinuteOffset); | |
chkerr(dpiStmt_release(stmt)); | |
// bind the fetched timestamp | |
chkerr(dpiConn_newVar(conn, DPI_ORACLE_TYPE_TIMESTAMP_TZ, DPI_NATIVE_TYPE_TIMESTAMP, 1, 0, 0, 0, NULL, &var, &data)); | |
dpiData_setTimestamp(data, ts.year, ts.month, ts.day, ts.hour, | |
ts.minute, ts.second, ts.fsecond, | |
ts.tzHourOffset, ts.tzMinuteOffset); | |
chkerr(dpiConn_prepareStmt(conn, 0, sqltext2, strlen(sqltext2), NULL, 0, &stmt)); | |
chkerr(dpiStmt_bindByPos(stmt, 1, var)); | |
chkerr(dpiStmt_execute(stmt, 0, &numQueryColumns)); // error here! | |
printf("No error\n"); | |
// cleanup | |
chkerr(dpiStmt_release(stmt)); | |
chkerr(dpiConn_release(conn)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment