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
;
33 CachedStmtNode() { sqlStmt
= NULL
; sqlString
=NULL
; stmtLength
=0; hits
=0; inUse
=0;}
35 printf(" SQL=%s hits=%d inuse=%d\n", sqlString
, hits
, inUse
);
39 class SqlConnection
: public AbsSqlConnection
43 #if (defined MMDB && defined EMBED)
44 DbRetVal
recoverCsqlDB();
45 DbRetVal
recoverSystemAndUserDB();
46 DbRetVal
applySchemaFile(FILE *fp
);
47 char getQueryFromSchemaFile(FILE *fp
, char *buf
);
48 int applyRedoLogs(char *redoFile
);
53 #if (defined MMDB && EMBED)
54 static bool firstThread
;
55 static GlobalUniqueID UID
;
60 SqlConnection(){ innerConn
= NULL
; isConnOpen
= false; }
63 /** opens connection to the sql engine
64 * @param user username for authentication
65 * @param pass password for authentication
68 DbRetVal
connect (char *user
, char * pass
);
70 /** closes connection to the database and releases all the resources
73 DbRetVal
disconnect () {
75 DbRetVal ret
= conn
.close();
76 if (ret
== OK
) isConnOpen
= false;
77 # if (defined MMDB && defined EMBED)
78 if (Conf::config
.useDurability() && connList
.size()==1) UID
.destroy();
80 connList
.remove(this);
84 /** Commits active transaction.
85 * It makes all the changes made in the current transaction permanent and <br/>
86 * it also releases the locks held by the current transaction.<br/>
87 * After a transaction commits, application is required to start another <br/>
88 * transaction for further database operations.
91 DbRetVal
commit() { return conn
.commit(); }
93 /** Aborts the active transaction.
94 * undo all the changes made in the current transaction and it also <br/>
95 * releases the locks held by the current transaction.<br/>
96 * After a transaction rollback, application is required to start another <br/>
97 * transaction for further database operations.
100 DbRetVal
rollback() { return conn
.rollback(); }
102 /** Starts a transaction.
103 * The previous transaction should be either committed or rollback <br/>
104 * before beginTrans is called. <br/>
105 * Applications are required to start transaction before they attempt any <br>
106 * database operation.
107 * @param isoLevel isolation level. Default is read committed.
110 DbRetVal
beginTrans (IsolationLevel isoLevel
= READ_COMMITTED
,
111 TransSyncMode mode
= OSYNC
)
112 { return conn
.startTransaction(isoLevel
); }
114 Connection
& getConnObject(){ return conn
; }
115 bool isConnectionOpen() { if (isConnOpen
) return true; return false; };
116 DbRetVal
getExclusiveLock(){ return conn
.getExclusiveLock(); }
118 SqlStatement
* findInCache(char *stmtStr
);
119 void flushCacheStmt();
120 void addToCache(SqlStatement
*stmt
, char *stmtStr
);
121 void removeLeastUsed();
122 void setStmtNotInUse(char *stmtStr
);
123 void displayStmtCache();
124 void display() { displayStmtCache(); }
127 friend class SqlFactory
;