forked from opengaussexamples/examples
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"gitee.com/chentanyang/ogx"
|
|
"gitee.com/chentanyang/ogx/dialect/ogdialect"
|
|
"gitee.com/chentanyang/ogx/extra/ogxdebug"
|
|
|
|
_ "gitee.com/opengauss/openGauss-connector-go-pq"
|
|
)
|
|
|
|
type Book struct {
|
|
ID int64 `ogx:",pk,autoincrement"`
|
|
Name string
|
|
CategoryID int64
|
|
}
|
|
|
|
var _ ogx.AfterCreateTableHook = (*Book)(nil)
|
|
|
|
func (*Book) AfterCreateTable(ctx context.Context, query *ogx.CreateTableQuery) error {
|
|
_, err := query.DB().NewCreateIndex().
|
|
Model((*Book)(nil)).
|
|
Index("category_id_idx").
|
|
Column("category_id").
|
|
Exec(ctx)
|
|
return err
|
|
}
|
|
|
|
func main() {
|
|
ctx := context.Background()
|
|
|
|
connStr := "host=192.168.20.40 port=26000 user=cuih password=Gauss@123 dbname=test sslmode=disable"
|
|
opengaussdb, err := sql.Open("opengauss", connStr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
opengaussdb.SetMaxOpenConns(1)
|
|
|
|
db := ogx.NewDB(opengaussdb, ogdialect.New())
|
|
db.AddQueryHook(ogxdebug.NewQueryHook(ogxdebug.WithVerbose(true)))
|
|
|
|
if _, err := db.NewCreateTable().Model((*Book)(nil)).Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
defer func() {
|
|
_ = dropSchema(ctx, db, (*Book)(nil))
|
|
}()
|
|
|
|
}
|
|
|
|
func dropSchema(ctx context.Context, db *ogx.DB, models ...interface{}) error {
|
|
for _, model := range models {
|
|
if _, err := db.NewDropTable().Model(model).IfExists().Cascade().Exec(ctx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|