-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
qbhy
committed
Jan 31, 2023
1 parent
27c7cfa
commit d6a58c4
Showing
5 changed files
with
106 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package table | ||
|
||
import ( | ||
"github.com/goal-web/application" | ||
"github.com/goal-web/contracts" | ||
"github.com/goal-web/querybuilder" | ||
) | ||
|
||
var factory contracts.DBFactory | ||
|
||
func SetFactory(dbFactory contracts.DBFactory) { | ||
factory = dbFactory | ||
} | ||
|
||
func getFactory() contracts.DBFactory { | ||
if factory == nil { | ||
factory = application.Get("db.factory").(contracts.DBFactory) | ||
} | ||
return factory | ||
} | ||
|
||
func getTable(name string) *Table { | ||
builder := querybuilder.NewQuery(name) | ||
instance := &Table{ | ||
QueryBuilder: builder, | ||
primaryKey: "id", | ||
table: name, | ||
} | ||
builder.Bind(instance) | ||
return instance | ||
} | ||
|
||
// Query 将使用默认 connection | ||
func Query(name string) *Table { | ||
return getTable(name).SetConnection(factory.Connection()) | ||
} | ||
|
||
func FromModel(model contracts.Model) *Table { | ||
return WithConnection(model.GetTable(), model.GetConnection()).SetClass(model.GetClass()).SetPrimaryKey(model.GetPrimaryKey()) | ||
} | ||
|
||
// WithConnection 使用指定链接 | ||
func WithConnection(name string, connection interface{}) *Table { | ||
if connection == "" || connection == nil { | ||
return Query(name) | ||
} | ||
return getTable(name).SetConnection(connection) | ||
} | ||
|
||
// WithTX 使用TX | ||
func WithTX(name string, tx contracts.DBTx) contracts.QueryBuilder { | ||
return getTable(name).SetExecutor(tx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters