From a15eb05821b1ae5b40b24bfa3efa0c70a095b3e9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 7 Sep 2012 13:58:47 +0200 Subject: Add support for passing database name as std::wstring on Windows --- odb/sqlite/database.cxx | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'odb/sqlite/database.cxx') 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 // WideCharToMultiByte +#endif + #include #include @@ -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 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 (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 (n)); + + n = WideCharToMultiByte ( + CP_UTF8, + 0, + name.c_str (), + static_cast (name.size ()), + const_cast (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[], -- cgit v1.1