diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-09-07 13:58:47 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-09-07 13:58:47 +0200 |
commit | a15eb05821b1ae5b40b24bfa3efa0c70a095b3e9 (patch) | |
tree | f8881fea63008cb48c068851f39cf84375593826 | |
parent | 95143f4028efb896348008dc4cf4ce2d5925f720 (diff) |
Add support for passing database name as std::wstring on Windows
-rw-r--r-- | odb/sqlite/database.cxx | 63 | ||||
-rw-r--r-- | odb/sqlite/database.hxx | 9 |
2 files changed, 72 insertions, 0 deletions
diff --git a/odb/sqlite/database.cxx b/odb/sqlite/database.cxx index bf2d8dd..0f1138f 100644 --- a/odb/sqlite/database.cxx +++ b/odb/sqlite/database.cxx @@ -2,6 +2,13 @@ // copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file +#ifdef _WIN32 +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include <windows.h> // WideCharToMultiByte +#endif + #include <sstream> #include <odb/sqlite/database.hxx> @@ -43,6 +50,62 @@ namespace odb factory_->database (*this); } +#ifdef _WIN32 + database:: + database (const wstring& name, + int flags, + bool foreign_keys, + const string& vfs, + transfer_ptr<connection_factory> factory) + : flags_ (flags), + foreign_keys_ (foreign_keys), + vfs_ (vfs), + factory_ (factory.transfer ()) + { + // Convert UTF-16 name to UTF-8 using the WideCharToMultiByte() Win32 + // function. + // + int n ( + WideCharToMultiByte ( + CP_UTF8, + 0, + name.c_str (), + static_cast<int> (name.size ()), + 0, + 0, + 0, + 0)); + + if (n == 0) + throw database_exception ( + SQLITE_CANTOPEN, SQLITE_CANTOPEN, "unable to open database file"); + + // This string is not shared so we are going to modify the underlying + // buffer directly. + // + name_.resize (static_cast<string::size_type> (n)); + + n = WideCharToMultiByte ( + CP_UTF8, + 0, + name.c_str (), + static_cast<int> (name.size ()), + const_cast<char*> (name_.c_str ()), + n, + 0, + 0); + + if (n == 0) + throw database_exception ( + SQLITE_CANTOPEN, SQLITE_CANTOPEN, "unable to open database file"); + + if (!factory_) + factory_.reset (new connection_pool_factory ()); + + factory_->database (*this); + } +#endif + database:: database (int& argc, char* argv[], diff --git a/odb/sqlite/database.hxx b/odb/sqlite/database.hxx index 042fb29..bbb01f6 100644 --- a/odb/sqlite/database.hxx +++ b/odb/sqlite/database.hxx @@ -42,6 +42,15 @@ namespace odb details::transfer_ptr<connection_factory> = details::transfer_ptr<connection_factory> ()); +#ifdef _WIN32 + database (const std::wstring& name, + int flags = SQLITE_OPEN_READWRITE, + bool foreign_keys = true, + const std::string& vfs = "", + details::transfer_ptr<connection_factory> = + details::transfer_ptr<connection_factory> ()); +#endif + // Extract the database parameters from the command line. The // following options are recognized: // |