浏览代码

修改session.Find()API

xormplus 10 年之前
父节点
当前提交
7bd7481c0f
共有 5 个文件被更改,包括 116 次插入19 次删除
  1. 1 1
      engine.go
  2. 2 2
      session.go
  3. 67 0
      sessionplus.go
  4. 32 2
      test/xorm_test.go
  5. 14 14
      test/测试结果.txt

+ 1 - 1
engine.go

@@ -1340,7 +1340,7 @@ func (engine *Engine) Get(bean interface{}) (bool, error) {
 func (engine *Engine) Find(beans interface{}, condiBeans ...interface{}) error {
 	session := engine.NewSession()
 	defer session.Close()
-	return session.Find(beans, condiBeans...)
+	return session.find(beans, condiBeans...)
 }
 
 // Iterate record by record handle records from table, bean's non-empty fields

+ 2 - 2
session.go

@@ -864,7 +864,7 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in
 			}
 		}
 
-		err = newSession.NoCache().Find(beans)
+		err = newSession.NoCache().find(beans)
 		if err != nil {
 			return err
 		}
@@ -1255,7 +1255,7 @@ func Atot(s string, tp reflect.Type) (interface{}, error) {
 // Find retrieve records from table, condiBeans's non-empty fields
 // are conditions. beans could be []Struct, []*Struct, map[int64]Struct
 // map[int64]*Struct
-func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{}) error {
+func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{}) error {
 	defer session.resetStatement()
 	if session.IsAutoClose {
 		defer session.Close()

+ 67 - 0
sessionplus.go

@@ -133,6 +133,73 @@ func (resultMap ResultMap) XmlIndent(prefix string, indent string, recordTag str
 	return string(results), nil
 }
 
+type ResultStructs struct {
+	Result interface{}
+	Error    error
+}
+
+func (resultStructs ResultStructs) Json() (string, error) {
+
+	if resultStructs.Error != nil {
+		return "", resultStructs.Error
+	}
+	return JSONString(resultStructs.Result, true)
+}
+
+func (resultStructs ResultStructs) Xml() (string, error) {
+	if resultStructs.Error != nil {
+		return "", resultStructs.Error
+	}
+
+	result,err:=resultStructs.Json()
+	if err != nil {
+		return "", err
+	}
+
+	var anydata = []byte(result)
+	var i interface{}
+	err = json.Unmarshal(anydata, &i)
+	if err != nil {
+		return "", err
+	}
+	resultByte, err := anyxml.Xml(i)
+	if err != nil {
+		return "", err
+	}
+
+	return string(resultByte), nil
+}
+
+func (resultStructs ResultStructs) XmlIndent(prefix string, indent string, recordTag string) (string, error) {
+	if resultStructs.Error != nil {
+		return "", resultStructs.Error
+	}
+
+	result,err:=resultStructs.Json()
+	if err != nil {
+		return "", err
+	}
+
+	var anydata = []byte(result)
+	var i interface{}
+	err = json.Unmarshal(anydata, &i)
+	if err != nil {
+		return "", err
+	}
+	resultByte, err := anyxml.XmlIndent(i, prefix, indent, recordTag)
+	if err != nil {
+		return "", err
+	}
+
+	return string(resultByte), nil
+}
+
+func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{}) ResultStructs{
+	err := session.find(rowsSlicePtr, condiBean...)
+	r := ResultStructs{Result: rowsSlicePtr, Error: err}
+	return r
+}
+
 // Exec a raw sql and return records as []map[string]interface{}
 func (session *Session) Query() ResultMap {
 	sql := session.Statement.RawSQL

+ 32 - 2
test/xorm_test.go

@@ -2,9 +2,9 @@ package xorm
 
 import (
 	"fmt"
+	"reflect"
 	"testing"
 	"time"
-"reflect"
 
 	"github.com/xormplus/xorm"
 
@@ -103,7 +103,7 @@ func Test_FindAll_ID(t *testing.T) {
 	if rows.Error != nil {
 		t.Fatal(rows.Error)
 	}
-	
+
 	t.Log("[Test_FindAll_Json]->rows[0][\"id\"]:\n", rows.Result[0]["id"])
 	t.Log("[Test_FindAll_Json]->reflect.TypeOf(rows.Result[0][\"id\"]):\n", reflect.TypeOf(rows.Result[0]["id"]))
 	t.Log("[Test_FindAll_Json]->rows[0][\"title\"]:\n", rows.Result[0]["title"])
@@ -295,3 +295,33 @@ func Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_XmlIndent(t *testing
 	}
 	t.Log("[Test_SqlTemplateClient_FindAllByParamMapWithDateFormat_XmlIndent]->rows:\n" + rows)
 }
+
+func Test_Find_Structs_Json(t *testing.T) {
+	articles := make([]Article, 0)
+	json,err := db.Where("id=?", 6).Find(&articles).Json()
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	t.Log("[Test_Find_Structs_Json]->rows:\n" + json)
+}
+
+func Test_Find_Structs_Xml(t *testing.T) {
+	articles := make([]Article, 0)
+	xml,err := db.Where("id=?", 6).Find(&articles).Xml()
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	t.Log("[Test_Find_Structs_Xml]->rows:\n" + xml)
+}
+
+func Test_Find_Structs_XmlIndent(t *testing.T) {
+	articles := make([]Article, 0)
+	xml,err := db.Where("id=?", 6).Find(&articles).XmlIndent("","  ","Article")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	t.Log("[Test_Find_Structs_XmlIndent]->rows:\n" + xml)
+}

文件差异内容过多而无法显示
+ 14 - 14
test/测试结果.txt


部分文件因为文件数量过多而无法显示