Embedded support for CSQL.
[csql.git] / include / SqlConnection.h
blob65f248b76162d84b90b5f556b1dbaabee59f6167
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 #if (defined MMDB && defined EMBED)
40 DbRetVal recoverCsqlDB();
41 DbRetVal recoverSystemAndUserDB();
42 DbRetVal applySchemaFile(FILE *fp);
43 char getQueryFromSchemaFile(FILE *fp, char *buf);
44 int applyRedoLogs(char *redoFile);
45 AbsSqlStatement *getStmtFromHashTable(int stmtId);
46 void removeFromHashTable(int stmtID);
47 void addToHashTable(int stmtID, AbsSqlStatement* sHdl);
48 #endif
49 public:
50 static List connList;
51 static bool isInit;
52 #if (defined MMDB && EMBED)
53 static bool firstThread;
54 static GlobalUniqueID UID;
55 void *stmtBuckets;
56 #endif
57 void initialize();
58 List cachedStmts;
59 SqlConnection(){ innerConn = NULL; isConnOpen = false; }
60 ~SqlConnection();
62 /** opens connection to the sql engine
63 * @param user username for authentication
64 * @param pass password for authentication
65 * @return DbRetVal
67 DbRetVal connect (char *user, char * pass);
69 /** closes connection to the database and releases all the resources
70 * @return DbRetVal
72 DbRetVal disconnect () {
73 DbRetVal ret = conn.close();
74 if (ret == OK) isConnOpen = false;
75 # if (defined MMDB && defined EMBED)
76 if (Conf::config.useDurability() && connList.size()==1) UID.destroy();
77 # endif
78 connList.remove(this);
79 return ret;
82 /** Commits active transaction.
83 * It makes all the changes made in the current transaction permanent and <br/>
84 * it also releases the locks held by the current transaction.<br/>
85 * After a transaction commits, application is required to start another <br/>
86 * transaction for further database operations.
87 * @return DbRetVal
89 DbRetVal commit() { return conn.commit(); }
91 /** Aborts the active transaction.
92 * undo all the changes made in the current transaction and it also <br/>
93 * releases the locks held by the current transaction.<br/>
94 * After a transaction rollback, application is required to start another <br/>
95 * transaction for further database operations.
96 * @return DbRetVal
98 DbRetVal rollback() { return conn.rollback(); }
100 /** Starts a transaction.
101 * The previous transaction should be either committed or rollback <br/>
102 * before beginTrans is called. <br/>
103 * Applications are required to start transaction before they attempt any <br>
104 * database operation.
105 * @param isoLevel isolation level. Default is read committed.
106 * @return DbRetVal
108 DbRetVal beginTrans (IsolationLevel isoLevel = READ_COMMITTED,
109 TransSyncMode mode = OSYNC)
110 { return conn.startTransaction(isoLevel); }
112 Connection& getConnObject(){ return conn; }
113 bool isConnectionOpen() { if (isConnOpen) return true; return false; };
114 DbRetVal getExclusiveLock(){ return conn.getExclusiveLock(); }
116 SqlStatement* findInCache(char *stmtStr);
117 void flushCacheStmt();
118 void addToCache(SqlStatement *stmt, char *stmtStr);
119 void removeLeastUsed();
121 friend class SqlFactory;
124 #endif