Skip to content

Commit

Permalink
Add table index example
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed May 17, 2021
1 parent 521cfed commit 01a7183
Show file tree
Hide file tree
Showing 17 changed files with 135 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ if rows.Next() {
panic(err)
}
}

if err := rows.Err(); err != nil {
panic(err)
}
```

For more details, please check [basic](example/basic) example.
4 changes: 1 addition & 3 deletions example/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ func main() {
sqlite.SetMaxOpenConns(1)

db := bun.NewDB(sqlite, sqlitedialect.New())
if false {
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose()))
}
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose()))

// Drop and create tables.
models := []interface{}{
Expand Down
9 changes: 9 additions & 0 deletions example/create-table-index/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Creating indexes example

To run this example:

```shell
go run main.go
```

See [docs](https://bun.uptrace.dev/) for details.
16 changes: 16 additions & 0 deletions example/create-table-index/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/uptrace/bun/example/create-table-index

go 1.16

replace github.com/uptrace/bun => ../..

replace github.com/uptrace/bun/extra/bundebug => ../../extra/bundebug

replace github.com/uptrace/bun/dialect/sqlitedialect => ../../dialect/sqlitedialect

require (
github.com/mattn/go-sqlite3 v1.14.7
github.com/uptrace/bun v0.0.0-20210507075305-2e91d2c5c8de
github.com/uptrace/bun/dialect/sqlitedialect v0.0.0-00010101000000-000000000000
github.com/uptrace/bun/extra/bundebug v0.0.0-00010101000000-000000000000
)
32 changes: 32 additions & 0 deletions example/create-table-index/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
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/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA=
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
github.com/vmihailenco/msgpack/v5 v5.3.1 h1:0i85a4dsZh8mC//wmyyTEzidDLPQfQAxZIOLtafGbFY=
github.com/vmihailenco/msgpack/v5 v5.3.1/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210507014357-30e306a8bba5 h1:cez+MEm4+A0CG7ik1Qzj3bmK9DFoouuLom9lwM+Ijow=
golang.org/x/sys v0.0.0-20210507014357-30e306a8bba5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
45 changes: 45 additions & 0 deletions example/create-table-index/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"context"
"database/sql"

_ "github.com/mattn/go-sqlite3"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/sqlitedialect"
"github.com/uptrace/bun/extra/bundebug"
)

type Book struct {
ID int64
Name string
CategoryID int64
}

var _ bun.AfterCreateTableQueryHook = (*Book)(nil)

func (*Book) AfterCreateTableQuery(ctx context.Context, query *bun.CreateTableQuery) error {
_, err := query.GetDB().NewCreateIndex().
Model((*Book)(nil)).
Index("category_id_idx").
Column("category_id").
Exec(ctx)
return err
}

func main() {
ctx := context.Background()

sqlite, err := sql.Open("sqlite3", ":memory:?cache=shared")
if err != nil {
panic(err)
}
sqlite.SetMaxOpenConns(1)

db := bun.NewDB(sqlite, sqlitedialect.New())
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose()))

if _, err := db.NewCreateTable().Model((*Book)(nil)).Exec(ctx); err != nil {
panic(err)
}
}
2 changes: 1 addition & 1 deletion internal/dbtest/.snapshots/TestQuery-mysql-41
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CREATE UNIQUE INDEX title_idx ON `films` (`title`)
CREATE UNIQUE INDEX `title_idx` ON `films` (`title`)
2 changes: 1 addition & 1 deletion internal/dbtest/.snapshots/TestQuery-mysql-42
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CREATE UNIQUE INDEX title_idx ON `films` (`title`) INCLUDE (`director`, `rating`)
CREATE UNIQUE INDEX `title_idx` ON `films` (`title`) INCLUDE (`director`, `rating`)
2 changes: 1 addition & 1 deletion internal/dbtest/.snapshots/TestQuery-pg-41
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CREATE UNIQUE INDEX title_idx ON "films" ("title")
CREATE UNIQUE INDEX "title_idx" ON "films" ("title")
2 changes: 1 addition & 1 deletion internal/dbtest/.snapshots/TestQuery-pg-42
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CREATE UNIQUE INDEX title_idx ON "films" ("title") INCLUDE ("director", "rating")
CREATE UNIQUE INDEX "title_idx" ON "films" ("title") INCLUDE ("director", "rating")
2 changes: 1 addition & 1 deletion internal/dbtest/.snapshots/TestQuery-sqlite-41
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CREATE UNIQUE INDEX title_idx ON "films" ("title")
CREATE UNIQUE INDEX "title_idx" ON "films" ("title")
2 changes: 1 addition & 1 deletion internal/dbtest/.snapshots/TestQuery-sqlite-42
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CREATE UNIQUE INDEX title_idx ON "films" ("title") INCLUDE ("director", "rating")
CREATE UNIQUE INDEX "title_idx" ON "films" ("title") INCLUDE ("director", "rating")
2 changes: 1 addition & 1 deletion model_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newMapModel(db *DB, ptr *map[string]interface{}) *mapModel {

func (m *mapModel) ScanRows(ctx context.Context, rows *sql.Rows) (int, error) {
if !rows.Next() {
return 0, sql.ErrNoRows
return 0, errNoRows(rows)
}

columns, err := rows.Columns()
Expand Down
2 changes: 1 addition & 1 deletion model_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newScanModel(dest []interface{}) scanModel {

func (m scanModel) ScanRows(ctx context.Context, rows *sql.Rows) (int, error) {
if !rows.Next() {
return 0, sql.ErrNoRows
return 0, errNoRows(rows)
}

if err := rows.Scan(m.dest...); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion model_table_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func (m *structTableModel) updateSoftDeleteField() error {

func (m *structTableModel) ScanRows(ctx context.Context, rows *sql.Rows) (int, error) {
if !rows.Next() {
return 0, sql.ErrNoRows
return 0, errNoRows(rows)
}

if err := m.ScanRow(ctx, rows); err != nil {
Expand Down
13 changes: 13 additions & 0 deletions query_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type baseQuery struct {
flags internal.Flag
}

func (q *baseQuery) GetDB() *DB {
return q.db
}

func (q *baseQuery) setTableModel(modeli interface{}) {
model, err := newSingleModel(q.db, modeli)
if err != nil {
Expand Down Expand Up @@ -837,3 +841,12 @@ func putByteSlice(b *byteSlice) {
b.b = b.b[:0]
byteSlicePool.Put(b)
}

//------------------------------------------------------------------------------

func errNoRows(rows *sql.Rows) error {
if err := rows.Err(); err != nil {
return err
}
return sql.ErrNoRows
}
7 changes: 6 additions & 1 deletion query_index_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ func (q *CreateIndexQuery) IfNotExists() *CreateIndexQuery {

//------------------------------------------------------------------------------

func (q *CreateIndexQuery) Index(query string, args ...interface{}) *CreateIndexQuery {
func (q *CreateIndexQuery) Index(query string) *CreateIndexQuery {
q.index = schema.UnsafeIdent(query)
return q
}

func (q *CreateIndexQuery) IndexExpr(query string, args ...interface{}) *CreateIndexQuery {
q.index = schema.SafeQuery(query, args)
return q
}
Expand Down

0 comments on commit 01a7183

Please sign in to comment.