00001 #ifndef s11n_net_SQLITE3X_HPP_INCLUDED 00002 #define s11n_net_SQLITE3X_HPP_INCLUDED 00003 /* 00004 00005 Copyright (C) 2004-2005 Cory Nelson 00006 Copyright (C) 2006 stephan beal (stephan s11n net) 00007 00008 This software is provided 'as-is', without any express or implied 00009 warranty. In no event will the authors be held liable for any damages 00010 arising from the use of this software. 00011 00012 Permission is granted to anyone to use this software for any purpose, 00013 including commercial applications, and to alter it and redistribute it 00014 freely, subject to the following restrictions: 00015 00016 1. The origin of this software must not be misrepresented; you must not 00017 claim that you wrote the original software. If you use this software 00018 in a product, an acknowledgment in the product documentation would be 00019 appreciated but is not required. 00020 2. Altered source versions must be plainly marked as such, and must not be 00021 misrepresented as being the original software. 00022 3. This notice may not be removed or altered from any source distribution. 00023 00024 00025 This file has been modified from the original sources by <stephan at 00026 s11n net>, as described briefly below. 00027 00028 The original code, by Cory Nelson, is available from: 00029 00030 http://dev.int64.org/sqlite.html 00031 00032 This hacked copy's home is: 00033 00034 http://wanderinghorse.net/computing/sqlite/ 00035 00036 Contributors to the hacked version include: 00037 00038 stephan beal <stephan at s11n net> 00039 - Maintainer, documentor. 00040 00041 Thomas Sailer <t.sailer at alumni ethz ch>: 00042 - A fix for wide-char support on 64-bit Linux. 00043 00044 Artem Gr <artem at bizlink ru> 00045 - Fixes to enable/disable wide-char support with a macro. 00046 00047 - Xose Anton Otero Ferreira submitted patches to remove 'long long' 00048 decls and replace those with sqlite_int64. He also submitted 00049 the isnull() functions. 00050 00051 00052 Significant changes from the original sqlite3x distribution include: 00053 00054 - Removed dependency on boost library, since it was only a dependency 00055 on boost::non_copyable (this same effect is easily achieved without 00056 the dependency on the huge Boost library). 00057 00058 - Reordered some code to get it to compile under gcc. 00059 00060 - Added some missing #includes. 00061 00062 - database_error was reimplemented to allow use of a varargs ctor. 00063 00064 - Added a few helpful functions, like sqlite3_cursor::colcount(). 00065 00066 - Removed various (char const * ...) overloads which were inherently 00067 already covered by implicit conversions via (std::string const &) 00068 overloads. Re-added them on 2006.09.25 after Artem Gr pointed out that 00069 those overloads avoid a potential extra copy of the strings, and that 00070 this could be a significant performance factor for some applications. 00071 00072 - Added lots of API docs. 00073 00074 - Improved some exception messages. 00075 00076 - Added table_generator class. 00077 00078 - Added sqlite3_connection::executecallback(). 00079 00080 - sqlite3_cursor renamed to sqlite3_cursor (2007.01.22). 00081 00082 - Added sqlite3_cursor::close() 00083 00084 - sqlite3_cursor::read() renamed to step() (2007.01.22). 00085 00086 */ 00087 00088 #include <string> 00089 #include <stdexcept> 00090 #include <sqlite3.h> // only for sqlite3_callback :/ 00091 00092 // Enable WCHAR support when it's there. Thanks to Artem Gr <artem@bizlink.ru> 00093 // for this... 00094 #ifndef SQLITE3X_USE_WCHAR 00095 # ifdef _GLIBCXX_USE_WCHAR_T 00096 # define SQLITE3X_USE_WCHAR 1 00097 # elif defined(UNICODE) // Windows uses this 00098 # define SQLITE3X_USE_WCHAR 1 00099 # else 00100 # define SQLITE3X_USE_WCHAR 0 // default 00101 # endif 00102 #endif 00103 00104 /** 00105 This namespace encapsulates a C++ API wrapper for sqlite3 00106 databases. It was originally written by Cory Nelson and was hacked 00107 slightly by stephan beal. 00108 00109 The home page for the original sources note that all of the 00110 w_char/wstring functions *probably* only work on Windows 00111 platforms. Your mileage may vary on other platforms. Users of this 00112 API are recommended to NOT use the wchar_t/wstring variants of any 00113 functions, as those functions may be removed at some point. 00114 00115 00116 Note that this API does not include support for all sqlite3 00117 features. However, the most commonly used features are available. 00118 00119 */ 00120 namespace sqlite3x { 00121 00122 /** 00123 64-bit integer type used by this code. 00124 */ 00125 typedef sqlite_int64 int64_t; 00126 00127 class sqlite3_command; 00128 00129 /** 00130 rc_is_okay() is an easy way to check if rc is one of 00131 SQLITE_OK, SQLITE_ROW, or SQLITE_DONE. This function 00132 returns true if rc is one of those values, else false. 00133 When writing code which accepts arbitrary client-supplied 00134 SQL, any of those three codes can signal success, depending 00135 on the SQL code and the context. 00136 */ 00137 bool rc_is_okay( int rc ); 00138 00139 00140 /** 00141 Represents a connection to an sqlite3 database. 00142 00143 About the only reason to subclass this type would be to do 00144 customizations to the underlying sqlite3 db handle upon 00145 construction of each object, e.g. to add custom sqlite 00146 functions or load custom modules. 00147 */ 00148 class sqlite3_connection 00149 { 00150 private: 00151 // copy operations not implemented 00152 sqlite3_connection & operator=( sqlite3_connection const & ); 00153 sqlite3_connection( sqlite3_connection const & ); 00154 00155 friend class sqlite3_command; 00156 00157 mutable struct sqlite3 *m_db; 00158 std::string m_name; 00159 00160 public: 00161 /** 00162 Returns a handle to the underlying sqlite3 00163 database. Friend classes should NEVER call 00164 sqlite3_close() on this handle. Doing so will 00165 result in undefined behaviour later on (when this 00166 class is used or destructs). 00167 00168 This function is only public so that clients can 00169 do things like register new sqlite3 functions 00170 with the database. 00171 */ 00172 sqlite3 * db() const; 00173 00174 /** 00175 Default ctor. DB is unusable until open() is 00176 called. 00177 */ 00178 sqlite3_connection(); 00179 00180 /** 00181 Opens a database with the given name. Throws if 00182 this->open(dbname) fails. 00183 */ 00184 explicit sqlite3_connection(std::string const & dbname); 00185 00186 /** 00187 See take(sqlite3*). This ctor is identical except 00188 that it throws if passed a null pointer. 00189 */ 00190 sqlite3_connection( sqlite3 * dbh ); 00191 00192 /** 00193 Calls this->close() if close() has not already 00194 been called. If it calls close() then the exception 00195 is silently ignored for the sake of having a no-throw 00196 dtor. 00197 */ 00198 virtual ~sqlite3_connection(); 00199 00200 /** Returns this object's name. It is only valid if 00201 the (char const *) ctor or open(char const *) was 00202 used with a non-null name, otherwise it is an 00203 empty string. 00204 */ 00205 std::string name() const; 00206 00207 00208 /** 00209 Creates/opens the given db, throwing on error. 00210 Remember that sqlite3 supports the name ":memory:" 00211 as a pseudo-name for an in-memory database. 00212 00213 On success it returns, else it throws. 00214 00215 Note that sqlite3 supports the special db name 00216 ":memory:" to represent an in-memory database. Such 00217 databases cannot be saved directly to disk and are 00218 lost when this object closes the db. 00219 00220 Internal notes: 00221 00222 Once an sqlite3_open() succeeds, the protected 00223 member this->on_open() in called. That member 00224 should throw on error. 00225 00226 Subclasses which override this and do not want to 00227 call the base implementation should call on_open() 00228 when done to allow subclasses to initialize the 00229 database if they like. 00230 */ 00231 virtual void open( char const * ); 00232 00233 /** 00234 Functionally the same as open( char const *). 00235 */ 00236 void open(std::string const &dbname); 00237 00238 /** 00239 00240 Transfers control of dbh to this object and makes 00241 this object point at dbh. dbh is assumed to be 00242 a valid, opened sqlite3 db handle. 00243 00244 If this->db() == dbh then this function 00245 does nothing. 00246 00247 If this object had an opened db handle 00248 then it is closed before dbh is taken. 00249 Closing may throw, but this function takes 00250 ownership of dbh regardless of whether 00251 it throws or not. 00252 00253 If dbh is null, the effect is identical 00254 to calling close(). 00255 00256 This function triggers the protected on_open() 00257 function if dbh is not null. 00258 */ 00259 void take( sqlite3 * dbh ); 00260 00261 /** 00262 Transfers ownership of the returned handle to the caller. 00263 This object is then considered closed. NULL is returned 00264 if this object is closed. 00265 */ 00266 sqlite3 * take() throw(); 00267 00268 00269 /** 00270 Closes this database. If the db is not opened, 00271 this is a no-op. 00272 */ 00273 void close(); 00274 00275 /** 00276 Returns the rowid of the most recently inserted row 00277 on this db. 00278 */ 00279 int64_t insertid(); 00280 00281 /** 00282 Returns the number of database rows that were 00283 changed (or inserted or deleted) by the most recently 00284 completed INSERT, UPDATE, or DELETE statement. 00285 00286 SQLite implements the command "DELETE FROM table" 00287 without a WHERE clause by dropping and recreating 00288 the table. To get an accurate count of the number 00289 of rows deleted, use "DELETE FROM table WHERE 1" 00290 instead. 00291 */ 00292 int changes(); 00293 00294 00295 /** 00296 See sqlite3_busy_timeout(). 00297 */ 00298 void setbusytimeout(int ms); 00299 00300 /** 00301 Executes a command which is assumed to have 00302 a single step and a void result. 00303 */ 00304 void executenonquery(const std::string &sql); 00305 /** 00306 Overloaded to avoid an internal copy of sql. 00307 sql MUST be non-NULL and null-terminated. 00308 */ 00309 void executenonquery(char const * sql); 00310 00311 /** 00312 Executes the query, which is expected to have an 00313 integer field as the first result field. 00314 */ 00315 int executeint(const std::string &sql); 00316 /** 00317 Overloaded to avoid an internal copy of sql. 00318 sql MUST be non-NULL and null-terminated. 00319 */ 00320 int executeint(char const * sql); 00321 00322 /** 00323 Executes the query, which is expected to have a 00324 (int64_t) field as the first result field. 00325 */ 00326 int64_t executeint64(const std::string &sql); 00327 /** 00328 Overloaded to avoid an internal copy of sql. 00329 sql MUST be non-NULL and null-terminated. 00330 */ 00331 int64_t executeint64(char const * sql); 00332 00333 /** 00334 Executes the query, which is expected to have a 00335 double field as the first result field. 00336 */ 00337 double executedouble(const std::string &sql); 00338 00339 /** 00340 Overloaded to avoid an internal copy of sql. 00341 sql MUST be non-NULL and null-terminated. 00342 */ 00343 double executedouble(char const * sql); 00344 00345 /** 00346 Executes the query, which is expected to have a 00347 string or blob field as the first result field. Note 00348 that numeric results can be returned using this function, 00349 but will come back as a string (lexically cast). 00350 */ 00351 std::string executestring(const std::string &sql); 00352 00353 /** 00354 Executes the query, which is expected to have a 00355 string or blob field as the first result field. Note 00356 that numeric results can be returned using this function, 00357 but will come back as a string (lexically cast). 00358 */ 00359 std::string executeblob(const std::string &sql); 00360 00361 /** 00362 Executes the given SQL code, calling callback for 00363 each row of the data set. The data pointer is 00364 passed on as-is to the callback, and may be 0. If 00365 execution generates an error message it is stored 00366 in errmsg. 00367 00368 If this function intercepts an exception (thrown 00369 from the callback) then it propagates that 00370 exception back to the caller. If it catches no 00371 exception, it returns the result code, with zero 00372 being success and non-zero being failure. 00373 00374 See sqlite3_exec() for more details. 00375 */ 00376 int executecallback( std::string const & sql, sqlite3_callback callback, void * data, std::string & errmsg ); 00377 00378 /** 00379 Convenience overload which has a default data value 00380 of 0 and ignores any error string passed back by 00381 sqlite3_exec(). 00382 */ 00383 int executecallback( std::string const & sql, sqlite3_callback callback, void * data = 0 ); 00384 00385 /** 00386 Returns the equivalent of sqlite3_errmsg(), or an 00387 empty string if that function returns 00388 null. Reminder: the sqlite3 docs say that 00389 sqlite3_errmsg() always returns a non-empty string, 00390 even if the string is "not an error" (no joke). 00391 */ 00392 std::string errormsg() const; 00393 00394 #if SQLITE3X_USE_WCHAR 00395 public: 00396 explicit sqlite3_connection(const wchar_t *dbname); 00397 void executenonquery(const std::wstring &sql); 00398 int executeint(const std::wstring &sql); 00399 int64_t executeint64(const std::wstring &sql); 00400 double executedouble(const std::wstring &sql); 00401 std::string executestring(const std::wstring &sql); 00402 std::wstring executestring16(const std::wstring &sql); 00403 std::wstring executestring16(const std::string &sql); 00404 std::string executeblob(const std::wstring &sql); 00405 void open(const wchar_t *dbname); 00406 #endif 00407 00408 protected: 00409 /** 00410 This function is called when open() succeeds. Subclasses 00411 which wish to do custom db initialization or sanity checks 00412 may do them here. 00413 */ 00414 virtual void on_open(); 00415 00416 }; 00417 00418 /** 00419 Manages an sqlite3 transaction. Remember that sqlite3 does not 00420 support nested transactions. 00421 00422 All functions of this class throw on error. 00423 */ 00424 class sqlite3_transaction { 00425 private: 00426 // copy operations not implemented 00427 sqlite3_transaction & operator=( sqlite3_transaction const & ); 00428 sqlite3_transaction( sqlite3_transaction const & ); 00429 sqlite3_connection &con; 00430 bool intrans; 00431 00432 public: 00433 /** 00434 Opens a transaction for the given connection. If 00435 start==true (the default) then this->begin() is 00436 called. 00437 */ 00438 sqlite3_transaction(sqlite3_connection &con, bool start=true); 00439 00440 /** If destructed before commit() is called, 00441 rollback() is called. 00442 */ 00443 ~sqlite3_transaction(); 00444 00445 /** Starts a transaction. */ 00446 void begin(); 00447 /** Commits a transaction. */ 00448 void commit(); 00449 /** Rolls back a transaction with a commit. */ 00450 void rollback(); 00451 }; 00452 00453 class sqlite3_command; 00454 00455 /** 00456 A type for reading results from an sqlite3_command. 00457 */ 00458 class sqlite3_cursor { 00459 private: 00460 friend class sqlite3_command; 00461 00462 sqlite3_command *cmd; 00463 00464 00465 public: 00466 /** 00467 Creates a cursor by calling cmd->executecursor(). 00468 */ 00469 sqlite3_cursor(sqlite3_command & cmd); 00470 /** 00471 Creates an empty cursor object, suitable only 00472 for use as the target of a copy/assignment. 00473 */ 00474 sqlite3_cursor(); 00475 /** 00476 Copies the given cursor object. This is a fairly 00477 efficient operation, using reference counting. 00478 */ 00479 sqlite3_cursor(const sqlite3_cursor ©); 00480 00481 /** 00482 Closes this cursor, freeing up db resources if this 00483 is the last cursor of a copied set. 00484 */ 00485 ~sqlite3_cursor(); 00486 00487 /** 00488 Copies the given cursor object. This is a fairly 00489 efficient operation, using reference counting. This 00490 object points to the same underlying result set as 00491 the original, so both objects should not be used. 00492 */ 00493 sqlite3_cursor& operator=(const sqlite3_cursor ©); 00494 00495 /** 00496 Steps one step through the sql result set and returns 00497 true on SQLITE_ROW, false on SQLITE3_DONE, and throws 00498 on any other result. 00499 */ 00500 bool step(); 00501 00502 /** Resets the underlying prepared statement of 00503 this cursor. Throws on error. 00504 */ 00505 void reset(); 00506 00507 /** 00508 Closes this cursor. Calling it multiple times is a 00509 no-op on the second and subsequent calls. 00510 */ 00511 void close(); 00512 00513 /** 00514 Returns the column count of the result set or 00515 throws on error. 00516 */ 00517 int colcount(); 00518 00519 /** 00520 Check if the given field number is NULL. This function 00521 returns true if is NULL, else false. 00522 */ 00523 bool isnull(int index); 00524 00525 00526 /** 00527 Gets the integer value at the given field number. 00528 */ 00529 int getint(int index); 00530 00531 /** 00532 Gets the (int64_t) value at the given field number. 00533 */ 00534 int64_t getint64(int index); 00535 00536 /** 00537 Gets the double value at the given field number. 00538 */ 00539 double getdouble(int index); 00540 00541 /** 00542 Gets the string value at the given field number. 00543 */ 00544 std::string getstring(int index); 00545 /** 00546 Like getstring(index), but returns a C-style 00547 string. We hope it is null-terminated, but the 00548 sqlite3 docs are ambiguous on this point. size 00549 is set to the length of the returned string. 00550 00551 The advantage of this over getstring(index) is that 00552 this version avoids a potential extra internal copy 00553 of the string. Note that there is no guaranty how 00554 long this pointer will remain valid - be sure to 00555 copy the string if you need it. 00556 */ 00557 char const * getstring(int index, int & size); 00558 00559 00560 /** 00561 Gets the blob value at the given field number. 00562 */ 00563 std::string getblob(int index); 00564 00565 /** 00566 Overloaded to avoid an internal copy of the blob data. 00567 00568 size is set to the number of bytes in the blob and 00569 the returned pointer is the blob. 00570 */ 00571 void const * getblob(int index, int & size ); 00572 00573 /** 00574 Gets the column name for the given column index. 00575 Throws on error. 00576 */ 00577 std::string getcolname(int index); 00578 00579 00580 00581 #if SQLITE3X_USE_WCHAR 00582 std::wstring getstring16(int index); 00583 std::wstring getcolname16(int index); 00584 #endif 00585 00586 }; 00587 00588 00589 /** 00590 Encapsulates a command to send to an sqlite3_connection. 00591 */ 00592 class sqlite3_command { 00593 private: 00594 // copy operations not implemented 00595 sqlite3_command & operator=( sqlite3_command const & ); 00596 sqlite3_command( sqlite3_command const & ); 00597 friend class sqlite3_cursor; 00598 00599 sqlite3_connection &con; 00600 mutable sqlite3_stmt *stmt; 00601 unsigned int refs; 00602 int argc; 00603 00604 public: 00605 /** 00606 Creates an unprepared statement. Use prepare() 00607 create the statement. 00608 */ 00609 explicit sqlite3_command(sqlite3_connection &con); 00610 00611 /** 00612 Creates an sql statement with the given connection object 00613 and sql code. 00614 */ 00615 sqlite3_command(sqlite3_connection &con, const std::string &sql); 00616 00617 /** 00618 An efficiency overload to avoid an extra copy of the sql 00619 code. len must be the length of sql. 00620 */ 00621 sqlite3_command(sqlite3_connection &con, char const * sql, size_t len); 00622 00623 /** 00624 Cleans up any resources in use by this object. 00625 */ 00626 ~sqlite3_command(); 00627 00628 /** 00629 Prepares this statement or throws on error. If len 00630 is -1 then sql is assumed to be null-terminated. 00631 */ 00632 void prepare( char const * sql, int len = -1 ); 00633 /** 00634 Convenience overload taking a std::string. 00635 */ 00636 void prepare( std::string const & sql ); 00637 00638 /** 00639 Binds NULL to the given index. 00640 */ 00641 void bind(int index); 00642 /** 00643 Binds data to the given query index. 00644 */ 00645 void bind(int index, int data); 00646 /** 00647 Binds data to the given query index. 00648 */ 00649 void bind(int index, int64_t data); 00650 /** 00651 Binds data to the given query index. 00652 */ 00653 void bind(int index, double data); 00654 /** 00655 Binds data to the given query index. Data must be 00656 exactly datalen bytes long. If datalen == -1 then 00657 strlen(data) is used to calculate it. 00658 */ 00659 void bind(int index, const char *data, int datalen = -1); 00660 00661 /** 00662 Binds data to the given query index. Data must be 00663 exactly datalen bytes long. 00664 */ 00665 void bind(int index, const void *data, int datalen); 00666 /** 00667 Binds data to the given query index. 00668 */ 00669 void bind(int index, const std::string &data); 00670 00671 /** Executes the query and returns a cursor object 00672 which can be used to iterate over the results. 00673 */ 00674 sqlite3_cursor executecursor(); 00675 /** 00676 Executes the query and provides no way to get 00677 the results. Throws on error. 00678 */ 00679 void executenonquery(); 00680 /** 00681 Executes the query, which is expected to have an 00682 integer field as the first result field. 00683 */ 00684 int executeint(); 00685 /** 00686 Executes the query, which is expected to have a 00687 (int64_t) field as the first result field. 00688 */ 00689 int64_t executeint64(); 00690 /** 00691 Executes the query, which is expected to have a 00692 double field as the first result field. 00693 */ 00694 double executedouble(); 00695 /** 00696 Executes the query, which is expected to have a 00697 string or blob field as the first result field. Note 00698 that numeric results can be returned using this function, 00699 but will come back as a string (lexically cast). 00700 */ 00701 std::string executestring(); 00702 /** 00703 Like executestring(), but returns a C-style 00704 string. We hope it is null-terminated, but the 00705 sqlite3 docs are ambiguous on this point. size 00706 is set to the length of the returned string. 00707 00708 The advantage of this over executestring() is that 00709 this version avoids a potential extra internal copy 00710 of the string. Note that there is no guaranty how 00711 long this pointer will remain valid - be sure to 00712 copy the string if you need it. 00713 */ 00714 char const * executestring( int & size ); 00715 00716 /** 00717 Executes the query, which is expected to have a 00718 string or blob field as the first result field. Note 00719 that numeric results can be returned using this function, 00720 but will come back as a string (lexically cast). 00721 */ 00722 std::string executeblob(); 00723 00724 /** 00725 Like executeblob(), but returns a void pointer to 00726 the data. size is set to the length of the returned 00727 data. 00728 00729 The advantage of this over executeblob() is that 00730 this version avoids a potential extra internal copy 00731 of the string and "should work" on wide-char 00732 strings. Note that there is no guaranty how long 00733 this pointer will remain valid - be sure to copy it 00734 if you need it for very long. 00735 */ 00736 void const * executeblob(int & size ); 00737 00738 /** 00739 Returns the column count of this object's query, 00740 or throws on error. 00741 */ 00742 int colcount(); 00743 00744 /** Resets this statement using sqlite3_reset(). 00745 Errors are considered to be minor and only cause false 00746 to be returned. 00747 */ 00748 bool reset(); 00749 00750 00751 /** 00752 Returns the underlying statement handle. It is not legal to 00753 finalize this statement handle, as that will put this object 00754 out of sync with the state of the handle. 00755 */ 00756 sqlite3_stmt * handle(); 00757 00758 /** 00759 Finalizes this statement. Throws if finalization fails. 00760 Calling finalize() multiple times is a no-op. 00761 */ 00762 void finalize(); 00763 00764 #if SQLITE3X_USE_WCHAR 00765 sqlite3_command(sqlite3_connection &con, const std::wstring &sql); 00766 void bind(int index, const wchar_t *data, int datalen); 00767 void bind(int index, const std::wstring &data); 00768 std::wstring executestring16(); 00769 #endif // SQLITE3_USE_WCHAR 00770 00771 }; 00772 00773 00774 /** 00775 Exception type used by the sqlite3x classes. 00776 */ 00777 class database_error : public std::exception { 00778 public: 00779 /** 00780 Takes a format specifier compatible with printf. 00781 00782 If the message length surpasses a hard-coded limit (2k?) 00783 then it is truncated to fit within that limit. 00784 */ 00785 explicit database_error(const char *format, ... ); 00786 00787 /** 00788 Creates an exception with con.errormsg() 00789 as the what() text. 00790 */ 00791 database_error(sqlite3_connection &con); 00792 00793 virtual ~database_error() throw(); 00794 00795 /** 00796 Returns this object's error string. 00797 */ 00798 virtual char const * what() const throw(); 00799 private: 00800 std::string m_what; 00801 }; 00802 00803 00804 // /** 00805 // EXPERIMENTAL. 00806 00807 // A helper type for storing information on 00808 // functions to register with sqlite. 00809 // */ 00810 // struct sqlite3_function_info_base 00811 // { 00812 // public: 00813 // enum { 00814 // TextUTF8 = SQLITE_UTF8, 00815 // TextUTF16 = SQLITE_UTF16, 00816 // TextUTF16BE = SQLITE_UTF16BE, 00817 // TextUTF16LE = SQLITE_UTF16LE, 00818 // TextAny = SQLITE_ANY 00819 // }; 00820 // int argc; 00821 // int text_rep; /* 1: UTF-16. 0: UTF-8 */ 00822 // void * user_data; 00823 // void (*func)(sqlite3_context*,int,sqlite3_value**); 00824 // void (*step)(sqlite3_context*,int,sqlite3_value**); 00825 // void (*final)(sqlite3_context*); 00826 // protected: 00827 // sqlite3_function_info_base() 00828 // : argc(0), 00829 // text_rep(TextUTF8), 00830 // user_data(0), 00831 // func(0), step(0), final(0) 00832 // {} 00833 00834 // virtual ~sqlite3_function_info_base() {} 00835 00836 // virtual int create( sqlite3 * db ) = 0; 00837 // }; 00838 00839 // /** 00840 // EXPERIMENTAL. 00841 // */ 00842 // struct sqlite3_function_info8 : sqlite3_function_info_base 00843 // { 00844 // const char * name; 00845 // explicit sqlite3_function_info8( char const * n ) 00846 // : sqlite3_function_info_base(), 00847 // name(n) 00848 // { 00849 // this->text_rep = TextUTF8; 00850 // } 00851 // virtual ~sqlite3_function_info8(){} 00852 // virtual int create( sqlite3 * db ); 00853 // }; 00854 00855 // /** 00856 // EXPERIMENTAL. 00857 // */ 00858 // struct sqlite3_function_info16 : sqlite3_function_info_base 00859 // { 00860 // void const * name; 00861 // explicit sqlite3_function_info16( void const * n ) 00862 // : sqlite3_function_info_base(), 00863 // name(n) 00864 // { 00865 // this->text_rep = TextUTF16; 00866 // } 00867 // virtual ~sqlite3_function_info16(){} 00868 // virtual int create( sqlite3 * db ); 00869 // }; 00870 00871 /** 00872 A helper class to generate db tables. 00873 00874 It is used like so: 00875 00876 table_generator( connection, "table_name" )( "field1" )( "field2" )("field3").create(); 00877 00878 That creates the named table with the given fields. It 00879 throws if table_name already exists in the db or if 00880 creation of the table fails. 00881 00882 An arbitrary number of fields can be added using 00883 operator()(string), up to the internal limits set by 00884 sqlite3. 00885 */ 00886 class table_generator 00887 { 00888 private: 00889 class table_generator_impl; 00890 table_generator_impl * m_pimpl; 00891 public: 00892 /** 00893 Initializes the table generation process. Throws if 00894 con contains a table with the same name. 00895 */ 00896 explicit table_generator( sqlite3_connection & con, std::string const & name ); 00897 00898 /** Frees up internal resources. */ 00899 ~table_generator() throw(); 00900 00901 /** 00902 Adds field_name as a field of this table. Checks 00903 for duplicate field names are deferred until 00904 create() is called. 00905 */ 00906 table_generator & operator()( std::string const & field_name ); 00907 00908 /** 00909 Executes the 'create table' statements. Throws on error. 00910 */ 00911 void create(); 00912 }; 00913 00914 } 00915 00916 #endif // s11n_net_SQLITE3X_HPP_INCLUDED