oguimbal / pg-mem

An in memory postgres DB instance for your unit tests
MIT License
1.95k stars 94 forks source link

Support `FOR UPDATE OF` syntax #321

Open ranmocy opened 1 year ago

ranmocy commented 1 year ago

Query:

SELECT * FROM "ABC" WHERE "ABC"."id" = 1 FOR UPDATE OF "ABC";

I got error:

Unexpected word token: \"of\". Instead, I was expecting to see one of the following:
    - A \"kw_union\" token
    - A \"semicolon\" token

I understand pg-mem doesn't intent to support concurrency. Could you help update the syntax parser so that FOR UPDATE OF "ABC" could be parsed but ignored during execution? At least not crash the test:)

youngkiu commented 2 months ago

I solved it as follows, similar to https://github.com/oguimbal/pg-mem/issues/149#issuecomment-1125448707.

    db.public.interceptQueries(sql => {
        const newSql = sql.replace(/for\s+update\sof\s+"([^"]*)"+/gi, 'FOR UPDATE');
        if (sql !== newSql) {
            return db.public.many(newSql);
        }

        // proceed to actual SQL execution for other requests.
        return null;
    });
youngkiu commented 2 months ago

To fundamentally resolve the issue, I have published an issue in another repository. https://github.com/oguimbal/pgsql-ast-parser/issues/164