1 |
#ifndef _DBManagement_hpp_included_
|
2 |
#define _DBManagement_hpp_included_
|
3 |
|
4 |
/* DBManagement.h
|
5 |
|
6 |
*/
|
7 |
#include <stdlib.h>
|
8 |
#include <stdio.h>
|
9 |
#include <sql.h>
|
10 |
#include <sqlext.h>
|
11 |
#include <sqltypes.h>
|
12 |
#include <string>
|
13 |
#include "ResultSet.hpp"
|
14 |
|
15 |
#define MAX_ERR_MSG_LEN 100
|
16 |
#define MAX_USERNAME_LEN 25
|
17 |
#define MAX_PASSWORD_LEN 25
|
18 |
#define MAX_COL_NAME_LEN 100
|
19 |
|
20 |
/**
|
21 |
* A DBManagement Class.
|
22 |
* This class handles all the lower level Database managemenet operations. It well encapusales the Database operations and provides a simpler and generic light weight API on the top.
|
23 |
*/
|
24 |
|
25 |
class DBManagement {
|
26 |
private:
|
27 |
SQLHENV envHandle;// Handle ODBC environment
|
28 |
SQLHSTMT stmtHandle;// Handle SQL Statement
|
29 |
SQLHDBC connHandle;// Handle connection
|
30 |
std::string username;// Username to connect to DB
|
31 |
std::string password;//Password used to connect to DB associated with username
|
32 |
std::string dataSourceName;// Data Source Name
|
33 |
std::string errMessage;
|
34 |
bool isSuccess();
|
35 |
SQLINTEGER getNoOfRows();
|
36 |
SQLSMALLINT getNoOfCols();
|
37 |
void disposeResultSet();
|
38 |
void freeEnvHandle();
|
39 |
void freeEnvConHandle();
|
40 |
void freeStmtHandle();
|
41 |
void freeEnvConStmtHandle();
|
42 |
void allocateEnvHandle();
|
43 |
void allocateConHandle();
|
44 |
void allocateStmtHandle();
|
45 |
void doDiagnostics();
|
46 |
void runGenericQuery(std::string sql);
|
47 |
|
48 |
public:
|
49 |
SQLINTEGER returnCode;/*!< This stores an interger value of the result of any lower level operations or method call to the unixODBC api. */
|
50 |
|
51 |
|
52 |
/**
|
53 |
* A constructor that stores the parameters passed, so they can be used later on by other methods.
|
54 |
* @param dataSourceName a string representing the value of the Data Source Name used to connect to the actual Databse amd load the driver.
|
55 |
* @param username a string representing the name of the user, required to access the database.
|
56 |
* @param password a string representing the password of the user, required to access the database.
|
57 |
*
|
58 |
*/
|
59 |
DBManagement(std::string dataSourceName, std::string username, std::string password);
|
60 |
|
61 |
/*A default destructor */
|
62 |
~DBManagement();
|
63 |
|
64 |
/**
|
65 |
* This method allocates internal data structures (handles) memory space and then uses them to actually make a socket connection to the database by using the parameters supplied in the constructor. Note that if this method raises an exception, it doesnot free up the internal memory alloacted. The user of this method has to call the close method which will free up the memory even iof the mothod succeeds or not.
|
66 |
@return If it return 0 that means the method succeded in establishing the socket connection to the Database.
|
67 |
*/
|
68 |
int open();
|
69 |
|
70 |
|
71 |
/**
|
72 |
* This method frees up all the memory allocated to the internal data structures and then closes the socket connection to the Database.
|
73 |
*/
|
74 |
void close();
|
75 |
|
76 |
|
77 |
/**
|
78 |
* This method initializes the internal data structures so that all the Database operation will be in the transection mode. This means the database will not get updated unless the user explictly calls commit to finalize all the Database operations.
|
79 |
* @return If it return 0 that means the method succeded in initializing the internal data structures as per the requirements.
|
80 |
*/
|
81 |
int beginTransection();
|
82 |
|
83 |
|
84 |
/**
|
85 |
* This method initializes the internal data structures so that all the Database operation will be in the non-transection mode. This means the database will not get updated right away once the queries re executed. It will not wait for user to explictly calls commit to finalize all the Database operations.
|
86 |
* @return If it return 0 that means the method succeded in initializing the internal data structures as per the requirements.
|
87 |
*/
|
88 |
int endTransection();
|
89 |
|
90 |
/**
|
91 |
* This method is used when the user has started working in transection mode by calling beginTransection. It commits all the changes that the user might have done to the database. Unless users explictely calls this method the database will not get updated if operating under transection mode.
|
92 |
* @return If it return 0 that means the method succeded in commiting all the changes that the user might have done to the database.
|
93 |
*/
|
94 |
int commit();
|
95 |
|
96 |
|
97 |
/**
|
98 |
* This method is used when the user has started working in transection mode by calling beginTransection. It will undo all the changes that the user might have done to the database.
|
99 |
* @return If it return 0 that means the method succeded in undoing all the changes that the user might have done to the database.
|
100 |
*/
|
101 |
int rollback();
|
102 |
|
103 |
|
104 |
/**
|
105 |
* This method is able to execute all the insert, update and delete sql queries. This method can be called in the transection or regular mode. In former case, the user have to call commit or rollback methods to commit or undo changes to the database. In later case it will directly update the database right away.
|
106 |
* @param sql a string representing the actual sql query to be executed.
|
107 |
* @return If it return 0 that means the method succeded in executing the query in the database.
|
108 |
*/
|
109 |
int executeQuery(std::string sql);
|
110 |
|
111 |
|
112 |
/**
|
113 |
* This method is able to execute all the select sql queries.
|
114 |
* @param sql a string representing the actual sql query to be executed.
|
115 |
* @return ResultSet* An object containning the data retrived from the database.
|
116 |
*/
|
117 |
ResultSet* executeQueryWithResults(std::string sql);
|
118 |
|
119 |
};
|
120 |
#endif
|
121 |
|