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 #include <tr1/memory> 44 45 namespace utils { 46 namespace sqlite { 47 48 49 class database; 50 51 52 /// Representation of the SQLite data types. 53 enum type { 54 type_blob, 55 type_float, 56 type_integer, 57 type_null, 58 type_text, 59 }; 60 61 62 /// Representation of a BLOB. 63 class blob { 64 public: 65 /// Memory representing the contents of the blob, or NULL if empty. 66 /// 67 /// This memory must remain valid throughout the life of this object, as we 68 /// do not grab ownership of the memory. 69 const void* memory; 70 71 /// Number of bytes in memory. 72 int size; 73 74 /// Constructs a new blob. 75 /// 76 /// \param memory_ Pointer to the contents of the blob. 77 /// \param size_ The size of memory_. 78 blob(const void* memory_, const int size_) : 79 memory(memory_), size(size_) 80 { 81 } 82 }; 83 84 85 /// Representation of a SQL NULL value. 86 class null { 87 }; 88 89 90 /// A RAII model for an SQLite 3 statement. 91 class statement { 92 struct impl; 93 94 /// Pointer to the shared internal implementation. 95 std::tr1::shared_ptr< impl > _pimpl; 96 97 statement(database&, void*); 98 friend class database; 99 100 public: 101 ~statement(void); 102 103 bool step(void); 104 void step_without_results(void); 105 106 int column_count(void); 107 std::string column_name(const int); 108 type column_type(const int); 109 int column_id(const char*); 110 111 blob column_blob(const int); 112 double column_double(const int); 113 int column_int(const int); 114 int64_t column_int64(const int); 115 std::string column_text(const int); 116 int column_bytes(const int); 117 118 blob safe_column_blob(const char*); 119 double safe_column_double(const char*); 120 int safe_column_int(const char*); 121 int64_t safe_column_int64(const char*); 122 std::string safe_column_text(const char*); 123 int safe_column_bytes(const char*); 124 125 void reset(void); 126 127 void bind(const int, const blob&); 128 void bind(const int, const double); 129 void bind(const int, const int); 130 void bind(const int, const int64_t); 131 void bind(const int, const null&); 132 void bind(const int, const std::string&); 133 template< class T > void bind(const char*, const T&); 134 135 int bind_parameter_count(void); 136 int bind_parameter_index(const std::string&); 137 std::string bind_parameter_name(const int); 138 139 void clear_bindings(void); 140 }; 141 142 143 } // namespace sqlite 144 } // namespace utils 145 146 #endif // !defined(UTILS_SQLITE_STATEMENT_HPP) 147