Skip to content

Commit

Permalink
insert returning
Browse files Browse the repository at this point in the history
  • Loading branch information
mstgnz committed Dec 22, 2024
1 parent b9a71b6 commit 1c7e272
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ 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)
}
if !reflect.DeepEqual(paramsExpected, params) {
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)
}
Expand Down

0 comments on commit 1c7e272

Please sign in to comment.