code reorg for Transactionw!
[csql.git] / src / sql / SqlFactory.cxx
blob0e5cdab9b07f2ec0c46d3b8f4969d0d9f32aec3f
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 ***************************************************************************/
16 #include <SqlFactory.h>
17 #include <SqlStatement.h>
18 #include <SqlConnection.h>
19 #include <SqlLogConnection.h>
20 #include <SqlLogStatement.h>
21 #include <SqlOdbcConnection.h>
22 #include <SqlOdbcStatement.h>
23 #include <SqlGwStatement.h>
24 #include <SqlNwConnection.h>
25 #include <SqlNwStatement.h>
27 Config Conf::config;
29 AbsSqlConnection* SqlFactory::createConnection(SqlApiImplType implFlag)
31 DbRetVal rv=OK;
32 AbsSqlConnection *conn = NULL ;
33 Conf::config.readAllValues(os::getenv("CSQL_CONFIG_FILE"));
34 bool isSqlLogNeeded = false;
35 #if !(defined MMDB && defined EMBED)
36 isSqlLogNeeded = Conf::config.useDurability() ||
37 (Conf::config.useCache() &&
38 (Conf::config.getCacheMode()==ASYNC_MODE ||
39 Conf::config.getCacheMode() == OFFLINE_MODE));
40 #else
41 isSqlLogNeeded = Conf::config.useDurability();
42 #endif
43 switch(implFlag)
45 case CSql:
46 if (!isSqlLogNeeded) conn = new SqlConnection();
47 else {
48 AbsSqlConnection *sqlCon = new SqlConnection();
49 #if (defined MMDB && defined EMBED)
50 ((SqlConnection *)sqlCon)->UID.create();
51 #endif
52 conn = new SqlLogConnection();
53 SqlLogConnection *logCon = (SqlLogConnection *)conn;
54 logCon->setNoMsgLog(true);
55 conn->setInnerConnection(sqlCon);
57 break;
58 case CSqlNetwork:
60 SqlNwConnection *sqlNwCon = new SqlNwConnection(CSqlNetwork);
61 sqlNwCon->setInnerConnection(NULL);
62 conn = sqlNwCon;
63 break;
65 case CSqlDirect:
66 conn = new SqlConnection();
67 break;
68 case CSqlLog:
70 //generates sql logs
71 AbsSqlConnection *sqlCon = new SqlConnection();
72 conn = new SqlLogConnection();
73 conn->setInnerConnection(sqlCon);
74 break;
76 #ifndef MMDB
77 case CSqlAdapter:
79 conn = new SqlOdbcConnection();
80 conn->setInnerConnection(NULL);
81 break;
83 case CSqlGateway:
85 SqlLogConnection *sqllogconn=NULL;
86 AbsSqlConnection *sqlCon = new SqlConnection();
87 SqlGwConnection *gwconn = new SqlGwConnection();
88 if (isSqlLogNeeded) {
89 sqllogconn = new SqlLogConnection();
90 sqllogconn->setInnerConnection(sqlCon);
91 bool isNoMsgLog = Conf::config.useDurability() &&
92 !(Conf::config.useCache() &&
93 Conf::config.getCacheMode()==ASYNC_MODE);
94 if (isNoMsgLog) sqllogconn->setNoMsgLog(true);
95 gwconn->setInnerConnection(sqllogconn);
96 } else gwconn->setInnerConnection(sqlCon);
98 //createAdapters for MultiDSN
99 rv=gwconn->createAdapters(gwconn);
100 if(rv != OK){
101 delete sqlCon;
102 delete gwconn;
103 if(isSqlLogNeeded) delete sqllogconn;
104 return NULL;
106 conn = gwconn;
107 break;
109 case CSqlNetworkAdapter:
111 SqlNwConnection *sqlNwCon = new SqlNwConnection(CSqlNetworkAdapter);
112 sqlNwCon->setInnerConnection(NULL);
113 conn = sqlNwCon;
114 break;
116 case CSqlNetworkGateway:
118 SqlNwConnection *sqlNwCon = new SqlNwConnection(CSqlNetworkGateway);
119 sqlNwCon->setInnerConnection(NULL);
120 conn = sqlNwCon;
121 break;
123 #endif
124 default:
125 printf("Todo");
126 break;
128 return conn;
131 AbsSqlStatement* SqlFactory::createStatement(SqlApiImplType implFlag)
133 AbsSqlStatement *stmt = NULL;
134 bool isSqlLogNeeded = false;
135 #if !(defined MMDB && defined EMBED)
136 isSqlLogNeeded = Conf::config.useDurability() ||
137 (Conf::config.useCache() &&
138 (Conf::config.getCacheMode()==ASYNC_MODE ||
139 Conf::config.getCacheMode() == OFFLINE_MODE));
140 #else
141 isSqlLogNeeded = Conf::config.useDurability();
142 #endif
143 switch(implFlag)
145 case CSql:
146 if (!isSqlLogNeeded) stmt = new SqlStatement();
147 else {
148 AbsSqlStatement *sqlStmt = new SqlStatement();
149 stmt = new SqlLogStatement();
150 stmt->setInnerStatement(sqlStmt);
152 break;
153 case CSqlNetwork:
155 SqlNwStatement *sqlNwStmt = new SqlNwStatement();
156 sqlNwStmt->setInnerStatement(NULL);
157 stmt = sqlNwStmt;
158 break;
160 case CSqlDirect:
161 stmt = new SqlStatement();
162 break;
163 case CSqlLog:
165 //generates sql logs
166 AbsSqlStatement *sqlStmt = new SqlStatement();
167 stmt = new SqlLogStatement();
168 stmt->setInnerStatement(sqlStmt);
169 break;
171 #ifndef MMDB
172 case CSqlAdapter:
174 stmt = new SqlOdbcStatement();
175 stmt->setInnerStatement(NULL);
176 break;
178 case CSqlGateway:
180 SqlGwStatement *gwstmt = new SqlGwStatement();
181 AbsSqlStatement *sqlstmt = new SqlStatement();
183 if (isSqlLogNeeded) {
184 AbsSqlStatement *sqllogstmt = new SqlLogStatement();
185 sqllogstmt->setInnerStatement(sqlstmt);
186 gwstmt->setInnerStatement(sqllogstmt);
187 } else gwstmt->setInnerStatement(sqlstmt);
188 AbsSqlStatement *adapterstmt = new SqlOdbcStatement();
189 gwstmt->setAdapter(adapterstmt);
190 stmt = gwstmt;
191 break;
193 case CSqlNetworkAdapter:
195 SqlNwStatement *sqlNwStmt = new SqlNwStatement();
196 sqlNwStmt->setInnerStatement(NULL);
197 stmt=sqlNwStmt;
198 break;
200 case CSqlNetworkGateway:
202 SqlNwStatement *sqlNwStmt = new SqlNwStatement();
203 sqlNwStmt->setInnerStatement(NULL);
204 stmt = sqlNwStmt;
205 break;
207 #endif
208 default:
209 printf("Todo");
210 break;
212 return stmt;