|
8 | 8 | import std.database.sqlite; |
9 | 9 | auto db = createDatabase("file:///testdb"); |
10 | 10 | auto rows = db.connection.query("select name,score from score").rows; |
11 | | - foreach (r; rows) { |
12 | | - writeln(r[0].as!string,",",r[1].as!int); |
13 | | - } |
| 11 | + foreach (r; rows) writeln(r[0], r[1]); |
14 | 12 | --- |
15 | 13 |
|
16 | | - For advanced usage, you can also explicitly instantiate a BasicDatabase |
17 | | - with a driver: |
| 14 | + BasicDatabase, and it's chain of types, provides a common, easy to use, |
| 15 | + and flexibe front end for client interactions with a database. it carefully |
| 16 | + manages lifetimes and states, making the front end easy to use and the driver layer |
| 17 | + easy to implement. |
| 18 | +
|
| 19 | + For advanced usage (such as library implementers), you can also explicitly |
| 20 | + instantiate a BasicDatabase with a specific Driver type: |
18 | 21 | --- |
| 22 | + struct MyDriver { |
| 23 | + struct Database {//...} |
| 24 | + struct Connection {//...} |
| 25 | + struct Statement {//...} |
| 26 | + struct Bind {//...} |
| 27 | + struct Result {//...} |
| 28 | + } |
| 29 | +
|
19 | 30 | import std.database; |
20 | 31 | alias DB = BasicDatabase!(MyDriver); |
| 32 | + auto db = DB("mysql://127.0.0.1"); |
21 | 33 | --- |
22 | 34 |
|
23 | 35 | */ |
@@ -84,20 +96,41 @@ struct BasicDatabase(D) { |
84 | 96 | static auto create() {return BasicDatabase(null);} |
85 | 97 | static auto create(string url) {return BasicDatabase(url);} |
86 | 98 |
|
| 99 | + /** |
| 100 | + return connection for default database |
| 101 | + */ |
87 | 102 | auto connection() {return Connection(this);} |
| 103 | + |
| 104 | + /** |
| 105 | + return connection for database specified by URI |
| 106 | + */ |
88 | 107 | auto connection(string uri) {return Connection(this, uri);} |
89 | 108 |
|
| 109 | + /** |
| 110 | + return statement |
| 111 | + */ |
90 | 112 | auto statement(string sql) {return connection().statement(sql);} |
| 113 | + |
| 114 | + /** |
| 115 | + return statement with specified input binds |
| 116 | + */ |
91 | 117 | auto statement(X...) (string sql, X args) {return connection.statement(sql,args);} |
92 | 118 |
|
| 119 | + /** |
| 120 | + return executed statement |
| 121 | + */ |
93 | 122 | auto query(string sql) {return connection().query(sql);} |
| 123 | + |
| 124 | + /** |
| 125 | + return executed statement object with specified input binds |
| 126 | + */ |
94 | 127 | auto query(T...) (string sql, T args) {return statement(sql).query(args);} |
95 | 128 |
|
96 | 129 | //static bool hasFeature(Feature feature); |
97 | 130 | // go with non-static hasFeature for now to accomidate poly driver |
98 | 131 |
|
99 | 132 | bool hasFeature(Feature feature) {return hasFeature(data_.database, feature);} |
100 | | -auto ref driverDatabase() {return data_.database;} |
| 133 | + auto ref driverDatabase() {return data_.database;} |
101 | 134 |
|
102 | 135 | private struct Payload { |
103 | 136 | string defaultURI; |
@@ -142,7 +175,7 @@ auto ref driverDatabase() {return data_.database;} |
142 | 175 | } |
143 | 176 |
|
144 | 177 | /** |
145 | | - Holds a connection to the database. |
| 178 | + Database connection class |
146 | 179 | */ |
147 | 180 | struct BasicConnection(D) { |
148 | 181 | alias Driver = D; |
|
0 commit comments