Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 182a62a

Browse files
authored
fix: Add quote to generated query (#51)
2 parents b2e881e + a0668a3 commit 182a62a

File tree

10 files changed

+55
-29
lines changed

10 files changed

+55
-29
lines changed

internal/arcgen/lang/go/consts.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package arcgengo
2+
3+
const (
4+
importName = "orm"
5+
receiverName = "_orm"
6+
queryerContextVarName = "dbtx"
7+
queryerContextTypeName = "DBTX"
8+
createFuncPrefix = "Create"
9+
readOneFuncPrefix = "Get"
10+
readManyFuncPrefix = "List"
11+
updateFuncPrefix = "Update"
12+
deleteFuncPrefix = "Delete"
13+
quote = `"`
14+
)

internal/arcgen/lang/go/dialect.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"strconv"
55
"strings"
66

7+
"github.com/kunitsucom/arcgen/internal/arcgen/lang/util"
78
"github.com/kunitsucom/arcgen/internal/config"
89
)
910

@@ -54,15 +55,15 @@ func whereColumnsPlaceholder(columns []string, op string, initialNumber int) str
5455
switch config.Dialect() {
5556
case "mysql", "sqlite3":
5657
// column1 = ? AND column2 = ? AND column3 = ...
57-
return strings.Join(columns, " = ? "+op+" ") + " = ?"
58+
return util.JoinStringsWithQuote(columns, " = ? "+op+" ", quote) + " = ?"
5859
case "postgres", "cockroach":
5960
// column1 = $1 AND column2 = $2 AND column3 = ...
6061
var s strings.Builder
6162
for i, column := range columns {
6263
if i > 0 {
6364
s.WriteString(" " + op + " ")
6465
}
65-
s.WriteString(column)
66+
s.WriteString(util.QuoteString(column, quote))
6667
s.WriteString(" = $")
6768
s.WriteString(strconv.Itoa(i + initialNumber))
6869
}
@@ -74,7 +75,7 @@ func whereColumnsPlaceholder(columns []string, op string, initialNumber int) str
7475
if i > 0 {
7576
s.WriteString(" " + op + " ")
7677
}
77-
s.WriteString(column)
78+
s.WriteString(util.QuoteString(column, quote))
7879
s.WriteString(" = @")
7980
s.WriteString(column)
8081
}
@@ -86,13 +87,13 @@ func whereColumnsPlaceholder(columns []string, op string, initialNumber int) str
8687
if i > 0 {
8788
s.WriteString(" " + op + " ")
8889
}
89-
s.WriteString(column)
90+
s.WriteString(util.QuoteString(column, quote))
9091
s.WriteString(" = :")
9192
s.WriteString(column)
9293
}
9394
return s.String()
9495
default:
9596
// column1 = ? AND column2 = ? AND column3 = ...
96-
return strings.Join(columns, " = ? "+op+" ") + " = ?"
97+
return util.JoinStringsWithQuote(columns, " = ? "+op+" ", quote) + " = ?"
9798
}
9899
}

