iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😃

Why Not Just Use SQL Instead of a DSL?

に公開
2025/06/06
7件

ORMs are good at CUD operations, but they struggle with R (SELECT) operations.

While the goal of a DSL is to translate into SQL, you have to go out of your way to "reverse-translate" (into the DSL) even when you already know the answer (SQL).

On top of that, the syntax of DSLs varies between different ORM libraries, making it a world even more filled with dialects than SQL itself.

So I'd like to ask again: Why don't you use SQL?

Reasons for Avoiding SQL

I can imagine five major reasons:

  • Fear of SQL injection
  • Low versatility
  • Static analysis and compilation checks don't work
  • Mapping to entity models is tedious
  • Poor maintainability of long queries

Are these still really problems today?

Wouldn't it be solved if we could analyze and manipulate SQL?

Q: I'm afraid of SQL injection

Depending on the team's skill level, it can be a bit scary.
Concatenating SQL strings is dangerous.

A: No need to worry if you can synthesize SQL and search conditions

Analyzing and manipulating SQL can solve this.

First, represent the search conditions as a model.

const state = {
    price: { min: 10, max: 100 },
    article_name: { like: '%super%' }
};

Next, prepare the SQL.

-- baseQuery
SELECT
    a.article_id
    , a.article_name
    , a.price
FROM
    article AS a

If you could synthesize these and create a parameter query without writing any WHERE conditions at all...

SELECT
    a.article_id
    , a.article_name
    , a.price
FROM
    article AS a
WHERE
    a.price >= $1
    AND a.price <= $2
    AND a.article_name like $3

SQL might not be so bad after all.

https://github.com/mk3008/rawsql-ts/blob/4a63d2720a628ea34d103114aae59a76d49c47d2/tests/transformers/SqlParamInjector.test.ts#L40-L52

Q: Low versatility

SQL statements are hard-coded.
For complex search conditions, string concatenation is unavoidable.

A: No need to worry if you can synthesize SQL and search conditions at a high level

Analyzing SQL down to CTEs and subqueries and being able to manipulate search conditions against physical tables solves this.

First, prepare the search conditions.

const state = { user_id: 42, name: 'Alice' };

Next, prepare a complex SQL query.

WITH
    cte_users AS (
        SELECT
            id
            , name
        FROM
            users
        WHERE
            active = true
    )
SELECT
    *
FROM
    (
        SELECT
            id AS user_id
            , name AS user_name
        FROM
            cte_users
    ) AS sub

If we could synthesize these and insert search conditions at a position close to the physical data source...

WITH
    cte_users AS (
        SELECT
            id
            , name
        FROM
            users
        WHERE
            active = true
            AND name = $1
    )
SELECT
    *
FROM
    (
        SELECT
            id AS user_id
            , name AS user_name
        FROM
            cte_users
        WHERE
            id = $2
    ) AS sub

SQL might not be so bad after all.

https://github.com/mk3008/rawsql-ts/blob/4a63d2720a628ea34d103114aae59a76d49c47d2/tests/transformers/SqlParamInjectorComplex.test.ts#L66-L118

Q: Static analysis and compilation checks don't work

How can we detect inconsistencies on the query side when the schema changes?
How do we know if existing SQL has broken without noticing (regression)?

A: No need to worry if you can statically inspect SQL

By analyzing the schema used in the SQL, you can perform schema compliance checks.

First, prepare the schema information.

const tableSchemas = [
    { name: 'users', columns: ['id', 'name'] }
];

Next, prepare the SQL.

SELECT id, age FROM users

If you could synthesize these and detect errors...

Table 'users' contains undefined columns: age.

SQL might not be so bad after all.

https://github.com/mk3008/rawsql-ts/blob/4a63d2720a628ea34d103114aae59a76d49c47d2/tests/utils/SqlSchemaValidator.validate.test.ts#L88-L99

Mapping to entity models is tedious

Mapping is a pain.

A: No need to worry if you eliminate the need for mapping itself

This can be solved by transforming SQL into a query that returns JSON.

First, prepare the SQL.

SELECT
    sale_id
    , sale_date
    , sale_total
    , detail_id
    , detail_quantity
    , detail_price
    , product_id
    , product_name
    , product_category
FROM
    sale_full_report

