dbcreateW.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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: dbcreateW.c (Unicode counterpart of dbcreate.c)
  14. *
  15. * Description: To create & populate the sample database. The schema
  16. * for the sample database is as follows -
  17. *
  18. * For servers supporting UDO (version no. > 9)
  19. *
  20. * Database - odbc_demodb (with log)
  21. *
  22. * Table - CUSTOMER
  23. * cust_num INTEGER, PRIMARY KEY
  24. * fname VARCHAR(10)
  25. * lname VARCHAR(10)
  26. * address ROW (address1 VARCHAR(20),
  27. * city VARCHAR(15),
  28. * state VARCHAR(5),
  29. * zip VARCHAR(6)
  30. * )
  31. * contact_dates LIST (DATETIME YEAR TO DAY NOT NULL)
  32. *
  33. *
  34. * Table - ITEM
  35. * item_num INTEGER, PRIMARY KEY
  36. * description VARCHAR(20)
  37. * stock SMALLINT
  38. * ship_unit VARCHAR(10)
  39. * advert CLOB
  40. * unit_price DISTINCT TYPE dollar
  41. *
  42. *
  43. * Table - ORDERS
  44. * order_num INTEGER, PRIMARY KEY
  45. * cust_num INTEGER, FOREIGN KEY, REFERENCES customer(customer_num)
  46. * item_num INTEGER, FOREIGN KEY, REFERENCES item(item_num)
  47. * order_date DATE
  48. * quantity INTEGER
  49. *
  50. *
  51. *
  52. * For servers not supporting UDO (version no. < 9)
  53. *
  54. * Database - odbc_demodb (with log)
  55. *
  56. * Table - CUSTOMER
  57. * cust_num INTEGER, PRIMARY KEY
  58. * fname VARCHAR(10)
  59. * lname VARCHAR(10)
  60. * address1 VARCHAR(20)
  61. * city VARCHAR(15)
  62. * state VARCHAR(5)
  63. * zip VARCHAR(6)
  64. * first_contact DATETIME YEAR TO DAY
  65. * last_contact DATETIME YEAR TO DAY
  66. *
  67. *
  68. * Table - ITEM
  69. * item_num INTEGER, PRIMARY KEY
  70. * description VARCHAR(20)
  71. * stock SMALLINT
  72. * ship_unit VARCHAR(10)
  73. * advert VARCHAR(100)
  74. * unit_price DECIMAL
  75. *
  76. *
  77. * Table - ORDERS
  78. * order_num INTEGER, PRIMARY KEY
  79. * cust_num INTEGER, FOREIGN KEY, REFERENCES customer(customer_num)
  80. * item_num INTEGER, FOREIGN KEY, REFERENCES item(item_num)
  81. * order_date DATE
  82. * quantity INTEGER
  83. *
  84. *
  85. ***************************************************************************
  86. */
  87. #include <stdio.h>
  88. #include <stdlib.h>
  89. #include <string.h>
  90. #ifndef NO_WIN32
  91. #include <io.h>
  92. #include <windows.h>
  93. #include <conio.h>
  94. #endif /*NO_WIN32*/
  95. #include "infxcli.h"
  96. #include "dbcreateW.h"
  97. #define ERRMSG_LEN 200
  98. SQLWCHAR defDsnW[] = L"odbc_demo";
  99. SQLCHAR defDsn[] = "odbc_demo";
  100. SQLINTEGER checkError (SQLRETURN rc,
  101. SQLSMALLINT handleType,
  102. SQLHANDLE handle,
  103. SQLCHAR* errmsg)
  104. {
  105. SQLRETURN retcode = SQL_SUCCESS;
  106. SQLSMALLINT errNum = 1;
  107. SQLWCHAR sqlStateW[6];
  108. SQLCHAR *sqlState;
  109. SQLINTEGER nativeError;
  110. SQLWCHAR errMsgW[ERRMSG_LEN];
  111. SQLCHAR *errMsg;
  112. SQLSMALLINT textLengthPtr;
  113. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  114. {
  115. while (retcode != SQL_NO_DATA)
  116. {
  117. retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, ERRMSG_LEN, &textLengthPtr);
  118. if (retcode == SQL_INVALID_HANDLE)
  119. {
  120. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  121. return 1;
  122. }
  123. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  124. {
  125. sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char));
  126. wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
  127. + sizeof(char));
  128. errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
  129. wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
  130. + sizeof(char));
  131. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  132. }
  133. errNum++;
  134. }
  135. fprintf (stderr, "%s\n", errmsg);
  136. return 1; /* all errors on this handle have been reported */
  137. }
  138. else
  139. return 0; /* no errors to report */
  140. }
  141. int main (long argc,
  142. char* argv[])
  143. {
  144. /* Declare variables
  145. */
  146. /* Handles */
  147. SQLHDBC hdbc;
  148. SQLHENV henv;
  149. SQLHSTMT hstmt;
  150. /* Miscellaneous variables */
  151. SQLCHAR *dsn; /*name of the DSN used for connecting to the database*/
  152. SQLWCHAR *dsnW; /*name of the DSN used for connecting to the database*/
  153. SQLRETURN rc = 0;
  154. SQLINTEGER i, in;
  155. SQLWCHAR connStrInW[BUFFER_LEN];
  156. SQLWCHAR connStrOutW[BUFFER_LEN];
  157. SQLSMALLINT connStrOutLen;
  158. SQLCHAR *verInfoBuffer;
  159. SQLWCHAR verInfoBufferW[BUFFER_LEN];
  160. SQLSMALLINT verInfoLen;
  161. SQLWCHAR majorVerW[3];
  162. SQLINTEGER isUdoEnabled;
  163. SQLWCHAR insertStmtW[BUFFER_LEN];
  164. int lenArgv1;
  165. /* STEP 1. Get data source name from command line (or use default)
  166. ** Allocate the environment handle and set ODBC version
  167. ** Allocate the connection handle
  168. ** Establish the database connection
  169. ** Allocate the statement handle
  170. ** Drop demo database if it already exists
  171. */
  172. /* If(dsnW is not explicitly passed in as arg) */
  173. if (argc != 2)
  174. {
  175. /* Use default dsnW - odbc_demo */
  176. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  177. dsn = (SQLCHAR *) malloc (sizeof(defDsn) + sizeof(char));
  178. strcpy( (char *)dsn, (char *)defDsn);
  179. dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
  180. + sizeof(SQLWCHAR));
  181. wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
  182. }
  183. else
  184. {
  185. /* Use specified dsnW */
  186. lenArgv1 = strlen((char *)argv[1]);
  187. dsn = (SQLCHAR *) malloc (lenArgv1 + sizeof(char));
  188. strcpy((char *)dsn, (char *)argv[1]);
  189. dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
  190. + sizeof(SQLWCHAR));
  191. mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
  192. fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
  193. }
  194. /* Allocate the Environment handle */
  195. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  196. if (rc != SQL_SUCCESS)
  197. {
  198. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  199. return (1);
  200. }
  201. /* Set the ODBC version to 3.0 */
  202. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  203. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  204. return (1);
  205. /* Allocate the connection handle */
  206. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  207. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  208. return (1);
  209. /* Establish the database connection */
  210. #ifndef _WINNT
  211. wsprintf((SQLWCHAR *) connStrInW, "DSN=%s;connectdatabase=NO", dsn);
  212. #else
  213. swprintf((SQLWCHAR *) connStrInW, L"DSN=%s;connectdatabase=NO", dsnW);
  214. #endif
  215. rc = SQLDriverConnectW (hdbc, NULL, connStrInW, SQL_NTS, connStrOutW, BUFFER_LEN, &connStrOutLen, SQL_DRIVER_NOPROMPT);
  216. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLDriverConnect failed\nExiting!!"))
  217. return (1);
  218. /* Allocate the statement handle */
  219. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  220. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  221. return (1);
  222. /* Drop demo database if it already exists */
  223. rc = SQLExecDirectW (hstmt, (SQLWCHAR*) L"DROP DATABASE odbc_demodb", SQL_NTS);
  224. fprintf (stdout, "STEP 1 done...connected to database server\n");
  225. /* STEP 2. Get version information from the database server
  226. ** Set the value of isUdoEnabled depending on the database version returned
  227. */
  228. /* Get version information from the database server */
  229. rc = SQLGetInfoW (hdbc, SQL_DBMS_VER, verInfoBufferW, BUFFER_LEN, &verInfoLen);
  230. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 2 -- SQLGetInfo failed\n"))
  231. goto Exit;
  232. /* Set the value of isUDOEnabled */
  233. wcsncpy ((SQLWCHAR *) majorVerW, (SQLWCHAR *) verInfoBufferW, 2);
  234. if (wcsncmp(majorVerW, L"09", 2) >= 0 )
  235. isUdoEnabled = 1;
  236. else
  237. isUdoEnabled = 0;
  238. verInfoBuffer = (SQLCHAR *) malloc (wcslen(verInfoBufferW) + sizeof(char));
  239. wcstombs( (char *) verInfoBuffer, verInfoBufferW, wcslen(verInfoBufferW)
  240. + sizeof(char));
  241. fprintf (stdout, "STEP 2 done...Database Version -- %s\n", verInfoBuffer);
  242. /* STEP 3. Execute the SQL statements to create the database
  243. ** (depending on the version of the database server)
  244. */
  245. if (isUdoEnabled)
  246. {
  247. fprintf (stdout, "Creating UDO database\n");
  248. for (i = 0; i < NUM_DBCREATE_STMTS; i++)
  249. {
  250. fprintf (stdout, "Executing stmt %d of %d\n", i+1, NUM_DBCREATE_STMTS);
  251. rc = SQLExecDirectW (hstmt, createUdoDbStmtsW[i], SQL_NTS);
  252. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  253. goto Exit;
  254. }
  255. }
  256. else
  257. {
  258. fprintf (stdout, "Creating non-UDO database\n");
  259. for (i = 0; i < NUM_DBCREATE_STMTS -1; i++)
  260. {
  261. fprintf (stdout, "Executing stmt %d\n", i+1, NUM_DBCREATE_STMTS -1);
  262. rc = SQLExecDirectW (hstmt, createNonUdoDbStmtsW[i], SQL_NTS);
  263. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  264. goto Exit;
  265. }
  266. }
  267. fprintf (stdout, "STEP 3 done...Sample Database Created\n");
  268. /* STEP 4. Construct the INSERT statement for table 'customer'
  269. ** Insert data into the table 'customer'. The data inserted depends on
  270. ** whether the database created is UDO or non-UDO enabled
  271. */
  272. fprintf (stdout, "Populating table 'customer'\n");
  273. for (i = 0; i < NUM_CUSTOMERS; i++)
  274. {
  275. fprintf (stdout, "Inserting row # %d\n", i+1);
  276. /* Construct the INSERT statement for table 'customer' */
  277. wcscpy ((SQLWCHAR *) insertStmtW, (SQLWCHAR *) insertDBStmtsW[0]);
  278. if (isUdoEnabled)
  279. wcscat ((SQLWCHAR *) insertStmtW, (SQLWCHAR *) udoCustW[i]);
  280. else
  281. wcscat ((SQLWCHAR *) insertStmtW, (SQLWCHAR *) nonUdoCustW[i]);
  282. /* Execute the INSERT statement */
  283. rc = SQLExecDirectW (hstmt, insertStmtW, SQL_NTS);
  284. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed\n"))
  285. goto Exit;
  286. }
  287. fprintf (stdout, "STEP 4 done...Data inserted into the 'customer' table\n");
  288. /* STEP 5. Construct the INSERT statement for table 'item'
  289. ** Insert data into the table 'item'. The data inserted depends on
  290. ** whether the database created is UDO or non-UDO enabled
  291. */
  292. fprintf (stdout, "Populating table 'item'\n");
  293. for (i = 0; i < NUM_ITEMS; i++)
  294. {
  295. fprintf (stdout, "Inserting row # %d\n", i+1);
  296. /* Construct the INSERT statement for table 'item' */
  297. wcscpy ((SQLWCHAR *) insertStmtW, (SQLWCHAR *) insertDBStmtsW[1]);
  298. if (isUdoEnabled)
  299. wcscat ((SQLWCHAR *) insertStmtW, (SQLWCHAR *) udoItemW[i]);
  300. else
  301. wcscat ((SQLWCHAR *) insertStmtW, (SQLWCHAR *) nonUdoItemW[i]);
  302. /* Execute the INSERT statement */
  303. rc = SQLExecDirectW (hstmt, insertStmtW, SQL_NTS);
  304. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLExecDirect failed\n"))
  305. goto Exit;
  306. }
  307. fprintf (stdout, "STEP 5 done...Data inserted into the 'item' table\n");
  308. /* STEP 6. Construct the INSERT statement for table 'orders'
  309. ** Insert data into the table 'orders'.
  310. */
  311. fprintf (stdout, "Populating table 'orders'\n");
  312. for (i = 0; i < NUM_ORDERS; i++)
  313. {
  314. fprintf (stdout, "Inserting row # %d\n", i+1);
  315. /* Construct the INSERT statement for table 'orders' */
  316. wcscpy ((SQLWCHAR *) insertStmtW, (SQLWCHAR *) insertDBStmtsW[2]);
  317. wcscat ((SQLWCHAR *) insertStmtW, (SQLWCHAR *) ordersW[i]);
  318. /* Execute the INSERT statement */
  319. rc = SQLExecDirectW (hstmt, insertStmtW, SQL_NTS);
  320. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLExecDirect failed\n"))
  321. goto Exit;
  322. }
  323. fprintf (stdout, "STEP 6 done...Data inserted into the 'orders' table\n");
  324. Exit:
  325. /* CLEANUP: Close the statement handle
  326. ** Free the statement handle
  327. ** Disconnect from the datasource
  328. ** Free the connection and environment handles
  329. ** Exit
  330. */
  331. /* Close the statement handle */
  332. SQLFreeStmt (hstmt, SQL_CLOSE);
  333. /* Free the statement handle */
  334. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  335. /* Disconnect from the data source */
  336. SQLDisconnect (hdbc);
  337. /* Free the environment handle and the database connection handle */
  338. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  339. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  340. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  341. in = getchar ();
  342. return (rc);
  343. }