1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
// file : odb/sqlite/database.cxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license : GNU GPL v2; see accompanying LICENSE file
#ifdef _WIN32
# include <odb/details/win32/windows.hxx> // WideCharToMultiByte
#endif
#include <sqlite3.h>
#include <cassert>
#include <sstream>
#include <odb/sqlite/database.hxx>
#include <odb/sqlite/connection.hxx>
#include <odb/sqlite/connection-factory.hxx>
#include <odb/sqlite/statement.hxx>
#include <odb/sqlite/transaction.hxx>
#include <odb/sqlite/error.hxx>
#include <odb/sqlite/exceptions.hxx>
#include <odb/sqlite/details/options.hxx>
using namespace std;
namespace odb
{
namespace sqlite
{
using odb::details::transfer_ptr;
database::
~database ()
{
}
database::
database (const string& name,
int flags,
bool foreign_keys,
const string& vfs,
transfer_ptr<connection_factory> factory)
: odb::database (id_sqlite),
name_ (name),
flags_ (flags),
foreign_keys_ (foreign_keys),
vfs_ (vfs),
factory_ (factory.transfer ())
{
if (!factory_)
factory_.reset (new connection_pool_factory ());
factory_->database (*this);
}
#ifdef _WIN32
database::
database (const wstring& name,
int flags,
bool foreign_keys,
const string& vfs,
transfer_ptr<connection_factory> factory)
: odb::database (id_sqlite),
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[],
bool erase,
int flags,
bool foreign_keys,
const string& vfs,
transfer_ptr<connection_factory> factory)
: odb::database (id_sqlite),
flags_ (flags),
foreign_keys_ (foreign_keys),
vfs_ (vfs),
factory_ (factory.transfer ())
{
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);
name_ = ops.database ();
if (ops.create ())
flags_ |= SQLITE_OPEN_CREATE;
if (ops.read_only ())
flags_ = (flags_ & ~SQLITE_OPEN_READWRITE) | SQLITE_OPEN_READONLY;
}
catch (const cli::exception& e)
{
ostringstream ostr;
ostr << e;
throw cli_exception (ostr.str ());
}
if (!factory_)
factory_.reset (new connection_pool_factory ());
factory_->database (*this);
}
void database::
print_usage (std::ostream& os)
{
details::options::print_usage (os);
}
transaction_impl* database::
begin ()
{
return new transaction_impl (*this, transaction_impl::deferred);
}
transaction_impl* database::
begin_immediate ()
{
return new transaction_impl (*this, transaction_impl::immediate);
}
transaction_impl* database::
begin_exclusive ()
{
return new transaction_impl (*this, transaction_impl::exclusive);
}
odb::connection* database::
connection_ ()
{
connection_ptr c (factory_->connect ());
return c.release ();
}
const database::schema_version_info& database::
load_schema_version (const string& name) const
{
schema_version_info& svi (schema_version_map_[name]);
// Construct the SELECT statement text.
//
string text ("SELECT \"version\", \"migration\" FROM ");
if (!svi.version_table.empty ())
text += svi.version_table; // Already quoted.
else if (!schema_version_table_.empty ())
text += schema_version_table_; // Already quoted.
else
text += "\"schema_version\"";
text += " WHERE \"name\" = ?";
// Bind parameters and results.
//
size_t psize[1] = {name.size ()};
bind pbind[1] = {{bind::text,
const_cast<char*> (name.c_str ()),
&psize[0],
0, 0, 0}};
binding param (pbind, 1);
param.version++;
long long migration;
bool rnull[2];
bind rbind[2] = {{bind::integer, &svi.version, 0, 0, &rnull[0], 0},
{bind::integer, &migration, 0, 0, &rnull[1], 0}};
binding result (rbind, 2);
result.version++;
// If we are not in transaction, SQLite will start an implicit one
// which suits us just fine.
//
connection_ptr cp;
if (!transaction::has_current ())
cp = factory_->connect ();
sqlite::connection& c (
cp != 0 ? *cp : transaction::current ().connection ());
try
{
select_statement st (c,
text,
false, // Don't process.
false, // Don't optimize.
param,
result);
st.execute ();
auto_result ar (st);
switch (st.fetch ())
{
case select_statement::success:
{
svi.migration = migration != 0;
assert (st.fetch () == select_statement::no_data);
break;
}
case select_statement::no_data:
{
svi.version = 0; // No schema.
break;
}
case select_statement::truncated:
{
assert (false);
break;
}
}
}
catch (const database_exception& e)
{
// Try to detect the case where there is no version table. SQLite
// doesn't have an extended error code for this so we have to use
// the error text.
//
if (e.error () == SQLITE_ERROR &&
e.message ().compare (0, 14, "no such table:") == 0)
svi.version = 0; // No schema.
else
throw;
}
return svi;
}
}
}
|