00001 #include "orasel.h"
00002
00003
00004 void open_sess(con_t* con)
00005 {
00006 sword status;
00007
00008 if ( OCIEnvCreate(&con->envhp, OCI_DEFAULT | OCI_NEW_LENGTH_SEMANTICS,
00009 NULL, NULL, NULL, NULL, 0, NULL) != OCI_SUCCESS
00010 || OCIHandleAlloc(con->envhp, (dvoid**)(dvoid*)&con->errhp,
00011 OCI_HTYPE_ERROR, 0, NULL) != OCI_SUCCESS
00012 || OCIHandleAlloc(con->envhp, (dvoid**)(dvoid*)&con->srvhp,
00013 OCI_HTYPE_SERVER, 0, NULL) != OCI_SUCCESS
00014 || OCIHandleAlloc(con->envhp, (dvoid**)(dvoid*)&con->svchp,
00015 OCI_HTYPE_SVCCTX, 0, NULL) != OCI_SUCCESS
00016 || OCIHandleAlloc(con->envhp, (dvoid**)(dvoid*)&con->authp,
00017 OCI_HTYPE_SESSION, 0, NULL) != OCI_SUCCESS
00018 || OCIHandleAlloc(con->envhp, (dvoid**)(dvoid*)&con->stmthp,
00019 OCI_HTYPE_STMT, 0, NULL) != OCI_SUCCESS)
00020 {
00021 errxit("no oracle memory left");
00022 }
00023
00024 status = OCIAttrSet(con->svchp, OCI_HTYPE_SVCCTX, con->srvhp, 0,
00025 OCI_ATTR_SERVER, con->errhp);
00026 if (status != OCI_SUCCESS) goto connect_err;
00027 status = OCIAttrSet(con->authp, OCI_HTYPE_SESSION,
00028 (text*)con->username->s, con->username->len,
00029 OCI_ATTR_USERNAME, con->errhp);
00030 if (status != OCI_SUCCESS) goto connect_err;
00031 status = OCIAttrSet(con->authp, OCI_HTYPE_SESSION,
00032 (text*)con->password->s, con->password->len,
00033 OCI_ATTR_PASSWORD, con->errhp);
00034 if (status != OCI_SUCCESS) goto connect_err;
00035 status = OCIAttrSet(con->svchp, OCI_HTYPE_SVCCTX, con->authp, 0,
00036 OCI_ATTR_SESSION, con->errhp);
00037 if (status != OCI_SUCCESS) goto connect_err;
00038 status = OCIServerAttach(con->srvhp, con->errhp, (OraText*)con->uri->s,
00039 con->uri->len, 0);
00040 if (status != OCI_SUCCESS) goto connect_err;
00041 status = OCISessionBegin(con->svchp, con->errhp, con->authp,
00042 OCI_CRED_RDBMS, OCI_DEFAULT);
00043 if (status != OCI_SUCCESS) {
00044 connect_err:
00045 oraxit(status, con);
00046 }
00047 }
00048
00049
00050 void send_req(con_t* con, const Str* req)
00051 {
00052 sword status;
00053
00054 status = OCIStmtPrepare(con->stmthp, con->errhp, (text*)req->s, req->len,
00055 OCI_NTV_SYNTAX, OCI_DEFAULT);
00056 if (status != OCI_SUCCESS) goto request_err;
00057 status = OCIStmtExecute(con->svchp, con->stmthp, con->errhp, 0, 0, NULL,
00058 NULL, OCI_STMT_SCROLLABLE_READONLY);
00059 if (status != OCI_SUCCESS) {
00060 request_err:
00061 fprintf(stderr, "%.*s\n", req->len, req->s);
00062 oraxit(status, con);
00063 }
00064 }
00065
00066