Hacker News new | ask | show | jobs
by rwmj 3635 days ago
It's possible in OCaml to use a preprocessor (eg. camlp4) to integrate another language at compile time. I did this with PG'OCaml (combining Postgres's SQL & OCaml). At compile time the SQL statements are sent to the DBMS to be "described" (syntax and type checking), and then we insert the necessary glue code to prepare the SQL and convert OCaml values to and from SQL columns and results. The result is type-safe across the two languages, and free of SQL injections. You literally cannot write non-well-formed or badly typed SQL and have the program still compile.

http://pgocaml.forge.ocamlcore.org/

1 comments

What happens if somebody does an ALTER TABLE changing a column type? I think it's fair to just not deal with that case, but I'm curious if you did more than that.
Then you'll get a runtime error. However if you recompiled before running the program you'd get a compile time error, so it's probably best to recompile your source after any schema change, although bad stuff won't happen if you don't do that.