Skip to content

Commit

Permalink
Bump REL and SQL (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fs02 authored Mar 19, 2022
1 parent 913ca19 commit ec950dd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
8 changes: 7 additions & 1 deletion builder/insert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package builder

import (
"log"

"github.com/go-rel/rel"
"github.com/go-rel/sql/builder"
)
Expand All @@ -11,13 +13,17 @@ type Insert struct {
}

// Build sql query and its arguments.
func (i Insert) Build(table string, primaryField string, mutates map[string]rel.Mutate) (string, []interface{}) {
func (i Insert) Build(table string, primaryField string, mutates map[string]rel.Mutate, onConflict rel.OnConflict) (string, []interface{}) {
var (
buffer = i.BufferFactory.Create()
_, identityInsert = mutates[primaryField]
arguments = make([]interface{}, 0, len(mutates))
)

if onConflict.Keys != nil {
log.Println("[REL] OnConflict clause is not yet supported in MSSQL, feel free to open a PR!")
}

if identityInsert {
buffer.WriteString("SET IDENTITY_INSERT ")
buffer.WriteEscape(table)
Expand Down
8 changes: 7 additions & 1 deletion builder/insert_all.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package builder

import (
"log"

"github.com/go-rel/rel"
"github.com/go-rel/sql/builder"
)
Expand All @@ -11,13 +13,17 @@ type InsertAll struct {
}

// Build SQL string and its arguments.
func (ia InsertAll) Build(table string, primaryField string, fields []string, bulkMutates []map[string]rel.Mutate) (string, []interface{}) {
func (ia InsertAll) Build(table string, primaryField string, fields []string, bulkMutates []map[string]rel.Mutate, onConflict rel.OnConflict) (string, []interface{}) {
var (
buffer = ia.BufferFactory.Create()
mutatesCount = len(bulkMutates)
identityInsert = false
)

if onConflict.Keys != nil {
log.Println("[REL] OnConflict clause is not yet supported in MSSQL, feel free to open a PR!")
}

for i := range fields {
if primaryField == fields[i] {
identityInsert = true
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/denisenkom/go-mssqldb v0.12.0
github.com/go-rel/rel v0.32.0
github.com/go-rel/sql v0.8.0
github.com/go-rel/rel v0.33.1
github.com/go-rel/sql v0.9.1
github.com/stretchr/testify v1.7.1
)
18 changes: 4 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@ github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5O
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-rel/rel v0.28.0/go.mod h1:zaIYPmM3AfJrh0xBmm7KoVKRgTNvr0cgZfcJ88gVA2U=
github.com/go-rel/rel v0.30.0/go.mod h1:Wu6HB6x0oIiEOdUJClwmn7ByCmdL73U5gItAYObyyuo=
github.com/go-rel/rel v0.31.0/go.mod h1:fCMlDiwDSSBSoZQ6ovUyjSqS2FfpLnhwr6tW9TgnhC8=
github.com/go-rel/rel v0.32.0 h1:Y0JeQ/ZqQDB3x9QkSn8HiLtvt+5oFuYgS0tj+px59KU=
github.com/go-rel/rel v0.32.0/go.mod h1:Ulbt8kctHMw3W1JBEflFkleOk+tIqmcKHY4r1eRuNSY=
github.com/go-rel/reltest v0.4.0/go.mod h1:3udgrKCZGCMFWc8k+RH975gUdUFPE/yspG2iWN9gjqU=
github.com/go-rel/reltest v0.5.0/go.mod h1:onixPXWMVL+KILH7OdUufKDm1KN0H6FzXOP7OoNC9qM=
github.com/go-rel/reltest v0.6.0 h1:mzF49w6jo1eyvfbpogL6BvxCuJrMwsjD+49qI9gCyEs=
github.com/go-rel/reltest v0.6.0/go.mod h1:4jR+L2vn7qe6NDW1carPomp2SlbTsQ9dqkxC+n4db+k=
github.com/go-rel/sql v0.8.0 h1:RszZxejBZh9Y6dwFY81Cv1svj+9tVjaar82bwPA55Ok=
github.com/go-rel/sql v0.8.0/go.mod h1:cTFh1i1InRhZyNA43QGtQzUkzso5EFfx1BrERoXQF+0=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-rel/rel v0.33.1 h1:yi3u6FDsQnOKZjo/DxEAC1WOLEjoas3fE1+Vyh6m6uY=
github.com/go-rel/rel v0.33.1/go.mod h1:DhB9Xmt/ymaumJAB6Z3Kq+IybLwQhhrzr6ZLeXMygPo=
github.com/go-rel/sql v0.9.1 h1:2tHu4U4Bk1DWKvzsitwcvv1sk6aGcxPh3LYJXspgk8w=
github.com/go-rel/sql v0.9.1/go.mod h1:4Ghpv6kjZG+EwHXRApJfo/Tg5K3rAYmxJq8wVcblU8g=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 h1:+eHOFJl1BaXrQxKX+T06f78590z4qA2ZzBTqahsKSE4=
Expand All @@ -39,8 +31,6 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
Expand Down
8 changes: 4 additions & 4 deletions mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func (m MSSQL) Begin(ctx context.Context) (rel.Adapter, error) {
}

// Insert inserts a record to database and returns its id.
func (m MSSQL) Insert(ctx context.Context, query rel.Query, primaryField string, mutates map[string]rel.Mutate) (interface{}, error) {
func (m MSSQL) Insert(ctx context.Context, query rel.Query, primaryField string, mutates map[string]rel.Mutate, onConflict rel.OnConflict) (interface{}, error) {
var (
id int64
statement, args = m.InsertBuilder.Build(query.Table, primaryField, mutates)
statement, args = m.InsertBuilder.Build(query.Table, primaryField, mutates, onConflict)
rows, err = m.DoQuery(ctx, statement, args)
)

Expand All @@ -55,10 +55,10 @@ func (m MSSQL) Insert(ctx context.Context, query rel.Query, primaryField string,
}

// InsertAll inserts multiple records to database and returns its ids.
func (m MSSQL) InsertAll(ctx context.Context, query rel.Query, primaryField string, fields []string, bulkMutates []map[string]rel.Mutate) ([]interface{}, error) {
func (m MSSQL) InsertAll(ctx context.Context, query rel.Query, primaryField string, fields []string, bulkMutates []map[string]rel.Mutate, onConflict rel.OnConflict) ([]interface{}, error) {
var (
ids []interface{}
statement, args = m.InsertAllBuilder.Build(query.Table, primaryField, fields, bulkMutates)
statement, args = m.InsertAllBuilder.Build(query.Table, primaryField, fields, bulkMutates, onConflict)
rows, err = m.DoQuery(ctx, statement, args)
)

Expand Down

0 comments on commit ec950dd

Please sign in to comment.