本文整理匯總了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(),