canonical / sqlair

Friendly type mapping for SQL databases
Apache License 2.0
17 stars 9 forks source link

Locating the original query once it's been transpiled to sql syntax from sqlair is non-obvious #164

Open SimonRichardson opened 6 days ago

SimonRichardson commented 6 days ago

I'm not expecting this to be solved now (or ever), but just an observation. Once queries have been transpiled from sqliar to SQL, locating the original query is no longer just a grep.

Consider the following output in the logs:

SELECT  u.external AS _sqlair_0,
        p.access_type AS _sqlair_1, p.uuid AS _sqlair_2
FROM    v_user_auth u
        LEFT JOIN v_permission p ON u.uuid = p.grant_to AND p.grant_on = @sqlair_0
        WHERE   u.name = @sqlair_1
        AND     u.disabled = false
        AND     u.removed = false

This was the original output:

SELECT  (u.external) AS (&dbPermissionUser.*),
        (p.access_type, p.uuid) AS (&dbPermission.*)
FROM    v_user_auth u
        LEFT JOIN v_permission p ON u.uuid = p.grant_to AND p.grant_on = $dbPermission.grant_on
WHERE   u.name = $dbPermissionUser.name
AND     u.disabled = false
AND     u.removed = false
SimonRichardson commented 6 days ago

One option is to enable a debug mode, which could input the original statement as a comment, thus allowing it to be grep-able.

-- SELECT  (u.external) AS (&dbPermissionUser.*),
--         (p.access_type, p.uuid) AS (&dbPermission.*)
-- FROM    v_user_auth u
--         LEFT JOIN v_permission p ON u.uuid = p.grant_to AND p.grant_on = $dbPermission.grant_on
-- WHERE   u.name = $dbPermissionUser.name
-- AND     u.disabled = false
-- AND     u.removed = false
SELECT  u.external AS _sqlair_0,
        p.access_type AS _sqlair_1, p.uuid AS _sqlair_2
FROM    v_user_auth u
        LEFT JOIN v_permission p ON u.uuid = p.grant_to AND p.grant_on = @sqlair_0
        WHERE   u.name = @sqlair_1
        AND     u.disabled = false
        AND     u.removed = false
SimonRichardson commented 6 days ago

Another alternative approach is to add a line number/position/file.

-- state.go:118
SELECT  u.external AS _sqlair_0,
        p.access_type AS _sqlair_1, p.uuid AS _sqlair_2
FROM    v_user_auth u
        LEFT JOIN v_permission p ON u.uuid = p.grant_to AND p.grant_on = @sqlair_0
        WHERE   u.name = @sqlair_1
        AND     u.disabled = false
        AND     u.removed = false