Skip to content

Commit

Permalink
Feature: Add With Insert Not Prepared as option to enforce query with…
Browse files Browse the repository at this point in the history
…out external arguments (#13)

* * add With Not Prepend as option to enforce query without external arguments

* * fix typo
  • Loading branch information
razt18 authored Feb 1, 2024
1 parent 4b5f08b commit ff59c3f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func WithInsertReturning(columns ...string) InsertOption {
}
}

func WithInsertNotPrepared() InsertOption {
return func(table exp.IdentifierExpression, s *goqu.InsertDataset) *goqu.InsertDataset {
return s.Prepared(false)
}
}

func BuildInsert(tableName string, values []any, options ...InsertOption) (string, []any, error) {
table := goqu.T(tableName)
q := goqu.Insert(table).WithDialect(defaultDialect)
Expand Down
10 changes: 10 additions & 0 deletions insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ func TestBuildInsert(t *testing.T) {
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "int_field", "other_value") VALUES ($1, $2, $3) RETURNING *`,
expectedArgs: []interface{}{"", int64(5), ""},
},
{
name: "insert_with_insert_not_prepared",
values: []any{
insertModel{IntField: 5},
insertModel{IntField: 6},
},
opts: []goqux.InsertOption{goqux.WithInsertNotPrepared()},
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "int_field", "other_value") VALUES ('', 5, ''), ('', 6, '')`,
expectedArgs: []interface{}{},
},
}
for _, tt := range testTables {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit ff59c3f

Please sign in to comment.