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