add support to int32, int, uint, uint32, uint64 for pk
This commit is contained in:
parent
bafda62d49
commit
d5a4324585
364
base_test.go
364
base_test.go
|
@ -1448,12 +1448,32 @@ func testIndexAndUnique(engine *Engine, t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type IntId struct {
|
type IntId struct {
|
||||||
Id int
|
Id int `xorm:"pk autoincr"`
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Int32Id struct {
|
type Int32Id struct {
|
||||||
Id int32
|
Id int32 `xorm:"pk autoincr"`
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type UintId struct {
|
||||||
|
Id uint `xorm:"pk autoincr"`
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Uint32Id struct {
|
||||||
|
Id uint32 `xorm:"pk autoincr"`
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Uint64Id struct {
|
||||||
|
Id uint64 `xorm:"pk autoincr"`
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type StringPK struct {
|
||||||
|
Id string `xorm:"pk notnull"`
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1470,11 +1490,51 @@ func testIntId(engine *Engine, t *testing.T) {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = engine.Insert(&IntId{Name: "test"})
|
cnt, err := engine.Insert(&IntId{Name: "test"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
if cnt != 1 {
|
||||||
|
err = errors.New("insert count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bean := new(IntId)
|
||||||
|
has, err := engine.Get(bean)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if !has {
|
||||||
|
err = errors.New("get count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
beans := make([]IntId, 0)
|
||||||
|
err = engine.Find(&beans)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if len(beans) != 1 {
|
||||||
|
err = errors.New("get count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt, err = engine.Id(bean.Id).Delete(&IntId{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if cnt != 1 {
|
||||||
|
err = errors.New("insert count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testInt32Id(engine *Engine, t *testing.T) {
|
func testInt32Id(engine *Engine, t *testing.T) {
|
||||||
|
@ -1490,11 +1550,295 @@ func testInt32Id(engine *Engine, t *testing.T) {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = engine.Insert(&Int32Id{Name: "test"})
|
cnt, err := engine.Insert(&Int32Id{Name: "test"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cnt != 1 {
|
||||||
|
err = errors.New("insert count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bean := new(Int32Id)
|
||||||
|
has, err := engine.Get(bean)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if !has {
|
||||||
|
err = errors.New("get count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
beans := make([]Int32Id, 0)
|
||||||
|
err = engine.Find(&beans)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if len(beans) != 1 {
|
||||||
|
err = errors.New("get count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt, err = engine.Id(bean.Id).Delete(&Int32Id{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if cnt != 1 {
|
||||||
|
err = errors.New("insert count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testUintId(engine *Engine, t *testing.T) {
|
||||||
|
err := engine.DropTables(&UintId{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = engine.CreateTables(&UintId{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt, err := engine.Insert(&UintId{Name: "test"})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if cnt != 1 {
|
||||||
|
err = errors.New("insert count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bean := new(UintId)
|
||||||
|
has, err := engine.Get(bean)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if !has {
|
||||||
|
err = errors.New("get count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
beans := make([]UintId, 0)
|
||||||
|
err = engine.Find(&beans)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if len(beans) != 1 {
|
||||||
|
err = errors.New("get count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt, err = engine.Id(bean.Id).Delete(&UintId{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if cnt != 1 {
|
||||||
|
err = errors.New("insert count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testUint32Id(engine *Engine, t *testing.T) {
|
||||||
|
err := engine.DropTables(&Uint32Id{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = engine.CreateTables(&Uint32Id{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt, err := engine.Insert(&Uint32Id{Name: "test"})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cnt != 1 {
|
||||||
|
err = errors.New("insert count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bean := new(Uint32Id)
|
||||||
|
has, err := engine.Get(bean)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if !has {
|
||||||
|
err = errors.New("get count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
beans := make([]Uint32Id, 0)
|
||||||
|
err = engine.Find(&beans)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if len(beans) != 1 {
|
||||||
|
err = errors.New("get count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt, err = engine.Id(bean.Id).Delete(&Uint32Id{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if cnt != 1 {
|
||||||
|
err = errors.New("insert count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testUint64Id(engine *Engine, t *testing.T) {
|
||||||
|
err := engine.DropTables(&Uint64Id{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = engine.CreateTables(&Uint64Id{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt, err := engine.Insert(&Uint64Id{Name: "test"})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cnt != 1 {
|
||||||
|
err = errors.New("insert count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bean := new(Uint64Id)
|
||||||
|
has, err := engine.Get(bean)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if !has {
|
||||||
|
err = errors.New("get count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
beans := make([]Uint64Id, 0)
|
||||||
|
err = engine.Find(&beans)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if len(beans) != 1 {
|
||||||
|
err = errors.New("get count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt, err = engine.Id(bean.Id).Delete(&Uint64Id{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if cnt != 1 {
|
||||||
|
err = errors.New("insert count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testStringPK(engine *Engine, t *testing.T) {
|
||||||
|
err := engine.DropTables(&StringPK{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = engine.CreateTables(&StringPK{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt, err := engine.Insert(&StringPK{Name: "test"})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cnt != 1 {
|
||||||
|
err = errors.New("insert count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bean := new(StringPK)
|
||||||
|
has, err := engine.Get(bean)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if !has {
|
||||||
|
err = errors.New("get count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
beans := make([]StringPK, 0)
|
||||||
|
err = engine.Find(&beans)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if len(beans) != 1 {
|
||||||
|
err = errors.New("get count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cnt, err = engine.Id(bean.Id).Delete(&StringPK{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if cnt != 1 {
|
||||||
|
err = errors.New("insert count should be one")
|
||||||
|
t.Error(err)
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testMetaInfo(engine *Engine, t *testing.T) {
|
func testMetaInfo(engine *Engine, t *testing.T) {
|
||||||
|
@ -3407,9 +3751,15 @@ func testAll2(engine *Engine, t *testing.T) {
|
||||||
fmt.Println("-------------- testIndexAndUnique --------------")
|
fmt.Println("-------------- testIndexAndUnique --------------")
|
||||||
testIndexAndUnique(engine, t)
|
testIndexAndUnique(engine, t)
|
||||||
fmt.Println("-------------- testIntId --------------")
|
fmt.Println("-------------- testIntId --------------")
|
||||||
//testIntId(engine, t)
|
testIntId(engine, t)
|
||||||
fmt.Println("-------------- testInt32Id --------------")
|
fmt.Println("-------------- testInt32Id --------------")
|
||||||
//testInt32Id(engine, t)
|
testInt32Id(engine, t)
|
||||||
|
fmt.Println("-------------- testUintId --------------")
|
||||||
|
testUintId(engine, t)
|
||||||
|
fmt.Println("-------------- testUint32Id --------------")
|
||||||
|
testUint32Id(engine, t)
|
||||||
|
fmt.Println("-------------- testUint64Id --------------")
|
||||||
|
testUint64Id(engine, t)
|
||||||
fmt.Println("-------------- testMetaInfo --------------")
|
fmt.Println("-------------- testMetaInfo --------------")
|
||||||
testMetaInfo(engine, t)
|
testMetaInfo(engine, t)
|
||||||
fmt.Println("-------------- testIterate --------------")
|
fmt.Println("-------------- testIterate --------------")
|
||||||
|
@ -3446,4 +3796,6 @@ func testAll3(engine *Engine, t *testing.T) {
|
||||||
testNullValue(engine, t)
|
testNullValue(engine, t)
|
||||||
fmt.Println("-------------- testCompositeKey --------------")
|
fmt.Println("-------------- testCompositeKey --------------")
|
||||||
testCompositeKey(engine, t)
|
testCompositeKey(engine, t)
|
||||||
|
fmt.Println("-------------- testStringPK --------------")
|
||||||
|
testStringPK(engine, t)
|
||||||
}
|
}
|
||||||
|
|
|
@ -476,7 +476,7 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
|
||||||
table.ColumnsSeq = append(table.ColumnsSeq, name)
|
table.ColumnsSeq = append(table.ColumnsSeq, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
table.PrimaryKey = parentTable.PrimaryKey
|
table.PrimaryKeys = parentTable.PrimaryKeys
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var indexType int
|
var indexType int
|
||||||
|
@ -602,12 +602,13 @@ func (engine *Engine) mapType(t reflect.Type) *Table {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if idFieldColName != "" && table.PrimaryKey == "" {
|
if idFieldColName != "" && len(table.PrimaryKeys) == 0 {
|
||||||
col := table.Columns[idFieldColName]
|
col := table.Columns[idFieldColName]
|
||||||
col.IsPrimaryKey = true
|
col.IsPrimaryKey = true
|
||||||
col.IsAutoIncrement = true
|
col.IsAutoIncrement = true
|
||||||
col.Nullable = false
|
col.Nullable = false
|
||||||
table.PrimaryKey = col.Name
|
table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
|
||||||
|
table.AutoIncrement = col.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
return table
|
return table
|
||||||
|
|
|
@ -40,10 +40,10 @@ type IdFilter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IdFilter) Do(sql string, session *Session) string {
|
func (i *IdFilter) Do(sql string, session *Session) string {
|
||||||
if session.Statement.RefTable != nil && session.Statement.RefTable.PrimaryKey != "" {
|
if session.Statement.RefTable != nil && len(session.Statement.RefTable.PrimaryKeys) == 1 {
|
||||||
sql = strings.Replace(sql, "`(id)`", session.Engine.Quote(session.Statement.RefTable.PrimaryKey), -1)
|
sql = strings.Replace(sql, "`(id)`", session.Engine.Quote(session.Statement.RefTable.PrimaryKeys[0]), -1)
|
||||||
sql = strings.Replace(sql, session.Engine.Quote("(id)"), session.Engine.Quote(session.Statement.RefTable.PrimaryKey), -1)
|
sql = strings.Replace(sql, session.Engine.Quote("(id)"), session.Engine.Quote(session.Statement.RefTable.PrimaryKeys[0]), -1)
|
||||||
return strings.Replace(sql, "(id)", session.Engine.Quote(session.Statement.RefTable.PrimaryKey), -1)
|
return strings.Replace(sql, "(id)", session.Engine.Quote(session.Statement.RefTable.PrimaryKeys[0]), -1)
|
||||||
}
|
}
|
||||||
return sql
|
return sql
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,11 @@ func (db *postgres) SqlType(c *Column) string {
|
||||||
switch t := c.SQLType.Name; t {
|
switch t := c.SQLType.Name; t {
|
||||||
case TinyInt:
|
case TinyInt:
|
||||||
res = SmallInt
|
res = SmallInt
|
||||||
|
|
||||||
case MediumInt, Int, Integer:
|
case MediumInt, Int, Integer:
|
||||||
|
if c.IsAutoIncrement {
|
||||||
|
return Serial
|
||||||
|
}
|
||||||
return Integer
|
return Integer
|
||||||
case Serial, BigSerial:
|
case Serial, BigSerial:
|
||||||
c.IsAutoIncrement = true
|
c.IsAutoIncrement = true
|
||||||
|
|
|
@ -8,11 +8,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func newPostgresEngine() (*Engine, error) {
|
func newPostgresEngine() (*Engine, error) {
|
||||||
return NewEngine("postgres", "dbname=xorm_test sslmode=disable")
|
return NewEngine("postgres", "dbname=xorm_test user=lunny password=1234 sslmode=disable")
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPostgresDriverDB() (*sql.DB, error) {
|
func newPostgresDriverDB() (*sql.DB, error) {
|
||||||
return sql.Open("postgres", "dbname=xorm_test sslmode=disable")
|
return sql.Open("postgres", "dbname=xorm_test user=lunny password=1234 sslmode=disable")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPostgres(t *testing.T) {
|
func TestPostgres(t *testing.T) {
|
||||||
|
|
140
session.go
140
session.go
|
@ -577,7 +577,7 @@ func (session *Session) DropTable(bean interface{}) error {
|
||||||
|
|
||||||
func (statement *Statement) convertIdSql(sql string) string {
|
func (statement *Statement) convertIdSql(sql string) string {
|
||||||
if statement.RefTable != nil {
|
if statement.RefTable != nil {
|
||||||
col := statement.RefTable.PKColumn()
|
col := statement.RefTable.PKColumns()[0]
|
||||||
if col != nil {
|
if col != nil {
|
||||||
sqls := splitNNoCase(sql, "from", 2)
|
sqls := splitNNoCase(sql, "from", 2)
|
||||||
if len(sqls) != 2 {
|
if len(sqls) != 2 {
|
||||||
|
@ -592,7 +592,8 @@ func (statement *Statement) convertIdSql(sql string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) cacheGet(bean interface{}, sql string, args ...interface{}) (has bool, err error) {
|
func (session *Session) cacheGet(bean interface{}, sql string, args ...interface{}) (has bool, err error) {
|
||||||
if session.Statement.RefTable == nil || session.Statement.RefTable.PrimaryKey == "" {
|
// if has no reftable or number of pks is not equal to 1, then don't use cache currently
|
||||||
|
if session.Statement.RefTable == nil || len(session.Statement.RefTable.PrimaryKeys) != 1 {
|
||||||
return false, ErrCacheFailed
|
return false, ErrCacheFailed
|
||||||
}
|
}
|
||||||
for _, filter := range session.Engine.Filters {
|
for _, filter := range session.Engine.Filters {
|
||||||
|
@ -617,7 +618,7 @@ func (session *Session) cacheGet(bean interface{}, sql string, args ...interface
|
||||||
if len(resultsSlice) > 0 {
|
if len(resultsSlice) > 0 {
|
||||||
data := resultsSlice[0]
|
data := resultsSlice[0]
|
||||||
var id int64
|
var id int64
|
||||||
if v, ok := data[session.Statement.RefTable.PrimaryKey]; !ok {
|
if v, ok := data[session.Statement.RefTable.PrimaryKeys[0]]; !ok {
|
||||||
return false, ErrCacheFailed
|
return false, ErrCacheFailed
|
||||||
} else {
|
} else {
|
||||||
id, err = strconv.ParseInt(string(v), 10, 64)
|
id, err = strconv.ParseInt(string(v), 10, 64)
|
||||||
|
@ -672,7 +673,7 @@ func (session *Session) cacheGet(bean interface{}, sql string, args ...interface
|
||||||
|
|
||||||
func (session *Session) cacheFind(t reflect.Type, sql string, rowsSlicePtr interface{}, args ...interface{}) (err error) {
|
func (session *Session) cacheFind(t reflect.Type, sql string, rowsSlicePtr interface{}, args ...interface{}) (err error) {
|
||||||
if session.Statement.RefTable == nil ||
|
if session.Statement.RefTable == nil ||
|
||||||
session.Statement.RefTable.PrimaryKey == "" ||
|
len(session.Statement.RefTable.PrimaryKeys) != 1 ||
|
||||||
indexNoCase(sql, "having") != -1 ||
|
indexNoCase(sql, "having") != -1 ||
|
||||||
indexNoCase(sql, "group by") != -1 {
|
indexNoCase(sql, "group by") != -1 {
|
||||||
return ErrCacheFailed
|
return ErrCacheFailed
|
||||||
|
@ -708,7 +709,7 @@ func (session *Session) cacheFind(t reflect.Type, sql string, rowsSlicePtr inter
|
||||||
for _, data := range resultsSlice {
|
for _, data := range resultsSlice {
|
||||||
//fmt.Println(data)
|
//fmt.Println(data)
|
||||||
var id int64
|
var id int64
|
||||||
if v, ok := data[session.Statement.RefTable.PrimaryKey]; !ok {
|
if v, ok := data[session.Statement.RefTable.PrimaryKeys[0]]; !ok {
|
||||||
return errors.New("no id")
|
return errors.New("no id")
|
||||||
} else {
|
} else {
|
||||||
id, err = strconv.ParseInt(string(v), 10, 64)
|
id, err = strconv.ParseInt(string(v), 10, 64)
|
||||||
|
@ -729,7 +730,7 @@ func (session *Session) cacheFind(t reflect.Type, sql string, rowsSlicePtr inter
|
||||||
}
|
}
|
||||||
|
|
||||||
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
|
sliceValue := reflect.Indirect(reflect.ValueOf(rowsSlicePtr))
|
||||||
pkFieldName := session.Statement.RefTable.PKColumn().FieldName
|
pkFieldName := session.Statement.RefTable.PKColumns()[0].FieldName
|
||||||
|
|
||||||
ididxes := make(map[int64]int)
|
ididxes := make(map[int64]int)
|
||||||
var ides []interface{} = make([]interface{}, 0)
|
var ides []interface{} = make([]interface{}, 0)
|
||||||
|
@ -743,7 +744,18 @@ func (session *Session) cacheFind(t reflect.Type, sql string, rowsSlicePtr inter
|
||||||
} else {
|
} else {
|
||||||
session.Engine.LogDebug("[xorm:cacheFind] cached bean:", tableName, id, bean)
|
session.Engine.LogDebug("[xorm:cacheFind] cached bean:", tableName, id, bean)
|
||||||
|
|
||||||
sid := reflect.Indirect(reflect.ValueOf(bean)).FieldByName(pkFieldName).Int()
|
pkField := reflect.Indirect(reflect.ValueOf(bean)).FieldByName(pkFieldName)
|
||||||
|
|
||||||
|
var sid int64
|
||||||
|
switch pkField.Type().Kind() {
|
||||||
|
case reflect.Int32, reflect.Int, reflect.Int64:
|
||||||
|
sid = pkField.Int()
|
||||||
|
case reflect.Uint, reflect.Uint32, reflect.Uint64:
|
||||||
|
sid = int64(pkField.Uint())
|
||||||
|
default:
|
||||||
|
return ErrCacheFailed
|
||||||
|
}
|
||||||
|
|
||||||
if sid != id {
|
if sid != id {
|
||||||
session.Engine.LogError("[xorm:cacheFind] error cache", id, sid, bean)
|
session.Engine.LogError("[xorm:cacheFind] error cache", id, sid, bean)
|
||||||
return ErrCacheFailed
|
return ErrCacheFailed
|
||||||
|
@ -795,7 +807,7 @@ func (session *Session) cacheFind(t reflect.Type, sql string, rowsSlicePtr inter
|
||||||
}
|
}
|
||||||
} else if sliceValue.Kind() == reflect.Map {
|
} else if sliceValue.Kind() == reflect.Map {
|
||||||
var key int64
|
var key int64
|
||||||
if table.PrimaryKey != "" {
|
if table.PrimaryKeys[0] != "" {
|
||||||
key = ids[j]
|
key = ids[j]
|
||||||
} else {
|
} else {
|
||||||
key = int64(j)
|
key = int64(j)
|
||||||
|
@ -923,6 +935,7 @@ func (session *Session) Get(bean interface{}) (bool, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(resultsSlice) < 1 {
|
if len(resultsSlice) < 1 {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
@ -1072,10 +1085,12 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
}
|
}
|
||||||
} else if sliceValue.Kind() == reflect.Map {
|
} else if sliceValue.Kind() == reflect.Map {
|
||||||
var key int64
|
var key int64
|
||||||
if table.PrimaryKey != "" {
|
// if there is only one pk, we can put the id as map key.
|
||||||
x, err := strconv.ParseInt(string(results[table.PrimaryKey]), 10, 64)
|
// TODO: should know if the column is ints
|
||||||
|
if len(table.PrimaryKeys) == 1 {
|
||||||
|
x, err := strconv.ParseInt(string(results[table.PrimaryKeys[0]]), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("pk " + table.PrimaryKey + " as int64: " + err.Error())
|
return errors.New("pk " + table.PrimaryKeys[0] + " as int64: " + err.Error())
|
||||||
}
|
}
|
||||||
key = x
|
key = x
|
||||||
} else {
|
} else {
|
||||||
|
@ -1258,7 +1273,6 @@ func row2map(rows *sql.Rows, fields []string) (resultsMap map[string][]byte, err
|
||||||
// sql driver converted type back to []bytes then to ORM's fields
|
// sql driver converted type back to []bytes then to ORM's fields
|
||||||
for ii, key := range fields {
|
for ii, key := range fields {
|
||||||
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
|
||||||
|
|
||||||
//if row is null then ignore
|
//if row is null then ignore
|
||||||
if rawValue.Interface() == nil {
|
if rawValue.Interface() == nil {
|
||||||
//fmt.Println("ignore ...", key, rawValue)
|
//fmt.Println("ignore ...", key, rawValue)
|
||||||
|
@ -1618,10 +1632,14 @@ func (session *Session) byte2Time(col *Column, data []byte) (outTime time.Time,
|
||||||
ssd := strings.Split(sdata, " ")
|
ssd := strings.Split(sdata, " ")
|
||||||
sdata = ssd[1]
|
sdata = ssd[1]
|
||||||
}
|
}
|
||||||
//if len(sdata) > 8 {
|
|
||||||
// sdata = sdata[len(sdata)-8:]
|
sdata = strings.TrimSpace(sdata)
|
||||||
//}
|
//fmt.Println(sdata)
|
||||||
fmt.Println(sdata)
|
if session.Engine.dialect.DBType() == MYSQL && len(sdata) > 8 {
|
||||||
|
sdata = sdata[len(sdata)-8:]
|
||||||
|
}
|
||||||
|
//fmt.Println(sdata)
|
||||||
|
|
||||||
st := fmt.Sprintf("2006-01-02 %v", sdata)
|
st := fmt.Sprintf("2006-01-02 %v", sdata)
|
||||||
x, err = time.Parse("2006-01-02 15:04:05", st)
|
x, err = time.Parse("2006-01-02 15:04:05", st)
|
||||||
} else {
|
} else {
|
||||||
|
@ -2082,14 +2100,14 @@ func (session *Session) value2Interface(col *Column, fieldValue reflect.Value) (
|
||||||
return fieldValue.Interface(), nil
|
return fieldValue.Interface(), nil
|
||||||
}
|
}
|
||||||
if fieldTable, ok := session.Engine.Tables[fieldValue.Type()]; ok {
|
if fieldTable, ok := session.Engine.Tables[fieldValue.Type()]; ok {
|
||||||
if fieldTable.PrimaryKey != "" {
|
if len(fieldTable.PrimaryKeys) == 1 {
|
||||||
pkField := reflect.Indirect(fieldValue).FieldByName(fieldTable.PKColumn().FieldName)
|
pkField := reflect.Indirect(fieldValue).FieldByName(fieldTable.PKColumns()[0].FieldName)
|
||||||
return pkField.Interface(), nil
|
return pkField.Interface(), nil
|
||||||
} else {
|
} else {
|
||||||
return 0, errors.New("no primary key")
|
return 0, fmt.Errorf("no primary key for col %v", col.Name)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return 0, errors.New(fmt.Sprintf("Unsupported type %v", fieldValue.Type()))
|
return 0, fmt.Errorf("Unsupported type %v\n", fieldValue.Type())
|
||||||
}
|
}
|
||||||
case reflect.Complex64, reflect.Complex128:
|
case reflect.Complex64, reflect.Complex128:
|
||||||
bytes, err := json.Marshal(fieldValue.Interface())
|
bytes, err := json.Marshal(fieldValue.Interface())
|
||||||
|
@ -2195,7 +2213,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
||||||
|
|
||||||
// for postgres, many of them didn't implement lastInsertId, so we should
|
// for postgres, many of them didn't implement lastInsertId, so we should
|
||||||
// implemented it ourself.
|
// implemented it ourself.
|
||||||
if session.Engine.DriverName != POSTGRES || table.PrimaryKey == "" {
|
if session.Engine.DriverName != POSTGRES || table.AutoIncrement == "" {
|
||||||
res, err := session.exec(sql, args...)
|
res, err := session.exec(sql, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
@ -2214,7 +2232,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if table.PrimaryKey == "" || table.PKColumn().SQLType.IsText() {
|
if table.AutoIncrement == "" {
|
||||||
return res.RowsAffected()
|
return res.RowsAffected()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2224,23 +2242,30 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
||||||
return res.RowsAffected()
|
return res.RowsAffected()
|
||||||
}
|
}
|
||||||
|
|
||||||
pkValue := table.PKColumn().ValueOf(bean)
|
aiValue := table.AutoIncrColumn().ValueOf(bean)
|
||||||
if !pkValue.IsValid() || pkValue.Int() != 0 || !pkValue.CanSet() {
|
if !aiValue.IsValid() /*|| aiValue.Int() != 0*/ || !aiValue.CanSet() {
|
||||||
return res.RowsAffected()
|
return res.RowsAffected()
|
||||||
}
|
}
|
||||||
|
|
||||||
var v interface{} = id
|
var v interface{} = id
|
||||||
switch pkValue.Type().Kind() {
|
switch aiValue.Type().Kind() {
|
||||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int:
|
case reflect.Int32:
|
||||||
|
v = int32(id)
|
||||||
|
case reflect.Int:
|
||||||
v = int(id)
|
v = int(id)
|
||||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
|
case reflect.Uint32:
|
||||||
|
v = uint32(id)
|
||||||
|
case reflect.Uint64:
|
||||||
|
v = uint64(id)
|
||||||
|
case reflect.Uint:
|
||||||
v = uint(id)
|
v = uint(id)
|
||||||
}
|
}
|
||||||
pkValue.Set(reflect.ValueOf(v))
|
aiValue.Set(reflect.ValueOf(v))
|
||||||
|
|
||||||
return res.RowsAffected()
|
return res.RowsAffected()
|
||||||
} else {
|
} else {
|
||||||
sql = sql + " RETURNING (id)"
|
//assert table.AutoIncrement != ""
|
||||||
|
sql = sql + " RETURNING " + session.Engine.Quote(table.AutoIncrement)
|
||||||
res, err := session.query(sql, args...)
|
res, err := session.query(sql, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
@ -2263,25 +2288,31 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
||||||
return 0, errors.New("insert no error but not returned id")
|
return 0, errors.New("insert no error but not returned id")
|
||||||
}
|
}
|
||||||
|
|
||||||
idByte := res[0][table.PrimaryKey]
|
idByte := res[0][table.AutoIncrement]
|
||||||
id, err := strconv.ParseInt(string(idByte), 10, 64)
|
id, err := strconv.ParseInt(string(idByte), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 1, err
|
return 1, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pkValue := table.PKColumn().ValueOf(bean)
|
aiValue := table.AutoIncrColumn().ValueOf(bean)
|
||||||
if !pkValue.IsValid() || pkValue.Int() != 0 || !pkValue.CanSet() {
|
if !aiValue.IsValid() /*|| aiValue. != 0*/ || !aiValue.CanSet() {
|
||||||
return 1, nil
|
return 1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var v interface{} = id
|
var v interface{} = id
|
||||||
switch pkValue.Type().Kind() {
|
switch aiValue.Type().Kind() {
|
||||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int:
|
case reflect.Int32:
|
||||||
|
v = int32(id)
|
||||||
|
case reflect.Int:
|
||||||
v = int(id)
|
v = int(id)
|
||||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
|
case reflect.Uint32:
|
||||||
|
v = uint32(id)
|
||||||
|
case reflect.Uint64:
|
||||||
|
v = uint64(id)
|
||||||
|
case reflect.Uint:
|
||||||
v = uint(id)
|
v = uint(id)
|
||||||
}
|
}
|
||||||
pkValue.Set(reflect.ValueOf(v))
|
aiValue.Set(reflect.ValueOf(v))
|
||||||
|
|
||||||
return 1, nil
|
return 1, nil
|
||||||
}
|
}
|
||||||
|
@ -2304,14 +2335,14 @@ func (session *Session) InsertOne(bean interface{}) (int64, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) convertUpdateSql(sql string) (string, string) {
|
func (statement *Statement) convertUpdateSql(sql string) (string, string) {
|
||||||
if statement.RefTable == nil || statement.RefTable.PrimaryKey == "" {
|
if statement.RefTable == nil || len(statement.RefTable.PrimaryKeys) != 1 {
|
||||||
return "", ""
|
return "", ""
|
||||||
}
|
}
|
||||||
sqls := splitNNoCase(sql, "where", 2)
|
sqls := splitNNoCase(sql, "where", 2)
|
||||||
if len(sqls) != 2 {
|
if len(sqls) != 2 {
|
||||||
if len(sqls) == 1 {
|
if len(sqls) == 1 {
|
||||||
return sqls[0], fmt.Sprintf("SELECT %v FROM %v",
|
return sqls[0], fmt.Sprintf("SELECT %v FROM %v",
|
||||||
statement.Engine.Quote(statement.RefTable.PrimaryKey),
|
statement.Engine.Quote(statement.RefTable.PrimaryKeys[0]),
|
||||||
statement.Engine.Quote(statement.RefTable.Name))
|
statement.Engine.Quote(statement.RefTable.Name))
|
||||||
}
|
}
|
||||||
return "", ""
|
return "", ""
|
||||||
|
@ -2320,22 +2351,31 @@ func (statement *Statement) convertUpdateSql(sql string) (string, string) {
|
||||||
var whereStr = sqls[1]
|
var whereStr = sqls[1]
|
||||||
|
|
||||||
//TODO: for postgres only, if any other database?
|
//TODO: for postgres only, if any other database?
|
||||||
if strings.Contains(sqls[1], "$") {
|
var paraStr string
|
||||||
dollers := strings.Split(sqls[1], "$")
|
if statement.Engine.dialect.DBType() == POSTGRES {
|
||||||
whereStr = dollers[0]
|
paraStr = "$"
|
||||||
for i, c := range dollers[1:] {
|
} else if statement.Engine.dialect.DBType() == MSSQL {
|
||||||
ccs := strings.SplitN(c, " ", 2)
|
paraStr = ":"
|
||||||
whereStr += fmt.Sprintf("$%v %v", i+1, ccs[1])
|
}
|
||||||
|
|
||||||
|
if paraStr != "" {
|
||||||
|
if strings.Contains(sqls[1], paraStr) {
|
||||||
|
dollers := strings.Split(sqls[1], paraStr)
|
||||||
|
whereStr = dollers[0]
|
||||||
|
for i, c := range dollers[1:] {
|
||||||
|
ccs := strings.SplitN(c, " ", 2)
|
||||||
|
whereStr += fmt.Sprintf(paraStr+"%v %v", i+1, ccs[1])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return sqls[0], fmt.Sprintf("SELECT %v FROM %v WHERE %v",
|
return sqls[0], fmt.Sprintf("SELECT %v FROM %v WHERE %v",
|
||||||
statement.Engine.Quote(statement.RefTable.PrimaryKey), statement.Engine.Quote(statement.TableName()),
|
statement.Engine.Quote(statement.RefTable.PrimaryKeys[0]), statement.Engine.Quote(statement.TableName()),
|
||||||
whereStr)
|
whereStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) cacheInsert(tables ...string) error {
|
func (session *Session) cacheInsert(tables ...string) error {
|
||||||
if session.Statement.RefTable == nil || session.Statement.RefTable.PrimaryKey == "" {
|
if session.Statement.RefTable == nil || len(session.Statement.RefTable.PrimaryKeys) != 1 {
|
||||||
return ErrCacheFailed
|
return ErrCacheFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2351,7 +2391,7 @@ func (session *Session) cacheInsert(tables ...string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) cacheUpdate(sql string, args ...interface{}) error {
|
func (session *Session) cacheUpdate(sql string, args ...interface{}) error {
|
||||||
if session.Statement.RefTable == nil || session.Statement.RefTable.PrimaryKey == "" {
|
if session.Statement.RefTable == nil || len(session.Statement.RefTable.PrimaryKeys) != 1 {
|
||||||
return ErrCacheFailed
|
return ErrCacheFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2389,7 +2429,7 @@ func (session *Session) cacheUpdate(sql string, args ...interface{}) error {
|
||||||
if len(resultsSlice) > 0 {
|
if len(resultsSlice) > 0 {
|
||||||
for _, data := range resultsSlice {
|
for _, data := range resultsSlice {
|
||||||
var id int64
|
var id int64
|
||||||
if v, ok := data[session.Statement.RefTable.PrimaryKey]; !ok {
|
if v, ok := data[session.Statement.RefTable.PrimaryKeys[0]]; !ok {
|
||||||
return errors.New("no id")
|
return errors.New("no id")
|
||||||
} else {
|
} else {
|
||||||
id, err = strconv.ParseInt(string(v), 10, 64)
|
id, err = strconv.ParseInt(string(v), 10, 64)
|
||||||
|
@ -2638,7 +2678,7 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) cacheDelete(sql string, args ...interface{}) error {
|
func (session *Session) cacheDelete(sql string, args ...interface{}) error {
|
||||||
if session.Statement.RefTable == nil || session.Statement.RefTable.PrimaryKey == "" {
|
if session.Statement.RefTable == nil || len(session.Statement.RefTable.PrimaryKeys) != 1 {
|
||||||
return ErrCacheFailed
|
return ErrCacheFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2663,7 +2703,7 @@ func (session *Session) cacheDelete(sql string, args ...interface{}) error {
|
||||||
if len(resultsSlice) > 0 {
|
if len(resultsSlice) > 0 {
|
||||||
for _, data := range resultsSlice {
|
for _, data := range resultsSlice {
|
||||||
var id int64
|
var id int64
|
||||||
if v, ok := data[session.Statement.RefTable.PrimaryKey]; !ok {
|
if v, ok := data[session.Statement.RefTable.PrimaryKeys[0]]; !ok {
|
||||||
return errors.New("no id")
|
return errors.New("no id")
|
||||||
} else {
|
} else {
|
||||||
id, err = strconv.ParseInt(string(v), 10, 64)
|
id, err = strconv.ParseInt(string(v), 10, 64)
|
||||||
|
|
13
sqlite3.go
13
sqlite3.go
|
@ -190,16 +190,19 @@ func (db *sqlite3) GetIndexes(tableName string) (map[string]*Index, error) {
|
||||||
|
|
||||||
indexes := make(map[string]*Index, 0)
|
indexes := make(map[string]*Index, 0)
|
||||||
for _, record := range res {
|
for _, record := range res {
|
||||||
var sql string
|
|
||||||
index := new(Index)
|
index := new(Index)
|
||||||
for name, content := range record {
|
sql := string(record["sql"])
|
||||||
if name == "sql" {
|
|
||||||
sql = string(content)
|
if sql == "" {
|
||||||
}
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
nNStart := strings.Index(sql, "INDEX")
|
nNStart := strings.Index(sql, "INDEX")
|
||||||
nNEnd := strings.Index(sql, "ON")
|
nNEnd := strings.Index(sql, "ON")
|
||||||
|
if nNStart == -1 || nNEnd == -1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
indexName := strings.Trim(sql[nNStart+6:nNEnd], "` []")
|
indexName := strings.Trim(sql[nNStart+6:nNEnd], "` []")
|
||||||
//fmt.Println(indexName)
|
//fmt.Println(indexName)
|
||||||
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
||||||
|
|
19
statement.go
19
statement.go
|
@ -335,11 +335,15 @@ func buildConditions(engine *Engine, table *Table, bean interface{},
|
||||||
} else {
|
} else {
|
||||||
engine.autoMapType(fieldValue.Type())
|
engine.autoMapType(fieldValue.Type())
|
||||||
if table, ok := engine.Tables[fieldValue.Type()]; ok {
|
if table, ok := engine.Tables[fieldValue.Type()]; ok {
|
||||||
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumn().FieldName)
|
if len(table.PrimaryKeys) == 1 {
|
||||||
if pkField.Int() != 0 {
|
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName)
|
||||||
val = pkField.Interface()
|
if pkField.Int() != 0 {
|
||||||
|
val = pkField.Interface()
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
continue
|
//TODO: how to handler?
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val = fieldValue.Interface()
|
val = fieldValue.Interface()
|
||||||
|
@ -753,9 +757,10 @@ func (statement *Statement) genCountSql(bean interface{}) (string, []interface{}
|
||||||
|
|
||||||
statement.ConditionStr = strings.Join(colNames, " AND ")
|
statement.ConditionStr = strings.Join(colNames, " AND ")
|
||||||
statement.BeanArgs = args
|
statement.BeanArgs = args
|
||||||
var id string = "*"
|
// count(index fieldname) > count(0) > count(*)
|
||||||
if table.PrimaryKey != "" {
|
var id string = "0"
|
||||||
id = statement.Engine.Quote(table.PrimaryKey)
|
if len(table.PrimaryKeys) == 1 {
|
||||||
|
id = statement.Engine.Quote(table.PrimaryKeys[0])
|
||||||
}
|
}
|
||||||
return statement.genSelectSql(fmt.Sprintf("COUNT(%v) AS %v", id, statement.Engine.Quote("total"))), append(statement.Params, statement.BeanArgs...)
|
return statement.genSelectSql(fmt.Sprintf("COUNT(%v) AS %v", id, statement.Engine.Quote("total"))), append(statement.Params, statement.BeanArgs...)
|
||||||
}
|
}
|
||||||
|
|
55
table.go
55
table.go
|
@ -339,16 +339,17 @@ func (col *Column) ValueOf(bean interface{}) reflect.Value {
|
||||||
|
|
||||||
// database table
|
// database table
|
||||||
type Table struct {
|
type Table struct {
|
||||||
Name string
|
Name string
|
||||||
Type reflect.Type
|
Type reflect.Type
|
||||||
ColumnsSeq []string
|
ColumnsSeq []string
|
||||||
Columns map[string]*Column
|
Columns map[string]*Column
|
||||||
Indexes map[string]*Index
|
Indexes map[string]*Index
|
||||||
PrimaryKey string
|
PrimaryKeys []string
|
||||||
Created map[string]bool
|
AutoIncrement string
|
||||||
Updated string
|
Created map[string]bool
|
||||||
Version string
|
Updated string
|
||||||
Cacher Cacher
|
Version string
|
||||||
|
Cacher Cacher
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -362,8 +363,16 @@ func NewTable(name string, t reflect.Type) *Table {
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
// if has primary key, return column
|
// if has primary key, return column
|
||||||
func (table *Table) PKColumn() *Column {
|
func (table *Table) PKColumns() []*Column {
|
||||||
return table.Columns[table.PrimaryKey]
|
columns := make([]*Column, 0)
|
||||||
|
for _, name := range table.PrimaryKeys {
|
||||||
|
columns = append(columns, table.Columns[name])
|
||||||
|
}
|
||||||
|
return columns
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Table) AutoIncrColumn() *Column {
|
||||||
|
return table.Columns[table.AutoIncrement]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (table *Table) VersionColumn() *Column {
|
func (table *Table) VersionColumn() *Column {
|
||||||
|
@ -375,7 +384,10 @@ func (table *Table) AddColumn(col *Column) {
|
||||||
table.ColumnsSeq = append(table.ColumnsSeq, col.Name)
|
table.ColumnsSeq = append(table.ColumnsSeq, col.Name)
|
||||||
table.Columns[col.Name] = col
|
table.Columns[col.Name] = col
|
||||||
if col.IsPrimaryKey {
|
if col.IsPrimaryKey {
|
||||||
table.PrimaryKey = col.Name
|
table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
|
||||||
|
}
|
||||||
|
if col.IsAutoIncrement {
|
||||||
|
table.AutoIncrement = col.Name
|
||||||
}
|
}
|
||||||
if col.IsCreated {
|
if col.IsCreated {
|
||||||
table.Created[col.Name] = true
|
table.Created[col.Name] = true
|
||||||
|
@ -408,8 +420,21 @@ func (table *Table) genCols(session *Session, bean interface{}, useCol bool, inc
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldValue := col.ValueOf(bean)
|
fieldValue := col.ValueOf(bean)
|
||||||
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
if col.IsAutoIncrement {
|
||||||
continue
|
switch fieldValue.Type().Kind() {
|
||||||
|
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
|
||||||
|
if fieldValue.Int() == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
|
||||||
|
if fieldValue.Uint() == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
case reflect.String:
|
||||||
|
if len(fieldValue.String()) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if session.Statement.ColumnStr != "" {
|
if session.Statement.ColumnStr != "" {
|
||||||
|
|
Loading…
Reference in New Issue