Skip to content
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

Bump REL and SQL #54

Merged
merged 1 commit into from
Mar 19, 2022
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
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