selcon.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "orasel.h"
  2. //-----------------------------------------------------------------------------
  3. void open_sess(con_t* con)
  4. {
  5. sword status;
  6. if ( OCIEnvCreate(&con->envhp, OCI_DEFAULT | OCI_NEW_LENGTH_SEMANTICS,
  7. NULL, NULL, NULL, NULL, 0, NULL) != OCI_SUCCESS
  8. || OCIHandleAlloc(con->envhp, (dvoid**)(dvoid*)&con->errhp,
  9. OCI_HTYPE_ERROR, 0, NULL) != OCI_SUCCESS
  10. || OCIHandleAlloc(con->envhp, (dvoid**)(dvoid*)&con->srvhp,
  11. OCI_HTYPE_SERVER, 0, NULL) != OCI_SUCCESS
  12. || OCIHandleAlloc(con->envhp, (dvoid**)(dvoid*)&con->svchp,
  13. OCI_HTYPE_SVCCTX, 0, NULL) != OCI_SUCCESS
  14. || OCIHandleAlloc(con->envhp, (dvoid**)(dvoid*)&con->authp,
  15. OCI_HTYPE_SESSION, 0, NULL) != OCI_SUCCESS
  16. || OCIHandleAlloc(con->envhp, (dvoid**)(dvoid*)&con->stmthp,
  17. OCI_HTYPE_STMT, 0, NULL) != OCI_SUCCESS)
  18. {
  19. errxit("no oracle memory left");
  20. }
  21. status = OCIAttrSet(con->svchp, OCI_HTYPE_SVCCTX, con->srvhp, 0,
  22. OCI_ATTR_SERVER, con->errhp);
  23. if (status != OCI_SUCCESS) goto connect_err;
  24. status = OCIAttrSet(con->authp, OCI_HTYPE_SESSION,
  25. (text*)con->username->s, con->username->len,
  26. OCI_ATTR_USERNAME, con->errhp);
  27. if (status != OCI_SUCCESS) goto connect_err;
  28. status = OCIAttrSet(con->authp, OCI_HTYPE_SESSION,
  29. (text*)con->password->s, con->password->len,
  30. OCI_ATTR_PASSWORD, con->errhp);
  31. if (status != OCI_SUCCESS) goto connect_err;
  32. status = OCIAttrSet(con->svchp, OCI_HTYPE_SVCCTX, con->authp, 0,
  33. OCI_ATTR_SESSION, con->errhp);
  34. if (status != OCI_SUCCESS) goto connect_err;
  35. status = OCIServerAttach(con->srvhp, con->errhp, (OraText*)con->uri->s,
  36. con->uri->len, 0);
  37. if (status != OCI_SUCCESS) goto connect_err;
  38. status = OCISessionBegin(con->svchp, con->errhp, con->authp,
  39. OCI_CRED_RDBMS, OCI_DEFAULT);
  40. if (status != OCI_SUCCESS) {
  41. connect_err:
  42. oraxit(status, con);
  43. }
  44. }
  45. //-----------------------------------------------------------------------------
  46. void send_req(con_t* con, const Str* req)
  47. {
  48. sword status;
  49. status = OCIStmtPrepare(con->stmthp, con->errhp, (text*)req->s, req->len,
  50. OCI_NTV_SYNTAX, OCI_DEFAULT);
  51. if (status != OCI_SUCCESS) goto request_err;
  52. status = OCIStmtExecute(con->svchp, con->stmthp, con->errhp, 0, 0, NULL,
  53. NULL, OCI_STMT_SCROLLABLE_READONLY);
  54. if (status != OCI_SUCCESS) {
  55. request_err:
  56. fprintf(stderr, "%.*s\n", req->len, req->s);
  57. oraxit(status, con);
  58. }
  59. }
  60. //-----------------------------------------------------------------------------