From 1c7e2725a4935e7212c8b115440f2b8e149b6cb5 Mon Sep 17 00:00:00 2001 From: Mesut GENEZ Date: Sun, 22 Dec 2024 21:58:58 +0300 Subject: [PATCH] insert returning --- README.md | 4 ++-- builder.go | 15 +++++++++------ builder_test.go | 6 +++--- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 9b097ee..a365c0e 100644 --- a/README.md +++ b/README.md @@ -116,13 +116,13 @@ Result: SELECT * FROM users WHERE lastname=$1 UNION SELECT * FROM users WHERE la Params: ["lorem", "ipsum"] ``` -## Insert +## Create ```go args := map[string]any{ "firstname": "Lorem", "lastname": "IPSUM", } -gb.Table("users").Insert(args).Prepare() +gb.Table("users").Create(args).Prepare() ``` ```sql Result: INSERT INTO users (lastname,firstname) VALUES ($1,$2) diff --git a/builder.go b/builder.go index 5640a57..6e9d989 100644 --- a/builder.go +++ b/builder.go @@ -68,8 +68,8 @@ func (gb *GoBuilder) SelectDistinct(columns ...string) *GoBuilder { return gb } -// Insert adds an INSERT INTO statement to the query with bind parameters -func (gb *GoBuilder) Insert(args map[string]any) *GoBuilder { +// Create adds an INSERT INTO statement to the query with bind parameters +func (gb *GoBuilder) Create(args map[string]any, returning ...string) *GoBuilder { if len(args) != 0 { keys := make([]string, 0, len(args)) for key := range args { @@ -90,6 +90,9 @@ func (gb *GoBuilder) Insert(args map[string]any) *GoBuilder { strings.Join(columns, ", "), strings.Join(values, ", "), ) + if len(returning) > 0 { + gb.selectClause += fmt.Sprintf(" RETURNING %s", strings.Join(returning, ", ")) + } } return gb } @@ -196,15 +199,15 @@ func (gb *GoBuilder) Having(condition string, args ...any) *GoBuilder { } // Join adds a JOIN clause -func (gb *GoBuilder) Join(table, first, opt, last string) *GoBuilder { - join := fmt.Sprintf("INNER JOIN %s ON %s %s %s", table, first, opt, last) +func (gb *GoBuilder) Join(table, first, operator, last string) *GoBuilder { + join := fmt.Sprintf("INNER JOIN %s ON %s %s %s", table, first, operator, last) gb.joinClauses = append(gb.joinClauses, join) return gb } // LeftJoin adds a LEFT JOIN clause -func (gb *GoBuilder) LeftJoin(table, first, opt, last string) *GoBuilder { - join := fmt.Sprintf("LEFT JOIN %s ON %s %s %s", table, first, opt, last) +func (gb *GoBuilder) LeftJoin(table, first, operator, last string) *GoBuilder { + join := fmt.Sprintf("LEFT JOIN %s ON %s %s %s", table, first, operator, last) gb.joinClauses = append(gb.joinClauses, join) return gb } diff --git a/builder_test.go b/builder_test.go index f360042..d301d90 100644 --- a/builder_test.go +++ b/builder_test.go @@ -64,11 +64,11 @@ func TestSql_Distinct(t *testing.T) { } } -func TestSql_Insert(t *testing.T) { +func TestSql_Create(t *testing.T) { queryExpected = "INSERT INTO users (firstname, lastname) VALUES ($1, $2)" paramsExpected := []any{"Mesut", "GENEZ"} args := map[string]any{"firstname": "Mesut", "lastname": "GENEZ"} - query, params = gb.Table("users").Insert(args).Prepare() + query, params = gb.Table("users").Create(args).Prepare() if !reflect.DeepEqual(queryExpected, query) { t.Errorf("queryExpected = %v, query %v", queryExpected, query) } @@ -76,7 +76,7 @@ func TestSql_Insert(t *testing.T) { t.Errorf("paramsExpected = %v, params %v", paramsExpected, params) } queryExpected = "INSERT INTO users (firstname, lastname) VALUES ('Mesut', 'GENEZ')" - query = gb.Table("users").Insert(args).Sql() + query = gb.Table("users").Create(args).Sql() if !reflect.DeepEqual(queryExpected, query) { t.Errorf("queryExpected = %v, query %v", queryExpected, query) }