Skip to content

Commit

Permalink
README update with dub link
Browse files Browse the repository at this point in the history
  • Loading branch information
cruisercoder committed Mar 3, 2016
1 parent d2d29c3 commit a7d9219
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
## A proposed standard database client interface and implementation for the [D](http://dlang.org) Language

Status: early stage project - unstable and minimally tested
DUB link: coming soon
DUB link: https://code.dlang.org/packages/dstddb

### Roadmap Highlights
- A database and driver neutral interface specification
Expand Down
6 changes: 4 additions & 2 deletions src/std/database/allocator.d
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module std.database.allocator;
import std.experimental.allocator.common;
import std.experimental.logger;

struct MyMallocator {
enum uint alignment = platformAlignment;

@trusted @nogc nothrow
@trusted // removed @nogc and nothrow for logging
void[] allocate(size_t bytes) {
import core.stdc.stdlib : malloc;
if (!bytes) return null;
Expand All @@ -13,9 +14,10 @@ struct MyMallocator {
}

/// Ditto
@system @nogc nothrow
@system // removed @nogc and nothrow for logging
bool deallocate(void[] b) {
import core.stdc.stdlib : free;
//log("deallocate: ptr: ", b.ptr, "size: ", b.length);
free(b.ptr);
return true;
}
Expand Down
84 changes: 47 additions & 37 deletions src/std/database/mysql/database.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module std.database.mysql.database;
import std.conv;
import core.stdc.config;
//import std.experimental.allocator.mallocator;
import std.experimental.allocator.mallocator;

version(Windows) {
pragma(lib, "libmysql");
Expand Down Expand Up @@ -50,6 +50,7 @@ struct Database(T) {
string defaultURI;
this(string defaultURI_) {
defaultURI = defaultURI_;
allocator = MyMallocator();
}
}

Expand Down Expand Up @@ -77,7 +78,7 @@ struct Connection(T) {
MYSQL *mysql;

this(Database!T db_, string uri_) {
db = db;
db = db_;
uri = uri_.length == 0 ? db_.data_.defaultURI : uri_;

mysql = mysql_init(null);
Expand Down Expand Up @@ -139,32 +140,30 @@ struct Describe {
struct Bind {
int mysql_type;
int allocSize;
MYSQL_BIND *bind;
//MYSQL_BIND *bind; //bad
c_ulong length;
my_bool is_null;
my_bool error;
}

void binder(T)(ref T allocator, int n, ref Bind b, ref MYSQL_BIND mb) {
void binder(T)(ref T allocator, int n, Bind *b, MYSQL_BIND *mb) {
import core.stdc.string: memset;
b.bind = &mb;
memset(&mb, 0, MYSQL_BIND.sizeof);
memset(mb, 0, MYSQL_BIND.sizeof);
mb.buffer_type = b.mysql_type;

//mb.buffer = malloc(b.allocSize);
//alias Allocator = Mallocator;
//mb.buffer = cast(void*)(Allocator.instance.allocate(b.allocSize));
mb.buffer = cast(void*)(allocator.allocate(b.allocSize));

mb.buffer_length = b.allocSize;
mb.length = &b.length;
mb.is_null = &b.is_null;
mb.error = &b.error;

log(
"bind: index: ", n,
", type: ", mb.buffer_type,
", allocSize: ", b.allocSize);
/*
log(
"bind: index: ", n,
", type: ", mb.buffer_type,
", allocSize: ", b.allocSize);
*/

//GC.addRange(b.mb.buffer, allocSize);
}
Expand All @@ -173,6 +172,7 @@ void binder(T)(ref T allocator, int n, ref Bind b, ref MYSQL_BIND mb) {
//auto range(T)(Statement!T stmt) {return Result!T(stmt).range();}

struct Statement(T) {
alias Allocator = T.Allocator;
//alias Result = .Result;

// temporary
Expand All @@ -198,6 +198,7 @@ struct Statement(T) {

void bind(int n, int value) {
log("input bind: n: ", n, ", value: ", value);
if (n==0) throw new DatabaseException("zero index");

{
Bind b;
Expand All @@ -208,9 +209,10 @@ struct Statement(T) {

{
auto b = &data_.inputBind.back();
auto mb = &data_.mysqlBind.back();
b.is_null = 0;
b.error = 0;
*cast(int*)(b.bind.buffer) = value;
*cast(int*)(mb.buffer) = value;
}

}
Expand All @@ -229,10 +231,11 @@ struct Statement(T) {

{
auto b = &data_.inputBind.back();
auto mb = &data_.mysqlBind.back();
b.is_null = 0;
b.error = 0;

auto p = cast(char*) b.bind.buffer;
auto p = cast(char*) mb.buffer;
strncpy(p, value.ptr, value.length);
p[value.length] = 0;
b.length = value.length;
Expand All @@ -248,13 +251,15 @@ struct Statement(T) {
data_.inputBind ~= bind;
data_.mysqlBind ~= MYSQL_BIND();

//auto allocator = data_.con.data_.db.data_.allocator;
binder(data_.allocator, n, data_.inputBind.back(), data_.mysqlBind.back());
auto b = &data_.inputBind.back();
auto mb = &data_.mysqlBind.back();

binder(data_.allocator, n, b, mb);
}

struct Payload {
Connection!T con;
MyMallocator allocator;
Allocator *allocator;
string sql;
MYSQL_STMT *stmt;
bool hasRows;
Expand All @@ -265,19 +270,18 @@ struct Statement(T) {

this(Connection!T con_, string sql_) {
con = con_;
allocator = MyMallocator();
sql = sql_;
allocator = &con.data_.db.data_.allocator;

stmt = mysql_stmt_init(con.data_.mysql);
if (!stmt) {
throw new DatabaseException("stmt error");
}
if (!stmt) throw new DatabaseException("stmt error");
}

~this() {
for(int i = 0; i < mysqlBind.length; ++i) {
free(mysqlBind[i].buffer);
//allocator.deallocate(mysqlBind[i].buffer);
allocator.deallocate(mysqlBind[i].buffer[0..inputBind[i].allocSize]);
}

if (stmt) mysql_stmt_close(stmt);
// stmt = null? needed
}
Expand All @@ -296,12 +300,14 @@ struct Statement(T) {
}

void execute() {

if (inputBind.length && !bindInit) {
bindInit = true;
check("mysql_stmt_bind_param",
stmt,
mysql_stmt_bind_param(stmt, &mysqlBind[0]));
}

check("mysql_stmt_execute", stmt, mysql_stmt_execute(stmt));
}

Expand Down Expand Up @@ -356,6 +362,7 @@ struct Statement(T) {


struct Result(T) {
alias Allocator = T.Allocator;
//alias Range = .ResultRange!T;
//alias Row = .Row;

Expand All @@ -376,7 +383,7 @@ struct Result(T) {

struct Payload {
Statement!T stmt;
MyMallocator allocator;
Allocator *allocator;
uint columns;
Array!Describe describe;
Array!Bind bind;
Expand All @@ -386,7 +393,7 @@ struct Result(T) {

this(Statement!T stmt_) {
stmt = stmt_;
allocator = MyMallocator();
allocator = stmt.data_.allocator;

result_metadata = mysql_stmt_result_metadata(stmt.data_.stmt);
//columns = mysql_num_fields(result_metadata);
Expand All @@ -398,9 +405,9 @@ struct Result(T) {

~this() {
for(int i = 0; i < mysqlBind.length; ++i) {
free(mysqlBind[i].buffer);
//allocator.deallocate(mysqlBind[i].buffer);
allocator.deallocate(mysqlBind[i].buffer[0..bind[i].allocSize]);
}

if (result_metadata) mysql_free_result(result_metadata);
}

Expand Down Expand Up @@ -440,12 +447,12 @@ struct Result(T) {
bind ~= Bind();
auto b = &bind.back();
mysqlBind ~= MYSQL_BIND();
auto mb = &mysqlBind.back();

b.allocSize = cast(uint)(d.field.length + 1);
b.mysql_type = MYSQL_TYPE_STRING;

//auto allocator = stmt.data_.con.data_.db.data_.allocator;
binder(allocator, i, bind.back(), mysqlBind.back()); //fix
binder(allocator, i, b, mb);
}

mysql_stmt_bind_result(stmt.data_.stmt, &mysqlBind.front());
Expand Down Expand Up @@ -475,26 +482,29 @@ struct Result(T) {

struct Value(T) {
package Bind* bind_;
private MYSQL_BIND *mysqlBind_;

this(Bind* bind) {
this(Bind* bind, MYSQL_BIND *mysqlBind) {
bind_ = bind;
mysqlBind_ = mysqlBind;
}

int get(X) () {
return toInt();
}

int toInt() {
check(bind_.bind.buffer_type, MYSQL_TYPE_LONG);
return *(cast(int*) bind_.bind.buffer);
check(mysqlBind_.buffer_type, MYSQL_TYPE_LONG);
return *cast(int*) mysqlBind_.buffer;
//return *(cast(int*) bind_.bind.buffer);
}

//inout(char)[]
auto chars() {
check(bind_.bind.buffer_type, MYSQL_TYPE_STRING);
check(mysqlBind_.buffer_type, MYSQL_TYPE_STRING);
import core.stdc.string: strlen;
auto data = cast(char*) bind_.bind.buffer;
return data ? data[0 .. strlen(data)] : data[0..0];
auto d = cast(char*) mysqlBind_.buffer;
return d? d[0 .. strlen(d)] : d[0..0]; //fix
}

private:
Expand All @@ -516,7 +526,7 @@ struct Row(T) {
int columns() {return result_.columns();}

Value opIndex(size_t idx) {
return Value(&result_.data_.bind[idx]);
return Value(&result_.data_.bind[idx],&result_.data_.mysqlBind[idx]);
}

private Result* result_;
Expand Down
21 changes: 19 additions & 2 deletions src/std/database/testsuite.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ void testAll(Database) (string source) {

simpleInsertSelect(db);
classicSelect(db);
bindTest(db);

bindTest1(db);
bindTest2(db);

bindInsertTest(db);

cascadeTest(db);
Expand Down Expand Up @@ -41,7 +44,21 @@ void classicSelect(Database) (Database db) {
writeResult(range);
}

void bindTest(Database) (Database db) {
void bindTest1(Database) (Database db) {
if (!db.bindable()) {
writeln("skip bindTest");
return;
}

create_score_table(db, "t1");
auto stmt = db.connection().statement("select name,score from t1 where score >= ?", 50);
assert(stmt.binds() == 1);
auto res = stmt.result();
assert(res.columns() == 2);
writeResult(res.range());
}

void bindTest2(Database) (Database db) {
if (!db.bindable()) {
writeln("skip bindTest");
return;
Expand Down
2 changes: 2 additions & 0 deletions test/mysql/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ unittest {
testAll!DB("mysql");
}

/*
unittest {
auto db = createDatabase("mysql://127.0.0.1/test");
auto con = db.connection();
con.statement("select * from score").writeResult();
}
*/

0 comments on commit a7d9219

Please sign in to comment.