remove old code
[sqlgg.git] / overview.md
blobc8e33d99ff600dafa22e8a803cba3ea46fec2e31
1 SQL to C++ code generator
2 =========================
4 Problem
5 -------
7 Writing database layer code is usually tedious and error-prone, due to the mix of different
8 languages. SQL queries constructed dynamically need to bind external data (from application), and
9 the resulting rowset must be decomposed into application native data.  Data crossing these
10 database-application boundaries is what causes troubles. One can factor out all common database
11 communication code, hide the database under some application-specific abstraction, but one always
12 needs to manully specify correspondence between SQL query binding slots (or resulting rowset
13 columns) and code variables. This mapping should be updated manually every time SQL query is
14 modified. 
16 Solution
17 --------
19 SQL parser and code generator which ensures that application code and database queries are in sync.
20 It analyzes SQL query and determines from it the set of input parameters (values for INSERT,
21 run-time substitution parameters) and the set of resulting columns (from SELECT). Then it generates
22 the C++ code which structures input and output values together and assignes corresponding native data
23 types. So basically you provide an SQL query and get a C++ function which takes the set of
24 parameters as required to fill slots in a query, combines them with your query and executes it.
25 SELECT statements additionally return the collection of structures with fields representing columns
26 of resulting rowset. So if you modify the query and forget to update the code -- the compiler will point 
27 on errorneous parts.
29 Example
30 -------
32 Queries:
34     CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,descr TEXT);
35     INSERT INTO test(name,descr) VALUES (?,?);
36     SELECT name,descr FROM test WHERE name = @name LIMIT @limit;
37     SELECT name,z FROM 
38       (SELECT name,
39         city || ' ' || descr as y,
40         max(length(city),random(*)) as z 
41         FROM test LEFT JOIN 
42           (SELECT name AS city FROM test WHERE id=@id))
43     WHERE x < @level;
45 Generated code (only prototypes):
47     template <class Traits>
48     struct sql2cpp
49     {
50       static bool create_test(typename Traits::connection db);
51       static bool insert_2(typename Traits::connection db, typename Traits::Text const& name, typename Traits::Text const& descr);
53       struct data_3
54       {
55         typename Traits::Int id;
56         typename Traits::Text name;
57         typename Traits::Text descr;
58       }; // struct data_3
60       template<class T>
61       static bool select_3(typename Traits::connection db, T& result, typename Traits::Any const&       name, typename Traits::Int const& limit);
63       struct data_4
64       {
65         typename Traits::Text name;
66         typename Traits::Text z;
67       }; // struct data_4
69       template<class T>
70       static bool select_4(typename Traits::connection db, T& result, typename Traits::Any const& id, typename Traits::Any const& level);
71     }
73 Things to note above:
75 1. The generated code is parametrized by database-specific class `Traits`. It specifies the
76                 corresponding between SQL and native types, provides types for database connection and other
77                 details. `Traits` also implements actual code to execute statements. It should be implemented
78                 once for every specific database API.
79 2. `insert_2()` requires two data parameters, the values to INSERT into table, the binding into
80                 statement is done behind the scenes.
81 3. `select_3()` returns data via `result` parameter. It is not visible above, but `T` should be a
82                 container with `T::value_type` having at least the fields of `data_3` (otherwise it will fail to
83                 compile), e.g. `std::vector<data_3>` is fine.
84 4. The type of limit is `Traits::Int` as expected, though the type of `name` is `Any` which is
85                 unfortunate. In the future it will be possible to infer a specific type.
86 5. Statements can be of arbitrary depth across many tables.
87 6. Statements are checked for correctness as far as generator is concerned, so it will detect
88                 syntax errors, non-existant columns in expressions, mismatched rowsets in compound statements,
89                 ambigous column names etc.
91 Details
92 -------
94 This is work in progress and there is plenty of room for improvement. 
95 The generator is already used for some simple database-access code. It uses
96 [sqlite3](http://sqlite.org) as a database and implements suitable `sqlite3_traits`
97 helper. 
99 Online version will be made available soon.
101 TODO
102 ----
104 * type check expressions, infer type for parameters
105 * validate expressions with regard to scheme in their scope
106 * detect statements on single tables and group the corresponding generated code in one class
107 * check names (functions and bindings) for uniqueness
108 * support/test other SQL engines
109 * generate code for more languages
110 * read SQL spec
112 ----
113 2009-05-03
115 <style>
116 code { font-family: monospace; font-style: italic; }
117 pre { background-color: #eee; color: black; border: 1px dashed #0c5; }
118 /*body { padding-bottom: 200px; }*/
119 </style>