locreate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /***************************************************************************
  2. * PROPRIETARY DATA
  3. *
  4. * Licensed Materials - Property of IBM and/or HCL
  5. *
  6. * Copyright IBM Corporation 1997, 2013. All rights reserved.
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. * *************************************************************************
  10. *
  11. * Title: locreate.c
  12. *
  13. * Description: To create a smart large object and insert it into the
  14. * database
  15. * -- the column created is the 'advert' column of the
  16. * table 'item' (datatype - CLOB)
  17. *
  18. ***************************************************************************
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25. #ifndef NO_WIN32
  26. #include <io.h>
  27. #include <windows.h>
  28. #include <conio.h>
  29. #endif /*NO_WIN32*/
  30. #include <fcntl.h>
  31. #include "infxcli.h"
  32. #define BUFFER_LEN 25
  33. #define ERRMSG_LEN 200
  34. SQLCHAR defDsn[] = "odbc_demo";
  35. SQLINTEGER checkError (SQLRETURN rc,
  36. SQLSMALLINT handleType,
  37. SQLHANDLE handle,
  38. SQLCHAR* errmsg)
  39. {
  40. SQLRETURN retcode = SQL_SUCCESS;
  41. SQLSMALLINT errNum = 1;
  42. SQLCHAR sqlState[6];
  43. SQLINTEGER nativeError;
  44. SQLCHAR errMsg[ERRMSG_LEN];
  45. SQLSMALLINT textLengthPtr;
  46. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  47. {
  48. while (retcode != SQL_NO_DATA)
  49. {
  50. retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, ERRMSG_LEN, &textLengthPtr);
  51. if (retcode == SQL_INVALID_HANDLE)
  52. {
  53. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  54. return 1;
  55. }
  56. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  57. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  58. errNum++;
  59. }
  60. fprintf (stderr, "%s\n", errmsg);
  61. return 1; /* all errors on this handle have been reported */
  62. }
  63. else
  64. return 0; /* no errors to report */
  65. }
  66. int main (long argc,
  67. char* argv[])
  68. {
  69. /* Declare variables */
  70. /* Handles */
  71. SQLHDBC hdbc;
  72. SQLHENV henv;
  73. SQLHSTMT hstmt;
  74. /* Smart large object file descriptor */
  75. SQLLEN lofd;
  76. SQLLEN lofd_valsize = 0;
  77. /* Smart large object pointer structure */
  78. SQLCHAR* loptr_buffer;
  79. SQLSMALLINT loptr_size;
  80. SQLLEN loptr_valsize = 0;
  81. /* Smart large object specification structure */
  82. SQLCHAR* lospec_buffer;
  83. SQLSMALLINT lospec_size;
  84. SQLLEN lospec_valsize = 0;
  85. /* Write buffer */
  86. SQLCHAR* write_buffer;
  87. SQLSMALLINT write_size;
  88. SQLLEN write_valsize = 0;
  89. size_t status;
  90. struct stat statbuf;
  91. int fd;
  92. /* Miscellaneous variables */
  93. SQLCHAR dsn[20]; /*name of the DSN used for connecting to the database*/
  94. SQLRETURN rc = 0;
  95. SQLLEN in;
  96. SQLCHAR verInfoBuffer[BUFFER_LEN];
  97. SQLSMALLINT verInfoLen;
  98. SQLCHAR* lo_file_name = (SQLCHAR *) "advert.txt";
  99. SQLCHAR colname[BUFFER_LEN] = "item.advert";
  100. SQLLEN colname_size = SQL_NTS;
  101. SQLINTEGER mode = LO_RDWR;
  102. SQLLEN cbMode = 0;
  103. SQLCHAR* insertStmt = (SQLCHAR *) "INSERT INTO item VALUES (1005, 'Helmet', 235, 'Each', '39.95', ?)";
  104. /* STEP 1. Get data source name from command line (or use default)
  105. ** Allocate environment handle and set ODBC version
  106. ** Allocate connection handle
  107. ** Establish the database connection
  108. ** Get version information from the database server
  109. ** -- if version < 9.x (not UDO enabled), exit with error message
  110. ** Allocate the statement handle
  111. */
  112. /* If(dsn is not explicitly passed in as arg) */
  113. if (argc != 2)
  114. {
  115. /* Use default dsn - odbc_demo */
  116. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  117. strcpy ((char *)dsn, (char *)defDsn);
  118. }
  119. else
  120. {
  121. /* Use specified dsn */
  122. strcpy ((char *)dsn, (char *)argv[1]);
  123. fprintf (stdout, "\nUsing specified DSN : %s\n", dsn);
  124. }
  125. /* Allocate the Environment handle */
  126. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  127. if (rc != SQL_SUCCESS)
  128. {
  129. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!\n");
  130. return (1);
  131. }
  132. /* Set the ODBC version to 3.0 */
  133. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  134. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!\n"))
  135. return (1);
  136. /* Allocate the connection handle */
  137. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  138. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!\n"))
  139. return (1);
  140. /* Establish the database connection */
  141. rc = SQLConnect (hdbc, dsn, SQL_NTS, (SQLCHAR *) "", SQL_NTS, (SQLCHAR *) "", SQL_NTS);
  142. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\n"))
  143. return (1);
  144. /* Get version information from the database server
  145. If version < 9.x (not UDO enabled), exit with error message */
  146. rc = SQLGetInfo (hdbc, SQL_DBMS_VER, verInfoBuffer, BUFFER_LEN, &verInfoLen);
  147. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLGetInfo failed\n"))
  148. return 1;
  149. if ((strncmp ((char *) verInfoBuffer, "09", 2)) < 0 )
  150. {
  151. fprintf (stdout, "\n** This test can only be run against database server -- version 9 or higher **\n");
  152. return 1;
  153. }
  154. /* Allocate the statement handle */
  155. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  156. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!\n"))
  157. return (1);
  158. fprintf (stdout, "STEP 1 done...connected to database\n");
  159. /* STEP 2. Get the size of the smart large object specification structure
  160. ** Allocate a buffer to hold the structure
  161. ** Create a default smart large object specification structure
  162. ** Reset the statement parameters
  163. */
  164. /* Get the size of a smart large object specification structure */
  165. rc = SQLGetInfo (hdbc, SQL_INFX_LO_SPEC_LENGTH, &lospec_size,
  166. sizeof(lospec_size), NULL);
  167. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 2 -- SQLGetInfo failed\n"))
  168. goto Exit;
  169. /* Allocate a buffer to hold the smart large object specification
  170. structure*/
  171. lospec_buffer = malloc (lospec_size);
  172. if (lospec_buffer == NULL)
  173. {
  174. fprintf(stdout, "Failed to allocate memory for the largeobject specification buffer \n");
  175. goto Exit;
  176. }
  177. /* Create a default smart large object specification structure */
  178. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT_OUTPUT, SQL_C_BINARY,
  179. SQL_INFX_UDT_FIXED, (UDWORD)lospec_size, 0,
  180. lospec_buffer, lospec_size, &lospec_valsize);
  181. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLBindParameter failed\n"))
  182. goto Exit;
  183. rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_def_create_spec(?)}", SQL_NTS);
  184. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
  185. goto Exit;
  186. /* Reset the statement parameters */
  187. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  188. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLFreeStmt failed\n"))
  189. goto Exit;
  190. fprintf (stdout, "STEP 2 done...default smart large object specification structure created\n");
  191. /* STEP 3. Initialise the smart large object specification structure with
  192. ** values for the database column where the smart large object is
  193. ** being inserted
  194. ** Reset the statement parameters
  195. */
  196. /* Initialise the smart large object specification structure */
  197. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
  198. BUFFER_LEN, 0, colname, BUFFER_LEN, &colname_size);
  199. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter failed (param 1)\n"))
  200. goto Exit;
  201. lospec_valsize = lospec_size;
  202. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT_OUTPUT, SQL_C_BINARY,
  203. SQL_INFX_UDT_FIXED, (UDWORD)lospec_size, 0, lospec_buffer,
  204. lospec_size, &lospec_valsize);
  205. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter failed (param 2)\n"))
  206. goto Exit;
  207. rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_col_info(?, ?)}", SQL_NTS);
  208. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  209. goto Exit;
  210. /* Reset the statement parameters */
  211. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  212. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFreeStmt failed\n"))
  213. goto Exit;
  214. fprintf(stdout, "STEP 3 done...smart large object specification structure initialised\n");
  215. /* STEP 4. Get the size of the smart large object pointer structure
  216. ** Allocate a buffer to hold the structure.
  217. */
  218. /* Get the size of the smart large object pointer structure */
  219. rc = SQLGetInfo (hdbc, SQL_INFX_LO_PTR_LENGTH, &loptr_size, sizeof(loptr_size), NULL);
  220. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 4 -- SQLGetInfo failed\n"))
  221. goto Exit;
  222. /* Allocate a buffer to hold the smart large object pointer structure */
  223. loptr_buffer = malloc (loptr_size);
  224. if (loptr_buffer == NULL)
  225. {
  226. fprintf(stdout, "Failed to allocate memory for the smart large object pointer structure buffer \n");
  227. goto Exit;
  228. }
  229. fprintf (stdout, "STEP 4 done...smart large object pointer structure allocated\n");
  230. /* STEP 5. Create a new smart large object.
  231. ** Reset the statement parameters
  232. */
  233. /* Create a new smart large object */
  234. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_INFX_UDT_FIXED,
  235. (UDWORD)lospec_size, 0, lospec_buffer, lospec_size, &lospec_valsize);
  236. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 1)\n"))
  237. goto Exit;
  238. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER,
  239. (UDWORD)0, 0, &mode, sizeof(mode), &cbMode);
  240. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 2)\n"))
  241. goto Exit;
  242. loptr_valsize = loptr_size;
  243. rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT_OUTPUT, SQL_C_BINARY, SQL_INFX_UDT_FIXED,
  244. (UDWORD)loptr_size, 0, loptr_buffer, loptr_size, &loptr_valsize);
  245. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 3)\n"))
  246. goto Exit;
  247. rc = SQLBindParameter (hstmt, 4, SQL_PARAM_OUTPUT, SQL_C_SLONG, SQL_INTEGER,
  248. (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);
  249. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 4)\n"))
  250. goto Exit;
  251. rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_create(?, ?, ?, ?)}", SQL_NTS);
  252. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLExecDirect failed\n"))
  253. goto Exit;
  254. /* Reset the statement parameters */
  255. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  256. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLFreeStmt failed\n"))
  257. goto Exit;
  258. fprintf (stdout, "STEP 5 done...smart large object created\n");
  259. /* STEP 6. Open the file containing data for the new smart large object
  260. ** Allocate a buffer to hold the smart large object data
  261. ** Read data from the input file into the smart large object data buffer
  262. ** Write data from the data buffer into the new smart large object.
  263. ** Reset the statement parameters
  264. */
  265. /* Get the size of the file containing data for the new smart large object */
  266. if (stat( (char *)lo_file_name,&statbuf) == -1)
  267. {
  268. fprintf (stdout, "Error %d reading %s\n", errno, lo_file_name);
  269. exit(1);
  270. }
  271. write_size = statbuf.st_size;
  272. /* Allocate a buffer to hold the smart large object data */
  273. write_buffer = malloc (write_size + 1);
  274. if (write_buffer == NULL)
  275. {
  276. fprintf(stdout, "Failed to allocate memory for the smart large object data buffer \n");
  277. goto Exit;
  278. }
  279. /* Read smart large object data from file */
  280. fd = open ((char *) lo_file_name, O_RDONLY);
  281. if (fd == -1)
  282. {
  283. fprintf (stdout, "Error %d creating file descriptor for %s\n", errno, lo_file_name);
  284. exit(1);
  285. }
  286. status = read (fd, write_buffer, write_size);
  287. if (status < 0)
  288. {
  289. fprintf (stdout, "Error %d reading %s\n", errno, lo_file_name);
  290. }
  291. if (close(fd) < 0)
  292. {
  293. fprintf (stdout, "Error %d closing the file %s\n", errno, lo_file_name);
  294. exit(1);
  295. }
  296. write_buffer[write_size] = '\0';
  297. write_valsize = write_size;
  298. /* Write data from the data buffer into the new smart large object */
  299. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER,
  300. (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);
  301. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLBindParameter failed (param 1)\n"))
  302. goto Exit;
  303. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR,
  304. (UDWORD)write_size, 0, write_buffer, write_size, &write_valsize);
  305. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLBindParameter failed (param 2)\n"))
  306. goto Exit;
  307. rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_write(?, ?)}", SQL_NTS);
  308. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLExecDirect failed\n"))
  309. goto Exit;
  310. /* Reset the statement parameters */
  311. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  312. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLFreeStmt failed\n"))
  313. goto Exit;
  314. fprintf (stdout, "STEP 6 done...data written to new smart large object\n");
  315. /* STEP 7. Insert the new smart large object into the database.
  316. ** Reset the statement parameters
  317. */
  318. /* Insert the new smart large object into the database */
  319. loptr_valsize = loptr_size;
  320. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_INFX_UDT_FIXED,
  321. (UDWORD)loptr_size, 0, loptr_buffer, loptr_size, &loptr_valsize);
  322. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLBindParameter failed\n"))
  323. goto Exit;
  324. rc = SQLExecDirect (hstmt, insertStmt, SQL_NTS);
  325. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLExecDirect failed\n"))
  326. goto Exit;
  327. /* Reset the statement parameters */
  328. rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS);
  329. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 7 -- SQLFreeStmt failed\n"))
  330. goto Exit;
  331. fprintf (stdout, "STEP 7 done...smart large object inserted into the database\n");
  332. /* STEP 8. Close the smart large object. */
  333. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER,
  334. (UDWORD)0, 0, &lofd, sizeof(lofd), &lofd_valsize);
  335. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 8 -- SQLBindParameter failed\n"))
  336. goto Exit;
  337. rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call ifx_lo_close(?)}", SQL_NTS);
  338. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 8 -- SQLExecDirect failed\n"))
  339. goto Exit;
  340. fprintf (stdout, "STEP 8 done...smart large object closed\n");
  341. /* STEP 9. Free the allocated buffers */
  342. if (lospec_buffer)
  343. free (lospec_buffer);
  344. if (loptr_buffer)
  345. free (loptr_buffer);
  346. if (write_buffer)
  347. free (write_buffer);
  348. fprintf (stdout, "STEP 9 done...smart large object buffers freed\n");
  349. Exit:
  350. /* CLEANUP: Close the statement handle
  351. ** Free the statement handle
  352. ** Disconnect from the datasource
  353. ** Free the connection and environment handles
  354. ** Exit
  355. */
  356. /* Close the statement handle */
  357. SQLFreeStmt (hstmt, SQL_CLOSE);
  358. /* Free the statement handle */
  359. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  360. /* Disconnect from the data source */
  361. SQLDisconnect (hdbc);
  362. /* Free the environment handle and the database connection handle */
  363. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  364. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  365. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  366. in = getchar ();
  367. return (rc);
  368. }