marten icon indicating copy to clipboard operation
marten copied to clipboard

Add support for raw subqueries

Open ellmetha opened this issue 3 years ago • 0 comments

Description

The Marten ORM presently allows the create of raw SQL queries for which the whole SQL statement must be provided and whose results are automatically mapped to model instances.

For example:

Article.raw("SELECT * FROM articles WHERE title = ?", "hello")
Article.raw("SELECT * FROM articles WHERE title = :title AND updated_at > :updated_at", title: "hello", updated_at: Time.local)

For cases where it's not needed to specify model field columns directly, it could be interesting to have the ability to filter records by simply providing a raw subquery predicate (eg. title = ?) and the associated parameter(s). When doing so, the resulting query set should allow "chaining" additional filters afterwards if necessary.

For example:

Article.all.filter("title = ?", "hello")
Article.all.filter("title = :title AND updated_at > :updated_at", title: "hello", updated_at: Time.local)

The advantage of doing so is that the selection of the actual model field columns is left to Marten's ORM, which simplifies the use of raw SQL predicates and avoid having to specify full SQL statements.

ellmetha avatar Nov 11 '22 12:11 ellmetha