本文整理汇总了Golang中github.com/cloudwan/gohan/db/transaction.Transaction类的典型用法代码示例。如果您正苦于以下问题:Golang Transaction类的具体用法?Golang Transaction怎么用?Golang Transaction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Transaction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: DBList
//DBList lists data from database.
func DBList(tx transaction.Transaction, schemaID string, filter map[string]interface{}) ([]interface{}, error) {
manager := schema.GetManager()
schemaObj, ok := manager.Schema(schemaID)
if !ok {
return nil, fmt.Errorf("Schema %s not found", schemaID)
}
for key, value := range filter {
switch v := value.(type) {
case string:
filter[key] = []string{v}
case bool:
filter[key] = []string{fmt.Sprintf("%v", v)}
case int:
filter[key] = []string{fmt.Sprintf("%v", v)}
case []interface{}:
filterList := make([]string, len(v))
for _, item := range v {
filterList = append(filterList, fmt.Sprintf("%v", item))
}
filter[key] = filterList
}
}
resources, _, err := tx.List(schemaObj, filter, nil)
resp := []interface{}{}
for _, resource := range resources {
resp = append(resp, resource.Data())
}
return resp, err
}
示例2: clearTable
func clearTable(tx transaction.Transaction, s *schema.Schema) error {
if s.IsAbstract() {
return nil
}
for _, schema := range schema.GetManager().Schemas() {
if schema.ParentSchema == s {
err := clearTable(tx, schema)
if err != nil {
return err
}
} else {
for _, property := range schema.Properties {
if property.Relation == s.Singular {
err := clearTable(tx, schema)
if err != nil {
return err
}
}
}
}
}
resources, _, err := tx.List(s, nil, nil)
if err != nil {
return err
}
for _, resource := range resources {
err = tx.Delete(s, resource.ID())
if err != nil {
return err
}
}
return nil
}
示例3: DBDelete
//DBDelete deletes a resource in a db.
func DBDelete(tx transaction.Transaction, schemaID string, id string) error {
manager := schema.GetManager()
schemaObj, ok := manager.Schema(schemaID)
if !ok {
return fmt.Errorf("Schema %s not found", schemaID)
}
return tx.Delete(schemaObj, id)
}
示例4: DBUpdate
//DBUpdate updates a resource in a db.
func DBUpdate(tx transaction.Transaction, schemaID string, data map[string]interface{}) error {
manager := schema.GetManager()
resource, err := manager.LoadResource(schemaID, data)
if err != nil {
return err
}
return tx.Update(resource)
}
示例5: DBQuery
//DBQuery fetchs data from db with additional query
func DBQuery(tx transaction.Transaction, schemaID string, sql string, arguments []interface{}) ([]interface{}, error) {
manager := schema.GetManager()
schemaObj, ok := manager.Schema(schemaID)
if !ok {
return nil, fmt.Errorf("Schema %s not found", schemaID)
}
resources, err := tx.Query(schemaObj, sql, arguments)
resp := []interface{}{}
for _, resource := range resources {
resp = append(resp, resource.Data())
}
return resp, err
}
示例6: DBGet
//DBGet get resource from a db.
func DBGet(tx transaction.Transaction, schemaID string, id string, tenantID string) (map[string]interface{}, error) {
manager := schema.GetManager()
schemaObj, ok := manager.Schema(schemaID)
if !ok {
return nil, fmt.Errorf("Schema %s not found", schemaID)
}
var tenantFilter []string
if tenantID != "" {
tenantFilter = []string{tenantID}
}
resp, err := tx.Fetch(schemaObj, id, tenantFilter)
if err != nil {
return nil, err
}
return resp.Data(), err
}
示例7: GohanDbDelete
//GohanDbDelete deletes resource from database
func GohanDbDelete(transaction transaction.Transaction, needCommit bool, schemaID, ID string) error {
schema, err := getSchema(schemaID)
if err != nil {
return fmt.Errorf("Error during gohan_db_delete: %s", err.Error())
}
if err := transaction.Delete(schema, ID); err != nil {
return fmt.Errorf("Error during gohan_db_delete: %s", err.Error())
}
if needCommit {
err := transaction.Commit()
if err != nil {
return fmt.Errorf("Error during gohan_db_delete: %s", err.Error())
}
}
return nil
}
示例8: GohanDbList
//GohanDbList lists resources in database filtered by filter and paginator
func GohanDbList(transaction transaction.Transaction, schemaID string,
filter map[string]interface{}, key string, limit uint64, offset uint64) ([]map[string]interface{}, error) {
schema, err := getSchema(schemaID)
if err != nil {
return []map[string]interface{}{}, err
}
var paginator *pagination.Paginator
if key != "" {
paginator, err = pagination.NewPaginator(schema, key, "", limit, offset)
if err != nil {
return []map[string]interface{}{}, fmt.Errorf("Error during gohan_db_list: %s", err.Error())
}
}
resources, _, err := transaction.List(schema, filter, paginator)
if err != nil {
return []map[string]interface{}{}, fmt.Errorf("Error during gohan_db_list: %s", err.Error())
}
resp := []map[string]interface{}{}
for _, resource := range resources {
resp = append(resp, resource.Data())
}
return resp, nil
}
示例9: GohanDbFetch
//GohanDbFetch gets resource from database
func GohanDbFetch(tx transaction.Transaction, schemaID, ID,
tenantID string) (*schema.Resource, error) {
schema, err := getSchema(schemaID)
if err != nil {
return nil, err
}
filter := transaction.IDFilter(ID)
if tenantID != "" {
filter["tenant_id"] = tenantID
}
resp, err := tx.Fetch(schema, filter)
if err != nil {
return nil, fmt.Errorf("Error during gohan_db_fetch: %s", err.Error())
}
return resp, nil
}
示例10: GohanDbStateUpdate
//GohanDbStateUpdate updates resource's state in database
func GohanDbStateUpdate(transaction transaction.Transaction, needCommit bool, schemaID string,
dataMap map[string]interface{}) (*schema.Resource, error) {
manager := schema.GetManager()
resource, err := manager.LoadResource(schemaID, dataMap)
if err != nil {
return nil, fmt.Errorf("Error during gohan_db_state_update: %s", err.Error())
}
if err = transaction.StateUpdate(resource, nil); err != nil {
return nil, fmt.Errorf("Error during gohan_db_state_update: %s", err.Error())
}
if needCommit {
err = transaction.Commit()
if err != nil {
return nil, fmt.Errorf("Error during gohan_db_state_update: %s", err.Error())
}
}
return resource, nil
}
示例11: clearTable
func clearTable(tx transaction.Transaction, s *schema.Schema) error {
for _, schema := range schema.GetManager().Schemas() {
if schema.ParentSchema == s {
err := clearTable(tx, schema)
if err != nil {
return err
}
}
}
resources, _, err := tx.List(s, nil, nil)
if err != nil {
return err
}
for _, resource := range resources {
err = tx.Delete(s, resource.ID())
if err != nil {
return err
}
}
return nil
}
示例12: GohanDbQuery
//GohanDbQuery get resources from database with query
func GohanDbQuery(transaction transaction.Transaction, needCommit bool, schemaID,
sqlString string, arguments []interface{}) ([]map[string]interface{}, error) {
schema, err := getSchema(schemaID)
if err != nil {
return []map[string]interface{}{}, err
}
resources, err := transaction.Query(schema, sqlString, arguments)
if err != nil {
return []map[string]interface{}{}, fmt.Errorf("Error during gohan_db_query: %s", err.Error())
}
if needCommit {
err = transaction.Commit()
if err != nil {
return []map[string]interface{}{}, fmt.Errorf("Error during gohan_db_query: %s", err.Error())
}
}
resp := []map[string]interface{}{}
for _, resource := range resources {
resp = append(resp, resource.Data())
}
return resp, nil
}
示例13: GohanDbStateFetch
//GohanDbStateFetch gets resource's state from database
func GohanDbStateFetch(tx transaction.Transaction, schemaID, ID,
tenantID string) (map[string]interface{}, error) {
schema, err := getSchema(schemaID)
if err != nil {
return map[string]interface{}{}, err
}
filter := transaction.IDFilter(ID)
if tenantID != "" {
filter["tenant_id"] = tenantID
}
resp, err := tx.StateFetch(schema, filter)
if err != nil {
return map[string]interface{}{}, fmt.Errorf("Error during gohan_db_state_fetch: %s", err.Error())
}
data := map[string]interface{}{
"config_version": resp.ConfigVersion,
"state_version": resp.StateVersion,
"error": resp.Error,
"state": resp.State,
"monitoring": resp.Monitoring,
}
return data, nil
}
示例14: GohanDbFetch
//GohanDbFetch gets resource from database
func GohanDbFetch(transaction transaction.Transaction, schemaID, ID,
tenantID string) (*schema.Resource, error) {
schema, err := getSchema(schemaID)
if err != nil {
return nil, err
}
var tenantFilter []string
if tenantID != "" {
tenantFilter = []string{tenantID}
}
resp, err := transaction.Fetch(schema, ID, tenantFilter)
if err != nil {
return nil, fmt.Errorf("Error during gohan_db_fetch: %s", err.Error())
}
return resp, nil
}
示例15:
"os"
"strings"
"github.com/cloudwan/gohan/db"
. "github.com/cloudwan/gohan/db/sql"
"github.com/cloudwan/gohan/db/transaction"
"github.com/cloudwan/gohan/schema"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Sql", func() {
var conn string
var tx transaction.Transaction
var sqlConn *DB
BeforeEach(func() {
var dbType string
if os.Getenv("MYSQL_TEST") == "true" {
conn = "gohan:[email protected]/gohan_test"
dbType = "mysql"
} else {
conn = "./test.db"
dbType = "sqlite3"
}
manager := schema.GetManager()
dbc, err := db.ConnectDB(dbType, conn, db.DefaultMaxOpenConn)
sqlConn = dbc.(*DB)