本文整理汇总了Golang中github.com/cloudwan/gohan/db/transaction.Transaction.Query方法的典型用法代码示例。如果您正苦于以下问题:Golang Transaction.Query方法的具体用法?Golang Transaction.Query怎么用?Golang Transaction.Query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cloudwan/gohan/db/transaction.Transaction
的用法示例。
在下文中一共展示了Transaction.Query方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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
}
示例3:
BeforeEach(func() {
manager := schema.GetManager()
var ok bool
s, ok = manager.Schema("test")
Expect(ok).To(BeTrue())
})
Context("Without place holders", func() {
It("Returns resources", func() {
query := fmt.Sprintf(
"SELECT %s FROM %s",
strings.Join(MakeColumns(s, s.GetDbTableName(), false), ", "),
s.GetDbTableName(),
)
results, err := tx.Query(s, query, []interface{}{})
Expect(err).ToNot(HaveOccurred())
Expect(results[0].Get("tenant_id")).To(Equal("tenant0"))
Expect(results[0].Get("test_string")).To(Equal("obj0"))
Expect(results[2].Get("tenant_id")).To(Equal("tenant1"))
Expect(results[2].Get("test_string")).To(Equal("obj2"))
Expect(len(results)).To(Equal(4))
})
})
Context("With a place holder", func() {
It("Replace the place holder and returns resources", func() {
query := fmt.Sprintf(
"SELECT %s FROM %s WHERE tenant_id = ?",
strings.Join(MakeColumns(s, s.GetDbTableName(), false), ", "),
s.GetDbTableName(),