Skip to content

Commit d9be2bf

Browse files
committed
opDispatch (WIP), field.name (WIP), stmt.hasRows
1 parent 75d39b4 commit d9be2bf

11 files changed

Lines changed: 325 additions & 204 deletions

File tree

poly/test.d

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,23 @@ unittest {
3232

3333
// these are all heading to test suite
3434

35-
if (true) {
35+
if (true) { //opDispatch
36+
auto db = createDatabase("oracle");
37+
auto con = db.connection;
38+
auto rows = db.query("select * from t").rows;
39+
40+
foreach (row; rows) {
41+
for(int c = 0; c != row.width; ++c) {
42+
//log("NAME: ", row[c].name);
43+
writeln("a:", row.A);
44+
writeln("b:", row.B);
45+
}
46+
writeln;
47+
}
48+
49+
}
50+
51+
if (false) {
3652
auto db = createDatabase;
3753
auto con1 = db.connection("oracle").autoCommit(false);
3854
auto con2 = db.connection("oracle").autoCommit(false);
@@ -42,7 +58,7 @@ unittest {
4258

4359
//con1.begin.query("insert into account(id,amount) values(1,100)");
4460
//con2.begin.query("insert into account(id,amount) values(2,-100)");
45-
61+
4662
con1.commit;
4763
con2.commit;
4864
}

src/std/database/common.d

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,58 @@ enum QueryVariableType {
66
Named,
77
}
88

9+
// this does not work currently
10+
// there is one with arrays in in forum
11+
// but waiting for better options
12+
/* Support empty Args? */
13+
@nogc pure nothrow auto toInputRange(Args...)() {
14+
struct Range {
15+
size_t index;
916

17+
bool empty() {return index >= Args.length;}
18+
void popFront() {++index;}
19+
20+
import std.traits : CommonType;
21+
alias E = CommonType!Args;
22+
23+
E front() {
24+
final switch (index) {
25+
/* static */ foreach (i, arg; Args) {
26+
case i:
27+
writeln("FRONT: ", arg, "INDEX: ", i);
28+
return arg;
29+
}
30+
}
31+
}
32+
}
33+
34+
return Range();
35+
}
36+
37+
/*
38+
unittest {
39+
import std.traits;
40+
import std.range;
41+
42+
43+
static assert(isInputRange!(ReturnType!(toInputRange!(1))));
44+
45+
static auto foo(Args...)(Args args) {
46+
import std.container.array;
47+
48+
import std.traits : CommonType;
49+
alias E = CommonType!Args;
50+
writeln("common:", typeid(E));
51+
52+
return Array!E(toInputRange!(args));
53+
}
54+
auto a = foo(1,2,3);
55+
//assert(a.length == 3 && a[0] == 1 && a[1] == 2 && a[2] == 3);
56+
//assert(a.length == 3 && a[0] -);
57+
58+
59+
for(int i = 0; i != a.length; ++i) {
60+
writeln("===================HERE: ", a[i]);
61+
}
62+
}
63+
*/

src/std/database/freetds/database.d

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ struct Driver(Policy) {
191191
query();
192192
}
193193

194+
//bool hasRows() {return status != NO_MORE_RESULTS;}
195+
bool hasRows() {return true;}
196+
194197
private void bindAll(T...) (T args) {
195198
//int col;
196199
//foreach (arg; args) bind(++col, arg);
@@ -241,15 +244,8 @@ struct Driver(Policy) {
241244
this(Statement* stmt_, int rowArraySize_) {
242245
stmt = stmt_;
243246
allocator = stmt.allocator;
244-
245247
status = check("dbresults", dbresults(dbproc));
246-
if (!hasResult()) return;
247-
248248
columns = dbnumcols(dbproc);
249-
info("COLUMNS:", columns);
250-
251-
//if (!columns) return;
252-
253249
build_describe();
254250
build_bind();
255251
}
@@ -328,8 +324,6 @@ struct Driver(Policy) {
328324
}
329325
}
330326

331-
bool hasResult() {return status != NO_MORE_RESULTS;}
332-
333327
int fetch() {
334328
status = check("dbnextrow", dbnextrow(dbproc));
335329
if (status == REG_ROW) {
@@ -341,6 +335,10 @@ struct Driver(Policy) {
341335
return 0;
342336
}
343337

338+
auto name(size_t idx) {
339+
return to!string(describe[idx].name);
340+
}
341+
344342
auto get(X:string)(Cell* cell) {
345343
import core.stdc.string: strlen;
346344
checkType(cell.bind.bindType, NTBSTRINGBIND);

src/std/database/front.d

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ struct BasicDatabase(D,P) {
8181
// go with non-static hasFeature for now to accomidate poly driver
8282

8383
bool hasFeature(Feature feature) {return hasFeature(data_.database, feature);}
84-
85-
auto ref driverDatabase() {return data_.database;}
84+
auto ref driverDatabase() {return data_.database;}
8685

8786
private struct Payload {
8887
string defaultURI;
@@ -258,7 +257,6 @@ struct BasicStatement(D,P) {
258257
auto query() {
259258
data_.query();
260259
state = State.Executed;
261-
//return Result(this, rowArraySize_);
262260
return this;
263261
}
264262

@@ -273,6 +271,8 @@ struct BasicStatement(D,P) {
273271
return this;
274272
}
275273

274+
bool hasRows() {return data_.hasRows;}
275+
276276
// rows()
277277
// accessor for single rowSet returned
278278
// length should be the number of rows
@@ -323,12 +323,11 @@ struct BasicResult(D,P) {
323323
alias Bind = Driver.Bind;
324324
//alias Row = .Row;
325325

326-
327326
this(Statement stmt, int rowArraySize = 1) {
328327
stmt_ = stmt;
329328
rowArraySize_ = rowArraySize;
330329
data_ = Data(&stmt.data_.refCountedPayload(), rowArraySize_);
331-
if (!data_.hasResult()) return;
330+
if (!stmt_.hasRows) throw new DatabaseException("not a result query");
332331
rowsFetched_ = data_.fetch();
333332
}
334333

@@ -357,6 +356,18 @@ package:
357356

358357
alias RefCounted!(ResultImpl, RefCountedAutoInitialize.no) Data;
359358
Data data_;
359+
360+
// these need to move
361+
int width() {return data_.columns;}
362+
363+
private size_t index(string name) {
364+
import std.uni;
365+
// slow name lookup, all case insensitive for now
366+
for(int i=0; i!=width; i++) {
367+
if (sicmp(data_.name(i), name) == 0) return i;
368+
}
369+
throw new DatabaseException("column name not found:" ~ name);
370+
}
360371
}
361372

362373
struct BasicColumnSet(D,P) {
@@ -500,11 +511,16 @@ struct BasicRow(D,P) {
500511

501512
auto opIndex(Column column) {return opIndex(column.idx);}
502513

514+
// experimental
515+
auto opDispatch(string s)() {
516+
return opIndex(rows_.result_.index(s));
517+
}
518+
503519
Value opIndex(size_t idx) {
504520
auto result = &rows_.result_;
505521
// needs work
506522
// sending a ptr to cell instead of reference (dangerous)
507-
auto cell = Cell(result, &result.data_.bind[idx]);
523+
auto cell = Cell(result, &result.data_.bind[idx], idx);
508524
return Value(result, cell);
509525
}
510526
}
@@ -529,7 +545,7 @@ struct BasicValue(D,P) {
529545
}
530546

531547
private auto resultPtr() {
532-
return &result_.result();
548+
return &result_.result(); // last parens matter here (something about delegate)
533549
}
534550

535551
auto type() {return cell_.bind.type;}
@@ -544,6 +560,8 @@ struct BasicValue(D,P) {
544560

545561
bool isNull() {return false;} //fix
546562

563+
string name() {return resultPtr.name(cell_.idx_);}
564+
547565
auto get(T)() {return Nullable!T(as!T);}
548566

549567
// experimental
@@ -583,10 +601,12 @@ struct BasicCell(D,P) {
583601
alias Bind = Driver.Bind;
584602
private Bind* bind_;
585603
private int rowIdx_;
604+
private size_t idx_;
586605

587-
this(Result *r, Bind *b) {
606+
this(Result *r, Bind *b, size_t idx) {
588607
bind_ = b;
589608
rowIdx_ = r.rowIdx_;
609+
idx_ = idx;
590610
}
591611

592612
auto bind() {return bind_;}

0 commit comments

Comments
 (0)