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 /// \file utils/sqlite/statement.hpp
3011be35a1SLionel Sambuc /// Wrapper classes and utilities for SQLite statement processing.
3111be35a1SLionel Sambuc ///
3211be35a1SLionel Sambuc /// This module contains thin RAII wrappers around the SQLite 3 structures
3311be35a1SLionel Sambuc /// representing statements.
3411be35a1SLionel Sambuc 
3511be35a1SLionel Sambuc #if !defined(UTILS_SQLITE_STATEMENT_HPP)
3611be35a1SLionel Sambuc #define UTILS_SQLITE_STATEMENT_HPP
3711be35a1SLionel Sambuc 
3811be35a1SLionel Sambuc extern "C" {
3911be35a1SLionel Sambuc #include <stdint.h>
4011be35a1SLionel Sambuc }
4111be35a1SLionel Sambuc 
4211be35a1SLionel Sambuc #include <string>
43*84d9c625SLionel Sambuc 
44*84d9c625SLionel Sambuc #include "utils/shared_ptr.hpp"
4511be35a1SLionel Sambuc 
4611be35a1SLionel Sambuc namespace utils {
4711be35a1SLionel Sambuc namespace sqlite {
4811be35a1SLionel Sambuc 
4911be35a1SLionel Sambuc 
5011be35a1SLionel Sambuc class database;
5111be35a1SLionel Sambuc 
5211be35a1SLionel Sambuc 
5311be35a1SLionel Sambuc /// Representation of the SQLite data types.
5411be35a1SLionel Sambuc enum type {
5511be35a1SLionel Sambuc     type_blob,
5611be35a1SLionel Sambuc     type_float,
5711be35a1SLionel Sambuc     type_integer,
5811be35a1SLionel Sambuc     type_null,
5911be35a1SLionel Sambuc     type_text,
6011be35a1SLionel Sambuc };
6111be35a1SLionel Sambuc 
6211be35a1SLionel Sambuc 
6311be35a1SLionel Sambuc /// Representation of a BLOB.
6411be35a1SLionel Sambuc class blob {
6511be35a1SLionel Sambuc public:
6611be35a1SLionel Sambuc     /// Memory representing the contents of the blob, or NULL if empty.
6711be35a1SLionel Sambuc     ///
6811be35a1SLionel Sambuc     /// This memory must remain valid throughout the life of this object, as we
6911be35a1SLionel Sambuc     /// do not grab ownership of the memory.
7011be35a1SLionel Sambuc     const void* memory;
7111be35a1SLionel Sambuc 
7211be35a1SLionel Sambuc     /// Number of bytes in memory.
7311be35a1SLionel Sambuc     int size;
7411be35a1SLionel Sambuc 
7511be35a1SLionel Sambuc     /// Constructs a new blob.
7611be35a1SLionel Sambuc     ///
7711be35a1SLionel Sambuc     /// \param memory_ Pointer to the contents of the blob.
7811be35a1SLionel Sambuc     /// \param size_ The size of memory_.
blob(const void * memory_,const int size_)7911be35a1SLionel Sambuc     blob(const void* memory_, const int size_) :
8011be35a1SLionel Sambuc         memory(memory_), size(size_)
8111be35a1SLionel Sambuc     {
8211be35a1SLionel Sambuc     }
8311be35a1SLionel Sambuc };
8411be35a1SLionel Sambuc 
8511be35a1SLionel Sambuc 
8611be35a1SLionel Sambuc /// Representation of a SQL NULL value.
8711be35a1SLionel Sambuc class null {
8811be35a1SLionel Sambuc };
8911be35a1SLionel Sambuc 
9011be35a1SLionel Sambuc 
9111be35a1SLionel Sambuc /// A RAII model for an SQLite 3 statement.
9211be35a1SLionel Sambuc class statement {
9311be35a1SLionel Sambuc     struct impl;
9411be35a1SLionel Sambuc 
9511be35a1SLionel Sambuc     /// Pointer to the shared internal implementation.
96*84d9c625SLionel Sambuc     std::shared_ptr< impl > _pimpl;
9711be35a1SLionel Sambuc 
9811be35a1SLionel Sambuc     statement(database&, void*);
9911be35a1SLionel Sambuc     friend class database;
10011be35a1SLionel Sambuc 
10111be35a1SLionel Sambuc public:
10211be35a1SLionel Sambuc     ~statement(void);
10311be35a1SLionel Sambuc 
10411be35a1SLionel Sambuc     bool step(void);
10511be35a1SLionel Sambuc     void step_without_results(void);
10611be35a1SLionel Sambuc 
10711be35a1SLionel Sambuc     int column_count(void);
10811be35a1SLionel Sambuc     std::string column_name(const int);
10911be35a1SLionel Sambuc     type column_type(const int);
11011be35a1SLionel Sambuc     int column_id(const char*);
11111be35a1SLionel Sambuc 
11211be35a1SLionel Sambuc     blob column_blob(const int);
11311be35a1SLionel Sambuc     double column_double(const int);
11411be35a1SLionel Sambuc     int column_int(const int);
11511be35a1SLionel Sambuc     int64_t column_int64(const int);
11611be35a1SLionel Sambuc     std::string column_text(const int);
11711be35a1SLionel Sambuc     int column_bytes(const int);
11811be35a1SLionel Sambuc 
11911be35a1SLionel Sambuc     blob safe_column_blob(const char*);
12011be35a1SLionel Sambuc     double safe_column_double(const char*);
12111be35a1SLionel Sambuc     int safe_column_int(const char*);
12211be35a1SLionel Sambuc     int64_t safe_column_int64(const char*);
12311be35a1SLionel Sambuc     std::string safe_column_text(const char*);
12411be35a1SLionel Sambuc     int safe_column_bytes(const char*);
12511be35a1SLionel Sambuc 
12611be35a1SLionel Sambuc     void reset(void);
12711be35a1SLionel Sambuc 
12811be35a1SLionel Sambuc     void bind(const int, const blob&);
12911be35a1SLionel Sambuc     void bind(const int, const double);
13011be35a1SLionel Sambuc     void bind(const int, const int);
13111be35a1SLionel Sambuc     void bind(const int, const int64_t);
13211be35a1SLionel Sambuc     void bind(const int, const null&);
13311be35a1SLionel Sambuc     void bind(const int, const std::string&);
13411be35a1SLionel Sambuc     template< class T > void bind(const char*, const T&);
13511be35a1SLionel Sambuc 
13611be35a1SLionel Sambuc     int bind_parameter_count(void);
13711be35a1SLionel Sambuc     int bind_parameter_index(const std::string&);
13811be35a1SLionel Sambuc     std::string bind_parameter_name(const int);
13911be35a1SLionel Sambuc 
14011be35a1SLionel Sambuc     void clear_bindings(void);
14111be35a1SLionel Sambuc };
14211be35a1SLionel Sambuc 
14311be35a1SLionel Sambuc 
14411be35a1SLionel Sambuc }  // namespace sqlite
14511be35a1SLionel Sambuc }  // namespace utils
14611be35a1SLionel Sambuc 
14711be35a1SLionel Sambuc #endif  // !defined(UTILS_SQLITE_STATEMENT_HPP)
148