rccreateW.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /***************************************************************************
  2. * Licensed Materials - Property of IBM and/or HCL
  3. *
  4. * IBM Informix Client-SDK
  5. *
  6. * Copyright IBM Corporation 1997, 2013 All rights reserved.
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. *
  10. *
  11. *
  12. *
  13. * Title: rccreateW.c (Unicode counterpart of rccreate.c)
  14. *
  15. * Description: To create a row and a list on the client, add items
  16. * to them and insert them into the database
  17. * The columns created are
  18. * -- column 'address' of table 'customer' (datatype - ROW)
  19. * -- column 'contact_dates' of table 'customer'
  20. * (datatype - LIST)
  21. *
  22. *
  23. ***************************************************************************
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #ifndef NO_WIN32
  29. #include <io.h>
  30. #include <windows.h>
  31. #include <conio.h>
  32. #endif /*NO_WIN32*/
  33. #include "infxcli.h"
  34. #define BUFFER_LENW 120
  35. #define ERRMSG_LEN 200
  36. SQLWCHAR defDsnW[] = L"odbc_demo";
  37. SQLCHAR defDsn[] = "odbc_demo";
  38. SQLINTEGER checkError (SQLRETURN rc,
  39. SQLSMALLINT handleType,
  40. SQLHANDLE handle,
  41. SQLCHAR* errmsg)
  42. {
  43. SQLRETURN retcode = SQL_SUCCESS;
  44. SQLSMALLINT errNum = 1;
  45. SQLWCHAR sqlStateW[6];
  46. SQLCHAR *sqlState;
  47. SQLINTEGER nativeError;
  48. SQLWCHAR errMsgW[ERRMSG_LEN];
  49. SQLCHAR *errMsg;
  50. SQLSMALLINT textLengthPtr;
  51. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  52. {
  53. while (retcode != SQL_NO_DATA)
  54. {
  55. retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, ERRMSG_LEN, &textLengthPtr);
  56. if (retcode == SQL_INVALID_HANDLE)
  57. {
  58. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  59. return 1;
  60. }
  61. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  62. {
  63. sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char))
  64. ;
  65. wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
  66. + sizeof(char));
  67. errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
  68. wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
  69. + sizeof(char));
  70. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState , errMsg);
  71. }
  72. errNum++;
  73. }
  74. fprintf (stderr, "%s\n", errmsg);
  75. return 1; /* all errors on this handle have been reported */
  76. }
  77. else
  78. return 0; /* no errors to report */
  79. }
  80. int main (long argc,
  81. char* argv[] )
  82. {
  83. /* Declare variables
  84. */
  85. /* Handles */
  86. SQLHDBC hdbc;
  87. SQLHENV henv;
  88. SQLHSTMT hstmt;
  89. HINFX_RC hrow;
  90. HINFX_RC hlist;
  91. /* Miscellaneous variables */
  92. SQLWCHAR *dsnW; /*name of the DSN used for connecting to the database*/
  93. SQLRETURN rc = 0;
  94. SQLINTEGER i, in;
  95. SQLWCHAR verInfoBufferW[BUFFER_LENW];
  96. SQLSMALLINT verInfoLen;
  97. SQLLEN data_size = SQL_NTS;
  98. SQLSMALLINT position = SQL_INFX_RC_ABSOLUTE;
  99. SQLSMALLINT jump;
  100. SQLWCHAR row_dataW[4][BUFFER_LENW] = {L"520 Topaz Way", L"Redwood City", L"CA", L"94062"};
  101. SQLLEN row_data_size = SQL_NTS;
  102. SQLWCHAR list_dataW[2][BUFFER_LENW] = {L"1991-06-20", L"1993-07-17"};
  103. SQLLEN list_data_size = SQL_NTS;
  104. SQLWCHAR* insertStmtW = (SQLWCHAR *) L"INSERT INTO customer VALUES (110, 'Roy', 'Jaeger', ?, ?)";
  105. SQLLEN cbHrow = 0, cbHlist = 0, cbPosition = 0, cbJump = 0;
  106. int lenArgv1;
  107. /* STEP 1. Get data source name from command line (or use default)
  108. ** Allocate environment handle and set ODBC version
  109. ** Allocate connection handle
  110. ** Establish the database connection
  111. ** Get version information from the database server
  112. ** -- if version < 9.x (not UDO enabled), exit with error message
  113. ** Allocate the statement handle
  114. */
  115. /* If(dsn is not explicitly passed in as arg) */
  116. if (argc != 2)
  117. {
  118. /* Use default dsnW - odbc_demo */
  119. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  120. dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
  121. + sizeof(SQLWCHAR) );
  122. wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
  123. }
  124. else
  125. {
  126. /* Use specified dsnW */
  127. lenArgv1 = strlen((char *)argv[1]);
  128. dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
  129. + sizeof(SQLWCHAR));
  130. mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
  131. fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
  132. }
  133. /* Allocate the Environment handle */
  134. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  135. if (rc != SQL_SUCCESS)
  136. {
  137. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!\n");
  138. return (1);
  139. }
  140. /* Set the ODBC version to 3.0 */
  141. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  142. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!\n"))
  143. return (1);
  144. /* Allocate the connection handle */
  145. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  146. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!\n"))
  147. return (1);
  148. /* Establish the database connection */
  149. rc = SQLConnectW (hdbc, dsnW, SQL_NTS, (SQLWCHAR *) L"", SQL_NTS, (SQLWCHAR *) L"", SQL_NTS);
  150. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\n"))
  151. return (1);
  152. /* Get version information from the database server
  153. If version < 9.x (not UDO enabled), exit with error message */
  154. rc = SQLGetInfoW (hdbc, SQL_DBMS_VER, verInfoBufferW, BUFFER_LENW, &verInfoLen);
  155. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLGetInfo failed\n"))
  156. return 1;
  157. if ((wcsncmp ((SQLWCHAR *) verInfoBufferW, L"09", 2)) < 0)
  158. {
  159. fprintf (stdout, "\n** This test can only be run against UDO-enabled database server -- version 9 or higher **\n");
  160. return 1;
  161. }
  162. /* Allocate the statement handle */
  163. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  164. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!\n"))
  165. return (1);
  166. fprintf (stdout, "STEP 1 done...connected to database\n");
  167. /* STEP 2. Allocate fixed-type row handle -- this creates a non-null row
  168. ** buffer, each of whose values is null, and can be updated.
  169. ** Allocate a fixed-type list handle -- this creates a non-null
  170. ** but empty list buffer into which values can be inserted.
  171. ** Reset the statement parameters
  172. */
  173. /* Allocate a fixed-type row handle -- this creates a row with each value empty */
  174. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_BINARY,
  175. SQL_INFX_RC_ROW, sizeof(HINFX_RC), 0,
  176. &hrow, sizeof(HINFX_RC), &cbHrow);
  177. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter (param 1) failed for row handle\n"))
  178. goto Exit;
  179. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
  180. SQL_CHAR, 0, 0, "ROW(address1 VARCHAR(25), city VARCHAR(15),\
  181. state VARCHAR(15), zip VARCHAR(5))", 0, &data_size);
  182. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter (param 2) failed for row handle\n"))
  183. goto Exit;
  184. rc = SQLExecDirectW (hstmt, (SQLWCHAR *) L"{? = call ifx_rc_create(?)}", SQL_NTS);
  185. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed for row handle\n"))
  186. goto Exit;
  187. /* Allocate a fixed-type list handle */
  188. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_OUTPUT, SQL_C_BINARY,
  189. SQL_INFX_RC_LIST, sizeof(HINFX_RC), 0,
  190. &hlist, sizeof(HINFX_RC), &cbHlist);
  191. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter (param 1) failed for list handle\n"))
  192. goto Exit;
  193. data_size = SQL_NTS;
  194. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
  195. SQL_CHAR, 0, 0, (SQLCHAR *) "LIST (DATETIME YEAR TO DAY NOT NULL)", 0, &data_size);
  196. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter (param 2) failed for list handle\n"))
  197. goto Exit;
  198. rc = SQLExecDirectW (hstmt, (SQLWCHAR *) L"{? = call ifx_rc_create(?)}", SQL_NTS);
  199. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed for list handle\n"))
  200. goto Exit;
  201. /* Reset the statement parameters */
  202. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  203. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLFreeStmt failed\n"))
  204. goto Exit;
  205. fprintf (stdout, "STEP 2 done...fixed-type row and collection handles allocated\n");
  206. /* STEP 3. Update the elements of the fixed-type row buffer allocated
  207. ** Insert elements into the fixed-type list buffer allocated
  208. ** Reset the statement parameters
  209. */
  210. /* Update elements of the row buffer */
  211. for (i=0; i<4; i++)
  212. {
  213. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  214. SQL_INFX_RC_ROW, sizeof(HINFX_RC), 0, hrow,
  215. sizeof(HINFX_RC), &cbHrow);
  216. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter (param 1) failed for row handle\n"))
  217. goto Exit;
  218. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_WCHAR,
  219. SQL_CHAR, BUFFER_LENW, 0, row_dataW[i], 0, &row_data_size);
  220. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter (param 2) failed for row handle\n"))
  221. goto Exit;
  222. rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT, SQL_C_SHORT,
  223. SQL_SMALLINT, 0, 0, &position, 0, &cbPosition);
  224. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter (param 3) failed for row handle\n"))
  225. goto Exit;
  226. jump = i + 1;
  227. rc = SQLBindParameter (hstmt, 4, SQL_PARAM_INPUT, SQL_C_SHORT,
  228. SQL_SMALLINT, 0, 0, &jump, 0, &cbJump);
  229. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter (param 4) failed for row handle\n"))
  230. goto Exit;
  231. rc = SQLExecDirectW (hstmt, (SQLWCHAR *)L"{call ifx_rc_update(?, ?, ?, ?)}", SQL_NTS);
  232. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed for row handle\n"))
  233. goto Exit;
  234. }
  235. /* Insert elements into the list buffer */
  236. for (i=0; i<2; i++)
  237. {
  238. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  239. SQL_INFX_RC_LIST, sizeof(HINFX_RC), 0, hlist,
  240. sizeof(HINFX_RC), &cbHlist);
  241. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter (param 1) failed for list handle\n"))
  242. goto Exit;
  243. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_WCHAR,
  244. SQL_DATE, 25, 0, list_dataW[i], 0, &list_data_size);
  245. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter (param 2) failed for list handle\n"))
  246. goto Exit;
  247. rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT, SQL_C_SHORT,
  248. SQL_SMALLINT, 0, 0, &position, 0, &cbPosition);
  249. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter (param 3) failed for list handle\n"))
  250. goto Exit;
  251. jump = i + 1;
  252. rc = SQLBindParameter (hstmt, 4, SQL_PARAM_INPUT, SQL_C_SHORT,
  253. SQL_SMALLINT, 0, 0, &jump, 0, &cbJump);
  254. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter (param 4) failed for list handle\n"))
  255. goto Exit;
  256. rc = SQLExecDirectW (hstmt, (SQLWCHAR *)L"{call ifx_rc_insert( ?, ?, ?, ? )}", SQL_NTS);
  257. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed for list handle\n"))
  258. goto Exit;
  259. }
  260. /* Reset the statement parameters */
  261. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  262. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFreeStmt failed\n"))
  263. goto Exit;
  264. fprintf (stdout, "STEP 3 done...row and list buffers populated\n");
  265. /* STEP 4. Bind paramters for the row and list handles
  266. ** Execute the insert statement to insert the new row
  267. ** into table 'customer'
  268. */
  269. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  270. SQL_INFX_RC_COLLECTION, sizeof(HINFX_RC), 0, hrow,
  271. sizeof(HINFX_RC), &cbHrow);
  272. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter failed (param 1)\n"))
  273. goto Exit;
  274. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_BINARY,
  275. SQL_INFX_RC_COLLECTION, sizeof(HINFX_RC), 0, hlist,
  276. sizeof(HINFX_RC), &cbHlist);
  277. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLBindParameter failed (param 2)\n"))
  278. goto Exit;
  279. rc = SQLExecDirectW (hstmt, insertStmtW, SQL_NTS);
  280. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed\n"))
  281. goto Exit;
  282. fprintf (stdout, "STEP 4 done...new row inserted into table 'customer'\n");
  283. /* STEP 5. Free the row and list handles
  284. */
  285. /* Free the row handle */
  286. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  287. SQL_INFX_RC_ROW, sizeof(HINFX_RC), 0, hrow,
  288. sizeof(HINFX_RC), &cbHrow);
  289. rc = SQLExecDirectW (hstmt, (SQLWCHAR *)L"{call ifx_rc_free(?)}", SQL_NTS);
  290. /* Free the list handle */
  291. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  292. SQL_INFX_RC_LIST, sizeof(HINFX_RC), 0, hlist,
  293. sizeof(HINFX_RC), &cbHlist);
  294. rc = SQLExecDirectW (hstmt, (SQLWCHAR *)L"{call ifx_rc_free(?)}", SQL_NTS);
  295. fprintf (stdout, "STEP 5 done...row and list handles freed\n");
  296. Exit:
  297. /* CLEANUP: Close the statement handle
  298. ** Free the statement handle
  299. ** Disconnect from the datasource
  300. ** Free the connection and environment handles
  301. ** Exit
  302. */
  303. /* Close the statement handle */
  304. SQLFreeStmt (hstmt, SQL_CLOSE);
  305. /* Free the statement handle */
  306. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  307. /* Disconnect from the data source */
  308. SQLDisconnect (hdbc);
  309. /* Free the environment handle and the database connection handle */
  310. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  311. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  312. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  313. in = getchar ();
  314. return (rc);
  315. }