home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cppsql.zip / SQLSRC.ZIP / SQL.CPP < prev    next >
Text File  |  1992-09-18  |  15KB  |  596 lines

  1. // :H1 SQL.CPP: START OF SPECIFICATIONS
  2. //----------------------------------------------------------------------
  3. //
  4. //  Module Name: SQL.CPP
  5. //
  6. //  Description: SQL Interface.
  7. //
  8. //  Product Classification:
  9. //    IBM Internal Use Only
  10. //    (C) Copyright IBM Corp. 1992
  11. //
  12. //  Status: New
  13. //
  14. //  Initial Author: George L. Havens
  15. //
  16. //  Function: To provide an interface to SQL databases.
  17. //
  18. //  Notes: None.
  19. //
  20. //  Dependencies: Requires PORTABLE.H include.
  21. //                This module requires SQL.C for the SQL C interface.
  22. //
  23. //  Restrictions: None.
  24. //
  25. //  Compiler: Zortech C++
  26. //
  27. // Change Activity -----------------------------------------------------
  28. //
  29. // $MOD(module) COMP(component) PROD(product): Description...
  30. //
  31. // FLAG  REASON  VERS  DATE  WHO   DESCRIPTION
  32. // ---- -------- ---- ------ ---   -----------
  33. //               V100 920224 GLH : Initial level
  34. // $P1  P0001979 V100 920608 GLH : Fix problems caused when start database 
  35. //                                 started from application
  36. //
  37. // END-OF-SPECIFICATIONS -----------------------------------------------
  38.  
  39. // Use the following define to create a unit test version.
  40. //#define TEST
  41.  
  42. // :H1 SYSTEM INCLUDE FILES.  SELECTIVITY USED WHERE POSSIBLE.
  43. #include "stdio.h"
  44. #include "stdlib.h"
  45. #include "stdarg.h"
  46. #include "string.h"
  47. #include "ctype.h"
  48.  
  49. // :H1 USER-DEFINED INCLUDE FILES.  SELECTIVITY USED WHERE POSSIBLE.
  50. #include "portable.h"
  51. #include "sql.hpp"
  52. extern "C"
  53.   {
  54. #include "sqlc.h"
  55.   }
  56.  
  57. // :H1 SQL: SQL Constructor
  58. //----------------------------------------------------------------------
  59. //
  60. //  Function Name: SQL constructor
  61. //
  62. //  Purpose: This method is the SQL object constructor.
  63. //
  64. //  Description: This method will save the name of the database.
  65. //
  66. //  Input:
  67. //    name       Database name to be used
  68. //    access     access is SHARED or EXCLUSIVE, default is SHARED
  69. //    drive      drive to create database on
  70. //
  71. //  Output:
  72. //    none
  73. //
  74. //----------------------------------------------------------------------
  75.  
  76. SQL::SQL (const CHAR *name, CHAR access, CHAR drive)
  77.   {
  78.     // save database name
  79.     database = strdup (name);
  80.  
  81.     // save database access
  82.     use = access;
  83.  
  84.     // save database drive
  85.     build_drive = drive;
  86.  
  87.                                                                        //6@P1D
  88.     // initialize id
  89.     id = 0;
  90.   }
  91.  
  92. // :H1 Create: Create SQL Database
  93. //----------------------------------------------------------------------
  94. //
  95. //  Function Name: Create
  96. //
  97. //  Purpose: This method creates an SQL database.
  98. //
  99. //  Description: This method creates an SQL database. The database must still
  100. //               be opened to be used.
  101. //
  102. //  Input:
  103. //    comment    comment to be placed into database
  104. //    bind_path  path for SQL bind file       
  105. //
  106. //  Output:
  107. //    return code 0 = successful, not 0 = error
  108. //
  109. //----------------------------------------------------------------------
  110.  
  111. SHORT SQL::Create (const CHAR *comment, const CHAR *bind_path)
  112.   {
  113.    USHORT len;
  114.    CHAR   *path;
  115.  
  116.     // get bind path length 
  117.     len = strlen (bind_path);
  118.  
  119.     // if get memory for bind path error
  120.     if ( (path = new CHAR [strlen (bind_path) + 15]) == 0) 
  121.       {
  122.         // set error location
  123.         id = SQL_ID_10;
  124.  
  125.         // set error return code
  126.         rc = SQL_MEMORY_ERROR;
  127.  
  128.         return (rc);
  129.       }
  130.  
  131.     // copy bind path
  132.     strcpy  (path, bind_path);
  133.  
  134.     // if path specified
  135.     if (len > 0)
  136.         // if end of path does not have a \    
  137.         if (path [len-1] != '\\')  
  138.             strcat (path, "\\");
  139.  
  140.     // add bind file name
  141.     strcat (path, BIND_FILE);
  142.  
  143.     // if error creating database
  144.     if ( (rc = sql_create (database, (CHAR *)comment, path,
  145.                build_drive)) != 0)
  146.       {
  147.         // set error location
  148.         id = SQL_ID_2;
  149.         return (rc);
  150.       }
  151.  
  152.     return (0);
  153.   }
  154.  
  155. // :H1 Open: Open SQL Database
  156. //----------------------------------------------------------------------
  157. //
  158. //  Function Name: Open
  159. //
  160. //  Purpose: This method opens an SQL database.
  161. //
  162. //  Description: This method attempts to open an SQL database.
  163. //
  164. //  Input:
  165. //    none      
  166. //
  167. //  Output:
  168. //    return code 0 = successful, not 0 = error
  169. //
  170. //----------------------------------------------------------------------
  171.  
  172. SHORT SQL::Open (VOID)
  173.   {
  174.     // if error opening database
  175.     if ( (rc = sql_open (database, use)) != 0)
  176.       {
  177.         // set error location
  178.         id = SQL_ID_4;
  179.         return (rc);
  180.       }
  181.  
  182.     return (0);
  183.   }
  184.  
  185. // :H1 SQL: Close SQL Database
  186. //----------------------------------------------------------------------
  187. //
  188. //  Function Name: Close
  189. //
  190. //  Purpose: This method will close an SQL database.
  191. //
  192. //  Description: This method will close an SQL database.
  193. //
  194. //  Input:
  195. //    none
  196. //
  197. //  Output:
  198. //    return code 0 = successful, not 0 = error
  199. //
  200. //----------------------------------------------------------------------
  201.  
  202. SHORT SQL::Close (VOID)
  203.   {
  204.     // if close database error
  205.     if ( (rc = sql_close ()) != 0 )
  206.       {
  207.         // set error location
  208.         id = SQL_ID_5;
  209.         return (rc);
  210.       }
  211.  
  212.     return (0);
  213.   }
  214.  
  215. // :H1 Delete: Delete SQL Database
  216. //----------------------------------------------------------------------
  217. //
  218. //  Function Name: Delete
  219. //
  220. //  Purpose: This method will delete an SQL database.
  221. //
  222. //  Description: This method will delete an SQL database.
  223. //
  224. //  Input:
  225. //    none
  226. //
  227. //  Output:
  228. //    return code 0 = successful, not 0 = error
  229. //
  230. //----------------------------------------------------------------------
  231.  
  232. SHORT SQL::Delete (VOID)
  233.   {
  234.     // if error deleting database
  235.     if ( (rc = sql_delete (database)) != 0)
  236.       {
  237.         // set error location
  238.         id = SQL_ID_7;
  239.         return (rc);
  240.       }
  241.  
  242.     return (0);
  243.   }
  244.  
  245. // :H1 Get_buffer_size : Get the database buffer size
  246. //----------------------------------------------------------------------
  247. //
  248. //  Function Name: Get_buffer_size
  249. //
  250. //  Purpose: This method will get the buffer size for an SQL database.
  251. //
  252. //  Description: This method will get the size for the specified SQL database.
  253. //
  254. //  Input:
  255. //    size       size of buffer  
  256. //
  257. //  Output:
  258. //    return code 0 = successful, not 0 = error
  259. //
  260. //----------------------------------------------------------------------
  261. SHORT SQL::Get_buffer_size (USHORT& size) 
  262.   {
  263.     // if get pool size error 
  264.     if ( (rc = sql_get_pool_size (database, &size)) != 0 )
  265.       {
  266.         // set error location
  267.         id = SQL_ID_11;
  268.         return (rc);
  269.       }
  270.  
  271.     return (0);
  272.   }
  273.  
  274. // :H1 Set_buffer_size : Set the database buffer size
  275. //----------------------------------------------------------------------
  276. //
  277. //  Function Name: Set_buffer_size
  278. //
  279. //  Purpose: This method will set the buffer size for an SQL database.
  280. //
  281. //  Description: This method will set the size for the specified SQL database.
  282. //
  283. //  Input:
  284. //    size       size of buffer  
  285. //
  286. //  Output:
  287. //    return code 0 = successful, not 0 = error
  288. //
  289. //----------------------------------------------------------------------
  290. SHORT SQL::Set_buffer_size (USHORT size) 
  291.   {
  292.     // if set pool size error 
  293.     if ( (rc = sql_set_pool_size (database, size)) != 0 )
  294.       {
  295.         // set error location
  296.         id = SQL_ID_12;
  297.         return (rc);
  298.       }
  299.  
  300.     return (0);
  301.   }
  302.  
  303. // :H1 Get_log_size : Get the database log file size
  304. //----------------------------------------------------------------------
  305. //
  306. //  Function Name: Get_log_size
  307. //
  308. //  Purpose: This method will get the log file size for an SQL database.
  309. //
  310. //  Description: This method will get the size for the specified SQL database.
  311. //
  312. //  Input:
  313. //    size       size of log file
  314. //
  315. //  Output:
  316. //    return code 0 = successful, not 0 = error
  317. //
  318. //----------------------------------------------------------------------
  319. SHORT SQL::Get_log_size (USHORT& size) 
  320.   {
  321.     // if get log file size error 
  322.     if ( (rc = sql_get_log_size (database, &size)) != 0 )
  323.       {
  324.         // set error location
  325.         id = SQL_ID_13;
  326.         return (rc);
  327.       }
  328.  
  329.     return (0);
  330.   }
  331.  
  332. // :H1 Set_log_size : Set the database log file size
  333. //----------------------------------------------------------------------
  334. //
  335. //  Function Name: Set_log_size
  336. //
  337. //  Purpose: This method will set the log file size for an SQL database.
  338. //
  339. //  Description: This method will set the size for the specified SQL database.
  340. //
  341. //  Input:
  342. //    size       size of buffer  
  343. //
  344. //  Output:
  345. //    return code 0 = successful, not 0 = error
  346. //
  347. //----------------------------------------------------------------------
  348. SHORT SQL::Set_log_size (USHORT size) 
  349.   {
  350.     // if set log file size error 
  351.     if ( (rc = sql_set_log_size (database, size)) != 0 )
  352.       {
  353.         // set error location
  354.         id = SQL_ID_14;
  355.         return (rc);
  356.       }
  357.  
  358.     return (0);
  359.   }
  360.  
  361. // :H1 Get_log_number : Get the number of database log files
  362. //----------------------------------------------------------------------
  363. //
  364. //  Function Name: Get_log_number
  365. //
  366. //  Purpose: This method will get the number log files for an SQL database.
  367. //
  368. //  Description: This method will get the number of primary and seconday 
  369. //               log files.
  370. //
  371. //  Input:
  372. //    num_pri    number of primary log files
  373. //    num_sec    number of secondary log files
  374. //
  375. //  Output:
  376. //    return code 0 = successful, not 0 = error
  377. //
  378. //----------------------------------------------------------------------
  379. SHORT SQL::Get_log_number (USHORT& num_pri, USHORT& num_sec)
  380.   {
  381.     // if get number of log file error 
  382.     if ( (rc = sql_get_log_number (database, &num_pri, &num_sec)) != 0 )
  383.       {
  384.         // set error location
  385.         id = SQL_ID_16;
  386.         return (rc);
  387.       }
  388.  
  389.     return (0);
  390.   }
  391.  
  392. // :H1 Set_log_number: Set the number of database log files
  393. //----------------------------------------------------------------------
  394. //
  395. //  Function Name: Set_log_number
  396. //
  397. //  Purpose: This method will set the number log files for an SQL database.
  398. //
  399. //  Description: This method will set the number of primary and seconday 
  400. //               log files.
  401. //
  402. //  Input:
  403. //    num_pri    number of primary log files
  404. //    num_sec    number of secondary log files
  405. //
  406. //  Output:
  407. //    return code 0 = successful, not 0 = error
  408. //
  409. //----------------------------------------------------------------------
  410. SHORT SQL::Set_log_number (USHORT num_pri, USHORT num_sec)
  411.   {
  412.     // if get number of log file error 
  413.     if ( (rc = sql_set_log_number (database, num_pri, num_sec)) != 0 )
  414.       {
  415.         // set error location
  416.         id = SQL_ID_17;
  417.         return (rc);
  418.       }
  419.  
  420.     return (0);
  421.   }
  422.  
  423. // :H1 Commit: Commit Changes To Database
  424. //----------------------------------------------------------------------
  425. //
  426. //  Function Name: Commit         
  427. //
  428. //  Purpose: This method will commit the changes to the database.
  429. //
  430. //  Description: This method will commit all the changes that have been      
  431. //               made to the database.
  432. //
  433. //  Input:
  434. //    none
  435. //
  436. //  Output:
  437. //    return code 0 = successful, not 0 = error
  438. //
  439. //----------------------------------------------------------------------
  440. SHORT SQL::Commit (VOID)
  441.   {
  442.  
  443.     // if commit error
  444.     if ( (rc =  sql_commit ()) != 0)
  445.         // set error location
  446.         id = SQL_ID_3;
  447.  
  448.     return (rc);
  449.   }
  450.  
  451. // :H1 Rollback: Rollback Changes From Database
  452. //----------------------------------------------------------------------
  453. //
  454. //  Function Name: Rollback
  455. //
  456. //  Purpose: This method will rollback changes to the database.
  457. //
  458. //  Description: This method will rollback all the changes that have been      
  459. //               made to the database.
  460. //
  461. //  Input:
  462. //    none
  463. //
  464. //  Output:
  465. //    return code 0 = successful, not 0 = error
  466. //
  467. //----------------------------------------------------------------------
  468. SHORT SQL::Rollback (VOID)
  469.   {
  470.  
  471.     // if rollback error
  472.     if ( (rc =  sql_rollback ()) != 0)
  473.         // set error location
  474.         id = SQL_ID_18;
  475.  
  476.     return (rc);
  477.   }
  478.  
  479. // :H1 SQL: SQL Destructor
  480. //----------------------------------------------------------------------
  481. //
  482. //  Function Name: SQL destructor
  483. //
  484. //  Purpose: This method is the SQL object destructor.
  485. //
  486. //  Description: This method will free the database name, close
  487. //               the database, and stop the database manager.
  488. //
  489. //  Input:
  490. //    none
  491. //
  492. //  Output:
  493. //    none
  494. //
  495. //----------------------------------------------------------------------
  496.  
  497. SQL::~SQL ()
  498.   {
  499.     // delete database name
  500.     delete database;
  501.  
  502.     // if close database error
  503.     if ( (rc = sql_close ()) != 0 )
  504.       {
  505.         // set error location
  506.         id = SQL_ID_8;
  507.         return;
  508.       }
  509.  
  510.     // if stop database error
  511.     if ( (rc = sql_stop ()) != 0 )
  512.       {
  513.         // set error location
  514.         id = SQL_ID_9;
  515.         return;
  516.       }
  517.   }
  518.  
  519. #ifdef TEST
  520.  
  521. SHORT _astart;   // for SD386 to find main
  522.  
  523. // This code is used to test the sql object.
  524. SHORT main ()
  525.   {
  526.    SHORT rc;
  527.    SHORT id;
  528.    SQL database ("test");       // create sql object
  529.    USHORT size;
  530.  
  531.     printf ("Creating database...\n");
  532.  
  533.     // if error creating database
  534.     if ( (rc = database.Create ("This is a test database")) != 0 )
  535.       {
  536.         printf ("Error creating database, rc = %d\n", rc);
  537.         exit (-1);
  538.       }
  539.     printf ("Database created...\n");
  540.  
  541.     // if error getting database buffer size
  542.     if ( (rc = database.Get_buffer_size (size)) != 0 )
  543.       {
  544.         printf ("Error getting database buffer size, rc = %d\n", rc);
  545.         exit (-1);
  546.       }
  547.  
  548.  
  549.     // if not error opening database
  550.     if ( (rc = database.Open ()) == 0 )
  551.       {
  552.         printf ("No error reopening database, rc = %d\n", rc);
  553.         database.Close ();
  554.         exit (-1);
  555.       }
  556.  
  557.     // get open database error
  558.     database.Error (rc, id);
  559.  
  560.     printf ("open database rc = %d, id = %d\n", rc, id);
  561.  
  562.     printf ("Database closing...\n");
  563.  
  564.     // if error closing database
  565.     if ( (rc = database.Close ()) != 0 )
  566.       {
  567.         printf ("Error closing database, rc = %d\n", rc);
  568.         exit (-1);
  569.       }
  570.  
  571.     printf ("Database closed...\n");
  572.  
  573.     printf ("Database opening...\n");
  574.  
  575.     // if error opening database
  576.     if ( (rc = database.Open ()) != 0 )
  577.       {
  578.         printf ("Error opening database, rc = %d\n", rc);
  579.         exit (-1);
  580.       }
  581.  
  582.     printf ("Database opened...\n");
  583.  
  584.     printf ("Press any key to complete program and delete data base\n");
  585.     getchar ();
  586.  
  587.     // if error deleting database
  588.     if ( (rc = database.Delete ()) != 0 )
  589.       {
  590.         printf ("Error deleting database, rc = %d\n", rc);
  591.         exit (-1);
  592.       }
  593.     printf ("Unit test completed successfully\n");
  594.   }
  595. #endif
  596.