-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './utils/format'; | ||
export * from './utils/tag'; | ||
export * from './utils/sql-utils'; | ||
export * from './client/clients'; | ||
export * from './client/broker/transport/transports'; | ||
export * from './client/errors/pinot'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Sql } from '@no-esm/sql-template-tag'; | ||
import { IPinotQueryOptions } from '../client/clients'; | ||
import { SqlFormat } from './format'; | ||
|
||
export class SqlUtils { | ||
static formatOptions(options?: IPinotQueryOptions): string { | ||
if (!options) { | ||
return ''; | ||
} | ||
return Object.entries(options) | ||
.map(([key, value]) => { | ||
switch (typeof value) { | ||
case 'string': | ||
return `SET ${key} = '${value}';`; | ||
case 'number': | ||
return `SET ${key} = ${value};`; | ||
case 'boolean': | ||
return `SET ${key} = ${value};`; | ||
default: | ||
return `SET ${key} = '${value}';`; | ||
} | ||
}) | ||
.join('\n'); | ||
} | ||
|
||
static stringifyQuery(query: Sql, options?: IPinotQueryOptions) { | ||
const sql = SqlFormat.format(query.sql, query.values); | ||
|
||
return [SqlUtils.formatOptions(options), sql].filter((x) => !!x).join('\n'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as assert from 'node:assert'; | ||
import { describe, test } from 'node:test'; | ||
import { IPinotQueryOptions, sql, SqlUtils } from '../src'; | ||
|
||
void describe('Sql Utils', async () => { | ||
await test('generates query with no options', () => { | ||
const query = sql`SELECT * FROM table WHERE id = ${1}`; | ||
|
||
assert.strictEqual( | ||
SqlUtils.stringifyQuery(query), | ||
'SELECT * FROM table WHERE id = 1', | ||
); | ||
}); | ||
|
||
await test('generates query with options', () => { | ||
const query = sql`SELECT * FROM table WHERE id = ${1}`; | ||
const options: IPinotQueryOptions = { | ||
useMultistageEngine: true, | ||
timeoutMs: 100, | ||
inPredicateLookupAlgorithm: 'SCAN', | ||
}; | ||
|
||
assert.strictEqual( | ||
SqlUtils.stringifyQuery(query, options), | ||
`SET useMultistageEngine = true;\nSET timeoutMs = 100;\nSET inPredicateLookupAlgorithm = 'SCAN';\nSELECT * FROM table WHERE id = 1`, | ||
); | ||
}); | ||
}); |