flush cache statements during create index and drop table/index
[csql.git] / include / SqlConnection.h
blob5e11189af02f757fda5bad380fbb9f0ac198ee15
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 CachedStmtNode() { sqlStmt = NULL; sqlString=NULL; stmtLength=0; hits=0;}
35 class SqlConnection : public AbsSqlConnection
37 Connection conn;
38 bool isConnOpen;
39 public:
40 static List connList;
41 static bool isInit;
42 void initialize();
43 List cachedStmts;
44 SqlConnection(){ innerConn = NULL; isConnOpen = false; }
45 ~SqlConnection();
47 /** opens connection to the sql engine
48 * @param user username for authentication
49 * @param pass password for authentication
50 * @return DbRetVal
52 DbRetVal connect (char *user, char * pass) {
53 DbRetVal ret = conn.open(user, pass);
54 if (ret == OK) isConnOpen = true;
55 if (!isInit) initialize();
56 connList.append(this);
57 return ret;
60 /** closes connection to the database and releases all the resources
61 * @return DbRetVal
63 DbRetVal disconnect () {
64 DbRetVal ret = conn.close();
65 if (ret == OK) isConnOpen = false;
66 connList.remove(this);
67 return ret;
70 /** Commits active transaction.
71 * It makes all the changes made in the current transaction permanent and <br/>
72 * it also releases the locks held by the current transaction.<br/>
73 * After a transaction commits, application is required to start another <br/>
74 * transaction for further database operations.
75 * @return DbRetVal
77 DbRetVal commit() { return conn.commit(); }
79 /** Aborts the active transaction.
80 * undo all the changes made in the current transaction and it also <br/>
81 * releases the locks held by the current transaction.<br/>
82 * After a transaction rollback, application is required to start another <br/>
83 * transaction for further database operations.
84 * @return DbRetVal
86 DbRetVal rollback() { return conn.rollback(); }
88 /** Starts a transaction.
89 * The previous transaction should be either committed or rollback <br/>
90 * before beginTrans is called. <br/>
91 * Applications are required to start transaction before they attempt any <br>
92 * database operation.
93 * @param isoLevel isolation level. Default is read committed.
94 * @return DbRetVal
96 DbRetVal beginTrans (IsolationLevel isoLevel = READ_COMMITTED,
97 TransSyncMode mode = OSYNC)
98 { return conn.startTransaction(isoLevel); }
100 Connection& getConnObject(){ return conn; }
101 bool isConnectionOpen() { if (isConnOpen) return true; return false; };
102 DbRetVal getExclusiveLock(){ return conn.getExclusiveLock(); }
104 SqlStatement* findInCache(char *stmtStr);
105 void flushCacheStmt();
106 void addToCache(SqlStatement *stmt, char *stmtStr);
107 void removeLeastUsed();
109 friend class SqlFactory;
112 #endif