Type-Checking SQL Queries With Crystal Macros 0 ▲ Will Richardson 53 minutes ago · 17 min read3400 words · Tech · hide · 0 comments The aim of the game here is to use as few macros as possible. I could just have one macro that parses an SQL query, but the fun here is to use the Crystal type system to check the query for us, similar to how I used the type system to implement dependency injection. I’ll be avoiding macros, but I’ll be using macro methods heavily, as they let you introspect on the types of variables. The API should also be decomposable, which is more difficult with regular macros. You should be able to split up the query like you would any other expression and have it work as expected, maybe after including a few type annotations. What makes this process easier is modelling the queries on SQL pipe syntax, instead of traditional SQL. Pipe syntax is conceptually simpler, as each statement is just a transform of the output of the previous statement, and this will make our type-checking much easier. If you’ve not come across the pipe syntax, you can take this query: SELECT name, computer_count FROM people… No comments yet. Log in to reply on the Fediverse. Comments will appear here.