Skip to content

Commit 3fba339

Browse files
committed
thread ID, better sqlite errors
db.query calls
1 parent d324bab commit 3fba339

4 files changed

Lines changed: 32 additions & 30 deletions

File tree

src/std/database/impl.d

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ struct BasicDatabase(T) {
4040
auto connection() {return Connection(this);}
4141
auto connection(string uri) {return Connection(this, uri);}
4242

43-
void query(string sql) {connection().query(sql);}
43+
auto statement(string sql) {return connection().statement(sql);}
44+
auto statement(X...) (string sql, X args) {return connection.statement(sql,args);}
45+
46+
auto query(string sql) {return connection().query(sql);}
47+
auto query(T...) (string sql, T args) {return statement(sql).query(args);}
4448

4549
bool bindable() {return data_.impl.bindable();}
4650
bool dateBinding() {return data_.impl.dateBinding();}

src/std/database/pool.d

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module std.database.pool;
22
import std.container.array;
33
import std.experimental.logger;
44
import std.database.allocator;
5+
import core.thread;
56

67
// not really a pool yet
78

@@ -52,21 +53,24 @@ struct Pool(T) {
5253

5354
struct Resource {
5455
int id;
56+
ThreadID tid;
5557
Res* resource;
56-
this(int id_, Res* resource_) {
58+
this(int id_, ThreadID tid_, Res* resource_) {
5759
id = id_;
60+
tid = tid_;
5861
resource = resource_;
5962
}
6063
}
6164

6265
auto acquire(A...)(auto ref A args) {
63-
if (!enable_) return Resource(0, factory_.acquire(args));
66+
ThreadID tid = Thread.getThis.id;
67+
if (!enable_) return Resource(0, tid, factory_.acquire(args));
6468
if (!data.empty()) {
6569
//log("======= pool: return back");
66-
return Resource(0, data.back.resource);
70+
return Resource(0, tid, data.back.resource);
6771
}
68-
//log("========== pool: create");
69-
data ~= Resource(0,factory_.acquire(args));
72+
//log("========== pool: create: ",tid);
73+
data ~= Resource(0, tid, factory_.acquire(args));
7074
return data.back();
7175
}
7276

@@ -81,7 +85,7 @@ struct Pool(T) {
8185
/*
8286
Scoped resource
8387
Useful for RefCounted
84-
*/
88+
*/
8589

8690
struct ScopedResource(T) {
8791
alias Pool = T;

src/std/database/sqlite/database.d

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ struct StatementImpl(T) {
179179
cast(int) sql.length + 1,
180180
&st,
181181
null);
182-
if (res != SQLITE_OK) error("prepare", res);
182+
if (res != SQLITE_OK) throw_error(sq, "prepare", res);
183183
binds_ = sqlite3_bind_parameter_count(st);
184184
}
185185
}
@@ -194,7 +194,9 @@ struct StatementImpl(T) {
194194
hasRows = true;
195195
} else if (status == SQLITE_DONE) {
196196
reset();
197-
} else throw new DatabaseException("step error");
197+
} else {
198+
throw_error(sq, "step error", status);
199+
}
198200
}
199201

200202
void query(X...) (X args) {
@@ -212,8 +214,6 @@ struct StatementImpl(T) {
212214
if (status != SQLITE_OK) throw new DatabaseException("sqlite3_reset error");
213215
}
214216

215-
void error(string msg, int ret) {con.error(msg, ret);}
216-
217217
}
218218

219219
struct Bind(T) {
@@ -222,8 +222,6 @@ struct Bind(T) {
222222
}
223223

224224
struct ResultImpl(T) {
225-
//alias ResultRange = .ResultRange!T;
226-
//alias Row = .Row!T;
227225
alias Statement = .StatementImpl!T;
228226
alias Bind = .Bind!T;
229227
alias Allocator = T.Allocator;
@@ -283,10 +281,10 @@ struct ResultImpl(T) {
283281
}
284282

285283
void throw_error()(sqlite3 *sq, string msg, int ret) {
284+
import std.conv;
286285
import core.stdc.string: strlen;
287286
const(char*) err = sqlite3_errmsg(sq);
288-
info(msg, ":", err[0..strlen(err)]);
289-
throw new DatabaseException("sqlite error: " ~ msg ~ ": "); // need to send err
287+
throw new DatabaseException("sqlite error: " ~ msg ~ ": " ~ to!string(err)); // need to send err
290288
}
291289

292290
void throw_error()(string label) {
@@ -306,17 +304,17 @@ void throw_error()(string label, char *msg) {
306304

307305
/*
308306
309-
auto as(T:string)() {
310-
import core.stdc.string: strlen;
311-
auto ptr = cast(immutable char*) sqlite3_column_text(result_.st_, cast(int) idx_);
312-
return cast(string) ptr[0..strlen(ptr)]; // fix with length
313-
}
307+
auto as(T:string)() {
308+
import core.stdc.string: strlen;
309+
auto ptr = cast(immutable char*) sqlite3_column_text(result_.st_, cast(int) idx_);
310+
return cast(string) ptr[0..strlen(ptr)]; // fix with length
311+
}
314312
315-
auto chars() {
316-
import core.stdc.string: strlen;
317-
auto data = sqlite3_column_text(result_.st_, cast(int) idx_);
318-
return data ? data[0 .. strlen(data)] : data[0..0];
319-
}
313+
auto chars() {
314+
import core.stdc.string: strlen;
315+
auto data = sqlite3_column_text(result_.st_, cast(int) idx_);
316+
return data ? data[0 .. strlen(data)] : data[0..0];
317+
}
320318
321319
// char*, string_ref?
322320
@@ -325,7 +323,7 @@ const(char*) toStringz() {
325323
return sqlite3_column_text(result_.st_, cast(int) idx_);
326324
}
327325
328-
*/
326+
*/
329327

330328
extern(C) int sqlite_callback(void* cb, int howmany, char** text, char** columns) {
331329
return 0;

test/sqlite/test.d

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ unittest {
77
import std.database.testsuite;
88
alias DB = Database!DefaultPolicy;
99
testAll!DB("sqlite");
10-
11-
//auto db = database("sqlite");
12-
//auto con = db.connection();
13-
//con.query("create table score(name varchar(10), score integer)");
1410
}
1511

1612
unittest {

0 commit comments

Comments
 (0)