View previous topic :: View next topic |
Author |
Message |
MyronAlexander
Joined: 11 Jan 2007 Posts: 9
|
Posted: Thu Jan 18, 2007 5:05 am Post subject: Have you considered the python dbapi2? |
|
|
Hello.
I am curious as to what people think about the python database interface?
For my first D project, I am building an index that is to be backed by sqlite. I have looked at DDBI but the API is in flux and I also looked at sqlited but not enough to make a comment. Since I seem to enjoy coding a fair bit a plumbing, I am toying with the idea of making a simple sqlite3 module based on dbapi2 but would like to know if anyone has considered something like that and found reasons that it is not appropriate for D.
By toying, I mean that I have not committed myself, it just seems that everytime I use a C style interface, I end up coding an OOP layer; must be wired that way
Thanks,
Myron. |
|
Back to top |
|
|
MyronAlexander
Joined: 11 Jan 2007 Posts: 9
|
Posted: Fri Feb 02, 2007 5:54 am Post subject: |
|
|
I've been familiarizing myself with the D api and mindset. My first job was to convert the SQLite .h file into .d and understand the C<->D interfacing requirements. That was quite a bit of fun.
The next task is to figure out how to bridge the static/dynamic typed worlds. I wanted to be as close to dbapi as possible but the concept of returning a mixed array of anything in python does not translate directly to D as you have to declare the type of the array. Templates could work as long as the queries are defined at compile time such that the resultset types are known. In that case I could use a tuple. Don't think that would work so well for when you read the query from a configuration file. Then I discovered box arrays.
My latest thinking is along the lines of:
Box[] fetchone
Box[][] fetchmany
Box[][] fetchall
BUT I can't seem to make a multidimensional box array.
Has anyone ever created a multidimensional box array? If so, how?
Thanks,
Myron. |
|
Back to top |
|
|
brad Site Admin
Joined: 22 Feb 2004 Posts: 490 Location: Atlanta, GA USA
|
Posted: Fri Feb 02, 2007 8:59 am Post subject: |
|
|
I'm facing this same problem in updating DDBI to return D types.
One of the things discussed is to store the data from the C <> D implementation in a Row object's field as ubyte[] or void[]. Then, a developer can use a Converter object on the field value, such as IntConverter, StringConverter, DateConverter. Converters would attempt to do the right thing, but may throw exceptions if you try a conversion on a field where it's not possible. I think we have to decide when/if you want to capture metadata at binding time, and that could govern what converters are permitted.
I was tinkering with it a bit, but got sidetracked with the Tango release. I'd be interested in your thoughts on this matter, and any feedback you get on your MD Box array.
Cheers,
BA _________________ I really like the vest! |
|
Back to top |
|
|
sean
Joined: 24 Jun 2004 Posts: 609 Location: Bay Area, CA
|
Posted: Fri Feb 02, 2007 10:42 am Post subject: |
|
|
Since the dataset contains information about the DB type of the stored data, the problem is mostly a matter of converting from one known format to another. With C++/ODBC I've done pretty much as Brad suggests: store in a generic buffer like byte[] and perform the appropriate conversions on output, throwing an exception if the conversion is not supported. Being able to bind converters to a specific column sounds like a useful approach, and the rowset could contain template methods for lazy people:
Code: |
int get(T)() { /* call converter to T explicitly */ }
|
If D provided implicit cast operations, that would be an option as well, but I suppose we'll have to wait for that feature.
It seems like a MD array of anything should work in D. Can't say why it isn't working offhand. |
|
Back to top |
|
|
jcc7
Joined: 22 Feb 2004 Posts: 657 Location: Muskogee, OK, USA
|
Posted: Fri Feb 02, 2007 2:02 pm Post subject: |
|
|
sean wrote: | It seems like a MD array of anything should work in D. Can't say why it isn't working offhand. | It seems that he got his MD array to work after he posted a question in the "learn" newsgroup:
"I seem to have solved my problem. I'm not sure what I am doing differently but it seems to work now "
digitalmars.D.learn:6311 |
|
Back to top |
|
|
MyronAlexander
Joined: 11 Jan 2007 Posts: 9
|
Posted: Fri Feb 02, 2007 6:52 pm Post subject: |
|
|
jcc7 wrote: | sean wrote: | It seems like a MD array of anything should work in D. Can't say why it isn't working offhand. | It seems that he got his MD array to work after he posted a question in the "learn" newsgroup:
"I seem to have solved my problem. I'm not sure what I am doing differently but it seems to work now "
digitalmars.D.learn:6311 |
Hello.
I did get it working. Must have been some simple noob mistake. I also upgraded from dmd 1.0 to 1.004.
My latest experimentation is along the likes of:
Code: |
import std.stdio;
import std.boxer;
void main() {
Box[][] ab = [
boxArray(25, "Hello World", 0.1)
, boxArray(1, "A", 0.1)
, boxArray(2, "B", 0.2)
, boxArray(3, "C", 0.3)
, boxArray(4, "D", 0.4)
, boxArray(9, "I", 0.9)
];
foreach (Box x[]; ab) {
writefln ("%d, %s, %f", unbox!(int)(x[0]), unbox!(char[])(x[1]), unbox!(real)(x[2]));
}
}
|
Seems really promising.
Regards,
Myron. |
|
Back to top |
|
|
MyronAlexander
Joined: 11 Jan 2007 Posts: 9
|
Posted: Mon Apr 30, 2007 10:34 am Post subject: Project created |
|
|
Hello.
After my last post I became busy and, for a while, could not look at hobby programming. Then, a few weeks ago, I had some free time so lo-and-behold, a rather primitive implementation.
The project is hosted on Google Code as "d-dbapi":
http://code.google.com/p/d-dbapi/
This version is not intended for general use and there will be changes so you have been warned BUT if you think you can do better, or have a suggestion, or think you can help, please let me know.
I don't mind any suggestions, be they style, or better error handling, tips for refactoring etc. I try not to be the ego type and believe I can learn from anything, even a "you suxorz" (well I learn that the commenter, like wine, needs some maturing )
All help will be most humbly appreciated. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|