Skip to content

Commit 5fbd10a

Browse files
committed
dub quickstart, sqlite fetch fix
BasicDatabase change fixes remove old stuff
1 parent e4284e8 commit 5fbd10a

9 files changed

Lines changed: 93 additions & 63 deletions

File tree

README.md

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,39 @@ Available in [DUB](https://code.dlang.org/packages/dstddb), the D package regist
77

88
[![Build Status](https://travis-ci.org/cruisercoder/dstddb.svg?branch=master)](https://travis-ci.org/cruisercoder/dstddb)
99

10+
### Quickstart with Dub
11+
12+
Add a dub.sdl file
13+
```
14+
name "demo"
15+
libs "sqlite3"
16+
dependency "dstddb" version="*"
17+
versions "StdLoggerDisableLogging"
18+
targetType "executable"
19+
```
20+
21+
Add a simple example in src/demo.d
22+
```D
23+
void main() {
24+
import std.database.sqlite;
25+
auto db = createDatabase("file:///testdb.sqlite");
26+
auto con = db.connection;
27+
con.query("drop table if exists score");
28+
con.query("create table score(name varchar(10), score integer)");
29+
con.query("insert into score values('Knuth',71)");
30+
con.query("insert into score values('Dijkstra',82)");
31+
con.query("insert into score values('Hopper',98)");
32+
auto rows = con.query("select name,score from score").rows;
33+
foreach (r; rows) writeln(r[0].as !string, ",", r[1].as !int);
34+
}
35+
```
36+
37+
Run it:
38+
```sh
39+
dub
40+
```
41+
42+
1043
### Roadmap Highlights
1144
- A database and driver neutral interface specification
1245
- Reference counted value objects provide ease of use
@@ -22,13 +55,6 @@ Available in [DUB](https://code.dlang.org/packages/dstddb), the D package regist
2255
- Array input/output binding support
2356
- Connection pooling
2457

25-
### Related Work
26-
[CPPSTDDB](https://github.com/cruisercoder/cppstddb) is a related project with
27-
similar objectives tailored to the constraints of the C++ language. The aim is
28-
for both projects to complement each other by proving the validity of specific
29-
design choices that apply to both and to draw on implementation correctness
30-
re-enforced from dual language development.
31-
3258
## Examples
3359

3460
#### simple query
@@ -99,8 +125,14 @@ Database.register!(std.database.mysql.Database)();
99125
Database.register!(std.database.oracle.Database)();
100126
auto db = createDatabase("mydb");
101127
```
102-
103128
### Notes
104129

105130
- The reference implementations use logging (std.experimental.logger). To hide the info logging, add this line to your package.json file: "versions": ["StdLoggerDisableInfo"].
106131

132+
### Related Work
133+
[CPPSTDDB](https://github.com/cruisercoder/cppstddb) is a related project with
134+
similar objectives tailored to the constraints of the C++ language. The aim is
135+
for both projects to complement each other by proving the validity of specific
136+
design choices that apply to both and to draw on implementation correctness
137+
re-enforced from dual language development.
138+

reference/test.d

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ unittest {
1717
con.query("drop table t");
1818

1919
auto stmt = statement(con, "select * from t");
20-
auto res = stmt.query();
20+
auto rows = stmt.query().rows;
2121

22-
auto range = res[];
23-
24-
writeResult(range);
22+
//auto range = res[];
23+
//writeRows(range);
2524
}
2625

src/std/database/BasicDatabase.d

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,28 @@
88
import std.database.sqlite;
99
auto db = createDatabase("file:///testdb");
1010
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]);
1412
---
1513
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:
1821
---
22+
struct MyDriver {
23+
struct Database {//...}
24+
struct Connection {//...}
25+
struct Statement {//...}
26+
struct Bind {//...}
27+
struct Result {//...}
28+
}
29+
1930
import std.database;
2031
alias DB = BasicDatabase!(MyDriver);
32+
auto db = DB("mysql://127.0.0.1");
2133
---
2234
2335
*/
@@ -84,20 +96,41 @@ struct BasicDatabase(D) {
8496
static auto create() {return BasicDatabase(null);}
8597
static auto create(string url) {return BasicDatabase(url);}
8698

99+
/**
100+
return connection for default database
101+
*/
87102
auto connection() {return Connection(this);}
103+
104+
/**
105+
return connection for database specified by URI
106+
*/
88107
auto connection(string uri) {return Connection(this, uri);}
89108

109+
/**
110+
return statement
111+
*/
90112
auto statement(string sql) {return connection().statement(sql);}
113+
114+
/**
115+
return statement with specified input binds
116+
*/
91117
auto statement(X...) (string sql, X args) {return connection.statement(sql,args);}
92118

119+
/**
120+
return executed statement
121+
*/
93122
auto query(string sql) {return connection().query(sql);}
123+
124+
/**
125+
return executed statement object with specified input binds
126+
*/
94127
auto query(T...) (string sql, T args) {return statement(sql).query(args);}
95128

96129
//static bool hasFeature(Feature feature);
97130
// go with non-static hasFeature for now to accomidate poly driver
98131

99132
bool hasFeature(Feature feature) {return hasFeature(data_.database, feature);}
100-
auto ref driverDatabase() {return data_.database;}
133+
auto ref driverDatabase() {return data_.database;}
101134

102135
private struct Payload {
103136
string defaultURI;
@@ -142,7 +175,7 @@ auto ref driverDatabase() {return data_.database;}
142175
}
143176

144177
/**
145-
Holds a connection to the database.
178+
Database connection class
146179
*/
147180
struct BasicConnection(D) {
148181
alias Driver = D;

src/std/database/freetds/database.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct DefaultPolicy {
2121
alias Allocator = MyMallocator;
2222
}
2323

24-
alias Database(T) = BasicDatabase!(Driver);
24+
alias Database(T) = BasicDatabase!(Driver!T);
2525

2626
auto createDatabase()(string defaultURI="") {
2727
return Database!DefaultPolicy(defaultURI);

src/std/database/mysql/database.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ private struct Driver(P) {
9898

9999

100100
struct Sync {
101+
alias Policy = P;
101102
alias Allocator = Policy.Allocator;
102103
alias Cell = BasicCell!(Sync);
103104
alias const(ubyte)* cstring;
@@ -434,6 +435,7 @@ private struct Driver(P) {
434435
}
435436

436437
struct Async {
438+
alias Policy = P;
437439
alias Allocator = Policy.Allocator;
438440
alias Describe = Driver!Policy.Describe;
439441
alias Bind = Driver!Policy.Bind;

src/std/database/odbc/database.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct DefaultPolicy {
2929
alias Allocator = MyMallocator;
3030
}
3131

32-
alias Database(T) = BasicDatabase!(Driver);
32+
alias Database(T) = BasicDatabase!(Driver!T);
3333

3434
auto createDatabase()(string defaultURI="") {
3535
return Database!DefaultPolicy(defaultURI);

src/std/database/sqlite/database.d

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ struct Driver(P) {
201201
struct Result {
202202
private Statement* stmt_;
203203
private sqlite3_stmt *st_;
204+
bool init_;
204205
int columns;
205206
int status_;
206207

@@ -227,6 +228,11 @@ struct Driver(P) {
227228
bool hasRows() {return stmt_.hasRows;}
228229

229230
int fetch() {
231+
// needs more attention
232+
if (!init_) {
233+
init_ = 1;
234+
return 1;
235+
}
230236
status_ = sqlite3_step(st_);
231237
if (status_ == SQLITE_ROW) return 1;
232238
if (status_ == SQLITE_DONE) {

webscalesql/dub.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

webscalesql/test.d

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)