[std.database]
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Tue Oct 11 10:40:52 PDT 2011
On 10/10/11 9:07 AM, Steve Teale wrote:
> Here's a sketch of an interface. This is based on my experiments with
> MySQL, and as such it is probably mid-level, and not a top level covers-
> all interface.
>
> Hopefully it will create a number of discussion points.
>
> // Can interfaces include template functions???
> interface SQLDBConnection
> {
> @property Handle handle();
> Handle connect(string host, string user, string password,
> string database = null);
> T getProperty(T)(string name);
> T getProperty(T)(int id);
> void setProperty(T)(T property, string name);
> void setProperty(T)(T property, int id);
> Handle disconnect();
> }
[snip]
This makes sense from the perspective of a database implementor who
needs to provide the appropriate interfaces, but I think a better angle
would be to focus on use cases and work the API backwards from there.
Here's what I think should be a complete program:
import std.sql, std.stdio;
void main() {
auto db = connect("
engine = mysql;
user = john;
password = doe;
port = 6900;
");
auto rows = db.execute("SELECT score, a.user FROM users a
JOIN scores b on a.user = b.user");
foreach (row; rows) {
writeln("%10s\t%s", row[0], row[1]);
// or
// writeln("%10s\t%s", row["score"], row["user"]);
}
}
This should cleanly close everything upon exiting from main, provide
proper error reporting if anything bad happens (and a lot could go
wrong: malformed conn string, missing driver, wrong username/password,
connection errors, malformed query, wrong column name...).
Using the interfaces you propose directly would allow one to implement
the same program, but with significant extra chaff. That's why suggest
we focus on user-level APIs first because in many ways those are the
most important thing. Then we will distill the proper interfaces.
So the question is - what should a typical database task look like? That
includes prepared statements, binding to tuples and structs, and a lot
more.
Andrei
More information about the Digitalmars-d
mailing list