Commit f2a575c3 authored by Vadim Zeitlin's avatar Vadim Zeitlin

Update all backends to take connection_parameters instead of connect string.

There are still no real changes as only the connect string part of the
connection parameters is currently used, but this will make it possible to use
other information stored in connection_parameters from the backend code in the
future.

Also replace session::lastFactory_ and lastConnectString_ with a single
lastConnectParameters_ making the code slightly shorter and simpler.
parent 068c9043
......@@ -16,9 +16,9 @@ using namespace soci::details;
// concrete factory for ODBC concrete strategies
db2_session_backend * db2_backend_factory::make_session(
std::string const &connectString) const
connection_parameters const & parameters) const
{
return new db2_session_backend(connectString);
return new db2_session_backend(parameters);
}
db2_backend_factory const soci::db2;
......
......@@ -8,6 +8,7 @@
#define SOCI_DB2_SOURCE
#include "soci-db2.h"
#include <connection-parameters.h>
#ifdef _MSC_VER
#pragma warning(disable:4355)
......@@ -61,6 +62,7 @@ void db2_session_backend::parseKeyVal(std::string const & keyVal) {
}
}
/* DSN=SAMPLE;Uid=db2inst1;Pwd=db2inst1;AutoCommit=off */
void db2_session_backend::parseConnectString(std::string const & connectString) {
std::string processingString(connectString);
size_t delimiter=processingString.find_first_of(";");
......@@ -76,9 +78,9 @@ void db2_session_backend::parseConnectString(std::string const & connectString)
}
db2_session_backend::db2_session_backend(
std::string const & connectString /* DSN=SAMPLE;Uid=db2inst1;Pwd=db2inst1;AutoCommit=off */)
connection_parameters const & parameters)
{
parseConnectString(connectString);
parseConnectString(parameters.get_connect_string());
SQLRETURN cliRC = SQL_SUCCESS;
/* Prepare handles */
......
......@@ -218,7 +218,7 @@ struct db2_blob_backend : details::blob_backend
struct db2_session_backend : details::session_backend
{
db2_session_backend(std::string const& connectString);
db2_session_backend(connection_parameters const& parameters);
~db2_session_backend();
......@@ -249,7 +249,8 @@ struct db2_session_backend : details::session_backend
struct SOCI_DB2_DECL db2_backend_factory : backend_factory
{
db2_backend_factory() {}
db2_session_backend* make_session(std::string const& connectString) const;
db2_session_backend* make_session(
connection_parameters const & parameters) const;
};
extern SOCI_DB2_DECL db2_backend_factory const db2;
......
......@@ -18,9 +18,9 @@ using namespace soci::details;
// concrete factory for Empty concrete strategies
empty_session_backend* empty_backend_factory::make_session(
std::string const& connectString) const
connection_parameters const& parameters) const
{
return new empty_session_backend(connectString);
return new empty_session_backend(parameters);
}
empty_backend_factory const soci::empty;
......
......@@ -17,7 +17,7 @@ using namespace soci::details;
empty_session_backend::empty_session_backend(
std::string const & /* connectString */)
connection_parameters const & /* parameters */)
{
// ...
}
......
......@@ -154,7 +154,7 @@ struct empty_blob_backend : details::blob_backend
struct empty_session_backend : details::session_backend
{
empty_session_backend(std::string const& connectString);
empty_session_backend(connection_parameters const& parameters);
~empty_session_backend();
......@@ -173,8 +173,8 @@ struct empty_session_backend : details::session_backend
struct SOCI_EMPTY_DECL empty_backend_factory : backend_factory
{
empty_backend_factory() {}
empty_session_backend* make_session(std::string const& connectString) const;
empty_backend_factory() {}
empty_session_backend* make_session(connection_parameters const& parameters) const;
};
extern SOCI_EMPTY_DECL empty_backend_factory const empty;
......
......@@ -12,9 +12,9 @@
using namespace soci;
firebird_session_backend * firebird_backend_factory::make_session(
std::string const &connectString) const
connection_parameters const & parameters) const
{
return new firebird_session_backend(connectString);
return new firebird_session_backend(parameters);
}
firebird_backend_factory const soci::firebird;
......
......@@ -199,12 +199,12 @@ bool getISCConnectParameter(std::map<std::string, std::string> const & m, std::s
} // namespace anonymous
firebird_session_backend::firebird_session_backend(
std::string const & connectString) : dbhp_(0), trhp_(0)
connection_parameters const & parameters) : dbhp_(0), trhp_(0)
, decimals_as_strings_(false)
{
// extract connection parameters
std::map<std::string, std::string>
params(explodeISCConnectString(connectString));
params(explodeISCConnectString(parameters.get_connect_string()));
ISC_STATUS stat[stat_size];
std::string param;
......
......@@ -291,7 +291,7 @@ protected:
struct firebird_session_backend : details::session_backend
{
firebird_session_backend(std::string const &connectString);
firebird_session_backend(connection_parameters const & parameters);
~firebird_session_backend();
......@@ -322,9 +322,9 @@ struct firebird_session_backend : details::session_backend
struct firebird_backend_factory : backend_factory
{
firebird_backend_factory() {}
firebird_backend_factory() {}
virtual firebird_session_backend * make_session(
std::string const &connectString) const;
connection_parameters const & parameters) const;
};
extern SOCI_FIREBIRD_DECL firebird_backend_factory const firebird;
......
......@@ -21,9 +21,9 @@ using namespace soci::details;
// concrete factory for MySQL concrete strategies
mysql_session_backend * mysql_backend_factory::make_session(
std::string const &connectString) const
connection_parameters const & parameters) const
{
return new mysql_session_backend(connectString);
return new mysql_session_backend(parameters);
}
mysql_backend_factory const soci::mysql;
......
......@@ -8,6 +8,7 @@
#define SOCI_MYSQL_SOURCE
#include "soci-mysql.h"
#include <connection-parameters.h>
// std
#include <cctype>
#include <cerrno>
......@@ -272,14 +273,14 @@ void parse_connect_string(const string & connectString,
} // namespace anonymous
mysql_session_backend::mysql_session_backend(
std::string const & connectString)
connection_parameters const & parameters)
{
string host, user, password, db, unix_socket, ssl_ca, ssl_cert, ssl_key,
charset;
int port, local_infile;
bool host_p, user_p, password_p, db_p, unix_socket_p, port_p,
ssl_ca_p, ssl_cert_p, ssl_key_p, local_infile_p, charset_p;
parse_connect_string(connectString, &host, &host_p, &user, &user_p,
parse_connect_string(parameters.get_connect_string(), &host, &host_p, &user, &user_p,
&password, &password_p, &db, &db_p,
&unix_socket, &unix_socket_p, &port, &port_p,
&ssl_ca, &ssl_ca_p, &ssl_cert, &ssl_cert_p, &ssl_key, &ssl_key_p,
......
......@@ -228,7 +228,7 @@ struct mysql_blob_backend : details::blob_backend
struct mysql_session_backend : details::session_backend
{
mysql_session_backend(std::string const &connectString);
mysql_session_backend(connection_parameters const & parameters);
~mysql_session_backend();
......@@ -250,9 +250,9 @@ struct mysql_session_backend : details::session_backend
struct mysql_backend_factory : backend_factory
{
mysql_backend_factory() {}
mysql_backend_factory() {}
virtual mysql_session_backend * make_session(
std::string const &connectString) const;
connection_parameters const & parameters) const;
};
extern SOCI_MYSQL_DECL mysql_backend_factory const mysql;
......
......@@ -15,9 +15,9 @@ using namespace soci::details;
// concrete factory for ODBC concrete strategies
odbc_session_backend * odbc_backend_factory::make_session(
std::string const &connectString) const
connection_parameters const & parameters) const
{
return new odbc_session_backend(connectString);
return new odbc_session_backend(parameters);
}
odbc_backend_factory const soci::odbc;
......
......@@ -12,7 +12,8 @@
using namespace soci;
using namespace soci::details;
odbc_session_backend::odbc_session_backend(std::string const & connectString)
odbc_session_backend::odbc_session_backend(
connection_parameters const & parameters)
: henv_(0), hdbc_(0), product_(prod_uninitialized)
{
SQLRETURN rc;
......
......@@ -259,7 +259,7 @@ struct odbc_blob_backend : details::blob_backend
struct odbc_session_backend : details::session_backend
{
odbc_session_backend(std::string const &connectString);
odbc_session_backend(connection_parameters const & parameters);
~odbc_session_backend();
......@@ -403,9 +403,9 @@ inline bool odbc_standard_type_backend_base::use_string_for_bigint() const
struct odbc_backend_factory : backend_factory
{
odbc_backend_factory() {}
odbc_backend_factory() {}
virtual odbc_session_backend * make_session(
std::string const &connectString) const;
connection_parameters const & parameters) const;
};
extern SOCI_ODBC_DECL odbc_backend_factory const odbc;
......
......@@ -7,6 +7,7 @@
#define SOCI_ORACLE_SOURCE
#include "soci-oracle.h"
#include <connection-parameters.h>
#include <backend-loader.h>
#include <cctype>
#include <cstdio>
......@@ -106,13 +107,13 @@ void chop_connect_string(std::string const & connectString,
// concrete factory for Empty concrete strategies
oracle_session_backend * oracle_backend_factory::make_session(
std::string const &connectString) const
connection_parameters const & parameters) const
{
std::string serviceName, userName, password;
int mode;
bool decimals_as_strings;
chop_connect_string(connectString, serviceName, userName, password,
chop_connect_string(parameters.get_connect_string(), serviceName, userName, password,
mode, decimals_as_strings);
return new oracle_session_backend(serviceName, userName, password,
......
......@@ -274,9 +274,9 @@ struct oracle_session_backend : details::session_backend
struct oracle_backend_factory : backend_factory
{
oracle_backend_factory() {}
oracle_backend_factory() {}
virtual oracle_session_backend * make_session(
std::string const &connectString) const;
connection_parameters const & parameters) const;
};
extern SOCI_ORACLE_DECL oracle_backend_factory const oracle;
......
......@@ -23,9 +23,9 @@ using namespace soci::details;
// concrete factory for Empty concrete strategies
postgresql_session_backend * postgresql_backend_factory::make_session(
std::string const & connectString) const
connection_parameters const & parameters) const
{
return new postgresql_session_backend(connectString);
return new postgresql_session_backend(parameters);
}
postgresql_backend_factory const soci::postgresql;
......
......@@ -8,6 +8,7 @@
#define SOCI_POSTGRESQL_SOURCE
#include "soci-postgresql.h"
#include "error.h"
#include <connection-parameters.h>
#include <libpq/libpq-fs.h> // libpq
#include <cctype>
#include <cstdio>
......@@ -27,10 +28,11 @@ using namespace soci;
using namespace soci::details;
using namespace soci::details::postgresql;
postgresql_session_backend::postgresql_session_backend(std::string const& connectString)
postgresql_session_backend::postgresql_session_backend(
connection_parameters const& parameters)
: statementCount_(0)
{
PGconn* conn = PQconnectdb(connectString.c_str());
PGconn* conn = PQconnectdb(parameters.get_connect_string().c_str());
if (0 == conn || CONNECTION_OK != PQstatus(conn))
{
std::string msg = "Cannot establish connection to the database.";
......
......@@ -229,7 +229,7 @@ struct postgresql_blob_backend : details::blob_backend
struct postgresql_session_backend : details::session_backend
{
postgresql_session_backend(std::string const & connectString);
postgresql_session_backend(connection_parameters const & parameters);
~postgresql_session_backend();
......@@ -258,7 +258,7 @@ struct postgresql_backend_factory : backend_factory
{
postgresql_backend_factory() {}
virtual postgresql_session_backend * make_session(
std::string const & connectString) const;
connection_parameters const & parameters) const;
};
extern SOCI_POSTGRESQL_DECL postgresql_backend_factory const postgresql;
......
......@@ -18,9 +18,9 @@ using namespace soci::details;
// concrete factory for Empty concrete strategies
sqlite3_session_backend * sqlite3_backend_factory::make_session(
std::string const &connectString) const
connection_parameters const & parameters) const
{
return new sqlite3_session_backend(connectString);
return new sqlite3_session_backend(parameters);
}
sqlite3_backend_factory const soci::sqlite3;
......
......@@ -8,6 +8,8 @@
#include "soci-sqlite3.h"
#include <connection-parameters.h>
#include <sstream>
#include <string>
......@@ -40,10 +42,11 @@ void execude_hardcoded(sqlite_api::sqlite3* conn, char const* const query, char
sqlite3_session_backend::sqlite3_session_backend(
std::string const & connectString)
connection_parameters const & parameters)
{
int timeout = 0;
std::string synchronous;
std::string const & connectString = parameters.get_connect_string();
std::string dbname(connectString);
std::stringstream ssconn(connectString);
while (!ssconn.eof() && ssconn.str().find('=') != std::string::npos)
......
......@@ -234,7 +234,7 @@ private:
struct sqlite3_session_backend : details::session_backend
{
sqlite3_session_backend(std::string const &connectString);
sqlite3_session_backend(connection_parameters const & parameters);
~sqlite3_session_backend();
......@@ -255,9 +255,9 @@ struct sqlite3_session_backend : details::session_backend
struct sqlite3_backend_factory : backend_factory
{
sqlite3_backend_factory() {}
sqlite3_backend_factory() {}
virtual sqlite3_session_backend * make_session(
std::string const &connectString) const;
connection_parameters const & parameters) const;
};
extern SOCI_SQLITE3_DECL sqlite3_backend_factory const sqlite3;
......
......@@ -33,6 +33,11 @@ void parseConnectString(std::string const & connectString,
} // namespace anonymous
connection_parameters::connection_parameters()
: factory_(NULL)
{
}
connection_parameters::connection_parameters(backend_factory const & factory,
std::string const & connectString)
: factory_(&factory), connectString_(connectString)
......
......@@ -21,6 +21,7 @@ class backend_factory;
class SOCI_DECL connection_parameters
{
public:
connection_parameters();
connection_parameters(backend_factory const & factory, std::string const & connectString);
connection_parameters(std::string const & backendName, std::string const & connectString);
explicit connection_parameters(std::string const & fullConnectString);
......
......@@ -34,45 +34,47 @@ void ensureConnected(session_backend * backEnd)
session::session()
: once(this), prepare(this), query_transformation_(NULL), logStream_(NULL),
lastFactory_(NULL), uppercaseColumnNames_(false), backEnd_(NULL),
uppercaseColumnNames_(false), backEnd_(NULL),
isFromPool_(false), pool_(NULL)
{
}
session::session(connection_parameters const & parameters)
: once(this), prepare(this), logStream_(NULL),
lastFactory_(&parameters.get_factory()), lastConnectString_(parameters.get_connect_string()),
: once(this), prepare(this), query_transformation_(NULL), logStream_(NULL),
lastConnectParameters_(parameters),
uppercaseColumnNames_(false), backEnd_(NULL),
isFromPool_(false), pool_(NULL)
{
open(parameters);
open(lastConnectParameters_);
}
session::session(backend_factory const & factory,
std::string const & connectString)
: once(this), prepare(this), query_transformation_(NULL), logStream_(NULL),
lastFactory_(&factory), lastConnectString_(connectString),
lastConnectParameters_(factory, connectString),
uppercaseColumnNames_(false), backEnd_(NULL),
isFromPool_(false), pool_(NULL)
{
open(factory, connectString);
open(lastConnectParameters_);
}
session::session(std::string const & backendName,
std::string const & connectString)
: once(this), prepare(this), query_transformation_(NULL), logStream_(NULL),
lastConnectParameters_(backendName, connectString),
uppercaseColumnNames_(false), backEnd_(NULL),
isFromPool_(false), pool_(NULL)
{
open(backendName, connectString);
open(lastConnectParameters_);
}
session::session(std::string const & connectString)
: once(this), prepare(this), query_transformation_(NULL), logStream_(NULL),
lastConnectParameters_(connectString),
uppercaseColumnNames_(false), backEnd_(NULL),
isFromPool_(false), pool_(NULL)
{
open(connectString);
open(lastConnectParameters_);
}
session::session(connection_pool & pool)
......@@ -112,9 +114,14 @@ void session::open(connection_parameters const & parameters)
throw soci_error("Cannot open already connected session.");
}
lastFactory_ = &parameters.get_factory();
lastConnectString_ = parameters.get_connect_string();
backEnd_ = lastFactory_->make_session(lastConnectString_);
backend_factory const * const factory = parameters.get_factory();
if (factory == NULL)
{
throw soci_error("Cannot connect without a valid backend.");
}
backEnd_ = factory->make_session(parameters);
lastConnectParameters_ = parameters;
}
}
......@@ -158,7 +165,8 @@ void session::reconnect()
}
else
{
if (lastFactory_ == NULL)
backend_factory const * const lastFactory = lastConnectParameters_.get_factory();
if (lastFactory == NULL)
{
throw soci_error("Cannot reconnect without previous connection.");
}
......@@ -168,7 +176,7 @@ void session::reconnect()
close();
}
backEnd_ = lastFactory_->make_session(lastConnectString_);
backEnd_ = lastFactory->make_session(lastConnectParameters_);
}
}
......
......@@ -10,6 +10,7 @@
#include "once-temp-type.h"
#include "query_transformation.h"
#include "connection-parameters.h"
// std
#include <cstddef>
......@@ -32,7 +33,6 @@ class blob_backend;
} // namespace details
class connection_parameters;
class connection_pool;
class SOCI_DECL session
......@@ -125,8 +125,7 @@ private:
std::ostream * logStream_;
std::string lastQuery_;
backend_factory const * lastFactory_;
std::string lastConnectString_;
connection_parameters lastConnectParameters_;
bool uppercaseColumnNames_;
......
......@@ -260,6 +260,8 @@ private:
// simple base class for the session back-end factory
class connection_parameters;
class SOCI_DECL backend_factory
{
public:
......@@ -267,7 +269,7 @@ public:
virtual ~backend_factory() {}
virtual details::session_backend* make_session(
std::string const& connectString) const = 0;
connection_parameters const& parameters) const = 0;
};
} // namespace soci
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment