xref: /netbsd-src/external/bsd/kyua-cli/dist/store/dbtypes.cpp (revision 6b3a42af15b5e090c339512c790dd68f3d11a9d8)
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 #include "store/dbtypes.hpp"
30 
31 #include "engine/test_program.hpp"
32 #include "store/exceptions.hpp"
33 #include "utils/format/macros.hpp"
34 #include "utils/sanity.hpp"
35 #include "utils/sqlite/statement.ipp"
36 
37 namespace datetime = utils::datetime;
38 namespace sqlite = utils::sqlite;
39 
40 
41 /// Binds a boolean value to a statement parameter.
42 ///
43 /// \param stmt The statement to which to bind the parameter.
44 /// \param field The name of the parameter; must exist.
45 /// \param value The value to bind.
46 void
bind_bool(sqlite::statement & stmt,const char * field,const bool value)47 store::bind_bool(sqlite::statement& stmt, const char* field, const bool value)
48 {
49     stmt.bind(field, value ? "true" : "false");
50 }
51 
52 
53 /// Binds a time delta to a statement parameter.
54 ///
55 /// \param stmt The statement to which to bind the parameter.
56 /// \param field The name of the parameter; must exist.
57 /// \param delta The value to bind.
58 void
bind_delta(sqlite::statement & stmt,const char * field,const datetime::delta & delta)59 store::bind_delta(sqlite::statement& stmt, const char* field,
60                   const datetime::delta& delta)
61 {
62     stmt.bind(field, static_cast< int64_t >(delta.to_microseconds()));
63 }
64 
65 
66 /// Binds a string to a statement parameter.
67 ///
68 /// If the string is not empty, this binds the string itself.  Otherwise, it
69 /// binds a NULL value.
70 ///
71 /// \param stmt The statement to which to bind the parameter.
72 /// \param stmt The statement to which to bind the field.
73 /// \param field The name of the parameter; must exist.
74 /// \param str The string to bind.
75 void
bind_optional_string(sqlite::statement & stmt,const char * field,const std::string & str)76 store::bind_optional_string(sqlite::statement& stmt, const char* field,
77                             const std::string& str)
78 {
79     if (str.empty())
80         stmt.bind(field, sqlite::null());
81     else
82         stmt.bind(field, str);
83 }
84 
85 
86 /// Binds a timestamp to a statement parameter.
87 ///
88 /// \param stmt The statement to which to bind the parameter.
89 /// \param field The name of the parameter; must exist.
90 /// \param timestamp The value to bind.
91 void
bind_timestamp(sqlite::statement & stmt,const char * field,const datetime::timestamp & timestamp)92 store::bind_timestamp(sqlite::statement& stmt, const char* field,
93                       const datetime::timestamp& timestamp)
94 {
95     stmt.bind(field, timestamp.to_microseconds());
96 }
97 
98 
99 /// Queries a boolean value from a statement.
100 ///
101 /// \param stmt The statement from which to get the column.
102 /// \param column The name of the column holding the value.
103 ///
104 /// \return The parsed value if all goes well.
105 ///
106 /// \throw integrity_error If the value in the specified column is invalid.
107 bool
column_bool(sqlite::statement & stmt,const char * column)108 store::column_bool(sqlite::statement& stmt, const char* column)
109 {
110     const int id = stmt.column_id(column);
111     if (stmt.column_type(id) != sqlite::type_text)
112         throw store::integrity_error(F("Boolean value in column %s is not a "
113                                        "string") % column);
114     const std::string value = stmt.column_text(id);
115     if (value == "true")
116         return true;
117     else if (value == "false")
118         return false;
119     else
120         throw store::integrity_error(F("Unknown boolean value '%s'") % value);
121 }
122 
123 
124 /// Queries a time delta from a statement.
125 ///
126 /// \param stmt The statement from which to get the column.
127 /// \param column The name of the column holding the value.
128 ///
129 /// \return The parsed value if all goes well.
130 ///
131 /// \throw integrity_error If the value in the specified column is invalid.
132 datetime::delta
column_delta(sqlite::statement & stmt,const char * column)133 store::column_delta(sqlite::statement& stmt, const char* column)
134 {
135     const int id = stmt.column_id(column);
136     if (stmt.column_type(id) != sqlite::type_integer)
137         throw store::integrity_error(F("Time delta in column %s is not an "
138                                        "integer") % column);
139     return datetime::delta::from_microseconds(stmt.column_int64(id));
140 }
141 
142 
143 /// Queries an optional string from a statement.
144 ///
145 /// \param stmt The statement from which to get the column.
146 /// \param column The name of the column holding the value.
147 ///
148 /// \return The parsed value if all goes well.
149 ///
150 /// \throw integrity_error If the value in the specified column is invalid.
151 std::string
column_optional_string(sqlite::statement & stmt,const char * column)152 store::column_optional_string(sqlite::statement& stmt, const char* column)
153 {
154     const int id = stmt.column_id(column);
155     switch (stmt.column_type(id)) {
156     case sqlite::type_text:
157         return stmt.column_text(id);
158     case sqlite::type_null:
159         return "";
160     default:
161         throw integrity_error(F("Invalid string type in column %s") % column);
162     }
163 }
164 
165 
166 /// Queries a timestamp from a statement.
167 ///
168 /// \param stmt The statement from which to get the column.
169 /// \param column The name of the column holding the value.
170 ///
171 /// \return The parsed value if all goes well.
172 ///
173 /// \throw integrity_error If the value in the specified column is invalid.
174 datetime::timestamp
column_timestamp(sqlite::statement & stmt,const char * column)175 store::column_timestamp(sqlite::statement& stmt, const char* column)
176 {
177     const int id = stmt.column_id(column);
178     if (stmt.column_type(id) != sqlite::type_integer)
179         throw store::integrity_error(F("Timestamp in column %s is not an "
180                                        "integer") % column);
181     const int64_t value = stmt.column_int64(id);
182     if (value < 0)
183         throw store::integrity_error(F("Timestamp in column %s must be "
184                                        "positive") % column);
185     return datetime::timestamp::from_microseconds(value);
186 }
187