[rust-dev] Generic Database Bindings

Eli Green eli at zigr.org
Mon Jun 9 13:39:01 PDT 2014


Having looked at this library and the other options out there, I have to say the designers of rust-postgres have built a very comfortable API and it would be an excellent place to start.

The two pieces I see missing are:

1. A generic way to specify bindings inside queries. JDBC and ODBC use ? as a placeholder for parameters whereas Python's DB-API lets you use a number of different formats. This was a mistake (the API came after several modules that implemented a similar interface) and one of the things that SQLAchemy Core does for users is to define a single style for passing parameters.

This part seems easy and by making a macro out of it, could even make rust-postgres' API slightly nicer:

// current syntax
conn.execute("SELECT a FROM b WHERE foo=$1 OR bar=$2", [&foo as &ToSql, &bar as &ToSql]);

// possible syntax - handles the casting to ToSql for you
conn.execute(sql!("SELECT a FROM b WHERE foo=$1 OR bar=$2", foo, bar));

2. rust-postgres defines two traits - ToSql and FromSql - which are what let the API do magical things as shown in their code snippet on their github page. I'm still learning about rust's type system but at the moment I don't see a way to make this work in a polymorphic environment.

Not only that, some database drivers may support types that others do not. The geographic extension for PostgreSQL, PostGIS, can store geometries and send them to the user in a textual or binary format.

This requirement could disappear if there was no need for the option to select a new driver at run-time, which is a feature common to all the other libraries I'm familiar with (though python technically doesn't do this - each module is completely stand-alone and there's no common code between them, the dynamic nature of python makes it trivial to load different modules based on runtime configuration).

Does rust have any run-time type information built into the language? I've been assuming the answer is "no" given that one of the main design goals of the language is to avoid having a costly runtime.

Eli

On Jun 8, 2014, at 14:19, Steve Klabnik <steve at steveklabnik.com> wrote:

> There isn't no. If you want to build a binding, just do it! The only
> one I'm really aware of right now is
> https://github.com/sfackler/rust-postgres

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4118 bytes
Desc: not available
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140609/494282f8/attachment.p7s>


More information about the Rust-dev mailing list