Skip to content

SQL quote #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion generator/client/golang/templates/queryx/insert.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *InsertStatement) ToSQL() (string, []interface{}) {
{{- if eq $.client.Adapter "mysql" }}
sql = fmt.Sprintf("%s(`%s`)", sql, strings.Join(s.columns, "`, `"))
{{- else }}
sql = fmt.Sprintf("%s(%s)", sql, strings.Join(s.columns, ", "))
sql = fmt.Sprintf("%s(\"%s\")", sql, strings.Join(s.columns, "\", \""))
{{- end }}
} else {
{{- if eq $.client.Adapter "mysql" }}
Expand Down
4 changes: 2 additions & 2 deletions generator/client/golang/templates/queryx/insert_test.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func TestNewInsert(t *testing.T) {

s3 := NewInsert().Into("users").Columns("name", "email").Values("test", "test@example.com")
sql, args = s3.ToSQL()
require.Equal(t, "INSERT INTO users(name, email) VALUES (?, ?)", sql)
require.Equal(t, "INSERT INTO users(\"name\", \"email\") VALUES (?, ?)", sql)
require.Equal(t, []interface{}{"test", "test@example.com"}, args)

s4 := NewInsert().Into("users").Columns("name", "email").
Values("test1", "test1@example.com").
Values("test2", "test2@example.com")
sql, args = s4.ToSQL()
require.Equal(t, "INSERT INTO users(name, email) VALUES (?, ?), (?, ?)", sql)
require.Equal(t, "INSERT INTO users(\"name\", \"email\") VALUES (?, ?), (?, ?)", sql)
require.Equal(t, []interface{}{"test1", "test1@example.com", "test2", "test2@example.com"}, args)
}
{{- end }}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ func TestSelect(t *testing.T) {
require.Equal(t, []interface{}{}, args)

sql, args = s.Update().Columns("name", "email").Values("test", "test@example.com").ToSQL()
require.Equal(t, `UPDATE users SET name = ?, email = ?`, sql)
{{- if eq .client.Adapter "mysql" }}
require.Equal(t, "UPDATE users SET `name` = ?, `email` = ?", sql)
{{- else }}
require.Equal(t, `UPDATE users SET "name" = ?, "email" = ?`, sql)
{{- end }}
require.Equal(t, []interface{}{"test", "test@example.com"}, args)

s1 := s.Limit(1)
Expand Down
22 changes: 22 additions & 0 deletions generator/client/golang/templates/queryx/update.go_test.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package queryx

import (
"testing"

"github.com/stretchr/testify/require"
)
{{- if eq .client.Adapter "mysql" }}
func TestNewUpdate(t *testing.T) {
s := NewUpdate().Table("users").Columns("name", "email").Values("test", "test@example.com")
sql, args := s.ToSQL()
require.Equal(t, "UPDATE users SET `name` = ?, `email` = ?", sql)
require.Equal(t, []interface{}{"test", "test@example.com"}, args)
}
{{- else }}
func TestNewUpdate(t *testing.T) {
s := NewUpdate().Table("users").Columns("name", "email").Values("test", "test@example.com")
sql, args := s.ToSQL()
require.Equal(t, `UPDATE users SET "name" = ?, "email" = ?`, sql)
require.Equal(t, []interface{}{"test", "test@example.com"}, args)
}
{{- end }}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func (s *UpdateStatement) ToSQL() (string, []interface{}) {

sets := []string{}
for _, col := range s.columns {
sets = append(sets, fmt.Sprintf("%s = ?", col))
{{- if eq .client.Adapter "mysql" }}
sets = append(sets, fmt.Sprintf("`%s` = ?", col))
{{- else }}
sets = append(sets, fmt.Sprintf("\"%s\" = ?", col))
{{- end }}
}

sql = fmt.Sprintf("%s %s", sql, strings.Join(sets, ", "))
Expand Down
14 changes: 0 additions & 14 deletions generator/client/golang/templates/queryx/update_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion generator/client/typescript/templates/queryx/insert.tstmpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class InsertStatement {
{{- if eq $.client.Adapter "mysql" }}
sql = `${sql} (\`${this._columns.join("`, `")}\`)`;
{{- else }}
sql = `${sql} (${this._columns.join(", ")})`;
sql = `${sql} (\"${this._columns.join("\", \"")}\")`;
{{- end }}
} else {
{{- if eq $.client.Adapter "mysql" }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ export class UpdateStatement {
let sets = [];
if (this._columns !== undefined) {
for (let col of this._columns) {
sets.push(`${col} = ?`);
{{- if eq $.client.Adapter "mysql" }}
sets.push(`\`${col}\` = ?`);
{{- else }}
sets.push(`\"${col}\" = ?`);
{{- end }}
}
}

Expand Down
19 changes: 17 additions & 2 deletions internal/integration/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ test("create", async () => {
expect(user.id).toBeGreaterThan(0);
});

test("delete", async () => {
await c.queryTag().create({ name: "delete_tag", right: 22 });
let rows = await c.queryTag().where(c.tagRight.eq(22)).deleteAll()
expect(rows).toBeGreaterThan(0);
});

test("updateAll", async () => {
await c.queryTag().deleteAll();
await c.queryTag().create({name:"name",right: 1});
let all=await c.queryTag().where(c.tagName.eq("name")).where(c.tagRight.eq(1)).updateAll({name:"name1",right: 0});
expect(all).toBeGreaterThan(0);
let exists=await c.queryTag().where(c.tagName.eq("name1")).exists();
expect(exists).toEqual(true);
});

test("insertAll", async () => {
await c.queryPost().deleteAll();
await expect(async () => {
Expand Down Expand Up @@ -397,8 +412,8 @@ test("changeJSON", async () => {
});

test("modelJSON", async () => {
let tag = await c.queryTag().create({ name: "test" });
expect(JSON.stringify(tag)).toEqual(`{"id":${tag.id},"name":"test"}`);
let tag = await c.queryTag().create({ name: "test",right: 1 });
expect(JSON.stringify(tag)).toEqual(`{"id":${tag.id},"name":"test","right":1}`);
});

test("modelString", async () => {
Expand Down
25 changes: 23 additions & 2 deletions internal/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ func TestCreate(t *testing.T) {
require.True(t, user.ID > 0)
}

func TestDelete(t *testing.T) {
_, err := c.QueryTag().Create(c.ChangeTag().SetRight(22).SetName("delete_tag"))
require.NoError(t, err)
rows, err := c.QueryTag().Where(c.TagRight.EQ(22)).DeleteAll()
require.NoError(t, err)
require.True(t, rows > 0)
}

func TestUpdateAll(t *testing.T) {
_, err := c.QueryTag().DeleteAll()
require.NoError(t, err)

tag, err := c.QueryTag().Create(c.ChangeTag().SetRight(1).SetName("name"))
require.NoError(t, err)

all, err := c.QueryTag().Where(c.TagID.EQ(tag.ID)).Where(c.TagRight.EQ(tag.Right.Val)).UpdateAll(c.ChangeTag().SetRight(0).SetName("name1"))
require.NoError(t, err)
require.True(t, all > 0)
require.Equal(t, int32(1), tag.Right.Val)
}

func TestInsertAll(t *testing.T) {
_, err := c.QueryUserPost().DeleteAll()
require.NoError(t, err)
Expand Down Expand Up @@ -494,11 +515,11 @@ func TestChangeJSON(t *testing.T) {
}

func TestModelJSON(t *testing.T) {
tag, err := c.QueryTag().Create(c.ChangeTag().SetName("test"))
tag, err := c.QueryTag().Create(c.ChangeTag().SetName("test").SetRight(1))
require.NoError(t, err)
b, err := json.Marshal(tag)
require.NoError(t, err)
require.Equal(t, fmt.Sprintf(`{"id":%d,"name":"test"}`, tag.ID), string(b))
require.Equal(t, fmt.Sprintf(`{"id":%d,"name":"test","right":1}`, tag.ID), string(b))
}

func TestModelStringer(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions internal/integration/mysql.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ database "db" {
type = string
}

column "right" {
type = integer
}

index {
columns = ["name"]
unique = true
Expand Down
4 changes: 4 additions & 0 deletions internal/integration/postgresql.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ database "db" {
type = string
}

column "right" {
type = integer
}

index {
columns = ["name",]
unique = true
Expand Down
4 changes: 4 additions & 0 deletions internal/integration/sqlite.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ database "db" {
type = string
}

column "right" {
type = integer
}

index {
columns = ["name"]
unique = true
Expand Down