core reorg
[csql.git] / include / SqlConnection.h
blob7aebb3cc25ddc302978320029c8c6dfaa96964a6
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifndef SQLCONNECTION_H
21 #define SQLCONNECTION_H
22 #include<CSql.h>
23 #include<AbsSqlConnection.h>
24 #include<SqlFactory.h>
25 class SqlStatement;
27 struct CachedStmtNode{
28 SqlStatement *sqlStmt;
29 int stmtLength;
30 char *sqlString;
31 int hits;
32 bool inUse;
33 CachedStmtNode() { sqlStmt = NULL; sqlString=NULL; stmtLength=0; hits=0; inUse=0;}
34 void display() {
35 printf(" SQL=%s hits=%d inuse=%d\n", sqlString, hits, inUse);
39 class DllExport SqlConnection : public AbsSqlConnection
41 Connection conn;
42 bool isConnOpen;
43 public:
44 static List connList;
45 static bool isInit;
46 #if (defined MMDB && EMBED)
47 static bool firstThread;
48 static GlobalUniqueID UID;
49 #endif
50 void initialize();
51 List cachedStmts;
52 SqlConnection(){ innerConn = NULL; isConnOpen = false; }
53 ~SqlConnection();
55 /** opens connection to the sql engine
56 * @param user username for authentication
57 * @param pass password for authentication
58 * @return DbRetVal
60 DbRetVal connect (char *user, char * pass);
62 /** closes connection to the database and releases all the resources
63 * @return DbRetVal
65 DbRetVal disconnect () {
66 flushCacheStmt();
67 DbRetVal ret = conn.close();
68 if (ret == OK) isConnOpen = false;
69 # if (defined MMDB && defined EMBED)
70 if (Conf::config.useDurability() && connList.size()==1) UID.destroy();
71 # endif
72 connList.remove(this);
73 return ret;
76 /** Commits active transaction.
77 * It makes all the changes made in the current transaction permanent and <br/>
78 * it also releases the locks held by the current transaction.<br/>
79 * After a transaction commits, application is required to start another <br/>
80 * transaction for further database operations.
81 * @return DbRetVal
83 DbRetVal commit() { return conn.commit(); }
85 /** Aborts the active transaction.
86 * undo all the changes made in the current transaction and it also <br/>
87 * releases the locks held by the current transaction.<br/>
88 * After a transaction rollback, application is required to start another <br/>
89 * transaction for further database operations.
90 * @return DbRetVal
92 DbRetVal rollback() { return conn.rollback(); }
94 /** Starts a transaction.
95 * The previous transaction should be either committed or rollback <br/>
96 * before beginTrans is called. <br/>
97 * Applications are required to start transaction before they attempt any <br>
98 * database operation.
99 * @param isoLevel isolation level. Default is read committed.
100 * @return DbRetVal
102 DbRetVal beginTrans (IsolationLevel isoLevel = READ_COMMITTED,
103 TransSyncMode mode = OSYNC)
104 { return conn.startTransaction(isoLevel); }
106 Connection& getConnObject(){ return conn; }
107 bool isConnectionOpen() { if (isConnOpen) return true; return false; };
108 DbRetVal getExclusiveLock(){ return conn.getExclusiveLock(); }
110 SqlStatement* findInCache(char *stmtStr);
111 void flushCacheStmt();
112 void addToCache(SqlStatement *stmt, char *stmtStr);
113 void removeLeastUsed();
114 void setStmtNotInUse(char *stmtStr);
115 void displayStmtCache();
116 void display() { displayStmtCache(); }
119 friend class SqlFactory;
122 #endif