[OT] Idea to get all columns from multiple tables in DB

Steven Schveighoffer schveiguy at gmail.com
Thu Jan 9 16:02:01 UTC 2020

On 1/9/20 10:36 AM, Paolo Invernizzi wrote:
> We also are writing SQL statements by hand, but I've found that's really 
> nice to have a compile time check against the current db schemas. For 
> example, actually I can do:
> ---
> string stat = public!`select recording_id from recordings where 
> wrongcolname < 10`;
>   ~/dlang/dmd-2.089.0/osx/bin/dmd -i -Isrc -g -unittest -debug -c -o- 
> src/test.d
> /Users/pinver/dlang/dmd-2.089.0/osx/bin/../../src/phobos/std/exception.d(515): 
> Error: uncaught CTFE exception object.Error("column 'wrongcolname' does 
> not exist: 'select recording_id from recordings where wrongcolname < 10'")
> src/bkn/sql/semantic.d(75):        called from here: <bigsnip>
> ---

This is pretty cool. But of course, you need to have a SQL parser etc. 
in your code base to do this.

I've considered making the SQL building code more robust. Right now, it 
just captures joins and conditionals (the joins are only included if 
they are used), but I'd like to identify columns for tables and use less 
strings and more compile-time checking. I'm really not too interested in 
parsing SQL in my application :)

I like this library from Swift a lot (used in all my iOS projects), and 
have considered how D could do something similar: 

> The struct representing the schema is created reading and processing a 
> SQL file with the schema dump, dumping it once to a D module, but can be 
> done also at compile time.

Haha, that's really cool. So basically if there's a chicken or an egg, 
no matter, you can deal with it ;)

> That's pretty nice also using an IDE, for example I've configured VSCode 
> to compile-on-save and I've cool red underlines under the wrong sql 
> statements.
> The semantic pass is really basic right now, but I've plan to expand it.

Excellent. I think there's really a lot of home-grown SQL-based 
libraries out there. We should have a showcase of all of them and figure 
out the best features. Dealing with databases is really in D's 
wheelhouse, and there are so many different ways to solve the problems.


More information about the Digitalmars-d mailing list