11import type { ClickHouseClient } from '@clickhouse/client' ;
22import { createClient } from '@clickhouse/client' ;
33import type { NodeClickHouseClientConfigOptions } from '@clickhouse/client/dist/config' ;
4- import { SQLDefault , SQLIdentifier , SQLRaw , SQLValues } from '~/sql-template-params.ts' ;
4+ import { SQLCommonParam , SQLDefault , SQLIdentifier , SQLQuery , SQLRaw , SQLValues } from '~/sql-template-params.ts' ;
55import { isConfig } from '~/utils.ts' ;
66import type { DbType } from '../clickhouse-core/index.ts' ;
77import { ClickHouseDialect , UnsafePromise } from '../clickhouse-core/index.ts' ;
@@ -63,28 +63,55 @@ export interface ClickHouseSQL extends Omit<SQL, 'unsafe' | 'values'> {
6363 * ```
6464 */
6565 values ( value : Values , types ?: DbType [ ] ) : SQLValues ;
66+ param ( value : any , type : DbType ) : SQLCommonParam ;
6667}
6768
69+ export interface ClickHouseSQLQuery
70+ extends Pick < ClickHouseSQL , 'values' | 'param' > , Pick < SQL , 'identifier' | 'raw' | 'default' >
71+ {
72+ ( strings : TemplateStringsArray , ...params : SQLParamType [ ] ) : SQLQuery ;
73+ }
74+
75+ const SQLFunctions = {
76+ identifier : ( value : Identifier < IdentifierObject > ) => {
77+ return new SQLIdentifier ( value ) ;
78+ } ,
79+ values : ( value : Values , types ?: DbType [ ] ) => {
80+ return new SQLValues ( value , types ) ;
81+ } ,
82+ param : ( value : any , type ?: DbType ) => {
83+ return new SQLCommonParam ( value , type ) ;
84+ } ,
85+ raw : ( value : Raw ) => {
86+ return new SQLRaw ( value ) ;
87+ } ,
88+ default : new SQLDefault ( ) ,
89+ } ;
90+
91+ const sql = ( ( strings : TemplateStringsArray , ...params : SQLParamType [ ] ) : SQLQuery => {
92+ const sqlWrapper = new SQLWrapper ( ) ;
93+ sqlWrapper . with ( { templateParams : { strings, params } } ) ;
94+ const dialect = new ClickHouseDialect ( ) ;
95+
96+ return new SQLQuery ( sqlWrapper , dialect ) ;
97+ } ) as ClickHouseSQLQuery ;
98+
99+ Object . assign ( sql , SQLFunctions ) ;
100+
101+ export { sql } ;
102+
68103const createSqlTemplate = (
69104 client : ClickHouseClient ,
70105 dialect : ClickHouseDialect ,
71106) : ClickHouseSQL => {
72107 const fn = < T > ( strings : TemplateStringsArray , ...params : SQLParamType [ ] ) : ClickHouseSQLTemplate < T > => {
73- const sql = new SQLWrapper ( ) ;
74- sql . with ( { templateParams : { strings, params } } ) . prepareQuery ( dialect ) ;
75- return new ClickHouseSQLTemplate < T > ( sql , client , dialect ) ;
108+ const sqlWrapper = new SQLWrapper ( ) ;
109+ sqlWrapper . with ( { templateParams : { strings, params } } ) . prepareQuery ( dialect ) ;
110+ return new ClickHouseSQLTemplate < T > ( sqlWrapper , client , dialect ) ;
76111 } ;
77112
78113 Object . assign ( fn , {
79- identifier : ( value : Identifier < IdentifierObject > ) => {
80- return new SQLIdentifier ( value ) ;
81- } ,
82- values : ( value : Values , types ?: DbType [ ] ) => {
83- return new SQLValues ( value , types ) ;
84- } ,
85- raw : ( value : Raw ) => {
86- return new SQLRaw ( value ) ;
87- } ,
114+ ...SQLFunctions ,
88115 unsafe : (
89116 query : string ,
90117 params ?: UnsafeParamType [ ] ,
@@ -93,15 +120,14 @@ const createSqlTemplate = (
93120 params = params ?? [ ] ;
94121 options = options ?? { rowMode : 'object' } ;
95122
96- const sql = new SQLWrapper ( ) ;
97- sql . with ( { rawParams : { query, params } } ) ;
123+ const sqlWrapper = new SQLWrapper ( ) ;
124+ sqlWrapper . with ( { rawParams : { query, params } } ) ;
98125
99- const unsafeDriver = new ClickHouseSQLTemplate ( sql , client , dialect , options ) ;
126+ const unsafeDriver = new ClickHouseSQLTemplate ( sqlWrapper , client , dialect , options ) ;
100127 const unsafePromise = new UnsafePromise ( unsafeDriver ) ;
101128
102129 return unsafePromise ;
103130 } ,
104- default : new SQLDefault ( ) ,
105131 } ) ;
106132
107133 return fn as any ;
0 commit comments