Skip to content

Instantly share code, notes, and snippets.

@kubo
Created July 9, 2019 12:56
Show Gist options
  • Save kubo/b7ddbe5a0a76629ca3bd08cd2eb862af to your computer and use it in GitHub Desktop.
Save kubo/b7ddbe5a0a76629ca3bd08cd2eb862af to your computer and use it in GitHub Desktop.
/*
* ORACLE_HOME-based client:
*
* gcc -o test218 test218.c -I$ORACLE_HOME/rdbms/public -L$ORACLE_HOME/lib -Wl,-rpath,$ORACLE_HOME/lib -lclntsh
*
* Instant client:
*
* IC_DIR=/xxxx # Instant client directory
* gcc -o test218 test218.c -I$IC_DIR/sdk/include -L$IC_DIR -Wl,-rpath,$IC_DIR -lclntsh
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <oci.h>
char *username = "ruby";
char *password = "oci8";
char *database = "//localhost/ora183";
char *object_name = "USER1.TEST_PKG.TEST_PROG";
static void report_error(void *handle, ub4 htype, sword status, const char *func)
{
text errbuf[512];
sb4 errcode = 0;
switch (status) {
case OCI_SUCCESS:
case OCI_SUCCESS_WITH_INFO:
break;
case OCI_NEED_DATA:
printf(" %s => OCI_NEED_DATA\n", func);
break;
case OCI_NO_DATA:
printf(" %s => OCI_NO_DATA\n", func);
break;
case OCI_ERROR:
OCIErrorGet(handle, 1, NULL, &errcode, errbuf, sizeof(errbuf), htype);
printf(" %s => OCI_ERROR\n %.*s\n", func, (int)sizeof(errbuf), errbuf);
break;
case OCI_INVALID_HANDLE:
printf(" %s => OCI_INVALID_HANDLE\n", func);
break;
case OCI_STILL_EXECUTING:
printf(" %s => OCI_STILL_EXECUTE\n", func);
break;
case OCI_CONTINUE:
printf(" %s => OCI_CONTINUE\n", func);
break;
default:
printf(" %s => Unknown %d\n", func, status);
}
exit(1);
}
int main()
{
OCIEnv *envhp;
OCIError *errhp;
OCISvcCtx *svchp;
OCISession *seshp;
OCIServer *srvhp;
OCIDescribe *dschp1;
sword rv;
// Allocate handles
printf("OCIEnvCreate\n");
if ((rv = OCIEnvCreate(&envhp, OCI_OBJECT | OCI_THREADED, NULL, NULL, NULL, NULL, 0, NULL)) != OCI_SUCCESS) {
report_error(envhp, OCI_HTYPE_ENV, rv, "OCIEnvCreate");
}
printf("OCIHandleAlloc(ERROR)\n");
if ((rv = OCIHandleAlloc(envhp, (void*)&errhp, OCI_HTYPE_ERROR, 0, NULL)) != OCI_SUCCESS) {
report_error(envhp, OCI_HTYPE_ENV, rv, "OCIHandleAlloc(ERROR)");
}
printf("OCIHandleAlloc(SVCCTX)\n");
if ((rv = OCIHandleAlloc(envhp, (void*)&svchp, OCI_HTYPE_SVCCTX, 0, NULL)) != OCI_SUCCESS) {
report_error(envhp, OCI_HTYPE_ENV, rv, "OCIHandleAlloc(SVCCTX)");
}
printf("OCIHandleAlloc(SESSION)\n");
if ((rv = OCIHandleAlloc(envhp, (void*)&seshp, OCI_HTYPE_SESSION, 0, NULL)) != OCI_SUCCESS) {
report_error(envhp, OCI_HTYPE_ENV, rv, "OCIHandleAlloc(SESSION)");
}
printf("OCIHandleAlloc(SERVER)\n");
if ((rv = OCIHandleAlloc(envhp, (void*)&srvhp, OCI_HTYPE_SERVER, 0, NULL)) != OCI_SUCCESS) {
report_error(envhp, OCI_HTYPE_ENV, rv, "OCIHandleAlloc(SERVER)");
}
// Connect
printf("OCIAttrSet(USERNAME)\n");
if ((rv = OCIAttrSet(seshp, OCI_HTYPE_SESSION, username, strlen(username), OCI_ATTR_USERNAME, errhp)) != OCI_SUCCESS) {
report_error(errhp, OCI_HTYPE_ERROR, rv, "OCIAttrSet(USERNAME)");
}
printf("OCIAttrSet(PASSWORD)\n");
if ((rv = OCIAttrSet(seshp, OCI_HTYPE_SESSION, password, strlen(password), OCI_ATTR_PASSWORD, errhp)) != OCI_SUCCESS) {
report_error(errhp, OCI_HTYPE_ERROR, rv, "OCIAttrSet(PASSWORD)");
}
printf("OCIServerAttach\n");
if ((rv = OCIServerAttach(srvhp, errhp, (text*)database, strlen(database), OCI_DEFAULT)) != OCI_SUCCESS) {
report_error(errhp, OCI_HTYPE_ERROR, rv, "OCIServerAttach");
}
printf("OCIAttrSet(SERVER)\n");
if ((rv = OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, srvhp, 0, OCI_ATTR_SERVER, errhp)) != OCI_SUCCESS) {
report_error(errhp, OCI_HTYPE_ERROR, rv, "OCIAttrSet(SERVER)");
}
printf("OCISessionBegin\n");
if ((rv = OCISessionBegin(svchp, errhp, seshp, OCI_CRED_RDBMS, 0)) != OCI_SUCCESS) {
report_error(errhp, OCI_HTYPE_ERROR, rv, "OCISessionBegin");
}
printf("OCIAttrSet(SESSION)\n");
if ((rv = OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, seshp, 0, OCI_ATTR_SESSION, errhp)) != OCI_SUCCESS) {
report_error(errhp, OCI_HTYPE_ERROR, rv, "OCIAttrSet(SESSION");
}
// Describe
printf("OCIHandleAlloc(DESCRIBE)\n");
if ((rv = OCIHandleAlloc(envhp, (void*)&dschp1, OCI_HTYPE_DESCRIBE, 0, NULL)) != OCI_SUCCESS) {
report_error(envhp, OCI_HTYPE_ENV, rv, "OCIHandleAlloc(DESCRIBE)");
}
printf("OCIDescribeAny\n");
if ((rv = OCIDescribeAny(svchp, errhp, object_name, strlen(object_name), OCI_OTYPE_NAME, OCI_DEFAULT, OCI_PTYPE_UNK, dschp1)) != OCI_SUCCESS) {
report_error(errhp, OCI_HTYPE_ERROR, rv, "OCIDescribeAny");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment