Home | History | Annotate | Line # | Download | only in sqlite
      1 // Copyright 2011 Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      8 // * Redistributions of source code must retain the above copyright
      9 //   notice, this list of conditions and the following disclaimer.
     10 // * Redistributions in binary form must reproduce the above copyright
     11 //   notice, this list of conditions and the following disclaimer in the
     12 //   documentation and/or other materials provided with the distribution.
     13 // * Neither the name of Google Inc. nor the names of its contributors
     14 //   may be used to endorse or promote products derived from this software
     15 //   without specific prior written permission.
     16 //
     17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 /// \file utils/sqlite/statement.hpp
     30 /// Wrapper classes and utilities for SQLite statement processing.
     31 ///
     32 /// This module contains thin RAII wrappers around the SQLite 3 structures
     33 /// representing statements.
     34 
     35 #if !defined(UTILS_SQLITE_STATEMENT_HPP)
     36 #define UTILS_SQLITE_STATEMENT_HPP
     37 
     38 extern "C" {
     39 #include <stdint.h>
     40 }
     41 
     42 #include <string>
     43 
     44 #include "utils/shared_ptr.hpp"
     45 
     46 namespace utils {
     47 namespace sqlite {
     48 
     49 
     50 class database;
     51 
     52 
     53 /// Representation of the SQLite data types.
     54 enum type {
     55     type_blob,
     56     type_float,
     57     type_integer,
     58     type_null,
     59     type_text,
     60 };
     61 
     62 
     63 /// Representation of a BLOB.
     64 class blob {
     65 public:
     66     /// Memory representing the contents of the blob, or NULL if empty.
     67     ///
     68     /// This memory must remain valid throughout the life of this object, as we
     69     /// do not grab ownership of the memory.
     70     const void* memory;
     71 
     72     /// Number of bytes in memory.
     73     int size;
     74 
     75     /// Constructs a new blob.
     76     ///
     77     /// \param memory_ Pointer to the contents of the blob.
     78     /// \param size_ The size of memory_.
     79     blob(const void* memory_, const int size_) :
     80         memory(memory_), size(size_)
     81     {
     82     }
     83 };
     84 
     85 
     86 /// Representation of a SQL NULL value.
     87 class null {
     88 };
     89 
     90 
     91 /// A RAII model for an SQLite 3 statement.
     92 class statement {
     93     struct impl;
     94 
     95     /// Pointer to the shared internal implementation.
     96     std::shared_ptr< impl > _pimpl;
     97 
     98     statement(database&, void*);
     99     friend class database;
    100 
    101 public:
    102     ~statement(void);
    103 
    104     bool step(void);
    105     void step_without_results(void);
    106 
    107     int column_count(void);
    108     std::string column_name(const int);
    109     type column_type(const int);
    110     int column_id(const char*);
    111 
    112     blob column_blob(const int);
    113     double column_double(const int);
    114     int column_int(const int);
    115     int64_t column_int64(const int);
    116     std::string column_text(const int);
    117     int column_bytes(const int);
    118 
    119     blob safe_column_blob(const char*);
    120     double safe_column_double(const char*);
    121     int safe_column_int(const char*);
    122     int64_t safe_column_int64(const char*);
    123     std::string safe_column_text(const char*);
    124     int safe_column_bytes(const char*);
    125 
    126     void reset(void);
    127 
    128     void bind(const int, const blob&);
    129     void bind(const int, const double);
    130     void bind(const int, const int);
    131     void bind(const int, const int64_t);
    132     void bind(const int, const null&);
    133     void bind(const int, const std::string&);
    134     template< class T > void bind(const char*, const T&);
    135 
    136     int bind_parameter_count(void);
    137     int bind_parameter_index(const std::string&);
    138     std::string bind_parameter_name(const int);
    139 
    140     void clear_bindings(void);
    141 };
    142 
    143 
    144 }  // namespace sqlite
    145 }  // namespace utils
    146 
    147 #endif  // !defined(UTILS_SQLITE_STATEMENT_HPP)
    148