//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 }
//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 }
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(),