15 ms·
For those that don't see the point of this yet, it's like a database query builder (or ORM) but for constructing regular expressions. The best implementation th
by zackmorris 6y ago
For those that don't see the point of this yet, it's like a database query builder (or ORM) but for constructing regular expressions. The best implementation that I know of is Eloquent for Laravel:
https://laravel.com/docs/master/eloquent https://laravel.com/docs/master/eloquent
The idea is that the idiosyncrasies of a DSL like SQL can be abstracted away by providing a functional interface that encodes the bugs/features of the grammar. The simplest example might be how SQL requires the WHERE clause to come before ORDER BY. But in Eloquent, clauses can generally be attached in any order. The clauses are stored as an abstract query object until they're executed, but the raw SQL can be retrieved at any time with the toSql() method, which is similar to the toRegex() method from the article:
https://laravel.com/api/master/Illuminate/Database/Query/Builder.html#method_toSql https://laravel.com/api/master/Illuminate/Database/Query/Bui...
You can also write your own methods to return clauses. Which allows you to write general queries like "select all of the articles from this user" and then append a clause like "limit to N results starting from X" when it comes time for a controller to run the query and return results to the user. I've found that composability is what makes query builders so exponentially more powerful than raw SQL.
Keep in mind that where Eloquent, this library (and most others) fall down is that they provide no way to go from a raw query (SQL, regex, whatever) back to a query object. Everyone is so busy writing software for writing software, that they forget that most of the everyday workload is in reading someone else's lackluster code.
I don't really know why I wrote all of this, but since I do most of my thinking in query builders now, I thought it might be a useful pattern for others to know.