resolved merge from upstream/master
This commit is contained in:
commit
46b46db6d0
|
@ -50,6 +50,12 @@ Drivers for Go's sql package which currently support database/sql includes:
|
||||||
|
|
||||||
# Installation
|
# Installation
|
||||||
|
|
||||||
|
If you have [gopm](https://github.com/gpmgo/gopm) installed,
|
||||||
|
|
||||||
|
gopm get github.com/lunny/xorm
|
||||||
|
|
||||||
|
Or
|
||||||
|
|
||||||
go get github.com/lunny/xorm
|
go get github.com/lunny/xorm
|
||||||
|
|
||||||
# Documents
|
# Documents
|
||||||
|
|
|
@ -50,6 +50,12 @@ xorm是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作
|
||||||
|
|
||||||
## 安装
|
## 安装
|
||||||
|
|
||||||
|
推荐使用 [gopm](https://github.com/gpmgo/gopm) 进行安装:
|
||||||
|
|
||||||
|
gopm get github.com/lunny/xorm
|
||||||
|
|
||||||
|
或者您也可以使用go工具进行安装:
|
||||||
|
|
||||||
go get github.com/lunny/xorm
|
go get github.com/lunny/xorm
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
433
base_test.go
433
base_test.go
|
@ -677,7 +677,7 @@ func combineTransaction(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
//session.IsAutoRollback = false
|
|
||||||
user1 := Userinfo{Username: "xiaoxiao2", Departname: "dev", Alias: "lunny", Created: time.Now()}
|
user1 := Userinfo{Username: "xiaoxiao2", Departname: "dev", Alias: "lunny", Created: time.Now()}
|
||||||
_, err = session.Insert(&user1)
|
_, err = session.Insert(&user1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1706,6 +1706,48 @@ func testPrefixTableName(engine *Engine, t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CreatedUpdated struct {
|
||||||
|
Id int64
|
||||||
|
Name string
|
||||||
|
Value float64 `xorm:"numeric"`
|
||||||
|
Created time.Time `xorm:"created"`
|
||||||
|
Created2 time.Time `xorm:"created"`
|
||||||
|
Updated time.Time `xorm:"updated"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func testCreatedUpdated(engine *Engine, t *testing.T) {
|
||||||
|
err := engine.Sync(&CreatedUpdated{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c := &CreatedUpdated{Name: "test"}
|
||||||
|
_, err = engine.Insert(c)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c2 := new(CreatedUpdated)
|
||||||
|
has, err := engine.Id(c.Id).Get(c2)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !has {
|
||||||
|
panic(errors.New("no id"))
|
||||||
|
}
|
||||||
|
|
||||||
|
c2.Value -= 1
|
||||||
|
_, err = engine.Id(c2.Id).Update(c2)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type ProcessorsStruct struct {
|
type ProcessorsStruct struct {
|
||||||
Id int64
|
Id int64
|
||||||
|
|
||||||
|
@ -1761,6 +1803,7 @@ func testProcessors(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
p := &ProcessorsStruct{}
|
||||||
|
|
||||||
err = tempEngine.CreateTables(&ProcessorsStruct{})
|
err = tempEngine.CreateTables(&ProcessorsStruct{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1768,9 +1811,6 @@ func testProcessors(engine *Engine, t *testing.T) {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
p := &ProcessorsStruct{}
|
|
||||||
|
|
||||||
// test insert processors
|
|
||||||
b4InsertFunc := func(bean interface{}) {
|
b4InsertFunc := func(bean interface{}) {
|
||||||
if v, ok := (bean).(*ProcessorsStruct); ok {
|
if v, ok := (bean).(*ProcessorsStruct); ok {
|
||||||
v.B4InsertViaExt = 1
|
v.B4InsertViaExt = 1
|
||||||
|
@ -1792,10 +1832,18 @@ func testProcessors(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
if p.B4InsertFlag == 0 {
|
||||||
if p.AfterInsertedFlag == 0 { t.Error(errors.New("AfterInsertedFlag not set")) }
|
t.Error(errors.New("B4InsertFlag not set"))
|
||||||
if p.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
}
|
||||||
if p.AfterInsertedViaExt == 0 { t.Error(errors.New("AfterInsertedViaExt not set")) }
|
if p.AfterInsertedFlag == 0 {
|
||||||
|
t.Error(errors.New("B4InsertFlag not set"))
|
||||||
|
}
|
||||||
|
if p.B4InsertViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4InsertFlag not set"))
|
||||||
|
}
|
||||||
|
if p.AfterInsertedViaExt == 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedViaExt not set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
p2 := &ProcessorsStruct{}
|
p2 := &ProcessorsStruct{}
|
||||||
|
@ -1804,10 +1852,18 @@ func testProcessors(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p2.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
if p2.B4InsertFlag == 0 {
|
||||||
if p2.AfterInsertedFlag != 0 { t.Error(errors.New("AfterInsertedFlag is set")) }
|
t.Error(errors.New("B4InsertFlag not set"))
|
||||||
if p2.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
}
|
||||||
if p2.AfterInsertedViaExt != 0 { t.Error(errors.New("AfterInsertedViaExt is set")) }
|
if p2.AfterInsertedFlag != 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedFlag is set"))
|
||||||
|
}
|
||||||
|
if p2.B4InsertViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4InsertViaExt not set"))
|
||||||
|
}
|
||||||
|
if p2.AfterInsertedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// --
|
// --
|
||||||
|
|
||||||
|
@ -1835,10 +1891,18 @@ func testProcessors(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4UpdateFlag == 0 { t.Error(errors.New("B4UpdateFlag not set")) }
|
if p.B4UpdateFlag == 0 {
|
||||||
if p.AfterUpdatedFlag == 0 { t.Error(errors.New("AfterUpdatedFlag not set")) }
|
t.Error(errors.New("B4UpdateFlag not set"))
|
||||||
if p.B4UpdateViaExt == 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
}
|
||||||
if p.AfterUpdatedViaExt == 0 { t.Error(errors.New("AfterUpdatedViaExt not set")) }
|
if p.AfterUpdatedFlag == 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedFlag not set"))
|
||||||
|
}
|
||||||
|
if p.B4UpdateViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4UpdateViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterUpdatedViaExt == 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedViaExt not set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
p2 = &ProcessorsStruct{}
|
p2 = &ProcessorsStruct{}
|
||||||
|
@ -1847,10 +1911,18 @@ func testProcessors(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p2.B4UpdateFlag == 0 { t.Error(errors.New("B4UpdateFlag not set")) }
|
if p2.B4UpdateFlag == 0 {
|
||||||
if p2.AfterUpdatedFlag != 0 { t.Error(errors.New("AfterUpdatedFlag is set")) }
|
t.Error(errors.New("B4UpdateFlag not set"))
|
||||||
if p2.B4UpdateViaExt == 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
}
|
||||||
if p2.AfterUpdatedViaExt != 0 { t.Error(errors.New("AfterUpdatedViaExt is set")) }
|
if p2.AfterUpdatedFlag != 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedFlag is set: " + string(p.AfterUpdatedFlag)))
|
||||||
|
}
|
||||||
|
if p2.B4UpdateViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4UpdateViaExt not set"))
|
||||||
|
}
|
||||||
|
if p2.AfterUpdatedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedViaExt is set: " + string(p.AfterUpdatedViaExt)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// --
|
// --
|
||||||
|
|
||||||
|
@ -1877,15 +1949,23 @@ func testProcessors(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4DeleteFlag == 0 { t.Error(errors.New("B4DeleteFlag not set")) }
|
if p.B4DeleteFlag == 0 {
|
||||||
if p.AfterDeletedFlag == 0 { t.Error(errors.New("AfterDeletedFlag not set")) }
|
t.Error(errors.New("B4DeleteFlag not set"))
|
||||||
if p.B4DeleteViaExt == 0 { t.Error(errors.New("B4DeleteViaExt not set")) }
|
}
|
||||||
if p.AfterDeletedViaExt == 0 { t.Error(errors.New("AfterDeletedViaExt not set")) }
|
if p.AfterDeletedFlag == 0 {
|
||||||
|
t.Error(errors.New("AfterDeletedFlag not set"))
|
||||||
|
}
|
||||||
|
if p.B4DeleteViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4DeleteViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterDeletedViaExt == 0 {
|
||||||
|
t.Error(errors.New("AfterDeletedViaExt not set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// --
|
// --
|
||||||
|
|
||||||
// test insert multi
|
// test insert multi
|
||||||
pslice := make([]*ProcessorsStruct, 0);
|
pslice := make([]*ProcessorsStruct, 0)
|
||||||
pslice = append(pslice, &ProcessorsStruct{})
|
pslice = append(pslice, &ProcessorsStruct{})
|
||||||
pslice = append(pslice, &ProcessorsStruct{})
|
pslice = append(pslice, &ProcessorsStruct{})
|
||||||
cnt, err := tempEngine.Before(b4InsertFunc).After(afterInsertFunc).Insert(&pslice)
|
cnt, err := tempEngine.Before(b4InsertFunc).After(afterInsertFunc).Insert(&pslice)
|
||||||
|
@ -1893,12 +1973,22 @@ func testProcessors(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if cnt != 2 { t.Error(errors.New("incorrect insert count")) }
|
if cnt != 2 {
|
||||||
|
t.Error(errors.New("incorrect insert count"))
|
||||||
|
}
|
||||||
for _, elem := range pslice {
|
for _, elem := range pslice {
|
||||||
if elem.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
if elem.B4InsertFlag == 0 {
|
||||||
if elem.AfterInsertedFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
t.Error(errors.New("B4InsertFlag not set"))
|
||||||
if elem.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
}
|
||||||
if elem.AfterInsertedViaExt == 0 { t.Error(errors.New("AfterInsertedViaExt not set")) }
|
if elem.AfterInsertedFlag == 0 {
|
||||||
|
t.Error(errors.New("B4InsertFlag not set"))
|
||||||
|
}
|
||||||
|
if elem.B4InsertViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4InsertFlag not set"))
|
||||||
|
}
|
||||||
|
if elem.AfterInsertedViaExt == 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedViaExt not set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1909,10 +1999,18 @@ func testProcessors(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p2.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
if p2.B4InsertFlag == 0 {
|
||||||
if p2.AfterInsertedFlag != 0 { t.Error(errors.New("AfterInsertedFlag is set")) }
|
t.Error(errors.New("B4InsertFlag not set"))
|
||||||
if p2.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
}
|
||||||
if p2.AfterInsertedViaExt != 0 { t.Error(errors.New("AfterInsertedViaExt is set")) }
|
if p2.AfterInsertedFlag != 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedFlag is set"))
|
||||||
|
}
|
||||||
|
if p2.B4InsertViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4InsertViaExt not set"))
|
||||||
|
}
|
||||||
|
if p2.AfterInsertedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// --
|
// --
|
||||||
|
@ -1938,7 +2036,6 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// test insert processors with tx rollback
|
// test insert processors with tx rollback
|
||||||
session := tempEngine.NewSession()
|
session := tempEngine.NewSession()
|
||||||
err = session.Begin()
|
err = session.Begin()
|
||||||
|
@ -1968,10 +2065,18 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
if p.B4InsertFlag == 0 {
|
||||||
if p.AfterInsertedFlag != 0 { t.Error(errors.New("B4InsertFlag is set")) }
|
t.Error(errors.New("B4InsertFlag not set"))
|
||||||
if p.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
}
|
||||||
if p.AfterInsertedViaExt != 0 { t.Error(errors.New("AfterInsertedViaExt is set")) }
|
if p.AfterInsertedFlag != 0 {
|
||||||
|
t.Error(errors.New("B4InsertFlag is set"))
|
||||||
|
}
|
||||||
|
if p.B4InsertViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4InsertViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterInsertedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = session.Rollback()
|
err = session.Rollback()
|
||||||
|
@ -1979,10 +2084,18 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
if p.B4InsertFlag == 0 {
|
||||||
if p.AfterInsertedFlag != 0 { t.Error(errors.New("B4InsertFlag is set")) }
|
t.Error(errors.New("B4InsertFlag not set"))
|
||||||
if p.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
}
|
||||||
if p.AfterInsertedViaExt != 0 { t.Error(errors.New("AfterInsertedViaExt is set")) }
|
if p.AfterInsertedFlag != 0 {
|
||||||
|
t.Error(errors.New("B4InsertFlag is set"))
|
||||||
|
}
|
||||||
|
if p.B4InsertViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4InsertViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterInsertedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
session.Close()
|
session.Close()
|
||||||
p2 := &ProcessorsStruct{}
|
p2 := &ProcessorsStruct{}
|
||||||
|
@ -1991,11 +2104,14 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p2.Id > 0 { t.Error(errors.New("tx got committed upon insert!?")) }
|
if p2.Id > 0 {
|
||||||
|
err = errors.New("tx got committed upon insert!?")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// --
|
// --
|
||||||
|
|
||||||
|
|
||||||
// test insert processors with tx commit
|
// test insert processors with tx commit
|
||||||
session = tempEngine.NewSession()
|
session = tempEngine.NewSession()
|
||||||
err = session.Begin()
|
err = session.Begin()
|
||||||
|
@ -2010,10 +2126,18 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
if p.B4InsertFlag == 0 {
|
||||||
if p.AfterInsertedFlag != 0 { t.Error(errors.New("AfterInsertedFlag is set")) }
|
t.Error(errors.New("B4InsertFlag not set"))
|
||||||
if p.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
}
|
||||||
if p.AfterInsertedViaExt != 0 { t.Error(errors.New("AfterInsertedViaExt is set")) }
|
if p.AfterInsertedFlag != 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedFlag is set"))
|
||||||
|
}
|
||||||
|
if p.B4InsertViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4InsertViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterInsertedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = session.Commit()
|
err = session.Commit()
|
||||||
|
@ -2021,10 +2145,18 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
if p.B4InsertFlag == 0 {
|
||||||
if p.AfterInsertedFlag == 0 { t.Error(errors.New("AfterInsertedFlag not set")) }
|
t.Error(errors.New("B4InsertFlag not set"))
|
||||||
if p.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
}
|
||||||
if p.AfterInsertedViaExt == 0 { t.Error(errors.New("AfterInsertedViaExt not set")) }
|
if p.AfterInsertedFlag == 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedFlag not set"))
|
||||||
|
}
|
||||||
|
if p.B4InsertViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4InsertViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterInsertedViaExt == 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedViaExt not set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
session.Close()
|
session.Close()
|
||||||
p2 = &ProcessorsStruct{}
|
p2 = &ProcessorsStruct{}
|
||||||
|
@ -2033,15 +2165,22 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p2.B4InsertFlag == 0 { t.Error(errors.New("B4InsertFlag not set")) }
|
if p2.B4InsertFlag == 0 {
|
||||||
if p2.AfterInsertedFlag != 0 { t.Error(errors.New("AfterInsertedFlag is set")) }
|
t.Error(errors.New("B4InsertFlag not set"))
|
||||||
if p2.B4InsertViaExt == 0 { t.Error(errors.New("B4InsertViaExt not set")) }
|
}
|
||||||
if p2.AfterInsertedViaExt != 0 { t.Error(errors.New("AfterInsertedViaExt is set")) }
|
if p2.AfterInsertedFlag != 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedFlag is set"))
|
||||||
|
}
|
||||||
|
if p2.B4InsertViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4InsertViaExt not set"))
|
||||||
|
}
|
||||||
|
if p2.AfterInsertedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterInsertedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
insertedId := p2.Id
|
insertedId := p2.Id
|
||||||
// --
|
// --
|
||||||
|
|
||||||
|
|
||||||
// test update processors with tx rollback
|
// test update processors with tx rollback
|
||||||
session = tempEngine.NewSession()
|
session = tempEngine.NewSession()
|
||||||
err = session.Begin()
|
err = session.Begin()
|
||||||
|
@ -2073,20 +2212,36 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4UpdateFlag == 0 { t.Error(errors.New("B4UpdateFlag not set")) }
|
if p.B4UpdateFlag == 0 {
|
||||||
if p.AfterUpdatedFlag != 0 { t.Error(errors.New("AfterUpdatedFlag is set")) }
|
t.Error(errors.New("B4UpdateFlag not set"))
|
||||||
if p.B4UpdateViaExt == 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
}
|
||||||
if p.AfterUpdatedViaExt != 0 { t.Error(errors.New("AfterUpdatedViaExt is set")) }
|
if p.AfterUpdatedFlag != 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedFlag is set"))
|
||||||
|
}
|
||||||
|
if p.B4UpdateViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4UpdateViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterUpdatedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
err = session.Rollback()
|
err = session.Rollback()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4UpdateFlag == 0 { t.Error(errors.New("B4UpdateFlag not set")) }
|
if p.B4UpdateFlag == 0 {
|
||||||
if p.AfterUpdatedFlag != 0 { t.Error(errors.New("AfterUpdatedFlag is set")) }
|
t.Error(errors.New("B4UpdateFlag not set"))
|
||||||
if p.B4UpdateViaExt == 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
}
|
||||||
if p.AfterUpdatedViaExt != 0 { t.Error(errors.New("AfterUpdatedViaExt is set")) }
|
if p.AfterUpdatedFlag != 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedFlag is set"))
|
||||||
|
}
|
||||||
|
if p.B4UpdateViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4UpdateViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterUpdatedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
session.Close()
|
session.Close()
|
||||||
|
@ -2096,10 +2251,18 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p2.B4UpdateFlag != 0 { t.Error(errors.New("B4UpdateFlag is set")) }
|
if p2.B4UpdateFlag != 0 {
|
||||||
if p2.AfterUpdatedFlag != 0 { t.Error(errors.New("AfterUpdatedFlag is set")) }
|
t.Error(errors.New("B4UpdateFlag is set"))
|
||||||
if p2.B4UpdateViaExt != 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
}
|
||||||
if p2.AfterUpdatedViaExt != 0 { t.Error(errors.New("AfterUpdatedViaExt is set")) }
|
if p2.AfterUpdatedFlag != 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedFlag is set"))
|
||||||
|
}
|
||||||
|
if p2.B4UpdateViaExt != 0 {
|
||||||
|
t.Error(errors.New("B4UpdateViaExt not set"))
|
||||||
|
}
|
||||||
|
if p2.AfterUpdatedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// --
|
// --
|
||||||
|
|
||||||
|
@ -2118,20 +2281,36 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4UpdateFlag == 0 { t.Error(errors.New("B4UpdateFlag not set")) }
|
if p.B4UpdateFlag == 0 {
|
||||||
if p.AfterUpdatedFlag != 0 { t.Error(errors.New("AfterUpdatedFlag is set")) }
|
t.Error(errors.New("B4UpdateFlag not set"))
|
||||||
if p.B4UpdateViaExt == 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
}
|
||||||
if p.AfterUpdatedViaExt != 0 { t.Error(errors.New("AfterUpdatedViaExt is set")) }
|
if p.AfterUpdatedFlag != 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedFlag is set"))
|
||||||
|
}
|
||||||
|
if p.B4UpdateViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4UpdateViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterUpdatedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
err = session.Commit()
|
err = session.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4UpdateFlag == 0 { t.Error(errors.New("B4UpdateFlag not set")) }
|
if p.B4UpdateFlag == 0 {
|
||||||
if p.AfterUpdatedFlag == 0 { t.Error(errors.New("AfterUpdatedFlag not set")) }
|
t.Error(errors.New("B4UpdateFlag not set"))
|
||||||
if p.B4UpdateViaExt == 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
}
|
||||||
if p.AfterUpdatedViaExt == 0 { t.Error(errors.New("AfterUpdatedViaExt not set")) }
|
if p.AfterUpdatedFlag == 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedFlag not set"))
|
||||||
|
}
|
||||||
|
if p.B4UpdateViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4UpdateViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterUpdatedViaExt == 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedViaExt not set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
session.Close()
|
session.Close()
|
||||||
p2 = &ProcessorsStruct{}
|
p2 = &ProcessorsStruct{}
|
||||||
|
@ -2140,10 +2319,18 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4UpdateFlag == 0 { t.Error(errors.New("B4UpdateFlag not set")) }
|
if p.B4UpdateFlag == 0 {
|
||||||
if p.AfterUpdatedFlag == 0 { t.Error(errors.New("AfterUpdatedFlag not set")) }
|
t.Error(errors.New("B4UpdateFlag not set"))
|
||||||
if p.B4UpdateViaExt == 0 { t.Error(errors.New("B4UpdateViaExt not set")) }
|
}
|
||||||
if p.AfterUpdatedViaExt == 0 { t.Error(errors.New("AfterUpdatedViaExt not set")) }
|
if p.AfterUpdatedFlag == 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedFlag not set"))
|
||||||
|
}
|
||||||
|
if p.B4UpdateViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4UpdateViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterUpdatedViaExt == 0 {
|
||||||
|
t.Error(errors.New("AfterUpdatedViaExt not set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// --
|
// --
|
||||||
|
|
||||||
|
@ -2178,20 +2365,36 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4DeleteFlag == 0 { t.Error(errors.New("B4DeleteFlag not set")) }
|
if p.B4DeleteFlag == 0 {
|
||||||
if p.AfterDeletedFlag != 0 { t.Error(errors.New("AfterDeletedFlag is set")) }
|
t.Error(errors.New("B4DeleteFlag not set"))
|
||||||
if p.B4DeleteViaExt == 0 { t.Error(errors.New("B4DeleteViaExt not set")) }
|
}
|
||||||
if p.AfterDeletedViaExt != 0 { t.Error(errors.New("AfterDeletedViaExt is set")) }
|
if p.AfterDeletedFlag != 0 {
|
||||||
|
t.Error(errors.New("AfterDeletedFlag is set"))
|
||||||
|
}
|
||||||
|
if p.B4DeleteViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4DeleteViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterDeletedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterDeletedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
err = session.Rollback()
|
err = session.Rollback()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4DeleteFlag == 0 { t.Error(errors.New("B4DeleteFlag not set")) }
|
if p.B4DeleteFlag == 0 {
|
||||||
if p.AfterDeletedFlag != 0 { t.Error(errors.New("AfterDeletedFlag is set")) }
|
t.Error(errors.New("B4DeleteFlag not set"))
|
||||||
if p.B4DeleteViaExt == 0 { t.Error(errors.New("B4DeleteViaExt not set")) }
|
}
|
||||||
if p.AfterDeletedViaExt != 0 { t.Error(errors.New("AfterDeletedViaExt is set")) }
|
if p.AfterDeletedFlag != 0 {
|
||||||
|
t.Error(errors.New("AfterDeletedFlag is set"))
|
||||||
|
}
|
||||||
|
if p.B4DeleteViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4DeleteViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterDeletedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterDeletedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
session.Close()
|
session.Close()
|
||||||
|
|
||||||
|
@ -2201,10 +2404,18 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p2.B4DeleteFlag != 0 { t.Error(errors.New("B4DeleteFlag is set")) }
|
if p2.B4DeleteFlag != 0 {
|
||||||
if p2.AfterDeletedFlag != 0 { t.Error(errors.New("AfterDeletedFlag is set")) }
|
t.Error(errors.New("B4DeleteFlag is set"))
|
||||||
if p2.B4DeleteViaExt != 0 { t.Error(errors.New("B4DeleteViaExt is set")) }
|
}
|
||||||
if p2.AfterDeletedViaExt != 0 { t.Error(errors.New("AfterDeletedViaExt is set")) }
|
if p2.AfterDeletedFlag != 0 {
|
||||||
|
t.Error(errors.New("AfterDeletedFlag is set"))
|
||||||
|
}
|
||||||
|
if p2.B4DeleteViaExt != 0 {
|
||||||
|
t.Error(errors.New("B4DeleteViaExt is set"))
|
||||||
|
}
|
||||||
|
if p2.AfterDeletedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterDeletedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// --
|
// --
|
||||||
|
|
||||||
|
@ -2223,20 +2434,36 @@ func testProcessorsTx(engine *Engine, t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4DeleteFlag == 0 { t.Error(errors.New("B4DeleteFlag not set")) }
|
if p.B4DeleteFlag == 0 {
|
||||||
if p.AfterDeletedFlag != 0 { t.Error(errors.New("AfterDeletedFlag is set")) }
|
t.Error(errors.New("B4DeleteFlag not set"))
|
||||||
if p.B4DeleteViaExt == 0 { t.Error(errors.New("B4DeleteViaExt not set")) }
|
}
|
||||||
if p.AfterDeletedViaExt != 0 { t.Error(errors.New("AfterDeletedViaExt is set")) }
|
if p.AfterDeletedFlag != 0 {
|
||||||
|
t.Error(errors.New("AfterDeletedFlag is set"))
|
||||||
|
}
|
||||||
|
if p.B4DeleteViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4DeleteViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterDeletedViaExt != 0 {
|
||||||
|
t.Error(errors.New("AfterDeletedViaExt is set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
err = session.Commit()
|
err = session.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if p.B4DeleteFlag == 0 { t.Error(errors.New("B4DeleteFlag not set")) }
|
if p.B4DeleteFlag == 0 {
|
||||||
if p.AfterDeletedFlag == 0 { t.Error(errors.New("AfterDeletedFlag not set")) }
|
t.Error(errors.New("B4DeleteFlag not set"))
|
||||||
if p.B4DeleteViaExt == 0 { t.Error(errors.New("B4DeleteViaExt not set")) }
|
}
|
||||||
if p.AfterDeletedViaExt == 0 { t.Error(errors.New("AfterDeletedViaExt not set")) }
|
if p.AfterDeletedFlag == 0 {
|
||||||
|
t.Error(errors.New("AfterDeletedFlag not set"))
|
||||||
|
}
|
||||||
|
if p.B4DeleteViaExt == 0 {
|
||||||
|
t.Error(errors.New("B4DeleteViaExt not set"))
|
||||||
|
}
|
||||||
|
if p.AfterDeletedViaExt == 0 {
|
||||||
|
t.Error(errors.New("AfterDeletedViaExt not set"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
session.Close()
|
session.Close()
|
||||||
// --
|
// --
|
||||||
|
@ -2336,6 +2563,8 @@ func testAll2(engine *Engine, t *testing.T) {
|
||||||
testTime(engine, t)
|
testTime(engine, t)
|
||||||
fmt.Println("-------------- testPrefixTableName --------------")
|
fmt.Println("-------------- testPrefixTableName --------------")
|
||||||
testPrefixTableName(engine, t)
|
testPrefixTableName(engine, t)
|
||||||
|
//fmt.Println("-------------- testCreatedUpdated --------------")
|
||||||
|
//testCreatedUpdated(engine, t)
|
||||||
fmt.Println("-------------- processors --------------")
|
fmt.Println("-------------- processors --------------")
|
||||||
testProcessors(engine, t)
|
testProcessors(engine, t)
|
||||||
fmt.Println("-------------- processors TX --------------")
|
fmt.Println("-------------- processors TX --------------")
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
## Changelog
|
## Changelog
|
||||||
|
|
||||||
|
* **v0.2.3** : Improved documents; Optimistic Locking support; Timestamp with time zone support; Mapper change to tableMapper and columnMapper & added PrefixMapper & SuffixMapper support custom table or column name's prefix and suffix;Insert now return affected, err instead of id, err; Added UseBool & Distinct;
|
||||||
* **v0.2.2** : Postgres drivers now support lib/pq; Added method Iterate for record by record to handler;Added SetMaxConns(go1.2+) support; some bugs fixed.
|
* **v0.2.2** : Postgres drivers now support lib/pq; Added method Iterate for record by record to handler;Added SetMaxConns(go1.2+) support; some bugs fixed.
|
||||||
* **v0.2.1** : Added database reverse tool, now support generate go & c++ codes, see [Xorm Tool README](https://github.com/lunny/xorm/blob/master/xorm/README.md); some bug fixed.
|
* **v0.2.1** : Added database reverse tool, now support generate go & c++ codes, see [Xorm Tool README](https://github.com/lunny/xorm/blob/master/xorm/README.md); some bug fixed.
|
||||||
* **v0.2.0** : Added Cache supported, select is speeder up 3~5x; Added SameMapper for same name between struct and table; Added Sync method for auto added tables, columns, indexes;
|
* **v0.2.0** : Added Cache supported, select is speeder up 3~5x; Added SameMapper for same name between struct and table; Added Sync method for auto added tables, columns, indexes;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
## 更新日志
|
## 更新日志
|
||||||
|
|
||||||
|
* **v0.2.3** : 改善了文档;提供了乐观锁支持;添加了带时区时间字段支持;Mapper现在分成表名Mapper和字段名Mapper,同时实现了表或字段的自定义前缀后缀;Insert方法的返回值含义从id, err更改为 affected, err,请大家注意;添加了UseBool 和 Distinct函数。
|
||||||
* **v0.2.2** : Postgres驱动新增了对lib/pq的支持;新增了逐条遍历方法Iterate;新增了SetMaxConns(go1.2+)支持,修复了bug若干;
|
* **v0.2.2** : Postgres驱动新增了对lib/pq的支持;新增了逐条遍历方法Iterate;新增了SetMaxConns(go1.2+)支持,修复了bug若干;
|
||||||
* **v0.2.1** : 新增数据库反转工具,当前支持go和c++代码的生成,详见 [Xorm Tool README](https://github.com/lunny/xorm/blob/master/xorm/README.md); 修复了一些bug.
|
* **v0.2.1** : 新增数据库反转工具,当前支持go和c++代码的生成,详见 [Xorm Tool README](https://github.com/lunny/xorm/blob/master/xorm/README.md); 修复了一些bug.
|
||||||
* **v0.2.0** : 新增 [缓存](https://github.com/lunny/xorm/blob/master/docs/QuickStart.md#120)支持,查询速度提升3-5倍; 新增数据库表和Struct同名的映射方式; 新增Sync同步表结构;
|
* **v0.2.0** : 新增 [缓存](https://github.com/lunny/xorm/blob/master/docs/QuickStart.md#120)支持,查询速度提升3-5倍; 新增数据库表和Struct同名的映射方式; 新增Sync同步表结构;
|
||||||
|
|
35
session.go
35
session.go
|
@ -1307,6 +1307,30 @@ func rows2maps(rows *sql.Rows) (resultsSlice []map[string][]byte, err error) {
|
||||||
return resultsSlice, nil
|
return resultsSlice, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (session *Session) query(sql string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error) {
|
||||||
|
for _, filter := range session.Engine.Filters {
|
||||||
|
sql = filter.Do(sql, session)
|
||||||
|
}
|
||||||
|
|
||||||
|
session.Engine.LogSQL(sql)
|
||||||
|
session.Engine.LogSQL(paramStr)
|
||||||
|
|
||||||
|
if session.IsAutoCommit {
|
||||||
|
return query(session.Db, sql, paramStr...)
|
||||||
|
}
|
||||||
|
return txQuery(session.Tx, sql, paramStr...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func txQuery(tx *sql.Tx, sql string, params ...interface{}) (resultsSlice []map[string][]byte, err error) {
|
||||||
|
rows, err := tx.Query(sql, params...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
return rows2maps(rows)
|
||||||
|
}
|
||||||
|
|
||||||
func query(db *sql.DB, sql string, params ...interface{}) (resultsSlice []map[string][]byte, err error) {
|
func query(db *sql.DB, sql string, params ...interface{}) (resultsSlice []map[string][]byte, err error) {
|
||||||
s, err := db.Prepare(sql)
|
s, err := db.Prepare(sql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1322,17 +1346,6 @@ func query(db *sql.DB, sql string, params ...interface{}) (resultsSlice []map[st
|
||||||
return rows2maps(rows)
|
return rows2maps(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) query(sql string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error) {
|
|
||||||
for _, filter := range session.Engine.Filters {
|
|
||||||
sql = filter.Do(sql, session)
|
|
||||||
}
|
|
||||||
|
|
||||||
session.Engine.LogSQL(sql)
|
|
||||||
session.Engine.LogSQL(paramStr)
|
|
||||||
|
|
||||||
return query(session.Db, sql, paramStr...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exec a raw sql and return records as []map[string][]byte
|
// Exec a raw sql and return records as []map[string][]byte
|
||||||
func (session *Session) Query(sql string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error) {
|
func (session *Session) Query(sql string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error) {
|
||||||
err = session.newDb()
|
err = session.newDb()
|
||||||
|
|
116
statement.go
116
statement.go
|
@ -116,6 +116,122 @@ func (statement *Statement) Table(tableNameOrBean interface{}) *Statement {
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*func (statement *Statement) genFields(bean interface{}) map[string]interface{} {
|
||||||
|
results := make(map[string]interface{})
|
||||||
|
table := statement.Engine.autoMap(bean)
|
||||||
|
for _, col := range table.Columns {
|
||||||
|
fieldValue := col.ValueOf(bean)
|
||||||
|
fieldType := reflect.TypeOf(fieldValue.Interface())
|
||||||
|
var val interface{}
|
||||||
|
switch fieldType.Kind() {
|
||||||
|
case reflect.Bool:
|
||||||
|
if allUseBool {
|
||||||
|
val = fieldValue.Interface()
|
||||||
|
} else if _, ok := boolColumnMap[col.Name]; ok {
|
||||||
|
val = fieldValue.Interface()
|
||||||
|
} else {
|
||||||
|
// if a bool in a struct, it will not be as a condition because it default is false,
|
||||||
|
// please use Where() instead
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
case reflect.String:
|
||||||
|
if fieldValue.String() == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// for MyString, should convert to string or panic
|
||||||
|
if fieldType.String() != reflect.String.String() {
|
||||||
|
val = fieldValue.String()
|
||||||
|
} else {
|
||||||
|
val = fieldValue.Interface()
|
||||||
|
}
|
||||||
|
case reflect.Int8, reflect.Int16, reflect.Int, reflect.Int32, reflect.Int64:
|
||||||
|
if fieldValue.Int() == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val = fieldValue.Interface()
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
if fieldValue.Float() == 0.0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val = fieldValue.Interface()
|
||||||
|
case reflect.Uint8, reflect.Uint16, reflect.Uint, reflect.Uint32, reflect.Uint64:
|
||||||
|
if fieldValue.Uint() == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val = fieldValue.Interface()
|
||||||
|
case reflect.Struct:
|
||||||
|
if fieldType == reflect.TypeOf(time.Now()) {
|
||||||
|
t := fieldValue.Interface().(time.Time)
|
||||||
|
if t.IsZero() || !fieldValue.IsValid() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var str string
|
||||||
|
if col.SQLType.Name == Time {
|
||||||
|
s := t.UTC().Format("2006-01-02 15:04:05")
|
||||||
|
val = s[11:19]
|
||||||
|
} else if col.SQLType.Name == Date {
|
||||||
|
str = t.Format("2006-01-02")
|
||||||
|
val = str
|
||||||
|
} else {
|
||||||
|
val = t
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
engine.autoMapType(fieldValue.Type())
|
||||||
|
if table, ok := engine.Tables[fieldValue.Type()]; ok {
|
||||||
|
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumn().FieldName)
|
||||||
|
if pkField.Int() != 0 {
|
||||||
|
val = pkField.Interface()
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val = fieldValue.Interface()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case reflect.Array, reflect.Slice, reflect.Map:
|
||||||
|
if fieldValue == reflect.Zero(fieldType) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if fieldValue.IsNil() || !fieldValue.IsValid() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if col.SQLType.IsText() {
|
||||||
|
bytes, err := json.Marshal(fieldValue.Interface())
|
||||||
|
if err != nil {
|
||||||
|
engine.LogSQL(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val = string(bytes)
|
||||||
|
} else if col.SQLType.IsBlob() {
|
||||||
|
var bytes []byte
|
||||||
|
var err error
|
||||||
|
if (fieldType.Kind() == reflect.Array || fieldType.Kind() == reflect.Slice) &&
|
||||||
|
fieldType.Elem().Kind() == reflect.Uint8 {
|
||||||
|
if fieldValue.Len() > 0 {
|
||||||
|
val = fieldValue.Bytes()
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
bytes, err = json.Marshal(fieldValue.Interface())
|
||||||
|
if err != nil {
|
||||||
|
engine.LogSQL(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val = bytes
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
val = fieldValue.Interface()
|
||||||
|
}
|
||||||
|
results[col.Name] = val
|
||||||
|
}
|
||||||
|
return results
|
||||||
|
}*/
|
||||||
|
|
||||||
// Auto generating conditions according a struct
|
// Auto generating conditions according a struct
|
||||||
func buildConditions(engine *Engine, table *Table, bean interface{}, includeVersion bool, allUseBool bool, boolColumnMap map[string]bool) ([]string, []interface{}) {
|
func buildConditions(engine *Engine, table *Table, bean interface{}, includeVersion bool, allUseBool bool, boolColumnMap map[string]bool) ([]string, []interface{}) {
|
||||||
colNames := make([]string, 0)
|
colNames := make([]string, 0)
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
if [ ! -f install.sh ]; then
|
|
||||||
echo 'install must be run within its container folder' 1>&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
CURDIR=`pwd`
|
|
||||||
NEWPATH="$GOPATH/src/github.com/lunny/xorm/${PWD##*/}"
|
|
||||||
if [ ! -d "$NEWPATH" ]; then
|
|
||||||
ln -s $CURDIR $NEWPATH
|
|
||||||
fi
|
|
||||||
|
|
||||||
gofmt -w $CURDIR
|
|
||||||
|
|
||||||
cd $NEWPATH
|
|
||||||
go install ${PWD##*/}
|
|
||||||
cd $CURDIR
|
|
||||||
|
|
||||||
echo 'finished'
|
|
|
@ -0,0 +1,121 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/lunny/xorm"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var CmdShell = &Command{
|
||||||
|
UsageLine: "shell driverName datasourceName",
|
||||||
|
Short: "a general shell to operate all kinds of database",
|
||||||
|
Long: `
|
||||||
|
general database's shell for sqlite3, mysql, postgres.
|
||||||
|
|
||||||
|
driverName Database driver name, now supported four: mysql mymysql sqlite3 postgres
|
||||||
|
datasourceName Database connection uri, for detail infomation please visit driver's project page
|
||||||
|
`,
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
CmdShell.Run = runShell
|
||||||
|
CmdShell.Flags = map[string]bool{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var engine *xorm.Engine
|
||||||
|
|
||||||
|
func runShell(cmd *Command, args []string) {
|
||||||
|
if len(args) != 2 {
|
||||||
|
fmt.Println("params error, please see xorm help shell")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
engine, err = xorm.NewEngine(args[0], args[1])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var scmd string
|
||||||
|
fmt.Print("xorm$ ")
|
||||||
|
for {
|
||||||
|
var input string
|
||||||
|
_, err := fmt.Scan(&input)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.ToLower(input) == "exit" {
|
||||||
|
fmt.Println("bye")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.HasSuffix(input, ";") {
|
||||||
|
scmd = scmd + " " + input
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
scmd = scmd + " " + input
|
||||||
|
lcmd := strings.TrimSpace(strings.ToLower(scmd))
|
||||||
|
if strings.HasPrefix(lcmd, "select") {
|
||||||
|
res, err := engine.Query(scmd + "\n")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
} else {
|
||||||
|
if len(res) <= 0 {
|
||||||
|
fmt.Println("no records")
|
||||||
|
} else {
|
||||||
|
columns := make(map[string]int)
|
||||||
|
for k, _ := range res[0] {
|
||||||
|
columns[k] = len(k)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, m := range res {
|
||||||
|
for k, s := range m {
|
||||||
|
l := len(string(s))
|
||||||
|
if l > columns[k] {
|
||||||
|
columns[k] = l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var maxlen = 0
|
||||||
|
for _, l := range columns {
|
||||||
|
maxlen = maxlen + l + 3
|
||||||
|
}
|
||||||
|
maxlen = maxlen + 1
|
||||||
|
|
||||||
|
fmt.Println(strings.Repeat("-", maxlen))
|
||||||
|
fmt.Print("|")
|
||||||
|
slice := make([]string, 0)
|
||||||
|
for k, l := range columns {
|
||||||
|
fmt.Print(" " + k + " ")
|
||||||
|
fmt.Print(strings.Repeat(" ", l-len(k)))
|
||||||
|
fmt.Print("|")
|
||||||
|
slice = append(slice, k)
|
||||||
|
}
|
||||||
|
fmt.Print("\n")
|
||||||
|
for _, r := range res {
|
||||||
|
fmt.Print("|")
|
||||||
|
for _, k := range slice {
|
||||||
|
fmt.Print(" " + string(r[k]) + " ")
|
||||||
|
fmt.Print(strings.Repeat(" ", columns[k]-len(string(r[k]))))
|
||||||
|
fmt.Print("|")
|
||||||
|
}
|
||||||
|
fmt.Print("\n")
|
||||||
|
}
|
||||||
|
fmt.Println(strings.Repeat("-", maxlen))
|
||||||
|
//fmt.Println(res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cnt, err := engine.Exec(scmd)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%d records changed.\n", cnt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scmd = ""
|
||||||
|
fmt.Print("xorm$ ")
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,6 +22,7 @@ const go11tag = true
|
||||||
// The order here is the order in which they are printed by 'gopm help'.
|
// The order here is the order in which they are printed by 'gopm help'.
|
||||||
var commands = []*Command{
|
var commands = []*Command{
|
||||||
CmdReverse,
|
CmdReverse,
|
||||||
|
CmdShell,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
Loading…
Reference in New Issue