From 00d972abb9dcbe7c99cd9b94cefc725024cfb4bc Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 17 Nov 2011 08:44:01 +0200 Subject: Add initial database class implementation --- odb/mssql/database.cxx | 246 ++++++++++++++++++++++++++++++++++++++++++++++++ odb/mssql/database.hxx | 203 +++++++++++++++++++++++++++++++++++++++ odb/mssql/database.ixx | 24 +++++ odb/mssql/error.cxx | 12 +-- odb/mssql/error.hxx | 6 +- odb/mssql/makefile | 1 + odb/mssql/mssql-fwd.hxx | 1 + 7 files changed, 484 insertions(+), 9 deletions(-) create mode 100644 odb/mssql/database.cxx create mode 100644 odb/mssql/database.hxx create mode 100644 odb/mssql/database.ixx diff --git a/odb/mssql/database.cxx b/odb/mssql/database.cxx new file mode 100644 index 0000000..b6ef146 --- /dev/null +++ b/odb/mssql/database.cxx @@ -0,0 +1,246 @@ +// file : odb/mssql/database.cxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +//@@ disabled functionality + +#include + +#include +#include +#include +#include + +#include + +using namespace std; + +namespace odb +{ + namespace mssql + { + database:: + database (const string& c, SQLHENV e, auto_ptr f) + : connect_string_ (c), environment_ (e), factory_ (f) + { + if (environment_ == 0) + { + SQLRETURN r ( + SQLAllocHandle (SQL_HANDLE_ENV, + SQL_NULL_HANDLE, + &environment_)); + + if (!SQL_SUCCEEDED (r)) + throw database_exception ( + 0, "?????", "unable to allocate environment handle"); + + auto_environment_.reset (environment_); + + // Set ODBC version. + // + r = SQLSetEnvAttr (environment_, + SQL_ATTR_ODBC_VERSION, + (SQLPOINTER) SQL_OV_ODBC3, + 0); + + if (!SQL_SUCCEEDED (r)) + translate_error (environment_, SQL_HANDLE_ENV); + } + } + + /* + database:: + database (const string& user, + const string& password, + const string& service, + const string& host, + unsigned int port, + ub2 charset, + ub2 ncharset, + OCIEnv* environment, + auto_ptr factory) + : user_ (user), + password_ (password), + service_ (service), + host_ (host), + port_ (port), + charset_ (charset), + ncharset_ (ncharset), + environment_ (environment), + factory_ (factory) + { + if (environment_ == 0) + { + sword s (OCIEnvNlsCreate (&environment_, + OCI_THREADED, + 0, 0, 0, 0, 0, 0, + charset, + ncharset)); + + if (s == OCI_ERROR) + translate_error (environment_); + + auto_environment_.reset (environment_); + } + + ostringstream ss; + + if (!host.empty ()) + { + ss << "//" << host_; + + if (port != 0) + ss << ":" << port; + } + + if (!service_.empty ()) + { + if (!host.empty ()) + ss << "/" << service_; + else + ss << service_; + } + + // @@ Quote FQ connect identifier. + // + db_ = ss.str (); + + if (factory_.get () == 0) + factory_.reset (new connection_pool_factory ()); + + factory_->database (*this); + } + */ + + /* + database:: + database (int& argc, + char* argv[], + bool erase, + ub2 charset, + ub2 ncharset, + OCIEnv* environment, + auto_ptr factory) + : port_ (0), + charset_ (charset), + ncharset_ (ncharset), + environment_ (environment), + factory_ (factory) + { + if (environment_ == 0) + { + sword s (OCIEnvNlsCreate (&environment_, + OCI_THREADED, + 0, 0, 0, 0, 0, 0, + charset, + ncharset)); + + if (s == OCI_ERROR) + translate_error (environment_); + + auto_environment_.reset (environment_); + } + + using namespace details; + + try + { + cli::argv_file_scanner scan (argc, argv, "--options-file", erase); + options ops (scan, cli::unknown_mode::skip, cli::unknown_mode::skip); + + if (ops.user_specified ()) + user_ = ops.user (); + + if (ops.password_specified ()) + password_ = ops.password (); + + if (ops.database_specified ()) + { + if (ops.host_specified () || + ops.port_specified () || + ops.service_specified ()) + + throw cli_exception ("--host, --port, or --service options " + "cannot be specified together with " + "--database option"); + db_ = ops.database (); + } + else + { + bool host_present (false); + ostringstream oss; + + if (ops.host_specified () && !ops.host ().empty ()) + { + host_present = true; + + host_ = ops.host (); + oss << "//" << host_; + + if (ops.port_specified ()) + { + port_ = ops.port (); + + if (port_ != 0) + oss << ":" << port_; + } + } + + if (ops.service_specified () && !ops.service ().empty ()) + { + service_ = ops.service (); + + if (host_present) + oss << "/" << service_; + else + oss << service_; + } + + db_ = oss.str (); + } + + // @@ Quote FQ connect identifier. + // + } + catch (const cli::exception& e) + { + ostringstream oss; + oss << e; + throw cli_exception (oss.str ()); + } + + if (factory_.get () == 0) + factory_.reset (new connection_pool_factory ()); + + factory_->database (*this); + } + */ + + void database:: + print_usage (std::ostream& os) + { + details::options::print_usage (os); + } + + database:: + ~database () + { + } + + /* + transaction_impl* database:: + begin () + { + return new transaction_impl (*this); + } + + odb::connection* database:: + connection_ () + { + connection_ptr c (factory_->connect ()); + return c.release (); + } + */ + } +} diff --git a/odb/mssql/database.hxx b/odb/mssql/database.hxx new file mode 100644 index 0000000..d5d5597 --- /dev/null +++ b/odb/mssql/database.hxx @@ -0,0 +1,203 @@ +// file : odb/mssql/database.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#ifndef ODB_MSSQL_DATABASE_HXX +#define ODB_MSSQL_DATABASE_HXX + +//@@ disabled functionality + +#include + +#include +#include // std::auto_ptr +#include // std::ostream + +#include + +#include +#include +//#include +//#include +//#include +#include +#include + +#include + +namespace odb +{ + namespace mssql + { + class transaction_impl; + + class LIBODB_MSSQL_EXPORT database: public odb::database + { + public: + database (const std::string& connect_string, + SQLHENV environment = 0, + std::auto_ptr factory = + std::auto_ptr (0)); + + /* + database (const std::string& user, + const std::string& password, + const std::string& service, + const std::string& host = "", + unsigned int port = 0, + ub2 charset = 0, + ub2 ncharset = 0, + OCIEnv* environment = 0, + std::auto_ptr factory = + std::auto_ptr (0)); + + // Extract the database parameters from the command line. The + // following options are recognized: + // + // --user + // --password + // --database + // --service + // --host + // --port + // --options-file + // + // For more information, see the output of the print_usage() function + // below. If erase is true, the above options are removed from the + // argv array and the argc count is updated accordingly. This + // constructor may throw the cli_exception exception. + // + database (int& argc, + char* argv[], + bool erase = false, + ub2 charset = 0, + ub2 ncharset = 0, + OCIEnv* environment = 0, + std::auto_ptr = + std::auto_ptr (0)); + + */ + + static void + print_usage (std::ostream&); + + + public: + //virtual transaction_impl* + //begin (); + + public: + //connection_ptr + //connection (); + + public: + /* + const std::string& + user () const + { + return user_; + } + + const std::string& + password () const + { + return password_; + } + + const std::string& + db () const + { + return db_; + } + + const std::string& + service () const + { + return service_; + } + + const std::string& + host () const + { + return host_; + } + + unsigned int + port () const + { + return port_; + } + */ + + const std::string& + connect_string () const + { + return connect_string_; + } + + SQLHENV + environment () + { + return environment_; + } + + // SQL statement tracing. + // + public: + /* + typedef mssql::tracer tracer_type; + + void + tracer (tracer_type& t) + { + odb::database::tracer (t); + } + + void + tracer (tracer_type* t) + { + odb::database::tracer (t); + } + + using odb::database::tracer; + */ + + public: + virtual + ~database (); + + protected: + //virtual odb::connection* + //connection_ (); + + private: + /* + std::string user_; + std::string password_; + + std::string db_; + + std::string service_; + std::string host_; + unsigned int port_; + + ub2 charset_; + ub2 ncharset_; + */ + + std::string connect_string_; + + auto_handle auto_environment_; + SQLHENV environment_; + + std::auto_ptr factory_; + }; + } +} + +#include + +#include + +#endif // ODB_MSSQL_DATABASE_HXX diff --git a/odb/mssql/database.ixx b/odb/mssql/database.ixx new file mode 100644 index 0000000..3c8bf8e --- /dev/null +++ b/odb/mssql/database.ixx @@ -0,0 +1,24 @@ +// file : odb/mssql/database.ixx +// author : Constantin Michael +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +//@@ disabled functionality + +namespace odb +{ + namespace mssql + { + /* + inline connection_ptr database:: + connection () + { + // Go through the virtual connection_() function instead of + // directly to allow overriding. + // + return connection_ptr ( + static_cast (connection_ ())); + } + */ + } +} diff --git a/odb/mssql/error.cxx b/odb/mssql/error.cxx index f2e172b..0d26ae8 100644 --- a/odb/mssql/error.cxx +++ b/odb/mssql/error.cxx @@ -56,16 +56,16 @@ namespace odb } void - translate_error (const auto_handle& h) - { - translate_error (h, SQL_HANDLE_ENV, 0); - } - - void translate_error (connection& /*c*/) { //@@ TODO enable (also header inclusion) // translate_error (c.handle (), SQL_HANDLE_DBC, &c); } + + LIBODB_MSSQL_EXPORT void + translate_error (SQLHANDLE h, SQLSMALLINT htype) + { + translate_error (h, htype, 0); + } } } diff --git a/odb/mssql/error.hxx b/odb/mssql/error.hxx index 2cbea5c..35906b7 100644 --- a/odb/mssql/error.hxx +++ b/odb/mssql/error.hxx @@ -10,7 +10,7 @@ #include #include // connection -#include +#include #include namespace odb @@ -18,10 +18,10 @@ namespace odb namespace mssql { LIBODB_MSSQL_EXPORT void - translate_error (const auto_handle&); + translate_error (connection&); LIBODB_MSSQL_EXPORT void - translate_error (connection&); + translate_error (SQLHANDLE, SQLSMALLINT htype); } } diff --git a/odb/mssql/makefile b/odb/mssql/makefile index fbe46f4..70fc9c6 100644 --- a/odb/mssql/makefile +++ b/odb/mssql/makefile @@ -8,6 +8,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make cxx := \ auto-handle.cxx \ +database.cxx \ error.cxx \ exceptions.cxx diff --git a/odb/mssql/mssql-fwd.hxx b/odb/mssql/mssql-fwd.hxx index 403e2a8..c837cd5 100644 --- a/odb/mssql/mssql-fwd.hxx +++ b/odb/mssql/mssql-fwd.hxx @@ -21,6 +21,7 @@ typedef short SQLSMALLINT; typedef unsigned short SQLUSMALLINT; typedef void* SQLHANDLE; +typedef SQLHANDLE SQLHENV; #include -- cgit v1.1