improving code readability
[csql.git] / src / sql / SqlFactory.cxx
blob438e12a85fd107db58a8482545289a2b884249a8
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() && Conf::config.getCacheMode() == ASYNC_MODE);
38 #else
39 isSqlLogNeeded = Conf::config.useDurability();
40 #endif
41 switch(implFlag)
43 case CSql:
44 if (!isSqlLogNeeded) conn = new SqlConnection();
45 else {
46 AbsSqlConnection *sqlCon = new SqlConnection();
47 #if (defined MMDB && defined EMBED)
48 ((SqlConnection *)sqlCon)->UID.create();
49 #endif
50 conn = new SqlLogConnection();
51 SqlLogConnection *logCon = (SqlLogConnection *)conn;
52 logCon->setNoMsgLog(true);
53 conn->setInnerConnection(sqlCon);
55 break;
56 case CSqlNetwork:
58 SqlNwConnection *sqlNwCon = new SqlNwConnection(CSqlNetwork);
59 sqlNwCon->setInnerConnection(NULL);
60 conn = sqlNwCon;
61 break;
63 case CSqlDirect:
64 conn = new SqlConnection();
65 break;
66 case CSqlLog:
68 //generates sql logs
69 AbsSqlConnection *sqlCon = new SqlConnection();
70 conn = new SqlLogConnection();
71 conn->setInnerConnection(sqlCon);
72 break;
74 #ifndef MMDB
75 case CSqlAdapter:
77 conn = new SqlOdbcConnection();
78 conn->setInnerConnection(NULL);
79 break;
81 case CSqlGateway:
83 SqlLogConnection *sqllogconn=NULL;
84 AbsSqlConnection *sqlCon = new SqlConnection();
85 SqlGwConnection *gwconn = new SqlGwConnection();
86 if (isSqlLogNeeded) {
87 sqllogconn = new SqlLogConnection();
88 sqllogconn->setInnerConnection(sqlCon);
89 gwconn->setInnerConnection(sqllogconn);
90 } else gwconn->setInnerConnection(sqlCon);
92 //createAdapters for MultiDSN
93 rv=gwconn->createAdapters(gwconn);
94 if(rv != OK){
95 delete sqlCon;
96 delete gwconn;
97 if(isSqlLogNeeded) delete sqllogconn;
98 return NULL;
100 conn = gwconn;
101 break;
103 case CSqlNetworkAdapter:
105 SqlNwConnection *sqlNwCon = new SqlNwConnection(CSqlNetworkAdapter);
106 sqlNwCon->setInnerConnection(NULL);
107 conn = sqlNwCon;
108 break;
110 case CSqlNetworkGateway:
112 SqlNwConnection *sqlNwCon = new SqlNwConnection(CSqlNetworkGateway);
113 sqlNwCon->setInnerConnection(NULL);
114 conn = sqlNwCon;
115 break;
117 #endif
118 default:
119 printf("Todo");
120 break;
122 return conn;
125 AbsSqlStatement* SqlFactory::createStatement(SqlApiImplType implFlag)
127 AbsSqlStatement *stmt = NULL;
128 bool isSqlLogNeeded = false;
129 #if !(defined MMDB && defined EMBED)
130 isSqlLogNeeded = Conf::config.useDurability() ||
131 (Conf::config.useCache() && Conf::config.getCacheMode() == ASYNC_MODE);
132 #else
133 isSqlLogNeeded = Conf::config.useDurability();
134 #endif
135 switch(implFlag)
137 case CSql:
138 if (!isSqlLogNeeded) stmt = new SqlStatement();
139 else {
140 AbsSqlStatement *sqlStmt = new SqlStatement();
141 stmt = new SqlLogStatement();
142 stmt->setInnerStatement(sqlStmt);
144 break;
145 case CSqlNetwork:
147 SqlNwStatement *sqlNwStmt = new SqlNwStatement();
148 sqlNwStmt->setInnerStatement(NULL);
149 stmt = sqlNwStmt;
150 break;
152 case CSqlDirect:
153 stmt = new SqlStatement();
154 break;
155 case CSqlLog:
157 //generates sql logs
158 AbsSqlStatement *sqlStmt = new SqlStatement();
159 stmt = new SqlLogStatement();
160 stmt->setInnerStatement(sqlStmt);
161 break;
163 #ifndef MMDB
164 case CSqlAdapter:
166 stmt = new SqlOdbcStatement();
167 stmt->setInnerStatement(NULL);
168 break;
170 case CSqlGateway:
172 SqlGwStatement *gwstmt = new SqlGwStatement();
173 AbsSqlStatement *sqlstmt = new SqlStatement();
175 if (isSqlLogNeeded) {
176 AbsSqlStatement *sqllogstmt = new SqlLogStatement();
177 sqllogstmt->setInnerStatement(sqlstmt);
178 gwstmt->setInnerStatement(sqllogstmt);
179 } else gwstmt->setInnerStatement(sqlstmt);
180 AbsSqlStatement *adapterstmt = new SqlOdbcStatement();
181 gwstmt->setAdapter(adapterstmt);
182 stmt = gwstmt;
183 break;
185 case CSqlNetworkAdapter:
187 SqlNwStatement *sqlNwStmt = new SqlNwStatement();
188 sqlNwStmt->setInnerStatement(NULL);
189 stmt=sqlNwStmt;
190 break;
192 case CSqlNetworkGateway:
194 SqlNwStatement *sqlNwStmt = new SqlNwStatement();
195 sqlNwStmt->setInnerStatement(NULL);
196 stmt = sqlNwStmt;
197 break;
199 #endif
200 default:
201 printf("Todo");
202 break;
204 return stmt;