dbcreate.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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: 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 "dbcreate.h"
  97. #define ERRMSG_LEN 200
  98. SQLCHAR defDsn[] = "odbc_demo";
  99. SQLINTEGER checkError (SQLRETURN rc,
  100. SQLSMALLINT handleType,
  101. SQLHANDLE handle,
  102. SQLCHAR* errmsg)
  103. {
  104. SQLRETURN retcode = SQL_SUCCESS;
  105. SQLSMALLINT errNum = 1;
  106. SQLCHAR sqlState[6];
  107. SQLINTEGER nativeError;
  108. SQLCHAR errMsg[ERRMSG_LEN];
  109. SQLSMALLINT textLengthPtr;
  110. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  111. {
  112. while (retcode != SQL_NO_DATA)
  113. {
  114. retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, ERRMSG_LEN, &textLengthPtr);
  115. if (retcode == SQL_INVALID_HANDLE)
  116. {
  117. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  118. return 1;
  119. }
  120. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  121. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  122. errNum++;
  123. }
  124. fprintf (stderr, "%s\n", errmsg);
  125. return 1; /* all errors on this handle have been reported */
  126. }
  127. else
  128. return 0; /* no errors to report */
  129. }
  130. int main (long argc,
  131. char* argv[])
  132. {
  133. /* Declare variables
  134. */
  135. /* Handles */
  136. SQLHDBC hdbc;
  137. SQLHENV henv;
  138. SQLHSTMT hstmt;
  139. /* Miscellaneous variables */
  140. SQLCHAR dsn[20]; /*name of the DSN used for connecting to the database*/
  141. SQLRETURN rc = 0;
  142. SQLINTEGER i, in;
  143. SQLCHAR connStrIn[BUFFER_LEN];
  144. SQLCHAR connStrOut[BUFFER_LEN];
  145. SQLSMALLINT connStrOutLen;
  146. SQLCHAR verInfoBuffer[BUFFER_LEN];
  147. SQLSMALLINT verInfoLen;
  148. SQLCHAR majorVer[3];
  149. SQLINTEGER isUdoEnabled;
  150. SQLCHAR insertStmt[BUFFER_LEN];
  151. /* STEP 1. Get data source name from command line (or use default)
  152. ** Allocate the environment handle and set ODBC version
  153. ** Allocate the connection handle
  154. ** Establish the database connection
  155. ** Allocate the statement handle
  156. ** Drop demo database if it already exists
  157. */
  158. /* If(dsn is not explicitly passed in as arg) */
  159. if (argc != 2)
  160. {
  161. /* Use default dsn - odbc_demo */
  162. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  163. strcpy ((char *)dsn, (char *)defDsn);
  164. }
  165. else
  166. {
  167. /* Use specified dsn */
  168. strcpy ((char *)dsn, (char *)argv[1]);
  169. fprintf (stdout, "\nUsing specified DSN : %s\n", dsn);
  170. }
  171. /* Allocate the Environment handle */
  172. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  173. if (rc != SQL_SUCCESS)
  174. {
  175. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  176. return (1);
  177. }
  178. /* Set the ODBC version to 3.0 */
  179. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  180. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  181. return (1);
  182. /* Allocate the connection handle */
  183. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  184. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  185. return (1);
  186. /* Establish the database connection */
  187. sprintf((char *) connStrIn, "DSN=%s;connectdatabase=NO", dsn);
  188. rc = SQLDriverConnect (hdbc, NULL, connStrIn, SQL_NTS, connStrOut, BUFFER_LEN, &connStrOutLen, SQL_DRIVER_NOPROMPT);
  189. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLDriverConnect failed\nExiting!!"))
  190. return (1);
  191. /* Allocate the statement handle */
  192. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  193. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  194. return (1);
  195. /* Drop demo database if it already exists */
  196. rc = SQLExecDirect (hstmt, (SQLCHAR*) "DROP DATABASE odbc_demodb", SQL_NTS);
  197. fprintf (stdout, "STEP 1 done...connected to database server\n");
  198. /* STEP 2. Get version information from the database server
  199. ** Set the value of isUdoEnabled depending on the database version returned
  200. */
  201. /* Get version information from the database server */
  202. rc = SQLGetInfo (hdbc, SQL_DBMS_VER, verInfoBuffer, BUFFER_LEN, &verInfoLen);
  203. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 2 -- SQLGetInfo failed\n"))
  204. goto Exit;
  205. /* Set the value of isUDOEnabled */
  206. strncpy ((char *) majorVer, (char *) verInfoBuffer, 2);
  207. if (strncmp (majorVer, "09", 2) >= 0 )
  208. isUdoEnabled = 1;
  209. else
  210. isUdoEnabled = 0;
  211. fprintf (stdout, "STEP 2 done...Database Version -- %s\n", verInfoBuffer);
  212. /* STEP 3. Execute the SQL statements to create the database
  213. ** (depending on the version of the database server)
  214. */
  215. if (isUdoEnabled)
  216. {
  217. fprintf (stdout, "Creating UDO database\n");
  218. for (i = 0; i < NUM_DBCREATE_STMTS; i++)
  219. {
  220. fprintf (stdout, "Executing stmt %d of %d\n", i+1, NUM_DBCREATE_STMTS);
  221. rc = SQLExecDirect (hstmt, createUdoDbStmts[i], SQL_NTS);
  222. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  223. goto Exit;
  224. }
  225. }
  226. else
  227. {
  228. fprintf (stdout, "Creating non-UDO database\n");
  229. for (i = 0; i < NUM_DBCREATE_STMTS -1; i++)
  230. {
  231. fprintf (stdout, "Executing stmt %d\n", i+1, NUM_DBCREATE_STMTS -1);
  232. rc = SQLExecDirect (hstmt, createNonUdoDbStmts[i], SQL_NTS);
  233. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  234. goto Exit;
  235. }
  236. }
  237. fprintf (stdout, "STEP 3 done...Sample Database Created\n");
  238. /* STEP 4. Construct the INSERT statement for table 'customer'
  239. ** Insert data into the table 'customer'. The data inserted depends on
  240. ** whether the database created is UDO or non-UDO enabled
  241. */
  242. fprintf (stdout, "Populating table 'customer'\n");
  243. for (i = 0; i < NUM_CUSTOMERS; i++)
  244. {
  245. fprintf (stdout, "Inserting row # %d\n", i+1);
  246. /* Construct the INSERT statement for table 'customer' */
  247. strcpy ((char *) insertStmt, (char *) insertDBStmts[0]);
  248. if (isUdoEnabled)
  249. strcat ((char *) insertStmt, (char *) udoCust[i]);
  250. else
  251. strcat ((char *) insertStmt, (char *) nonUdoCust[i]);
  252. /* Execute the INSERT statement */
  253. rc = SQLExecDirect (hstmt, insertStmt, SQL_NTS);
  254. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed\n"))
  255. goto Exit;
  256. }
  257. fprintf (stdout, "STEP 4 done...Data inserted into the 'customer' table\n");
  258. /* STEP 5. Construct the INSERT statement for table 'item'
  259. ** Insert data into the table 'item'. The data inserted depends on
  260. ** whether the database created is UDO or non-UDO enabled
  261. */
  262. fprintf (stdout, "Populating table 'item'\n");
  263. for (i = 0; i < NUM_ITEMS; i++)
  264. {
  265. fprintf (stdout, "Inserting row # %d\n", i+1);
  266. /* Construct the INSERT statement for table 'item' */
  267. strcpy ((char *) insertStmt, (char *) insertDBStmts[1]);
  268. if (isUdoEnabled)
  269. strcat ((char *) insertStmt, (char *) udoItem[i]);
  270. else
  271. strcat ((char *) insertStmt, (char *) nonUdoItem[i]);
  272. /* Execute the INSERT statement */
  273. rc = SQLExecDirect (hstmt, insertStmt, SQL_NTS);
  274. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 5 -- SQLExecDirect failed\n"))
  275. goto Exit;
  276. }
  277. fprintf (stdout, "STEP 5 done...Data inserted into the 'item' table\n");
  278. /* STEP 6. Construct the INSERT statement for table 'orders'
  279. ** Insert data into the table 'orders'.
  280. */
  281. fprintf (stdout, "Populating table 'orders'\n");
  282. for (i = 0; i < NUM_ORDERS; i++)
  283. {
  284. fprintf (stdout, "Inserting row # %d\n", i+1);
  285. /* Construct the INSERT statement for table 'orders' */
  286. strcpy ((char *) insertStmt, (char *) insertDBStmts[2]);
  287. strcat ((char *) insertStmt, (char *) orders[i]);
  288. /* Execute the INSERT statement */
  289. rc = SQLExecDirect (hstmt, insertStmt, SQL_NTS);
  290. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 6 -- SQLExecDirect failed\n"))
  291. goto Exit;
  292. }
  293. fprintf (stdout, "STEP 6 done...Data inserted into the 'orders' table\n");
  294. Exit:
  295. /* CLEANUP: Close the statement handle
  296. ** Free the statement handle
  297. ** Disconnect from the datasource
  298. ** Free the connection and environment handles
  299. ** Exit
  300. */
  301. /* Close the statement handle */
  302. SQLFreeStmt (hstmt, SQL_CLOSE);
  303. /* Free the statement handle */
  304. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  305. /* Disconnect from the data source */
  306. SQLDisconnect (hdbc);
  307. /* Free the environment handle and the database connection handle */
  308. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  309. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  310. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  311. in = getchar ();
  312. return (rc);
  313. }