Good vs. Evil

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,799
248
96 I started toying with sqlx (an sql library for rust). The dev managed to do the impossible: typesave sql.
lets say I have the following code:
Code:
query!("SELECT id,username,password FROM users WHERE id = $1", 1)
it will construct the correct type at compile time to represent what I selected, if I have a typo in the table name I get an error about that during compile time. Same is true for the column names AND to top this all off I also get a nice and detailed error if the parameters that I give it have the wrong type.

It does this check by connecting to the database while being compiled and asks the database to prepare every query you want checked like that (Yes, it is even optional).

If you don't get why this is impressive: Normally the best you get is some types you can generate that in theory represent your database. However, there is nothing to keep those types and your database in sync. Those solutions also don't allow you to write sql directly but have "abstracted" that away in some way.