Skip to content

Commit

Permalink
Merge pull request #97 from doug-martin/v7.3.0-rc
Browse files Browse the repository at this point in the history
V7.3.0
  • Loading branch information
doug-martin authored Jul 12, 2019
2 parents 3d12ff0 + 033c2d1 commit 40b44d0
Show file tree
Hide file tree
Showing 13 changed files with 472 additions and 245 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v7.3.0

* [ADDED] UPDATE and INSERT should use struct Field name if db tag is not specified [#57](https://github.com/doug-martin/goqu/issues/57)
* [CHANGE] Changed goqu.Database to accept a SQLDatabase interface to allow using goqu.Database with other libraries such as `sqlx` [#95](https://github.com/doug-martin/goqu/issues/95)

## v7.2.0

* [FIXED] Sqlite3 does not accept SELECT * UNION (SELECT *) [#79](https://github.com/doug-martin/goqu/issues/79)
Expand Down
13 changes: 11 additions & 2 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ type (
Logger interface {
Printf(format string, v ...interface{})
}
// Interface for sql.DB, an interface is used so you can use with other
// libraries such as sqlx instead of the native sql.DB
SQLDatabase interface {
Begin() (*sql.Tx, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}
// This struct is the wrapper for a Db. The struct delegates most calls to either an Exec instance or to the Db
// passed into the constructor.
Database struct {
logger Logger
dialect string
Db *sql.DB
Db SQLDatabase
qf exec.QueryFactory
}
)
Expand Down Expand Up @@ -49,7 +58,7 @@ type (
// panic(err.Error())
// }
// fmt.Printf("%+v", ids)
func newDatabase(dialect string, db *sql.DB) *Database {
func newDatabase(dialect string, db SQLDatabase) *Database {
return &Database{dialect: dialect, Db: db}
}

Expand Down
158 changes: 158 additions & 0 deletions dataset_sql_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,34 @@ func ExampleDataset_ToUpdateSQL() {
// UPDATE "items" SET "address"='111 Test Addr',"name"='Test' []
}

func ExampleDataset_ToUpdateSQL_withSkipUpdateTag() {
type item struct {
Address string `db:"address"`
Name string `db:"name" goqu:"skipupdate"`
}
sql, args, _ := goqu.From("items").ToUpdateSQL(
item{Name: "Test", Address: "111 Test Addr"},
)
fmt.Println(sql, args)

// Output:
// UPDATE "items" SET "address"='111 Test Addr' []
}

func ExampleDataset_ToUpdateSQL_withNoTags() {
type item struct {
Address string
Name string
}
sql, args, _ := goqu.From("items").ToUpdateSQL(
item{Name: "Test", Address: "111 Test Addr"},
)
fmt.Println(sql, args)

// Output:
// UPDATE "items" SET "address"='111 Test Addr',"name"='Test' []
}

func ExampleDataset_ToUpdateSQL_prepared() {
type item struct {
Address string `db:"address"`
Expand Down Expand Up @@ -1042,6 +1070,36 @@ func ExampleDataset_ToInsertSQL() {
// INSERT INTO "items" ("address", "name") VALUES ('111 Test Addr', 'Test1'), ('112 Test Addr', 'Test2') []
}

func ExampleDataset_ToInsertSQL_withNoDbTag() {
type item struct {
ID uint32 `goqu:"skipinsert"`
Address string
Name string
}
sql, args, _ := goqu.From("items").ToInsertSQL(
item{Name: "Test1", Address: "111 Test Addr"},
item{Name: "Test2", Address: "112 Test Addr"},
)
fmt.Println(sql, args)
// Output:
// INSERT INTO "items" ("address", "name") VALUES ('111 Test Addr', 'Test1'), ('112 Test Addr', 'Test2') []
}

func ExampleDataset_ToInsertSQL_withGoquSkipInsertTag() {
type item struct {
ID uint32 `goqu:"skipinsert"`
Address string `goqu:"skipinsert"`
Name string
}
sql, args, _ := goqu.From("items").ToInsertSQL(
item{Name: "Test1", Address: "111 Test Addr"},
item{Name: "Test2", Address: "112 Test Addr"},
)
fmt.Println(sql, args)
// Output:
// INSERT INTO "items" ("name") VALUES ('Test1'), ('Test2') []
}

func ExampleDataset_ToInsertSQL_prepared() {
type item struct {
ID uint32 `db:"id" goqu:"skipinsert"`
Expand Down Expand Up @@ -1119,6 +1177,36 @@ func ExampleDataset_ToInsertIgnoreSQL() {
// INSERT INTO "items" ("address", "name") VALUES ('111 Test Addr', 'Test1'), ('112 Test Addr', 'Test2') ON CONFLICT DO NOTHING []
}

func ExampleDataset_ToInsertIgnoreSQL_withNoDBTag() {
type item struct {
ID uint32 `goqu:"skipinsert"`
Address string
Name string
}
sql, args, _ := goqu.From("items").ToInsertIgnoreSQL(
item{Name: "Test1", Address: "111 Test Addr"},
item{Name: "Test2", Address: "112 Test Addr"},
)
fmt.Println(sql, args)
// Output:
// INSERT INTO "items" ("address", "name") VALUES ('111 Test Addr', 'Test1'), ('112 Test Addr', 'Test2') ON CONFLICT DO NOTHING []
}

func ExampleDataset_ToInsertIgnoreSQL_withGoquSkipInsertTag() {
type item struct {
ID uint32 `goqu:"skipinsert"`
Address string
Name string `goqu:"skipinsert"`
}
sql, args, _ := goqu.From("items").ToInsertIgnoreSQL(
item{Name: "Test1", Address: "111 Test Addr"},
item{Name: "Test2", Address: "112 Test Addr"},
)
fmt.Println(sql, args)
// Output:
// INSERT INTO "items" ("address") VALUES ('111 Test Addr'), ('112 Test Addr') ON CONFLICT DO NOTHING []
}

func ExampleDataset_ToInsertConflictSQL() {
type item struct {
ID uint32 `db:"id" goqu:"skipinsert"`
Expand Down Expand Up @@ -1154,6 +1242,76 @@ func ExampleDataset_ToInsertConflictSQL() {
// INSERT INTO "items" ("address", "name") VALUES ('111 Test Addr', 'Test1'), ('112 Test Addr', 'Test2') ON CONFLICT (key) DO UPDATE SET "updated"=NOW() WHERE ("allow_update" IS TRUE) []
}

func ExampleDataset_ToInsertConflictSQL_withNoDbTag() {
type item struct {
ID uint32 `goqu:"skipinsert"`
Address string
Name string
}
sql, args, _ := goqu.From("items").ToInsertConflictSQL(
goqu.DoNothing(),
item{Name: "Test1", Address: "111 Test Addr"},
item{Name: "Test2", Address: "112 Test Addr"},
)
fmt.Println(sql, args)

sql, args, _ = goqu.From("items").ToInsertConflictSQL(
goqu.DoUpdate("key", goqu.Record{"updated": goqu.L("NOW()")}),
item{Name: "Test1", Address: "111 Test Addr"},
item{Name: "Test2", Address: "112 Test Addr"},
)
fmt.Println(sql, args)

sql, args, _ = goqu.From("items").ToInsertConflictSQL(
goqu.DoUpdate("key", goqu.Record{"updated": goqu.L("NOW()")}).Where(goqu.C("allow_update").IsTrue()),
[]item{
{Name: "Test1", Address: "111 Test Addr"},
{Name: "Test2", Address: "112 Test Addr"},
})
fmt.Println(sql, args)

// nolint:lll
// Output:
// INSERT INTO "items" ("address", "name") VALUES ('111 Test Addr', 'Test1'), ('112 Test Addr', 'Test2') ON CONFLICT DO NOTHING []
// INSERT INTO "items" ("address", "name") VALUES ('111 Test Addr', 'Test1'), ('112 Test Addr', 'Test2') ON CONFLICT (key) DO UPDATE SET "updated"=NOW() []
// INSERT INTO "items" ("address", "name") VALUES ('111 Test Addr', 'Test1'), ('112 Test Addr', 'Test2') ON CONFLICT (key) DO UPDATE SET "updated"=NOW() WHERE ("allow_update" IS TRUE) []
}

func ExampleDataset_ToInsertConflictSQL_withGoquSkipInsertTag() {
type item struct {
ID uint32 `goqu:"skipinsert"`
Address string
Name string `goqu:"skipinsert"`
}
sql, args, _ := goqu.From("items").ToInsertConflictSQL(
goqu.DoNothing(),
item{Name: "Test1", Address: "111 Test Addr"},
item{Name: "Test2", Address: "112 Test Addr"},
)
fmt.Println(sql, args)

sql, args, _ = goqu.From("items").ToInsertConflictSQL(
goqu.DoUpdate("key", goqu.Record{"updated": goqu.L("NOW()")}),
item{Name: "Test1", Address: "111 Test Addr"},
item{Name: "Test2", Address: "112 Test Addr"},
)
fmt.Println(sql, args)

sql, args, _ = goqu.From("items").ToInsertConflictSQL(
goqu.DoUpdate("key", goqu.Record{"updated": goqu.L("NOW()")}).Where(goqu.C("allow_update").IsTrue()),
[]item{
{Name: "Test1", Address: "111 Test Addr"},
{Name: "Test2", Address: "112 Test Addr"},
})
fmt.Println(sql, args)

// nolint:lll
// Output:
// INSERT INTO "items" ("address") VALUES ('111 Test Addr'), ('112 Test Addr') ON CONFLICT DO NOTHING []
// INSERT INTO "items" ("address") VALUES ('111 Test Addr'), ('112 Test Addr') ON CONFLICT (key) DO UPDATE SET "updated"=NOW() []
// INSERT INTO "items" ("address") VALUES ('111 Test Addr'), ('112 Test Addr') ON CONFLICT (key) DO UPDATE SET "updated"=NOW() WHERE ("allow_update" IS TRUE) []
}

func ExampleDataset_ToDeleteSQL() {
sql, args, _ := goqu.From("items").ToDeleteSQL()
fmt.Println(sql, args)
Expand Down
Loading

0 comments on commit 40b44d0

Please sign in to comment.