Guest User

Untitled

a guest
Jan 7th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. // compile with:
  2. // gcc timezones.c odpi/embed/dpi.c -I"opdi/include"
  3.  
  4.  
  5. #include "odpi/include/dpi.h"
  6. #include <stdio.h> // printf
  7. #include <string.h> // strlen
  8.  
  9. const char *userName = "scott";
  10. const char *password = "tiger";
  11. const char *tns = "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.1.43)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=XE)))";
  12. int main(int argc, char **argv){
  13.  
  14. dpiContext *context = NULL;
  15. dpiConn *conn = NULL;
  16. dpiErrorInfo error;
  17. dpiContext_create(3, 0, &context, &error);
  18. dpiConn_create(context, userName, strlen(userName), password, strlen(password), tns, strlen(tns), NULL, NULL, &conn);
  19.  
  20. dpiStmt *alterStmt = NULL; // set time zone to a fixed value
  21. const char *alterSQL = "ALTER SESSION SET TIME_ZONE='10:55'";
  22. dpiConn_prepareStmt(conn, 0, alterSQL, strlen(alterSQL), NULL, 0, &alterStmt);
  23. dpiStmt_execute(alterStmt, DPI_MODE_EXEC_DEFAULT, NULL);
  24.  
  25. dpiStmt *dropStmt = NULL; // get rid of the table incase it exists from a previous run
  26. const char *dropSQL = "DROP TABLE timezones";
  27. dpiConn_prepareStmt(conn, 0, dropSQL, strlen(dropSQL), NULL, 0, &dropStmt);
  28. dpiStmt_execute(dropStmt, DPI_MODE_EXEC_DEFAULT, NULL);
  29.  
  30. dpiStmt *createStmt = NULL; // create the table
  31. const char *createSQL = "CREATE TABLE timezones (c_id NUMBER, c_tstz TIMESTAMP(9) WITH TIME ZONE)";
  32. dpiConn_prepareStmt(conn, 0, createSQL, strlen(createSQL), NULL, 0, &createStmt);
  33. dpiStmt_execute(createStmt, DPI_MODE_EXEC_DEFAULT, NULL);
  34.  
  35. dpiStmt *insertStmt = NULL; // insert timestamp with time zone, this works correctly
  36. const char * insertSQL = "INSERT INTO timezones VALUES(1, TIMESTAMP '2003-01-02 3:44:55.66 +7:08')";
  37. dpiConn_prepareStmt(conn, 0, insertSQL, strlen(insertSQL), NULL, 0, &insertStmt);
  38. dpiStmt_execute(insertStmt, DPI_MODE_EXEC_DEFAULT, NULL);
  39. dpiConn_commit(conn);
  40.  
  41. dpiStmt *bindStmt = NULL; // insert timestamp with time zone using bind, this doesn't work correctly
  42. const char *bindSQL = "INSERT INTO timezones VALUES(2, :A)";
  43. dpiConn_prepareStmt(conn, 0, bindSQL, strlen(bindSQL), NULL, 0, &bindStmt);
  44. dpiData bindData;
  45. dpiData_setTimestamp(&bindData, 2003, 1, 2, 3, 44, 55, /*fsec*/660000000, /*tzHour*/7, /*tzMin*/8);
  46. dpiTimestamp t0 = bindData.value.asTimestamp;
  47. printf("Binding timestamp: %4d-%.2d-%.2d %.2d:%.2d:%.2d.%.6d +%.2d:%.2d\n", t0.year, t0.month, t0.day, t0.hour, t0.minute, t0.second, t0.fsecond, t0.tzHourOffset, t0.tzMinuteOffset);
  48. dpiStmt_bindValueByPos(bindStmt, 1, DPI_NATIVE_TYPE_TIMESTAMP, &bindData);
  49. dpiStmt_execute(bindStmt, DPI_MODE_EXEC_DEFAULT, NULL);
  50. for (int i = 0; i < 2; i++){
  51. dpiStmt *fetchStmt = NULL; // get resulting timestamp
  52. char fetchSQL[64];
  53. sprintf(fetchSQL, "SELECT c_tstz FROM timezones where c_id = %d", i+1);
  54. dpiConn_prepareStmt(conn, 0, fetchSQL, strlen(fetchSQL), NULL, 0, &fetchStmt);
  55. dpiStmt_execute(fetchStmt, DPI_MODE_EXEC_DEFAULT, NULL);
  56. int found = 0;
  57. uint32_t bufferRowIndex = 0;
  58. dpiData *dataFetch = NULL;
  59. dpiNativeTypeNum type = 0;
  60. dpiStmt_fetch(fetchStmt, &found, &bufferRowIndex);
  61. dpiStmt_getQueryValue(fetchStmt, 1, &type, &dataFetch);
  62. dpiTimestamp t = dataFetch->value.asTimestamp;
  63. printf("Result timestamp [%d]: %4d-%.2d-%.2d %.2d:%.2d:%.2d.%.6d +%.2d:%.2d - %s\n",
  64. i+1, t.year, t.month, t.day, t.hour, t.minute, t.second, t.fsecond, t.tzHourOffset, t.tzMinuteOffset,
  65. i? "This is the timestamp that was bound" : "This is the timestamp that was inserted directly");
  66. }
  67. dpiConn_commit(conn);
  68. return 0;
  69. }
Add Comment
Please, Sign In to add comment