rccreate.c 15 KB

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