瀏覽代碼

fix bug and add set conn max lifetime

xormplus 8 年之前
父節點
當前提交
9763976a69
共有 4 個文件被更改,包括 77 次插入6 次删除
  1. 13 4
      dialect_sqlite3.go
  2. 14 0
      engine_maxlife.go
  3. 49 1
      session_pk_test.go
  4. 1 1
      xorm.go

+ 13 - 4
dialect_sqlite3.go

@@ -14,10 +14,6 @@ import (
 	"github.com/xormplus/core"
 )
 
-// func init() {
-// 	RegisterDialect("sqlite3", &sqlite3{})
-// }
-
 var (
 	sqlite3ReservedWords = map[string]bool{
 		"ABORT":             true,
@@ -310,6 +306,19 @@ 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)

+ 14 - 0
engine_maxlife.go

@@ -0,0 +1,14 @@
+// Copyright 2017 The Xorm Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build go1.6
+
+package xorm
+
+import "time"
+
+// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
+func (engine *Engine) SetConnMaxLifetime(d time.Duration) {
+	engine.db.SetConnMaxLifetime(d)
+}

+ 49 - 1
session_pk_test.go

@@ -7,9 +7,10 @@ package xorm
 import (
 	"errors"
 	"testing"
+	"time"
 
-	"github.com/go-xorm/core"
 	"github.com/stretchr/testify/assert"
+	"github.com/xormplus/core"
 )
 
 type IntId struct {
@@ -1117,3 +1118,50 @@ 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)
+}
+
+func TestNoPKIdQueryUpdate(t *testing.T) {
+	type NoPKTable struct {
+		Username string
+	}
+
+	assert.NoError(t, prepareEngine())
+	assertSync(t, new(NoPKTable))
+
+	cnt, err := testEngine.Insert(&NoPKTable{
+		Username: "test",
+	})
+	assert.NoError(t, err)
+	assert.EqualValues(t, 1, cnt)
+
+	var res NoPKTable
+	has, err := testEngine.ID("test").Get(&res)
+	assert.Error(t, err)
+	assert.False(t, has)
+
+	cnt, err = testEngine.ID("test").Update(&NoPKTable{
+		Username: "test1",
+	})
+	assert.Error(t, err)
+	assert.EqualValues(t, 0, cnt)
+}

+ 1 - 1
xorm.go

@@ -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 {