123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- import { expectPerDialect, sequelize } from '../../support';
- const dialect = sequelize.dialect;
- const dialectName = dialect.name;
- const notSupportedError = new Error(`JSON Paths are not supported in ${dialectName}.`);
- describe('QueryGenerator#jsonPathExtractionQuery', () => {
- const queryGenerator = sequelize.queryGenerator;
- if (dialect.supports.jsonExtraction.quoted) {
- it('creates a json extract operation (object)', () => {
- // "jsonPathExtractionQuery" does not quote the first parameter, because the first parameter is *not* an identifier,
- // it can be any SQL expression, e.g. a column name, a function call, a subquery, etc.
- expectPerDialect(
- () =>
- queryGenerator.jsonPathExtractionQuery(
- queryGenerator.quoteIdentifier('profile'),
- ['id'],
- false,
- ),
- {
- default: notSupportedError,
- mariadb: `json_compact(json_extract(\`profile\`,'$.id'))`,
- 'mysql sqlite3': `json_extract(\`profile\`,'$.id')`,
- postgres: `"profile"->'id'`,
- },
- );
- });
- it('creates a json extract operation (array)', () => {
- expectPerDialect(
- () =>
- queryGenerator.jsonPathExtractionQuery(
- queryGenerator.quoteIdentifier('profile'),
- [0],
- false,
- ),
- {
- default: notSupportedError,
- mariadb: `json_compact(json_extract(\`profile\`,'$[0]'))`,
- 'mysql sqlite3': `json_extract(\`profile\`,'$[0]')`,
- postgres: `"profile"->0`,
- },
- );
- });
- it('creates a nested json extract operation', () => {
- expectPerDialect(
- () =>
- queryGenerator.jsonPathExtractionQuery(
- queryGenerator.quoteIdentifier('profile'),
- ['id', 'username', 0, '0', 'name'],
- false,
- ),
- {
- default: notSupportedError,
- mariadb: `json_compact(json_extract(\`profile\`,'$.id.username[0]."0".name'))`,
- 'mysql sqlite3': `json_extract(\`profile\`,'$.id.username[0]."0".name')`,
- postgres: `"profile"#>ARRAY['id','username','0','0','name']::VARCHAR(255)[]`,
- },
- );
- });
- it(`escapes characters such as ", $, and '`, () => {
- expectPerDialect(
- () =>
- queryGenerator.jsonPathExtractionQuery(
- queryGenerator.quoteIdentifier('profile'),
- [`"`, `'`, `$`],
- false,
- ),
- {
- default: notSupportedError,
- mysql: `json_extract(\`profile\`,'$."\\\\""."\\'"."$"')`,
- mariadb: `json_compact(json_extract(\`profile\`,'$."\\\\""."\\'"."$"'))`,
- sqlite3: `json_extract(\`profile\`,'$."\\""."''"."$"')`,
- postgres: `"profile"#>ARRAY['"','''','$']::VARCHAR(255)[]`,
- },
- );
- });
- }
- if (dialect.supports.jsonExtraction.unquoted) {
- it('creates a json extract+unquote operation (object)', () => {
- // "jsonPathExtractionQuery" does not quote the first parameter, because the first parameter is *not* an identifier,
- // it can be any SQL expression, e.g. a column name, a function call, a subquery, etc.
- expectPerDialect(
- () =>
- queryGenerator.jsonPathExtractionQuery(
- queryGenerator.quoteIdentifier('profile'),
- ['id'],
- true,
- ),
- {
- default: notSupportedError,
- mssql: `JSON_VALUE([profile], N'$.id')`,
- 'mariadb mysql sqlite3': `json_unquote(json_extract(\`profile\`,'$.id'))`,
- postgres: `"profile"->>'id'`,
- },
- );
- });
- it('creates a json extract+unquote operation (array)', () => {
- expectPerDialect(
- () =>
- queryGenerator.jsonPathExtractionQuery(
- queryGenerator.quoteIdentifier('profile'),
- [0],
- true,
- ),
- {
- default: notSupportedError,
- mssql: `JSON_VALUE([profile], N'$[0]')`,
- 'mariadb mysql sqlite3': `json_unquote(json_extract(\`profile\`,'$[0]'))`,
- postgres: `"profile"->>0`,
- },
- );
- });
- it('creates a nested json extract+unquote operation', () => {
- expectPerDialect(
- () =>
- queryGenerator.jsonPathExtractionQuery(
- queryGenerator.quoteIdentifier('profile'),
- ['id', 'username', 0, '0', 'name'],
- true,
- ),
- {
- default: notSupportedError,
- mssql: `JSON_VALUE([profile], N'$.id.username[0]."0".name')`,
- 'mysql mariadb sqlite3': `json_unquote(json_extract(\`profile\`,'$.id.username[0]."0".name'))`,
- postgres: `"profile"#>>ARRAY['id','username','0','0','name']::VARCHAR(255)[]`,
- },
- );
- });
- }
- });
|