Skip to content

Commit

Permalink
dub quickstart, sqlite fetch fix
Browse files Browse the repository at this point in the history
BasicDatabase change fixes
remove old stuff
  • Loading branch information
cruisercoder committed Feb 25, 2018
1 parent e4284e8 commit 5fbd10a
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 63 deletions.
48 changes: 40 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,39 @@ Available in [DUB](https://code.dlang.org/packages/dstddb), the D package regist

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

### Quickstart with Dub

Add a dub.sdl file
```
name "demo"
libs "sqlite3"
dependency "dstddb" version="*"
versions "StdLoggerDisableLogging"
targetType "executable"
```

Add a simple example in src/demo.d
```D
void main() {
import std.database.sqlite;
auto db = createDatabase("file:///testdb.sqlite");
auto con = db.connection;
con.query("drop table if exists score");
con.query("create table score(name varchar(10), score integer)");
con.query("insert into score values('Knuth',71)");
con.query("insert into score values('Dijkstra',82)");
con.query("insert into score values('Hopper',98)");
auto rows = con.query("select name,score from score").rows;
foreach (r; rows) writeln(r[0].as !string, ",", r[1].as !int);
}
```

Run it:
```sh
dub
```


### Roadmap Highlights
- A database and driver neutral interface specification
- Reference counted value objects provide ease of use
Expand All @@ -22,13 +55,6 @@ Available in [DUB](https://code.dlang.org/packages/dstddb), the D package regist
- Array input/output binding support
- Connection pooling

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

## Examples

#### simple query
Expand Down Expand Up @@ -99,8 +125,14 @@ Database.register!(std.database.mysql.Database)();
Database.register!(std.database.oracle.Database)();
auto db = createDatabase("mydb");
```

### Notes

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

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

7 changes: 3 additions & 4 deletions reference/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ unittest {
con.query("drop table t");

auto stmt = statement(con, "select * from t");
auto res = stmt.query();
auto rows = stmt.query().rows;

auto range = res[];

writeResult(range);
//auto range = res[];
//writeRows(range);
}

47 changes: 40 additions & 7 deletions src/std/database/BasicDatabase.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,28 @@
import std.database.sqlite;
auto db = createDatabase("file:///testdb");
auto rows = db.connection.query("select name,score from score").rows;
foreach (r; rows) {
writeln(r[0].as!string,",",r[1].as!int);
}
foreach (r; rows) writeln(r[0], r[1]);
---
For advanced usage, you can also explicitly instantiate a BasicDatabase
with a driver:
BasicDatabase, and it's chain of types, provides a common, easy to use,
and flexibe front end for client interactions with a database. it carefully
manages lifetimes and states, making the front end easy to use and the driver layer
easy to implement.
For advanced usage (such as library implementers), you can also explicitly
instantiate a BasicDatabase with a specific Driver type:
---
struct MyDriver {
struct Database {//...}
struct Connection {//...}
struct Statement {//...}
struct Bind {//...}
struct Result {//...}
}
import std.database;
alias DB = BasicDatabase!(MyDriver);
auto db = DB("mysql://127.0.0.1");
---
*/
Expand Down Expand Up @@ -84,20 +96,41 @@ struct BasicDatabase(D) {
static auto create() {return BasicDatabase(null);}
static auto create(string url) {return BasicDatabase(url);}

/**
return connection for default database
*/
auto connection() {return Connection(this);}

/**
return connection for database specified by URI
*/
auto connection(string uri) {return Connection(this, uri);}

/**
return statement
*/
auto statement(string sql) {return connection().statement(sql);}

/**
return statement with specified input binds
*/
auto statement(X...) (string sql, X args) {return connection.statement(sql,args);}

/**
return executed statement
*/
auto query(string sql) {return connection().query(sql);}

/**
return executed statement object with specified input binds
*/
auto query(T...) (string sql, T args) {return statement(sql).query(args);}

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

bool hasFeature(Feature feature) {return hasFeature(data_.database, feature);}
auto ref driverDatabase() {return data_.database;}
auto ref driverDatabase() {return data_.database;}

private struct Payload {
string defaultURI;
Expand Down Expand Up @@ -142,7 +175,7 @@ auto ref driverDatabase() {return data_.database;}
}

/**
Holds a connection to the database.
Database connection class
*/
struct BasicConnection(D) {
alias Driver = D;
Expand Down
2 changes: 1 addition & 1 deletion src/std/database/freetds/database.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct DefaultPolicy {
alias Allocator = MyMallocator;
}

alias Database(T) = BasicDatabase!(Driver);
alias Database(T) = BasicDatabase!(Driver!T);

auto createDatabase()(string defaultURI="") {
return Database!DefaultPolicy(defaultURI);
Expand Down
2 changes: 2 additions & 0 deletions src/std/database/mysql/database.d
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ private struct Driver(P) {


struct Sync {
alias Policy = P;
alias Allocator = Policy.Allocator;
alias Cell = BasicCell!(Sync);
alias const(ubyte)* cstring;
Expand Down Expand Up @@ -434,6 +435,7 @@ private struct Driver(P) {
}

struct Async {
alias Policy = P;
alias Allocator = Policy.Allocator;
alias Describe = Driver!Policy.Describe;
alias Bind = Driver!Policy.Bind;
Expand Down
2 changes: 1 addition & 1 deletion src/std/database/odbc/database.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct DefaultPolicy {
alias Allocator = MyMallocator;
}

alias Database(T) = BasicDatabase!(Driver);
alias Database(T) = BasicDatabase!(Driver!T);

auto createDatabase()(string defaultURI="") {
return Database!DefaultPolicy(defaultURI);
Expand Down
6 changes: 6 additions & 0 deletions src/std/database/sqlite/database.d
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ struct Driver(P) {
struct Result {
private Statement* stmt_;
private sqlite3_stmt *st_;
bool init_;
int columns;
int status_;

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

int fetch() {
// needs more attention
if (!init_) {
init_ = 1;
return 1;
}
status_ = sqlite3_step(st_);
if (status_ == SQLITE_ROW) return 1;
if (status_ == SQLITE_DONE) {
Expand Down
21 changes: 0 additions & 21 deletions webscalesql/dub.json

This file was deleted.

21 changes: 0 additions & 21 deletions webscalesql/test.d

This file was deleted.

0 comments on commit 5fbd10a

Please sign in to comment.