Skip to content

Commit

Permalink
refactor: Engine 接口部分表操作语句可同时操作多个表
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Apr 22, 2024
1 parent 5a6fa3c commit 2fa6254
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 33 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ postgres | postgres github.com/lib/pq | [![Postgres](https://github.c
sqlite 为纯 Go 代码编写,如果涉及到交叉编译的可以采用此驱动,会很方便。

其它数据库,用户可以通过实现 Dialect 接口,来实现相应的支持。
如果用到了 check 约束,则需要 mysql > 8.0.19、mariadb > 10.2.1。
如果用到了 check 约束,则需要 mysql > 8.0.19、mariadb > 10.2.1,
mysql 的 MyISAM 是不支持外键约束的。

#### 初始化

Expand Down
67 changes: 58 additions & 9 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,72 @@ func (db *DB) Select(v TableNamer) (bool, error) { return db.SelectContext(conte

func (db *DB) SelectContext(ctx context.Context, v TableNamer) (bool, error) { return find(ctx, db, v) }

func (db *DB) Create(v TableNamer) error { return db.CreateContext(context.Background(), v) }
func (db *DB) Create(v ...TableNamer) error { return db.CreateContext(context.Background(), v...) }

func (db *DB) CreateContext(ctx context.Context, v TableNamer) error { return create(ctx, db, v) }
func (db *DB) CreateContext(ctx context.Context, v ...TableNamer) error {
if !db.Dialect().TransactionalDDL() {
for _, t := range v {
if err := create(ctx, db, t); err != nil {
return err
}
}
return nil
}

func (db *DB) Drop(v TableNamer) error { return db.DropContext(context.Background(), v) }
return db.DoTransaction(func(tx *Tx) error {
for _, t := range v {
if err := create(ctx, tx, t); err != nil {
return err
}
}
return nil
})
}

func (db *DB) DropContext(ctx context.Context, v TableNamer) error { return drop(ctx, db, v) }
func (db *DB) Drop(v ...TableNamer) error { return db.DropContext(context.Background(), v...) }

func (db *DB) Truncate(v TableNamer) error {
return db.TruncateContext(context.Background(), v)
func (db *DB) DropContext(ctx context.Context, v ...TableNamer) error {
if !db.Dialect().TransactionalDDL() {
for _, t := range v {
if err := drop(ctx, db, t); err != nil {
return err
}
}
return nil
}

return db.DoTransaction(func(tx *Tx) error {
for _, t := range v {
if err := drop(ctx, tx, t); err != nil {
return err
}
}
return nil
})
}

func (db *DB) Truncate(v ...TableNamer) error {
return db.TruncateContext(context.Background(), v...)
}

func (db *DB) TruncateContext(ctx context.Context, v TableNamer) error {
func (db *DB) TruncateContext(ctx context.Context, v ...TableNamer) error {
if !db.Dialect().TransactionalDDL() {
return truncate(ctx, db, v)
for _, t := range v {
if err := truncate(ctx, db, t); err != nil {
return err
}
}
return nil
}
return db.DoTransaction(func(tx *Tx) error { return truncate(ctx, tx, v) })

return db.DoTransaction(func(tx *Tx) error {
for _, t := range v {
if err := truncate(ctx, tx, t); err != nil {
return err
}
}
return nil
})
}

// InsertMany 一次插入多条数据
Expand Down
68 changes: 55 additions & 13 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,38 @@ func (tx *Tx) DeleteContext(ctx context.Context, v TableNamer) (sql.Result, erro
return del(ctx, tx, v)
}

func (tx *Tx) Create(v TableNamer) error { return tx.CreateContext(context.Background(), v) }
func (tx *Tx) Create(v ...TableNamer) error { return tx.CreateContext(context.Background(), v...) }

func (tx *Tx) CreateContext(ctx context.Context, v TableNamer) error { return create(ctx, tx, v) }
func (tx *Tx) CreateContext(ctx context.Context, v ...TableNamer) error {
for _, t := range v {
if err := create(ctx, tx, t); err != nil {
return err
}
}
return nil
}

func (tx *Tx) Drop(v TableNamer) error { return tx.DropContext(context.Background(), v) }
func (tx *Tx) Drop(v ...TableNamer) error { return tx.DropContext(context.Background(), v...) }

func (tx *Tx) DropContext(ctx context.Context, v TableNamer) error { return drop(ctx, tx, v) }
func (tx *Tx) DropContext(ctx context.Context, v ...TableNamer) error {
for _, t := range v {
if err := drop(ctx, tx, t); err != nil {
return err
}
}
return nil
}

func (tx *Tx) Truncate(v TableNamer) error { return tx.TruncateContext(context.Background(), v) }
func (tx *Tx) Truncate(v ...TableNamer) error { return tx.TruncateContext(context.Background(), v...) }

func (tx *Tx) TruncateContext(ctx context.Context, v TableNamer) error { return truncate(ctx, tx, v) }
func (tx *Tx) TruncateContext(ctx context.Context, v ...TableNamer) error {
for _, t := range v {
if err := truncate(ctx, tx, t); err != nil {
return err
}
}
return nil
}

func (tx *Tx) SQLBuilder() *sqlbuilder.SQLBuilder {
return sqlbuilder.New(tx) // 事务一般是一个临时对象,没必要像 [DB] 一样固定 sqlbuilder 对象。
Expand Down Expand Up @@ -170,18 +191,39 @@ func (e *txEngine) SelectContext(ctx context.Context, v TableNamer) (bool, error
return find(ctx, e, v)
}

func (e *txEngine) Create(v TableNamer) error { return e.CreateContext(context.Background(), v) }
func (e *txEngine) Create(v ...TableNamer) error { return e.CreateContext(context.Background(), v...) }

func (e *txEngine) CreateContext(ctx context.Context, v TableNamer) error { return create(ctx, e, v) }
func (e *txEngine) CreateContext(ctx context.Context, v ...TableNamer) error {
for _, t := range v {
if err := create(ctx, e, t); err != nil {
return err
}
}
return nil
}

func (e *txEngine) Drop(v TableNamer) error { return e.DropContext(context.Background(), v) }
func (e *txEngine) Drop(v ...TableNamer) error { return e.DropContext(context.Background(), v...) }

func (e *txEngine) DropContext(ctx context.Context, v TableNamer) error { return drop(ctx, e, v) }
func (e *txEngine) DropContext(ctx context.Context, v ...TableNamer) error {
for _, t := range v {
if err := drop(ctx, e, t); err != nil {
return err
}
}
return nil
}

func (e *txEngine) Truncate(v TableNamer) error { return e.TruncateContext(context.Background(), v) }
func (e *txEngine) Truncate(v ...TableNamer) error {
return e.TruncateContext(context.Background(), v...)
}

func (e *txEngine) TruncateContext(ctx context.Context, v TableNamer) error {
return truncate(ctx, e, v)
func (e *txEngine) TruncateContext(ctx context.Context, v ...TableNamer) error {
for _, t := range v {
if err := truncate(ctx, e, t); err != nil {
return err
}
}
return nil
}

func (e *txEngine) InsertMany(max int, v ...TableNamer) error {
Expand Down
19 changes: 9 additions & 10 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,24 @@ type (
//
// 查找条件的查找顺序是为 自增 > 主键 > 唯一约束,
// 如果同时存在多个唯一约束满足条件(可能每个唯一约束查询至的结果是不一样的),则返回错误信息。
SelectContext(ctx context.Context, v TableNamer) (found bool, err error)
Select(v TableNamer) (found bool, err error)
SelectContext(context.Context, TableNamer) (found bool, err error)
Select(TableNamer) (found bool, err error)

CreateContext(ctx context.Context, v TableNamer) error
Create(v TableNamer) error
CreateContext(context.Context, ...TableNamer) error
Create(...TableNamer) error

DropContext(ctx context.Context, v TableNamer) error
Drop(v TableNamer) error
DropContext(context.Context, ...TableNamer) error
Drop(...TableNamer) error

// TruncateContext 清空表并重置 ai 但保留表结构
TruncateContext(ctx context.Context, v TableNamer) error
Truncate(v TableNamer) error
TruncateContext(context.Context, ...TableNamer) error
Truncate(...TableNamer) error

// InsertManyContext 插入多条相同的数据
//
// 若需要向某张表中插入多条记录,此方法会比 [Engine.Insert] 性能上好很多。
//
// max 表示一次最多插入的数量,如果超过此值,会分批执行,
// 但是依然在一个事务中完成。
// max 表示一次最多插入的数量,如果超过此值,会分批执行,但是依然在一个事务中完成。
InsertManyContext(ctx context.Context, max int, v ...TableNamer) error
InsertMany(max int, v ...TableNamer) error

Expand Down

0 comments on commit 2fa6254

Please sign in to comment.