libsqlite3x 2007.10.18
sqlite3x.hpp
1#ifndef s11n_net_SQLITE3X_HPP_INCLUDED
2#define s11n_net_SQLITE3X_HPP_INCLUDED
3/*
4
5 Copyright (C) 2004-2005 Cory Nelson
6 Copyright (C) 2006 stephan beal (stephan s11n net)
7
8 This software is provided 'as-is', without any express or implied
9 warranty. In no event will the authors be held liable for any damages
10 arising from the use of this software.
11
12 Permission is granted to anyone to use this software for any purpose,
13 including commercial applications, and to alter it and redistribute it
14 freely, subject to the following restrictions:
15
16 1. The origin of this software must not be misrepresented; you must not
17 claim that you wrote the original software. If you use this software
18 in a product, an acknowledgment in the product documentation would be
19 appreciated but is not required.
20 2. Altered source versions must be plainly marked as such, and must not be
21 misrepresented as being the original software.
22 3. This notice may not be removed or altered from any source distribution.
23
24
25This file has been modified from the original sources by <stephan at
26s11n net>, as described briefly below.
27
28The original code, by Cory Nelson, is available from:
29
30http://dev.int64.org/sqlite.html
31
32This hacked copy's home is:
33
34http://wanderinghorse.net/computing/sqlite/
35
36Contributors to the hacked version include:
37
38stephan beal <stephan at s11n net>
39- Maintainer, documentor.
40
41Thomas Sailer <t.sailer at alumni ethz ch>:
42- A fix for wide-char support on 64-bit Linux.
43
44Artem Gr <artem at bizlink ru>
45- Fixes to enable/disable wide-char support with a macro.
46
47- Xose Anton Otero Ferreira submitted patches to remove 'long long'
48decls and replace those with sqlite_int64. He also submitted
49the isnull() functions.
50
51
52Significant changes from the original sqlite3x distribution include:
53
54- Removed dependency on boost library, since it was only a dependency
55on boost::non_copyable (this same effect is easily achieved without
56the dependency on the huge Boost library).
57
58- Reordered some code to get it to compile under gcc.
59
60- Added some missing #includes.
61
62- database_error was reimplemented to allow use of a varargs ctor.
63
64- Added a few helpful functions, like sqlite3_cursor::colcount().
65
66- Removed various (char const * ...) overloads which were inherently
67already covered by implicit conversions via (std::string const &)
68overloads. Re-added them on 2006.09.25 after Artem Gr pointed out that
69those overloads avoid a potential extra copy of the strings, and that
70this could be a significant performance factor for some applications.
71
72- Added lots of API docs.
73
74- Improved some exception messages.
75
76- Added table_generator class.
77
78- Added sqlite3_connection::executecallback().
79
80- sqlite3_cursor renamed to sqlite3_cursor (2007.01.22).
81
82- Added sqlite3_cursor::close()
83
84- sqlite3_cursor::read() renamed to step() (2007.01.22).
85
86*/
87
88#include <string>
89#include <stdexcept>
90#include <sqlite3.h> // only for sqlite3_callback :/
91
92// Enable WCHAR support when it's there. Thanks to Artem Gr <artem@bizlink.ru>
93// for this...
94#ifndef SQLITE3X_USE_WCHAR
95# ifdef _GLIBCXX_USE_WCHAR_T
96# define SQLITE3X_USE_WCHAR 1
97# elif defined(UNICODE) // Windows uses this
98# define SQLITE3X_USE_WCHAR 1
99# else
100# define SQLITE3X_USE_WCHAR 0 // default
101# endif
102#endif
103
104/**
105 This namespace encapsulates a C++ API wrapper for sqlite3
106 databases. It was originally written by Cory Nelson and was hacked
107 slightly by stephan beal.
108
109 The home page for the original sources note that all of the
110 w_char/wstring functions *probably* only work on Windows
111 platforms. Your mileage may vary on other platforms. Users of this
112 API are recommended to NOT use the wchar_t/wstring variants of any
113 functions, as those functions may be removed at some point.
114
115
116 Note that this API does not include support for all sqlite3
117 features. However, the most commonly used features are available.
118
119*/
120namespace sqlite3x {
121
122 /**
123 64-bit integer type used by this code.
124 */
125 typedef sqlite_int64 int64_t;
126
127 class sqlite3_command;
128
129 /**
130 rc_is_okay() is an easy way to check if rc is one of
131 SQLITE_OK, SQLITE_ROW, or SQLITE_DONE. This function
132 returns true if rc is one of those values, else false.
133 When writing code which accepts arbitrary client-supplied
134 SQL, any of those three codes can signal success, depending
135 on the SQL code and the context.
136 */
137 bool rc_is_okay( int rc );
138
139
140 /**
141 Represents a connection to an sqlite3 database.
142
143 About the only reason to subclass this type would be to do
144 customizations to the underlying sqlite3 db handle upon
145 construction of each object, e.g. to add custom sqlite
146 functions or load custom modules.
147 */
149 {
150 private:
151 // copy operations not implemented
152 sqlite3_connection & operator=( sqlite3_connection const & );
154
155 friend class sqlite3_command;
156
157 mutable struct sqlite3 *m_db;
158 std::string m_name;
159
160 public:
161 /**
162 Returns a handle to the underlying sqlite3
163 database. Friend classes should NEVER call
164 sqlite3_close() on this handle. Doing so will
165 result in undefined behaviour later on (when this
166 class is used or destructs).
167
168 This function is only public so that clients can
169 do things like register new sqlite3 functions
170 with the database.
171 */
172 sqlite3 * db() const;
173
174 /**
175 Default ctor. DB is unusable until open() is
176 called.
177 */
179
180 /**
181 Opens a database with the given name. Throws if
182 this->open(dbname) fails.
183 */
184 explicit sqlite3_connection(std::string const & dbname);
185
186 /**
187 See take(sqlite3*). This ctor is identical except
188 that it throws if passed a null pointer.
189 */
190 sqlite3_connection( sqlite3 * dbh );
191
192 /**
193 Calls this->close() if close() has not already
194 been called. If it calls close() then the exception
195 is silently ignored for the sake of having a no-throw
196 dtor.
197 */
198 virtual ~sqlite3_connection();
199
200 /** Returns this object's name. It is only valid if
201 the (char const *) ctor or open(char const *) was
202 used with a non-null name, otherwise it is an
203 empty string.
204 */
205 std::string name() const;
206
207
208 /**
209 Creates/opens the given db, throwing on error.
210 Remember that sqlite3 supports the name ":memory:"
211 as a pseudo-name for an in-memory database.
212
213 On success it returns, else it throws.
214
215 Note that sqlite3 supports the special db name
216 ":memory:" to represent an in-memory database. Such
217 databases cannot be saved directly to disk and are
218 lost when this object closes the db.
219
220 Internal notes:
221
222 Once an sqlite3_open() succeeds, the protected
223 member this->on_open() in called. That member
224 should throw on error.
225
226 Subclasses which override this and do not want to
227 call the base implementation should call on_open()
228 when done to allow subclasses to initialize the
229 database if they like.
230 */
231 virtual void open( char const * );
232
233 /**
234 Functionally the same as open( char const *).
235 */
236 void open(std::string const &dbname);
237
238 /**
239
240 Transfers control of dbh to this object and makes
241 this object point at dbh. dbh is assumed to be
242 a valid, opened sqlite3 db handle.
243
244 If this->db() == dbh then this function
245 does nothing.
246
247 If this object had an opened db handle
248 then it is closed before dbh is taken.
249 Closing may throw, but this function takes
250 ownership of dbh regardless of whether
251 it throws or not.
252
253 If dbh is null, the effect is identical
254 to calling close().
255
256 This function triggers the protected on_open()
257 function if dbh is not null.
258 */
259 void take( sqlite3 * dbh );
260
261 /**
262 Transfers ownership of the returned handle to the caller.
263 This object is then considered closed. NULL is returned
264 if this object is closed.
265 */
266 sqlite3 * take() throw();
267
268
269 /**
270 Closes this database. If the db is not opened,
271 this is a no-op.
272 */
273 void close();
274
275 /**
276 Returns the rowid of the most recently inserted row
277 on this db.
278 */
280
281 /**
282 Returns the number of database rows that were
283 changed (or inserted or deleted) by the most recently
284 completed INSERT, UPDATE, or DELETE statement.
285
286 SQLite implements the command "DELETE FROM table"
287 without a WHERE clause by dropping and recreating
288 the table. To get an accurate count of the number
289 of rows deleted, use "DELETE FROM table WHERE 1"
290 instead.
291 */
292 int changes();
293
294
295 /**
296 See sqlite3_busy_timeout().
297 */
298 void setbusytimeout(int ms);
299
300 /**
301 Executes a command which is assumed to have
302 a single step and a void result.
303 */
304 void executenonquery(const std::string &sql);
305 /**
306 Overloaded to avoid an internal copy of sql.
307 sql MUST be non-NULL and null-terminated.
308 */
309 void executenonquery(char const * sql);
310
311 /**
312 Executes the query, which is expected to have an
313 integer field as the first result field.
314 */
315 int executeint(const std::string &sql);
316 /**
317 Overloaded to avoid an internal copy of sql.
318 sql MUST be non-NULL and null-terminated.
319 */
320 int executeint(char const * sql);
321
322 /**
323 Executes the query, which is expected to have a
324 (int64_t) field as the first result field.
325 */
326 int64_t executeint64(const std::string &sql);
327 /**
328 Overloaded to avoid an internal copy of sql.
329 sql MUST be non-NULL and null-terminated.
330 */
331 int64_t executeint64(char const * sql);
332
333 /**
334 Executes the query, which is expected to have a
335 double field as the first result field.
336 */
337 double executedouble(const std::string &sql);
338
339 /**
340 Overloaded to avoid an internal copy of sql.
341 sql MUST be non-NULL and null-terminated.
342 */
343 double executedouble(char const * sql);
344
345 /**
346 Executes the query, which is expected to have a
347 string or blob field as the first result field. Note
348 that numeric results can be returned using this function,
349 but will come back as a string (lexically cast).
350 */
351 std::string executestring(const std::string &sql);
352
353 /**
354 Executes the query, which is expected to have a
355 string or blob field as the first result field. Note
356 that numeric results can be returned using this function,
357 but will come back as a string (lexically cast).
358 */
359 std::string executeblob(const std::string &sql);
360
361 /**
362 Executes the given SQL code, calling callback for
363 each row of the data set. The data pointer is
364 passed on as-is to the callback, and may be 0. If
365 execution generates an error message it is stored
366 in errmsg.
367
368 If this function intercepts an exception (thrown
369 from the callback) then it propagates that
370 exception back to the caller. If it catches no
371 exception, it returns the result code, with zero
372 being success and non-zero being failure.
373
374 See sqlite3_exec() for more details.
375 */
376 int executecallback( std::string const & sql, sqlite3_callback callback, void * data, std::string & errmsg );
377
378 /**
379 Convenience overload which has a default data value
380 of 0 and ignores any error string passed back by
381 sqlite3_exec().
382 */
383 int executecallback( std::string const & sql, sqlite3_callback callback, void * data = 0 );
384
385 /**
386 Returns the equivalent of sqlite3_errmsg(), or an
387 empty string if that function returns
388 null. Reminder: the sqlite3 docs say that
389 sqlite3_errmsg() always returns a non-empty string,
390 even if the string is "not an error" (no joke).
391 */
392 std::string errormsg() const;
393
394#if SQLITE3X_USE_WCHAR
395 public:
396 explicit sqlite3_connection(const wchar_t *dbname);
397 void executenonquery(const std::wstring &sql);
398 int executeint(const std::wstring &sql);
399 int64_t executeint64(const std::wstring &sql);
400 double executedouble(const std::wstring &sql);
401 std::string executestring(const std::wstring &sql);
402 std::wstring executestring16(const std::wstring &sql);
403 std::wstring executestring16(const std::string &sql);
404 std::string executeblob(const std::wstring &sql);
405 void open(const wchar_t *dbname);
406#endif
407
408 protected:
409 /**
410 This function is called when open() succeeds. Subclasses
411 which wish to do custom db initialization or sanity checks
412 may do them here.
413 */
414 virtual void on_open();
415
416 };
417
418 /**
419 Manages an sqlite3 transaction. Remember that sqlite3 does not
420 support nested transactions.
421
422 All functions of this class throw on error.
423 */
425 private:
426 // copy operations not implemented
427 sqlite3_transaction & operator=( sqlite3_transaction const & );
430 bool intrans;
431
432 public:
433 /**
434 Opens a transaction for the given connection. If
435 start==true (the default) then this->begin() is
436 called.
437 */
438 sqlite3_transaction(sqlite3_connection &con, bool start=true);
439
440 /** If destructed before commit() is called,
441 rollback() is called.
442 */
444
445 /** Starts a transaction. */
446 void begin();
447 /** Commits a transaction. */
448 void commit();
449 /** Rolls back a transaction with a commit. */
450 void rollback();
451 };
452
453 class sqlite3_command;
454
455 /**
456 A type for reading results from an sqlite3_command.
457 */
459 private:
460 friend class sqlite3_command;
461
462 sqlite3_command *cmd;
463
464
465 public:
466 /**
467 Creates a cursor by calling cmd->executecursor().
468 */
470 /**
471 Creates an empty cursor object, suitable only
472 for use as the target of a copy/assignment.
473 */
475 /**
476 Copies the given cursor object. This is a fairly
477 efficient operation, using reference counting.
478 */
479 sqlite3_cursor(const sqlite3_cursor &copy);
480
481 /**
482 Closes this cursor, freeing up db resources if this
483 is the last cursor of a copied set.
484 */
486
487 /**
488 Copies the given cursor object. This is a fairly
489 efficient operation, using reference counting. This
490 object points to the same underlying result set as
491 the original, so both objects should not be used.
492 */
494
495 /**
496 Steps one step through the sql result set and returns
497 true on SQLITE_ROW, false on SQLITE3_DONE, and throws
498 on any other result.
499 */
500 bool step();
501
502 /** Resets the underlying prepared statement of
503 this cursor. Throws on error.
504 */
505 void reset();
506
507 /**
508 Closes this cursor. Calling it multiple times is a
509 no-op on the second and subsequent calls.
510 */
511 void close();
512
513 /**
514 Returns the column count of the result set or
515 throws on error.
516 */
517 int colcount();
518
519 /**
520 Check if the given field number is NULL. This function
521 returns true if is NULL, else false.
522 */
523 bool isnull(int index);
524
525
526 /**
527 Gets the integer value at the given field number.
528 */
529 int getint(int index);
530
531 /**
532 Gets the (int64_t) value at the given field number.
533 */
534 int64_t getint64(int index);
535
536 /**
537 Gets the double value at the given field number.
538 */
539 double getdouble(int index);
540
541 /**
542 Gets the string value at the given field number.
543 */
544 std::string getstring(int index);
545 /**
546 Like getstring(index), but returns a C-style
547 string. We hope it is null-terminated, but the
548 sqlite3 docs are ambiguous on this point. size
549 is set to the length of the returned string.
550
551 The advantage of this over getstring(index) is that
552 this version avoids a potential extra internal copy
553 of the string. Note that there is no guaranty how
554 long this pointer will remain valid - be sure to
555 copy the string if you need it.
556 */
557 char const * getstring(int index, int & size);
558
559
560 /**
561 Gets the blob value at the given field number.
562 */
563 std::string getblob(int index);
564
565 /**
566 Overloaded to avoid an internal copy of the blob data.
567
568 size is set to the number of bytes in the blob and
569 the returned pointer is the blob.
570 */
571 void const * getblob(int index, int & size );
572
573 /**
574 Gets the column name for the given column index.
575 Throws on error.
576 */
577 std::string getcolname(int index);
578
579
580
581#if SQLITE3X_USE_WCHAR
582 std::wstring getstring16(int index);
583 std::wstring getcolname16(int index);
584#endif
585
586 };
587
588
589 /**
590 Encapsulates a command to send to an sqlite3_connection.
591 */
593 private:
594 // copy operations not implemented
595 sqlite3_command & operator=( sqlite3_command const & );
597 friend class sqlite3_cursor;
598
600 mutable sqlite3_stmt *stmt;
601 unsigned int refs;
602 int argc;
603
604 public:
605 /**
606 Creates an unprepared statement. Use prepare()
607 create the statement.
608 */
609 explicit sqlite3_command(sqlite3_connection &con);
610
611 /**
612 Creates an sql statement with the given connection object
613 and sql code.
614 */
615 sqlite3_command(sqlite3_connection &con, const std::string &sql);
616
617 /**
618 An efficiency overload to avoid an extra copy of the sql
619 code. len must be the length of sql.
620 */
621 sqlite3_command(sqlite3_connection &con, char const * sql, size_t len);
622
623 /**
624 Cleans up any resources in use by this object.
625 */
627
628 /**
629 Prepares this statement or throws on error. If len
630 is -1 then sql is assumed to be null-terminated.
631 */
632 void prepare( char const * sql, int len = -1 );
633 /**
634 Convenience overload taking a std::string.
635 */
636 void prepare( std::string const & sql );
637
638 /**
639 Binds NULL to the given index.
640 */
641 void bind(int index);
642 /**
643 Binds data to the given query index.
644 */
645 void bind(int index, int data);
646 /**
647 Binds data to the given query index.
648 */
649 void bind(int index, int64_t data);
650 /**
651 Binds data to the given query index.
652 */
653 void bind(int index, double data);
654 /**
655 Binds data to the given query index. Data must be
656 exactly datalen bytes long. If datalen == -1 then
657 strlen(data) is used to calculate it.
658 */
659 void bind(int index, const char *data, int datalen = -1);
660
661 /**
662 Binds data to the given query index. Data must be
663 exactly datalen bytes long.
664 */
665 void bind(int index, const void *data, int datalen);
666 /**
667 Binds data to the given query index.
668 */
669 void bind(int index, const std::string &data);
670
671 /** Executes the query and returns a cursor object
672 which can be used to iterate over the results.
673 */
675 /**
676 Executes the query and provides no way to get
677 the results. Throws on error.
678 */
679 void executenonquery();
680 /**
681 Executes the query, which is expected to have an
682 integer field as the first result field.
683 */
684 int executeint();
685 /**
686 Executes the query, which is expected to have a
687 (int64_t) field as the first result field.
688 */
690 /**
691 Executes the query, which is expected to have a
692 double field as the first result field.
693 */
694 double executedouble();
695 /**
696 Executes the query, which is expected to have a
697 string or blob field as the first result field. Note
698 that numeric results can be returned using this function,
699 but will come back as a string (lexically cast).
700 */
701 std::string executestring();
702 /**
703 Like executestring(), but returns a C-style
704 string. We hope it is null-terminated, but the
705 sqlite3 docs are ambiguous on this point. size
706 is set to the length of the returned string.
707
708 The advantage of this over executestring() is that
709 this version avoids a potential extra internal copy
710 of the string. Note that there is no guaranty how
711 long this pointer will remain valid - be sure to
712 copy the string if you need it.
713 */
714 char const * executestring( int & size );
715
716 /**
717 Executes the query, which is expected to have a
718 string or blob field as the first result field. Note
719 that numeric results can be returned using this function,
720 but will come back as a string (lexically cast).
721 */
722 std::string executeblob();
723
724 /**
725 Like executeblob(), but returns a void pointer to
726 the data. size is set to the length of the returned
727 data.
728
729 The advantage of this over executeblob() is that
730 this version avoids a potential extra internal copy
731 of the string and "should work" on wide-char
732 strings. Note that there is no guaranty how long
733 this pointer will remain valid - be sure to copy it
734 if you need it for very long.
735 */
736 void const * executeblob(int & size );
737
738 /**
739 Returns the column count of this object's query,
740 or throws on error.
741 */
742 int colcount();
743
744 /** Resets this statement using sqlite3_reset().
745 Errors are considered to be minor and only cause false
746 to be returned.
747 */
748 bool reset();
749
750
751 /**
752 Returns the underlying statement handle. It is not legal to
753 finalize this statement handle, as that will put this object
754 out of sync with the state of the handle.
755 */
756 sqlite3_stmt * handle();
757
758 /**
759 Finalizes this statement. Throws if finalization fails.
760 Calling finalize() multiple times is a no-op.
761 */
762 void finalize();
763
764#if SQLITE3X_USE_WCHAR
765 sqlite3_command(sqlite3_connection &con, const std::wstring &sql);
766 void bind(int index, const wchar_t *data, int datalen);
767 void bind(int index, const std::wstring &data);
768 std::wstring executestring16();
769#endif // SQLITE3_USE_WCHAR
770
771 };
772
773
774 /**
775 Exception type used by the sqlite3x classes.
776 */
777 class database_error : public std::exception {
778 public:
779 /**
780 Takes a format specifier compatible with printf.
781
782 If the message length surpasses a hard-coded limit (2k?)
783 then it is truncated to fit within that limit.
784 */
785 explicit database_error(const char *format, ... );
786
787 /**
788 Creates an exception with con.errormsg()
789 as the what() text.
790 */
792
793 virtual ~database_error() throw();
794
795 /**
796 Returns this object's error string.
797 */
798 virtual char const * what() const throw();
799 private:
800 std::string m_what;
801 };
802
803
804// /**
805// EXPERIMENTAL.
806
807// A helper type for storing information on
808// functions to register with sqlite.
809// */
810// struct sqlite3_function_info_base
811// {
812// public:
813// enum {
814// TextUTF8 = SQLITE_UTF8,
815// TextUTF16 = SQLITE_UTF16,
816// TextUTF16BE = SQLITE_UTF16BE,
817// TextUTF16LE = SQLITE_UTF16LE,
818// TextAny = SQLITE_ANY
819// };
820// int argc;
821// int text_rep; /* 1: UTF-16. 0: UTF-8 */
822// void * user_data;
823// void (*func)(sqlite3_context*,int,sqlite3_value**);
824// void (*step)(sqlite3_context*,int,sqlite3_value**);
825// void (*final)(sqlite3_context*);
826// protected:
827// sqlite3_function_info_base()
828// : argc(0),
829// text_rep(TextUTF8),
830// user_data(0),
831// func(0), step(0), final(0)
832// {}
833
834// virtual ~sqlite3_function_info_base() {}
835
836// virtual int create( sqlite3 * db ) = 0;
837// };
838
839// /**
840// EXPERIMENTAL.
841// */
842// struct sqlite3_function_info8 : sqlite3_function_info_base
843// {
844// const char * name;
845// explicit sqlite3_function_info8( char const * n )
846// : sqlite3_function_info_base(),
847// name(n)
848// {
849// this->text_rep = TextUTF8;
850// }
851// virtual ~sqlite3_function_info8(){}
852// virtual int create( sqlite3 * db );
853// };
854
855// /**
856// EXPERIMENTAL.
857// */
858// struct sqlite3_function_info16 : sqlite3_function_info_base
859// {
860// void const * name;
861// explicit sqlite3_function_info16( void const * n )
862// : sqlite3_function_info_base(),
863// name(n)
864// {
865// this->text_rep = TextUTF16;
866// }
867// virtual ~sqlite3_function_info16(){}
868// virtual int create( sqlite3 * db );
869// };
870
871 /**
872 A helper class to generate db tables.
873
874 It is used like so:
875
876 table_generator( connection, "table_name" )( "field1" )( "field2" )("field3").create();
877
878 That creates the named table with the given fields. It
879 throws if table_name already exists in the db or if
880 creation of the table fails.
881
882 An arbitrary number of fields can be added using
883 operator()(string), up to the internal limits set by
884 sqlite3.
885 */
887 {
888 private:
890 table_generator_impl * m_pimpl;
891 public:
892 /**
893 Initializes the table generation process. Throws if
894 con contains a table with the same name.
895 */
896 explicit table_generator( sqlite3_connection & con, std::string const & name );
897
898 /** Frees up internal resources. */
899 ~table_generator() throw();
900
901 /**
902 Adds field_name as a field of this table. Checks
903 for duplicate field names are deferred until
904 create() is called.
905 */
906 table_generator & operator()( std::string const & field_name );
907
908 /**
909 Executes the 'create table' statements. Throws on error.
910 */
911 void create();
912 };
913
914}
915
916#endif // s11n_net_SQLITE3X_HPP_INCLUDED
Exception type used by the sqlite3x classes.
Definition sqlite3x.hpp:777
database_error(const char *format,...)
Takes a format specifier compatible with printf.
virtual char const * what() const
Returns this object's error string.
Encapsulates a command to send to an sqlite3_connection.
Definition sqlite3x.hpp:592
int executeint()
Executes the query, which is expected to have an integer field as the first result field.
~sqlite3_command()
Cleans up any resources in use by this object.
bool reset()
Resets this statement using sqlite3_reset().
void executenonquery()
Executes the query and provides no way to get the results.
std::string executeblob()
Executes the query, which is expected to have a string or blob field as the first result field.
void prepare(char const *sql, int len=-1)
Prepares this statement or throws on error.
void finalize()
Finalizes this statement.
int colcount()
Returns the column count of this object's query, or throws on error.
sqlite3_cursor executecursor()
Executes the query and returns a cursor object which can be used to iterate over the results.
double executedouble()
Executes the query, which is expected to have a double field as the first result field.
std::string executestring()
Executes the query, which is expected to have a string or blob field as the first result field.
int64_t executeint64()
Executes the query, which is expected to have a (int64_t) field as the first result field.
sqlite3_stmt * handle()
Returns the underlying statement handle.
void bind(int index)
Binds NULL to the given index.
Represents a connection to an sqlite3 database.
Definition sqlite3x.hpp:149
void close()
Closes this database.
sqlite3 * db() const
Returns a handle to the underlying sqlite3 database.
void executenonquery(const std::string &sql)
Executes a command which is assumed to have a single step and a void result.
virtual void open(char const *)
Creates/opens the given db, throwing on error.
int64_t executeint64(const std::string &sql)
Executes the query, which is expected to have a (int64_t) field as the first result field.
int64_t insertid()
Returns the rowid of the most recently inserted row on this db.
std::string name() const
Returns this object's name.
int changes()
Returns the number of database rows that were changed (or inserted or deleted) by the most recently c...
sqlite3 * take()
Transfers ownership of the returned handle to the caller.
virtual ~sqlite3_connection()
Calls this->close() if close() has not already been called.
std::string executeblob(const std::string &sql)
Executes the query, which is expected to have a string or blob field as the first result field.
double executedouble(const std::string &sql)
Executes the query, which is expected to have a double field as the first result field.
int executecallback(std::string const &sql, sqlite3_callback callback, void *data, std::string &errmsg)
Executes the given SQL code, calling callback for each row of the data set.
void setbusytimeout(int ms)
See sqlite3_busy_timeout().
std::string executestring(const std::string &sql)
Executes the query, which is expected to have a string or blob field as the first result field.
std::string errormsg() const
Returns the equivalent of sqlite3_errmsg(), or an empty string if that function returns null.
virtual void on_open()
This function is called when open() succeeds.
int executeint(const std::string &sql)
Executes the query, which is expected to have an integer field as the first result field.
A type for reading results from an sqlite3_command.
Definition sqlite3x.hpp:458
int getint(int index)
Gets the integer value at the given field number.
int64_t getint64(int index)
Gets the (int64_t) value at the given field number.
std::string getstring(int index)
Gets the string value at the given field number.
sqlite3_cursor()
Creates an empty cursor object, suitable only for use as the target of a copy/assignment.
void close()
Closes this cursor.
bool isnull(int index)
Check if the given field number is NULL.
~sqlite3_cursor()
Closes this cursor, freeing up db resources if this is the last cursor of a copied set.
int colcount()
Returns the column count of the result set or throws on error.
std::string getcolname(int index)
Gets the column name for the given column index.
bool step()
Steps one step through the sql result set and returns true on SQLITE_ROW, false on SQLITE3_DONE,...
sqlite3_cursor & operator=(const sqlite3_cursor &copy)
Copies the given cursor object.
void reset()
Resets the underlying prepared statement of this cursor.
std::string getblob(int index)
Gets the blob value at the given field number.
double getdouble(int index)
Gets the double value at the given field number.
Manages an sqlite3 transaction.
Definition sqlite3x.hpp:424
~sqlite3_transaction()
If destructed before commit() is called, rollback() is called.
void begin()
Starts a transaction.
void commit()
Commits a transaction.
void rollback()
Rolls back a transaction with a commit.
An internal implementation detail of table_generator.
table_generator(sqlite3_connection &con, std::string const &name)
Initializes the table generation process.
~table_generator()
Frees up internal resources.
void create()
Executes the 'create table' statements.
table_generator & operator()(std::string const &field_name)
Adds field_name as a field of this table.
This namespace encapsulates a C++ API wrapper for sqlite3 databases.
Definition sqlite3x.hpp:120
sqlite_int64 int64_t
64-bit integer type used by this code.
Definition sqlite3x.hpp:125
bool rc_is_okay(int rc)
rc_is_okay() is an easy way to check if rc is one of SQLITE_OK, SQLITE_ROW, or SQLITE_DONE.