internal/arcgen/lang/go/generate_orm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func generateORMFileContent(buf buffer, arcSrcSet *ARCSourceSet) (string, error)
4747
structPackageImportPath := config.GoORMStructPackageImportPath()
4848
if structPackageImportPath == "" {
4949
var err error
50-
structPackageImportPath, err = util.GetPackageImportPath(filepath.Dir(arcSrcSet.Filename))
50+
structPackageImportPath, err = util.DetectPackageImportPath(filepath.Dir(arcSrcSet.Filename))
5151
if err != nil {
5252
return "", errorz.Errorf("GetPackagePath: %w", err)
5353
}

internal/arcgen/lang/go/generate_orm_common.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ import (
1616
"github.com/kunitsucom/arcgen/internal/config"
1717
)
1818

19-
const (
20-
importName = "orm"
21-
receiverName = "_orm"
22-
queryerContextVarName = "dbtx"
23-
queryerContextTypeName = "DBTX"
24-
readOneFuncPrefix = "Get"
25-
readManyFuncPrefix = "List"
26-
)
27-
2819
func fprintORMCommon(osFile osFile, buf buffer, arcSrcSetSlice ARCSourceSetSlice, ormFiles []string) error {
2920
content, err := generateORMCommonFileContent(buf, arcSrcSetSlice, ormFiles)
3021
if err != nil {
@@ -55,7 +46,7 @@ func generateORMCommonFileContent(buf buffer, arcSrcSetSlice ARCSourceSetSlice,
5546
structPackageImportPath := config.GoORMStructPackageImportPath()
5647
if structPackageImportPath == "" {
5748
var err error
58-
structPackageImportPath, err = util.GetPackageImportPath(filepath.Dir(arcSrcSetSlice[0].Filename))
49+
structPackageImportPath, err = util.DetectPackageImportPath(filepath.Dir(arcSrcSetSlice[0].Filename))
5950
if err != nil {
6051
return "", errorz.Errorf("GetPackagePath: %w", err)
6152
}

internal/arcgen/lang/go/generate_orm_create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"go/ast"
55
"go/token"
66
"strconv"
7-
"strings"
87

8+
"github.com/kunitsucom/arcgen/internal/arcgen/lang/util"
99
"github.com/kunitsucom/arcgen/internal/config"
1010
)
1111

@@ -27,7 +27,7 @@ func generateCREATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
2727
// }
2828
// return nil
2929
// }
30-
funcName := "Create" + structName
30+
funcName := createFuncPrefix + structName
3131
queryName := funcName + "Query"
3232
astFile.Decls = append(astFile.Decls,
3333
&ast.GenDecl{
@@ -37,7 +37,7 @@ func generateCREATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
3737
Names: []*ast.Ident{{Name: queryName}},
3838
Values: []ast.Expr{&ast.BasicLit{
3939
Kind: token.STRING,
40-
Value: "`INSERT INTO " + tableName + " (" + strings.Join(columnNames, ", ") + ") VALUES (" + columnValuesPlaceholder(columnNames, 1) + ")`",
40+
Value: "`INSERT INTO " + tableName + " (" + util.JoinStringsWithQuote(columnNames, ", ", `"`) + ") VALUES (" + columnValuesPlaceholder(columnNames, 1) + ")`",
4141
}},
4242
},
4343
},

internal/arcgen/lang/go/generate_orm_delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func generateDELETEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
2525
// }
2626
// return nil
2727
// }
28-
funcName := "Delete" + structName + "ByPK"
28+
funcName := deleteFuncPrefix + structName + "ByPK"
2929
queryName := funcName + "Query"
3030
pkColumns := tableInfo.Columns.PrimaryKeys()
3131
pkColumnNames := func() (pkColumnNames []string) {
@@ -140,7 +140,7 @@ func generateDELETEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
140140
// }
141141
// return nil
142142
// }
143-
byHasManyTagFuncName := "Delete" + structName + "By" + hasManyTag
143+
byHasManyTagFuncName := deleteFuncPrefix + structName + "By" + hasManyTag
144144
byHasManyTagQueryName := byHasManyTagFuncName + "Query"
145145
hasManyColumns := hasManyColumnsByTag[hasManyTag]
146146
astFile.Decls = append(astFile.Decls,

internal/arcgen/lang/go/generate_orm_read.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"go/ast"
55
"go/token"
66
"strconv"
7-
"strings"
87

98
"github.com/kunitsucom/arcgen/internal/arcgen/lang/util"
109
"github.com/kunitsucom/arcgen/internal/config"
@@ -43,7 +42,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
4342
Names: []*ast.Ident{{Name: byPKQueryName}},
4443
Values: []ast.Expr{&ast.BasicLit{
4544
Kind: token.STRING,
46-
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(pks.ColumnNames(), "AND", 1) + "`",
45+
Value: "`SELECT " + util.JoinStringsWithQuote(columnNames, ", ", `"`) + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(pks.ColumnNames(), "AND", 1) + "`",
4746
}},
4847
},
4948
},
@@ -192,7 +191,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
192191
Names: []*ast.Ident{{Name: byHasOneTagQueryName}},
193192
Values: []ast.Expr{&ast.BasicLit{
194193
Kind: token.STRING,
195-
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(hasOneColumns.ColumnNames(), "AND", 1) + "`",
194+
Value: "`SELECT " + util.JoinStringsWithQuote(columnNames, ", ", `"`) + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(hasOneColumns.ColumnNames(), "AND", 1) + "`",
196195
}},
197196
},
198197
},
@@ -359,7 +358,7 @@ func generateREADContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
359358
Names: []*ast.Ident{{Name: byHasOneTagQueryName}},
360359
Values: []ast.Expr{&ast.BasicLit{
361360
Kind: token.STRING,
362-
Value: "`SELECT " + strings.Join(columnNames, ", ") + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(hasManyColumns.ColumnNames(), "AND", 1) + "`",
361+
Value: "`SELECT " + util.JoinStringsWithQuote(columnNames, ", ", `"`) + " FROM " + tableName + " WHERE " + whereColumnsPlaceholder(hasManyColumns.ColumnNames(), "AND", 1) + "`",
363362
}},
364363
},
365364
},

