00001 // 00002 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton 00003 // Distributed under the Boost Software License, Version 1.0. 00004 // (See accompanying file LICENSE_1_0.txt or copy at 00005 // http://www.boost.org/LICENSE_1_0.txt) 00006 // 00007 00008 #define SOCI_SOURCE 00009 #include "session.h" 00010 #include "connection-pool.h" 00011 #include "soci-backend.h" 00012 #include "backend-loader.h" 00013 00014 #ifdef _MSC_VER 00015 #pragma warning(disable:4355) 00016 #endif 00017 00018 using namespace soci; 00019 using namespace soci::details; 00020 00021 namespace // anonymous 00022 { 00023 00024 void parseConnectString(std::string const & connectString, 00025 std::string & backendName, 00026 std::string & connectionParameters) 00027 { 00028 std::string const protocolSeparator = "://"; 00029 00030 std::string::size_type const p = connectString.find(protocolSeparator); 00031 if (p == std::string::npos) 00032 { 00033 throw soci_error("No backend name found in " + connectString); 00034 } 00035 00036 backendName = connectString.substr(0, p); 00037 connectionParameters = connectString.substr(p + protocolSeparator.size()); 00038 } 00039 00040 void ensureConnected(session_backend * backEnd) 00041 { 00042 if (backEnd == NULL) 00043 { 00044 throw soci_error("Session is not connected."); 00045 } 00046 } 00047 00048 } // namespace anonymous 00049 00050 session::session() 00051 : once(this), prepare(this), logStream_(NULL), 00052 lastFactory_(NULL), uppercaseColumnNames_(false), backEnd_(NULL), 00053 isFromPool_(false), pool_(NULL) 00054 { 00055 } 00056 00057 session::session(backend_factory const & factory, 00058 std::string const & connectString) 00059 : once(this), prepare(this), logStream_(NULL), 00060 lastFactory_(&factory), lastConnectString_(connectString), 00061 uppercaseColumnNames_(false), 00062 isFromPool_(false), pool_(NULL) 00063 { 00064 backEnd_ = factory.make_session(connectString); 00065 } 00066 00067 session::session(std::string const & backendName, 00068 std::string const & connectString) 00069 : once(this), prepare(this), logStream_(NULL), 00070 uppercaseColumnNames_(false), 00071 isFromPool_(false), pool_(NULL) 00072 { 00073 backend_factory const & factory = dynamic_backends::get(backendName); 00074 00075 lastFactory_ = &factory; 00076 lastConnectString_ = connectString; 00077 00078 backEnd_ = factory.make_session(connectString); 00079 } 00080 00081 session::session(std::string const & connectString) 00082 : once(this), prepare(this), logStream_(NULL), 00083 uppercaseColumnNames_(false), 00084 isFromPool_(false), pool_(NULL) 00085 { 00086 std::string backendName; 00087 std::string connectionParameters; 00088 00089 parseConnectString(connectString, backendName, connectionParameters); 00090 00091 backend_factory const & factory = dynamic_backends::get(backendName); 00092 00093 lastFactory_ = &factory; 00094 lastConnectString_ = connectionParameters; 00095 00096 backEnd_ = factory.make_session(connectionParameters); 00097 } 00098 00099 session::session(connection_pool & pool) 00100 : isFromPool_(true), pool_(&pool) 00101 { 00102 poolPosition_ = pool.lease(); 00103 session & pooledSession = pool.at(poolPosition_); 00104 00105 once.set_session(&pooledSession); 00106 prepare.set_session(&pooledSession); 00107 backEnd_ = pooledSession.get_backend(); 00108 } 00109 00110 session::~session() 00111 { 00112 if (isFromPool_) 00113 { 00114 pool_->give_back(poolPosition_); 00115 } 00116 else 00117 { 00118 delete backEnd_; 00119 } 00120 } 00121 00122 void session::open(backend_factory const & factory, 00123 std::string const & connectString) 00124 { 00125 if (isFromPool_) 00126 { 00127 pool_->at(poolPosition_).open(factory, connectString); 00128 } 00129 else 00130 { 00131 if (backEnd_ != NULL) 00132 { 00133 throw soci_error("Cannot open already connected session."); 00134 } 00135 00136 backEnd_ = factory.make_session(connectString); 00137 lastFactory_ = &factory; 00138 lastConnectString_ = connectString; 00139 } 00140 } 00141 00142 void session::open(std::string const & backendName, 00143 std::string const & connectString) 00144 { 00145 if (isFromPool_) 00146 { 00147 pool_->at(poolPosition_).open(backendName, connectString); 00148 } 00149 else 00150 { 00151 if (backEnd_ != NULL) 00152 { 00153 throw soci_error("Cannot open already connected session."); 00154 } 00155 00156 backend_factory const & factory = dynamic_backends::get(backendName); 00157 00158 backEnd_ = factory.make_session(connectString); 00159 lastFactory_ = &factory; 00160 lastConnectString_ = connectString; 00161 } 00162 } 00163 00164 void session::open(std::string const & connectString) 00165 { 00166 if (isFromPool_) 00167 { 00168 pool_->at(poolPosition_).open(connectString); 00169 } 00170 else 00171 { 00172 if (backEnd_ != NULL) 00173 { 00174 throw soci_error("Cannot open already connected session."); 00175 } 00176 00177 std::string backendName; 00178 std::string connectionParameters; 00179 00180 parseConnectString(connectString, backendName, connectionParameters); 00181 00182 backend_factory const & factory = dynamic_backends::get(backendName); 00183 00184 backEnd_ = factory.make_session(connectionParameters); 00185 lastFactory_ = &factory; 00186 lastConnectString_ = connectionParameters; 00187 } 00188 } 00189 00190 void session::close() 00191 { 00192 if (isFromPool_) 00193 { 00194 pool_->at(poolPosition_).close(); 00195 } 00196 else 00197 { 00198 delete backEnd_; 00199 backEnd_ = NULL; 00200 } 00201 } 00202 00203 void session::reconnect() 00204 { 00205 if (isFromPool_) 00206 { 00207 pool_->at(poolPosition_).reconnect(); 00208 } 00209 else 00210 { 00211 if (lastFactory_ == NULL) 00212 { 00213 throw soci_error("Cannot reconnect without previous connection."); 00214 } 00215 00216 if (backEnd_ != NULL) 00217 { 00218 close(); 00219 } 00220 00221 backEnd_ = lastFactory_->make_session(lastConnectString_); 00222 } 00223 } 00224 00225 void session::begin() 00226 { 00227 backEnd_->begin(); 00228 } 00229 00230 void session::commit() 00231 { 00232 backEnd_->commit(); 00233 } 00234 00235 void session::rollback() 00236 { 00237 backEnd_->rollback(); 00238 } 00239 00240 std::ostringstream & session::get_query_stream() 00241 { 00242 if (isFromPool_) 00243 { 00244 return pool_->at(poolPosition_).get_query_stream(); 00245 } 00246 else 00247 { 00248 return query_stream_; 00249 } 00250 } 00251 00252 void session::set_log_stream(std::ostream * s) 00253 { 00254 if (isFromPool_) 00255 { 00256 pool_->at(poolPosition_).set_log_stream(s); 00257 } 00258 else 00259 { 00260 logStream_ = s; 00261 } 00262 } 00263 00264 std::ostream * session::get_log_stream() const 00265 { 00266 if (isFromPool_) 00267 { 00268 return pool_->at(poolPosition_).get_log_stream(); 00269 } 00270 else 00271 { 00272 return logStream_; 00273 } 00274 } 00275 00276 void session::log_query(std::string const & query) 00277 { 00278 if (isFromPool_) 00279 { 00280 pool_->at(poolPosition_).log_query(query); 00281 } 00282 else 00283 { 00284 if (logStream_ != NULL) 00285 { 00286 *logStream_ << query << '\n'; 00287 } 00288 00289 lastQuery_ = query; 00290 } 00291 } 00292 00293 std::string session::get_last_query() const 00294 { 00295 if (isFromPool_) 00296 { 00297 return pool_->at(poolPosition_).get_last_query(); 00298 } 00299 else 00300 { 00301 return lastQuery_; 00302 } 00303 } 00304 00305 void session::set_got_data(bool gotData) 00306 { 00307 if (isFromPool_) 00308 { 00309 pool_->at(poolPosition_).set_got_data(gotData); 00310 } 00311 else 00312 { 00313 gotData_ = gotData; 00314 } 00315 } 00316 00317 bool session::got_data() const 00318 { 00319 if (isFromPool_) 00320 { 00321 return pool_->at(poolPosition_).got_data(); 00322 } 00323 else 00324 { 00325 return gotData_; 00326 } 00327 } 00328 00329 void session::uppercase_column_names(bool forceToUpper) 00330 { 00331 if (isFromPool_) 00332 { 00333 pool_->at(poolPosition_).uppercase_column_names(forceToUpper); 00334 } 00335 else 00336 { 00337 uppercaseColumnNames_ = forceToUpper; 00338 } 00339 } 00340 00341 bool session::get_uppercase_column_names() const 00342 { 00343 if (isFromPool_) 00344 { 00345 return pool_->at(poolPosition_).get_uppercase_column_names(); 00346 } 00347 else 00348 { 00349 return uppercaseColumnNames_; 00350 } 00351 } 00352 00353 std::string session::get_backend_name() const 00354 { 00355 return backEnd_->get_backend_name(); 00356 } 00357 00358 statement_backend * session::make_statement_backend() 00359 { 00360 ensureConnected(backEnd_); 00361 00362 return backEnd_->make_statement_backend(); 00363 } 00364 00365 rowid_backend * session::make_rowid_backend() 00366 { 00367 ensureConnected(backEnd_); 00368 00369 return backEnd_->make_rowid_backend(); 00370 } 00371 00372 blob_backend * session::make_blob_backend() 00373 { 00374 ensureConnected(backEnd_); 00375 00376 return backEnd_->make_blob_backend(); 00377 }
Generated on Sun Oct 3 2010 17:42:16 for EXTRAS-SOCI by Doxygen 1.7.1