Next, create the mapping definition.

const mapping: JsonMapping = {
    rootName: "Sales",
    rootEntity: {
        id: "sale",
        name: "Sale",
        columns: {
            "id": "sale_id",
            "date": "sale_date",
            "total": "sale_total"
        }
    },
    nestedEntities: [
        {
            id: "saleDetails",
            name: "SaleDetails",
            parentId: "sale",
            propertyName: "details",
            relationshipType: "array", // Sale has multiple SaleDetails (downstream)
            columns: {
                "id": "detail_id",
                "quantity": "detail_quantity",
                "price": "detail_price"
            }
        },
        {
            id: "product",
            name: "Product",
            parentId: "saleDetails",
            propertyName: "product",
            relationshipType: "object", // Each SaleDetail has one Product (upstream)
            columns: {
                "id": "product_id",
                "name": "product_name",
                "category": "product_category"
            }
        }
    ],
    useJsonb: true
}; 

If you could synthesize these and create a scalar query that returns JSON...

with "origin_query" as (select "sale_id" , "sale_date" , "sale_total" , "detail_id" , "detail_quantity" , "detail_price" , "product_id" , "product_name" , "product_category" from "sale_full_report") , "cte_object_depth_1" as (select * , case when "product_id" is null and "product_name" is null and "product_category" is null then null else jsonb_build_object('id', "product_id", 'name', "product_name", 'category', "product_category") end as "product_json" from "origin_query") , "cte_array_depth_1" as (select "product_json" , "sale_id" , "sale_date" , "sale_total" , "product_id" , "product_name" , "product_category" , jsonb_agg(jsonb_build_object('id', "detail_id", 'quantity', "detail_quantity", 'price', "detail_price", 'product', "product_json")) as "details" from "cte_object_depth_1" group by "product_json" , "sale_id" , "sale_date" , "sale_total" , "product_id" , "product_name" , "product_category") , "cte_root_sales" as (select jsonb_build_object('id', "sale_id", 'date', "sale_date", 'total', "sale_total", 'details', "details") as "Sales" from "cte_array_depth_1") select jsonb_agg("Sales") as "Sales_array" from "cte_root_sales"

SQL might not be so bad after all.

https://github.com/mk3008/rawsql-ts/blob/4a63d2720a628ea34d103114aae59a76d49c47d2/tests/transformers/hierarchical/GroupedHierarchyBuilder.test.ts#L307-L427

Poor Maintainability of Long Queries

DSLs are also quite hard to read when they are long, but the poor maintainability of long SQL is in a league of its own.
To debug CTEs or subqueries, you have to break the SQL structure.

A: I'm sorry!

I just couldn't manage this one.

Summary

Don't you think the issue of "SQL is no good" has been somewhat improved?

Aren't we potentially achieving productivity that surpasses DSLs while maintaining maintainability?

Since SQL's SELECT processing is very efficient, there's originally no reason not to use it.

If we can analyze and manipulate SQL, the possibilities for ORMs will expand!

That's what I believe.

Finally, here is a link to the library introduced in this article. By the way, I am the author.

https://github.com/mk3008/rawsql-ts

P.S.

I just couldn't manage this one.

I am currently looking for an answer to this problem. I think I am close to finding it.

Discussion

あいや - aiya000あいや - aiya000

僕は別解「直接GraphQLをサーバー・言語として使う」を考えました👀
https://chatgpt.com/share/6842f9d9-f180-8005-8f23-f4350b3117ce

僕もChatGPTの結論の「前述のGraphQL構成ではパフォーマンスチューニングに劣る」に同意していますが、今回の論点である以下には、ひとつの答えとしてもよさそうです!

DSLのゴールはSQLへの翻訳であるのに、答え(SQL)を知っていても、わざわざ逆翻訳(DSL)しなければならない。
その上、各ORMライブラリごとにDSLの文法は異なり、SQL以上に方言だらけの世界です。

3
M SugiuraM Sugiura

拙い記事にコメントいただき大変うれしいです。

あいやさんのコメントですが、「GraphQLをDSLとして使用する」と勝手に解釈しました。
「あり」だと思います。
なにせ私もやってることは「SQLをDSLとして使用」しているに過ぎないからです。


