Skip to content
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: 2 additions & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ func pluginQueries(r *compiler.Result) []*plugin.Query {
Params: params,
Filename: q.Metadata.Filename,
InsertIntoTable: iit,
IsReplace: q.IsReplace,
IgnoreErr: q.IgnoreErr,
})
}
return out
Expand Down
4 changes: 3 additions & 1 deletion internal/codegen/golang/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ type Query struct {
Ret QueryValue
Arg QueryValue
// Used for :copyfrom
Table *plugin.Identifier
Table *plugin.Identifier
IsReplace bool
IgnoreErr bool
}

func (q Query) hasRetType() bool {
Expand Down
2 changes: 2 additions & 0 deletions internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ func buildQueries(req *plugin.GenerateRequest, options *opts.Options, structs []
SQL: query.Text,
Comments: comments,
Table: query.InsertIntoTable,
IsReplace: query.IsReplace,
IgnoreErr: query.IgnoreErr,
}
sqlpkg := parseDriver(options.SqlPackage)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context{{if $.EmitMethodsWithDBArg
go convertRowsFor{{.MethodName}}(pw, {{.Arg.Name}})
// The string interpolation is necessary because LOAD DATA INFILE requires
// the file name to be given as a literal string.
result, err := {{if (not $.EmitMethodsWithDBArgument)}}q.{{end}}db.ExecContext(ctx, fmt.Sprintf("LOAD DATA LOCAL INFILE '%s' INTO TABLE {{.TableIdentifierForMySQL}} %s ({{range $index, $name := .Arg.ColumnNames}}{{if gt $index 0}}, {{end}}{{$name}}{{end}})", "Reader::" + rh, mysqltsv.Escaping))
result, err := {{if (not $.EmitMethodsWithDBArgument)}}q.{{end}}db.ExecContext(ctx, fmt.Sprintf("LOAD DATA LOCAL INFILE '%s' {{if .IsReplace}}REPLACE {{else if .IgnoreErr}}IGNORE {{end}}INTO TABLE {{.TableIdentifierForMySQL}} %s ({{range $index, $name := .Arg.ColumnNames}}{{if gt $index 0}}, {{end}}{{$name}}{{end}})", "Reader::" + rh, mysqltsv.Escaping))
if err != nil {
return 0, err
}
Expand Down
8 changes: 8 additions & 0 deletions internal/compiler/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

type analysis struct {
Table *ast.TableName
IsReplace bool
IgnoreErr bool
Columns []*Column
Parameters []Parameter
Named *named.ParamSet
Expand Down Expand Up @@ -142,6 +144,8 @@ func (c *Compiler) _analyzeQuery(raw *ast.RawStmt, query string, failfast bool)
raw, namedParams, edits := rewrite.NamedParameters(c.conf.Engine, raw, numbers, dollar)

var table *ast.TableName
var isReplace bool
var ignoreErr bool
switch n := raw.Stmt.(type) {
case *ast.InsertStmt:
if err := check(validate.InsertStmt(n)); err != nil {
Expand All @@ -152,6 +156,8 @@ func (c *Compiler) _analyzeQuery(raw *ast.RawStmt, query string, failfast bool)
if err := check(err); err != nil {
return nil, err
}
isReplace = n.IsReplace
ignoreErr = n.IgnoreErr
}

if err := check(validate.FuncCall(c.catalog, c.combo, raw)); err != nil {
Expand Down Expand Up @@ -207,6 +213,8 @@ func (c *Compiler) _analyzeQuery(raw *ast.RawStmt, query string, failfast bool)

return &analysis{
Table: table,
IsReplace: isReplace,
IgnoreErr: ignoreErr,
Columns: cols,
Parameters: params,
Query: expanded,
Expand Down
2 changes: 2 additions & 0 deletions internal/compiler/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
Columns: anlys.Columns,
SQL: trimmed,
InsertIntoTable: anlys.Table,
IsReplace: anlys.IsReplace,
IgnoreErr: anlys.IgnoreErr,
}, nil
}

Expand Down
2 changes: 2 additions & 0 deletions internal/compiler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type Query struct {

// Needed for CopyFrom
InsertIntoTable *ast.TableName
IsReplace bool
IgnoreErr bool

// Needed for vet
RawStmt *ast.RawStmt
Expand Down
92 changes: 92 additions & 0 deletions internal/endtoend/testdata/mysql_copyfrom_replace/db/copyfrom.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions internal/endtoend/testdata/mysql_copyfrom_replace/db/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions internal/endtoend/testdata/mysql_copyfrom_replace/db/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions internal/endtoend/testdata/mysql_copyfrom_replace/db/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/endtoend/testdata/mysql_copyfrom_replace/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- name: UpsertLocations :copyfrom
REPLACE INTO locations (id, name, address, latitude, longitude)
VALUES (?, ?, ?, ?, ?);

-- name: IgnoreLocations :copyfrom
INSERT IGNORE INTO locations (id, name, address, latitude, longitude)
VALUES (?, ?, ?, ?, ?);
7 changes: 7 additions & 0 deletions internal/endtoend/testdata/mysql_copyfrom_replace/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE locations (
id VARCHAR(512) PRIMARY KEY,
name TEXT NOT NULL,
address TEXT NOT NULL,
latitude FLOAT,
longitude FLOAT
);
11 changes: 11 additions & 0 deletions internal/endtoend/testdata/mysql_copyfrom_replace/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: "2"
sql:
- engine: "mysql"
queries: "query.sql"
schema: "schema.sql"
gen:
go:
package: "db"
out: "db"
sql_package: "database/sql"
sql_driver: "github.com/go-sql-driver/mysql"
2 changes: 2 additions & 0 deletions internal/engine/dolphin/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ func (c *cc) convertInsertStmt(n *pcast.InsertStmt) *ast.InsertStmt {
Relation: rangeVar,
Cols: c.convertColumnNames(n.Columns),
ReturningList: &ast.List{},
IsReplace: n.IsReplace,
IgnoreErr: n.IgnoreErr,
}
if ss, ok := c.convert(n.Select).(*ast.SelectStmt); ok {
ss.ValuesLists = c.convertLists(n.Lists)
Expand Down
16 changes: 16 additions & 0 deletions internal/plugin/codegen.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/sql/ast/insert_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type InsertStmt struct {
WithClause *WithClause
Override OverridingKind
DefaultValues bool // SQLite-specific: INSERT INTO ... DEFAULT VALUES
IsReplace bool // MySQL-specific
IgnoreErr bool // MySQL-specific
}

func (n *InsertStmt) Pos() int {
Expand Down
Loading