1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
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. *
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. *
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
23 #include<AbsSqlConnection.h>
24 #include<SqlFactory.h>
27 struct CachedStmtNode
{
28 SqlStatement
*sqlStmt
;
32 CachedStmtNode() { sqlStmt
= NULL
; sqlString
=NULL
; stmtLength
=0; hits
=0;}
35 class SqlConnection
: public AbsSqlConnection
41 SqlConnection(){ innerConn
= NULL
; isConnOpen
= false; }
44 /** opens connection to the sql engine
45 * @param user username for authentication
46 * @param pass password for authentication
49 DbRetVal
connect (char *user
, char * pass
) {
50 DbRetVal ret
= conn
.open(user
, pass
);
51 if (ret
== OK
) isConnOpen
= true;
55 /** closes connection to the database and releases all the resources
58 DbRetVal
disconnect () {
59 DbRetVal ret
= conn
.close();
60 if (ret
== OK
) isConnOpen
= false;
64 /** Commits active transaction.
65 * It makes all the changes made in the current transaction permanent and <br/>
66 * it also releases the locks held by the current transaction.<br/>
67 * After a transaction commits, application is required to start another <br/>
68 * transaction for further database operations.
71 DbRetVal
commit() { return conn
.commit(); }
73 /** Aborts the active transaction.
74 * undo all the changes made in the current transaction and it also <br/>
75 * releases the locks held by the current transaction.<br/>
76 * After a transaction rollback, application is required to start another <br/>
77 * transaction for further database operations.
80 DbRetVal
rollback() { return conn
.rollback(); }
82 /** Starts a transaction.
83 * The previous transaction should be either committed or rollback <br/>
84 * before beginTrans is called. <br/>
85 * Applications are required to start transaction before they attempt any <br>
87 * @param isoLevel isolation level. Default is read committed.
90 DbRetVal
beginTrans (IsolationLevel isoLevel
= READ_COMMITTED
,
91 TransSyncMode mode
= OSYNC
)
92 { return conn
.startTransaction(isoLevel
); }
94 Connection
& getConnObject(){ return conn
; }
95 bool isConnectionOpen() { if (isConnOpen
) return true; return false; };
96 DbRetVal
getExclusiveLock(){ return conn
.getExclusiveLock(); }
98 SqlStatement
* findInCache(char *stmtStr
);
99 void flushCacheStmt();
100 void addToCache(SqlStatement
*stmt
, char *stmtStr
);
101 void removeLeastUsed();
103 friend class SqlFactory
;