Skip to content

Commit

Permalink
fix bind with array
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzuo committed Aug 6, 2024
1 parent 8c3e16c commit f6f1b97
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
26 changes: 16 additions & 10 deletions generator/client/typescript/templates/queryx/adapter.postgresql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Adapter {

private _query<R extends pg.QueryResultRow = any, I extends any[] = any[]>(
query: string,
args?: I,
args?: I
) {
let [query1, args1] = rebind<I>(query, args);
return this.db.query<R, I>(query1, args1);
Expand Down Expand Up @@ -77,19 +77,25 @@ export function rebind<T extends any[] = any[]>(query: string, args?: T) {
let str = "";
let i = 0;
let j = 1;
let k = 0;
let args1: any[] = [];

for (i = query.indexOf("?"); i !== -1; i = query.indexOf("?")) {
while (i !== -1) {
i = query.indexOf("?");
str += query.substring(0, i);

if (Array.isArray(args[j - 1])) {
args1 = args1.concat(args[j - 1]);
str += args[j - 1].map((_, i) => "$" + (j + i)).join(", ");
j += args.length;
} else {
args1.push(args[j - 1]);
str += "$" + j;
j++;
if (args.length > k) {
const arg = args[k];
if (Array.isArray(arg)) {
args1 = args1.concat(arg);
str += arg.map((_, i) => "$" + (j + i)).join(", ");
j += arg.length;
} else {
args1.push(arg);
str += "$" + j;
j++;
}
k++;
}

query = query.substring(i + 1);
Expand Down
14 changes: 14 additions & 0 deletions internal/integration/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,20 @@ test("in", async () => {
.where(c.userName.nin([user1.name, user2.name]))
.all();
expect(users4).toEqual([user3]);

const users5 = await c
.queryUser()
.where(c.userID.in([user1.id]))
.where(c.userName.eq(user1.name))
.all();
expect(users5).toEqual([user1]);

const users6 = await c
.queryUser()
.where(c.userID.in([user1.id, user2.id]))
.where(c.userName.eq(user1.name))
.all();
expect(users6).toEqual([user1]);
});

test("inEmpty", async () => {
Expand Down
8 changes: 8 additions & 0 deletions internal/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,14 @@ func TestIn(t *testing.T) {
users4, _ := c.QueryUser().Where(c.UserName.NIn([]string{user1.Name.Val, user2.Name.Val})).All()
require.Equal(t, 1, len(users4))
require.Equal(t, user3, users4[0])

user5, _ := c.QueryUser().Where(c.UserID.EQ(user1.ID)).Where(c.UserName.EQ(user1.Name.Val)).All()
require.Equal(t, 1, len(user5))
require.Equal(t, user1, user5[0])

user6, _ := c.QueryUser().Where(c.UserID.In([]int64{user1.ID, user2.ID})).Where(c.UserName.EQ(user1.Name.Val)).All()
require.Equal(t, 1, len(user6))
require.Equal(t, user1, user6[0])
}

func TestInEmpty(t *testing.T) {
Expand Down

0 comments on commit f6f1b97

Please sign in to comment.