ORMのようなことをやろうとすると、

  • SQLのモデル化が必要になり、
  • モデルの組み立てを愚直にコンストラクタ使って記述するのは相当面倒なので、
  • ビルディングするなにか(DSL)が必要

になりがちです。

そのDSLは「ぼくのかんがえたさいきょうのなんたら」なので、ライブラリの思想そのものです。互換性というものはなくて当然。
思想に賛同する方をユーザにつけるという割り切りがあります。

ただ、これは後発組にはハードルが高い。
学習コストが高過ぎるし、未熟なライブラリに賭けるユーザは少ない。

そこで以下のように考えました。

  • 既に普及している記述に相乗りしよう
  • 不具合があった時に回避策を用意しよう

その結果、「DSLとしてSQLが採用された」に過ぎません。
SQLなら何かあってもライブラリを通さなければいいだけですからねw

1
NakamuraNakamura

私もSQLをよく使います!
最後の「長文の保守性が悪い」は、そもそも長文にしないというのはどうでしょうか。
まずはSQL初心者でもすぐに書けるシンプルなCRUDに留めて、それだとどうしてもパフォーマンスが落ちるときだけ長文にするのがいいと思います。
NoSQLで構築されたシステムも沢山あるわけで、どうしても長文にしなければならない場面は滅多に訪れないでしょう。

1
calloc134calloc134

素晴らしい記事ありがとうございます!Twitterの方でもRTさせていただきました。
自分も過去に似たような記事を書いたことがあります。是非こちらも興味があれば確認してみてください!
https://zenn.dev/calloc134/articles/4a8c1af0eb5aae

2
M SugiuraM Sugiura

記事の紹介ありがとうございます。バックエンド、SQL関係の記事、大変興味があります。

calloc134さんのカテゴリで言えば、私が紹介したライブラリは、「SQL→ コード のアプローチ」にあたります。

そのうえrawsql-ts特長を加えさせていただきます。

  • SQLに検索条件を書かなくてもよい
  • ASTを解析できる
  • 静的検査が可能(DB接続設定は要らない)
  • ゼロ依存。ブラウザですら動きます

デモサイト https://mk3008.github.io/rawsql-ts/

紹介していただいたライブラリについて想像で語ってしまいますが、RawSQLをパラメータクエリそのものとして扱っているのではないでしょうか?

例文ですと、以下のように書かれておりました。

SELECT * FROM authors WHERE id = $1 LIMIT 1;

このパラメータクエリですが、ID検索専用のように見えます。別の用途(たとえば名前検索)には使用できませんので、SQLが乱立しそうな印象を受けました。(もしかしたらSQLコメントを使用した独自構文があるのかも)

一方、rawsql-ts では上記のコードは以下のように書きます。(意味があるかは置いといて)

SELECT id, name FROM authors

静的条件は書きますが、動的検索条件は書かないのがポイントです。SQLのAST解析をしているため、1つの汎用的SQLを用意すれば、パラメータ定義に合わせて、以下のような多種多様のSQLを生成可能です。

--{id: 1, name: undefined}
SELECT id, name FROM authors where id = $1
--{id: undefined, name: { like: '%taro%' }}
SELECT id, name FROM authors where name like $1
--{id: 1, name: { like: '%taro%' }}
SELECT id, name FROM authors where id = $1 and  name like $2

本文での説明は省略しておりましたが、値にundefined を指定すると検索条件の対象から除外されます。これは任意検索条件に対して非常に強力で、IF文の数を大きく減らすことが出来ます。

id, name という列が存在するかどうかは、SQL自身から取得しています(例文はワイルドカードだったのでそこは変えました)。無ければエラー(静的検査可能)になります。ワイルドカードを使用したい場合は、スキーマ情報を引数に渡すことで解析は可能です。どちらの方法を採用するかはライブラリ利用者の裁量という考えです。

また、型安全については、かなり割り切ったアプローチをしています。

列「id」が存在しているかは分かりますが、できるのはそこまでです。列「id」の型が何であるかを把握することはしていません。ただ、SQLにおいては、厳密な型情報というのはそこまで必要ではないのでは、と考えています。なぜなら、SQL(DB)においては型はテーブルにて固定化されており、型情報は暗黙の了解として開発者に認知されていることが多いためです。ここは意見が分かれるところかもしれません。

