111be35a1SLionel Sambuc // Copyright 2011 Google Inc.
211be35a1SLionel Sambuc // All rights reserved.
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
511be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
611be35a1SLionel Sambuc // met:
711be35a1SLionel Sambuc //
811be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
911be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
1011be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
1111be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
1211be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
1311be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
1411be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
1511be35a1SLionel Sambuc // without specific prior written permission.
1611be35a1SLionel Sambuc //
1711be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1811be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1911be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2011be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2111be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2211be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2311be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2411be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2511be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2611be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2711be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc
2911be35a1SLionel Sambuc #include "utils/sqlite/statement.hpp"
3011be35a1SLionel Sambuc
3111be35a1SLionel Sambuc extern "C" {
3211be35a1SLionel Sambuc #include <sqlite3.h>
3311be35a1SLionel Sambuc }
3411be35a1SLionel Sambuc
3511be35a1SLionel Sambuc #include <map>
3611be35a1SLionel Sambuc
3711be35a1SLionel Sambuc #include "utils/defs.hpp"
3811be35a1SLionel Sambuc #include "utils/format/macros.hpp"
3911be35a1SLionel Sambuc #include "utils/logging/macros.hpp"
4011be35a1SLionel Sambuc #include "utils/sanity.hpp"
4111be35a1SLionel Sambuc #include "utils/sqlite/c_gate.hpp"
4211be35a1SLionel Sambuc #include "utils/sqlite/exceptions.hpp"
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc namespace sqlite = utils::sqlite;
4511be35a1SLionel Sambuc
4611be35a1SLionel Sambuc
4711be35a1SLionel Sambuc namespace {
4811be35a1SLionel Sambuc
4911be35a1SLionel Sambuc
5011be35a1SLionel Sambuc static sqlite::type c_type_to_cxx(const int) UTILS_PURE;
5111be35a1SLionel Sambuc
5211be35a1SLionel Sambuc
5311be35a1SLionel Sambuc /// Maps a SQLite 3 data type to our own representation.
5411be35a1SLionel Sambuc ///
5511be35a1SLionel Sambuc /// \param original The native SQLite 3 data type.
5611be35a1SLionel Sambuc ///
5711be35a1SLionel Sambuc /// \return Our internal representation for the native data type.
5811be35a1SLionel Sambuc static sqlite::type
c_type_to_cxx(const int original)5911be35a1SLionel Sambuc c_type_to_cxx(const int original)
6011be35a1SLionel Sambuc {
6111be35a1SLionel Sambuc switch (original) {
6211be35a1SLionel Sambuc case SQLITE_BLOB: return sqlite::type_blob;
6311be35a1SLionel Sambuc case SQLITE_FLOAT: return sqlite::type_float;
6411be35a1SLionel Sambuc case SQLITE_INTEGER: return sqlite::type_integer;
6511be35a1SLionel Sambuc case SQLITE_NULL: return sqlite::type_null;
6611be35a1SLionel Sambuc case SQLITE_TEXT: return sqlite::type_text;
6711be35a1SLionel Sambuc default: UNREACHABLE_MSG("Unknown data type returned by SQLite 3");
6811be35a1SLionel Sambuc }
6911be35a1SLionel Sambuc UNREACHABLE;
7011be35a1SLionel Sambuc }
7111be35a1SLionel Sambuc
7211be35a1SLionel Sambuc
7311be35a1SLionel Sambuc /// Handles the return value of a sqlite3_bind_* call.
7411be35a1SLionel Sambuc ///
7511be35a1SLionel Sambuc /// \param db The database the call was made on.
7611be35a1SLionel Sambuc /// \param api_function The name of the sqlite3_bind_* function called.
7711be35a1SLionel Sambuc /// \param error The error code returned by the function; can be SQLITE_OK.
7811be35a1SLionel Sambuc ///
7911be35a1SLionel Sambuc /// \throw std::bad_alloc If there was no memory for the binding.
8011be35a1SLionel Sambuc /// \throw api_error If the binding fails for any other reason.
8111be35a1SLionel Sambuc static void
handle_bind_error(sqlite::database & db,const char * api_function,const int error)8211be35a1SLionel Sambuc handle_bind_error(sqlite::database& db, const char* api_function,
8311be35a1SLionel Sambuc const int error)
8411be35a1SLionel Sambuc {
8511be35a1SLionel Sambuc switch (error) {
8611be35a1SLionel Sambuc case SQLITE_OK:
8711be35a1SLionel Sambuc return;
8811be35a1SLionel Sambuc case SQLITE_RANGE:
8911be35a1SLionel Sambuc UNREACHABLE_MSG("Invalid index for bind argument");
9011be35a1SLionel Sambuc case SQLITE_NOMEM:
9111be35a1SLionel Sambuc throw std::bad_alloc();
9211be35a1SLionel Sambuc default:
9311be35a1SLionel Sambuc throw sqlite::api_error::from_database(db, api_function);
9411be35a1SLionel Sambuc }
9511be35a1SLionel Sambuc }
9611be35a1SLionel Sambuc
9711be35a1SLionel Sambuc
9811be35a1SLionel Sambuc } // anonymous namespace
9911be35a1SLionel Sambuc
10011be35a1SLionel Sambuc
10111be35a1SLionel Sambuc /// Internal implementation for sqlite::statement.
10211be35a1SLionel Sambuc struct utils::sqlite::statement::impl {
10311be35a1SLionel Sambuc /// The database this statement belongs to.
10411be35a1SLionel Sambuc sqlite::database& db;
10511be35a1SLionel Sambuc
10611be35a1SLionel Sambuc /// The SQLite 3 internal statement.
10711be35a1SLionel Sambuc ::sqlite3_stmt* stmt;
10811be35a1SLionel Sambuc
10911be35a1SLionel Sambuc /// Cache for the column names in a statement; lazily initialized.
11011be35a1SLionel Sambuc std::map< std::string, int > column_cache;
11111be35a1SLionel Sambuc
11211be35a1SLionel Sambuc /// Constructor.
11311be35a1SLionel Sambuc ///
11411be35a1SLionel Sambuc /// \param db_ The database this statement belongs to. Be aware that we
11511be35a1SLionel Sambuc /// keep a *reference* to the database; in other words, if the database
11611be35a1SLionel Sambuc /// vanishes, this object will become invalid. (It'd be trivial to keep
11711be35a1SLionel Sambuc /// a shallow copy here instead, but I feel that statements that outlive
11811be35a1SLionel Sambuc /// their database represents sloppy programming.)
11911be35a1SLionel Sambuc /// \param stmt_ The SQLite internal statement.
implutils::sqlite::statement::impl12011be35a1SLionel Sambuc impl(database& db_, ::sqlite3_stmt* stmt_) :
12111be35a1SLionel Sambuc db(db_),
12211be35a1SLionel Sambuc stmt(stmt_)
12311be35a1SLionel Sambuc {
12411be35a1SLionel Sambuc }
12511be35a1SLionel Sambuc
12611be35a1SLionel Sambuc /// Destructor.
12711be35a1SLionel Sambuc ///
12811be35a1SLionel Sambuc /// It is important to keep this as part of the 'impl' class instead of the
12911be35a1SLionel Sambuc /// container class. The 'impl' class is destroyed exactly once (because it
13011be35a1SLionel Sambuc /// is managed by a shared_ptr) and thus releasing the resources here is
13111be35a1SLionel Sambuc /// OK. However, the container class is potentially released many times,
13211be35a1SLionel Sambuc /// which means that we would be double-freeing the internal object and
13311be35a1SLionel Sambuc /// reusing invalid data.
~implutils::sqlite::statement::impl13411be35a1SLionel Sambuc ~impl(void)
13511be35a1SLionel Sambuc {
13611be35a1SLionel Sambuc (void)::sqlite3_finalize(stmt);
13711be35a1SLionel Sambuc }
13811be35a1SLionel Sambuc };
13911be35a1SLionel Sambuc
14011be35a1SLionel Sambuc
14111be35a1SLionel Sambuc /// Initializes a statement object.
14211be35a1SLionel Sambuc ///
14311be35a1SLionel Sambuc /// This is an internal function. Use database::create_statement() to
14411be35a1SLionel Sambuc /// instantiate one of these objects.
14511be35a1SLionel Sambuc ///
14611be35a1SLionel Sambuc /// \param db The database this statement belongs to.
14711be35a1SLionel Sambuc /// \param raw_stmt A void pointer representing a SQLite native statement of
14811be35a1SLionel Sambuc /// type sqlite3_stmt.
statement(database & db,void * raw_stmt)14911be35a1SLionel Sambuc sqlite::statement::statement(database& db, void* raw_stmt) :
15011be35a1SLionel Sambuc _pimpl(new impl(db, static_cast< ::sqlite3_stmt* >(raw_stmt)))
15111be35a1SLionel Sambuc {
15211be35a1SLionel Sambuc }
15311be35a1SLionel Sambuc
15411be35a1SLionel Sambuc
15511be35a1SLionel Sambuc /// Destructor for the statement.
15611be35a1SLionel Sambuc ///
15711be35a1SLionel Sambuc /// Remember that statements are reference-counted, so the statement will only
15811be35a1SLionel Sambuc /// cease to be valid once its last copy is destroyed.
~statement(void)15911be35a1SLionel Sambuc sqlite::statement::~statement(void)
16011be35a1SLionel Sambuc {
16111be35a1SLionel Sambuc }
16211be35a1SLionel Sambuc
16311be35a1SLionel Sambuc
16411be35a1SLionel Sambuc /// Executes a statement that is not supposed to return any data.
16511be35a1SLionel Sambuc ///
16611be35a1SLionel Sambuc /// Use this function to execute DDL and INSERT statements; i.e. statements that
16711be35a1SLionel Sambuc /// only have one processing step and deliver no rows. This frees the caller
16811be35a1SLionel Sambuc /// from having to deal with the return value of the step() function.
16911be35a1SLionel Sambuc ///
17011be35a1SLionel Sambuc /// \pre The statement to execute will not produce any rows.
17111be35a1SLionel Sambuc void
step_without_results(void)17211be35a1SLionel Sambuc sqlite::statement::step_without_results(void)
17311be35a1SLionel Sambuc {
174*3260d16fSLionel Sambuc #if defined(__minix) && !defined(NDEBUG)
175*3260d16fSLionel Sambuc const bool data =
176*3260d16fSLionel Sambuc #endif /* defined(__minix) && !defined(NDEBUG) */
177*3260d16fSLionel Sambuc step();
17811be35a1SLionel Sambuc INV_MSG(!data, "The statement should not have produced any rows, but it "
17911be35a1SLionel Sambuc "did");
18011be35a1SLionel Sambuc }
18111be35a1SLionel Sambuc
18211be35a1SLionel Sambuc
18311be35a1SLionel Sambuc /// Performs a processing step on the statement.
18411be35a1SLionel Sambuc ///
18511be35a1SLionel Sambuc /// \return True if the statement returned a row; false if the processing has
18611be35a1SLionel Sambuc /// finished.
18711be35a1SLionel Sambuc ///
18811be35a1SLionel Sambuc /// \throw api_error If the processing of the step raises an error.
18911be35a1SLionel Sambuc bool
step(void)19011be35a1SLionel Sambuc sqlite::statement::step(void)
19111be35a1SLionel Sambuc {
19211be35a1SLionel Sambuc const int error = ::sqlite3_step(_pimpl->stmt);
19311be35a1SLionel Sambuc switch (error) {
19411be35a1SLionel Sambuc case SQLITE_DONE:
19511be35a1SLionel Sambuc LD("Step statement; no more rows");
19611be35a1SLionel Sambuc return false;
19711be35a1SLionel Sambuc case SQLITE_ROW:
19811be35a1SLionel Sambuc LD("Step statement; row available for processing");
19911be35a1SLionel Sambuc return true;
20011be35a1SLionel Sambuc default:
20111be35a1SLionel Sambuc throw api_error::from_database(_pimpl->db, "sqlite3_step");
20211be35a1SLionel Sambuc }
20311be35a1SLionel Sambuc UNREACHABLE;
20411be35a1SLionel Sambuc }
20511be35a1SLionel Sambuc
20611be35a1SLionel Sambuc
20711be35a1SLionel Sambuc /// Returns the number of columns in the step result.
20811be35a1SLionel Sambuc ///
20911be35a1SLionel Sambuc /// \return The number of columns available for data retrieval.
21011be35a1SLionel Sambuc int
column_count(void)21111be35a1SLionel Sambuc sqlite::statement::column_count(void)
21211be35a1SLionel Sambuc {
21311be35a1SLionel Sambuc return ::sqlite3_column_count(_pimpl->stmt);
21411be35a1SLionel Sambuc }
21511be35a1SLionel Sambuc
21611be35a1SLionel Sambuc
21711be35a1SLionel Sambuc /// Returns the name of a particular column in the result.
21811be35a1SLionel Sambuc ///
21911be35a1SLionel Sambuc /// \param index The column to request the name of.
22011be35a1SLionel Sambuc ///
22111be35a1SLionel Sambuc /// \return The name of the requested column.
22211be35a1SLionel Sambuc std::string
column_name(const int index)22311be35a1SLionel Sambuc sqlite::statement::column_name(const int index)
22411be35a1SLionel Sambuc {
22511be35a1SLionel Sambuc const char* name = ::sqlite3_column_name(_pimpl->stmt, index);
22611be35a1SLionel Sambuc if (name == NULL)
22711be35a1SLionel Sambuc throw api_error::from_database(_pimpl->db, "sqlite3_column_name");
22811be35a1SLionel Sambuc return name;
22911be35a1SLionel Sambuc }
23011be35a1SLionel Sambuc
23111be35a1SLionel Sambuc
23211be35a1SLionel Sambuc /// Returns the type of a particular column in the result.
23311be35a1SLionel Sambuc ///
23411be35a1SLionel Sambuc /// \param index The column to request the type of.
23511be35a1SLionel Sambuc ///
23611be35a1SLionel Sambuc /// \return The type of the requested column.
23711be35a1SLionel Sambuc sqlite::type
column_type(const int index)23811be35a1SLionel Sambuc sqlite::statement::column_type(const int index)
23911be35a1SLionel Sambuc {
24011be35a1SLionel Sambuc return c_type_to_cxx(::sqlite3_column_type(_pimpl->stmt, index));
24111be35a1SLionel Sambuc }
24211be35a1SLionel Sambuc
24311be35a1SLionel Sambuc
24411be35a1SLionel Sambuc /// Finds a column by name.
24511be35a1SLionel Sambuc ///
24611be35a1SLionel Sambuc /// \param name The name of the column to search for.
24711be35a1SLionel Sambuc ///
24811be35a1SLionel Sambuc /// \return The column identifier.
24911be35a1SLionel Sambuc ///
25011be35a1SLionel Sambuc /// \throw value_error If the name cannot be found.
25111be35a1SLionel Sambuc int
column_id(const char * name)25211be35a1SLionel Sambuc sqlite::statement::column_id(const char* name)
25311be35a1SLionel Sambuc {
25411be35a1SLionel Sambuc std::map< std::string, int >& cache = _pimpl->column_cache;
25511be35a1SLionel Sambuc
25611be35a1SLionel Sambuc if (cache.empty()) {
25711be35a1SLionel Sambuc for (int i = 0; i < column_count(); i++) {
25811be35a1SLionel Sambuc const std::string aux_name = column_name(i);
25911be35a1SLionel Sambuc INV(cache.find(aux_name) == cache.end());
26011be35a1SLionel Sambuc cache[aux_name] = i;
26111be35a1SLionel Sambuc }
26211be35a1SLionel Sambuc }
26311be35a1SLionel Sambuc
26411be35a1SLionel Sambuc const std::map< std::string, int >::const_iterator iter = cache.find(name);
26511be35a1SLionel Sambuc if (iter == cache.end())
26611be35a1SLionel Sambuc throw invalid_column_error(name);
26711be35a1SLionel Sambuc else
26811be35a1SLionel Sambuc return (*iter).second;
26911be35a1SLionel Sambuc }
27011be35a1SLionel Sambuc
27111be35a1SLionel Sambuc
27211be35a1SLionel Sambuc /// Returns a particular column in the result as a blob.
27311be35a1SLionel Sambuc ///
27411be35a1SLionel Sambuc /// \param index The column to retrieve.
27511be35a1SLionel Sambuc ///
27611be35a1SLionel Sambuc /// \return A block of memory with the blob contents. Note that the pointer
27711be35a1SLionel Sambuc /// returned by this call will be invalidated on the next call to any SQLite API
27811be35a1SLionel Sambuc /// function.
27911be35a1SLionel Sambuc sqlite::blob
column_blob(const int index)28011be35a1SLionel Sambuc sqlite::statement::column_blob(const int index)
28111be35a1SLionel Sambuc {
28211be35a1SLionel Sambuc PRE(column_type(index) == type_blob);
28311be35a1SLionel Sambuc return blob(::sqlite3_column_blob(_pimpl->stmt, index),
28411be35a1SLionel Sambuc ::sqlite3_column_bytes(_pimpl->stmt, index));
28511be35a1SLionel Sambuc }
28611be35a1SLionel Sambuc
28711be35a1SLionel Sambuc
28811be35a1SLionel Sambuc /// Returns a particular column in the result as a double.
28911be35a1SLionel Sambuc ///
29011be35a1SLionel Sambuc /// \param index The column to retrieve.
29111be35a1SLionel Sambuc ///
29211be35a1SLionel Sambuc /// \return The double value.
29311be35a1SLionel Sambuc double
column_double(const int index)29411be35a1SLionel Sambuc sqlite::statement::column_double(const int index)
29511be35a1SLionel Sambuc {
29611be35a1SLionel Sambuc PRE(column_type(index) == type_float);
29711be35a1SLionel Sambuc return ::sqlite3_column_double(_pimpl->stmt, index);
29811be35a1SLionel Sambuc }
29911be35a1SLionel Sambuc
30011be35a1SLionel Sambuc
30111be35a1SLionel Sambuc /// Returns a particular column in the result as an integer.
30211be35a1SLionel Sambuc ///
30311be35a1SLionel Sambuc /// \param index The column to retrieve.
30411be35a1SLionel Sambuc ///
30511be35a1SLionel Sambuc /// \return The integer value. Note that the value may not fit in an integer
30611be35a1SLionel Sambuc /// depending on the platform. Use column_int64 to retrieve the integer without
30711be35a1SLionel Sambuc /// truncation.
30811be35a1SLionel Sambuc int
column_int(const int index)30911be35a1SLionel Sambuc sqlite::statement::column_int(const int index)
31011be35a1SLionel Sambuc {
31111be35a1SLionel Sambuc PRE(column_type(index) == type_integer);
31211be35a1SLionel Sambuc return ::sqlite3_column_int(_pimpl->stmt, index);
31311be35a1SLionel Sambuc }
31411be35a1SLionel Sambuc
31511be35a1SLionel Sambuc
31611be35a1SLionel Sambuc /// Returns a particular column in the result as a 64-bit integer.
31711be35a1SLionel Sambuc ///
31811be35a1SLionel Sambuc /// \param index The column to retrieve.
31911be35a1SLionel Sambuc ///
32011be35a1SLionel Sambuc /// \return The integer value.
32111be35a1SLionel Sambuc int64_t
column_int64(const int index)32211be35a1SLionel Sambuc sqlite::statement::column_int64(const int index)
32311be35a1SLionel Sambuc {
32411be35a1SLionel Sambuc PRE(column_type(index) == type_integer);
32511be35a1SLionel Sambuc return ::sqlite3_column_int64(_pimpl->stmt, index);
32611be35a1SLionel Sambuc }
32711be35a1SLionel Sambuc
32811be35a1SLionel Sambuc
32911be35a1SLionel Sambuc /// Returns a particular column in the result as a double.
33011be35a1SLionel Sambuc ///
33111be35a1SLionel Sambuc /// \param index The column to retrieve.
33211be35a1SLionel Sambuc ///
33311be35a1SLionel Sambuc /// \return A C string with the contents. Note that the pointer returned by
33411be35a1SLionel Sambuc /// this call will be invalidated on the next call to any SQLite API function.
33511be35a1SLionel Sambuc /// If you want to be extra safe, store the result in a std::string to not worry
33611be35a1SLionel Sambuc /// about this.
33711be35a1SLionel Sambuc std::string
column_text(const int index)33811be35a1SLionel Sambuc sqlite::statement::column_text(const int index)
33911be35a1SLionel Sambuc {
34011be35a1SLionel Sambuc PRE(column_type(index) == type_text);
34111be35a1SLionel Sambuc return reinterpret_cast< const char* >(::sqlite3_column_text(
34211be35a1SLionel Sambuc _pimpl->stmt, index));
34311be35a1SLionel Sambuc }
34411be35a1SLionel Sambuc
34511be35a1SLionel Sambuc
34611be35a1SLionel Sambuc /// Returns the number of bytes stored in the column.
34711be35a1SLionel Sambuc ///
34811be35a1SLionel Sambuc /// \pre This is only valid for columns of type blob and text.
34911be35a1SLionel Sambuc ///
35011be35a1SLionel Sambuc /// \param index The column to retrieve the size of.
35111be35a1SLionel Sambuc ///
35211be35a1SLionel Sambuc /// \return The number of bytes in the column. Remember that strings are stored
35311be35a1SLionel Sambuc /// in their UTF-8 representation; this call returns the number of *bytes*, not
35411be35a1SLionel Sambuc /// characters.
35511be35a1SLionel Sambuc int
column_bytes(const int index)35611be35a1SLionel Sambuc sqlite::statement::column_bytes(const int index)
35711be35a1SLionel Sambuc {
35811be35a1SLionel Sambuc PRE(column_type(index) == type_blob || column_type(index) == type_text);
35911be35a1SLionel Sambuc return ::sqlite3_column_bytes(_pimpl->stmt, index);
36011be35a1SLionel Sambuc }
36111be35a1SLionel Sambuc
36211be35a1SLionel Sambuc
36311be35a1SLionel Sambuc /// Type-checked version of column_blob.
36411be35a1SLionel Sambuc ///
36511be35a1SLionel Sambuc /// \param name The name of the column to retrieve.
36611be35a1SLionel Sambuc ///
36711be35a1SLionel Sambuc /// \return The same as column_blob if the value can be retrieved.
36811be35a1SLionel Sambuc ///
36911be35a1SLionel Sambuc /// \throw error If the type of the cell to retrieve is invalid.
37011be35a1SLionel Sambuc /// \throw invalid_column_error If name is invalid.
37111be35a1SLionel Sambuc sqlite::blob
safe_column_blob(const char * name)37211be35a1SLionel Sambuc sqlite::statement::safe_column_blob(const char* name)
37311be35a1SLionel Sambuc {
37411be35a1SLionel Sambuc const int column = column_id(name);
37511be35a1SLionel Sambuc if (column_type(column) != sqlite::type_blob)
37611be35a1SLionel Sambuc throw sqlite::error(F("Column '%s' is not a blob") % name);
37711be35a1SLionel Sambuc return column_blob(column);
37811be35a1SLionel Sambuc }
37911be35a1SLionel Sambuc
38011be35a1SLionel Sambuc
38111be35a1SLionel Sambuc /// Type-checked version of column_double.
38211be35a1SLionel Sambuc ///
38311be35a1SLionel Sambuc /// \param name The name of the column to retrieve.
38411be35a1SLionel Sambuc ///
38511be35a1SLionel Sambuc /// \return The same as column_double if the value can be retrieved.
38611be35a1SLionel Sambuc ///
38711be35a1SLionel Sambuc /// \throw error If the type of the cell to retrieve is invalid.
38811be35a1SLionel Sambuc /// \throw invalid_column_error If name is invalid.
38911be35a1SLionel Sambuc double
safe_column_double(const char * name)39011be35a1SLionel Sambuc sqlite::statement::safe_column_double(const char* name)
39111be35a1SLionel Sambuc {
39211be35a1SLionel Sambuc const int column = column_id(name);
39311be35a1SLionel Sambuc if (column_type(column) != sqlite::type_float)
39411be35a1SLionel Sambuc throw sqlite::error(F("Column '%s' is not a float") % name);
39511be35a1SLionel Sambuc return column_double(column);
39611be35a1SLionel Sambuc }
39711be35a1SLionel Sambuc
39811be35a1SLionel Sambuc
39911be35a1SLionel Sambuc /// Type-checked version of column_int.
40011be35a1SLionel Sambuc ///
40111be35a1SLionel Sambuc /// \param name The name of the column to retrieve.
40211be35a1SLionel Sambuc ///
40311be35a1SLionel Sambuc /// \return The same as column_int if the value can be retrieved.
40411be35a1SLionel Sambuc ///
40511be35a1SLionel Sambuc /// \throw error If the type of the cell to retrieve is invalid.
40611be35a1SLionel Sambuc /// \throw invalid_column_error If name is invalid.
40711be35a1SLionel Sambuc int
safe_column_int(const char * name)40811be35a1SLionel Sambuc sqlite::statement::safe_column_int(const char* name)
40911be35a1SLionel Sambuc {
41011be35a1SLionel Sambuc const int column = column_id(name);
41111be35a1SLionel Sambuc if (column_type(column) != sqlite::type_integer)
41211be35a1SLionel Sambuc throw sqlite::error(F("Column '%s' is not an integer") % name);
41311be35a1SLionel Sambuc return column_int(column);
41411be35a1SLionel Sambuc }
41511be35a1SLionel Sambuc
41611be35a1SLionel Sambuc
41711be35a1SLionel Sambuc /// Type-checked version of column_int64.
41811be35a1SLionel Sambuc ///
41911be35a1SLionel Sambuc /// \param name The name of the column to retrieve.
42011be35a1SLionel Sambuc ///
42111be35a1SLionel Sambuc /// \return The same as column_int64 if the value can be retrieved.
42211be35a1SLionel Sambuc ///
42311be35a1SLionel Sambuc /// \throw error If the type of the cell to retrieve is invalid.
42411be35a1SLionel Sambuc /// \throw invalid_column_error If name is invalid.
42511be35a1SLionel Sambuc int64_t
safe_column_int64(const char * name)42611be35a1SLionel Sambuc sqlite::statement::safe_column_int64(const char* name)
42711be35a1SLionel Sambuc {
42811be35a1SLionel Sambuc const int column = column_id(name);
42911be35a1SLionel Sambuc if (column_type(column) != sqlite::type_integer)
43011be35a1SLionel Sambuc throw sqlite::error(F("Column '%s' is not an integer") % name);
43111be35a1SLionel Sambuc return column_int64(column);
43211be35a1SLionel Sambuc }
43311be35a1SLionel Sambuc
43411be35a1SLionel Sambuc
43511be35a1SLionel Sambuc /// Type-checked version of column_text.
43611be35a1SLionel Sambuc ///
43711be35a1SLionel Sambuc /// \param name The name of the column to retrieve.
43811be35a1SLionel Sambuc ///
43911be35a1SLionel Sambuc /// \return The same as column_text if the value can be retrieved.
44011be35a1SLionel Sambuc ///
44111be35a1SLionel Sambuc /// \throw error If the type of the cell to retrieve is invalid.
44211be35a1SLionel Sambuc /// \throw invalid_column_error If name is invalid.
44311be35a1SLionel Sambuc std::string
safe_column_text(const char * name)44411be35a1SLionel Sambuc sqlite::statement::safe_column_text(const char* name)
44511be35a1SLionel Sambuc {
44611be35a1SLionel Sambuc const int column = column_id(name);
44711be35a1SLionel Sambuc if (column_type(column) != sqlite::type_text)
44811be35a1SLionel Sambuc throw sqlite::error(F("Column '%s' is not a string") % name);
44911be35a1SLionel Sambuc return column_text(column);
45011be35a1SLionel Sambuc }
45111be35a1SLionel Sambuc
45211be35a1SLionel Sambuc
45311be35a1SLionel Sambuc /// Type-checked version of column_bytes.
45411be35a1SLionel Sambuc ///
45511be35a1SLionel Sambuc /// \param name The name of the column to retrieve the size of.
45611be35a1SLionel Sambuc ///
45711be35a1SLionel Sambuc /// \return The same as column_bytes if the value can be retrieved.
45811be35a1SLionel Sambuc ///
45911be35a1SLionel Sambuc /// \throw error If the type of the cell to retrieve the size of is invalid.
46011be35a1SLionel Sambuc /// \throw invalid_column_error If name is invalid.
46111be35a1SLionel Sambuc int
safe_column_bytes(const char * name)46211be35a1SLionel Sambuc sqlite::statement::safe_column_bytes(const char* name)
46311be35a1SLionel Sambuc {
46411be35a1SLionel Sambuc const int column = column_id(name);
46511be35a1SLionel Sambuc if (column_type(column) != sqlite::type_blob &&
46611be35a1SLionel Sambuc column_type(column) != sqlite::type_text)
46711be35a1SLionel Sambuc throw sqlite::error(F("Column '%s' is not a blob or a string") % name);
46811be35a1SLionel Sambuc return column_bytes(column);
46911be35a1SLionel Sambuc }
47011be35a1SLionel Sambuc
47111be35a1SLionel Sambuc
47211be35a1SLionel Sambuc /// Resets a statement to allow further processing.
47311be35a1SLionel Sambuc void
reset(void)47411be35a1SLionel Sambuc sqlite::statement::reset(void)
47511be35a1SLionel Sambuc {
47611be35a1SLionel Sambuc (void)::sqlite3_reset(_pimpl->stmt);
47711be35a1SLionel Sambuc }
47811be35a1SLionel Sambuc
47911be35a1SLionel Sambuc
48011be35a1SLionel Sambuc /// Binds a blob to a prepared statement.
48111be35a1SLionel Sambuc ///
48211be35a1SLionel Sambuc /// \param index The index of the binding.
48311be35a1SLionel Sambuc /// \param b Description of the blob, which must remain valid during the
48411be35a1SLionel Sambuc /// execution of the statement.
48511be35a1SLionel Sambuc ///
48611be35a1SLionel Sambuc /// \throw api_error If the binding fails.
48711be35a1SLionel Sambuc void
bind(const int index,const blob & b)48811be35a1SLionel Sambuc sqlite::statement::bind(const int index, const blob& b)
48911be35a1SLionel Sambuc {
49011be35a1SLionel Sambuc const int error = ::sqlite3_bind_blob(_pimpl->stmt, index, b.memory, b.size,
49111be35a1SLionel Sambuc SQLITE_STATIC);
49211be35a1SLionel Sambuc handle_bind_error(_pimpl->db, "sqlite3_bind_blob", error);
49311be35a1SLionel Sambuc }
49411be35a1SLionel Sambuc
49511be35a1SLionel Sambuc
49611be35a1SLionel Sambuc /// Binds a double value to a prepared statement.
49711be35a1SLionel Sambuc ///
49811be35a1SLionel Sambuc /// \param index The index of the binding.
49911be35a1SLionel Sambuc /// \param value The double value to bind.
50011be35a1SLionel Sambuc ///
50111be35a1SLionel Sambuc /// \throw api_error If the binding fails.
50211be35a1SLionel Sambuc void
bind(const int index,const double value)50311be35a1SLionel Sambuc sqlite::statement::bind(const int index, const double value)
50411be35a1SLionel Sambuc {
50511be35a1SLionel Sambuc const int error = ::sqlite3_bind_double(_pimpl->stmt, index, value);
50611be35a1SLionel Sambuc handle_bind_error(_pimpl->db, "sqlite3_bind_double", error);
50711be35a1SLionel Sambuc }
50811be35a1SLionel Sambuc
50911be35a1SLionel Sambuc
51011be35a1SLionel Sambuc /// Binds an integer value to a prepared statement.
51111be35a1SLionel Sambuc ///
51211be35a1SLionel Sambuc /// \param index The index of the binding.
51311be35a1SLionel Sambuc /// \param value The integer value to bind.
51411be35a1SLionel Sambuc ///
51511be35a1SLionel Sambuc /// \throw api_error If the binding fails.
51611be35a1SLionel Sambuc void
bind(const int index,const int value)51711be35a1SLionel Sambuc sqlite::statement::bind(const int index, const int value)
51811be35a1SLionel Sambuc {
51911be35a1SLionel Sambuc const int error = ::sqlite3_bind_int(_pimpl->stmt, index, value);
52011be35a1SLionel Sambuc handle_bind_error(_pimpl->db, "sqlite3_bind_int", error);
52111be35a1SLionel Sambuc }
52211be35a1SLionel Sambuc
52311be35a1SLionel Sambuc
52411be35a1SLionel Sambuc /// Binds a 64-bit integer value to a prepared statement.
52511be35a1SLionel Sambuc ///
52611be35a1SLionel Sambuc /// \param index The index of the binding.
52711be35a1SLionel Sambuc /// \param value The 64-bin integer value to bind.
52811be35a1SLionel Sambuc ///
52911be35a1SLionel Sambuc /// \throw api_error If the binding fails.
53011be35a1SLionel Sambuc void
bind(const int index,const int64_t value)53111be35a1SLionel Sambuc sqlite::statement::bind(const int index, const int64_t value)
53211be35a1SLionel Sambuc {
53311be35a1SLionel Sambuc const int error = ::sqlite3_bind_int64(_pimpl->stmt, index, value);
53411be35a1SLionel Sambuc handle_bind_error(_pimpl->db, "sqlite3_bind_int64", error);
53511be35a1SLionel Sambuc }
53611be35a1SLionel Sambuc
53711be35a1SLionel Sambuc
53811be35a1SLionel Sambuc /// Binds a NULL value to a prepared statement.
53911be35a1SLionel Sambuc ///
54011be35a1SLionel Sambuc /// \param index The index of the binding.
54111be35a1SLionel Sambuc /// \param unused_null An instance of the null class.
54211be35a1SLionel Sambuc ///
54311be35a1SLionel Sambuc /// \throw api_error If the binding fails.
54411be35a1SLionel Sambuc void
bind(const int index,const null & UTILS_UNUSED_PARAM (null))54511be35a1SLionel Sambuc sqlite::statement::bind(const int index,
54611be35a1SLionel Sambuc const null& UTILS_UNUSED_PARAM(null))
54711be35a1SLionel Sambuc {
54811be35a1SLionel Sambuc const int error = ::sqlite3_bind_null(_pimpl->stmt, index);
54911be35a1SLionel Sambuc handle_bind_error(_pimpl->db, "sqlite3_bind_null", error);
55011be35a1SLionel Sambuc }
55111be35a1SLionel Sambuc
55211be35a1SLionel Sambuc
55311be35a1SLionel Sambuc /// Binds a text string to a prepared statement.
55411be35a1SLionel Sambuc ///
55511be35a1SLionel Sambuc /// \param index The index of the binding.
55611be35a1SLionel Sambuc /// \param text The string to bind. SQLite generates an internal copy of this
55711be35a1SLionel Sambuc /// string, so the original string object does not have to remain live. We
55811be35a1SLionel Sambuc /// do this because handling the lifetime of std::string objects is very
55911be35a1SLionel Sambuc /// hard (think about implicit conversions), so it is very easy to shoot
56011be35a1SLionel Sambuc /// ourselves in the foot if we don't do this.
56111be35a1SLionel Sambuc ///
56211be35a1SLionel Sambuc /// \throw api_error If the binding fails.
56311be35a1SLionel Sambuc void
bind(const int index,const std::string & text)56411be35a1SLionel Sambuc sqlite::statement::bind(const int index, const std::string& text)
56511be35a1SLionel Sambuc {
56611be35a1SLionel Sambuc const int error = ::sqlite3_bind_text(_pimpl->stmt, index, text.c_str(),
56711be35a1SLionel Sambuc text.length(), SQLITE_TRANSIENT);
56811be35a1SLionel Sambuc handle_bind_error(_pimpl->db, "sqlite3_bind_text", error);
56911be35a1SLionel Sambuc }
57011be35a1SLionel Sambuc
57111be35a1SLionel Sambuc
57211be35a1SLionel Sambuc /// Returns the index of the highest parameter.
57311be35a1SLionel Sambuc ///
57411be35a1SLionel Sambuc /// \return A parameter index.
57511be35a1SLionel Sambuc int
bind_parameter_count(void)57611be35a1SLionel Sambuc sqlite::statement::bind_parameter_count(void)
57711be35a1SLionel Sambuc {
57811be35a1SLionel Sambuc return ::sqlite3_bind_parameter_count(_pimpl->stmt);
57911be35a1SLionel Sambuc }
58011be35a1SLionel Sambuc
58111be35a1SLionel Sambuc
58211be35a1SLionel Sambuc /// Returns the index of a named parameter.
58311be35a1SLionel Sambuc ///
58411be35a1SLionel Sambuc /// \param name The name of the parameter to be queried; must exist.
58511be35a1SLionel Sambuc ///
58611be35a1SLionel Sambuc /// \return A parameter index.
58711be35a1SLionel Sambuc int
bind_parameter_index(const std::string & name)58811be35a1SLionel Sambuc sqlite::statement::bind_parameter_index(const std::string& name)
58911be35a1SLionel Sambuc {
59011be35a1SLionel Sambuc const int index = ::sqlite3_bind_parameter_index(_pimpl->stmt,
59111be35a1SLionel Sambuc name.c_str());
59211be35a1SLionel Sambuc PRE_MSG(index > 0, "Parameter name not in statement");
59311be35a1SLionel Sambuc return index;
59411be35a1SLionel Sambuc }
59511be35a1SLionel Sambuc
59611be35a1SLionel Sambuc
59711be35a1SLionel Sambuc /// Returns the name of a parameter by index.
59811be35a1SLionel Sambuc ///
59911be35a1SLionel Sambuc /// \param index The index to query; must be valid.
60011be35a1SLionel Sambuc ///
60111be35a1SLionel Sambuc /// \return The name of the parameter.
60211be35a1SLionel Sambuc std::string
bind_parameter_name(const int index)60311be35a1SLionel Sambuc sqlite::statement::bind_parameter_name(const int index)
60411be35a1SLionel Sambuc {
60511be35a1SLionel Sambuc const char* name = ::sqlite3_bind_parameter_name(_pimpl->stmt, index);
60611be35a1SLionel Sambuc PRE_MSG(name != NULL, "Index value out of range or nameless parameter");
60711be35a1SLionel Sambuc return std::string(name);
60811be35a1SLionel Sambuc }
60911be35a1SLionel Sambuc
61011be35a1SLionel Sambuc
61111be35a1SLionel Sambuc /// Clears any bindings and releases their memory.
61211be35a1SLionel Sambuc void
clear_bindings(void)61311be35a1SLionel Sambuc sqlite::statement::clear_bindings(void)
61411be35a1SLionel Sambuc {
61511be35a1SLionel Sambuc const int error = ::sqlite3_clear_bindings(_pimpl->stmt);
616*3260d16fSLionel Sambuc #if defined(__minix) && defined(NDEBUG)
617*3260d16fSLionel Sambuc #undef PRE_MSG
618*3260d16fSLionel Sambuc #define PRE_MSG(expr, msg) \
619*3260d16fSLionel Sambuc do { \
620*3260d16fSLionel Sambuc if (!(expr)) \
621*3260d16fSLionel Sambuc utils::sanity_failure(utils::precondition, __FILE__, __LINE__, msg); \
622*3260d16fSLionel Sambuc } while (0)
623*3260d16fSLionel Sambuc #endif /* defined(__minix) && defined(NDEBUG) */
62411be35a1SLionel Sambuc PRE_MSG(error == SQLITE_OK, "SQLite3 contract has changed; it should "
62511be35a1SLionel Sambuc "only return SQLITE_OK");
62611be35a1SLionel Sambuc }
627