Add beast_sqdb module

This commit is contained in:
Vinnie Falco committed 2013-07-24 11:47:42 -07:00
1 parent ed8d9b1ed3
commit 76e0d4c770
35 files changed
+4983

No files matched your search

+120
View File
@@ -0,0 +1,120 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Portions based on SOCI - The C++ Database Access Library:
SOCI: http://soci.sourceforge.net/
This file incorporates work covered by the following copyright
and permission notice:
Copyright (C) 2004 Maciej Sobczak, Stephen Hutton, Mateusz Loskot,
Pawel Aleksander Fedorynski, David Courtney, Rafal Bobrowski,
Julian Taylor, Henning Basold, Ilia Barahovski, Denis Arnaud,
Daniel Lidström, Matthieu Kermagoret, Artyom Beilis, Cory Bennett,
Chris Weed, Michael Davidsaver, Jakub Stachowski, Alex Ott, Rainer Bauer,
Martin Muenstermann, Philip Pemberton, Eli Green, Frederic Chateau,
Artyom Tonkikh, Roger Orr, Robert Massaioli, Sergey Nikulov,
Shridhar Daithankar, Sören Meyer-Eppler, Mario Valesco.
Boost Software License - Version 1.0, August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
//==============================================================================
namespace sqdb
{
blob::blob()
: m_blob(0)
{
}
blob::~blob()
{
close();
}
Error blob::select(session& s,
char const* zTable,
char const* zColumn,
rowid id,
bool readAndWrite) noexcept
{
close();
m_session = &s;
int result = sqlite3_blob_open(s.get_connection(),
"main",
zTable,
zColumn,
id,
readAndWrite ? 1 : 0,
&m_blob);
return detail::sqliteError(__FILE__, __LINE__, result);
}
std::size_t blob::get_len()
{
return sqlite3_blob_bytes(m_blob);
}
Error blob::read(std::size_t offset, void* buf, std::size_t toRead)
{
return detail::sqliteError(__FILE__, __LINE__,
sqlite3_blob_read(m_blob, static_cast<char*>(buf), toRead, offset));
}
Error blob::write(std::size_t offset, void const* buf, std::size_t toWrite)
{
return detail::sqliteError(__FILE__, __LINE__,
sqlite3_blob_write(m_blob, buf, toWrite, offset));
}
void blob::close()
{
if (m_blob)
{
sqlite3_blob_close(m_blob);
m_blob = 0;
}
}
}
+232
View File
@@ -0,0 +1,232 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Portions based on SOCI - The C++ Database Access Library:
SOCI: http://soci.sourceforge.net/
This file incorporates work covered by the following copyright
and permission notice:
Copyright (C) 2004 Maciej Sobczak, Stephen Hutton, Mateusz Loskot,
Pawel Aleksander Fedorynski, David Courtney, Rafal Bobrowski,
Julian Taylor, Henning Basold, Ilia Barahovski, Denis Arnaud,
Daniel Lidström, Matthieu Kermagoret, Artyom Beilis, Cory Bennett,
Chris Weed, Michael Davidsaver, Jakub Stachowski, Alex Ott, Rainer Bauer,
Martin Muenstermann, Philip Pemberton, Eli Green, Frederic Chateau,
Artyom Tonkikh, Roger Orr, Robert Massaioli, Sergey Nikulov,
Shridhar Daithankar, Sören Meyer-Eppler, Mario Valesco.
Boost Software License - Version 1.0, August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
//==============================================================================
namespace sqdb
{
namespace detail
{
const Error sqliteError(const char* sourceFileName,
int lineNumber,
int sqliteErrorCode)
{
/*
jassert (sqliteErrorCode != SQLITE_ROW &&
sqliteErrorCode != SQLITE_DONE);
*/
Error e;
String s;
Error::Code c;
switch (sqliteErrorCode)
{
// should never get these two here but include them just in case
case SQLITE_ROW:
case SQLITE_DONE:
case SQLITE_OK:
// this is fine, and lets us avoid having to explicitly
// check for SQLITE_OK and just deal in terms of the Error object.
c = Error::success;
break;
case SQLITE_ERROR:
s = TRANS("an sqlite error or missing database was encountered");
c = Error::general;
break;
case SQLITE_INTERNAL:
s = TRANS("sqlite encountered an internal logic error");
c = Error::unexpected;
break;
case SQLITE_PERM:
s = TRANS("sqlite was denied file access permission");
c = Error::fileNoPerm;
break;
case SQLITE_ABORT:
s = TRANS("the sqlite operation was canceled due to a callback");
c = Error::canceled;
break;
case SQLITE_BUSY:
s = TRANS("the sqlite database file is locked");
c = Error::fileInUse;
break;
case SQLITE_LOCKED:
s = TRANS("the sqlite database table was locked");
c = Error::fileInUse;
break;
case SQLITE_NOMEM:
s = TRANS("sqlite ran out of memory");
c = Error::noMemory;
break;
case SQLITE_READONLY:
s = TRANS("sqlite tried to write to a read-only database");
c = Error::fileNoPerm;
break;
case SQLITE_INTERRUPT:
s = TRANS("the sqlite operation was interrupted");
c = Error::canceled;
break;
case SQLITE_IOERR:
s = TRANS("sqlite encountered a device I/O error");
c = Error::fileIOError;
break;
case SQLITE_CORRUPT:
s = TRANS("the sqlite database is corrupt");
c = Error::invalidData;
break;
case SQLITE_FULL:
s = TRANS("the sqlite database is full");
c = Error::fileNoSpace;
break;
case SQLITE_CANTOPEN:
s = TRANS("the sqlite database could not be opened");
c = Error::fileNotFound;
break;
case SQLITE_PROTOCOL:
s = TRANS("sqlite encountered a lock protocol error");
c = Error::badParameter;
break;
case SQLITE_EMPTY:
s = TRANS("the sqlite database is empty");
c = Error::noMoreData;
break;
case SQLITE_SCHEMA:
s = TRANS("the sqlite database scheme was changed");
c = Error::invalidData;
break;
case SQLITE_TOOBIG:
s = TRANS("the sqlite string or blob was too large");
c = Error::fileNoSpace;
break;
case SQLITE_CONSTRAINT:
s = TRANS("the sqlite operation was aborted due to a constraint violation");
c = Error::badParameter;
break;
case SQLITE_MISMATCH:
s = TRANS("the sqlite data was mismatched");
c = Error::badParameter;
break;
case SQLITE_MISUSE:
s = TRANS("the sqlite library parameter was invalid");
c = Error::badParameter;
break;
case SQLITE_NOLFS:
s = TRANS("the sqlite platform feature is unavailable");
c = Error::badParameter;
break;
case SQLITE_AUTH:
s = TRANS("sqlite authorization was denied");
c = Error::fileNoPerm;
break;
case SQLITE_FORMAT:
s = TRANS("the auxiliary sqlite database has an invalid format");
c = Error::invalidData;
break;
case SQLITE_RANGE:
s = TRANS("the sqlite parameter was invalid");
c = Error::badParameter;
break;
case SQLITE_NOTADB:
s = TRANS("the file is not a sqlite database");
c = Error::invalidData;
break;
default:
s << TRANS("an unknown sqlite3 error code #")
<< String(sqliteErrorCode)
<< TRANS("was returned");
c = Error::general;
break;
}
if (c != Error::success)
e.fail(sourceFileName, lineNumber, s, c);
return e;
}
}
}
+301
View File
@@ -0,0 +1,301 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Portions based on SOCI - The C++ Database Access Library:
SOCI: http://soci.sourceforge.net/
This file incorporates work covered by the following copyright
and permission notice:
Copyright (C) 2004 Maciej Sobczak, Stephen Hutton, Mateusz Loskot,
Pawel Aleksander Fedorynski, David Courtney, Rafal Bobrowski,
Julian Taylor, Henning Basold, Ilia Barahovski, Denis Arnaud,
Daniel Lidström, Matthieu Kermagoret, Artyom Beilis, Cory Bennett,
Chris Weed, Michael Davidsaver, Jakub Stachowski, Alex Ott, Rainer Bauer,
Martin Muenstermann, Philip Pemberton, Eli Green, Frederic Chateau,
Artyom Tonkikh, Roger Orr, Robert Massaioli, Sergey Nikulov,
Shridhar Daithankar, Sören Meyer-Eppler, Mario Valesco.
Boost Software License - Version 1.0, August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
//==============================================================================
namespace sqdb
{
namespace detail
{
namespace
{
template <typename T>
inline T& as(void* data)
{
return *static_cast<T*>(data);
}
template <typename T>
static void integer_into(void* data, sqlite3_int64 v)
{
*(static_cast<T*>(data)) = T(v);
}
}
standard_into_type::~standard_into_type()
{
}
void standard_into_type::bind(statement_imp& st, int& iCol)
{
m_st = &st;
m_iCol = iCol++;
}
void standard_into_type::do_into()
{
int colType = sqlite3_column_type(m_st->m_stmt, m_iCol);
if (colType == SQLITE_NULL)
{
// null encountered with no indicator
if (!m_ind)
Throw(Error().fail(__FILE__, __LINE__));
*m_ind = i_null;
}
else
{
if (m_ind)
*m_ind = i_ok;
switch (colType)
{
case SQLITE_INTEGER:
{
sqlite3_int64 v = sqlite3_column_int64(m_st->m_stmt, m_iCol);
switch (m_type)
{
case x_bool:
as<bool>(m_data) = v != 0;
break;
case x_char:
integer_into<char>(m_data, v);
break;
case x_short:
integer_into<short>(m_data, v);
break;
case x_int:
integer_into<int>(m_data, v);
break;
case x_long:
integer_into<long>(m_data, v);
break;
case x_int64:
integer_into<int64>(m_data, v);
break;
case x_uchar:
integer_into<unsigned char>(m_data, v);
break;
case x_ushort:
integer_into<unsigned short>(m_data, v);
break;
case x_uint:
integer_into<unsigned int>(m_data, v);
break;
case x_ulong:
integer_into<unsigned long>(m_data, v);
break;
case x_uint64:
integer_into<uint64>(m_data, v);
break;
case x_beastString:
as <String> (m_data) = String(v);
break;
default:
Throw(Error().fail(__FILE__, __LINE__));
}
}
break;
case SQLITE_FLOAT:
{
double v = sqlite3_column_double(m_st->m_stmt, m_iCol);
switch (m_type)
{
case x_float:
as<float>(m_data) = static_cast<float>(v);
break;
case x_double:
as<double>(m_data) = v;
break;
case x_beastString:
as <String> (m_data) = String(v);
break;
default:
Throw(Error().fail(__FILE__, __LINE__));
};
}
break;
case SQLITE_TEXT:
{
switch (m_type)
{
case x_stdstring:
{
// excludes terminator
int bytes = sqlite3_column_bytes(m_st->m_stmt, m_iCol);
unsigned char const* v = sqlite3_column_text(m_st->m_stmt, m_iCol);
std::string& result = as<std::string>(m_data);
result.assign(reinterpret_cast<char const*>(v), bytes);
}
break;
case x_stdwstring:
Throw(Error().fail(__FILE__, __LINE__));
break;
case x_beastString:
{
// excludes terminator
int bytes = sqlite3_column_bytes(m_st->m_stmt, m_iCol);
const CharPointer_UTF8::CharType* c = reinterpret_cast
<const CharPointer_UTF8::CharType*>
(sqlite3_column_text(m_st->m_stmt, m_iCol));
String& s = as <String> (m_data);
s = String(CharPointer_UTF8(c), CharPointer_UTF8(c + bytes));
}
break;
default:
{
sqlite3_int64 v = sqlite3_column_int64(m_st->m_stmt, m_iCol);
switch (m_type)
{
case x_bool:
as<bool>(m_data) = v != 0;
break;
case x_char:
integer_into<char>(m_data, v);
break;
case x_short:
integer_into<short>(m_data, v);
break;
case x_int:
integer_into<int>(m_data, v);
break;
case x_long:
integer_into<long>(m_data, v);
break;
case x_int64:
integer_into<int64>(m_data, v);
break;
case x_uchar:
integer_into<unsigned char>(m_data, v);
break;
case x_ushort:
integer_into<unsigned short>(m_data, v);
break;
case x_uint:
integer_into<unsigned int>(m_data, v);
break;
case x_ulong:
integer_into<unsigned long>(m_data, v);
break;
case x_uint64:
integer_into<uint64>(m_data, v);
break;
default:
Throw(Error().fail(__FILE__, __LINE__));
}
}
break;
};
}
break;
case SQLITE_BLOB:
Throw(Error().fail(__FILE__, __LINE__));
default:
Throw(Error().fail(__FILE__, __LINE__));
};
}
convert_from_base();
}
}
}
@@ -0,0 +1,120 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Portions based on SOCI - The C++ Database Access Library:
SOCI: http://soci.sourceforge.net/
This file incorporates work covered by the following copyright
and permission notice:
Copyright (C) 2004 Maciej Sobczak, Stephen Hutton, Mateusz Loskot,
Pawel Aleksander Fedorynski, David Courtney, Rafal Bobrowski,
Julian Taylor, Henning Basold, Ilia Barahovski, Denis Arnaud,
Daniel Lidström, Matthieu Kermagoret, Artyom Beilis, Cory Bennett,
Chris Weed, Michael Davidsaver, Jakub Stachowski, Alex Ott, Rainer Bauer,
Martin Muenstermann, Philip Pemberton, Eli Green, Frederic Chateau,
Artyom Tonkikh, Roger Orr, Robert Massaioli, Sergey Nikulov,
Shridhar Daithankar, Sören Meyer-Eppler, Mario Valesco.
Boost Software License - Version 1.0, August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
//==============================================================================
namespace sqdb
{
namespace detail
{
once_type::once_type(const once_type& other)
: m_session(other.m_session)
, m_error(other.m_error)
{
}
once_temp_type::once_temp_type(session& s, Error& error)
: m_rcst(new ref_counted_statement(s, error))
{
// new query
s.get_query_stream().str("");
}
once_temp_type::once_temp_type(once_temp_type const& o)
: m_rcst(o.m_rcst)
{
m_rcst->addref();
}
once_temp_type& once_temp_type::operator= (once_temp_type const& o)
{
o.m_rcst->addref();
m_rcst->release();
m_rcst = o.m_rcst;
return *this;
}
once_temp_type::~once_temp_type()
{
m_rcst->release();
}
once_temp_type& once_temp_type::operator, (into_type_ptr const& i)
{
m_rcst->exchange(i);
return *this;
}
once_temp_type& once_temp_type::operator, (use_type_ptr const& u)
{
m_rcst->exchange(u);
return *this;
}
//------------------------------------------------------------------------------
once_type::once_type(session* s, Error& error)
: m_session(*s)
, m_error(error)
{
}
}
}
@@ -0,0 +1,106 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Portions based on SOCI - The C++ Database Access Library:
SOCI: http://soci.sourceforge.net/
This file incorporates work covered by the following copyright
and permission notice:
Copyright (C) 2004 Maciej Sobczak, Stephen Hutton, Mateusz Loskot,
Pawel Aleksander Fedorynski, David Courtney, Rafal Bobrowski,
Julian Taylor, Henning Basold, Ilia Barahovski, Denis Arnaud,
Daniel Lidström, Matthieu Kermagoret, Artyom Beilis, Cory Bennett,
Chris Weed, Michael Davidsaver, Jakub Stachowski, Alex Ott, Rainer Bauer,
Martin Muenstermann, Philip Pemberton, Eli Green, Frederic Chateau,
Artyom Tonkikh, Roger Orr, Robert Massaioli, Sergey Nikulov,
Shridhar Daithankar, Sören Meyer-Eppler, Mario Valesco.
Boost Software License - Version 1.0, August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
//==============================================================================
namespace sqdb
{
namespace detail
{
prepare_temp_type::prepare_temp_type(session& s)
: m_rcpi(new ref_counted_prepare_info(s))
{
// new query
s.get_query_stream().str("");
}
prepare_temp_type::prepare_temp_type(prepare_temp_type const& o)
: m_rcpi(o.m_rcpi)
{
m_rcpi->addref();
}
prepare_temp_type& prepare_temp_type::operator=(prepare_temp_type const& o)
{
o.m_rcpi->addref();
m_rcpi->release();
m_rcpi = o.m_rcpi;
return *this;
}
prepare_temp_type::~prepare_temp_type()
{
m_rcpi->release();
}
prepare_temp_type& prepare_temp_type::operator, (into_type_ptr const& i)
{
m_rcpi->exchange(i);
return *this;
}
prepare_temp_type& prepare_temp_type::operator, (use_type_ptr const& u)
{
m_rcpi->exchange(u);
return *this;
}
}
}
@@ -0,0 +1,105 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Portions based on SOCI - The C++ Database Access Library:
SOCI: http://soci.sourceforge.net/
This file incorporates work covered by the following copyright
and permission notice:
Copyright (C) 2004 Maciej Sobczak, Stephen Hutton, Mateusz Loskot,
Pawel Aleksander Fedorynski, David Courtney, Rafal Bobrowski,
Julian Taylor, Henning Basold, Ilia Barahovski, Denis Arnaud,
Daniel Lidström, Matthieu Kermagoret, Artyom Beilis, Cory Bennett,
Chris Weed, Michael Davidsaver, Jakub Stachowski, Alex Ott, Rainer Bauer,
Martin Muenstermann, Philip Pemberton, Eli Green, Frederic Chateau,
Artyom Tonkikh, Roger Orr, Robert Massaioli, Sergey Nikulov,
Shridhar Daithankar, Sören Meyer-Eppler, Mario Valesco.
Boost Software License - Version 1.0, August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
//==============================================================================
namespace sqdb
{
namespace detail
{
ref_counted_prepare_info::ref_counted_prepare_info(session& s)
: ref_counted_statement_base(s)
{
}
void ref_counted_prepare_info::exchange(into_type_ptr const& i)
{
m_intos.push_back(i.get());
i.release();
}
void ref_counted_prepare_info::exchange(use_type_ptr const& u)
{
m_uses.push_back(u.get());
u.release();
}
void ref_counted_prepare_info::final_action()
{
for (std::size_t i = m_intos.size(); i > 0; --i)
{
delete m_intos[i - 1];
m_intos.resize(i - 1);
}
for (std::size_t i = m_uses.size(); i > 0; --i)
{
delete m_uses[i - 1];
m_uses.resize(i - 1);
}
}
std::string ref_counted_prepare_info::get_query() const
{
return m_session.get_query_stream().str();
}
}
}
@@ -0,0 +1,127 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Portions based on SOCI - The C++ Database Access Library:
SOCI: http://soci.sourceforge.net/
This file incorporates work covered by the following copyright
and permission notice:
Copyright (C) 2004 Maciej Sobczak, Stephen Hutton, Mateusz Loskot,
Pawel Aleksander Fedorynski, David Courtney, Rafal Bobrowski,
Julian Taylor, Henning Basold, Ilia Barahovski, Denis Arnaud,
Daniel Lidström, Matthieu Kermagoret, Artyom Beilis, Cory Bennett,
Chris Weed, Michael Davidsaver, Jakub Stachowski, Alex Ott, Rainer Bauer,
Martin Muenstermann, Philip Pemberton, Eli Green, Frederic Chateau,
Artyom Tonkikh, Roger Orr, Robert Massaioli, Sergey Nikulov,
Shridhar Daithankar, Sören Meyer-Eppler, Mario Valesco.
Boost Software License - Version 1.0, August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
//==============================================================================
namespace sqdb
{
namespace detail
{
ref_counted_statement_base::ref_counted_statement_base(session& s)
: m_refs(1)
, m_session(s)
{
}
ref_counted_statement_base ::~ref_counted_statement_base()
{
}
void ref_counted_statement_base::addref()
{
++m_refs;
}
void ref_counted_statement_base::release()
{
if (--m_refs == 0)
{
final_action();
delete this;
}
}
std::ostringstream& ref_counted_statement_base::get_query_stream()
{
return m_session.get_query_stream();
}
//------------------------------------------------------------------------------
ref_counted_statement::ref_counted_statement(session& s, Error& error)
: ref_counted_statement_base(s)
, m_error(error)
, m_st(s)
{
}
void ref_counted_statement::exchange(into_type_ptr const& i)
{
m_st.exchange(i);
}
void ref_counted_statement::exchange(use_type_ptr const& u)
{
m_st.exchange(u);
}
void ref_counted_statement::final_action()
{
m_st.prepare(m_session.get_query_stream().str(), false);
Error error;
m_st.execute_and_fetch(error);
m_error = error;
m_st.clean_up();
}
}
}
+397
View File
@@ -0,0 +1,397 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Portions based on SOCI - The C++ Database Access Library:
SOCI: http://soci.sourceforge.net/
This file incorporates work covered by the following copyright
and permission notice:
Copyright (C) 2004 Maciej Sobczak, Stephen Hutton, Mateusz Loskot,
Pawel Aleksander Fedorynski, David Courtney, Rafal Bobrowski,
Julian Taylor, Henning Basold, Ilia Barahovski, Denis Arnaud,
Daniel Lidström, Matthieu Kermagoret, Artyom Beilis, Cory Bennett,
Chris Weed, Michael Davidsaver, Jakub Stachowski, Alex Ott, Rainer Bauer,
Martin Muenstermann, Philip Pemberton, Eli Green, Frederic Chateau,
Artyom Tonkikh, Roger Orr, Robert Massaioli, Sergey Nikulov,
Shridhar Daithankar, Sören Meyer-Eppler, Mario Valesco.
Boost Software License - Version 1.0, August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
//==============================================================================
namespace sqdb
{
class session::Sqlite3 : public SharedSingleton <Sqlite3>
{
private:
friend class SharedSingleton <Sqlite3>;
Sqlite3() : SharedSingleton <Sqlite3> (SingletonLifetime::persistAfterCreation)
{
int threadSafe = sqlite3_threadsafe();
if (threadSafe == 0)
Throw(Error().fail(__FILE__, __LINE__, Error::assertFailed));
int result = sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
if (result != SQLITE_OK)
Throw(Error().fail(__FILE__, __LINE__, Error::assertFailed));
sqlite3_initialize();
}
~Sqlite3()
{
sqlite3_shutdown();
}
static Sqlite3* createInstance()
{
return new Sqlite3;
}
};
//------------------------------------------------------------------------------
session::session()
: prepare(this)
, m_instance(Sqlite3::getInstance())
, m_bInTransaction(false)
, m_connection(nullptr)
{
}
session::session(const session& deferredClone)
: prepare(this)
, m_instance(Sqlite3::getInstance())
, m_bInTransaction(false)
, m_connection(nullptr)
, m_fileName(deferredClone.m_fileName)
, m_connectString(deferredClone.m_connectString)
{
// shouldn't be needed since deferredClone did it
//Sqlite::initialize();
}
session::~session()
{
close();
}
Error session::clone()
{
if (m_connection)
Throw(Error().fail(__FILE__, __LINE__)); // already open
return open(m_fileName, m_connectString);
}
/*
static int infiniteBusyHandler (void* data, int tries)
{
boost::this_thread::sleep (boost::posix_time::seconds(1));
return 1; // try again
}
*/
Error session::open(String fileName, std::string options)
{
Error err;
// can't open twice
if (m_connection)
Throw(err.fail(__FILE__, __LINE__, Error::fileInUse));
int mode = 0;
int flags = 0;
int timeout = 0;
std::stringstream ssconn(options);
while (!err && !ssconn.eof() && ssconn.str().find('=') >= 0)
{
std::string key, val;
std::getline(ssconn, key, '=');
std::getline(ssconn, val, '|');
if ("timeout" == key)
{
if ("infinite" == val)
{
//timeout = -1;
timeout = 0x7fffffff;
}
else
{
std::istringstream converter(val);
converter >> timeout;
if (timeout < 1)
timeout = 1;
}
}
else if ("mode" == key)
{
if (!(mode & (SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)))
{
if ("read" == val)
{
mode = SQLITE_OPEN_READONLY;
}
else if ("write" == val)
{
mode = SQLITE_OPEN_READWRITE;
}
else if ("create" == val)
{
mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
}
else
{
Throw(err.fail(__FILE__, __LINE__, Error::badParameter));
}
}
else
{
// duplicate
Throw(err.fail(__FILE__, __LINE__, Error::badParameter));
}
}
/* Most native SQLite libraries don't have these experimental features.
*/
#if ! VF_USE_NATIVE_SQLITE
else if ("cache" == key)
{
if (!(flags & (SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_PRIVATECACHE)))
{
if ("shared" == val)
{
flags |= SQLITE_OPEN_SHAREDCACHE;
}
else if ("private" == val)
{
flags |= SQLITE_OPEN_PRIVATECACHE;
}
else
{
Throw(err.fail(__FILE__, __LINE__, Error::badParameter));
}
}
else
{
// duplicate
Throw(err.fail(__FILE__, __LINE__, Error::badParameter));
}
}
#endif
else if ("threads" == key)
{
if (!(flags & (SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_FULLMUTEX)))
{
if ("single" == val)
{
flags |= SQLITE_OPEN_FULLMUTEX;
}
else if ("multi" == val)
{
flags |= SQLITE_OPEN_NOMUTEX;
}
else
{
Throw(err.fail(__FILE__, __LINE__, Error::badParameter));
}
}
else
{
// duplicate
Throw(err.fail(__FILE__, __LINE__, Error::badParameter));
}
}
else
{
// unknown option
Throw(err.fail(__FILE__, __LINE__, Error::badParameter));
}
}
if (!err)
{
if (! mode)
mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
flags |= mode;
err = detail::sqliteError(__FILE__, __LINE__,
sqlite3_open_v2(fileName.toUTF8(), &m_connection, flags, 0));
if (!err)
{
if (timeout > 0)
{
err = detail::sqliteError(__FILE__, __LINE__,
sqlite3_busy_timeout(m_connection, timeout));
}
/*
else
{
err = detail::sqliteError (__FILE__, __LINE__,
sqlite3_busy_handler(m_connection, infiniteBusyHandler, 0));
}
*/
}
if (!err)
{
m_fileName = fileName;
m_connectString = options;
}
if (err)
{
close();
}
}
return err;
}
void session::close()
{
if (m_connection)
{
sqlite3_close(m_connection);
m_connection = 0;
m_fileName = String::empty;
m_connectString = "";
}
}
void session::begin()
{
bassert(!m_bInTransaction);
m_bInTransaction = true;
//Error error = hard_exec("BEGIN EXCLUSIVE");
Error error = hard_exec("BEGIN");
if (error)
Throw(error);
}
Error session::commit()
{
bassert(m_bInTransaction);
m_bInTransaction = false;
return hard_exec("COMMIT");
}
void session::rollback()
{
bassert(m_bInTransaction);
m_bInTransaction = false;
Error error = hard_exec("ROLLBACK");
if (error)
Throw(error);
}
detail::once_type session::once(Error& error)
{
return detail::once_type(this, error);
}
rowid session::last_insert_rowid()
{
return sqlite3_last_insert_rowid(m_connection);
}
std::ostringstream& session::get_query_stream()
{
return m_query_stream;
}
void session::log_query(std::string const& query)
{
// TODO
}
void session::set_got_data(bool bGotData)
{
m_bGotData = bGotData;
}
bool session::got_data() const
{
return m_bGotData;
}
Error session::hard_exec(std::string const& query)
{
Error error;
sqlite3_stmt* stmt;
char const* tail = 0;
int result = sqlite3_prepare_v2(
m_connection,
query.c_str(),
static_cast<int>(query.size()),
&stmt,
&tail);
if (result == SQLITE_OK)
{
result = sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
if (result != SQLITE_DONE)
error = detail::sqliteError(__FILE__, __LINE__, result);
return error;
}
}
+139
View File
@@ -0,0 +1,139 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Portions based on SOCI - The C++ Database Access Library:
SOCI: http://soci.sourceforge.net/
This file incorporates work covered by the following copyright
and permission notice:
Copyright (C) 2004 Maciej Sobczak, Stephen Hutton, Mateusz Loskot,
Pawel Aleksander Fedorynski, David Courtney, Rafal Bobrowski,
Julian Taylor, Henning Basold, Ilia Barahovski, Denis Arnaud,
Daniel Lidström, Matthieu Kermagoret, Artyom Beilis, Cory Bennett,
Chris Weed, Michael Davidsaver, Jakub Stachowski, Alex Ott, Rainer Bauer,
Martin Muenstermann, Philip Pemberton, Eli Green, Frederic Chateau,
Artyom Tonkikh, Roger Orr, Robert Massaioli, Sergey Nikulov,
Shridhar Daithankar, Sören Meyer-Eppler, Mario Valesco.
Boost Software License - Version 1.0, August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
//==============================================================================
namespace sqdb
{
statement::statement(session& s)
: m_imp(new detail::statement_imp(s))
{
}
statement::statement(detail::prepare_temp_type const& prep)
: m_imp(new detail::statement_imp(prep))
{
}
statement::~statement()
{
}
statement::statement(statement const& o)
: m_imp(o.m_imp)
{
}
void statement::operator= (statement const& o)
{
m_imp = o.m_imp;
}
void statement::exchange(detail::into_type_ptr const& i)
{
m_imp->exchange(i);
}
void statement::exchange(detail::use_type_ptr const& u)
{
m_imp->exchange(u);
}
void statement::prepare(std::string const& query, bool bRepeatable)
{
m_imp->prepare(query, bRepeatable);
}
Error statement::execute()
{
return m_imp->execute();
}
bool statement::execute_and_fetch(Error& error)
{
bool gotData = false;
error = m_imp->execute();
if (!error)
gotData = fetch(error);
return gotData;
}
bool statement::fetch(Error& error)
{
return m_imp->fetch(error);
}
bool statement::got_data() const
{
return m_imp->got_data();
}
rowid statement::last_insert_rowid()
{
return m_imp->last_insert_rowid();
}
void statement::clean_up()
{
m_imp->clean_up();
}
}
+311
View File
@@ -0,0 +1,311 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Portions based on SOCI - The C++ Database Access Library:
SOCI: http://soci.sourceforge.net/
This file incorporates work covered by the following copyright
and permission notice:
Copyright (C) 2004 Maciej Sobczak, Stephen Hutton, Mateusz Loskot,
Pawel Aleksander Fedorynski, David Courtney, Rafal Bobrowski,
Julian Taylor, Henning Basold, Ilia Barahovski, Denis Arnaud,
Daniel Lidström, Matthieu Kermagoret, Artyom Beilis, Cory Bennett,
Chris Weed, Michael Davidsaver, Jakub Stachowski, Alex Ott, Rainer Bauer,
Martin Muenstermann, Philip Pemberton, Eli Green, Frederic Chateau,
Artyom Tonkikh, Roger Orr, Robert Massaioli, Sergey Nikulov,
Shridhar Daithankar, Sören Meyer-Eppler, Mario Valesco.
Boost Software License - Version 1.0, August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
//==============================================================================
namespace sqdb
{
namespace detail
{
/*
Running a new prepared statement:
sqlite3_prepare() // create prepared statement
sqlite3_bind() // define input variables
sqlite3_step() // retrieve next row
sqlite3_column() // extract output value
sqlite3_finalize() // destroy prepared statement
Re-using an existing prepared statement
sqlite3_reset() // reset the statement (input bindings are not cleared)
sqlite3_clear_bindings() // set all input variables to NULL
AND/OR
sqlite3_bind() // define input variables
sqlite3_step() // retrieve next row
sqlite3_column() // extract output value
*/
statement_imp::statement_imp(session& s)
: m_session(s)
, m_stmt(0)
, m_bReady(false)
, m_bGotData(false)
, m_last_insert_rowid(0)
{
}
statement_imp::statement_imp(prepare_temp_type const& prep)
: m_session(prep.get_prepare_info().m_session)
, m_stmt(0)
, m_bReady(false)
, m_bGotData(false)
{
ref_counted_prepare_info& rcpi = prep.get_prepare_info();
m_intos.swap(rcpi.m_intos);
m_uses.swap(rcpi.m_uses);
prepare(rcpi.get_query());
}
statement_imp::~statement_imp()
{
clean_up();
}
void statement_imp::erase_bindings()
{
// delete intos
for (std::size_t i = m_intos.size(); i != 0; --i)
{
into_type_base* p = m_intos[i - 1];
delete p;
m_intos.resize(i - 1);
}
// delete uses
for (std::size_t i = m_uses.size(); i != 0; --i)
{
use_type_base* p = m_uses[i - 1];
p->clean_up();
delete p;
m_uses.resize(i - 1);
}
}
void statement_imp::exchange(detail::into_type_ptr const& i)
{
m_intos.push_back(i.get());
i.release();
}
void statement_imp::exchange(detail::use_type_ptr const& u)
{
m_uses.push_back(u.get());
u.release();
}
void statement_imp::clean_up()
{
erase_bindings();
release_resources();
}
void statement_imp::prepare(std::string const& query, bool bRepeatable)
{
m_query = query;
m_session.log_query(query);
m_last_insert_rowid = 0;
release_resources();
char const* tail = 0;
int result = sqlite3_prepare_v2(
m_session.get_connection(),
query.c_str(),
static_cast<int>(query.size()),
&m_stmt,
&tail);
if (result == SQLITE_OK)
{
m_bReady = true;
}
else
{
Throw(detail::sqliteError(__FILE__, __LINE__, result));
}
}
Error statement_imp::execute()
{
Error error;
if (!m_stmt)
Throw(Error().fail(__FILE__, __LINE__, Error::badParameter));
// ???
m_bGotData = false;
m_session.set_got_data(m_bGotData);
// binds
int iCol = 0;
for (intos_t::iterator iter = m_intos.begin(); iter != m_intos.end(); ++iter)
(*iter)->bind(*this, iCol);
int iParam = 1;
for (uses_t::iterator iter = m_uses.begin(); iter != m_uses.end(); ++iter)
(*iter)->bind(*this, iParam);
// reset
error = detail::sqliteError(__FILE__, __LINE__, sqlite3_reset(m_stmt));
if (!error)
{
// set input variables
do_uses();
m_bReady = true;
m_bFirstTime = true;
}
return error;
}
bool statement_imp::fetch(Error& error)
{
int result = sqlite3_step(m_stmt);
if (result == SQLITE_ROW ||
result == SQLITE_DONE)
{
if (m_bFirstTime)
{
m_last_insert_rowid = m_session.last_insert_rowid();
m_bFirstTime = false;
}
if (result == SQLITE_ROW)
{
m_bGotData = true;
m_session.set_got_data(m_bGotData);
do_intos();
}
else
{
m_bGotData = false;
m_session.set_got_data(m_bGotData);
if (result == SQLITE_DONE)
{
m_bReady = false;
}
}
}
else if (result != SQLITE_OK)
{
m_bGotData = false;
error = detail::sqliteError(__FILE__, __LINE__, result);
}
else
{
// should never get SQLITE_OK here
Throw(Error().fail(__FILE__, __LINE__, Error::assertFailed));
}
return m_bGotData;
}
bool statement_imp::got_data() const
{
return m_bGotData;
}
void statement_imp::do_intos()
{
for (intos_t::iterator iter = m_intos.begin(); iter != m_intos.end(); ++iter)
(*iter)->do_into();
}
void statement_imp::do_uses()
{
Error error = detail::sqliteError(__FILE__, __LINE__,
sqlite3_clear_bindings(m_stmt));
if (error)
Throw(error);
for (uses_t::iterator iter = m_uses.begin(); iter != m_uses.end(); ++iter)
(*iter)->do_use();
}
void statement_imp::post_use()
{
// reverse order
for (uses_t::reverse_iterator iter = m_uses.rbegin(); iter != m_uses.rend(); ++iter)
(*iter)->post_use();
}
void statement_imp::release_resources()
{
if (m_stmt)
{
sqlite3_finalize(m_stmt);
m_stmt = 0;
}
m_bReady = false;
m_bGotData = false;
}
rowid statement_imp::last_insert_rowid()
{
return m_last_insert_rowid;
}
}
}
+103
View File
@@ -0,0 +1,103 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Portions based on SOCI - The C++ Database Access Library:
SOCI: http://soci.sourceforge.net/
This file incorporates work covered by the following copyright
and permission notice:
Copyright (C) 2004 Maciej Sobczak, Stephen Hutton, Mateusz Loskot,
Pawel Aleksander Fedorynski, David Courtney, Rafal Bobrowski,
Julian Taylor, Henning Basold, Ilia Barahovski, Denis Arnaud,
Daniel Lidström, Matthieu Kermagoret, Artyom Beilis, Cory Bennett,
Chris Weed, Michael Davidsaver, Jakub Stachowski, Alex Ott, Rainer Bauer,
Martin Muenstermann, Philip Pemberton, Eli Green, Frederic Chateau,
Artyom Tonkikh, Roger Orr, Robert Massaioli, Sergey Nikulov,
Shridhar Daithankar, Sören Meyer-Eppler, Mario Valesco.
Boost Software License - Version 1.0, August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
//==============================================================================
namespace sqdb
{
transaction::transaction(session& s)
: m_session(s), m_bHandled(false)
{
m_session.begin();
}
transaction::~transaction()
{
if (!m_bHandled)
rollback();
}
Error transaction::commit()
{
Error error;
// already handled
if (m_bHandled)
Throw(Error().fail(__FILE__, __LINE__));
// if commit() throws, m_bHandled will remain false
// and the destructor will attempt a rollback.
m_bHandled = true;
error = m_session.commit();
return error;
}
void transaction::rollback()
{
// already handled
if (m_bHandled)
Throw(Error().fail(__FILE__, __LINE__));
m_session.rollback();
m_bHandled = true;
}
}
+224
View File
@@ -0,0 +1,224 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Portions based on SOCI - The C++ Database Access Library:
SOCI: http://soci.sourceforge.net/
This file incorporates work covered by the following copyright
and permission notice:
Copyright (C) 2004 Maciej Sobczak, Stephen Hutton, Mateusz Loskot,
Pawel Aleksander Fedorynski, David Courtney, Rafal Bobrowski,
Julian Taylor, Henning Basold, Ilia Barahovski, Denis Arnaud,
Daniel Lidström, Matthieu Kermagoret, Artyom Beilis, Cory Bennett,
Chris Weed, Michael Davidsaver, Jakub Stachowski, Alex Ott, Rainer Bauer,
Martin Muenstermann, Philip Pemberton, Eli Green, Frederic Chateau,
Artyom Tonkikh, Roger Orr, Robert Massaioli, Sergey Nikulov,
Shridhar Daithankar, Sören Meyer-Eppler, Mario Valesco.
Boost Software License - Version 1.0, August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
//==============================================================================
namespace sqdb
{
namespace detail
{
standard_use_type::~standard_use_type()
{
}
void standard_use_type::bind(statement_imp& st, int& iParam)
{
m_st = &st;
m_iParam = iParam++;
}
namespace
{
template<typename T>
inline T const& as(void const* v)
{
return *static_cast <T const*>(v);
}
template<typename T, typename L>
inline T const& as(void const* v)
{
T const& val = *static_cast <T const*>(v);
if (val > T(std::numeric_limits<L>::max()))
Throw(Error().fail(__FILE__, __LINE__));
return val;
}
}
void standard_use_type::do_use()
{
int result = SQLITE_OK;
convert_to_base();
switch (m_type)
{
case x_null:
result = sqlite3_bind_null(m_st->m_stmt, m_iParam);
break;
case x_bool:
result = sqlite3_bind_int(m_st->m_stmt, m_iParam, as <bool> (m_data) ? 1 : 0);
break;
case x_char:
result = sqlite3_bind_int(m_st->m_stmt, m_iParam, as <char> (m_data));
break;
case x_short:
result = sqlite3_bind_int(m_st->m_stmt, m_iParam, as <short> (m_data));
break;
case x_int:
result = sqlite3_bind_int(m_st->m_stmt, m_iParam, as <int> (m_data));
break;
case x_long:
result = sqlite3_bind_int(m_st->m_stmt, m_iParam, as <long> (m_data));
break;
case x_int64:
result = sqlite3_bind_int64(m_st->m_stmt, m_iParam, as <int64> (m_data));
break;
case x_uchar:
result = sqlite3_bind_int(m_st->m_stmt, m_iParam, as <unsigned char> (m_data));
break;
case x_ushort:
result = sqlite3_bind_int(m_st->m_stmt, m_iParam, as <unsigned short> (m_data));
break;
case x_uint:
result = sqlite3_bind_int64(m_st->m_stmt, m_iParam, as <unsigned int> (m_data));
break;
case x_ulong:
result = sqlite3_bind_int64(m_st->m_stmt, m_iParam, as <unsigned long> (m_data));
break;
case x_uint64:
result = sqlite3_bind_int64(m_st->m_stmt, m_iParam, as <sqlite3_uint64, sqlite3_int64> (m_data));
break;
case x_float:
result = sqlite3_bind_double(m_st->m_stmt, m_iParam, as <float> (m_data));
break;
case x_double:
result = sqlite3_bind_double(m_st->m_stmt, m_iParam, as <double> (m_data));
break;
case x_cstring:
result = sqlite3_bind_text(m_st->m_stmt, m_iParam, as <char*> (m_data), -1, SQLITE_STATIC);
break;
case x_cwstring:
result = sqlite3_bind_text16(m_st->m_stmt, m_iParam, as <wchar_t*> (m_data), -1, SQLITE_STATIC);
break;
case x_stdstring:
{
std::string const& s = as <std::string> (m_data);
result = sqlite3_bind_text(m_st->m_stmt,
m_iParam,
s.c_str(),
s.size() * sizeof(s[0]),
SQLITE_STATIC);
}
break;
case x_stdwstring:
{
std::wstring const& s = as <std::wstring> (m_data);
result = sqlite3_bind_text16(m_st->m_stmt,
m_iParam,
s.c_str(),
s.size() * sizeof(s[0]),
SQLITE_STATIC);
}
break;
case x_beastString:
{
String const& s = as <String> (m_data);
result = sqlite3_bind_text(m_st->m_stmt,
m_iParam,
s.toUTF8(),
-1,
SQLITE_STATIC);
}
break;
case x_stdtm:
case x_blob:
default:
Throw(Error().fail(__FILE__, __LINE__, Error::badParameter));
}
if (result != SQLITE_OK)
Throw(detail::sqliteError(__FILE__, __LINE__, result));
}
void standard_use_type::post_use()
{
convert_from_base();
}
void standard_use_type::clean_up()
{
}
}
}