Skip to content
Merged
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 go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/upfluence/sql

go 1.21
go 1.23

require (
github.com/lib/pq v1.10.9
Expand Down
49 changes: 43 additions & 6 deletions x/sqlbuilder/insert_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import (
"fmt"
"io"
"slices"
"strings"

"github.com/upfluence/sql"
)
Expand Down Expand Up @@ -30,20 +32,20 @@
}

func (oct *OnConflictTarget) WriteTo(qw QueryWriter, vs map[string]interface{}) error {
io.WriteString(qw, "(")

Check failure on line 35 in x/sqlbuilder/insert_statement.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

Error return value of `io.WriteString` is not checked (errcheck)

for i, f := range oct.Fields {
io.WriteString(qw, columnName(f))

Check failure on line 38 in x/sqlbuilder/insert_statement.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

Error return value of `io.WriteString` is not checked (errcheck)

if i < len(oct.Fields)-1 {
io.WriteString(qw, ", ")

Check failure on line 41 in x/sqlbuilder/insert_statement.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

Error return value of `io.WriteString` is not checked (errcheck)
}
}

io.WriteString(qw, ")")

Check failure on line 45 in x/sqlbuilder/insert_statement.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

Error return value of `io.WriteString` is not checked (errcheck)

if oct.Where != nil {
io.WriteString(qw, " WHERE ")

Check failure on line 48 in x/sqlbuilder/insert_statement.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

Error return value of `io.WriteString` is not checked (errcheck)

return oct.Where.WriteTo(qw, vs)
}
Expand All @@ -66,7 +68,7 @@

func (Update) isOnConflictAction() {}
func (ms Update) WriteTo(qw QueryWriter, vs map[string]interface{}) error {
io.WriteString(qw, "UPDATE SET ")

Check failure on line 71 in x/sqlbuilder/insert_statement.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

Error return value of `io.WriteString` is not checked (errcheck)

return writeUpdateClauses(ms, qw, vs)
}
Expand Down Expand Up @@ -104,8 +106,14 @@

Fields []Marker

// Deprecated: Please use Returnings
Returning *sql.Returning

Returnings []*sql.Returning

OnConfict *OnConflictClause

isQuery bool
}

func (is InsertStatement) Clone() InsertStatement {
Expand All @@ -118,11 +126,25 @@
}

return InsertStatement{
Table: is.Table,
Fields: cloneMarkers(is.Fields),
Returning: r,
OnConfict: is.OnConfict.Clone(),
Table: is.Table,
Fields: cloneMarkers(is.Fields),
Returnings: slices.Clone(is.Returnings),
Returning: r,
isQuery: is.isQuery,
OnConfict: is.OnConfict.Clone(),
}
}

func (is InsertStatement) returnings() []*sql.Returning {
var res []*sql.Returning

res = append(res, is.Returnings...)

if is.Returning != nil {
res = append(res, is.Returning)
}

return res
}

func (is InsertStatement) buildQuery(qvs map[string]interface{}) (string, []interface{}, error) {
Expand Down Expand Up @@ -190,8 +212,23 @@
}
}

if is.Returning != nil {
qw.vs = append(qw.vs, is.Returning)
switch rs := is.returnings(); len(rs) {
case 0:
case 1:
if !is.isQuery {
qw.vs = append(qw.vs, is.Returning)
break

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
break with no blank line before (nlreturn)

}

fallthrough
default:
var fields = make([]string, len(rs))

for i, r := range rs {
fields[i] = r.Field
}

fmt.Fprintf(&qw, " RETURNING %s", strings.Join(fields, ", "))
}

return qw.String(), qw.vs, nil
Expand Down
26 changes: 26 additions & 0 deletions x/sqlbuilder/insert_statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ func TestInsertQuery(t *testing.T) {
stmt: "INSERT INTO foo(buz) VALUES ($1) ON CONFLICT (buz) DO UPDATE SET bar = $2",
args: []interface{}{1, 2},
},
{
name: "with returning + isQuery",
is: InsertStatement{
Table: "foo",
Fields: []Marker{Column("buz")},
Returning: &sql.Returning{Field: "buz"},
isQuery: true,
},
vs: map[string]interface{}{"buz": 1},
stmt: "INSERT INTO foo(buz) VALUES ($1) RETURNING buz",
args: []interface{}{1},
},
{
name: "with returnings ",
is: InsertStatement{
Table: "foo",
Fields: []Marker{Column("buz")},
Returnings: []*sql.Returning{
{Field: "buz"},
{Field: "bar"},
},
},
vs: map[string]interface{}{"buz": 1},
stmt: "INSERT INTO foo(buz) VALUES ($1) RETURNING buz, bar",
args: []interface{}{1},
},
} {
t.Run(tt.name, func(t *testing.T) {
stmt, args, err := tt.is.Clone().buildQuery(tt.vs)
Expand Down
21 changes: 21 additions & 0 deletions x/sqlbuilder/query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ type InsertExecer struct {
Statement InsertStatement
}

type errScanner struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
the error type name errScanner should conform to the xxxError format (errname)

error
}

func (es errScanner) Scan(...interface{}) error {
return es.error
}

func (ie *InsertExecer) QueryRow(ctx context.Context, qvs map[string]interface{}) sql.Scanner {
stmt := ie.Statement
stmt.isQuery = true

sstmt, vs, err := stmt.buildQuery(qvs)

if err != nil {
return errScanner{error: err}
}

return ie.qb.QueryRow(ctx, sstmt, vs...)
}

func (ie *InsertExecer) MultiExec(ctx context.Context, vvs []map[string]interface{}, qvs map[string]interface{}) (sql.Result, error) {
stmt, vs, err := ie.Statement.buildQueries(vvs, qvs)

Expand Down
Loading