ソヨカゼソヨカゼ

有用な記事をありがとうございます。
素朴な疑問なのですが、↓のSQL

WITH
cte_users AS (
SELECT
id
, name
FROM
users
WHERE
active = true
AND name = $1
)
SELECT
*
FROM
(
SELECT
id AS user_id
, name AS user_name
FROM
cte_users
WHERE
id = $2
) AS sub

を以下のようにすれば簡潔と思うのですが、そうしない理由はありますか?

WITH
cte_users AS (
SELECT
id AS user_id
, name AS user_name
FROM
users
WHERE
active = true
AND name = $1
)
SELECT
*
FROM
cte_users
WHERE
id = $2

なんならWHEREをくっつけて一文にすることもできそうです。
批判とかでなく、純粋な疑問です。ご教示いただけると幸いです。

1
M SugiuraM Sugiura

ご質問、ありがとうございます。
興味を持っていただけたことが素直にうれしいです。
では、本題のライブラリの機能について説明します。

該当のTypeScriptのコードを要点だけ記述するとこのようになります。

// クエリをパースしてモデル化
const baseQuery = SelectQueryParser.parse(sql) as SimpleSelectQuery;
// 検索条件を定義
const state = { user_id: 42, name: 'Alice' };

// 合成する
const injector = new SqlParamInjector();
const injectedQuery = injector.inject(baseQuery, state);

パラメータ合成のもととなるsqlですが、SQLの記述に縛りがなく、CTEやサブクエリ、ユニオンクエリが使用可能というのがポイントです。

次に、パラメータ合成クラスSqlParamInjectorですが、

  • 検索条件プロパティ名と同一の列名を探し出して、検索条件を挿入する
  • データソース(FROM句)がサブクエリ、またはCTEである場合、より上位のものを優先する
  • ユニオンクエリである場合、それぞれが加工対象になる

という仕様で検索条件を合成します。

このため、生成されるSQLは元のSQLに強く依存します。

例文では、一般的には書かないようなSQLを書いています。実践ならば以下のクエリを書くべきです。

SELECT
    id as user_id
    , name as user_name
FROM
    users
WHERE
    active = true

これを無駄に複雑に記述(サブクエリとCTEを使い、かつ列名のエイリアスもばらばらに定義)したものがアレです。
あくまで複雑なクエリのメタファー として見ていただければと思います。「複雑である」こと以上の意味はありません。

そしてこのようなサブクエリ、CTEを使用した複雑なクエリは、以下のように階層を解析されます。

--level 1: SelectQuery
SELECT
    user_id
    , user_name
FROM
    sub
;
--level 2: SubQuery
SELECT
    id AS user_id
    , name AS user_name
FROM
    cte_users
;
--level3: CTE
SELECT
    id
    , name
FROM
    users
WHERE
    active = true

複雑なのはuser_idだと思うので、こちらを説明しますと、user_idはlevel1に存在するので、level1のクエリが加工の候補になります。ただし、user_idはサブクエリであるため、上流に定義があるとして、level2が検査対象になります。level2を検査するとuser_idは列エイリアスであることが分かります。列エイリアスの場合、検査を打ち切りしますので、level2がuser_idが定義されている最深部である、と判定され、level2のクエリに検索条件が挿入される。

name も同じ理屈で検査するので、level3のクエリに検索条件が挿入されます。

実際のロジックはこれとは異なります。実際は最深部から検査を行い、最深部に定義があれば探索を打ち切り、なければ1個前を探すを繰り返します。このため、一番最後のSELECTクエリに存在しない列名であってもフィルタは可能です。列の存在はSQLそのものから解析しますが、スキーマ情報を与えた場合はそこも参照されます。


というわけで、サンプルのSQL自体はあまり気になさらず、
CTE、サブクエリ、ユニオンクエリを使用した場合もできるだけ有効な場所にパラメータを挿入してくれる と思っていただけば幸いです。

ライブラリは npm で公開しておりますので、よろしければお試しください。またドキュメントも用意してありますので、よろしければどうぞ。
https://github.com/mk3008/rawsql-ts/blob/main/docs/usage-guides/class-SqlParamInjector-usage-guide.md