internal/arcgen/lang/go/generate_orm_update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"go/ast"
55
"go/token"
66
"strconv"
7-
"strings"
87

8+
"github.com/kunitsucom/arcgen/internal/arcgen/lang/util"
99
"github.com/kunitsucom/arcgen/internal/config"
1010
)
1111

@@ -25,7 +25,7 @@ func generateUPDATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
2525
// }
2626
// return nil
2727
// }
28-
funcName := "Update" + structName
28+
funcName := updateFuncPrefix + structName
2929
queryName := funcName + "Query"
3030
pkColumns := tableInfo.Columns.PrimaryKeys()
3131
nonPKColumns := tableInfo.Columns.NonPrimaryKeys()
@@ -43,7 +43,7 @@ func generateUPDATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
4343
Names: []*ast.Ident{{Name: queryName}},
4444
Values: []ast.Expr{&ast.BasicLit{
4545
Kind: token.STRING,
46-
Value: "`UPDATE " + tableName + " SET (" + strings.Join(nonPKColumnNames, ", ") + ") = (" + columnValuesPlaceholder(nonPKColumnNames, 1) + ") WHERE " + whereColumnsPlaceholder(pkColumns.ColumnNames(), "AND", len(nonPKColumnNames)+1) + "`",
46+
Value: "`UPDATE " + tableName + " SET (" + util.JoinStringsWithQuote(nonPKColumnNames, ", ", `"`) + ") = (" + columnValuesPlaceholder(nonPKColumnNames, 1) + ") WHERE " + whereColumnsPlaceholder(pkColumns.ColumnNames(), "AND", len(nonPKColumnNames)+1) + "`",
4747
}},
4848
},
4949
},

internal/arcgen/lang/util/package_name.go renamed to internal/arcgen/lang/util/package_import_name.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
apperr "github.com/kunitsucom/arcgen/pkg/errors"
99
)
1010

11-
func GetPackageImportPath(path string) (string, error) {
11+
func DetectPackageImportPath(path string) (string, error) {
1212
absDir, err := filepath.Abs(path)
1313
if err != nil {
1414
return "", fmt.Errorf("filepath.Abs: path=%s %w", path, err)

internal/arcgen/lang/util/quote.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package util
2+
3+
import (
4+
"strings"
5+
)
6+
7+
func QuoteString(s string, quote string) string {
8+
return quote + s + quote
9+
}
10+
11+
func JoinStringsWithQuote(ss []string, sep string, quote string) string {
12+
if len(ss) == 0 {
13+
return ""
14+
}
15+
16+
if len(ss) == 1 {
17+
return QuoteString(ss[0], quote)
18+
}
19+
20+
return quote + strings.Join(ss, quote+sep+quote) + quote
21+
}

0 commit comments

Comments
 (0)