Created
September 2, 2017 06:59
-
-
Save kubo/aea497d746d3d1603882c86b450accc0 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 <inttypes.h> | |
#include <dpi.h> | |
static dpiContext *g_context; | |
#ifndef ARRAY_SIZE | |
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) | |
#endif | |
#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 *sqltext = "select ObjectCol from TestObjects where IntCol = 1"; | |
dpiErrorInfo err; | |
dpiConn *conn; | |
dpiStmt *stmt; | |
uint32_t numQueryColumns; | |
uint32_t bufferRowIndex; | |
dpiObjectAttr *attrs[7]; | |
dpiQueryInfo queryInfo; | |
int found; | |
dpiNativeTypeNum nativeTypeNum; | |
dpiData *objColValue, attrValue; | |
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)); | |
chkerr(dpiConn_prepareStmt(conn, 0, sqltext, strlen(sqltext), 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, &objColValue)); | |
chkerr(dpiStmt_getQueryInfo(stmt, 1, &queryInfo)); | |
chkerr(dpiObjectType_getAttributes(queryInfo.typeInfo.objectType, ARRAY_SIZE(attrs), attrs)); | |
printf("Getting ObjectCol.NumberValue as DPI_NATIVE_TYPE_DOUBLE\n"); | |
chkerr(dpiObject_getAttributeValue(objColValue->value.asObject, attrs[0], DPI_NATIVE_TYPE_DOUBLE, &attrValue)); | |
printf(" => %g\n", attrValue.value.asDouble); | |
printf("Getting ObjectCol.NumberValue as DPI_NATIVE_TYPE_INT64\n"); | |
if (dpiObject_getAttributeValue(objColValue->value.asObject, attrs[0], DPI_NATIVE_TYPE_INT64, &attrValue) == 0) { | |
printf(" => %" PRId64 "\n", attrValue.value.asInt64); | |
} else { | |
dpiContext_getError(g_context, &err); | |
printf(" => ERROR: %s\n", err.message); | |
} | |
printf("Getting ObjectCol.NumberValue as DPI_NATIVE_TYPE_BYTES\n"); | |
if (dpiObject_getAttributeValue(objColValue->value.asObject, attrs[0], DPI_NATIVE_TYPE_BYTES, &attrValue) == 0) { | |
printf(" => %.*s\n", attrValue.value.asBytes.length, attrValue.value.asBytes.ptr); | |
} else { | |
dpiContext_getError(g_context, &err); | |
printf(" => ERROR: %s\n", err.message); | |
} | |
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