|
2 | 2 | ==============
|
3 | 3 |
|
4 | 4 | `Eggs.SQLite` is a thin wrapper over [`SQLite`][sqlite], written following
|
5 |
| -_Modern **C++**_ style, and makes extensive use of the _C++ Standard Library_ |
| 5 | +_Modern **C++**_ style and making extensive use of the _C++ Standard Library_ |
6 | 6 | as well as the [_Boost C++ Libraries_][boost].
|
7 | 7 |
|
8 | 8 | The library is in its early development stages, and currently provides no
|
9 | 9 | examples, tests nor documentation. Its use in a production environment is not
|
10 | 10 | recommended at the time.
|
11 | 11 |
|
12 |
| -> Copyright Agustín Bergé, Fusion Fenix 2012 |
| 12 | +[sqlite]: http://sqlite.org/ "SQLite Home Page" |
| 13 | +[boost]: http://boost.org/ "Boost C++ Libraries" |
| 14 | + |
| 15 | +# Basic usage # |
| 16 | + |
| 17 | +## Main components ## |
| 18 | + |
| 19 | +The main components in the library are the `database` that handles the |
| 20 | +connection to a `SQLite` database, and the `statement`s that handles the |
| 21 | +execution of `SQL` statements. |
| 22 | + |
| 23 | + #include <eggs/sqlite/sqlite.hpp> |
| 24 | + namespace sqlite = eggs::sqlite; |
| 25 | + |
| 26 | + ... |
| 27 | + |
| 28 | + sqlite::database books_db( "books.db" ); |
| 29 | + sqlite::istatement books_by_author( |
| 30 | + books_db |
| 31 | + , "SELECT title, year FROM books " |
| 32 | + "WHERE author=:author " |
| 33 | + "ORDER BY year ASC" |
| 34 | + ); |
| 35 | + |
| 36 | +Statements are loosely based on standard streams; an `istatement` reads back |
| 37 | +the results of a `SELECT` statements, and a `ostatement` writes the fields of |
| 38 | +an `INSERT` or `UPDATE` statement. |
| 39 | + |
| 40 | +## Working with data ## |
| 41 | + |
| 42 | +#### core access #### |
| 43 | + |
| 44 | +Input and output statements respectively have a `get` and `put` member |
| 45 | +function providing core access to the statement data. The most basic usage |
| 46 | +includes stepping through a statement (using the member function `step`) and |
| 47 | +retrieving the data using `get`. _Example:_ |
| 48 | + |
| 49 | + books_by_author["author"] = "Bjarne Stroustrup"; |
| 50 | + { |
| 51 | + books_by_author.step(); |
| 52 | + std::cout |
| 53 | + << "title: " << books_by_author.get< std::string >( 0 ) << ", " |
| 54 | + << "year: " << books_by_author.get< int >( 1 ) |
| 55 | + ; |
| 56 | + } |
| 57 | + books_by_author.reset(); |
| 58 | + |
| 59 | +#### row objects #### |
| 60 | + |
| 61 | +Statements only hold valid data for the row they are pointing to. Once a |
| 62 | +statement is stepped, previous data becames inaccessible. In particular, for |
| 63 | +columns of type _text_ and _blob_ their buffers became invalidated. The `row` |
| 64 | +object provides a way to extract all data from a statement, data that will not |
| 65 | +be invalidate as the statement is stepped. Otherwise `row`s behave exactly |
| 66 | +like `statement`s. _Example:_ |
| 67 | + |
| 68 | + books_by_author["author"] = "Bjarne Stroustrup"; |
| 69 | + { |
| 70 | + sqlite::row row_results; |
| 71 | + |
| 72 | + books_by_author >> row_results; |
| 73 | + std::cout |
| 74 | + << "title: " << row_results.get< std::string >( 0 ) << ", " |
| 75 | + << "year: " << row_results.get< int >( 1 ) |
| 76 | + ; |
| 77 | + } |
| 78 | + books_by_author.reset(); |
| 79 | + |
| 80 | + |
| 81 | +#### _Boost.Fusion_ sequences #### |
| 82 | + |
| 83 | +The indended use of the library is for most data to be accessed using _Fusion_ |
| 84 | +or _Fusion_ adapted sequences. These sequences hold a number of elements of |
| 85 | +arbitrary type, and provide reflection capabilities to each element's index and |
| 86 | +type which allows the library to operate with them requesting no additional |
| 87 | +information. _Example:_ |
| 88 | + |
| 89 | + books_by_author["author"] = "Bjarne Stroustrup"; |
| 90 | + { |
| 91 | + std::pair< std::string, int > pair_results; |
| 92 | + |
| 93 | + books_by_author >> pair_results; |
| 94 | + std::cout |
| 95 | + << "title: " << pair_results.first << ", " |
| 96 | + << "year: " << pair_results.second |
| 97 | + ; |
| 98 | + |
| 99 | + std::string title; |
| 100 | + int year; |
| 101 | + |
| 102 | + books_by_author >> boost::tie( title, year ); |
| 103 | + std::cout |
| 104 | + << "title: " << title << ", " |
| 105 | + << "year: " << year |
| 106 | + ; |
| 107 | + } |
| 108 | + books_by_author.reset(); |
| 109 | + |
| 110 | +## Iterators ## |
| 111 | + |
| 112 | +Iterators over `statement`s are provided in the same fashion as standard |
| 113 | +stream objects. These iterators, named `istatement_iterator` and |
| 114 | +`ostatement_iterator`, allow the interoperation with standard containers, |
| 115 | +algorithms, and even third party code. For instance, combining a `std::vector` |
| 116 | +with a `sqlite::row` (or a _Fusion_ sequence) gives what is sometimes referred |
| 117 | +to as a _result set_. _Example:_ |
| 118 | + |
| 119 | + std::vector< sqlite::row > result_set; |
| 120 | + result_set.assign( |
| 121 | + sqlite::istatement_iterator< sqlite::row >( books_by_author ) |
| 122 | + , sqlite::istatement_iterator< sqlite::row >() |
| 123 | + ); |
| 124 | + books_by_author.reset(); |
| 125 | + |
| 126 | + // do something with result_set |
| 127 | + |
| 128 | +## Customization points ## |
| 129 | + |
| 130 | +The library provides customization points at three different levels: |
| 131 | + |
| 132 | +#### raw_traits #### |
| 133 | + |
| 134 | +These traits handle _raw_ access to the database. They operate directly with |
| 135 | +`SQLite` handles to read or write a single field. Specializations are provided |
| 136 | +for all fundamental `SQLite` datatypes; extending these traits should only be |
| 137 | +needed when working with `SQLite` extensions that provide a new datatype. |
| 138 | + |
| 139 | +#### conversion_traits #### |
| 140 | + |
| 141 | +These traits handle the conversion between a given type and the _raw_ type that |
| 142 | +will be used with `SQLite`. Specializations are provided for all fundamental |
| 143 | +integral types (mapped to `int32_t` or `int64_t`), floating point types (mapped to |
| 144 | +`double`) and string types (mapped to `char const*`). More specializations of |
| 145 | +fundamental and standard types will be added in the future. |
| 146 | + |
| 147 | +#### row extraction/insertion #### |
| 148 | + |
| 149 | +The extraction or insertion of an entire row can be customized by overloading |
| 150 | +functions `void extract( istatement& stmt, T& row )` and |
| 151 | +`void insert( ostatement& stmt, T const& row )`, which are found via _ADL_. |
| 152 | +Both `row` and _Fusion_ sequences access data by providing these overloads. |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +> Copyright _Agustín Bergé_, _Fusion Fenix_ 2012 |
13 | 157 | >
|
14 | 158 | > Distributed under the Boost Software License, Version 1.0. (See accompanying
|
15 | 159 | > file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
16 |
| -
|
17 |
| -[sqlite]: http://sqlite.org/ "SQLite Home Page" |
18 |
| -[boost]: http://boost.org/ "Boost C++ Libraries" |
|
0 commit comments