Skip to content

Commit 67c7e10

Browse files
committed
added test
1 parent 8653d7d commit 67c7e10

3 files changed

Lines changed: 80 additions & 2 deletions

File tree

integration-tests/tests/cockroach/waddler.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,3 +597,77 @@ test('insert benchmark', async () => {
597597

598598
await sql.unsafe(`drop table tests;`);
599599
});
600+
601+
test('1d array of strings, integer as SQLCommonParam test', async () => {
602+
await sql.unsafe(`create table tests(
603+
id int4,
604+
path string
605+
);
606+
`);
607+
608+
const valuesToInsert = [[1, '/'], [2, '/watch'], [3, '/over_watch']];
609+
610+
await sql`insert into tests values ${sql.values(valuesToInsert)};`;
611+
612+
// case0
613+
const query0 = sql`select * from tests where path = any(${['/', '/watch']});`;
614+
expect(query0.toSQL()).toStrictEqual({
615+
sql: 'select * from tests where path = any($1);',
616+
params: [['/', '/watch']],
617+
});
618+
619+
const res0 = await query0;
620+
expect(res0).toStrictEqual([{ id: 1, path: '/' }, { id: 2, path: '/watch' }]);
621+
622+
// case1 (int32 max)
623+
const query1 = sql`select ${2_147_483_647} as max_int32;`;
624+
expect(query1.toSQL()).toStrictEqual({
625+
sql: 'select $1::int4 as max_int32;',
626+
params: [2147483647],
627+
});
628+
629+
const res1 = await query1;
630+
expect(res1).toStrictEqual([{ max_int32: 2147483647 }]);
631+
632+
// case2 (int32 min)
633+
const query2 = sql`select ${-2_147_483_648} as min_int32;`;
634+
expect(query2.toSQL()).toStrictEqual({
635+
sql: 'select $1::int4 as min_int32;',
636+
params: [-2147483648],
637+
});
638+
639+
const res2 = await query2;
640+
expect(res2).toStrictEqual([{ min_int32: -2147483648 }]);
641+
642+
// case3 (float)
643+
const query3 = sql`select ${sql.param(-2_147_483_648.123, 'double precision')} as some_float;`;
644+
expect(query3.toSQL()).toStrictEqual({
645+
sql: 'select $1::double precision as some_float;',
646+
params: [-2147483648.123],
647+
});
648+
649+
const res3 = await query3;
650+
expect(res3).toStrictEqual([{ some_float: -2147483648.123 }]);
651+
652+
await sql.unsafe(`drop table tests;`);
653+
654+
// case4 (int32 max + 1)
655+
const query4 = sql`select ${2_147_483_647 + 1} as max_int32_plus_one;`;
656+
expect(query4.toSQL()).toStrictEqual({
657+
sql: 'select $1::int8 as max_int32_plus_one;',
658+
params: [2147483648],
659+
});
660+
661+
const res4 = await query4;
662+
expect(res4).toStrictEqual([{ max_int32_plus_one: '2147483648' }]);
663+
664+
// case5 (bigint)
665+
const query5 = sql`select ${BigInt(1)} as bigint_;`;
666+
expect(query5.toSQL()).toStrictEqual({
667+
sql: 'select $1::int8 as bigint_;',
668+
params: [1n],
669+
});
670+
671+
const res5 = await query5;
672+
expect(res5).toStrictEqual([{ bigint_: '1' }]);
673+
});

integration-tests/tests/pg/pg-core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export const commonPgTests = (dialect?: string) => {
144144
test('base test with bigint param', (ctx) => {
145145
const res = ctx.sql`select ${BigInt(10)};`.toSQL();
146146

147-
expect(res).toStrictEqual({ sql: `select $1;`, params: [10n] });
147+
expect(res).toStrictEqual({ sql: `select $1${dialect === 'cockroach' ? '::int8' : ''};`, params: [10n] });
148148
});
149149

150150
test('base test with string param', (ctx) => {

waddler/src/cockroach/cockroach-core/dialect.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ export type DbType =
104104
| 'int8'
105105
| 'numeric'
106106
| 'decimal'
107+
| 'float'
107108
| 'real'
108-
| 'precision'
109+
| 'double precision'
109110
| 'boolean'
110111
| 'char'
111112
| 'varchar'
@@ -136,6 +137,9 @@ export class CockroachSQLCommonParam extends SQLCommonParam {
136137
override generateSQL(
137138
{ dialect, lastParamIdx }: { dialect: Dialect; lastParamIdx: number },
138139
) {
140+
// bigint case
141+
if (typeof this.value === 'bigint') this.type = 'int8';
142+
139143
// integer case
140144
if (typeof this.value === 'number' && this.value % 1 === 0) {
141145
this.type = 'int4';

0 commit comments

Comments
 (0)