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 +++++++++++++++++++++++++++++++++++++++++++++++++ odb/sqlite/database.hxx | 9 +++++++ 2 files changed, 72 insertions(+) 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[], 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 = details::transfer_ptr ()); +#ifdef _WIN32 + database (const std::wstring& name, + int flags = SQLITE_OPEN_READWRITE, + bool foreign_keys = true, + const std::string& vfs = "", + details::transfer_ptr = + details::transfer_ptr ()); +#endif + // Extract the database parameters from the command line. The // following options are recognized: // -- cgit v1.1