allocator fixes
[csql.git] / include / SqlConnection.h
blobbd71eba0e0145af740a7de3e94e1e0e8f31e3e65
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 SqlConnection : public AbsSqlConnection
41 Connection conn;
42 bool isConnOpen;
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);
49 AbsSqlStatement *getStmtFromHashTable(int stmtId);
50 void removeFromHashTable(int stmtID);
51 void addToHashTable(int stmtID, AbsSqlStatement* sHdl);
52 #endif
53 public:
54 static List connList;
55 static bool isInit;
56 #if (defined MMDB && EMBED)
57 static bool firstThread;
58 static GlobalUniqueID UID;
59 void *stmtBuckets;
60 #endif
61 void initialize();
62 List cachedStmts;
63 SqlConnection(){ innerConn = NULL; isConnOpen = false; }
64 ~SqlConnection();
66 /** opens connection to the sql engine
67 * @param user username for authentication
68 * @param pass password for authentication
69 * @return DbRetVal
71 DbRetVal connect (char *user, char * pass);
73 /** closes connection to the database and releases all the resources
74 * @return DbRetVal
76 DbRetVal disconnect () {
77 flushCacheStmt();
78 DbRetVal ret = conn.close();
79 if (ret == OK) isConnOpen = false;
80 # if (defined MMDB && defined EMBED)
81 if (Conf::config.useDurability() && connList.size()==1) UID.destroy();
82 # endif
83 connList.remove(this);
84 return ret;
87 /** Commits active transaction.
88 * It makes all the changes made in the current transaction permanent and <br/>
89 * it also releases the locks held by the current transaction.<br/>
90 * After a transaction commits, application is required to start another <br/>
91 * transaction for further database operations.
92 * @return DbRetVal
94 DbRetVal commit() { return conn.commit(); }
96 /** Aborts the active transaction.
97 * undo all the changes made in the current transaction and it also <br/>
98 * releases the locks held by the current transaction.<br/>
99 * After a transaction rollback, application is required to start another <br/>
100 * transaction for further database operations.
101 * @return DbRetVal
103 DbRetVal rollback() { return conn.rollback(); }
105 /** Starts a transaction.
106 * The previous transaction should be either committed or rollback <br/>
107 * before beginTrans is called. <br/>
108 * Applications are required to start transaction before they attempt any <br>
109 * database operation.
110 * @param isoLevel isolation level. Default is read committed.
111 * @return DbRetVal
113 DbRetVal beginTrans (IsolationLevel isoLevel = READ_COMMITTED,
114 TransSyncMode mode = OSYNC)
115 { return conn.startTransaction(isoLevel); }
117 Connection& getConnObject(){ return conn; }
118 bool isConnectionOpen() { if (isConnOpen) return true; return false; };
119 DbRetVal getExclusiveLock(){ return conn.getExclusiveLock(); }
121 SqlStatement* findInCache(char *stmtStr);
122 void flushCacheStmt();
123 void addToCache(SqlStatement *stmt, char *stmtStr);
124 void removeLeastUsed();
125 void setStmtNotInUse(char *stmtStr);
126 void displayStmtCache();
127 void display() { displayStmtCache(); }
130 friend class SqlFactory;
133 #endif