fix bug and add set conn max lifetime (#606)

* fix sqlite bug and add SetConnMaxLifetime

* add parse for composite keys

* required go 1.5+
This commit is contained in:
Lunny Xiao 2017-06-05 15:08:30 +08:00 committed by GitHub
parent 942887dea0
commit a4a8e4a739
5 changed files with 43 additions and 6 deletions

View File

@ -14,10 +14,6 @@ import (
"github.com/go-xorm/core"
)
// func init() {
// RegisterDialect("sqlite3", &sqlite3{})
// }
var (
sqlite3ReservedWords = map[string]bool{
"ABORT": true,
@ -310,11 +306,25 @@ func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Colu
for _, colStr := range colCreates {
reg = regexp.MustCompile(`,\s`)
colStr = reg.ReplaceAllString(colStr, ",")
if strings.HasPrefix(strings.TrimSpace(colStr), "PRIMARY KEY") {
parts := strings.Split(strings.TrimSpace(colStr), "(")
if len(parts) == 2 {
pkCols := strings.Split(strings.TrimRight(strings.TrimSpace(parts[1]), ")"), ",")
for _, pk := range pkCols {
if col, ok := cols[strings.Trim(strings.TrimSpace(pk), "`")]; ok {
col.IsPrimaryKey = true
}
}
}
continue
}
fields := strings.Fields(strings.TrimSpace(colStr))
col := new(core.Column)
col.Indexes = make(map[string]int)
col.Nullable = true
col.DefaultIsEmpty = true
for idx, field := range fields {
if idx == 0 {
col.Name = strings.Trim(strings.Trim(field, "`[] "), `"`)

2
doc.go
View File

@ -8,7 +8,7 @@ Package xorm is a simple and powerful ORM for Go.
Installation
Make sure you have installed Go 1.1+ and then:
Make sure you have installed Go 1.5+ and then:
go get github.com/go-xorm/xorm

View File

@ -195,6 +195,11 @@ func (engine *Engine) SetMaxIdleConns(conns int) {
engine.db.SetMaxIdleConns(conns)
}
// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
func (engine *Engine) SetConnMaxLifetime(d time.Duration) {
engine.db.SetConnMaxLifetime(d)
}
// SetDefaultCacher set the default cacher. Xorm's default not enable cacher.
func (engine *Engine) SetDefaultCacher(cacher core.Cacher) {
engine.Cacher = cacher

View File

@ -7,6 +7,7 @@ package xorm
import (
"errors"
"testing"
"time"
"github.com/go-xorm/core"
"github.com/stretchr/testify/assert"
@ -1117,3 +1118,24 @@ func TestSingleAutoIncrColumn(t *testing.T) {
_, err := testEngine.Insert(&Account{})
assert.NoError(t, err)
}
func TestCompositePK(t *testing.T) {
type TaskSolution struct {
UID string `xorm:"notnull pk UUID 'uid'"`
TID string `xorm:"notnull pk UUID 'tid'"`
Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
}
assert.NoError(t, prepareEngine())
assertSync(t, new(TaskSolution))
assert.NoError(t, testEngine.Sync2(new(TaskSolution)))
tables, err := testEngine.DBMetas()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(tables))
pkCols := tables[0].PKColumns()
assert.EqualValues(t, 2, len(pkCols))
assert.EqualValues(t, "uid", pkCols[0].Name)
assert.EqualValues(t, "tid", pkCols[1].Name)
}

View File

@ -17,7 +17,7 @@ import (
const (
// Version show the xorm's version
Version string = "0.6.2.0517"
Version string = "0.6.2.0605"
)
func regDrvsNDialects() bool {