replace tab to 4 spaces for all codes
This commit is contained in:
parent
e84e14f972
commit
c70b4ad8d3
5514
base_test.go
5514
base_test.go
File diff suppressed because it is too large
Load Diff
|
|
@ -1,180 +1,180 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
"database/sql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type BigStruct struct {
|
||||
Id int64
|
||||
Name string
|
||||
Title string
|
||||
Age string
|
||||
Alias string
|
||||
NickName string
|
||||
Id int64
|
||||
Name string
|
||||
Title string
|
||||
Age string
|
||||
Alias string
|
||||
NickName string
|
||||
}
|
||||
|
||||
func doBenchDriverInsert(engine *Engine, db *sql.DB, b *testing.B) {
|
||||
b.StopTimer()
|
||||
err := engine.CreateTables(&BigStruct{})
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
b.StopTimer()
|
||||
err := engine.CreateTables(&BigStruct{})
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
doBenchDriverInsertS(db, b)
|
||||
doBenchDriverInsertS(db, b)
|
||||
|
||||
err = engine.DropTables(&BigStruct{})
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
err = engine.DropTables(&BigStruct{})
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func doBenchDriverInsertS(db *sql.DB, b *testing.B) {
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := db.Exec(`insert into big_struct (name, title, age, alias, nick_name)
|
||||
values ('fafdasf', 'fadfa', 'afadfsaf', 'fadfafdsafd', 'fadfafdsaf')`)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := db.Exec(`insert into big_struct (name, title, age, alias, nick_name)
|
||||
values ('fafdasf', 'fadfa', 'afadfsaf', 'fadfafdsafd', 'fadfafdsaf')`)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
}
|
||||
|
||||
func doBenchDriverFind(engine *Engine, db *sql.DB, b *testing.B) {
|
||||
b.StopTimer()
|
||||
err := engine.CreateTables(&BigStruct{})
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
b.StopTimer()
|
||||
err := engine.CreateTables(&BigStruct{})
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
doBenchDriverFindS(db, b)
|
||||
doBenchDriverFindS(db, b)
|
||||
|
||||
err = engine.DropTables(&BigStruct{})
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
err = engine.DropTables(&BigStruct{})
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func doBenchDriverFindS(db *sql.DB, b *testing.B) {
|
||||
b.StopTimer()
|
||||
for i := 0; i < 100; i++ {
|
||||
_, err := db.Exec(`insert into big_struct (name, title, age, alias, nick_name)
|
||||
values ('fafdasf', 'fadfa', 'afadfsaf', 'fadfafdsafd', 'fadfafdsaf')`)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
for i := 0; i < 100; i++ {
|
||||
_, err := db.Exec(`insert into big_struct (name, title, age, alias, nick_name)
|
||||
values ('fafdasf', 'fadfa', 'afadfsaf', 'fadfafdsafd', 'fadfafdsaf')`)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := db.Query("select * from big_struct limit 50")
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := db.Query("select * from big_struct limit 50")
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
}
|
||||
|
||||
func doBenchInsert(engine *Engine, b *testing.B) {
|
||||
b.StopTimer()
|
||||
bs := &BigStruct{0, "fafdasf", "fadfa", "afadfsaf", "fadfafdsafd", "fadfafdsaf"}
|
||||
err := engine.CreateTables(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
b.StopTimer()
|
||||
bs := &BigStruct{0, "fafdasf", "fadfa", "afadfsaf", "fadfafdsafd", "fadfafdsaf"}
|
||||
err := engine.CreateTables(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
bs.Id = 0
|
||||
_, err = engine.Insert(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
err = engine.DropTables(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
bs.Id = 0
|
||||
_, err = engine.Insert(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
err = engine.DropTables(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func doBenchFind(engine *Engine, b *testing.B) {
|
||||
b.StopTimer()
|
||||
bs := &BigStruct{0, "fafdasf", "fadfa", "afadfsaf", "fadfafdsafd", "fadfafdsaf"}
|
||||
err := engine.CreateTables(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
b.StopTimer()
|
||||
bs := &BigStruct{0, "fafdasf", "fadfa", "afadfsaf", "fadfafdsafd", "fadfafdsaf"}
|
||||
err := engine.CreateTables(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
bs.Id = 0
|
||||
_, err = engine.Insert(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
for i := 0; i < 100; i++ {
|
||||
bs.Id = 0
|
||||
_, err = engine.Insert(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
bss := new([]BigStruct)
|
||||
err = engine.Limit(50).Find(bss)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
err = engine.DropTables(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
bss := new([]BigStruct)
|
||||
err = engine.Limit(50).Find(bss)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
err = engine.DropTables(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func doBenchFindPtr(engine *Engine, b *testing.B) {
|
||||
b.StopTimer()
|
||||
bs := &BigStruct{0, "fafdasf", "fadfa", "afadfsaf", "fadfafdsafd", "fadfafdsaf"}
|
||||
err := engine.CreateTables(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
b.StopTimer()
|
||||
bs := &BigStruct{0, "fafdasf", "fadfa", "afadfsaf", "fadfafdsafd", "fadfafdsaf"}
|
||||
err := engine.CreateTables(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
bs.Id = 0
|
||||
_, err = engine.Insert(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
for i := 0; i < 100; i++ {
|
||||
bs.Id = 0
|
||||
_, err = engine.Insert(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
bss := new([]*BigStruct)
|
||||
err = engine.Limit(50).Find(bss)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
err = engine.DropTables(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
b.StartTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
bss := new([]*BigStruct)
|
||||
err = engine.Limit(50).Find(bss)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
err = engine.DropTables(bs)
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
530
cache.go
530
cache.go
|
|
@ -1,395 +1,395 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"container/list"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// default cache expired time
|
||||
CacheExpired = 60 * time.Minute
|
||||
// not use now
|
||||
CacheMaxMemory = 256
|
||||
// evey ten minutes to clear all expired nodes
|
||||
CacheGcInterval = 10 * time.Minute
|
||||
// each time when gc to removed max nodes
|
||||
CacheGcMaxRemoved = 20
|
||||
// default cache expired time
|
||||
CacheExpired = 60 * time.Minute
|
||||
// not use now
|
||||
CacheMaxMemory = 256
|
||||
// evey ten minutes to clear all expired nodes
|
||||
CacheGcInterval = 10 * time.Minute
|
||||
// each time when gc to removed max nodes
|
||||
CacheGcMaxRemoved = 20
|
||||
)
|
||||
|
||||
// CacheStore is a interface to store cache
|
||||
type CacheStore interface {
|
||||
Put(key, value interface{}) error
|
||||
Get(key interface{}) (interface{}, error)
|
||||
Del(key interface{}) error
|
||||
Put(key, value interface{}) error
|
||||
Get(key interface{}) (interface{}, error)
|
||||
Del(key interface{}) error
|
||||
}
|
||||
|
||||
// MemoryStore implements CacheStore provide local machine
|
||||
// memory store
|
||||
type MemoryStore struct {
|
||||
store map[interface{}]interface{}
|
||||
mutex sync.RWMutex
|
||||
store map[interface{}]interface{}
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
return &MemoryStore{store: make(map[interface{}]interface{})}
|
||||
return &MemoryStore{store: make(map[interface{}]interface{})}
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Put(key, value interface{}) error {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
s.store[key] = value
|
||||
return nil
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
s.store[key] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Get(key interface{}) (interface{}, error) {
|
||||
s.mutex.RLock()
|
||||
defer s.mutex.RUnlock()
|
||||
if v, ok := s.store[key]; ok {
|
||||
return v, nil
|
||||
}
|
||||
s.mutex.RLock()
|
||||
defer s.mutex.RUnlock()
|
||||
if v, ok := s.store[key]; ok {
|
||||
return v, nil
|
||||
}
|
||||
|
||||
return nil, ErrNotExist
|
||||
return nil, ErrNotExist
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Del(key interface{}) error {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
delete(s.store, key)
|
||||
return nil
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
delete(s.store, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Cacher is an interface to provide cache
|
||||
type Cacher interface {
|
||||
GetIds(tableName, sql string) interface{}
|
||||
GetBean(tableName string, id int64) interface{}
|
||||
PutIds(tableName, sql string, ids interface{})
|
||||
PutBean(tableName string, id int64, obj interface{})
|
||||
DelIds(tableName, sql string)
|
||||
DelBean(tableName string, id int64)
|
||||
ClearIds(tableName string)
|
||||
ClearBeans(tableName string)
|
||||
GetIds(tableName, sql string) interface{}
|
||||
GetBean(tableName string, id int64) interface{}
|
||||
PutIds(tableName, sql string, ids interface{})
|
||||
PutBean(tableName string, id int64, obj interface{})
|
||||
DelIds(tableName, sql string)
|
||||
DelBean(tableName string, id int64)
|
||||
ClearIds(tableName string)
|
||||
ClearBeans(tableName string)
|
||||
}
|
||||
|
||||
type idNode struct {
|
||||
tbName string
|
||||
id int64
|
||||
lastVisit time.Time
|
||||
tbName string
|
||||
id int64
|
||||
lastVisit time.Time
|
||||
}
|
||||
|
||||
type sqlNode struct {
|
||||
tbName string
|
||||
sql string
|
||||
lastVisit time.Time
|
||||
tbName string
|
||||
sql string
|
||||
lastVisit time.Time
|
||||
}
|
||||
|
||||
func newIdNode(tbName string, id int64) *idNode {
|
||||
return &idNode{tbName, id, time.Now()}
|
||||
return &idNode{tbName, id, time.Now()}
|
||||
}
|
||||
|
||||
func newSqlNode(tbName, sql string) *sqlNode {
|
||||
return &sqlNode{tbName, sql, time.Now()}
|
||||
return &sqlNode{tbName, sql, time.Now()}
|
||||
}
|
||||
|
||||
// LRUCacher implements Cacher according to LRU algorithm
|
||||
type LRUCacher struct {
|
||||
idList *list.List
|
||||
sqlList *list.List
|
||||
idIndex map[string]map[interface{}]*list.Element
|
||||
sqlIndex map[string]map[interface{}]*list.Element
|
||||
store CacheStore
|
||||
Max int
|
||||
mutex sync.Mutex
|
||||
Expired time.Duration
|
||||
maxSize int
|
||||
GcInterval time.Duration
|
||||
idList *list.List
|
||||
sqlList *list.List
|
||||
idIndex map[string]map[interface{}]*list.Element
|
||||
sqlIndex map[string]map[interface{}]*list.Element
|
||||
store CacheStore
|
||||
Max int
|
||||
mutex sync.Mutex
|
||||
Expired time.Duration
|
||||
maxSize int
|
||||
GcInterval time.Duration
|
||||
}
|
||||
|
||||
func newLRUCacher(store CacheStore, expired time.Duration, maxSize int, max int) *LRUCacher {
|
||||
cacher := &LRUCacher{store: store, idList: list.New(),
|
||||
sqlList: list.New(), Expired: expired, maxSize: maxSize,
|
||||
GcInterval: CacheGcInterval, Max: max,
|
||||
sqlIndex: make(map[string]map[interface{}]*list.Element),
|
||||
idIndex: make(map[string]map[interface{}]*list.Element),
|
||||
}
|
||||
cacher.RunGC()
|
||||
return cacher
|
||||
cacher := &LRUCacher{store: store, idList: list.New(),
|
||||
sqlList: list.New(), Expired: expired, maxSize: maxSize,
|
||||
GcInterval: CacheGcInterval, Max: max,
|
||||
sqlIndex: make(map[string]map[interface{}]*list.Element),
|
||||
idIndex: make(map[string]map[interface{}]*list.Element),
|
||||
}
|
||||
cacher.RunGC()
|
||||
return cacher
|
||||
}
|
||||
|
||||
func NewLRUCacher(store CacheStore, max int) *LRUCacher {
|
||||
return newLRUCacher(store, CacheExpired, CacheMaxMemory, max)
|
||||
return newLRUCacher(store, CacheExpired, CacheMaxMemory, max)
|
||||
}
|
||||
|
||||
func NewLRUCacher2(store CacheStore, expired time.Duration, max int) *LRUCacher {
|
||||
return newLRUCacher(store, expired, 0, max)
|
||||
return newLRUCacher(store, expired, 0, max)
|
||||
}
|
||||
|
||||
//func NewLRUCacher3(store CacheStore, expired time.Duration, maxSize int) *LRUCacher {
|
||||
// return newLRUCacher(store, expired, maxSize, 0)
|
||||
// return newLRUCacher(store, expired, maxSize, 0)
|
||||
//}
|
||||
|
||||
// RunGC run once every m.GcInterval
|
||||
func (m *LRUCacher) RunGC() {
|
||||
time.AfterFunc(m.GcInterval, func() {
|
||||
m.RunGC()
|
||||
m.GC()
|
||||
})
|
||||
time.AfterFunc(m.GcInterval, func() {
|
||||
m.RunGC()
|
||||
m.GC()
|
||||
})
|
||||
}
|
||||
|
||||
// GC check ids lit and sql list to remove all element expired
|
||||
func (m *LRUCacher) GC() {
|
||||
//fmt.Println("begin gc ...")
|
||||
//defer fmt.Println("end gc ...")
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
var removedNum int
|
||||
for e := m.idList.Front(); e != nil; {
|
||||
if removedNum <= CacheGcMaxRemoved &&
|
||||
time.Now().Sub(e.Value.(*idNode).lastVisit) > m.Expired {
|
||||
removedNum++
|
||||
next := e.Next()
|
||||
//fmt.Println("removing ...", e.Value)
|
||||
node := e.Value.(*idNode)
|
||||
m.delBean(node.tbName, node.id)
|
||||
e = next
|
||||
} else {
|
||||
//fmt.Printf("removing %d cache nodes ..., left %d\n", removedNum, m.idList.Len())
|
||||
break
|
||||
}
|
||||
}
|
||||
//fmt.Println("begin gc ...")
|
||||
//defer fmt.Println("end gc ...")
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
var removedNum int
|
||||
for e := m.idList.Front(); e != nil; {
|
||||
if removedNum <= CacheGcMaxRemoved &&
|
||||
time.Now().Sub(e.Value.(*idNode).lastVisit) > m.Expired {
|
||||
removedNum++
|
||||
next := e.Next()
|
||||
//fmt.Println("removing ...", e.Value)
|
||||
node := e.Value.(*idNode)
|
||||
m.delBean(node.tbName, node.id)
|
||||
e = next
|
||||
} else {
|
||||
//fmt.Printf("removing %d cache nodes ..., left %d\n", removedNum, m.idList.Len())
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
removedNum = 0
|
||||
for e := m.sqlList.Front(); e != nil; {
|
||||
if removedNum <= CacheGcMaxRemoved &&
|
||||
time.Now().Sub(e.Value.(*sqlNode).lastVisit) > m.Expired {
|
||||
removedNum++
|
||||
next := e.Next()
|
||||
//fmt.Println("removing ...", e.Value)
|
||||
node := e.Value.(*sqlNode)
|
||||
m.delIds(node.tbName, node.sql)
|
||||
e = next
|
||||
} else {
|
||||
//fmt.Printf("removing %d cache nodes ..., left %d\n", removedNum, m.sqlList.Len())
|
||||
break
|
||||
}
|
||||
}
|
||||
removedNum = 0
|
||||
for e := m.sqlList.Front(); e != nil; {
|
||||
if removedNum <= CacheGcMaxRemoved &&
|
||||
time.Now().Sub(e.Value.(*sqlNode).lastVisit) > m.Expired {
|
||||
removedNum++
|
||||
next := e.Next()
|
||||
//fmt.Println("removing ...", e.Value)
|
||||
node := e.Value.(*sqlNode)
|
||||
m.delIds(node.tbName, node.sql)
|
||||
e = next
|
||||
} else {
|
||||
//fmt.Printf("removing %d cache nodes ..., left %d\n", removedNum, m.sqlList.Len())
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get all bean's ids according to sql and parameter from cache
|
||||
func (m *LRUCacher) GetIds(tableName, sql string) interface{} {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
if _, ok := m.sqlIndex[tableName]; !ok {
|
||||
m.sqlIndex[tableName] = make(map[interface{}]*list.Element)
|
||||
}
|
||||
if v, err := m.store.Get(sql); err == nil {
|
||||
if el, ok := m.sqlIndex[tableName][sql]; !ok {
|
||||
el = m.sqlList.PushBack(newSqlNode(tableName, sql))
|
||||
m.sqlIndex[tableName][sql] = el
|
||||
} else {
|
||||
lastTime := el.Value.(*sqlNode).lastVisit
|
||||
// if expired, remove the node and return nil
|
||||
if time.Now().Sub(lastTime) > m.Expired {
|
||||
m.delIds(tableName, sql)
|
||||
return nil
|
||||
}
|
||||
m.sqlList.MoveToBack(el)
|
||||
el.Value.(*sqlNode).lastVisit = time.Now()
|
||||
}
|
||||
return v
|
||||
} else {
|
||||
m.delIds(tableName, sql)
|
||||
}
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
if _, ok := m.sqlIndex[tableName]; !ok {
|
||||
m.sqlIndex[tableName] = make(map[interface{}]*list.Element)
|
||||
}
|
||||
if v, err := m.store.Get(sql); err == nil {
|
||||
if el, ok := m.sqlIndex[tableName][sql]; !ok {
|
||||
el = m.sqlList.PushBack(newSqlNode(tableName, sql))
|
||||
m.sqlIndex[tableName][sql] = el
|
||||
} else {
|
||||
lastTime := el.Value.(*sqlNode).lastVisit
|
||||
// if expired, remove the node and return nil
|
||||
if time.Now().Sub(lastTime) > m.Expired {
|
||||
m.delIds(tableName, sql)
|
||||
return nil
|
||||
}
|
||||
m.sqlList.MoveToBack(el)
|
||||
el.Value.(*sqlNode).lastVisit = time.Now()
|
||||
}
|
||||
return v
|
||||
} else {
|
||||
m.delIds(tableName, sql)
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get bean according tableName and id from cache
|
||||
func (m *LRUCacher) GetBean(tableName string, id int64) interface{} {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
if _, ok := m.idIndex[tableName]; !ok {
|
||||
m.idIndex[tableName] = make(map[interface{}]*list.Element)
|
||||
}
|
||||
tid := genId(tableName, id)
|
||||
if v, err := m.store.Get(tid); err == nil {
|
||||
if el, ok := m.idIndex[tableName][id]; ok {
|
||||
lastTime := el.Value.(*idNode).lastVisit
|
||||
// if expired, remove the node and return nil
|
||||
if time.Now().Sub(lastTime) > m.Expired {
|
||||
m.delBean(tableName, id)
|
||||
//m.clearIds(tableName)
|
||||
return nil
|
||||
}
|
||||
m.idList.MoveToBack(el)
|
||||
el.Value.(*idNode).lastVisit = time.Now()
|
||||
} else {
|
||||
el = m.idList.PushBack(newIdNode(tableName, id))
|
||||
m.idIndex[tableName][id] = el
|
||||
}
|
||||
return v
|
||||
} else {
|
||||
// store bean is not exist, then remove memory's index
|
||||
m.delBean(tableName, id)
|
||||
//m.clearIds(tableName)
|
||||
return nil
|
||||
}
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
if _, ok := m.idIndex[tableName]; !ok {
|
||||
m.idIndex[tableName] = make(map[interface{}]*list.Element)
|
||||
}
|
||||
tid := genId(tableName, id)
|
||||
if v, err := m.store.Get(tid); err == nil {
|
||||
if el, ok := m.idIndex[tableName][id]; ok {
|
||||
lastTime := el.Value.(*idNode).lastVisit
|
||||
// if expired, remove the node and return nil
|
||||
if time.Now().Sub(lastTime) > m.Expired {
|
||||
m.delBean(tableName, id)
|
||||
//m.clearIds(tableName)
|
||||
return nil
|
||||
}
|
||||
m.idList.MoveToBack(el)
|
||||
el.Value.(*idNode).lastVisit = time.Now()
|
||||
} else {
|
||||
el = m.idList.PushBack(newIdNode(tableName, id))
|
||||
m.idIndex[tableName][id] = el
|
||||
}
|
||||
return v
|
||||
} else {
|
||||
// store bean is not exist, then remove memory's index
|
||||
m.delBean(tableName, id)
|
||||
//m.clearIds(tableName)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Clear all sql-ids mapping on table tableName from cache
|
||||
func (m *LRUCacher) clearIds(tableName string) {
|
||||
if tis, ok := m.sqlIndex[tableName]; ok {
|
||||
for sql, v := range tis {
|
||||
m.sqlList.Remove(v)
|
||||
m.store.Del(sql)
|
||||
}
|
||||
}
|
||||
m.sqlIndex[tableName] = make(map[interface{}]*list.Element)
|
||||
if tis, ok := m.sqlIndex[tableName]; ok {
|
||||
for sql, v := range tis {
|
||||
m.sqlList.Remove(v)
|
||||
m.store.Del(sql)
|
||||
}
|
||||
}
|
||||
m.sqlIndex[tableName] = make(map[interface{}]*list.Element)
|
||||
}
|
||||
|
||||
func (m *LRUCacher) ClearIds(tableName string) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.clearIds(tableName)
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.clearIds(tableName)
|
||||
}
|
||||
|
||||
func (m *LRUCacher) clearBeans(tableName string) {
|
||||
if tis, ok := m.idIndex[tableName]; ok {
|
||||
for id, v := range tis {
|
||||
m.idList.Remove(v)
|
||||
tid := genId(tableName, id.(int64))
|
||||
m.store.Del(tid)
|
||||
}
|
||||
}
|
||||
m.idIndex[tableName] = make(map[interface{}]*list.Element)
|
||||
if tis, ok := m.idIndex[tableName]; ok {
|
||||
for id, v := range tis {
|
||||
m.idList.Remove(v)
|
||||
tid := genId(tableName, id.(int64))
|
||||
m.store.Del(tid)
|
||||
}
|
||||
}
|
||||
m.idIndex[tableName] = make(map[interface{}]*list.Element)
|
||||
}
|
||||
|
||||
func (m *LRUCacher) ClearBeans(tableName string) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.clearBeans(tableName)
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.clearBeans(tableName)
|
||||
}
|
||||
|
||||
func (m *LRUCacher) PutIds(tableName, sql string, ids interface{}) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
if _, ok := m.sqlIndex[tableName]; !ok {
|
||||
m.sqlIndex[tableName] = make(map[interface{}]*list.Element)
|
||||
}
|
||||
if el, ok := m.sqlIndex[tableName][sql]; !ok {
|
||||
el = m.sqlList.PushBack(newSqlNode(tableName, sql))
|
||||
m.sqlIndex[tableName][sql] = el
|
||||
} else {
|
||||
el.Value.(*sqlNode).lastVisit = time.Now()
|
||||
}
|
||||
m.store.Put(sql, ids)
|
||||
if m.sqlList.Len() > m.Max {
|
||||
e := m.sqlList.Front()
|
||||
node := e.Value.(*sqlNode)
|
||||
m.delIds(node.tbName, node.sql)
|
||||
}
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
if _, ok := m.sqlIndex[tableName]; !ok {
|
||||
m.sqlIndex[tableName] = make(map[interface{}]*list.Element)
|
||||
}
|
||||
if el, ok := m.sqlIndex[tableName][sql]; !ok {
|
||||
el = m.sqlList.PushBack(newSqlNode(tableName, sql))
|
||||
m.sqlIndex[tableName][sql] = el
|
||||
} else {
|
||||
el.Value.(*sqlNode).lastVisit = time.Now()
|
||||
}
|
||||
m.store.Put(sql, ids)
|
||||
if m.sqlList.Len() > m.Max {
|
||||
e := m.sqlList.Front()
|
||||
node := e.Value.(*sqlNode)
|
||||
m.delIds(node.tbName, node.sql)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *LRUCacher) PutBean(tableName string, id int64, obj interface{}) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
var el *list.Element
|
||||
var ok bool
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
var el *list.Element
|
||||
var ok bool
|
||||
|
||||
if el, ok = m.idIndex[tableName][id]; !ok {
|
||||
el = m.idList.PushBack(newIdNode(tableName, id))
|
||||
m.idIndex[tableName][id] = el
|
||||
} else {
|
||||
el.Value.(*idNode).lastVisit = time.Now()
|
||||
}
|
||||
if el, ok = m.idIndex[tableName][id]; !ok {
|
||||
el = m.idList.PushBack(newIdNode(tableName, id))
|
||||
m.idIndex[tableName][id] = el
|
||||
} else {
|
||||
el.Value.(*idNode).lastVisit = time.Now()
|
||||
}
|
||||
|
||||
m.store.Put(genId(tableName, id), obj)
|
||||
if m.idList.Len() > m.Max {
|
||||
e := m.idList.Front()
|
||||
node := e.Value.(*idNode)
|
||||
m.delBean(node.tbName, node.id)
|
||||
}
|
||||
m.store.Put(genId(tableName, id), obj)
|
||||
if m.idList.Len() > m.Max {
|
||||
e := m.idList.Front()
|
||||
node := e.Value.(*idNode)
|
||||
m.delBean(node.tbName, node.id)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *LRUCacher) delIds(tableName, sql string) {
|
||||
if _, ok := m.sqlIndex[tableName]; ok {
|
||||
if el, ok := m.sqlIndex[tableName][sql]; ok {
|
||||
delete(m.sqlIndex[tableName], sql)
|
||||
m.sqlList.Remove(el)
|
||||
}
|
||||
}
|
||||
m.store.Del(sql)
|
||||
if _, ok := m.sqlIndex[tableName]; ok {
|
||||
if el, ok := m.sqlIndex[tableName][sql]; ok {
|
||||
delete(m.sqlIndex[tableName], sql)
|
||||
m.sqlList.Remove(el)
|
||||
}
|
||||
}
|
||||
m.store.Del(sql)
|
||||
}
|
||||
|
||||
func (m *LRUCacher) DelIds(tableName, sql string) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.delIds(tableName, sql)
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.delIds(tableName, sql)
|
||||
}
|
||||
|
||||
func (m *LRUCacher) delBean(tableName string, id int64) {
|
||||
tid := genId(tableName, id)
|
||||
if el, ok := m.idIndex[tableName][id]; ok {
|
||||
delete(m.idIndex[tableName], id)
|
||||
m.idList.Remove(el)
|
||||
m.clearIds(tableName)
|
||||
}
|
||||
m.store.Del(tid)
|
||||
tid := genId(tableName, id)
|
||||
if el, ok := m.idIndex[tableName][id]; ok {
|
||||
delete(m.idIndex[tableName], id)
|
||||
m.idList.Remove(el)
|
||||
m.clearIds(tableName)
|
||||
}
|
||||
m.store.Del(tid)
|
||||
}
|
||||
|
||||
func (m *LRUCacher) DelBean(tableName string, id int64) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.delBean(tableName, id)
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.delBean(tableName, id)
|
||||
}
|
||||
|
||||
func encodeIds(ids []int64) (s string) {
|
||||
s = "["
|
||||
for _, id := range ids {
|
||||
s += fmt.Sprintf("%v,", id)
|
||||
}
|
||||
s = s[:len(s)-1] + "]"
|
||||
return
|
||||
s = "["
|
||||
for _, id := range ids {
|
||||
s += fmt.Sprintf("%v,", id)
|
||||
}
|
||||
s = s[:len(s)-1] + "]"
|
||||
return
|
||||
}
|
||||
|
||||
func decodeIds(s string) []int64 {
|
||||
res := make([]int64, 0)
|
||||
if len(s) >= 2 {
|
||||
ss := strings.Split(s[1:len(s)-1], ",")
|
||||
for _, s := range ss {
|
||||
i, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
return res
|
||||
}
|
||||
res = append(res, i)
|
||||
}
|
||||
}
|
||||
return res
|
||||
res := make([]int64, 0)
|
||||
if len(s) >= 2 {
|
||||
ss := strings.Split(s[1:len(s)-1], ",")
|
||||
for _, s := range ss {
|
||||
i, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
return res
|
||||
}
|
||||
res = append(res, i)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func getCacheSql(m Cacher, tableName, sql string, args interface{}) ([]int64, error) {
|
||||
bytes := m.GetIds(tableName, genSqlKey(sql, args))
|
||||
if bytes == nil {
|
||||
return nil, errors.New("Not Exist")
|
||||
}
|
||||
objs := decodeIds(bytes.(string))
|
||||
return objs, nil
|
||||
bytes := m.GetIds(tableName, genSqlKey(sql, args))
|
||||
if bytes == nil {
|
||||
return nil, errors.New("Not Exist")
|
||||
}
|
||||
objs := decodeIds(bytes.(string))
|
||||
return objs, nil
|
||||
}
|
||||
|
||||
func putCacheSql(m Cacher, ids []int64, tableName, sql string, args interface{}) error {
|
||||
bytes := encodeIds(ids)
|
||||
m.PutIds(tableName, genSqlKey(sql, args), bytes)
|
||||
return nil
|
||||
bytes := encodeIds(ids)
|
||||
m.PutIds(tableName, genSqlKey(sql, args), bytes)
|
||||
return nil
|
||||
}
|
||||
|
||||
func genSqlKey(sql string, args interface{}) string {
|
||||
return fmt.Sprintf("%v-%v", sql, args)
|
||||
return fmt.Sprintf("%v-%v", sql, args)
|
||||
}
|
||||
|
||||
func genId(prefix string, id int64) string {
|
||||
return fmt.Sprintf("%v-%v", prefix, id)
|
||||
return fmt.Sprintf("%v-%v", prefix, id)
|
||||
}
|
||||
|
|
|
|||
102
doc.go
102
doc.go
|
|
@ -9,13 +9,13 @@ Installation
|
|||
|
||||
Make sure you have installed Go 1.1+ and then:
|
||||
|
||||
go get github.com/lunny/xorm
|
||||
go get github.com/lunny/xorm
|
||||
|
||||
Create Engine
|
||||
|
||||
Firstly, we should new an engine for a database
|
||||
|
||||
engine, err := xorm.NewEngine(driverName, dataSourceName)
|
||||
engine, err := xorm.NewEngine(driverName, dataSourceName)
|
||||
|
||||
Method NewEngine's parameters is the same as sql.Open. It depends
|
||||
drivers' implementation.
|
||||
|
|
@ -27,11 +27,11 @@ Xorm also support raw sql execution:
|
|||
|
||||
1. query sql, the returned results is []map[string][]byte
|
||||
|
||||
results, err := engine.Query("select * from user")
|
||||
results, err := engine.Query("select * from user")
|
||||
|
||||
2. exec sql, the returned results
|
||||
|
||||
affected, err := engine.Exec("update user set .... where ...")
|
||||
affected, err := engine.Exec("update user set .... where ...")
|
||||
|
||||
ORM Methods
|
||||
|
||||
|
|
@ -39,46 +39,46 @@ There are 7 major ORM methods and many helpful methods to use to operate databas
|
|||
|
||||
1. Insert one or multipe records to database
|
||||
|
||||
affected, err := engine.Insert(&struct)
|
||||
// INSERT INTO struct () values ()
|
||||
affected, err := engine.Insert(&struct1, &struct2)
|
||||
// INSERT INTO struct1 () values ()
|
||||
// INSERT INTO struct2 () values ()
|
||||
affected, err := engine.Insert(&sliceOfStruct)
|
||||
// INSERT INTO struct () values (),(),()
|
||||
affected, err := engine.Insert(&struct1, &sliceOfStruct2)
|
||||
// INSERT INTO struct1 () values ()
|
||||
// INSERT INTO struct2 () values (),(),()
|
||||
affected, err := engine.Insert(&struct)
|
||||
// INSERT INTO struct () values ()
|
||||
affected, err := engine.Insert(&struct1, &struct2)
|
||||
// INSERT INTO struct1 () values ()
|
||||
// INSERT INTO struct2 () values ()
|
||||
affected, err := engine.Insert(&sliceOfStruct)
|
||||
// INSERT INTO struct () values (),(),()
|
||||
affected, err := engine.Insert(&struct1, &sliceOfStruct2)
|
||||
// INSERT INTO struct1 () values ()
|
||||
// INSERT INTO struct2 () values (),(),()
|
||||
|
||||
2. Query one record from database
|
||||
|
||||
has, err := engine.Get(&user)
|
||||
// SELECT * FROM user LIMIT 1
|
||||
has, err := engine.Get(&user)
|
||||
// SELECT * FROM user LIMIT 1
|
||||
|
||||
3. Query multiple records from database
|
||||
|
||||
err := engine.Find(...)
|
||||
// SELECT * FROM user
|
||||
err := engine.Find(...)
|
||||
// SELECT * FROM user
|
||||
|
||||
4. Query multiple records and record by record handle
|
||||
|
||||
err := engine.Iterate(...)
|
||||
// SELECT * FROM user
|
||||
err := engine.Iterate(...)
|
||||
// SELECT * FROM user
|
||||
|
||||
5. Update one or more records
|
||||
|
||||
affected, err := engine.Update(&user)
|
||||
// UPDATE user SET
|
||||
affected, err := engine.Update(&user)
|
||||
// UPDATE user SET
|
||||
|
||||
6. Delete one or more records
|
||||
|
||||
affected, err := engine.Delete(&user)
|
||||
// DELETE FROM user Where ...
|
||||
affected, err := engine.Delete(&user)
|
||||
// DELETE FROM user Where ...
|
||||
|
||||
7. Count records
|
||||
|
||||
counts, err := engine.Count(&user)
|
||||
// SELECT count(*) AS total FROM user
|
||||
counts, err := engine.Count(&user)
|
||||
// SELECT count(*) AS total FROM user
|
||||
|
||||
Conditions
|
||||
|
||||
|
|
@ -86,49 +86,49 @@ The above 7 methods could use with condition methods.
|
|||
|
||||
1. Id, In
|
||||
|
||||
engine.Id(1).Get(&user)
|
||||
// SELECT * FROM user WHERE id = 1
|
||||
engine.In("id", 1, 2, 3).Find(&users)
|
||||
// SELECT * FROM user WHERE id IN (1, 2, 3)
|
||||
engine.Id(1).Get(&user)
|
||||
// SELECT * FROM user WHERE id = 1
|
||||
engine.In("id", 1, 2, 3).Find(&users)
|
||||
// SELECT * FROM user WHERE id IN (1, 2, 3)
|
||||
|
||||
2. Where, And, Or
|
||||
|
||||
engine.Where().And().Or().Find()
|
||||
// SELECT * FROM user WHERE (.. AND ..) OR ...
|
||||
engine.Where().And().Or().Find()
|
||||
// SELECT * FROM user WHERE (.. AND ..) OR ...
|
||||
|
||||
3. OrderBy, Asc, Desc
|
||||
|
||||
engine.Asc().Desc().Find()
|
||||
// SELECT * FROM user ORDER BY .. ASC, .. DESC
|
||||
engine.OrderBy().Find()
|
||||
// SELECT * FROM user ORDER BY ..
|
||||
engine.Asc().Desc().Find()
|
||||
// SELECT * FROM user ORDER BY .. ASC, .. DESC
|
||||
engine.OrderBy().Find()
|
||||
// SELECT * FROM user ORDER BY ..
|
||||
|
||||
4. Limit, Top
|
||||
|
||||
engine.Limit().Find()
|
||||
// SELECT * FROM user LIMIT .. OFFSET ..
|
||||
engine.Top().Find()
|
||||
// SELECT * FROM user LIMIT ..
|
||||
engine.Limit().Find()
|
||||
// SELECT * FROM user LIMIT .. OFFSET ..
|
||||
engine.Top().Find()
|
||||
// SELECT * FROM user LIMIT ..
|
||||
|
||||
5. Sql
|
||||
|
||||
engine.Sql("select * from user").Find()
|
||||
engine.Sql("select * from user").Find()
|
||||
|
||||
6. Cols, Omit, Distinct
|
||||
|
||||
engine.Cols("col1, col2").Find()
|
||||
// SELECT col1, col2 FROM user
|
||||
engine.Omit("col1").Find()
|
||||
// SELECT col2, col3 FROM user
|
||||
engine.Distinct("col1").Find()
|
||||
// SELECT DISTINCT col1 FROM user
|
||||
engine.Cols("col1, col2").Find()
|
||||
// SELECT col1, col2 FROM user
|
||||
engine.Omit("col1").Find()
|
||||
// SELECT col2, col3 FROM user
|
||||
engine.Distinct("col1").Find()
|
||||
// SELECT DISTINCT col1 FROM user
|
||||
|
||||
7. Join, GroupBy, Having
|
||||
|
||||
engine.GroupBy("name").Having("name='xlw'").Find()
|
||||
//SELECT * FROM user GROUP BY name HAVING name='xlw'
|
||||
engine.Join("LEFT", "userdetail", "user.id=userdetail.id").Find()
|
||||
//SELECT * FROM user LEFT JOIN userdetail ON user.id=userdetail.id
|
||||
engine.GroupBy("name").Having("name='xlw'").Find()
|
||||
//SELECT * FROM user GROUP BY name HAVING name='xlw'
|
||||
engine.Join("LEFT", "userdetail", "user.id=userdetail.id").Find()
|
||||
//SELECT * FROM user LEFT JOIN userdetail ON user.id=userdetail.id
|
||||
|
||||
More usage, please visit https://github.com/lunny/xorm/blob/master/docs/QuickStartEn.md
|
||||
*/
|
||||
|
|
|
|||
16
error.go
16
error.go
|
|
@ -1,15 +1,15 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrParamsType error = errors.New("Params type error")
|
||||
ErrTableNotFound error = errors.New("Not found table")
|
||||
ErrUnSupportedType error = errors.New("Unsupported type error")
|
||||
ErrNotExist error = errors.New("Not exist error")
|
||||
ErrCacheFailed error = errors.New("Cache failed")
|
||||
ErrNeedDeletedCond error = errors.New("Delete need at least one condition")
|
||||
ErrNotImplemented error = errors.New("Not implemented.")
|
||||
ErrParamsType error = errors.New("Params type error")
|
||||
ErrTableNotFound error = errors.New("Not found table")
|
||||
ErrUnSupportedType error = errors.New("Unsupported type error")
|
||||
ErrNotExist error = errors.New("Not exist error")
|
||||
ErrCacheFailed error = errors.New("Cache failed")
|
||||
ErrNeedDeletedCond error = errors.New("Delete need at least one condition")
|
||||
ErrNotImplemented error = errors.New("Not implemented.")
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,109 +1,109 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
. "xorm"
|
||||
"fmt"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
. "xorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id int64
|
||||
Name string
|
||||
Id int64
|
||||
Name string
|
||||
}
|
||||
|
||||
func main() {
|
||||
f := "cache.db"
|
||||
os.Remove(f)
|
||||
f := "cache.db"
|
||||
os.Remove(f)
|
||||
|
||||
Orm, err := NewEngine("sqlite3", f)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm.ShowSQL = true
|
||||
cacher := NewLRUCacher(NewMemoryStore(), 1000)
|
||||
Orm.SetDefaultCacher(cacher)
|
||||
Orm, err := NewEngine("sqlite3", f)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm.ShowSQL = true
|
||||
cacher := NewLRUCacher(NewMemoryStore(), 1000)
|
||||
Orm.SetDefaultCacher(cacher)
|
||||
|
||||
err = Orm.CreateTables(&User{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = Orm.CreateTables(&User{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = Orm.Insert(&User{Name: "xlw"})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
_, err = Orm.Insert(&User{Name: "xlw"})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
users := make([]User, 0)
|
||||
err = Orm.Find(&users)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
users := make([]User, 0)
|
||||
err = Orm.Find(&users)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("users:", users)
|
||||
fmt.Println("users:", users)
|
||||
|
||||
users2 := make([]User, 0)
|
||||
users2 := make([]User, 0)
|
||||
|
||||
err = Orm.Find(&users2)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = Orm.Find(&users2)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("users2:", users2)
|
||||
fmt.Println("users2:", users2)
|
||||
|
||||
users3 := make([]User, 0)
|
||||
users3 := make([]User, 0)
|
||||
|
||||
err = Orm.Find(&users3)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = Orm.Find(&users3)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("users3:", users3)
|
||||
fmt.Println("users3:", users3)
|
||||
|
||||
user4 := new(User)
|
||||
has, err := Orm.Id(1).Get(user4)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
user4 := new(User)
|
||||
has, err := Orm.Id(1).Get(user4)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("user4:", has, user4)
|
||||
fmt.Println("user4:", has, user4)
|
||||
|
||||
user4.Name = "xiaolunwen"
|
||||
_, err = Orm.Id(1).Update(user4)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("user4:", user4)
|
||||
user4.Name = "xiaolunwen"
|
||||
_, err = Orm.Id(1).Update(user4)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("user4:", user4)
|
||||
|
||||
user5 := new(User)
|
||||
has, err = Orm.Id(1).Get(user5)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("user5:", has, user5)
|
||||
user5 := new(User)
|
||||
has, err = Orm.Id(1).Get(user5)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("user5:", has, user5)
|
||||
|
||||
_, err = Orm.Id(1).Delete(new(User))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
_, err = Orm.Id(1).Delete(new(User))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
user6 := new(User)
|
||||
has, err = Orm.Id(1).Get(user6)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("user6:", has, user6)
|
||||
}
|
||||
for {
|
||||
user6 := new(User)
|
||||
has, err = Orm.Id(1).Get(user6)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("user6:", has, user6)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,113 +1,113 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
//xorm "github.com/lunny/xorm"
|
||||
"fmt"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
//"time"
|
||||
//"sync/atomic"
|
||||
xorm "xorm"
|
||||
//xorm "github.com/lunny/xorm"
|
||||
"fmt"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
//"time"
|
||||
//"sync/atomic"
|
||||
xorm "xorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id int64
|
||||
Name string
|
||||
Id int64
|
||||
Name string
|
||||
}
|
||||
|
||||
func sqliteEngine() (*xorm.Engine, error) {
|
||||
os.Remove("./test.db")
|
||||
return xorm.NewEngine("sqlite3", "./goroutine.db")
|
||||
os.Remove("./test.db")
|
||||
return xorm.NewEngine("sqlite3", "./goroutine.db")
|
||||
}
|
||||
|
||||
func mysqlEngine() (*xorm.Engine, error) {
|
||||
return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
|
||||
return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
|
||||
}
|
||||
|
||||
var u *User = &User{}
|
||||
|
||||
func test(engine *xorm.Engine) {
|
||||
err := engine.CreateTables(u)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err := engine.CreateTables(u)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
size := 500
|
||||
queue := make(chan int, size)
|
||||
size := 500
|
||||
queue := make(chan int, size)
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
go func(x int) {
|
||||
//x := i
|
||||
err := engine.Test()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
err = engine.Map(u)
|
||||
if err != nil {
|
||||
fmt.Println("Map user failed")
|
||||
} else {
|
||||
for j := 0; j < 10; j++ {
|
||||
if x+j < 2 {
|
||||
_, err = engine.Get(u)
|
||||
} else if x+j < 4 {
|
||||
users := make([]User, 0)
|
||||
err = engine.Find(&users)
|
||||
} else if x+j < 8 {
|
||||
_, err = engine.Count(u)
|
||||
} else if x+j < 16 {
|
||||
_, err = engine.Insert(&User{Name: "xlw"})
|
||||
} else if x+j < 32 {
|
||||
//_, err = engine.Id(1).Delete(u)
|
||||
_, err = engine.Delete(u)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
queue <- x
|
||||
return
|
||||
}
|
||||
}
|
||||
fmt.Printf("%v success!\n", x)
|
||||
}
|
||||
}
|
||||
queue <- x
|
||||
}(i)
|
||||
}
|
||||
for i := 0; i < size; i++ {
|
||||
go func(x int) {
|
||||
//x := i
|
||||
err := engine.Test()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
err = engine.Map(u)
|
||||
if err != nil {
|
||||
fmt.Println("Map user failed")
|
||||
} else {
|
||||
for j := 0; j < 10; j++ {
|
||||
if x+j < 2 {
|
||||
_, err = engine.Get(u)
|
||||
} else if x+j < 4 {
|
||||
users := make([]User, 0)
|
||||
err = engine.Find(&users)
|
||||
} else if x+j < 8 {
|
||||
_, err = engine.Count(u)
|
||||
} else if x+j < 16 {
|
||||
_, err = engine.Insert(&User{Name: "xlw"})
|
||||
} else if x+j < 32 {
|
||||
//_, err = engine.Id(1).Delete(u)
|
||||
_, err = engine.Delete(u)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
queue <- x
|
||||
return
|
||||
}
|
||||
}
|
||||
fmt.Printf("%v success!\n", x)
|
||||
}
|
||||
}
|
||||
queue <- x
|
||||
}(i)
|
||||
}
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
<-queue
|
||||
}
|
||||
for i := 0; i < size; i++ {
|
||||
<-queue
|
||||
}
|
||||
|
||||
//conns := atomic.LoadInt32(&xorm.ConnectionNum)
|
||||
//fmt.Println("connection number:", conns)
|
||||
fmt.Println("end")
|
||||
//conns := atomic.LoadInt32(&xorm.ConnectionNum)
|
||||
//fmt.Println("connection number:", conns)
|
||||
fmt.Println("end")
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("-----start sqlite go routines-----")
|
||||
engine, err := sqliteEngine()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
engine.ShowSQL = true
|
||||
cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
|
||||
engine.SetDefaultCacher(cacher)
|
||||
fmt.Println(engine)
|
||||
test(engine)
|
||||
fmt.Println("test end")
|
||||
engine.Close()
|
||||
fmt.Println("-----start sqlite go routines-----")
|
||||
engine, err := sqliteEngine()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
engine.ShowSQL = true
|
||||
cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
|
||||
engine.SetDefaultCacher(cacher)
|
||||
fmt.Println(engine)
|
||||
test(engine)
|
||||
fmt.Println("test end")
|
||||
engine.Close()
|
||||
|
||||
fmt.Println("-----start mysql go routines-----")
|
||||
engine, err = mysqlEngine()
|
||||
engine.ShowSQL = true
|
||||
cacher = xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
|
||||
engine.SetDefaultCacher(cacher)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
test(engine)
|
||||
fmt.Println("-----start mysql go routines-----")
|
||||
engine, err = mysqlEngine()
|
||||
engine.ShowSQL = true
|
||||
cacher = xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
|
||||
engine.SetDefaultCacher(cacher)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
test(engine)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,76 +1,76 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
. "xorm"
|
||||
"errors"
|
||||
"fmt"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
. "xorm"
|
||||
)
|
||||
|
||||
type Status struct {
|
||||
Name string
|
||||
Color string
|
||||
Name string
|
||||
Color string
|
||||
}
|
||||
|
||||
var (
|
||||
Registed Status = Status{"Registed", "white"}
|
||||
Approved Status = Status{"Approved", "green"}
|
||||
Removed Status = Status{"Removed", "red"}
|
||||
Statuses map[string]Status = map[string]Status{
|
||||
Registed.Name: Registed,
|
||||
Approved.Name: Approved,
|
||||
Removed.Name: Removed,
|
||||
}
|
||||
Registed Status = Status{"Registed", "white"}
|
||||
Approved Status = Status{"Approved", "green"}
|
||||
Removed Status = Status{"Removed", "red"}
|
||||
Statuses map[string]Status = map[string]Status{
|
||||
Registed.Name: Registed,
|
||||
Approved.Name: Approved,
|
||||
Removed.Name: Removed,
|
||||
}
|
||||
)
|
||||
|
||||
func (s *Status) FromDB(bytes []byte) error {
|
||||
if r, ok := Statuses[string(bytes)]; ok {
|
||||
*s = r
|
||||
return nil
|
||||
} else {
|
||||
return errors.New("no this data")
|
||||
}
|
||||
if r, ok := Statuses[string(bytes)]; ok {
|
||||
*s = r
|
||||
return nil
|
||||
} else {
|
||||
return errors.New("no this data")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Status) ToDB() ([]byte, error) {
|
||||
return []byte(s.Name), nil
|
||||
return []byte(s.Name), nil
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Id int64
|
||||
Name string
|
||||
Status Status `xorm:"varchar(40)"`
|
||||
Id int64
|
||||
Name string
|
||||
Status Status `xorm:"varchar(40)"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
f := "conversion.db"
|
||||
os.Remove(f)
|
||||
f := "conversion.db"
|
||||
os.Remove(f)
|
||||
|
||||
Orm, err := NewEngine("sqlite3", f)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm.ShowSQL = true
|
||||
err = Orm.CreateTables(&User{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm, err := NewEngine("sqlite3", f)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm.ShowSQL = true
|
||||
err = Orm.CreateTables(&User{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = Orm.Insert(&User{1, "xlw", Registed})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
_, err = Orm.Insert(&User{1, "xlw", Registed})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
users := make([]User, 0)
|
||||
err = Orm.Find(&users)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
users := make([]User, 0)
|
||||
err = Orm.Find(&users)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(users)
|
||||
fmt.Println(users)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,66 +1,66 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
. "xorm"
|
||||
"fmt"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
. "xorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id int64
|
||||
Name string
|
||||
Id int64
|
||||
Name string
|
||||
}
|
||||
|
||||
type LoginInfo struct {
|
||||
Id int64
|
||||
IP string
|
||||
UserId int64
|
||||
Id int64
|
||||
IP string
|
||||
UserId int64
|
||||
}
|
||||
|
||||
type LoginInfo1 struct {
|
||||
LoginInfo `xorm:"extends"`
|
||||
UserName string
|
||||
LoginInfo `xorm:"extends"`
|
||||
UserName string
|
||||
}
|
||||
|
||||
func main() {
|
||||
f := "derive.db"
|
||||
os.Remove(f)
|
||||
f := "derive.db"
|
||||
os.Remove(f)
|
||||
|
||||
Orm, err := NewEngine("sqlite3", f)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer Orm.Close()
|
||||
Orm.ShowSQL = true
|
||||
err = Orm.CreateTables(&User{}, &LoginInfo{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm, err := NewEngine("sqlite3", f)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer Orm.Close()
|
||||
Orm.ShowSQL = true
|
||||
err = Orm.CreateTables(&User{}, &LoginInfo{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = Orm.Insert(&User{1, "xlw"}, &LoginInfo{1, "127.0.0.1", 1})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
_, err = Orm.Insert(&User{1, "xlw"}, &LoginInfo{1, "127.0.0.1", 1})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
info := LoginInfo{}
|
||||
_, err = Orm.Id(1).Get(&info)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(info)
|
||||
info := LoginInfo{}
|
||||
_, err = Orm.Id(1).Get(&info)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(info)
|
||||
|
||||
infos := make([]LoginInfo1, 0)
|
||||
err = Orm.Sql(`select *, (select name from user where id = login_info.user_id) as user_name from
|
||||
login_info limit 10`).Find(&infos)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
infos := make([]LoginInfo1, 0)
|
||||
err = Orm.Sql(`select *, (select name from user where id = login_info.user_id) as user_name from
|
||||
login_info limit 10`).Find(&infos)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(infos)
|
||||
fmt.Println(infos)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,109 +1,109 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
//xorm "github.com/lunny/xorm"
|
||||
"fmt"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
//"time"
|
||||
//"sync/atomic"
|
||||
"runtime"
|
||||
xorm "xorm"
|
||||
//xorm "github.com/lunny/xorm"
|
||||
"fmt"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
//"time"
|
||||
//"sync/atomic"
|
||||
"runtime"
|
||||
xorm "xorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id int64
|
||||
Name string
|
||||
Id int64
|
||||
Name string
|
||||
}
|
||||
|
||||
func sqliteEngine() (*xorm.Engine, error) {
|
||||
os.Remove("./test.db")
|
||||
return xorm.NewEngine("sqlite3", "./goroutine.db")
|
||||
os.Remove("./test.db")
|
||||
return xorm.NewEngine("sqlite3", "./goroutine.db")
|
||||
}
|
||||
|
||||
func mysqlEngine() (*xorm.Engine, error) {
|
||||
return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
|
||||
return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
|
||||
}
|
||||
|
||||
var u *User = &User{}
|
||||
|
||||
func test(engine *xorm.Engine) {
|
||||
err := engine.CreateTables(u)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err := engine.CreateTables(u)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
size := 500
|
||||
queue := make(chan int, size)
|
||||
size := 500
|
||||
queue := make(chan int, size)
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
go func(x int) {
|
||||
//x := i
|
||||
err := engine.Test()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
err = engine.Map(u)
|
||||
if err != nil {
|
||||
fmt.Println("Map user failed")
|
||||
} else {
|
||||
for j := 0; j < 10; j++ {
|
||||
if x+j < 2 {
|
||||
_, err = engine.Get(u)
|
||||
} else if x+j < 4 {
|
||||
users := make([]User, 0)
|
||||
err = engine.Find(&users)
|
||||
} else if x+j < 8 {
|
||||
_, err = engine.Count(u)
|
||||
} else if x+j < 16 {
|
||||
_, err = engine.Insert(&User{Name: "xlw"})
|
||||
} else if x+j < 32 {
|
||||
_, err = engine.Id(1).Delete(u)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
queue <- x
|
||||
return
|
||||
}
|
||||
}
|
||||
fmt.Printf("%v success!\n", x)
|
||||
}
|
||||
}
|
||||
queue <- x
|
||||
}(i)
|
||||
}
|
||||
for i := 0; i < size; i++ {
|
||||
go func(x int) {
|
||||
//x := i
|
||||
err := engine.Test()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
err = engine.Map(u)
|
||||
if err != nil {
|
||||
fmt.Println("Map user failed")
|
||||
} else {
|
||||
for j := 0; j < 10; j++ {
|
||||
if x+j < 2 {
|
||||
_, err = engine.Get(u)
|
||||
} else if x+j < 4 {
|
||||
users := make([]User, 0)
|
||||
err = engine.Find(&users)
|
||||
} else if x+j < 8 {
|
||||
_, err = engine.Count(u)
|
||||
} else if x+j < 16 {
|
||||
_, err = engine.Insert(&User{Name: "xlw"})
|
||||
} else if x+j < 32 {
|
||||
_, err = engine.Id(1).Delete(u)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
queue <- x
|
||||
return
|
||||
}
|
||||
}
|
||||
fmt.Printf("%v success!\n", x)
|
||||
}
|
||||
}
|
||||
queue <- x
|
||||
}(i)
|
||||
}
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
<-queue
|
||||
}
|
||||
for i := 0; i < size; i++ {
|
||||
<-queue
|
||||
}
|
||||
|
||||
//conns := atomic.LoadInt32(&xorm.ConnectionNum)
|
||||
//fmt.Println("connection number:", conns)
|
||||
fmt.Println("end")
|
||||
//conns := atomic.LoadInt32(&xorm.ConnectionNum)
|
||||
//fmt.Println("connection number:", conns)
|
||||
fmt.Println("end")
|
||||
}
|
||||
|
||||
func main() {
|
||||
runtime.GOMAXPROCS(2)
|
||||
fmt.Println("-----start sqlite go routines-----")
|
||||
engine, err := sqliteEngine()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
engine.ShowSQL = true
|
||||
fmt.Println(engine)
|
||||
test(engine)
|
||||
fmt.Println("test end")
|
||||
engine.Close()
|
||||
runtime.GOMAXPROCS(2)
|
||||
fmt.Println("-----start sqlite go routines-----")
|
||||
engine, err := sqliteEngine()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
engine.ShowSQL = true
|
||||
fmt.Println(engine)
|
||||
test(engine)
|
||||
fmt.Println("test end")
|
||||
engine.Close()
|
||||
|
||||
fmt.Println("-----start mysql go routines-----")
|
||||
engine, err = mysqlEngine()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
test(engine)
|
||||
fmt.Println("-----start mysql go routines-----")
|
||||
engine, err = mysqlEngine()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
test(engine)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,108 +1,108 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
xorm "github.com/lunny/xorm"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
//"time"
|
||||
//"sync/atomic"
|
||||
"runtime"
|
||||
//xorm "xorm"
|
||||
"fmt"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
xorm "github.com/lunny/xorm"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
//"time"
|
||||
//"sync/atomic"
|
||||
"runtime"
|
||||
//xorm "xorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id int64
|
||||
Name string
|
||||
Id int64
|
||||
Name string
|
||||
}
|
||||
|
||||
func sqliteEngine() (*xorm.Engine, error) {
|
||||
os.Remove("./test.db")
|
||||
return xorm.NewEngine("sqlite3", "./goroutine.db")
|
||||
os.Remove("./test.db")
|
||||
return xorm.NewEngine("sqlite3", "./goroutine.db")
|
||||
}
|
||||
|
||||
func mysqlEngine() (*xorm.Engine, error) {
|
||||
return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
|
||||
return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
|
||||
}
|
||||
|
||||
var u *User = &User{}
|
||||
|
||||
func test(engine *xorm.Engine) {
|
||||
err := engine.CreateTables(u)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err := engine.CreateTables(u)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
engine.ShowSQL = true
|
||||
engine.Pool.SetMaxConns(5)
|
||||
size := 1000
|
||||
queue := make(chan int, size)
|
||||
engine.ShowSQL = true
|
||||
engine.Pool.SetMaxConns(5)
|
||||
size := 1000
|
||||
queue := make(chan int, size)
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
go func(x int) {
|
||||
//x := i
|
||||
err := engine.Test()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
err = engine.Map(u)
|
||||
if err != nil {
|
||||
fmt.Println("Map user failed")
|
||||
} else {
|
||||
for j := 0; j < 10; j++ {
|
||||
if x+j < 2 {
|
||||
_, err = engine.Get(u)
|
||||
} else if x+j < 4 {
|
||||
users := make([]User, 0)
|
||||
err = engine.Find(&users)
|
||||
} else if x+j < 8 {
|
||||
_, err = engine.Count(u)
|
||||
} else if x+j < 16 {
|
||||
_, err = engine.Insert(&User{Name: "xlw"})
|
||||
} else if x+j < 32 {
|
||||
_, err = engine.Id(1).Delete(u)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
queue <- x
|
||||
return
|
||||
}
|
||||
}
|
||||
fmt.Printf("%v success!\n", x)
|
||||
}
|
||||
}
|
||||
queue <- x
|
||||
}(i)
|
||||
}
|
||||
for i := 0; i < size; i++ {
|
||||
go func(x int) {
|
||||
//x := i
|
||||
err := engine.Test()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
err = engine.Map(u)
|
||||
if err != nil {
|
||||
fmt.Println("Map user failed")
|
||||
} else {
|
||||
for j := 0; j < 10; j++ {
|
||||
if x+j < 2 {
|
||||
_, err = engine.Get(u)
|
||||
} else if x+j < 4 {
|
||||
users := make([]User, 0)
|
||||
err = engine.Find(&users)
|
||||
} else if x+j < 8 {
|
||||
_, err = engine.Count(u)
|
||||
} else if x+j < 16 {
|
||||
_, err = engine.Insert(&User{Name: "xlw"})
|
||||
} else if x+j < 32 {
|
||||
_, err = engine.Id(1).Delete(u)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
queue <- x
|
||||
return
|
||||
}
|
||||
}
|
||||
fmt.Printf("%v success!\n", x)
|
||||
}
|
||||
}
|
||||
queue <- x
|
||||
}(i)
|
||||
}
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
<-queue
|
||||
}
|
||||
for i := 0; i < size; i++ {
|
||||
<-queue
|
||||
}
|
||||
|
||||
fmt.Println("end")
|
||||
fmt.Println("end")
|
||||
}
|
||||
|
||||
func main() {
|
||||
runtime.GOMAXPROCS(2)
|
||||
fmt.Println("create engine")
|
||||
engine, err := sqliteEngine()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
engine.ShowSQL = true
|
||||
fmt.Println(engine)
|
||||
test(engine)
|
||||
fmt.Println("------------------------")
|
||||
engine.Close()
|
||||
runtime.GOMAXPROCS(2)
|
||||
fmt.Println("create engine")
|
||||
engine, err := sqliteEngine()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
engine.ShowSQL = true
|
||||
fmt.Println(engine)
|
||||
test(engine)
|
||||
fmt.Println("------------------------")
|
||||
engine.Close()
|
||||
|
||||
engine, err = mysqlEngine()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
test(engine)
|
||||
engine, err = mysqlEngine()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
test(engine)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,45 +1,45 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
. "xorm"
|
||||
"fmt"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
. "xorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id int64
|
||||
Name string
|
||||
Id int64
|
||||
Name string
|
||||
}
|
||||
|
||||
func main() {
|
||||
f := "pool.db"
|
||||
os.Remove(f)
|
||||
f := "pool.db"
|
||||
os.Remove(f)
|
||||
|
||||
Orm, err := NewEngine("sqlite3", f)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = Orm.SetPool(NewSimpleConnectPool())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm, err := NewEngine("sqlite3", f)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = Orm.SetPool(NewSimpleConnectPool())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
Orm.ShowSQL = true
|
||||
err = Orm.CreateTables(&User{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm.ShowSQL = true
|
||||
err = Orm.CreateTables(&User{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
_, err = Orm.Get(&User{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
for i := 0; i < 10; i++ {
|
||||
_, err = Orm.Get(&User{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,54 +1,54 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
. "xorm"
|
||||
"fmt"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
. "xorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id int64
|
||||
Name string
|
||||
Id int64
|
||||
Name string
|
||||
}
|
||||
|
||||
type LoginInfo struct {
|
||||
Id int64
|
||||
IP string
|
||||
UserId int64
|
||||
// timestamp should be updated by database, so only allow get from db
|
||||
TimeStamp string `xorm:"<-"`
|
||||
// assume
|
||||
Nonuse int `xorm:"->"`
|
||||
Id int64
|
||||
IP string
|
||||
UserId int64
|
||||
// timestamp should be updated by database, so only allow get from db
|
||||
TimeStamp string `xorm:"<-"`
|
||||
// assume
|
||||
Nonuse int `xorm:"->"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
f := "singleMapping.db"
|
||||
os.Remove(f)
|
||||
f := "singleMapping.db"
|
||||
os.Remove(f)
|
||||
|
||||
Orm, err := NewEngine("sqlite3", f)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm.ShowSQL = true
|
||||
err = Orm.CreateTables(&User{}, &LoginInfo{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm, err := NewEngine("sqlite3", f)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm.ShowSQL = true
|
||||
err = Orm.CreateTables(&User{}, &LoginInfo{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = Orm.Insert(&User{1, "xlw"}, &LoginInfo{1, "127.0.0.1", 1, "", 23})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
_, err = Orm.Insert(&User{1, "xlw"}, &LoginInfo{1, "127.0.0.1", 1, "", 23})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
info := LoginInfo{}
|
||||
_, err = Orm.Id(1).Get(&info)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(info)
|
||||
info := LoginInfo{}
|
||||
_, err = Orm.Id(1).Get(&info)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(info)
|
||||
}
|
||||
|
|
|
|||
124
examples/sync.go
124
examples/sync.go
|
|
@ -1,92 +1,92 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
_ "github.com/bylevel/pq"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"xorm"
|
||||
"fmt"
|
||||
_ "github.com/bylevel/pq"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"xorm"
|
||||
)
|
||||
|
||||
type SyncUser2 struct {
|
||||
Id int64
|
||||
Name string `xorm:"unique"`
|
||||
Age int `xorm:"index"`
|
||||
Title string
|
||||
Address string
|
||||
Genre string
|
||||
Area string
|
||||
Date int
|
||||
Id int64
|
||||
Name string `xorm:"unique"`
|
||||
Age int `xorm:"index"`
|
||||
Title string
|
||||
Address string
|
||||
Genre string
|
||||
Area string
|
||||
Date int
|
||||
}
|
||||
|
||||
type SyncLoginInfo2 struct {
|
||||
Id int64
|
||||
IP string `xorm:"index"`
|
||||
UserId int64
|
||||
AddedCol int
|
||||
// timestamp should be updated by database, so only allow get from db
|
||||
TimeStamp string
|
||||
// assume
|
||||
Nonuse int `xorm:"unique"`
|
||||
Newa string `xorm:"index"`
|
||||
Id int64
|
||||
IP string `xorm:"index"`
|
||||
UserId int64
|
||||
AddedCol int
|
||||
// timestamp should be updated by database, so only allow get from db
|
||||
TimeStamp string
|
||||
// assume
|
||||
Nonuse int `xorm:"unique"`
|
||||
Newa string `xorm:"index"`
|
||||
}
|
||||
|
||||
func sync(engine *xorm.Engine) error {
|
||||
return engine.Sync(&SyncLoginInfo2{}, &SyncUser2{})
|
||||
return engine.Sync(&SyncLoginInfo2{}, &SyncUser2{})
|
||||
}
|
||||
|
||||
func sqliteEngine() (*xorm.Engine, error) {
|
||||
f := "sync.db"
|
||||
//os.Remove(f)
|
||||
f := "sync.db"
|
||||
//os.Remove(f)
|
||||
|
||||
return xorm.NewEngine("sqlite3", f)
|
||||
return xorm.NewEngine("sqlite3", f)
|
||||
}
|
||||
|
||||
func mysqlEngine() (*xorm.Engine, error) {
|
||||
return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
|
||||
return xorm.NewEngine("mysql", "root:@/test?charset=utf8")
|
||||
}
|
||||
|
||||
func postgresEngine() (*xorm.Engine, error) {
|
||||
return xorm.NewEngine("postgres", "dbname=xorm_test sslmode=disable")
|
||||
return xorm.NewEngine("postgres", "dbname=xorm_test sslmode=disable")
|
||||
}
|
||||
|
||||
type engineFunc func() (*xorm.Engine, error)
|
||||
|
||||
func main() {
|
||||
//engines := []engineFunc{sqliteEngine, mysqlEngine, postgresEngine}
|
||||
//engines := []engineFunc{sqliteEngine}
|
||||
//engines := []engineFunc{mysqlEngine}
|
||||
engines := []engineFunc{postgresEngine}
|
||||
for _, enginefunc := range engines {
|
||||
Orm, err := enginefunc()
|
||||
fmt.Println("--------", Orm.DriverName, "----------")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm.ShowSQL = true
|
||||
err = sync(Orm)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
//engines := []engineFunc{sqliteEngine, mysqlEngine, postgresEngine}
|
||||
//engines := []engineFunc{sqliteEngine}
|
||||
//engines := []engineFunc{mysqlEngine}
|
||||
engines := []engineFunc{postgresEngine}
|
||||
for _, enginefunc := range engines {
|
||||
Orm, err := enginefunc()
|
||||
fmt.Println("--------", Orm.DriverName, "----------")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
Orm.ShowSQL = true
|
||||
err = sync(Orm)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
_, err = Orm.Where("id > 0").Delete(&SyncUser2{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
_, err = Orm.Where("id > 0").Delete(&SyncUser2{})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
user := &SyncUser2{
|
||||
Name: "testsdf",
|
||||
Age: 15,
|
||||
Title: "newsfds",
|
||||
Address: "fasfdsafdsaf",
|
||||
Genre: "fsafd",
|
||||
Area: "fafdsafd",
|
||||
Date: 1000,
|
||||
}
|
||||
_, err = Orm.Insert(user)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
user := &SyncUser2{
|
||||
Name: "testsdf",
|
||||
Age: 15,
|
||||
Title: "newsfds",
|
||||
Address: "fasfdsafdsaf",
|
||||
Genre: "fsafd",
|
||||
Area: "fafdsafd",
|
||||
Date: 1000,
|
||||
}
|
||||
_, err = Orm.Insert(user)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
40
filter.go
40
filter.go
|
|
@ -1,13 +1,13 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Filter is an interface to filter SQL
|
||||
type Filter interface {
|
||||
Do(sql string, session *Session) string
|
||||
Do(sql string, session *Session) string
|
||||
}
|
||||
|
||||
// PgSeqFilter filter SQL replace ?, ? ... to $1, $2 ...
|
||||
|
|
@ -15,16 +15,16 @@ type PgSeqFilter struct {
|
|||
}
|
||||
|
||||
func (s *PgSeqFilter) Do(sql string, session *Session) string {
|
||||
segs := strings.Split(sql, "?")
|
||||
size := len(segs)
|
||||
res := ""
|
||||
for i, c := range segs {
|
||||
if i < size-1 {
|
||||
res += c + fmt.Sprintf("$%v", i+1)
|
||||
}
|
||||
}
|
||||
res += segs[size-1]
|
||||
return res
|
||||
segs := strings.Split(sql, "?")
|
||||
size := len(segs)
|
||||
res := ""
|
||||
for i, c := range segs {
|
||||
if i < size-1 {
|
||||
res += c + fmt.Sprintf("$%v", i+1)
|
||||
}
|
||||
}
|
||||
res += segs[size-1]
|
||||
return res
|
||||
}
|
||||
|
||||
// QuoteFilter filter SQL replace ` to database's own quote character
|
||||
|
|
@ -32,7 +32,7 @@ type QuoteFilter struct {
|
|||
}
|
||||
|
||||
func (s *QuoteFilter) Do(sql string, session *Session) string {
|
||||
return strings.Replace(sql, "`", session.Engine.QuoteStr(), -1)
|
||||
return strings.Replace(sql, "`", session.Engine.QuoteStr(), -1)
|
||||
}
|
||||
|
||||
// IdFilter filter SQL replace (id) to primary key column name
|
||||
|
|
@ -40,10 +40,10 @@ type IdFilter struct {
|
|||
}
|
||||
|
||||
func (i *IdFilter) Do(sql string, session *Session) string {
|
||||
if session.Statement.RefTable != nil && session.Statement.RefTable.PrimaryKey != "" {
|
||||
sql = strings.Replace(sql, "`(id)`", session.Engine.Quote(session.Statement.RefTable.PrimaryKey), -1)
|
||||
sql = strings.Replace(sql, session.Engine.Quote("(id)"), session.Engine.Quote(session.Statement.RefTable.PrimaryKey), -1)
|
||||
return strings.Replace(sql, "(id)", session.Engine.Quote(session.Statement.RefTable.PrimaryKey), -1)
|
||||
}
|
||||
return sql
|
||||
if session.Statement.RefTable != nil && session.Statement.RefTable.PrimaryKey != "" {
|
||||
sql = strings.Replace(sql, "`(id)`", session.Engine.Quote(session.Statement.RefTable.PrimaryKey), -1)
|
||||
sql = strings.Replace(sql, session.Engine.Quote("(id)"), session.Engine.Quote(session.Statement.RefTable.PrimaryKey), -1)
|
||||
return strings.Replace(sql, "(id)", session.Engine.Quote(session.Statement.RefTable.PrimaryKey), -1)
|
||||
}
|
||||
return sql
|
||||
}
|
||||
|
|
|
|||
74
helpers.go
74
helpers.go
|
|
@ -1,63 +1,63 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func indexNoCase(s, sep string) int {
|
||||
return strings.Index(strings.ToLower(s), strings.ToLower(sep))
|
||||
return strings.Index(strings.ToLower(s), strings.ToLower(sep))
|
||||
}
|
||||
|
||||
func splitNoCase(s, sep string) []string {
|
||||
idx := indexNoCase(s, sep)
|
||||
if idx < 0 {
|
||||
return []string{s}
|
||||
}
|
||||
return strings.Split(s, s[idx:idx+len(sep)])
|
||||
idx := indexNoCase(s, sep)
|
||||
if idx < 0 {
|
||||
return []string{s}
|
||||
}
|
||||
return strings.Split(s, s[idx:idx+len(sep)])
|
||||
}
|
||||
|
||||
func splitNNoCase(s, sep string, n int) []string {
|
||||
idx := indexNoCase(s, sep)
|
||||
if idx < 0 {
|
||||
return []string{s}
|
||||
}
|
||||
return strings.SplitN(s, s[idx:idx+len(sep)], n)
|
||||
idx := indexNoCase(s, sep)
|
||||
if idx < 0 {
|
||||
return []string{s}
|
||||
}
|
||||
return strings.SplitN(s, s[idx:idx+len(sep)], n)
|
||||
}
|
||||
|
||||
func makeArray(elem string, count int) []string {
|
||||
res := make([]string, count)
|
||||
for i := 0; i < count; i++ {
|
||||
res[i] = elem
|
||||
}
|
||||
return res
|
||||
res := make([]string, count)
|
||||
for i := 0; i < count; i++ {
|
||||
res[i] = elem
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func rType(bean interface{}) reflect.Type {
|
||||
sliceValue := reflect.Indirect(reflect.ValueOf(bean))
|
||||
return reflect.TypeOf(sliceValue.Interface())
|
||||
sliceValue := reflect.Indirect(reflect.ValueOf(bean))
|
||||
return reflect.TypeOf(sliceValue.Interface())
|
||||
}
|
||||
|
||||
func structName(v reflect.Type) string {
|
||||
for v.Kind() == reflect.Ptr {
|
||||
v = v.Elem()
|
||||
}
|
||||
return v.Name()
|
||||
for v.Kind() == reflect.Ptr {
|
||||
v = v.Elem()
|
||||
}
|
||||
return v.Name()
|
||||
}
|
||||
|
||||
func sliceEq(left, right []string) bool {
|
||||
for _, l := range left {
|
||||
var find bool
|
||||
for _, r := range right {
|
||||
if l == r {
|
||||
find = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !find {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for _, l := range left {
|
||||
var find bool
|
||||
for _, r := range right {
|
||||
if l == r {
|
||||
find = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !find {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
return true
|
||||
}
|
||||
|
|
|
|||
118
mapper.go
118
mapper.go
|
|
@ -7,8 +7,8 @@ import (
|
|||
|
||||
// name translation between struct, fields names and table, column names
|
||||
type IMapper interface {
|
||||
Obj2Table(string) string
|
||||
Table2Obj(string) string
|
||||
Obj2Table(string) string
|
||||
Table2Obj(string) string
|
||||
}
|
||||
|
||||
// SameMapper implements IMapper and provides same name between struct and
|
||||
|
|
@ -17,11 +17,11 @@ type SameMapper struct {
|
|||
}
|
||||
|
||||
func (m SameMapper) Obj2Table(o string) string {
|
||||
return o
|
||||
return o
|
||||
}
|
||||
|
||||
func (m SameMapper) Table2Obj(t string) string {
|
||||
return t
|
||||
return t
|
||||
}
|
||||
|
||||
// SnakeMapper implements IMapper and provides name transaltion between
|
||||
|
|
@ -30,101 +30,101 @@ type SnakeMapper struct {
|
|||
}
|
||||
|
||||
func snakeCasedName(name string) string {
|
||||
newstr := make([]rune, 0)
|
||||
for idx, chr := range name {
|
||||
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
|
||||
if idx > 0 {
|
||||
newstr = append(newstr, '_')
|
||||
}
|
||||
chr -= ('A' - 'a')
|
||||
}
|
||||
newstr = append(newstr, chr)
|
||||
}
|
||||
newstr := make([]rune, 0)
|
||||
for idx, chr := range name {
|
||||
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
|
||||
if idx > 0 {
|
||||
newstr = append(newstr, '_')
|
||||
}
|
||||
chr -= ('A' - 'a')
|
||||
}
|
||||
newstr = append(newstr, chr)
|
||||
}
|
||||
|
||||
return string(newstr)
|
||||
return string(newstr)
|
||||
}
|
||||
|
||||
/*func pascal2Sql(s string) (d string) {
|
||||
d = ""
|
||||
lastIdx := 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] >= 'A' && s[i] <= 'Z' {
|
||||
if lastIdx < i {
|
||||
d += s[lastIdx+1 : i]
|
||||
}
|
||||
if i != 0 {
|
||||
d += "_"
|
||||
}
|
||||
d += string(s[i] + 32)
|
||||
lastIdx = i
|
||||
}
|
||||
}
|
||||
d += s[lastIdx+1:]
|
||||
return
|
||||
d = ""
|
||||
lastIdx := 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] >= 'A' && s[i] <= 'Z' {
|
||||
if lastIdx < i {
|
||||
d += s[lastIdx+1 : i]
|
||||
}
|
||||
if i != 0 {
|
||||
d += "_"
|
||||
}
|
||||
d += string(s[i] + 32)
|
||||
lastIdx = i
|
||||
}
|
||||
}
|
||||
d += s[lastIdx+1:]
|
||||
return
|
||||
}*/
|
||||
|
||||
func (mapper SnakeMapper) Obj2Table(name string) string {
|
||||
return snakeCasedName(name)
|
||||
return snakeCasedName(name)
|
||||
}
|
||||
|
||||
func titleCasedName(name string) string {
|
||||
newstr := make([]rune, 0)
|
||||
upNextChar := true
|
||||
newstr := make([]rune, 0)
|
||||
upNextChar := true
|
||||
|
||||
for _, chr := range name {
|
||||
switch {
|
||||
case upNextChar:
|
||||
upNextChar = false
|
||||
if 'a' <= chr && chr <= 'z' {
|
||||
chr -= ('a' - 'A')
|
||||
}
|
||||
case chr == '_':
|
||||
upNextChar = true
|
||||
continue
|
||||
}
|
||||
for _, chr := range name {
|
||||
switch {
|
||||
case upNextChar:
|
||||
upNextChar = false
|
||||
if 'a' <= chr && chr <= 'z' {
|
||||
chr -= ('a' - 'A')
|
||||
}
|
||||
case chr == '_':
|
||||
upNextChar = true
|
||||
continue
|
||||
}
|
||||
|
||||
newstr = append(newstr, chr)
|
||||
}
|
||||
newstr = append(newstr, chr)
|
||||
}
|
||||
|
||||
return string(newstr)
|
||||
return string(newstr)
|
||||
}
|
||||
|
||||
func (mapper SnakeMapper) Table2Obj(name string) string {
|
||||
return titleCasedName(name)
|
||||
return titleCasedName(name)
|
||||
}
|
||||
|
||||
// provide prefix table name support
|
||||
type PrefixMapper struct {
|
||||
Mapper IMapper
|
||||
Prefix string
|
||||
Mapper IMapper
|
||||
Prefix string
|
||||
}
|
||||
|
||||
func (mapper PrefixMapper) Obj2Table(name string) string {
|
||||
return mapper.Prefix + mapper.Mapper.Obj2Table(name)
|
||||
return mapper.Prefix + mapper.Mapper.Obj2Table(name)
|
||||
}
|
||||
|
||||
func (mapper PrefixMapper) Table2Obj(name string) string {
|
||||
return mapper.Mapper.Table2Obj(name[len(mapper.Prefix):])
|
||||
return mapper.Mapper.Table2Obj(name[len(mapper.Prefix):])
|
||||
}
|
||||
|
||||
func NewPrefixMapper(mapper IMapper, prefix string) PrefixMapper {
|
||||
return PrefixMapper{mapper, prefix}
|
||||
return PrefixMapper{mapper, prefix}
|
||||
}
|
||||
|
||||
// provide suffix table name support
|
||||
type SuffixMapper struct {
|
||||
Mapper IMapper
|
||||
Suffix string
|
||||
Mapper IMapper
|
||||
Suffix string
|
||||
}
|
||||
|
||||
func (mapper SuffixMapper) Obj2Table(name string) string {
|
||||
return mapper.Suffix + mapper.Mapper.Obj2Table(name)
|
||||
return mapper.Suffix + mapper.Mapper.Obj2Table(name)
|
||||
}
|
||||
|
||||
func (mapper SuffixMapper) Table2Obj(name string) string {
|
||||
return mapper.Mapper.Table2Obj(name[len(mapper.Suffix):])
|
||||
return mapper.Mapper.Table2Obj(name[len(mapper.Suffix):])
|
||||
}
|
||||
|
||||
func NewSuffixMapper(mapper IMapper, suffix string) SuffixMapper {
|
||||
return SuffixMapper{mapper, suffix}
|
||||
return SuffixMapper{mapper, suffix}
|
||||
}
|
||||
|
|
|
|||
110
mymysql.go
110
mymysql.go
|
|
@ -1,66 +1,66 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type mymysql struct {
|
||||
mysql
|
||||
proto string
|
||||
raddr string
|
||||
laddr string
|
||||
timeout time.Duration
|
||||
db string
|
||||
user string
|
||||
passwd string
|
||||
mysql
|
||||
proto string
|
||||
raddr string
|
||||
laddr string
|
||||
timeout time.Duration
|
||||
db string
|
||||
user string
|
||||
passwd string
|
||||
}
|
||||
|
||||
func (db *mymysql) Init(drivername, uri string) error {
|
||||
db.mysql.base.init(drivername, uri)
|
||||
pd := strings.SplitN(uri, "*", 2)
|
||||
if len(pd) == 2 {
|
||||
// Parse protocol part of URI
|
||||
p := strings.SplitN(pd[0], ":", 2)
|
||||
if len(p) != 2 {
|
||||
return errors.New("Wrong protocol part of URI")
|
||||
}
|
||||
db.proto = p[0]
|
||||
options := strings.Split(p[1], ",")
|
||||
db.raddr = options[0]
|
||||
for _, o := range options[1:] {
|
||||
kv := strings.SplitN(o, "=", 2)
|
||||
var k, v string
|
||||
if len(kv) == 2 {
|
||||
k, v = kv[0], kv[1]
|
||||
} else {
|
||||
k, v = o, "true"
|
||||
}
|
||||
switch k {
|
||||
case "laddr":
|
||||
db.laddr = v
|
||||
case "timeout":
|
||||
to, err := time.ParseDuration(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db.timeout = to
|
||||
default:
|
||||
return errors.New("Unknown option: " + k)
|
||||
}
|
||||
}
|
||||
// Remove protocol part
|
||||
pd = pd[1:]
|
||||
}
|
||||
// Parse database part of URI
|
||||
dup := strings.SplitN(pd[0], "/", 3)
|
||||
if len(dup) != 3 {
|
||||
return errors.New("Wrong database part of URI")
|
||||
}
|
||||
db.dbname = dup[0]
|
||||
db.user = dup[1]
|
||||
db.passwd = dup[2]
|
||||
db.mysql.base.init(drivername, uri)
|
||||
pd := strings.SplitN(uri, "*", 2)
|
||||
if len(pd) == 2 {
|
||||
// Parse protocol part of URI
|
||||
p := strings.SplitN(pd[0], ":", 2)
|
||||
if len(p) != 2 {
|
||||
return errors.New("Wrong protocol part of URI")
|
||||
}
|
||||
db.proto = p[0]
|
||||
options := strings.Split(p[1], ",")
|
||||
db.raddr = options[0]
|
||||
for _, o := range options[1:] {
|
||||
kv := strings.SplitN(o, "=", 2)
|
||||
var k, v string
|
||||
if len(kv) == 2 {
|
||||
k, v = kv[0], kv[1]
|
||||
} else {
|
||||
k, v = o, "true"
|
||||
}
|
||||
switch k {
|
||||
case "laddr":
|
||||
db.laddr = v
|
||||
case "timeout":
|
||||
to, err := time.ParseDuration(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db.timeout = to
|
||||
default:
|
||||
return errors.New("Unknown option: " + k)
|
||||
}
|
||||
}
|
||||
// Remove protocol part
|
||||
pd = pd[1:]
|
||||
}
|
||||
// Parse database part of URI
|
||||
dup := strings.SplitN(pd[0], "/", 3)
|
||||
if len(dup) != 3 {
|
||||
return errors.New("Wrong database part of URI")
|
||||
}
|
||||
db.dbname = dup[0]
|
||||
db.user = dup[1]
|
||||
db.passwd = dup[2]
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
204
mymysql_test.go
204
mymysql_test.go
|
|
@ -1,8 +1,8 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
_ "github.com/ziutek/mymysql/godrv"
|
||||
"testing"
|
||||
_ "github.com/ziutek/mymysql/godrv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/*
|
||||
|
|
@ -13,145 +13,145 @@ utf8 COLLATE utf8_general_ci;
|
|||
var showTestSql bool = true
|
||||
|
||||
func TestMyMysql(t *testing.T) {
|
||||
err := mymysqlDdlImport()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine, err := NewEngine("mymysql", "xorm_test/root/")
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
err := mymysqlDdlImport()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine, err := NewEngine("mymysql", "xorm_test/root/")
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
|
||||
sqlResults, _ := engine.Import("tests/mysql_ddl.sql")
|
||||
engine.LogDebug("sql results: %v", sqlResults)
|
||||
sqlResults, _ := engine.Import("tests/mysql_ddl.sql")
|
||||
engine.LogDebug("sql results: %v", sqlResults)
|
||||
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
testAll3(engine, t)
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
testAll3(engine, t)
|
||||
}
|
||||
|
||||
func TestMyMysqlWithCache(t *testing.T) {
|
||||
err := mymysqlDdlImport()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine, err := NewEngine("mymysql", "xorm_test2/root/")
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
err := mymysqlDdlImport()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine, err := NewEngine("mymysql", "xorm_test2/root/")
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
|
||||
sqlResults, _ := engine.Import("tests/mysql_ddl.sql")
|
||||
engine.LogDebug("sql results: %v", sqlResults)
|
||||
sqlResults, _ := engine.Import("tests/mysql_ddl.sql")
|
||||
engine.LogDebug("sql results: %v", sqlResults)
|
||||
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
}
|
||||
|
||||
func newMyMysqlEngine() (*Engine, error) {
|
||||
return NewEngine("mymysql", "xorm_test2/root/")
|
||||
return NewEngine("mymysql", "xorm_test2/root/")
|
||||
}
|
||||
|
||||
func mymysqlDdlImport() error {
|
||||
engine, err := NewEngine("mymysql", "/root/")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
engine, err := NewEngine("mymysql", "/root/")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
|
||||
sqlResults, _ := engine.Import("tests/mysql_ddl.sql")
|
||||
engine.LogDebug("sql results: %v", sqlResults)
|
||||
engine.Close()
|
||||
return nil
|
||||
sqlResults, _ := engine.Import("tests/mysql_ddl.sql")
|
||||
engine.LogDebug("sql results: %v", sqlResults)
|
||||
engine.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func BenchmarkMyMysqlNoCacheInsert(t *testing.B) {
|
||||
engine, err := newMyMysqlEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
engine, err := newMyMysqlEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
|
||||
doBenchInsert(engine, t)
|
||||
doBenchInsert(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkMyMysqlNoCacheFind(t *testing.B) {
|
||||
engine, err := newMyMysqlEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
engine, err := newMyMysqlEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
|
||||
//engine.ShowSQL = true
|
||||
doBenchFind(engine, t)
|
||||
//engine.ShowSQL = true
|
||||
doBenchFind(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkMyMysqlNoCacheFindPtr(t *testing.B) {
|
||||
engine, err := newMyMysqlEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
engine, err := newMyMysqlEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
|
||||
//engine.ShowSQL = true
|
||||
doBenchFindPtr(engine, t)
|
||||
//engine.ShowSQL = true
|
||||
doBenchFindPtr(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkMyMysqlCacheInsert(t *testing.B) {
|
||||
engine, err := newMyMysqlEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine, err := newMyMysqlEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
defer engine.Close()
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
defer engine.Close()
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
|
||||
doBenchInsert(engine, t)
|
||||
doBenchInsert(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkMyMysqlCacheFind(t *testing.B) {
|
||||
engine, err := newMyMysqlEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine, err := newMyMysqlEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
defer engine.Close()
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
defer engine.Close()
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
|
||||
doBenchFind(engine, t)
|
||||
doBenchFind(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkMyMysqlCacheFindPtr(t *testing.B) {
|
||||
engine, err := newMyMysqlEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine, err := newMyMysqlEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
defer engine.Close()
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
defer engine.Close()
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
|
||||
doBenchFindPtr(engine, t)
|
||||
doBenchFindPtr(engine, t)
|
||||
}
|
||||
|
|
|
|||
488
mysql.go
488
mysql.go
|
|
@ -1,311 +1,311 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"crypto/tls"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type base struct {
|
||||
drivername string
|
||||
dataSourceName string
|
||||
drivername string
|
||||
dataSourceName string
|
||||
}
|
||||
|
||||
func (b *base) init(drivername, dataSourceName string) {
|
||||
b.drivername, b.dataSourceName = drivername, dataSourceName
|
||||
b.drivername, b.dataSourceName = drivername, dataSourceName
|
||||
}
|
||||
|
||||
type mysql struct {
|
||||
base
|
||||
user string
|
||||
passwd string
|
||||
net string
|
||||
addr string
|
||||
dbname string
|
||||
params map[string]string
|
||||
loc *time.Location
|
||||
timeout time.Duration
|
||||
tls *tls.Config
|
||||
allowAllFiles bool
|
||||
allowOldPasswords bool
|
||||
clientFoundRows bool
|
||||
base
|
||||
user string
|
||||
passwd string
|
||||
net string
|
||||
addr string
|
||||
dbname string
|
||||
params map[string]string
|
||||
loc *time.Location
|
||||
timeout time.Duration
|
||||
tls *tls.Config
|
||||
allowAllFiles bool
|
||||
allowOldPasswords bool
|
||||
clientFoundRows bool
|
||||
}
|
||||
|
||||
/*func readBool(input string) (value bool, valid bool) {
|
||||
switch input {
|
||||
case "1", "true", "TRUE", "True":
|
||||
return true, true
|
||||
case "0", "false", "FALSE", "False":
|
||||
return false, true
|
||||
}
|
||||
switch input {
|
||||
case "1", "true", "TRUE", "True":
|
||||
return true, true
|
||||
case "0", "false", "FALSE", "False":
|
||||
return false, true
|
||||
}
|
||||
|
||||
// Not a valid bool value
|
||||
return
|
||||
// Not a valid bool value
|
||||
return
|
||||
}*/
|
||||
|
||||
func (cfg *mysql) parseDSN(dsn string) (err error) {
|
||||
//cfg.params = make(map[string]string)
|
||||
dsnPattern := regexp.MustCompile(
|
||||
`^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
|
||||
`(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
|
||||
`\/(?P<dbname>.*?)` + // /dbname
|
||||
`(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1¶mN=valueN]
|
||||
matches := dsnPattern.FindStringSubmatch(dsn)
|
||||
//tlsConfigRegister := make(map[string]*tls.Config)
|
||||
names := dsnPattern.SubexpNames()
|
||||
//cfg.params = make(map[string]string)
|
||||
dsnPattern := regexp.MustCompile(
|
||||
`^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
|
||||
`(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
|
||||
`\/(?P<dbname>.*?)` + // /dbname
|
||||
`(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1¶mN=valueN]
|
||||
matches := dsnPattern.FindStringSubmatch(dsn)
|
||||
//tlsConfigRegister := make(map[string]*tls.Config)
|
||||
names := dsnPattern.SubexpNames()
|
||||
|
||||
for i, match := range matches {
|
||||
switch names[i] {
|
||||
case "dbname":
|
||||
cfg.dbname = match
|
||||
}
|
||||
}
|
||||
return
|
||||
for i, match := range matches {
|
||||
switch names[i] {
|
||||
case "dbname":
|
||||
cfg.dbname = match
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (db *mysql) Init(drivername, uri string) error {
|
||||
db.base.init(drivername, uri)
|
||||
return db.parseDSN(uri)
|
||||
db.base.init(drivername, uri)
|
||||
return db.parseDSN(uri)
|
||||
}
|
||||
|
||||
func (db *mysql) SqlType(c *Column) string {
|
||||
var res string
|
||||
switch t := c.SQLType.Name; t {
|
||||
case Bool:
|
||||
res = TinyInt
|
||||
case Serial:
|
||||
c.IsAutoIncrement = true
|
||||
c.IsPrimaryKey = true
|
||||
c.Nullable = false
|
||||
res = Int
|
||||
case BigSerial:
|
||||
c.IsAutoIncrement = true
|
||||
c.IsPrimaryKey = true
|
||||
c.Nullable = false
|
||||
res = BigInt
|
||||
case Bytea:
|
||||
res = Blob
|
||||
case TimeStampz:
|
||||
res = Char
|
||||
c.Length = 64
|
||||
default:
|
||||
res = t
|
||||
}
|
||||
var res string
|
||||
switch t := c.SQLType.Name; t {
|
||||
case Bool:
|
||||
res = TinyInt
|
||||
case Serial:
|
||||
c.IsAutoIncrement = true
|
||||
c.IsPrimaryKey = true
|
||||
c.Nullable = false
|
||||
res = Int
|
||||
case BigSerial:
|
||||
c.IsAutoIncrement = true
|
||||
c.IsPrimaryKey = true
|
||||
c.Nullable = false
|
||||
res = BigInt
|
||||
case Bytea:
|
||||
res = Blob
|
||||
case TimeStampz:
|
||||
res = Char
|
||||
c.Length = 64
|
||||
default:
|
||||
res = t
|
||||
}
|
||||
|
||||
var hasLen1 bool = (c.Length > 0)
|
||||
var hasLen2 bool = (c.Length2 > 0)
|
||||
if hasLen1 {
|
||||
res += "(" + strconv.Itoa(c.Length) + ")"
|
||||
} else if hasLen2 {
|
||||
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
|
||||
}
|
||||
return res
|
||||
var hasLen1 bool = (c.Length > 0)
|
||||
var hasLen2 bool = (c.Length2 > 0)
|
||||
if hasLen1 {
|
||||
res += "(" + strconv.Itoa(c.Length) + ")"
|
||||
} else if hasLen2 {
|
||||
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (db *mysql) SupportInsertMany() bool {
|
||||
return true
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *mysql) QuoteStr() string {
|
||||
return "`"
|
||||
return "`"
|
||||
}
|
||||
|
||||
func (db *mysql) SupportEngine() bool {
|
||||
return true
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *mysql) AutoIncrStr() string {
|
||||
return "AUTO_INCREMENT"
|
||||
return "AUTO_INCREMENT"
|
||||
}
|
||||
|
||||
func (db *mysql) SupportCharset() bool {
|
||||
return true
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *mysql) IndexOnTable() bool {
|
||||
return true
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *mysql) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
|
||||
args := []interface{}{db.dbname, tableName, idxName}
|
||||
sql := "SELECT `INDEX_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS`"
|
||||
sql += " WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `INDEX_NAME`=?"
|
||||
return sql, args
|
||||
args := []interface{}{db.dbname, tableName, idxName}
|
||||
sql := "SELECT `INDEX_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS`"
|
||||
sql += " WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `INDEX_NAME`=?"
|
||||
return sql, args
|
||||
}
|
||||
|
||||
func (db *mysql) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||
args := []interface{}{db.dbname, tableName, colName}
|
||||
sql := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
|
||||
return sql, args
|
||||
args := []interface{}{db.dbname, tableName, colName}
|
||||
sql := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
|
||||
return sql, args
|
||||
}
|
||||
|
||||
func (db *mysql) TableCheckSql(tableName string) (string, []interface{}) {
|
||||
args := []interface{}{db.dbname, tableName}
|
||||
sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
|
||||
return sql, args
|
||||
args := []interface{}{db.dbname, tableName}
|
||||
sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
|
||||
return sql, args
|
||||
}
|
||||
|
||||
func (db *mysql) GetColumns(tableName string) ([]string, map[string]*Column, error) {
|
||||
args := []interface{}{db.dbname, tableName}
|
||||
s := "SELECT `COLUMN_NAME`, `IS_NULLABLE`, `COLUMN_DEFAULT`, `COLUMN_TYPE`," +
|
||||
" `COLUMN_KEY`, `EXTRA` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
cols := make(map[string]*Column)
|
||||
colSeq := make([]string, 0)
|
||||
for _, record := range res {
|
||||
col := new(Column)
|
||||
col.Indexes = make(map[string]bool)
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "COLUMN_NAME":
|
||||
col.Name = strings.Trim(string(content), "` ")
|
||||
case "IS_NULLABLE":
|
||||
if "YES" == string(content) {
|
||||
col.Nullable = true
|
||||
}
|
||||
case "COLUMN_DEFAULT":
|
||||
// add ''
|
||||
col.Default = string(content)
|
||||
case "COLUMN_TYPE":
|
||||
cts := strings.Split(string(content), "(")
|
||||
var len1, len2 int
|
||||
if len(cts) == 2 {
|
||||
idx := strings.Index(cts[1], ")")
|
||||
lens := strings.Split(cts[1][0:idx], ",")
|
||||
len1, err = strconv.Atoi(strings.TrimSpace(lens[0]))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if len(lens) == 2 {
|
||||
len2, err = strconv.Atoi(lens[1])
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
colName := cts[0]
|
||||
colType := strings.ToUpper(colName)
|
||||
col.Length = len1
|
||||
col.Length2 = len2
|
||||
if _, ok := sqlTypes[colType]; ok {
|
||||
col.SQLType = SQLType{colType, len1, len2}
|
||||
} else {
|
||||
return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", colType))
|
||||
}
|
||||
case "COLUMN_KEY":
|
||||
key := string(content)
|
||||
if key == "PRI" {
|
||||
col.IsPrimaryKey = true
|
||||
}
|
||||
if key == "UNI" {
|
||||
//col.is
|
||||
}
|
||||
case "EXTRA":
|
||||
extra := string(content)
|
||||
if extra == "auto_increment" {
|
||||
col.IsAutoIncrement = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if col.SQLType.IsText() {
|
||||
if col.Default != "" {
|
||||
col.Default = "'" + col.Default + "'"
|
||||
}
|
||||
}
|
||||
cols[col.Name] = col
|
||||
colSeq = append(colSeq, col.Name)
|
||||
}
|
||||
return colSeq, cols, nil
|
||||
args := []interface{}{db.dbname, tableName}
|
||||
s := "SELECT `COLUMN_NAME`, `IS_NULLABLE`, `COLUMN_DEFAULT`, `COLUMN_TYPE`," +
|
||||
" `COLUMN_KEY`, `EXTRA` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
cols := make(map[string]*Column)
|
||||
colSeq := make([]string, 0)
|
||||
for _, record := range res {
|
||||
col := new(Column)
|
||||
col.Indexes = make(map[string]bool)
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "COLUMN_NAME":
|
||||
col.Name = strings.Trim(string(content), "` ")
|
||||
case "IS_NULLABLE":
|
||||
if "YES" == string(content) {
|
||||
col.Nullable = true
|
||||
}
|
||||
case "COLUMN_DEFAULT":
|
||||
// add ''
|
||||
col.Default = string(content)
|
||||
case "COLUMN_TYPE":
|
||||
cts := strings.Split(string(content), "(")
|
||||
var len1, len2 int
|
||||
if len(cts) == 2 {
|
||||
idx := strings.Index(cts[1], ")")
|
||||
lens := strings.Split(cts[1][0:idx], ",")
|
||||
len1, err = strconv.Atoi(strings.TrimSpace(lens[0]))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if len(lens) == 2 {
|
||||
len2, err = strconv.Atoi(lens[1])
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
colName := cts[0]
|
||||
colType := strings.ToUpper(colName)
|
||||
col.Length = len1
|
||||
col.Length2 = len2
|
||||
if _, ok := sqlTypes[colType]; ok {
|
||||
col.SQLType = SQLType{colType, len1, len2}
|
||||
} else {
|
||||
return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", colType))
|
||||
}
|
||||
case "COLUMN_KEY":
|
||||
key := string(content)
|
||||
if key == "PRI" {
|
||||
col.IsPrimaryKey = true
|
||||
}
|
||||
if key == "UNI" {
|
||||
//col.is
|
||||
}
|
||||
case "EXTRA":
|
||||
extra := string(content)
|
||||
if extra == "auto_increment" {
|
||||
col.IsAutoIncrement = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if col.SQLType.IsText() {
|
||||
if col.Default != "" {
|
||||
col.Default = "'" + col.Default + "'"
|
||||
}
|
||||
}
|
||||
cols[col.Name] = col
|
||||
colSeq = append(colSeq, col.Name)
|
||||
}
|
||||
return colSeq, cols, nil
|
||||
}
|
||||
|
||||
func (db *mysql) GetTables() ([]*Table, error) {
|
||||
args := []interface{}{db.dbname}
|
||||
s := "SELECT `TABLE_NAME`, `ENGINE`, `TABLE_ROWS`, `AUTO_INCREMENT` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=?"
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args := []interface{}{db.dbname}
|
||||
s := "SELECT `TABLE_NAME`, `ENGINE`, `TABLE_ROWS`, `AUTO_INCREMENT` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=?"
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tables := make([]*Table, 0)
|
||||
for _, record := range res {
|
||||
table := new(Table)
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "TABLE_NAME":
|
||||
table.Name = strings.Trim(string(content), "` ")
|
||||
case "ENGINE":
|
||||
}
|
||||
}
|
||||
tables = append(tables, table)
|
||||
}
|
||||
return tables, nil
|
||||
tables := make([]*Table, 0)
|
||||
for _, record := range res {
|
||||
table := new(Table)
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "TABLE_NAME":
|
||||
table.Name = strings.Trim(string(content), "` ")
|
||||
case "ENGINE":
|
||||
}
|
||||
}
|
||||
tables = append(tables, table)
|
||||
}
|
||||
return tables, nil
|
||||
}
|
||||
|
||||
func (db *mysql) GetIndexes(tableName string) (map[string]*Index, error) {
|
||||
args := []interface{}{db.dbname, tableName}
|
||||
s := "SELECT `INDEX_NAME`, `NON_UNIQUE`, `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args := []interface{}{db.dbname, tableName}
|
||||
s := "SELECT `INDEX_NAME`, `NON_UNIQUE`, `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
indexes := make(map[string]*Index, 0)
|
||||
for _, record := range res {
|
||||
var indexType int
|
||||
var indexName, colName string
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "NON_UNIQUE":
|
||||
if "YES" == string(content) || string(content) == "1" {
|
||||
indexType = IndexType
|
||||
} else {
|
||||
indexType = UniqueType
|
||||
}
|
||||
case "INDEX_NAME":
|
||||
indexName = string(content)
|
||||
case "COLUMN_NAME":
|
||||
colName = strings.Trim(string(content), "` ")
|
||||
}
|
||||
}
|
||||
if indexName == "PRIMARY" {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
||||
indexName = indexName[5+len(tableName) : len(indexName)]
|
||||
}
|
||||
indexes := make(map[string]*Index, 0)
|
||||
for _, record := range res {
|
||||
var indexType int
|
||||
var indexName, colName string
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "NON_UNIQUE":
|
||||
if "YES" == string(content) || string(content) == "1" {
|
||||
indexType = IndexType
|
||||
} else {
|
||||
indexType = UniqueType
|
||||
}
|
||||
case "INDEX_NAME":
|
||||
indexName = string(content)
|
||||
case "COLUMN_NAME":
|
||||
colName = strings.Trim(string(content), "` ")
|
||||
}
|
||||
}
|
||||
if indexName == "PRIMARY" {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
||||
indexName = indexName[5+len(tableName) : len(indexName)]
|
||||
}
|
||||
|
||||
var index *Index
|
||||
var ok bool
|
||||
if index, ok = indexes[indexName]; !ok {
|
||||
index = new(Index)
|
||||
index.Type = indexType
|
||||
index.Name = indexName
|
||||
indexes[indexName] = index
|
||||
}
|
||||
index.AddColumn(colName)
|
||||
}
|
||||
return indexes, nil
|
||||
var index *Index
|
||||
var ok bool
|
||||
if index, ok = indexes[indexName]; !ok {
|
||||
index = new(Index)
|
||||
index.Type = indexType
|
||||
index.Name = indexName
|
||||
indexes[indexName] = index
|
||||
}
|
||||
index.AddColumn(colName)
|
||||
}
|
||||
return indexes, nil
|
||||
}
|
||||
|
|
|
|||
198
mysql_test.go
198
mysql_test.go
|
|
@ -1,8 +1,8 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"testing"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/*
|
||||
|
|
@ -13,136 +13,136 @@ utf8 COLLATE utf8_general_ci;
|
|||
var mysqlShowTestSql bool = true
|
||||
|
||||
func TestMysql(t *testing.T) {
|
||||
err := mysqlDdlImport()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
err := mysqlDdlImport()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
engine, err := NewEngine("mysql", "root:@/xorm_test?charset=utf8")
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.ShowSQL = mysqlShowTestSql
|
||||
engine.ShowErr = mysqlShowTestSql
|
||||
engine.ShowWarn = mysqlShowTestSql
|
||||
engine.ShowDebug = mysqlShowTestSql
|
||||
engine, err := NewEngine("mysql", "root:@/xorm_test?charset=utf8")
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.ShowSQL = mysqlShowTestSql
|
||||
engine.ShowErr = mysqlShowTestSql
|
||||
engine.ShowWarn = mysqlShowTestSql
|
||||
engine.ShowDebug = mysqlShowTestSql
|
||||
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
testAll3(engine, t)
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
testAll3(engine, t)
|
||||
}
|
||||
|
||||
func TestMysqlWithCache(t *testing.T) {
|
||||
err := mysqlDdlImport()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
err := mysqlDdlImport()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
engine, err := NewEngine("mysql", "root:@/xorm_test?charset=utf8")
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
engine.ShowSQL = mysqlShowTestSql
|
||||
engine.ShowErr = mysqlShowTestSql
|
||||
engine.ShowWarn = mysqlShowTestSql
|
||||
engine.ShowDebug = mysqlShowTestSql
|
||||
engine, err := NewEngine("mysql", "root:@/xorm_test?charset=utf8")
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
engine.ShowSQL = mysqlShowTestSql
|
||||
engine.ShowErr = mysqlShowTestSql
|
||||
engine.ShowWarn = mysqlShowTestSql
|
||||
engine.ShowDebug = mysqlShowTestSql
|
||||
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
}
|
||||
|
||||
func newMysqlEngine() (*Engine, error) {
|
||||
return NewEngine("mysql", "root:@/xorm_test?charset=utf8")
|
||||
return NewEngine("mysql", "root:@/xorm_test?charset=utf8")
|
||||
}
|
||||
|
||||
func mysqlDdlImport() error {
|
||||
engine, err := NewEngine("mysql", "root:@/?charset=utf8")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
engine.ShowSQL = mysqlShowTestSql
|
||||
engine.ShowErr = mysqlShowTestSql
|
||||
engine.ShowWarn = mysqlShowTestSql
|
||||
engine.ShowDebug = mysqlShowTestSql
|
||||
engine, err := NewEngine("mysql", "root:@/?charset=utf8")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
engine.ShowSQL = mysqlShowTestSql
|
||||
engine.ShowErr = mysqlShowTestSql
|
||||
engine.ShowWarn = mysqlShowTestSql
|
||||
engine.ShowDebug = mysqlShowTestSql
|
||||
|
||||
sqlResults, _ := engine.Import("tests/mysql_ddl.sql")
|
||||
engine.LogDebug("sql results: %v", sqlResults)
|
||||
engine.Close()
|
||||
return nil
|
||||
sqlResults, _ := engine.Import("tests/mysql_ddl.sql")
|
||||
engine.LogDebug("sql results: %v", sqlResults)
|
||||
engine.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func BenchmarkMysqlNoCacheInsert(t *testing.B) {
|
||||
engine, err := newMysqlEngine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchInsert(engine, t)
|
||||
engine, err := newMysqlEngine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchInsert(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkMysqlNoCacheFind(t *testing.B) {
|
||||
engine, err := newMysqlEngine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchFind(engine, t)
|
||||
engine, err := newMysqlEngine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchFind(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkMysqlNoCacheFindPtr(t *testing.B) {
|
||||
engine, err := newMysqlEngine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchFindPtr(engine, t)
|
||||
engine, err := newMysqlEngine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchFindPtr(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkMysqlCacheInsert(t *testing.B) {
|
||||
engine, err := newMysqlEngine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
engine, err := newMysqlEngine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
|
||||
doBenchInsert(engine, t)
|
||||
doBenchInsert(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkMysqlCacheFind(t *testing.B) {
|
||||
engine, err := newMysqlEngine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
engine, err := newMysqlEngine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
|
||||
doBenchFind(engine, t)
|
||||
doBenchFind(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkMysqlCacheFindPtr(t *testing.B) {
|
||||
engine, err := newMysqlEngine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
engine, err := newMysqlEngine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
|
||||
doBenchFindPtr(engine, t)
|
||||
doBenchFindPtr(engine, t)
|
||||
}
|
||||
|
|
|
|||
290
pool.go
290
pool.go
|
|
@ -1,13 +1,13 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
//"fmt"
|
||||
"sync"
|
||||
//"sync/atomic"
|
||||
"container/list"
|
||||
"reflect"
|
||||
"time"
|
||||
"database/sql"
|
||||
//"fmt"
|
||||
"sync"
|
||||
//"sync/atomic"
|
||||
"container/list"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Interface IConnecPool is a connection pool interface, all implements should implement
|
||||
|
|
@ -17,14 +17,14 @@ import (
|
|||
// ReleaseDB for releasing a db connection;
|
||||
// Close for invoking when engine.Close
|
||||
type IConnectPool interface {
|
||||
Init(engine *Engine) error
|
||||
RetrieveDB(engine *Engine) (*sql.DB, error)
|
||||
ReleaseDB(engine *Engine, db *sql.DB)
|
||||
Close(engine *Engine) error
|
||||
SetMaxIdleConns(conns int)
|
||||
MaxIdleConns() int
|
||||
SetMaxConns(conns int)
|
||||
MaxConns() int
|
||||
Init(engine *Engine) error
|
||||
RetrieveDB(engine *Engine) (*sql.DB, error)
|
||||
ReleaseDB(engine *Engine, db *sql.DB)
|
||||
Close(engine *Engine) error
|
||||
SetMaxIdleConns(conns int)
|
||||
MaxIdleConns() int
|
||||
SetMaxConns(conns int)
|
||||
MaxConns() int
|
||||
}
|
||||
|
||||
// Struct NoneConnectPool is a implement for IConnectPool. It provides directly invoke driver's
|
||||
|
|
@ -34,35 +34,35 @@ type NoneConnectPool struct {
|
|||
|
||||
// NewNoneConnectPool new a NoneConnectPool.
|
||||
func NewNoneConnectPool() IConnectPool {
|
||||
return &NoneConnectPool{}
|
||||
return &NoneConnectPool{}
|
||||
}
|
||||
|
||||
// Init do nothing
|
||||
func (p *NoneConnectPool) Init(engine *Engine) error {
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// RetrieveDB directly open a connection
|
||||
func (p *NoneConnectPool) RetrieveDB(engine *Engine) (db *sql.DB, err error) {
|
||||
db, err = engine.OpenDB()
|
||||
return
|
||||
db, err = engine.OpenDB()
|
||||
return
|
||||
}
|
||||
|
||||
// ReleaseDB directly close a connection
|
||||
func (p *NoneConnectPool) ReleaseDB(engine *Engine, db *sql.DB) {
|
||||
db.Close()
|
||||
db.Close()
|
||||
}
|
||||
|
||||
// Close do nothing
|
||||
func (p *NoneConnectPool) Close(engine *Engine) error {
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *NoneConnectPool) SetMaxIdleConns(conns int) {
|
||||
}
|
||||
|
||||
func (p *NoneConnectPool) MaxIdleConns() int {
|
||||
return 0
|
||||
return 0
|
||||
}
|
||||
|
||||
// not implemented
|
||||
|
|
@ -71,133 +71,133 @@ func (p *NoneConnectPool) SetMaxConns(conns int) {
|
|||
|
||||
// not implemented
|
||||
func (p *NoneConnectPool) MaxConns() int {
|
||||
return -1
|
||||
return -1
|
||||
}
|
||||
|
||||
// Struct SysConnectPool is a simple wrapper for using system default connection pool.
|
||||
// About the system connection pool, you can review the code database/sql/sql.go
|
||||
// It's currently default Pool implments.
|
||||
type SysConnectPool struct {
|
||||
db *sql.DB
|
||||
maxIdleConns int
|
||||
maxConns int
|
||||
curConns int
|
||||
mutex *sync.Mutex
|
||||
queue *list.List
|
||||
db *sql.DB
|
||||
maxIdleConns int
|
||||
maxConns int
|
||||
curConns int
|
||||
mutex *sync.Mutex
|
||||
queue *list.List
|
||||
}
|
||||
|
||||
// NewSysConnectPool new a SysConnectPool.
|
||||
func NewSysConnectPool() IConnectPool {
|
||||
return &SysConnectPool{}
|
||||
return &SysConnectPool{}
|
||||
}
|
||||
|
||||
// Init create a db immediately and keep it util engine closed.
|
||||
func (s *SysConnectPool) Init(engine *Engine) error {
|
||||
db, err := engine.OpenDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.db = db
|
||||
s.maxIdleConns = 2
|
||||
s.maxConns = -1
|
||||
s.curConns = 0
|
||||
s.mutex = &sync.Mutex{}
|
||||
s.queue = list.New()
|
||||
return nil
|
||||
db, err := engine.OpenDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.db = db
|
||||
s.maxIdleConns = 2
|
||||
s.maxConns = -1
|
||||
s.curConns = 0
|
||||
s.mutex = &sync.Mutex{}
|
||||
s.queue = list.New()
|
||||
return nil
|
||||
}
|
||||
|
||||
type node struct {
|
||||
mutex sync.Mutex
|
||||
cond *sync.Cond
|
||||
mutex sync.Mutex
|
||||
cond *sync.Cond
|
||||
}
|
||||
|
||||
func newCondNode() *node {
|
||||
n := &node{}
|
||||
n.cond = sync.NewCond(&n.mutex)
|
||||
return n
|
||||
n := &node{}
|
||||
n.cond = sync.NewCond(&n.mutex)
|
||||
return n
|
||||
}
|
||||
|
||||
// RetrieveDB just return the only db
|
||||
func (s *SysConnectPool) RetrieveDB(engine *Engine) (db *sql.DB, err error) {
|
||||
/*if s.maxConns > 0 {
|
||||
fmt.Println("before retrieve")
|
||||
s.mutex.Lock()
|
||||
for s.curConns >= s.maxConns {
|
||||
fmt.Println("before waiting...", s.curConns, s.queue.Len())
|
||||
s.mutex.Unlock()
|
||||
n := NewNode()
|
||||
n.cond.L.Lock()
|
||||
s.queue.PushBack(n)
|
||||
n.cond.Wait()
|
||||
n.cond.L.Unlock()
|
||||
s.mutex.Lock()
|
||||
fmt.Println("after waiting...", s.curConns, s.queue.Len())
|
||||
}
|
||||
s.curConns += 1
|
||||
s.mutex.Unlock()
|
||||
fmt.Println("after retrieve")
|
||||
}*/
|
||||
return s.db, nil
|
||||
/*if s.maxConns > 0 {
|
||||
fmt.Println("before retrieve")
|
||||
s.mutex.Lock()
|
||||
for s.curConns >= s.maxConns {
|
||||
fmt.Println("before waiting...", s.curConns, s.queue.Len())
|
||||
s.mutex.Unlock()
|
||||
n := NewNode()
|
||||
n.cond.L.Lock()
|
||||
s.queue.PushBack(n)
|
||||
n.cond.Wait()
|
||||
n.cond.L.Unlock()
|
||||
s.mutex.Lock()
|
||||
fmt.Println("after waiting...", s.curConns, s.queue.Len())
|
||||
}
|
||||
s.curConns += 1
|
||||
s.mutex.Unlock()
|
||||
fmt.Println("after retrieve")
|
||||
}*/
|
||||
return s.db, nil
|
||||
}
|
||||
|
||||
// ReleaseDB do nothing
|
||||
func (s *SysConnectPool) ReleaseDB(engine *Engine, db *sql.DB) {
|
||||
/*if s.maxConns > 0 {
|
||||
s.mutex.Lock()
|
||||
fmt.Println("before release", s.queue.Len())
|
||||
s.curConns -= 1
|
||||
/*if s.maxConns > 0 {
|
||||
s.mutex.Lock()
|
||||
fmt.Println("before release", s.queue.Len())
|
||||
s.curConns -= 1
|
||||
|
||||
if e := s.queue.Front(); e != nil {
|
||||
n := e.Value.(*node)
|
||||
//n.cond.L.Lock()
|
||||
n.cond.Signal()
|
||||
fmt.Println("signaled...")
|
||||
s.queue.Remove(e)
|
||||
//n.cond.L.Unlock()
|
||||
}
|
||||
fmt.Println("after released", s.queue.Len())
|
||||
s.mutex.Unlock()
|
||||
}*/
|
||||
if e := s.queue.Front(); e != nil {
|
||||
n := e.Value.(*node)
|
||||
//n.cond.L.Lock()
|
||||
n.cond.Signal()
|
||||
fmt.Println("signaled...")
|
||||
s.queue.Remove(e)
|
||||
//n.cond.L.Unlock()
|
||||
}
|
||||
fmt.Println("after released", s.queue.Len())
|
||||
s.mutex.Unlock()
|
||||
}*/
|
||||
}
|
||||
|
||||
// Close closed the only db
|
||||
func (p *SysConnectPool) Close(engine *Engine) error {
|
||||
return p.db.Close()
|
||||
return p.db.Close()
|
||||
}
|
||||
|
||||
func (p *SysConnectPool) SetMaxIdleConns(conns int) {
|
||||
p.db.SetMaxIdleConns(conns)
|
||||
p.maxIdleConns = conns
|
||||
p.db.SetMaxIdleConns(conns)
|
||||
p.maxIdleConns = conns
|
||||
}
|
||||
|
||||
func (p *SysConnectPool) MaxIdleConns() int {
|
||||
return p.maxIdleConns
|
||||
return p.maxIdleConns
|
||||
}
|
||||
|
||||
// not implemented
|
||||
func (p *SysConnectPool) SetMaxConns(conns int) {
|
||||
p.maxConns = conns
|
||||
// if support SetMaxOpenConns, go 1.2+, then set
|
||||
if reflect.ValueOf(p.db).MethodByName("SetMaxOpenConns").IsValid() {
|
||||
reflect.ValueOf(p.db).MethodByName("SetMaxOpenConns").Call([]reflect.Value{reflect.ValueOf(conns)})
|
||||
}
|
||||
//p.db.SetMaxOpenConns(conns)
|
||||
p.maxConns = conns
|
||||
// if support SetMaxOpenConns, go 1.2+, then set
|
||||
if reflect.ValueOf(p.db).MethodByName("SetMaxOpenConns").IsValid() {
|
||||
reflect.ValueOf(p.db).MethodByName("SetMaxOpenConns").Call([]reflect.Value{reflect.ValueOf(conns)})
|
||||
}
|
||||
//p.db.SetMaxOpenConns(conns)
|
||||
}
|
||||
|
||||
// not implemented
|
||||
func (p *SysConnectPool) MaxConns() int {
|
||||
return p.maxConns
|
||||
return p.maxConns
|
||||
}
|
||||
|
||||
// NewSimpleConnectPool new a SimpleConnectPool
|
||||
func NewSimpleConnectPool() IConnectPool {
|
||||
return &SimpleConnectPool{releasedConnects: make([]*sql.DB, 10),
|
||||
usingConnects: map[*sql.DB]time.Time{},
|
||||
cur: -1,
|
||||
maxWaitTimeOut: 14400,
|
||||
maxIdleConns: 10,
|
||||
mutex: &sync.Mutex{},
|
||||
}
|
||||
return &SimpleConnectPool{releasedConnects: make([]*sql.DB, 10),
|
||||
usingConnects: map[*sql.DB]time.Time{},
|
||||
cur: -1,
|
||||
maxWaitTimeOut: 14400,
|
||||
maxIdleConns: 10,
|
||||
mutex: &sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
// Struct SimpleConnectPool is a simple implementation for IConnectPool.
|
||||
|
|
@ -205,75 +205,75 @@ func NewSimpleConnectPool() IConnectPool {
|
|||
// Opening or Closing a database connection must be enter a lock.
|
||||
// This implements will be improved in furture.
|
||||
type SimpleConnectPool struct {
|
||||
releasedConnects []*sql.DB
|
||||
cur int
|
||||
usingConnects map[*sql.DB]time.Time
|
||||
maxWaitTimeOut int
|
||||
mutex *sync.Mutex
|
||||
maxIdleConns int
|
||||
releasedConnects []*sql.DB
|
||||
cur int
|
||||
usingConnects map[*sql.DB]time.Time
|
||||
maxWaitTimeOut int
|
||||
mutex *sync.Mutex
|
||||
maxIdleConns int
|
||||
}
|
||||
|
||||
func (s *SimpleConnectPool) Init(engine *Engine) error {
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// RetrieveDB get a connection from connection pool
|
||||
func (p *SimpleConnectPool) RetrieveDB(engine *Engine) (*sql.DB, error) {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
var db *sql.DB = nil
|
||||
var err error = nil
|
||||
//fmt.Printf("%x, rbegin - released:%v, using:%v\n", &p, p.cur+1, len(p.usingConnects))
|
||||
if p.cur < 0 {
|
||||
db, err = engine.OpenDB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.usingConnects[db] = time.Now()
|
||||
} else {
|
||||
db = p.releasedConnects[p.cur]
|
||||
p.usingConnects[db] = time.Now()
|
||||
p.releasedConnects[p.cur] = nil
|
||||
p.cur = p.cur - 1
|
||||
}
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
var db *sql.DB = nil
|
||||
var err error = nil
|
||||
//fmt.Printf("%x, rbegin - released:%v, using:%v\n", &p, p.cur+1, len(p.usingConnects))
|
||||
if p.cur < 0 {
|
||||
db, err = engine.OpenDB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.usingConnects[db] = time.Now()
|
||||
} else {
|
||||
db = p.releasedConnects[p.cur]
|
||||
p.usingConnects[db] = time.Now()
|
||||
p.releasedConnects[p.cur] = nil
|
||||
p.cur = p.cur - 1
|
||||
}
|
||||
|
||||
//fmt.Printf("%x, rend - released:%v, using:%v\n", &p, p.cur+1, len(p.usingConnects))
|
||||
return db, nil
|
||||
//fmt.Printf("%x, rend - released:%v, using:%v\n", &p, p.cur+1, len(p.usingConnects))
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// ReleaseDB release a db from connection pool
|
||||
func (p *SimpleConnectPool) ReleaseDB(engine *Engine, db *sql.DB) {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
//fmt.Printf("%x, lbegin - released:%v, using:%v\n", &p, p.cur+1, len(p.usingConnects))
|
||||
if p.cur >= p.maxIdleConns-1 {
|
||||
db.Close()
|
||||
} else {
|
||||
p.cur = p.cur + 1
|
||||
p.releasedConnects[p.cur] = db
|
||||
}
|
||||
delete(p.usingConnects, db)
|
||||
//fmt.Printf("%x, lend - released:%v, using:%v\n", &p, p.cur+1, len(p.usingConnects))
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
//fmt.Printf("%x, lbegin - released:%v, using:%v\n", &p, p.cur+1, len(p.usingConnects))
|
||||
if p.cur >= p.maxIdleConns-1 {
|
||||
db.Close()
|
||||
} else {
|
||||
p.cur = p.cur + 1
|
||||
p.releasedConnects[p.cur] = db
|
||||
}
|
||||
delete(p.usingConnects, db)
|
||||
//fmt.Printf("%x, lend - released:%v, using:%v\n", &p, p.cur+1, len(p.usingConnects))
|
||||
}
|
||||
|
||||
// Close release all db
|
||||
func (p *SimpleConnectPool) Close(engine *Engine) error {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
for len(p.releasedConnects) > 0 {
|
||||
p.releasedConnects[0].Close()
|
||||
p.releasedConnects = p.releasedConnects[1:]
|
||||
}
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
for len(p.releasedConnects) > 0 {
|
||||
p.releasedConnects[0].Close()
|
||||
p.releasedConnects = p.releasedConnects[1:]
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *SimpleConnectPool) SetMaxIdleConns(conns int) {
|
||||
p.maxIdleConns = conns
|
||||
p.maxIdleConns = conns
|
||||
}
|
||||
|
||||
func (p *SimpleConnectPool) MaxIdleConns() int {
|
||||
return p.maxIdleConns
|
||||
return p.maxIdleConns
|
||||
}
|
||||
|
||||
// not implemented
|
||||
|
|
@ -282,5 +282,5 @@ func (p *SimpleConnectPool) SetMaxConns(conns int) {
|
|||
|
||||
// not implemented
|
||||
func (p *SimpleConnectPool) MaxConns() int {
|
||||
return -1
|
||||
return -1
|
||||
}
|
||||
|
|
|
|||
450
postgres.go
450
postgres.go
|
|
@ -1,300 +1,300 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type postgres struct {
|
||||
base
|
||||
dbname string
|
||||
base
|
||||
dbname string
|
||||
}
|
||||
|
||||
type values map[string]string
|
||||
|
||||
func (vs values) Set(k, v string) {
|
||||
vs[k] = v
|
||||
vs[k] = v
|
||||
}
|
||||
|
||||
func (vs values) Get(k string) (v string) {
|
||||
return vs[k]
|
||||
return vs[k]
|
||||
}
|
||||
|
||||
func errorf(s string, args ...interface{}) {
|
||||
panic(fmt.Errorf("pq: %s", fmt.Sprintf(s, args...)))
|
||||
panic(fmt.Errorf("pq: %s", fmt.Sprintf(s, args...)))
|
||||
}
|
||||
|
||||
func parseOpts(name string, o values) {
|
||||
if len(name) == 0 {
|
||||
return
|
||||
}
|
||||
if len(name) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
name = strings.TrimSpace(name)
|
||||
name = strings.TrimSpace(name)
|
||||
|
||||
ps := strings.Split(name, " ")
|
||||
for _, p := range ps {
|
||||
kv := strings.Split(p, "=")
|
||||
if len(kv) < 2 {
|
||||
errorf("invalid option: %q", p)
|
||||
}
|
||||
o.Set(kv[0], kv[1])
|
||||
}
|
||||
ps := strings.Split(name, " ")
|
||||
for _, p := range ps {
|
||||
kv := strings.Split(p, "=")
|
||||
if len(kv) < 2 {
|
||||
errorf("invalid option: %q", p)
|
||||
}
|
||||
o.Set(kv[0], kv[1])
|
||||
}
|
||||
}
|
||||
|
||||
func (db *postgres) Init(drivername, uri string) error {
|
||||
db.base.init(drivername, uri)
|
||||
db.base.init(drivername, uri)
|
||||
|
||||
o := make(values)
|
||||
parseOpts(uri, o)
|
||||
o := make(values)
|
||||
parseOpts(uri, o)
|
||||
|
||||
db.dbname = o.Get("dbname")
|
||||
if db.dbname == "" {
|
||||
return errors.New("dbname is empty")
|
||||
}
|
||||
return nil
|
||||
db.dbname = o.Get("dbname")
|
||||
if db.dbname == "" {
|
||||
return errors.New("dbname is empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *postgres) SqlType(c *Column) string {
|
||||
var res string
|
||||
switch t := c.SQLType.Name; t {
|
||||
case TinyInt:
|
||||
res = SmallInt
|
||||
case MediumInt, Int, Integer:
|
||||
return Integer
|
||||
case Serial, BigSerial:
|
||||
c.IsAutoIncrement = true
|
||||
c.Nullable = false
|
||||
res = t
|
||||
case Binary, VarBinary:
|
||||
return Bytea
|
||||
case DateTime:
|
||||
res = TimeStamp
|
||||
case TimeStampz:
|
||||
return "timestamp with time zone"
|
||||
case Float:
|
||||
res = Real
|
||||
case TinyText, MediumText, LongText:
|
||||
res = Text
|
||||
case Blob, TinyBlob, MediumBlob, LongBlob:
|
||||
return Bytea
|
||||
case Double:
|
||||
return "DOUBLE PRECISION"
|
||||
default:
|
||||
if c.IsAutoIncrement {
|
||||
return Serial
|
||||
}
|
||||
res = t
|
||||
}
|
||||
var res string
|
||||
switch t := c.SQLType.Name; t {
|
||||
case TinyInt:
|
||||
res = SmallInt
|
||||
case MediumInt, Int, Integer:
|
||||
return Integer
|
||||
case Serial, BigSerial:
|
||||
c.IsAutoIncrement = true
|
||||
c.Nullable = false
|
||||
res = t
|
||||
case Binary, VarBinary:
|
||||
return Bytea
|
||||
case DateTime:
|
||||
res = TimeStamp
|
||||
case TimeStampz:
|
||||
return "timestamp with time zone"
|
||||
case Float:
|
||||
res = Real
|
||||
case TinyText, MediumText, LongText:
|
||||
res = Text
|
||||
case Blob, TinyBlob, MediumBlob, LongBlob:
|
||||
return Bytea
|
||||
case Double:
|
||||
return "DOUBLE PRECISION"
|
||||
default:
|
||||
if c.IsAutoIncrement {
|
||||
return Serial
|
||||
}
|
||||
res = t
|
||||
}
|
||||
|
||||
var hasLen1 bool = (c.Length > 0)
|
||||
var hasLen2 bool = (c.Length2 > 0)
|
||||
if hasLen1 {
|
||||
res += "(" + strconv.Itoa(c.Length) + ")"
|
||||
} else if hasLen2 {
|
||||
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
|
||||
}
|
||||
return res
|
||||
var hasLen1 bool = (c.Length > 0)
|
||||
var hasLen2 bool = (c.Length2 > 0)
|
||||
if hasLen1 {
|
||||
res += "(" + strconv.Itoa(c.Length) + ")"
|
||||
} else if hasLen2 {
|
||||
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (db *postgres) SupportInsertMany() bool {
|
||||
return true
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *postgres) QuoteStr() string {
|
||||
return "\""
|
||||
return "\""
|
||||
}
|
||||
|
||||
func (db *postgres) AutoIncrStr() string {
|
||||
return ""
|
||||
return ""
|
||||
}
|
||||
|
||||
func (db *postgres) SupportEngine() bool {
|
||||
return false
|
||||
return false
|
||||
}
|
||||
|
||||
func (db *postgres) SupportCharset() bool {
|
||||
return false
|
||||
return false
|
||||
}
|
||||
|
||||
func (db *postgres) IndexOnTable() bool {
|
||||
return false
|
||||
return false
|
||||
}
|
||||
|
||||
func (db *postgres) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
|
||||
args := []interface{}{tableName, idxName}
|
||||
return `SELECT indexname FROM pg_indexes ` +
|
||||
`WHERE tablename = ? AND indexname = ?`, args
|
||||
args := []interface{}{tableName, idxName}
|
||||
return `SELECT indexname FROM pg_indexes ` +
|
||||
`WHERE tablename = ? AND indexname = ?`, args
|
||||
}
|
||||
|
||||
func (db *postgres) TableCheckSql(tableName string) (string, []interface{}) {
|
||||
args := []interface{}{tableName}
|
||||
return `SELECT tablename FROM pg_tables WHERE tablename = ?`, args
|
||||
args := []interface{}{tableName}
|
||||
return `SELECT tablename FROM pg_tables WHERE tablename = ?`, args
|
||||
}
|
||||
|
||||
func (db *postgres) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||
args := []interface{}{tableName, colName}
|
||||
return "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ?" +
|
||||
" AND column_name = ?", args
|
||||
args := []interface{}{tableName, colName}
|
||||
return "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ?" +
|
||||
" AND column_name = ?", args
|
||||
}
|
||||
|
||||
func (db *postgres) GetColumns(tableName string) ([]string, map[string]*Column, error) {
|
||||
args := []interface{}{tableName}
|
||||
s := "SELECT column_name, column_default, is_nullable, data_type, character_maximum_length" +
|
||||
", numeric_precision, numeric_precision_radix FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = $1"
|
||||
args := []interface{}{tableName}
|
||||
s := "SELECT column_name, column_default, is_nullable, data_type, character_maximum_length" +
|
||||
", numeric_precision, numeric_precision_radix FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = $1"
|
||||
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
cols := make(map[string]*Column)
|
||||
colSeq := make([]string, 0)
|
||||
for _, record := range res {
|
||||
col := new(Column)
|
||||
col.Indexes = make(map[string]bool)
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "column_name":
|
||||
col.Name = strings.Trim(string(content), `" `)
|
||||
case "column_default":
|
||||
if strings.HasPrefix(string(content), "nextval") {
|
||||
col.IsPrimaryKey = true
|
||||
} else {
|
||||
col.Default = string(content)
|
||||
}
|
||||
case "is_nullable":
|
||||
if string(content) == "YES" {
|
||||
col.Nullable = true
|
||||
} else {
|
||||
col.Nullable = false
|
||||
}
|
||||
case "data_type":
|
||||
ct := string(content)
|
||||
switch ct {
|
||||
case "character varying", "character":
|
||||
col.SQLType = SQLType{Varchar, 0, 0}
|
||||
case "timestamp without time zone":
|
||||
col.SQLType = SQLType{DateTime, 0, 0}
|
||||
case "timestamp with time zone":
|
||||
col.SQLType = SQLType{TimeStampz, 0, 0}
|
||||
case "double precision":
|
||||
col.SQLType = SQLType{Double, 0, 0}
|
||||
case "boolean":
|
||||
col.SQLType = SQLType{Bool, 0, 0}
|
||||
case "time without time zone":
|
||||
col.SQLType = SQLType{Time, 0, 0}
|
||||
default:
|
||||
col.SQLType = SQLType{strings.ToUpper(ct), 0, 0}
|
||||
}
|
||||
if _, ok := sqlTypes[col.SQLType.Name]; !ok {
|
||||
return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", ct))
|
||||
}
|
||||
case "character_maximum_length":
|
||||
i, err := strconv.Atoi(string(content))
|
||||
if err != nil {
|
||||
return nil, nil, errors.New("retrieve length error")
|
||||
}
|
||||
col.Length = i
|
||||
case "numeric_precision":
|
||||
case "numeric_precision_radix":
|
||||
}
|
||||
}
|
||||
if col.SQLType.IsText() {
|
||||
if col.Default != "" {
|
||||
col.Default = "'" + col.Default + "'"
|
||||
}
|
||||
}
|
||||
cols[col.Name] = col
|
||||
colSeq = append(colSeq, col.Name)
|
||||
}
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
cols := make(map[string]*Column)
|
||||
colSeq := make([]string, 0)
|
||||
for _, record := range res {
|
||||
col := new(Column)
|
||||
col.Indexes = make(map[string]bool)
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "column_name":
|
||||
col.Name = strings.Trim(string(content), `" `)
|
||||
case "column_default":
|
||||
if strings.HasPrefix(string(content), "nextval") {
|
||||
col.IsPrimaryKey = true
|
||||
} else {
|
||||
col.Default = string(content)
|
||||
}
|
||||
case "is_nullable":
|
||||
if string(content) == "YES" {
|
||||
col.Nullable = true
|
||||
} else {
|
||||
col.Nullable = false
|
||||
}
|
||||
case "data_type":
|
||||
ct := string(content)
|
||||
switch ct {
|
||||
case "character varying", "character":
|
||||
col.SQLType = SQLType{Varchar, 0, 0}
|
||||
case "timestamp without time zone":
|
||||
col.SQLType = SQLType{DateTime, 0, 0}
|
||||
case "timestamp with time zone":
|
||||
col.SQLType = SQLType{TimeStampz, 0, 0}
|
||||
case "double precision":
|
||||
col.SQLType = SQLType{Double, 0, 0}
|
||||
case "boolean":
|
||||
col.SQLType = SQLType{Bool, 0, 0}
|
||||
case "time without time zone":
|
||||
col.SQLType = SQLType{Time, 0, 0}
|
||||
default:
|
||||
col.SQLType = SQLType{strings.ToUpper(ct), 0, 0}
|
||||
}
|
||||
if _, ok := sqlTypes[col.SQLType.Name]; !ok {
|
||||
return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", ct))
|
||||
}
|
||||
case "character_maximum_length":
|
||||
i, err := strconv.Atoi(string(content))
|
||||
if err != nil {
|
||||
return nil, nil, errors.New("retrieve length error")
|
||||
}
|
||||
col.Length = i
|
||||
case "numeric_precision":
|
||||
case "numeric_precision_radix":
|
||||
}
|
||||
}
|
||||
if col.SQLType.IsText() {
|
||||
if col.Default != "" {
|
||||
col.Default = "'" + col.Default + "'"
|
||||
}
|
||||
}
|
||||
cols[col.Name] = col
|
||||
colSeq = append(colSeq, col.Name)
|
||||
}
|
||||
|
||||
return colSeq, cols, nil
|
||||
return colSeq, cols, nil
|
||||
}
|
||||
|
||||
func (db *postgres) GetTables() ([]*Table, error) {
|
||||
args := []interface{}{}
|
||||
s := "SELECT tablename FROM pg_tables where schemaname = 'public'"
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args := []interface{}{}
|
||||
s := "SELECT tablename FROM pg_tables where schemaname = 'public'"
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tables := make([]*Table, 0)
|
||||
for _, record := range res {
|
||||
table := new(Table)
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "tablename":
|
||||
table.Name = string(content)
|
||||
}
|
||||
}
|
||||
tables = append(tables, table)
|
||||
}
|
||||
return tables, nil
|
||||
tables := make([]*Table, 0)
|
||||
for _, record := range res {
|
||||
table := new(Table)
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "tablename":
|
||||
table.Name = string(content)
|
||||
}
|
||||
}
|
||||
tables = append(tables, table)
|
||||
}
|
||||
return tables, nil
|
||||
}
|
||||
|
||||
func (db *postgres) GetIndexes(tableName string) (map[string]*Index, error) {
|
||||
args := []interface{}{tableName}
|
||||
s := "SELECT tablename, indexname, indexdef FROM pg_indexes WHERE schemaname = 'public' and tablename = $1"
|
||||
args := []interface{}{tableName}
|
||||
s := "SELECT tablename, indexname, indexdef FROM pg_indexes WHERE schemaname = 'public' and tablename = $1"
|
||||
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
indexes := make(map[string]*Index, 0)
|
||||
for _, record := range res {
|
||||
var indexType int
|
||||
var indexName string
|
||||
var colNames []string
|
||||
indexes := make(map[string]*Index, 0)
|
||||
for _, record := range res {
|
||||
var indexType int
|
||||
var indexName string
|
||||
var colNames []string
|
||||
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "indexname":
|
||||
indexName = strings.Trim(string(content), `" `)
|
||||
case "indexdef":
|
||||
c := string(content)
|
||||
if strings.HasPrefix(c, "CREATE UNIQUE INDEX") {
|
||||
indexType = UniqueType
|
||||
} else {
|
||||
indexType = IndexType
|
||||
}
|
||||
cs := strings.Split(c, "(")
|
||||
colNames = strings.Split(cs[1][0:len(cs[1])-1], ",")
|
||||
}
|
||||
}
|
||||
if strings.HasSuffix(indexName, "_pkey") {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
||||
newIdxName := indexName[5+len(tableName) : len(indexName)]
|
||||
if newIdxName != "" {
|
||||
indexName = newIdxName
|
||||
}
|
||||
}
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "indexname":
|
||||
indexName = strings.Trim(string(content), `" `)
|
||||
case "indexdef":
|
||||
c := string(content)
|
||||
if strings.HasPrefix(c, "CREATE UNIQUE INDEX") {
|
||||
indexType = UniqueType
|
||||
} else {
|
||||
indexType = IndexType
|
||||
}
|
||||
cs := strings.Split(c, "(")
|
||||
colNames = strings.Split(cs[1][0:len(cs[1])-1], ",")
|
||||
}
|
||||
}
|
||||
if strings.HasSuffix(indexName, "_pkey") {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
||||
newIdxName := indexName[5+len(tableName) : len(indexName)]
|
||||
if newIdxName != "" {
|
||||
indexName = newIdxName
|
||||
}
|
||||
}
|
||||
|
||||
index := &Index{Name: indexName, Type: indexType, Cols: make([]string, 0)}
|
||||
for _, colName := range colNames {
|
||||
index.Cols = append(index.Cols, strings.Trim(colName, `" `))
|
||||
}
|
||||
indexes[index.Name] = index
|
||||
}
|
||||
return indexes, nil
|
||||
index := &Index{Name: indexName, Type: indexType, Cols: make([]string, 0)}
|
||||
for _, colName := range colNames {
|
||||
index.Cols = append(index.Cols, strings.Trim(colName, `" `))
|
||||
}
|
||||
indexes[index.Name] = index
|
||||
}
|
||||
return indexes, nil
|
||||
}
|
||||
|
|
|
|||
398
postgres_test.go
398
postgres_test.go
|
|
@ -1,259 +1,259 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
//"fmt"
|
||||
//_ "github.com/bylevel/pq"
|
||||
_ "github.com/lib/pq"
|
||||
"testing"
|
||||
//"fmt"
|
||||
//_ "github.com/bylevel/pq"
|
||||
_ "github.com/lib/pq"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newPostgresEngine() (*Engine, error) {
|
||||
return NewEngine("postgres", "dbname=xorm_test sslmode=disable")
|
||||
return NewEngine("postgres", "dbname=xorm_test sslmode=disable")
|
||||
}
|
||||
|
||||
func TestPostgres(t *testing.T) {
|
||||
engine, err := newPostgresEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
engine, err := newPostgresEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
testAll3(engine, t)
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
testAll3(engine, t)
|
||||
}
|
||||
|
||||
func TestPostgresWithCache(t *testing.T) {
|
||||
engine, err := newPostgresEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
defer engine.Close()
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
engine, err := newPostgresEngine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
defer engine.Close()
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
}
|
||||
|
||||
/*
|
||||
func TestPostgres2(t *testing.T) {
|
||||
engine, err := NewEngine("postgres", "dbname=xorm_test sslmode=disable")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.Mapper = SameMapper{}
|
||||
engine, err := NewEngine("postgres", "dbname=xorm_test sslmode=disable")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.Mapper = SameMapper{}
|
||||
|
||||
fmt.Println("-------------- directCreateTable --------------")
|
||||
directCreateTable(engine, t)
|
||||
fmt.Println("-------------- mapper --------------")
|
||||
mapper(engine, t)
|
||||
fmt.Println("-------------- insert --------------")
|
||||
insert(engine, t)
|
||||
fmt.Println("-------------- querySameMapper --------------")
|
||||
querySameMapper(engine, t)
|
||||
fmt.Println("-------------- execSameMapper --------------")
|
||||
execSameMapper(engine, t)
|
||||
fmt.Println("-------------- insertAutoIncr --------------")
|
||||
insertAutoIncr(engine, t)
|
||||
fmt.Println("-------------- insertMulti --------------")
|
||||
insertMulti(engine, t)
|
||||
fmt.Println("-------------- insertTwoTable --------------")
|
||||
insertTwoTable(engine, t)
|
||||
fmt.Println("-------------- updateSameMapper --------------")
|
||||
updateSameMapper(engine, t)
|
||||
fmt.Println("-------------- testdelete --------------")
|
||||
testdelete(engine, t)
|
||||
fmt.Println("-------------- get --------------")
|
||||
get(engine, t)
|
||||
fmt.Println("-------------- cascadeGet --------------")
|
||||
cascadeGet(engine, t)
|
||||
fmt.Println("-------------- find --------------")
|
||||
find(engine, t)
|
||||
fmt.Println("-------------- find2 --------------")
|
||||
find2(engine, t)
|
||||
fmt.Println("-------------- findMap --------------")
|
||||
findMap(engine, t)
|
||||
fmt.Println("-------------- findMap2 --------------")
|
||||
findMap2(engine, t)
|
||||
fmt.Println("-------------- count --------------")
|
||||
count(engine, t)
|
||||
fmt.Println("-------------- where --------------")
|
||||
where(engine, t)
|
||||
fmt.Println("-------------- in --------------")
|
||||
in(engine, t)
|
||||
fmt.Println("-------------- limit --------------")
|
||||
limit(engine, t)
|
||||
fmt.Println("-------------- orderSameMapper --------------")
|
||||
orderSameMapper(engine, t)
|
||||
fmt.Println("-------------- joinSameMapper --------------")
|
||||
joinSameMapper(engine, t)
|
||||
fmt.Println("-------------- havingSameMapper --------------")
|
||||
havingSameMapper(engine, t)
|
||||
fmt.Println("-------------- combineTransactionSameMapper --------------")
|
||||
combineTransactionSameMapper(engine, t)
|
||||
fmt.Println("-------------- table --------------")
|
||||
table(engine, t)
|
||||
fmt.Println("-------------- createMultiTables --------------")
|
||||
createMultiTables(engine, t)
|
||||
fmt.Println("-------------- tableOp --------------")
|
||||
tableOp(engine, t)
|
||||
fmt.Println("-------------- testColsSameMapper --------------")
|
||||
testColsSameMapper(engine, t)
|
||||
fmt.Println("-------------- testCharst --------------")
|
||||
testCharst(engine, t)
|
||||
fmt.Println("-------------- testStoreEngine --------------")
|
||||
testStoreEngine(engine, t)
|
||||
fmt.Println("-------------- testExtends --------------")
|
||||
testExtends(engine, t)
|
||||
fmt.Println("-------------- testColTypes --------------")
|
||||
testColTypes(engine, t)
|
||||
fmt.Println("-------------- testCustomType --------------")
|
||||
testCustomType(engine, t)
|
||||
fmt.Println("-------------- testCreatedAndUpdated --------------")
|
||||
testCreatedAndUpdated(engine, t)
|
||||
fmt.Println("-------------- testIndexAndUnique --------------")
|
||||
testIndexAndUnique(engine, t)
|
||||
fmt.Println("-------------- testMetaInfo --------------")
|
||||
testMetaInfo(engine, t)
|
||||
fmt.Println("-------------- testIterate --------------")
|
||||
testIterate(engine, t)
|
||||
fmt.Println("-------------- testStrangeName --------------")
|
||||
testStrangeName(engine, t)
|
||||
fmt.Println("-------------- testVersion --------------")
|
||||
testVersion(engine, t)
|
||||
fmt.Println("-------------- testDistinct --------------")
|
||||
testDistinct(engine, t)
|
||||
fmt.Println("-------------- testUseBool --------------")
|
||||
testUseBool(engine, t)
|
||||
fmt.Println("-------------- transaction --------------")
|
||||
transaction(engine, t)
|
||||
fmt.Println("-------------- directCreateTable --------------")
|
||||
directCreateTable(engine, t)
|
||||
fmt.Println("-------------- mapper --------------")
|
||||
mapper(engine, t)
|
||||
fmt.Println("-------------- insert --------------")
|
||||
insert(engine, t)
|
||||
fmt.Println("-------------- querySameMapper --------------")
|
||||
querySameMapper(engine, t)
|
||||
fmt.Println("-------------- execSameMapper --------------")
|
||||
execSameMapper(engine, t)
|
||||
fmt.Println("-------------- insertAutoIncr --------------")
|
||||
insertAutoIncr(engine, t)
|
||||
fmt.Println("-------------- insertMulti --------------")
|
||||
insertMulti(engine, t)
|
||||
fmt.Println("-------------- insertTwoTable --------------")
|
||||
insertTwoTable(engine, t)
|
||||
fmt.Println("-------------- updateSameMapper --------------")
|
||||
updateSameMapper(engine, t)
|
||||
fmt.Println("-------------- testdelete --------------")
|
||||
testdelete(engine, t)
|
||||
fmt.Println("-------------- get --------------")
|
||||
get(engine, t)
|
||||
fmt.Println("-------------- cascadeGet --------------")
|
||||
cascadeGet(engine, t)
|
||||
fmt.Println("-------------- find --------------")
|
||||
find(engine, t)
|
||||
fmt.Println("-------------- find2 --------------")
|
||||
find2(engine, t)
|
||||
fmt.Println("-------------- findMap --------------")
|
||||
findMap(engine, t)
|
||||
fmt.Println("-------------- findMap2 --------------")
|
||||
findMap2(engine, t)
|
||||
fmt.Println("-------------- count --------------")
|
||||
count(engine, t)
|
||||
fmt.Println("-------------- where --------------")
|
||||
where(engine, t)
|
||||
fmt.Println("-------------- in --------------")
|
||||
in(engine, t)
|
||||
fmt.Println("-------------- limit --------------")
|
||||
limit(engine, t)
|
||||
fmt.Println("-------------- orderSameMapper --------------")
|
||||
orderSameMapper(engine, t)
|
||||
fmt.Println("-------------- joinSameMapper --------------")
|
||||
joinSameMapper(engine, t)
|
||||
fmt.Println("-------------- havingSameMapper --------------")
|
||||
havingSameMapper(engine, t)
|
||||
fmt.Println("-------------- combineTransactionSameMapper --------------")
|
||||
combineTransactionSameMapper(engine, t)
|
||||
fmt.Println("-------------- table --------------")
|
||||
table(engine, t)
|
||||
fmt.Println("-------------- createMultiTables --------------")
|
||||
createMultiTables(engine, t)
|
||||
fmt.Println("-------------- tableOp --------------")
|
||||
tableOp(engine, t)
|
||||
fmt.Println("-------------- testColsSameMapper --------------")
|
||||
testColsSameMapper(engine, t)
|
||||
fmt.Println("-------------- testCharst --------------")
|
||||
testCharst(engine, t)
|
||||
fmt.Println("-------------- testStoreEngine --------------")
|
||||
testStoreEngine(engine, t)
|
||||
fmt.Println("-------------- testExtends --------------")
|
||||
testExtends(engine, t)
|
||||
fmt.Println("-------------- testColTypes --------------")
|
||||
testColTypes(engine, t)
|
||||
fmt.Println("-------------- testCustomType --------------")
|
||||
testCustomType(engine, t)
|
||||
fmt.Println("-------------- testCreatedAndUpdated --------------")
|
||||
testCreatedAndUpdated(engine, t)
|
||||
fmt.Println("-------------- testIndexAndUnique --------------")
|
||||
testIndexAndUnique(engine, t)
|
||||
fmt.Println("-------------- testMetaInfo --------------")
|
||||
testMetaInfo(engine, t)
|
||||
fmt.Println("-------------- testIterate --------------")
|
||||
testIterate(engine, t)
|
||||
fmt.Println("-------------- testStrangeName --------------")
|
||||
testStrangeName(engine, t)
|
||||
fmt.Println("-------------- testVersion --------------")
|
||||
testVersion(engine, t)
|
||||
fmt.Println("-------------- testDistinct --------------")
|
||||
testDistinct(engine, t)
|
||||
fmt.Println("-------------- testUseBool --------------")
|
||||
testUseBool(engine, t)
|
||||
fmt.Println("-------------- transaction --------------")
|
||||
transaction(engine, t)
|
||||
}*/
|
||||
|
||||
/*
|
||||
func BenchmarkPostgresDriverInsert(t *testing.B) {
|
||||
t.StopTimer()
|
||||
engine, err := newPostgresEngine()
|
||||
t.StopTimer()
|
||||
engine, err := newPostgresEngine()
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
|
||||
db, err := engine.OpenDB()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
db, err := engine.OpenDB()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
doBenchDriverInsert(engine, db, t)
|
||||
doBenchDriverInsert(engine, db, t)
|
||||
}
|
||||
|
||||
func BenchmarkPostgresDriverFind(t *testing.B) {
|
||||
t.StopTimer()
|
||||
engine, err := newPostgresEngine()
|
||||
t.StopTimer()
|
||||
engine, err := newPostgresEngine()
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
|
||||
db, err := engine.OpenDB()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//defer db.Close()
|
||||
db, err := engine.OpenDB()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//defer db.Close()
|
||||
|
||||
doBenchDriverFind(engine, db, t)
|
||||
doBenchDriverFind(engine, db, t)
|
||||
}*/
|
||||
|
||||
func BenchmarkPostgresNoCacheInsert(t *testing.B) {
|
||||
engine, err := newPostgresEngine()
|
||||
engine, err := newPostgresEngine()
|
||||
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchInsert(engine, t)
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchInsert(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkPostgresNoCacheFind(t *testing.B) {
|
||||
engine, err := newPostgresEngine()
|
||||
engine, err := newPostgresEngine()
|
||||
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchFind(engine, t)
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchFind(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkPostgresNoCacheFindPtr(t *testing.B) {
|
||||
engine, err := newPostgresEngine()
|
||||
engine, err := newPostgresEngine()
|
||||
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchFindPtr(engine, t)
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchFindPtr(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkPostgresCacheInsert(t *testing.B) {
|
||||
engine, err := newPostgresEngine()
|
||||
engine, err := newPostgresEngine()
|
||||
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
|
||||
doBenchInsert(engine, t)
|
||||
doBenchInsert(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkPostgresCacheFind(t *testing.B) {
|
||||
engine, err := newPostgresEngine()
|
||||
engine, err := newPostgresEngine()
|
||||
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
|
||||
doBenchFind(engine, t)
|
||||
doBenchFind(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkPostgresCacheFindPtr(t *testing.B) {
|
||||
engine, err := newPostgresEngine()
|
||||
engine, err := newPostgresEngine()
|
||||
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
|
||||
doBenchFindPtr(engine, t)
|
||||
doBenchFindPtr(engine, t)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,38 +2,38 @@ package xorm
|
|||
|
||||
// Executed before an object is initially persisted to the database
|
||||
type BeforeInsertProcessor interface {
|
||||
BeforeInsert()
|
||||
BeforeInsert()
|
||||
}
|
||||
|
||||
// Executed before an object is updated
|
||||
type BeforeUpdateProcessor interface {
|
||||
BeforeUpdate()
|
||||
BeforeUpdate()
|
||||
}
|
||||
|
||||
// Executed before an object is deleted
|
||||
type BeforeDeleteProcessor interface {
|
||||
BeforeDelete()
|
||||
BeforeDelete()
|
||||
}
|
||||
|
||||
// !nashtsai! TODO enable BeforeValidateProcessor when xorm start to support validations
|
||||
//// Executed before an object is validated
|
||||
//type BeforeValidateProcessor interface {
|
||||
// BeforeValidate()
|
||||
// BeforeValidate()
|
||||
//}
|
||||
// --
|
||||
|
||||
// Executed after an object is persisted to the database
|
||||
type AfterInsertProcessor interface {
|
||||
AfterInsert()
|
||||
AfterInsert()
|
||||
}
|
||||
|
||||
// Executed after an object has been updated
|
||||
type AfterUpdateProcessor interface {
|
||||
AfterUpdate()
|
||||
AfterUpdate()
|
||||
}
|
||||
|
||||
// Executed after an object has been deleted
|
||||
type AfterDeleteProcessor interface {
|
||||
AfterDelete()
|
||||
AfterDelete()
|
||||
}
|
||||
|
||||
|
|
|
|||
4430
session.go
4430
session.go
File diff suppressed because it is too large
Load Diff
328
sqlite3.go
328
sqlite3.go
|
|
@ -1,223 +1,223 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
"database/sql"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type sqlite3 struct {
|
||||
base
|
||||
base
|
||||
}
|
||||
|
||||
func (db *sqlite3) Init(drivername, dataSourceName string) error {
|
||||
db.base.init(drivername, dataSourceName)
|
||||
return nil
|
||||
db.base.init(drivername, dataSourceName)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *sqlite3) SqlType(c *Column) string {
|
||||
switch t := c.SQLType.Name; t {
|
||||
case Date, DateTime, TimeStamp, Time:
|
||||
return Numeric
|
||||
case TimeStampz:
|
||||
return Text
|
||||
case Char, Varchar, TinyText, Text, MediumText, LongText:
|
||||
return Text
|
||||
case Bit, TinyInt, SmallInt, MediumInt, Int, Integer, BigInt, Bool:
|
||||
return Integer
|
||||
case Float, Double, Real:
|
||||
return Real
|
||||
case Decimal, Numeric:
|
||||
return Numeric
|
||||
case TinyBlob, Blob, MediumBlob, LongBlob, Bytea, Binary, VarBinary:
|
||||
return Blob
|
||||
case Serial, BigSerial:
|
||||
c.IsPrimaryKey = true
|
||||
c.IsAutoIncrement = true
|
||||
c.Nullable = false
|
||||
return Integer
|
||||
default:
|
||||
return t
|
||||
}
|
||||
switch t := c.SQLType.Name; t {
|
||||
case Date, DateTime, TimeStamp, Time:
|
||||
return Numeric
|
||||
case TimeStampz:
|
||||
return Text
|
||||
case Char, Varchar, TinyText, Text, MediumText, LongText:
|
||||
return Text
|
||||
case Bit, TinyInt, SmallInt, MediumInt, Int, Integer, BigInt, Bool:
|
||||
return Integer
|
||||
case Float, Double, Real:
|
||||
return Real
|
||||
case Decimal, Numeric:
|
||||
return Numeric
|
||||
case TinyBlob, Blob, MediumBlob, LongBlob, Bytea, Binary, VarBinary:
|
||||
return Blob
|
||||
case Serial, BigSerial:
|
||||
c.IsPrimaryKey = true
|
||||
c.IsAutoIncrement = true
|
||||
c.Nullable = false
|
||||
return Integer
|
||||
default:
|
||||
return t
|
||||
}
|
||||
}
|
||||
|
||||
func (db *sqlite3) SupportInsertMany() bool {
|
||||
return true
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *sqlite3) QuoteStr() string {
|
||||
return "`"
|
||||
return "`"
|
||||
}
|
||||
|
||||
func (db *sqlite3) AutoIncrStr() string {
|
||||
return "AUTOINCREMENT"
|
||||
return "AUTOINCREMENT"
|
||||
}
|
||||
|
||||
func (db *sqlite3) SupportEngine() bool {
|
||||
return false
|
||||
return false
|
||||
}
|
||||
|
||||
func (db *sqlite3) SupportCharset() bool {
|
||||
return false
|
||||
return false
|
||||
}
|
||||
|
||||
func (db *sqlite3) IndexOnTable() bool {
|
||||
return false
|
||||
return false
|
||||
}
|
||||
|
||||
func (db *sqlite3) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
|
||||
args := []interface{}{idxName}
|
||||
return "SELECT name FROM sqlite_master WHERE type='index' and name = ?", args
|
||||
args := []interface{}{idxName}
|
||||
return "SELECT name FROM sqlite_master WHERE type='index' and name = ?", args
|
||||
}
|
||||
|
||||
func (db *sqlite3) TableCheckSql(tableName string) (string, []interface{}) {
|
||||
args := []interface{}{tableName}
|
||||
return "SELECT name FROM sqlite_master WHERE type='table' and name = ?", args
|
||||
args := []interface{}{tableName}
|
||||
return "SELECT name FROM sqlite_master WHERE type='table' and name = ?", args
|
||||
}
|
||||
|
||||
func (db *sqlite3) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||
args := []interface{}{tableName}
|
||||
sql := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + colName + "`%') or (sql like '%[" + colName + "]%'))"
|
||||
return sql, args
|
||||
args := []interface{}{tableName}
|
||||
sql := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + colName + "`%') or (sql like '%[" + colName + "]%'))"
|
||||
return sql, args
|
||||
}
|
||||
|
||||
func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*Column, error) {
|
||||
args := []interface{}{tableName}
|
||||
s := "SELECT sql FROM sqlite_master WHERE type='table' and name = ?"
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
args := []interface{}{tableName}
|
||||
s := "SELECT sql FROM sqlite_master WHERE type='table' and name = ?"
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var sql string
|
||||
for _, record := range res {
|
||||
for name, content := range record {
|
||||
if name == "sql" {
|
||||
sql = string(content)
|
||||
}
|
||||
}
|
||||
}
|
||||
var sql string
|
||||
for _, record := range res {
|
||||
for name, content := range record {
|
||||
if name == "sql" {
|
||||
sql = string(content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nStart := strings.Index(sql, "(")
|
||||
nEnd := strings.Index(sql, ")")
|
||||
colCreates := strings.Split(sql[nStart+1:nEnd], ",")
|
||||
cols := make(map[string]*Column)
|
||||
colSeq := make([]string, 0)
|
||||
for _, colStr := range colCreates {
|
||||
fields := strings.Fields(strings.TrimSpace(colStr))
|
||||
col := new(Column)
|
||||
col.Indexes = make(map[string]bool)
|
||||
col.Nullable = true
|
||||
for idx, field := range fields {
|
||||
if idx == 0 {
|
||||
col.Name = strings.Trim(field, "`[] ")
|
||||
continue
|
||||
} else if idx == 1 {
|
||||
col.SQLType = SQLType{field, 0, 0}
|
||||
}
|
||||
switch field {
|
||||
case "PRIMARY":
|
||||
col.IsPrimaryKey = true
|
||||
case "AUTOINCREMENT":
|
||||
col.IsAutoIncrement = true
|
||||
case "NULL":
|
||||
if fields[idx-1] == "NOT" {
|
||||
col.Nullable = false
|
||||
} else {
|
||||
col.Nullable = true
|
||||
}
|
||||
}
|
||||
}
|
||||
cols[col.Name] = col
|
||||
colSeq = append(colSeq, col.Name)
|
||||
}
|
||||
return colSeq, cols, nil
|
||||
nStart := strings.Index(sql, "(")
|
||||
nEnd := strings.Index(sql, ")")
|
||||
colCreates := strings.Split(sql[nStart+1:nEnd], ",")
|
||||
cols := make(map[string]*Column)
|
||||
colSeq := make([]string, 0)
|
||||
for _, colStr := range colCreates {
|
||||
fields := strings.Fields(strings.TrimSpace(colStr))
|
||||
col := new(Column)
|
||||
col.Indexes = make(map[string]bool)
|
||||
col.Nullable = true
|
||||
for idx, field := range fields {
|
||||
if idx == 0 {
|
||||
col.Name = strings.Trim(field, "`[] ")
|
||||
continue
|
||||
} else if idx == 1 {
|
||||
col.SQLType = SQLType{field, 0, 0}
|
||||
}
|
||||
switch field {
|
||||
case "PRIMARY":
|
||||
col.IsPrimaryKey = true
|
||||
case "AUTOINCREMENT":
|
||||
col.IsAutoIncrement = true
|
||||
case "NULL":
|
||||
if fields[idx-1] == "NOT" {
|
||||
col.Nullable = false
|
||||
} else {
|
||||
col.Nullable = true
|
||||
}
|
||||
}
|
||||
}
|
||||
cols[col.Name] = col
|
||||
colSeq = append(colSeq, col.Name)
|
||||
}
|
||||
return colSeq, cols, nil
|
||||
}
|
||||
|
||||
func (db *sqlite3) GetTables() ([]*Table, error) {
|
||||
args := []interface{}{}
|
||||
s := "SELECT name FROM sqlite_master WHERE type='table'"
|
||||
args := []interface{}{}
|
||||
s := "SELECT name FROM sqlite_master WHERE type='table'"
|
||||
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tables := make([]*Table, 0)
|
||||
for _, record := range res {
|
||||
table := new(Table)
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "name":
|
||||
table.Name = string(content)
|
||||
}
|
||||
}
|
||||
if table.Name == "sqlite_sequence" {
|
||||
continue
|
||||
}
|
||||
tables = append(tables, table)
|
||||
}
|
||||
return tables, nil
|
||||
tables := make([]*Table, 0)
|
||||
for _, record := range res {
|
||||
table := new(Table)
|
||||
for name, content := range record {
|
||||
switch name {
|
||||
case "name":
|
||||
table.Name = string(content)
|
||||
}
|
||||
}
|
||||
if table.Name == "sqlite_sequence" {
|
||||
continue
|
||||
}
|
||||
tables = append(tables, table)
|
||||
}
|
||||
return tables, nil
|
||||
}
|
||||
|
||||
func (db *sqlite3) GetIndexes(tableName string) (map[string]*Index, error) {
|
||||
args := []interface{}{tableName}
|
||||
s := "SELECT sql FROM sqlite_master WHERE type='index' and tbl_name = ?"
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args := []interface{}{tableName}
|
||||
s := "SELECT sql FROM sqlite_master WHERE type='index' and tbl_name = ?"
|
||||
cnn, err := sql.Open(db.drivername, db.dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cnn.Close()
|
||||
res, err := query(cnn, s, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
indexes := make(map[string]*Index, 0)
|
||||
for _, record := range res {
|
||||
var sql string
|
||||
index := new(Index)
|
||||
for name, content := range record {
|
||||
if name == "sql" {
|
||||
sql = string(content)
|
||||
}
|
||||
}
|
||||
indexes := make(map[string]*Index, 0)
|
||||
for _, record := range res {
|
||||
var sql string
|
||||
index := new(Index)
|
||||
for name, content := range record {
|
||||
if name == "sql" {
|
||||
sql = string(content)
|
||||
}
|
||||
}
|
||||
|
||||
nNStart := strings.Index(sql, "INDEX")
|
||||
nNEnd := strings.Index(sql, "ON")
|
||||
indexName := strings.Trim(sql[nNStart+6:nNEnd], "` []")
|
||||
//fmt.Println(indexName)
|
||||
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
||||
index.Name = indexName[5+len(tableName) : len(indexName)]
|
||||
} else {
|
||||
index.Name = indexName
|
||||
}
|
||||
nNStart := strings.Index(sql, "INDEX")
|
||||
nNEnd := strings.Index(sql, "ON")
|
||||
indexName := strings.Trim(sql[nNStart+6:nNEnd], "` []")
|
||||
//fmt.Println(indexName)
|
||||
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
||||
index.Name = indexName[5+len(tableName) : len(indexName)]
|
||||
} else {
|
||||
index.Name = indexName
|
||||
}
|
||||
|
||||
if strings.HasPrefix(sql, "CREATE UNIQUE INDEX") {
|
||||
index.Type = UniqueType
|
||||
} else {
|
||||
index.Type = IndexType
|
||||
}
|
||||
if strings.HasPrefix(sql, "CREATE UNIQUE INDEX") {
|
||||
index.Type = UniqueType
|
||||
} else {
|
||||
index.Type = IndexType
|
||||
}
|
||||
|
||||
nStart := strings.Index(sql, "(")
|
||||
nEnd := strings.Index(sql, ")")
|
||||
colIndexes := strings.Split(sql[nStart+1:nEnd], ",")
|
||||
nStart := strings.Index(sql, "(")
|
||||
nEnd := strings.Index(sql, ")")
|
||||
colIndexes := strings.Split(sql[nStart+1:nEnd], ",")
|
||||
|
||||
index.Cols = make([]string, 0)
|
||||
for _, col := range colIndexes {
|
||||
index.Cols = append(index.Cols, strings.Trim(col, "` []"))
|
||||
}
|
||||
indexes[index.Name] = index
|
||||
}
|
||||
index.Cols = make([]string, 0)
|
||||
for _, col := range colIndexes {
|
||||
index.Cols = append(index.Cols, strings.Trim(col, "` []"))
|
||||
}
|
||||
indexes[index.Name] = index
|
||||
}
|
||||
|
||||
return indexes, nil
|
||||
return indexes, nil
|
||||
}
|
||||
|
|
|
|||
296
sqlite3_test.go
296
sqlite3_test.go
|
|
@ -1,199 +1,199 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
//"database/sql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
"testing"
|
||||
//"database/sql"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newSqlite3Engine() (*Engine, error) {
|
||||
os.Remove("./test.db")
|
||||
return NewEngine("sqlite3", "./test.db")
|
||||
os.Remove("./test.db")
|
||||
return NewEngine("sqlite3", "./test.db")
|
||||
}
|
||||
|
||||
func TestSqlite3(t *testing.T) {
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
testAll3(engine, t)
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
testAll3(engine, t)
|
||||
}
|
||||
|
||||
func TestSqlite3WithCache(t *testing.T) {
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
engine.ShowSQL = showTestSql
|
||||
engine.ShowErr = showTestSql
|
||||
engine.ShowWarn = showTestSql
|
||||
engine.ShowDebug = showTestSql
|
||||
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
testAll(engine, t)
|
||||
testAll2(engine, t)
|
||||
}
|
||||
|
||||
/*func BenchmarkSqlite3DriverInsert(t *testing.B) {
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = engine.CreateTables(&BigStruct{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.Close()
|
||||
err = engine.CreateTables(&BigStruct{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.Close()
|
||||
|
||||
db, err := sql.Open("sqlite3", "./test.db")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
db, err := sql.Open("sqlite3", "./test.db")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
doBenchDriverInsertS(db, t)
|
||||
doBenchDriverInsertS(db, t)
|
||||
|
||||
db.Close()
|
||||
db.Close()
|
||||
|
||||
engine, err = newSqlite3Engine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine, err = newSqlite3Engine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = engine.DropTables(&BigStruct{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
err = engine.DropTables(&BigStruct{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
}
|
||||
|
||||
func BenchmarkSqlite3DriverFind(t *testing.B) {
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = engine.CreateTables(&BigStruct{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.Close()
|
||||
err = engine.CreateTables(&BigStruct{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.Close()
|
||||
|
||||
db, err := sql.Open("sqlite3", "./test.db")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
db, err := sql.Open("sqlite3", "./test.db")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
doBenchDriverFindS(db, t)
|
||||
doBenchDriverFindS(db, t)
|
||||
|
||||
db.Close()
|
||||
db.Close()
|
||||
|
||||
engine, err = newSqlite3Engine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine, err = newSqlite3Engine()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = engine.DropTables(&BigStruct{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
err = engine.DropTables(&BigStruct{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer engine.Close()
|
||||
}*/
|
||||
|
||||
func BenchmarkSqlite3NoCacheInsert(t *testing.B) {
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchInsert(engine, t)
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchInsert(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkSqlite3NoCacheFind(t *testing.B) {
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchFind(engine, t)
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchFind(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkSqlite3NoCacheFindPtr(t *testing.B) {
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchFindPtr(engine, t)
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
//engine.ShowSQL = true
|
||||
doBenchFindPtr(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkSqlite3CacheInsert(t *testing.B) {
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
doBenchInsert(engine, t)
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
doBenchInsert(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkSqlite3CacheFind(t *testing.B) {
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
doBenchFind(engine, t)
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
doBenchFind(engine, t)
|
||||
}
|
||||
|
||||
func BenchmarkSqlite3CacheFindPtr(t *testing.B) {
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
doBenchFindPtr(engine, t)
|
||||
t.StopTimer()
|
||||
engine, err := newSqlite3Engine()
|
||||
defer engine.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
engine.SetDefaultCacher(NewLRUCacher(NewMemoryStore(), 1000))
|
||||
doBenchFindPtr(engine, t)
|
||||
}
|
||||
|
|
|
|||
1104
statement.go
1104
statement.go
File diff suppressed because it is too large
Load Diff
590
table.go
590
table.go
|
|
@ -1,416 +1,416 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// xorm SQL types
|
||||
type SQLType struct {
|
||||
Name string
|
||||
DefaultLength int
|
||||
DefaultLength2 int
|
||||
Name string
|
||||
DefaultLength int
|
||||
DefaultLength2 int
|
||||
}
|
||||
|
||||
func (s *SQLType) IsText() bool {
|
||||
return s.Name == Char || s.Name == Varchar || s.Name == TinyText ||
|
||||
s.Name == Text || s.Name == MediumText || s.Name == LongText
|
||||
return s.Name == Char || s.Name == Varchar || s.Name == TinyText ||
|
||||
s.Name == Text || s.Name == MediumText || s.Name == LongText
|
||||
}
|
||||
|
||||
func (s *SQLType) IsBlob() bool {
|
||||
return (s.Name == TinyBlob) || (s.Name == Blob) ||
|
||||
s.Name == MediumBlob || s.Name == LongBlob ||
|
||||
s.Name == Binary || s.Name == VarBinary || s.Name == Bytea
|
||||
return (s.Name == TinyBlob) || (s.Name == Blob) ||
|
||||
s.Name == MediumBlob || s.Name == LongBlob ||
|
||||
s.Name == Binary || s.Name == VarBinary || s.Name == Bytea
|
||||
}
|
||||
|
||||
const ()
|
||||
|
||||
var (
|
||||
Bit = "BIT"
|
||||
TinyInt = "TINYINT"
|
||||
SmallInt = "SMALLINT"
|
||||
MediumInt = "MEDIUMINT"
|
||||
Int = "INT"
|
||||
Integer = "INTEGER"
|
||||
BigInt = "BIGINT"
|
||||
Bit = "BIT"
|
||||
TinyInt = "TINYINT"
|
||||
SmallInt = "SMALLINT"
|
||||
MediumInt = "MEDIUMINT"
|
||||
Int = "INT"
|
||||
Integer = "INTEGER"
|
||||
BigInt = "BIGINT"
|
||||
|
||||
Char = "CHAR"
|
||||
Varchar = "VARCHAR"
|
||||
TinyText = "TINYTEXT"
|
||||
Text = "TEXT"
|
||||
MediumText = "MEDIUMTEXT"
|
||||
LongText = "LONGTEXT"
|
||||
Binary = "BINARY"
|
||||
VarBinary = "VARBINARY"
|
||||
Char = "CHAR"
|
||||
Varchar = "VARCHAR"
|
||||
TinyText = "TINYTEXT"
|
||||
Text = "TEXT"
|
||||
MediumText = "MEDIUMTEXT"
|
||||
LongText = "LONGTEXT"
|
||||
Binary = "BINARY"
|
||||
VarBinary = "VARBINARY"
|
||||
|
||||
Date = "DATE"
|
||||
DateTime = "DATETIME"
|
||||
Time = "TIME"
|
||||
TimeStamp = "TIMESTAMP"
|
||||
TimeStampz = "TIMESTAMPZ"
|
||||
Date = "DATE"
|
||||
DateTime = "DATETIME"
|
||||
Time = "TIME"
|
||||
TimeStamp = "TIMESTAMP"
|
||||
TimeStampz = "TIMESTAMPZ"
|
||||
|
||||
Decimal = "DECIMAL"
|
||||
Numeric = "NUMERIC"
|
||||
Decimal = "DECIMAL"
|
||||
Numeric = "NUMERIC"
|
||||
|
||||
Real = "REAL"
|
||||
Float = "FLOAT"
|
||||
Double = "DOUBLE"
|
||||
Real = "REAL"
|
||||
Float = "FLOAT"
|
||||
Double = "DOUBLE"
|
||||
|
||||
TinyBlob = "TINYBLOB"
|
||||
Blob = "BLOB"
|
||||
MediumBlob = "MEDIUMBLOB"
|
||||
LongBlob = "LONGBLOB"
|
||||
Bytea = "BYTEA"
|
||||
TinyBlob = "TINYBLOB"
|
||||
Blob = "BLOB"
|
||||
MediumBlob = "MEDIUMBLOB"
|
||||
LongBlob = "LONGBLOB"
|
||||
Bytea = "BYTEA"
|
||||
|
||||
Bool = "BOOL"
|
||||
Bool = "BOOL"
|
||||
|
||||
Serial = "SERIAL"
|
||||
BigSerial = "BIGSERIAL"
|
||||
Serial = "SERIAL"
|
||||
BigSerial = "BIGSERIAL"
|
||||
|
||||
sqlTypes = map[string]bool{
|
||||
Bit: true,
|
||||
TinyInt: true,
|
||||
SmallInt: true,
|
||||
MediumInt: true,
|
||||
Int: true,
|
||||
Integer: true,
|
||||
BigInt: true,
|
||||
sqlTypes = map[string]bool{
|
||||
Bit: true,
|
||||
TinyInt: true,
|
||||
SmallInt: true,
|
||||
MediumInt: true,
|
||||
Int: true,
|
||||
Integer: true,
|
||||
BigInt: true,
|
||||
|
||||
Char: true,
|
||||
Varchar: true,
|
||||
TinyText: true,
|
||||
Text: true,
|
||||
MediumText: true,
|
||||
LongText: true,
|
||||
Binary: true,
|
||||
VarBinary: true,
|
||||
Char: true,
|
||||
Varchar: true,
|
||||
TinyText: true,
|
||||
Text: true,
|
||||
MediumText: true,
|
||||
LongText: true,
|
||||
Binary: true,
|
||||
VarBinary: true,
|
||||
|
||||
Date: true,
|
||||
DateTime: true,
|
||||
Time: true,
|
||||
TimeStamp: true,
|
||||
TimeStampz: true,
|
||||
Date: true,
|
||||
DateTime: true,
|
||||
Time: true,
|
||||
TimeStamp: true,
|
||||
TimeStampz: true,
|
||||
|
||||
Decimal: true,
|
||||
Numeric: true,
|
||||
Decimal: true,
|
||||
Numeric: true,
|
||||
|
||||
Real: true,
|
||||
Float: true,
|
||||
Double: true,
|
||||
TinyBlob: true,
|
||||
Blob: true,
|
||||
MediumBlob: true,
|
||||
LongBlob: true,
|
||||
Bytea: true,
|
||||
Real: true,
|
||||
Float: true,
|
||||
Double: true,
|
||||
TinyBlob: true,
|
||||
Blob: true,
|
||||
MediumBlob: true,
|
||||
LongBlob: true,
|
||||
Bytea: true,
|
||||
|
||||
Bool: true,
|
||||
Bool: true,
|
||||
|
||||
Serial: true,
|
||||
BigSerial: true,
|
||||
}
|
||||
Serial: true,
|
||||
BigSerial: true,
|
||||
}
|
||||
|
||||
intTypes = sort.StringSlice{"*int", "*int16", "*int32", "*int8"}
|
||||
uintTypes = sort.StringSlice{"*uint", "*uint16", "*uint32", "*uint8"}
|
||||
intTypes = sort.StringSlice{"*int", "*int16", "*int32", "*int8"}
|
||||
uintTypes = sort.StringSlice{"*uint", "*uint16", "*uint32", "*uint8"}
|
||||
)
|
||||
|
||||
var b byte
|
||||
var tm time.Time
|
||||
|
||||
func Type2SQLType(t reflect.Type) (st SQLType) {
|
||||
switch k := t.Kind(); k {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
|
||||
st = SQLType{Int, 0, 0}
|
||||
case reflect.Int64, reflect.Uint64:
|
||||
st = SQLType{BigInt, 0, 0}
|
||||
case reflect.Float32:
|
||||
st = SQLType{Float, 0, 0}
|
||||
case reflect.Float64:
|
||||
st = SQLType{Double, 0, 0}
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
st = SQLType{Varchar, 64, 0}
|
||||
case reflect.Array, reflect.Slice, reflect.Map:
|
||||
if t.Elem() == reflect.TypeOf(b) {
|
||||
st = SQLType{Blob, 0, 0}
|
||||
} else {
|
||||
st = SQLType{Text, 0, 0}
|
||||
}
|
||||
case reflect.Bool:
|
||||
st = SQLType{Bool, 0, 0}
|
||||
case reflect.String:
|
||||
st = SQLType{Varchar, 255, 0}
|
||||
case reflect.Struct:
|
||||
if t == reflect.TypeOf(tm) {
|
||||
st = SQLType{DateTime, 0, 0}
|
||||
} else {
|
||||
st = SQLType{Text, 0, 0}
|
||||
}
|
||||
case reflect.Ptr:
|
||||
st, _ = ptrType2SQLType(t)
|
||||
default:
|
||||
st = SQLType{Text, 0, 0}
|
||||
}
|
||||
return
|
||||
switch k := t.Kind(); k {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
|
||||
st = SQLType{Int, 0, 0}
|
||||
case reflect.Int64, reflect.Uint64:
|
||||
st = SQLType{BigInt, 0, 0}
|
||||
case reflect.Float32:
|
||||
st = SQLType{Float, 0, 0}
|
||||
case reflect.Float64:
|
||||
st = SQLType{Double, 0, 0}
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
st = SQLType{Varchar, 64, 0}
|
||||
case reflect.Array, reflect.Slice, reflect.Map:
|
||||
if t.Elem() == reflect.TypeOf(b) {
|
||||
st = SQLType{Blob, 0, 0}
|
||||
} else {
|
||||
st = SQLType{Text, 0, 0}
|
||||
}
|
||||
case reflect.Bool:
|
||||
st = SQLType{Bool, 0, 0}
|
||||
case reflect.String:
|
||||
st = SQLType{Varchar, 255, 0}
|
||||
case reflect.Struct:
|
||||
if t == reflect.TypeOf(tm) {
|
||||
st = SQLType{DateTime, 0, 0}
|
||||
} else {
|
||||
st = SQLType{Text, 0, 0}
|
||||
}
|
||||
case reflect.Ptr:
|
||||
st, _ = ptrType2SQLType(t)
|
||||
default:
|
||||
st = SQLType{Text, 0, 0}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ptrType2SQLType(t reflect.Type) (st SQLType, has bool) {
|
||||
typeStr := t.String()
|
||||
has = true
|
||||
typeStr := t.String()
|
||||
has = true
|
||||
|
||||
switch typeStr {
|
||||
case "*string":
|
||||
st = SQLType{Varchar, 255, 0}
|
||||
case "*bool":
|
||||
st = SQLType{Bool, 0, 0}
|
||||
case "*complex64", "*complex128":
|
||||
st = SQLType{Varchar, 64, 0}
|
||||
case "*float32":
|
||||
st = SQLType{Float, 0, 0}
|
||||
case "*float64":
|
||||
st = SQLType{Double, 0, 0}
|
||||
case "*int64", "*uint64":
|
||||
st = SQLType{BigInt, 0, 0}
|
||||
case "*time.Time":
|
||||
st = SQLType{DateTime, 0, 0}
|
||||
case "*int", "*int16", "*int32", "*int8", "*uint", "*uint16", "*uint32", "*uint8":
|
||||
st = SQLType{Int, 0, 0}
|
||||
default:
|
||||
has = false
|
||||
}
|
||||
return
|
||||
switch typeStr {
|
||||
case "*string":
|
||||
st = SQLType{Varchar, 255, 0}
|
||||
case "*bool":
|
||||
st = SQLType{Bool, 0, 0}
|
||||
case "*complex64", "*complex128":
|
||||
st = SQLType{Varchar, 64, 0}
|
||||
case "*float32":
|
||||
st = SQLType{Float, 0, 0}
|
||||
case "*float64":
|
||||
st = SQLType{Double, 0, 0}
|
||||
case "*int64", "*uint64":
|
||||
st = SQLType{BigInt, 0, 0}
|
||||
case "*time.Time":
|
||||
st = SQLType{DateTime, 0, 0}
|
||||
case "*int", "*int16", "*int32", "*int8", "*uint", "*uint16", "*uint32", "*uint8":
|
||||
st = SQLType{Int, 0, 0}
|
||||
default:
|
||||
has = false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// default sql type change to go types
|
||||
func SQLType2Type(st SQLType) reflect.Type {
|
||||
name := strings.ToUpper(st.Name)
|
||||
switch name {
|
||||
case Bit, TinyInt, SmallInt, MediumInt, Int, Integer, Serial:
|
||||
return reflect.TypeOf(1)
|
||||
case BigInt, BigSerial:
|
||||
return reflect.TypeOf(int64(1))
|
||||
case Float, Real:
|
||||
return reflect.TypeOf(float32(1))
|
||||
case Double:
|
||||
return reflect.TypeOf(float64(1))
|
||||
case Char, Varchar, TinyText, Text, MediumText, LongText:
|
||||
return reflect.TypeOf("")
|
||||
case TinyBlob, Blob, LongBlob, Bytea, Binary, MediumBlob, VarBinary:
|
||||
return reflect.TypeOf([]byte{})
|
||||
case Bool:
|
||||
return reflect.TypeOf(true)
|
||||
case DateTime, Date, Time, TimeStamp, TimeStampz:
|
||||
return reflect.TypeOf(tm)
|
||||
case Decimal, Numeric:
|
||||
return reflect.TypeOf("")
|
||||
default:
|
||||
return reflect.TypeOf("")
|
||||
}
|
||||
name := strings.ToUpper(st.Name)
|
||||
switch name {
|
||||
case Bit, TinyInt, SmallInt, MediumInt, Int, Integer, Serial:
|
||||
return reflect.TypeOf(1)
|
||||
case BigInt, BigSerial:
|
||||
return reflect.TypeOf(int64(1))
|
||||
case Float, Real:
|
||||
return reflect.TypeOf(float32(1))
|
||||
case Double:
|
||||
return reflect.TypeOf(float64(1))
|
||||
case Char, Varchar, TinyText, Text, MediumText, LongText:
|
||||
return reflect.TypeOf("")
|
||||
case TinyBlob, Blob, LongBlob, Bytea, Binary, MediumBlob, VarBinary:
|
||||
return reflect.TypeOf([]byte{})
|
||||
case Bool:
|
||||
return reflect.TypeOf(true)
|
||||
case DateTime, Date, Time, TimeStamp, TimeStampz:
|
||||
return reflect.TypeOf(tm)
|
||||
case Decimal, Numeric:
|
||||
return reflect.TypeOf("")
|
||||
default:
|
||||
return reflect.TypeOf("")
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
IndexType = iota + 1
|
||||
UniqueType
|
||||
IndexType = iota + 1
|
||||
UniqueType
|
||||
)
|
||||
|
||||
// database index
|
||||
type Index struct {
|
||||
Name string
|
||||
Type int
|
||||
Cols []string
|
||||
Name string
|
||||
Type int
|
||||
Cols []string
|
||||
}
|
||||
|
||||
// add columns which will be composite index
|
||||
func (index *Index) AddColumn(cols ...string) {
|
||||
for _, col := range cols {
|
||||
index.Cols = append(index.Cols, col)
|
||||
}
|
||||
for _, col := range cols {
|
||||
index.Cols = append(index.Cols, col)
|
||||
}
|
||||
}
|
||||
|
||||
// new an index
|
||||
func NewIndex(name string, indexType int) *Index {
|
||||
return &Index{name, indexType, make([]string, 0)}
|
||||
return &Index{name, indexType, make([]string, 0)}
|
||||
}
|
||||
|
||||
const (
|
||||
TWOSIDES = iota + 1
|
||||
ONLYTODB
|
||||
ONLYFROMDB
|
||||
TWOSIDES = iota + 1
|
||||
ONLYTODB
|
||||
ONLYFROMDB
|
||||
)
|
||||
|
||||
// database column
|
||||
type Column struct {
|
||||
Name string
|
||||
FieldName string
|
||||
SQLType SQLType
|
||||
Length int
|
||||
Length2 int
|
||||
Nullable bool
|
||||
Default string
|
||||
Indexes map[string]bool
|
||||
IsPrimaryKey bool
|
||||
IsAutoIncrement bool
|
||||
MapType int
|
||||
IsCreated bool
|
||||
IsUpdated bool
|
||||
IsCascade bool
|
||||
IsVersion bool
|
||||
Name string
|
||||
FieldName string
|
||||
SQLType SQLType
|
||||
Length int
|
||||
Length2 int
|
||||
Nullable bool
|
||||
Default string
|
||||
Indexes map[string]bool
|
||||
IsPrimaryKey bool
|
||||
IsAutoIncrement bool
|
||||
MapType int
|
||||
IsCreated bool
|
||||
IsUpdated bool
|
||||
IsCascade bool
|
||||
IsVersion bool
|
||||
}
|
||||
|
||||
// generate column description string according dialect
|
||||
func (col *Column) String(d dialect) string {
|
||||
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
|
||||
sql := d.QuoteStr() + col.Name + d.QuoteStr() + " "
|
||||
|
||||
sql += d.SqlType(col) + " "
|
||||
sql += d.SqlType(col) + " "
|
||||
|
||||
if col.IsPrimaryKey {
|
||||
sql += "PRIMARY KEY "
|
||||
}
|
||||
if col.IsPrimaryKey {
|
||||
sql += "PRIMARY KEY "
|
||||
}
|
||||
|
||||
if col.IsAutoIncrement {
|
||||
sql += d.AutoIncrStr() + " "
|
||||
}
|
||||
if col.IsAutoIncrement {
|
||||
sql += d.AutoIncrStr() + " "
|
||||
}
|
||||
|
||||
if col.Nullable {
|
||||
sql += "NULL "
|
||||
} else {
|
||||
sql += "NOT NULL "
|
||||
}
|
||||
if col.Nullable {
|
||||
sql += "NULL "
|
||||
} else {
|
||||
sql += "NOT NULL "
|
||||
}
|
||||
|
||||
if col.Default != "" {
|
||||
sql += "DEFAULT " + col.Default + " "
|
||||
}
|
||||
if col.Default != "" {
|
||||
sql += "DEFAULT " + col.Default + " "
|
||||
}
|
||||
|
||||
return sql
|
||||
return sql
|
||||
}
|
||||
|
||||
// return col's filed of struct's value
|
||||
func (col *Column) ValueOf(bean interface{}) reflect.Value {
|
||||
var fieldValue reflect.Value
|
||||
if strings.Contains(col.FieldName, ".") {
|
||||
fields := strings.Split(col.FieldName, ".")
|
||||
if len(fields) > 2 {
|
||||
return reflect.ValueOf(nil)
|
||||
}
|
||||
var fieldValue reflect.Value
|
||||
if strings.Contains(col.FieldName, ".") {
|
||||
fields := strings.Split(col.FieldName, ".")
|
||||
if len(fields) > 2 {
|
||||
return reflect.ValueOf(nil)
|
||||
}
|
||||
|
||||
fieldValue = reflect.Indirect(reflect.ValueOf(bean)).FieldByName(fields[0])
|
||||
fieldValue = fieldValue.FieldByName(fields[1])
|
||||
} else {
|
||||
fieldValue = reflect.Indirect(reflect.ValueOf(bean)).FieldByName(col.FieldName)
|
||||
}
|
||||
return fieldValue
|
||||
fieldValue = reflect.Indirect(reflect.ValueOf(bean)).FieldByName(fields[0])
|
||||
fieldValue = fieldValue.FieldByName(fields[1])
|
||||
} else {
|
||||
fieldValue = reflect.Indirect(reflect.ValueOf(bean)).FieldByName(col.FieldName)
|
||||
}
|
||||
return fieldValue
|
||||
}
|
||||
|
||||
// database table
|
||||
type Table struct {
|
||||
Name string
|
||||
Type reflect.Type
|
||||
ColumnsSeq []string
|
||||
Columns map[string]*Column
|
||||
Indexes map[string]*Index
|
||||
PrimaryKey string
|
||||
Created map[string]bool
|
||||
Updated string
|
||||
Version string
|
||||
Cacher Cacher
|
||||
Name string
|
||||
Type reflect.Type
|
||||
ColumnsSeq []string
|
||||
Columns map[string]*Column
|
||||
Indexes map[string]*Index
|
||||
PrimaryKey string
|
||||
Created map[string]bool
|
||||
Updated string
|
||||
Version string
|
||||
Cacher Cacher
|
||||
}
|
||||
|
||||
/*
|
||||
func NewTable(name string, t reflect.Type) *Table {
|
||||
return &Table{Name: name, Type: t,
|
||||
ColumnsSeq: make([]string, 0),
|
||||
Columns: make(map[string]*Column),
|
||||
Indexes: make(map[string]*Index),
|
||||
Created: make(map[string]bool),
|
||||
}
|
||||
return &Table{Name: name, Type: t,
|
||||
ColumnsSeq: make([]string, 0),
|
||||
Columns: make(map[string]*Column),
|
||||
Indexes: make(map[string]*Index),
|
||||
Created: make(map[string]bool),
|
||||
}
|
||||
}*/
|
||||
|
||||
// if has primary key, return column
|
||||
func (table *Table) PKColumn() *Column {
|
||||
return table.Columns[table.PrimaryKey]
|
||||
return table.Columns[table.PrimaryKey]
|
||||
}
|
||||
|
||||
func (table *Table) VersionColumn() *Column {
|
||||
return table.Columns[table.Version]
|
||||
return table.Columns[table.Version]
|
||||
}
|
||||
|
||||
// add a column to table
|
||||
func (table *Table) AddColumn(col *Column) {
|
||||
table.ColumnsSeq = append(table.ColumnsSeq, col.Name)
|
||||
table.Columns[col.Name] = col
|
||||
if col.IsPrimaryKey {
|
||||
table.PrimaryKey = col.Name
|
||||
}
|
||||
if col.IsCreated {
|
||||
table.Created[col.Name] = true
|
||||
}
|
||||
if col.IsUpdated {
|
||||
table.Updated = col.Name
|
||||
}
|
||||
if col.IsVersion {
|
||||
table.Version = col.Name
|
||||
}
|
||||
table.ColumnsSeq = append(table.ColumnsSeq, col.Name)
|
||||
table.Columns[col.Name] = col
|
||||
if col.IsPrimaryKey {
|
||||
table.PrimaryKey = col.Name
|
||||
}
|
||||
if col.IsCreated {
|
||||
table.Created[col.Name] = true
|
||||
}
|
||||
if col.IsUpdated {
|
||||
table.Updated = col.Name
|
||||
}
|
||||
if col.IsVersion {
|
||||
table.Version = col.Name
|
||||
}
|
||||
}
|
||||
|
||||
// add an index or an unique to table
|
||||
func (table *Table) AddIndex(index *Index) {
|
||||
table.Indexes[index.Name] = index
|
||||
table.Indexes[index.Name] = index
|
||||
}
|
||||
|
||||
func (table *Table) genCols(session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) {
|
||||
colNames := make([]string, 0)
|
||||
args := make([]interface{}, 0)
|
||||
colNames := make([]string, 0)
|
||||
args := make([]interface{}, 0)
|
||||
|
||||
for _, col := range table.Columns {
|
||||
if useCol && !col.IsVersion && !col.IsCreated && !col.IsUpdated {
|
||||
if _, ok := session.Statement.columnMap[col.Name]; !ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if col.MapType == ONLYFROMDB {
|
||||
continue
|
||||
}
|
||||
for _, col := range table.Columns {
|
||||
if useCol && !col.IsVersion && !col.IsCreated && !col.IsUpdated {
|
||||
if _, ok := session.Statement.columnMap[col.Name]; !ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if col.MapType == ONLYFROMDB {
|
||||
continue
|
||||
}
|
||||
|
||||
fieldValue := col.ValueOf(bean)
|
||||
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
||||
continue
|
||||
}
|
||||
fieldValue := col.ValueOf(bean)
|
||||
if col.IsAutoIncrement && fieldValue.Int() == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if session.Statement.ColumnStr != "" {
|
||||
if _, ok := session.Statement.columnMap[col.Name]; !ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if session.Statement.OmitStr != "" {
|
||||
if _, ok := session.Statement.columnMap[col.Name]; ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if session.Statement.ColumnStr != "" {
|
||||
if _, ok := session.Statement.columnMap[col.Name]; !ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if session.Statement.OmitStr != "" {
|
||||
if _, ok := session.Statement.columnMap[col.Name]; ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
|
||||
args = append(args, time.Now())
|
||||
} else if col.IsVersion && session.Statement.checkVersion {
|
||||
args = append(args, 1)
|
||||
} else {
|
||||
arg, err := session.value2Interface(col, fieldValue)
|
||||
if err != nil {
|
||||
return colNames, args, err
|
||||
}
|
||||
args = append(args, arg)
|
||||
}
|
||||
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
|
||||
args = append(args, time.Now())
|
||||
} else if col.IsVersion && session.Statement.checkVersion {
|
||||
args = append(args, 1)
|
||||
} else {
|
||||
arg, err := session.value2Interface(col, fieldValue)
|
||||
if err != nil {
|
||||
return colNames, args, err
|
||||
}
|
||||
args = append(args, arg)
|
||||
}
|
||||
|
||||
if includeQuote {
|
||||
colNames = append(colNames, session.Engine.Quote(col.Name)+" = ?")
|
||||
} else {
|
||||
colNames = append(colNames, col.Name)
|
||||
}
|
||||
}
|
||||
return colNames, args, nil
|
||||
if includeQuote {
|
||||
colNames = append(colNames, session.Engine.Quote(col.Name)+" = ?")
|
||||
} else {
|
||||
colNames = append(colNames, col.Name)
|
||||
}
|
||||
}
|
||||
return colNames, args, nil
|
||||
}
|
||||
|
||||
// Conversion is an interface. A type implements Conversion will according
|
||||
// the custom method to fill into database and retrieve from database.
|
||||
type Conversion interface {
|
||||
FromDB([]byte) error
|
||||
ToDB() ([]byte, error)
|
||||
FromDB([]byte) error
|
||||
ToDB() ([]byte, error)
|
||||
}
|
||||
|
|
|
|||
78
xorm.go
78
xorm.go
|
|
@ -1,58 +1,58 @@
|
|||
package xorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"sync"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
version string = "0.2.2"
|
||||
version string = "0.2.2"
|
||||
)
|
||||
|
||||
func close(engine *Engine) {
|
||||
engine.Close()
|
||||
engine.Close()
|
||||
}
|
||||
|
||||
// new a db manager according to the parameter. Currently support four
|
||||
// drivers
|
||||
func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
|
||||
engine := &Engine{DriverName: driverName,
|
||||
DataSourceName: dataSourceName, Filters: make([]Filter, 0)}
|
||||
engine.SetMapper(SnakeMapper{})
|
||||
engine := &Engine{DriverName: driverName,
|
||||
DataSourceName: dataSourceName, Filters: make([]Filter, 0)}
|
||||
engine.SetMapper(SnakeMapper{})
|
||||
|
||||
if driverName == SQLITE {
|
||||
engine.dialect = &sqlite3{}
|
||||
} else if driverName == MYSQL {
|
||||
engine.dialect = &mysql{}
|
||||
} else if driverName == POSTGRES {
|
||||
engine.dialect = &postgres{}
|
||||
engine.Filters = append(engine.Filters, &PgSeqFilter{})
|
||||
engine.Filters = append(engine.Filters, &QuoteFilter{})
|
||||
} else if driverName == MYMYSQL {
|
||||
engine.dialect = &mymysql{}
|
||||
} else {
|
||||
return nil, errors.New(fmt.Sprintf("Unsupported driver name: %v", driverName))
|
||||
}
|
||||
err := engine.dialect.Init(driverName, dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if driverName == SQLITE {
|
||||
engine.dialect = &sqlite3{}
|
||||
} else if driverName == MYSQL {
|
||||
engine.dialect = &mysql{}
|
||||
} else if driverName == POSTGRES {
|
||||
engine.dialect = &postgres{}
|
||||
engine.Filters = append(engine.Filters, &PgSeqFilter{})
|
||||
engine.Filters = append(engine.Filters, &QuoteFilter{})
|
||||
} else if driverName == MYMYSQL {
|
||||
engine.dialect = &mymysql{}
|
||||
} else {
|
||||
return nil, errors.New(fmt.Sprintf("Unsupported driver name: %v", driverName))
|
||||
}
|
||||
err := engine.dialect.Init(driverName, dataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
engine.Tables = make(map[reflect.Type]*Table)
|
||||
engine.mutex = &sync.Mutex{}
|
||||
engine.TagIdentifier = "xorm"
|
||||
engine.Tables = make(map[reflect.Type]*Table)
|
||||
engine.mutex = &sync.Mutex{}
|
||||
engine.TagIdentifier = "xorm"
|
||||
|
||||
engine.Filters = append(engine.Filters, &IdFilter{})
|
||||
engine.Logger = os.Stdout
|
||||
engine.Filters = append(engine.Filters, &IdFilter{})
|
||||
engine.Logger = os.Stdout
|
||||
|
||||
//engine.Pool = NewSimpleConnectPool()
|
||||
//engine.Pool = NewNoneConnectPool()
|
||||
//engine.Cacher = NewLRUCacher()
|
||||
err = engine.SetPool(NewSysConnectPool())
|
||||
runtime.SetFinalizer(engine, close)
|
||||
return engine, err
|
||||
//engine.Pool = NewSimpleConnectPool()
|
||||
//engine.Pool = NewNoneConnectPool()
|
||||
//engine.Cacher = NewLRUCacher()
|
||||
err = engine.SetPool(NewSysConnectPool())
|
||||
runtime.SetFinalizer(engine, close)
|
||||
return engine, err
|
||||
}
|
||||
|
|
|
|||
102
xorm/c++.go
102
xorm/c++.go
|
|
@ -1,65 +1,65 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
//"fmt"
|
||||
"github.com/lunny/xorm"
|
||||
"strings"
|
||||
"text/template"
|
||||
//"fmt"
|
||||
"github.com/lunny/xorm"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var (
|
||||
CPlusTmpl LangTmpl = LangTmpl{
|
||||
template.FuncMap{"Mapper": mapper.Table2Obj,
|
||||
"Type": cPlusTypeStr,
|
||||
"UnTitle": unTitle,
|
||||
},
|
||||
nil,
|
||||
genCPlusImports,
|
||||
}
|
||||
CPlusTmpl LangTmpl = LangTmpl{
|
||||
template.FuncMap{"Mapper": mapper.Table2Obj,
|
||||
"Type": cPlusTypeStr,
|
||||
"UnTitle": unTitle,
|
||||
},
|
||||
nil,
|
||||
genCPlusImports,
|
||||
}
|
||||
)
|
||||
|
||||
func cPlusTypeStr(col *xorm.Column) string {
|
||||
tp := col.SQLType
|
||||
name := strings.ToUpper(tp.Name)
|
||||
switch name {
|
||||
case xorm.Bit, xorm.TinyInt, xorm.SmallInt, xorm.MediumInt, xorm.Int, xorm.Integer, xorm.Serial:
|
||||
return "int"
|
||||
case xorm.BigInt, xorm.BigSerial:
|
||||
return "__int64"
|
||||
case xorm.Char, xorm.Varchar, xorm.TinyText, xorm.Text, xorm.MediumText, xorm.LongText:
|
||||
return "tstring"
|
||||
case xorm.Date, xorm.DateTime, xorm.Time, xorm.TimeStamp:
|
||||
return "time_t"
|
||||
case xorm.Decimal, xorm.Numeric:
|
||||
return "tstring"
|
||||
case xorm.Real, xorm.Float:
|
||||
return "float"
|
||||
case xorm.Double:
|
||||
return "double"
|
||||
case xorm.TinyBlob, xorm.Blob, xorm.MediumBlob, xorm.LongBlob, xorm.Bytea:
|
||||
return "tstring"
|
||||
case xorm.Bool:
|
||||
return "bool"
|
||||
default:
|
||||
return "tstring"
|
||||
}
|
||||
return ""
|
||||
tp := col.SQLType
|
||||
name := strings.ToUpper(tp.Name)
|
||||
switch name {
|
||||
case xorm.Bit, xorm.TinyInt, xorm.SmallInt, xorm.MediumInt, xorm.Int, xorm.Integer, xorm.Serial:
|
||||
return "int"
|
||||
case xorm.BigInt, xorm.BigSerial:
|
||||
return "__int64"
|
||||
case xorm.Char, xorm.Varchar, xorm.TinyText, xorm.Text, xorm.MediumText, xorm.LongText:
|
||||
return "tstring"
|
||||
case xorm.Date, xorm.DateTime, xorm.Time, xorm.TimeStamp:
|
||||
return "time_t"
|
||||
case xorm.Decimal, xorm.Numeric:
|
||||
return "tstring"
|
||||
case xorm.Real, xorm.Float:
|
||||
return "float"
|
||||
case xorm.Double:
|
||||
return "double"
|
||||
case xorm.TinyBlob, xorm.Blob, xorm.MediumBlob, xorm.LongBlob, xorm.Bytea:
|
||||
return "tstring"
|
||||
case xorm.Bool:
|
||||
return "bool"
|
||||
default:
|
||||
return "tstring"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func genCPlusImports(tables []*xorm.Table) map[string]string {
|
||||
imports := make(map[string]string)
|
||||
imports := make(map[string]string)
|
||||
|
||||
for _, table := range tables {
|
||||
for _, col := range table.Columns {
|
||||
switch cPlusTypeStr(col) {
|
||||
case "time_t":
|
||||
imports[`<time.h>`] = `<time.h>`
|
||||
case "tstring":
|
||||
imports["<string>"] = "<string>"
|
||||
//case "__int64":
|
||||
// imports[""] = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
return imports
|
||||
for _, table := range tables {
|
||||
for _, col := range table.Columns {
|
||||
switch cPlusTypeStr(col) {
|
||||
case "time_t":
|
||||
imports[`<time.h>`] = `<time.h>`
|
||||
case "tstring":
|
||||
imports["<string>"] = "<string>"
|
||||
//case "__int64":
|
||||
// imports[""] = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
return imports
|
||||
}
|
||||
|
|
|
|||
94
xorm/cmd.go
94
xorm/cmd.go
|
|
@ -1,78 +1,78 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// A Command is an implementation of a go command
|
||||
// like go build or go fix.
|
||||
type Command struct {
|
||||
// Run runs the command.
|
||||
// The args are the arguments after the command name.
|
||||
Run func(cmd *Command, args []string)
|
||||
// Run runs the command.
|
||||
// The args are the arguments after the command name.
|
||||
Run func(cmd *Command, args []string)
|
||||
|
||||
// UsageLine is the one-line usage message.
|
||||
// The first word in the line is taken to be the command name.
|
||||
UsageLine string
|
||||
// UsageLine is the one-line usage message.
|
||||
// The first word in the line is taken to be the command name.
|
||||
UsageLine string
|
||||
|
||||
// Short is the short description shown in the 'go help' output.
|
||||
Short string
|
||||
// Short is the short description shown in the 'go help' output.
|
||||
Short string
|
||||
|
||||
// Long is the long message shown in the 'go help <this-command>' output.
|
||||
Long string
|
||||
// Long is the long message shown in the 'go help <this-command>' output.
|
||||
Long string
|
||||
|
||||
// Flag is a set of flags specific to this command.
|
||||
Flags map[string]bool
|
||||
// Flag is a set of flags specific to this command.
|
||||
Flags map[string]bool
|
||||
}
|
||||
|
||||
// Name returns the command's name: the first word in the usage line.
|
||||
func (c *Command) Name() string {
|
||||
name := c.UsageLine
|
||||
i := strings.Index(name, " ")
|
||||
if i >= 0 {
|
||||
name = name[:i]
|
||||
}
|
||||
return name
|
||||
name := c.UsageLine
|
||||
i := strings.Index(name, " ")
|
||||
if i >= 0 {
|
||||
name = name[:i]
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func (c *Command) Usage() {
|
||||
fmt.Fprintf(os.Stderr, "usage: %s\n\n", c.UsageLine)
|
||||
fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(c.Long))
|
||||
os.Exit(2)
|
||||
fmt.Fprintf(os.Stderr, "usage: %s\n\n", c.UsageLine)
|
||||
fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(c.Long))
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
// Runnable reports whether the command can be run; otherwise
|
||||
// it is a documentation pseudo-command such as importpath.
|
||||
func (c *Command) Runnable() bool {
|
||||
return c.Run != nil
|
||||
return c.Run != nil
|
||||
}
|
||||
|
||||
// checkFlags checks if the flag exists with correct format.
|
||||
func checkFlags(flags map[string]bool, args []string, print func(string)) int {
|
||||
num := 0 // Number of valid flags, use to cut out.
|
||||
for i, f := range args {
|
||||
// Check flag prefix '-'.
|
||||
if !strings.HasPrefix(f, "-") {
|
||||
// Not a flag, finish check process.
|
||||
break
|
||||
}
|
||||
num := 0 // Number of valid flags, use to cut out.
|
||||
for i, f := range args {
|
||||
// Check flag prefix '-'.
|
||||
if !strings.HasPrefix(f, "-") {
|
||||
// Not a flag, finish check process.
|
||||
break
|
||||
}
|
||||
|
||||
// Check if it a valid flag.
|
||||
if v, ok := flags[f]; ok {
|
||||
flags[f] = !v
|
||||
if !v {
|
||||
print(f)
|
||||
} else {
|
||||
fmt.Println("DISABLE: " + f)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("[ERRO] Unknown flag: %s.\n", f)
|
||||
return -1
|
||||
}
|
||||
num = i + 1
|
||||
}
|
||||
// Check if it a valid flag.
|
||||
if v, ok := flags[f]; ok {
|
||||
flags[f] = !v
|
||||
if !v {
|
||||
print(f)
|
||||
} else {
|
||||
fmt.Println("DISABLE: " + f)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("[ERRO] Unknown flag: %s.\n", f)
|
||||
return -1
|
||||
}
|
||||
num = i + 1
|
||||
}
|
||||
|
||||
return num
|
||||
return num
|
||||
}
|
||||
|
|
|
|||
418
xorm/go.go
418
xorm/go.go
|
|
@ -1,261 +1,261 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/lunny/xorm"
|
||||
"go/format"
|
||||
"reflect"
|
||||
"strings"
|
||||
"text/template"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/lunny/xorm"
|
||||
"go/format"
|
||||
"reflect"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var (
|
||||
GoLangTmpl LangTmpl = LangTmpl{
|
||||
template.FuncMap{"Mapper": mapper.Table2Obj,
|
||||
"Type": typestring,
|
||||
"Tag": tag,
|
||||
"UnTitle": unTitle,
|
||||
"gt": gt,
|
||||
"getCol": getCol,
|
||||
},
|
||||
formatGo,
|
||||
genGoImports,
|
||||
}
|
||||
GoLangTmpl LangTmpl = LangTmpl{
|
||||
template.FuncMap{"Mapper": mapper.Table2Obj,
|
||||
"Type": typestring,
|
||||
"Tag": tag,
|
||||
"UnTitle": unTitle,
|
||||
"gt": gt,
|
||||
"getCol": getCol,
|
||||
},
|
||||
formatGo,
|
||||
genGoImports,
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
errBadComparisonType = errors.New("invalid type for comparison")
|
||||
errBadComparison = errors.New("incompatible types for comparison")
|
||||
errNoComparison = errors.New("missing argument for comparison")
|
||||
errBadComparisonType = errors.New("invalid type for comparison")
|
||||
errBadComparison = errors.New("incompatible types for comparison")
|
||||
errNoComparison = errors.New("missing argument for comparison")
|
||||
)
|
||||
|
||||
type kind int
|
||||
|
||||
const (
|
||||
invalidKind kind = iota
|
||||
boolKind
|
||||
complexKind
|
||||
intKind
|
||||
floatKind
|
||||
integerKind
|
||||
stringKind
|
||||
uintKind
|
||||
invalidKind kind = iota
|
||||
boolKind
|
||||
complexKind
|
||||
intKind
|
||||
floatKind
|
||||
integerKind
|
||||
stringKind
|
||||
uintKind
|
||||
)
|
||||
|
||||
func basicKind(v reflect.Value) (kind, error) {
|
||||
switch v.Kind() {
|
||||
case reflect.Bool:
|
||||
return boolKind, nil
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return intKind, nil
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
return uintKind, nil
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return floatKind, nil
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
return complexKind, nil
|
||||
case reflect.String:
|
||||
return stringKind, nil
|
||||
}
|
||||
return invalidKind, errBadComparisonType
|
||||
switch v.Kind() {
|
||||
case reflect.Bool:
|
||||
return boolKind, nil
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return intKind, nil
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
return uintKind, nil
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return floatKind, nil
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
return complexKind, nil
|
||||
case reflect.String:
|
||||
return stringKind, nil
|
||||
}
|
||||
return invalidKind, errBadComparisonType
|
||||
}
|
||||
|
||||
// eq evaluates the comparison a == b || a == c || ...
|
||||
func eq(arg1 interface{}, arg2 ...interface{}) (bool, error) {
|
||||
v1 := reflect.ValueOf(arg1)
|
||||
k1, err := basicKind(v1)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(arg2) == 0 {
|
||||
return false, errNoComparison
|
||||
}
|
||||
for _, arg := range arg2 {
|
||||
v2 := reflect.ValueOf(arg)
|
||||
k2, err := basicKind(v2)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if k1 != k2 {
|
||||
return false, errBadComparison
|
||||
}
|
||||
truth := false
|
||||
switch k1 {
|
||||
case boolKind:
|
||||
truth = v1.Bool() == v2.Bool()
|
||||
case complexKind:
|
||||
truth = v1.Complex() == v2.Complex()
|
||||
case floatKind:
|
||||
truth = v1.Float() == v2.Float()
|
||||
case intKind:
|
||||
truth = v1.Int() == v2.Int()
|
||||
case stringKind:
|
||||
truth = v1.String() == v2.String()
|
||||
case uintKind:
|
||||
truth = v1.Uint() == v2.Uint()
|
||||
default:
|
||||
panic("invalid kind")
|
||||
}
|
||||
if truth {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
v1 := reflect.ValueOf(arg1)
|
||||
k1, err := basicKind(v1)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(arg2) == 0 {
|
||||
return false, errNoComparison
|
||||
}
|
||||
for _, arg := range arg2 {
|
||||
v2 := reflect.ValueOf(arg)
|
||||
k2, err := basicKind(v2)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if k1 != k2 {
|
||||
return false, errBadComparison
|
||||
}
|
||||
truth := false
|
||||
switch k1 {
|
||||
case boolKind:
|
||||
truth = v1.Bool() == v2.Bool()
|
||||
case complexKind:
|
||||
truth = v1.Complex() == v2.Complex()
|
||||
case floatKind:
|
||||
truth = v1.Float() == v2.Float()
|
||||
case intKind:
|
||||
truth = v1.Int() == v2.Int()
|
||||
case stringKind:
|
||||
truth = v1.String() == v2.String()
|
||||
case uintKind:
|
||||
truth = v1.Uint() == v2.Uint()
|
||||
default:
|
||||
panic("invalid kind")
|
||||
}
|
||||
if truth {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// lt evaluates the comparison a < b.
|
||||
func lt(arg1, arg2 interface{}) (bool, error) {
|
||||
v1 := reflect.ValueOf(arg1)
|
||||
k1, err := basicKind(v1)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
v2 := reflect.ValueOf(arg2)
|
||||
k2, err := basicKind(v2)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if k1 != k2 {
|
||||
return false, errBadComparison
|
||||
}
|
||||
truth := false
|
||||
switch k1 {
|
||||
case boolKind, complexKind:
|
||||
return false, errBadComparisonType
|
||||
case floatKind:
|
||||
truth = v1.Float() < v2.Float()
|
||||
case intKind:
|
||||
truth = v1.Int() < v2.Int()
|
||||
case stringKind:
|
||||
truth = v1.String() < v2.String()
|
||||
case uintKind:
|
||||
truth = v1.Uint() < v2.Uint()
|
||||
default:
|
||||
panic("invalid kind")
|
||||
}
|
||||
return truth, nil
|
||||
v1 := reflect.ValueOf(arg1)
|
||||
k1, err := basicKind(v1)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
v2 := reflect.ValueOf(arg2)
|
||||
k2, err := basicKind(v2)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if k1 != k2 {
|
||||
return false, errBadComparison
|
||||
}
|
||||
truth := false
|
||||
switch k1 {
|
||||
case boolKind, complexKind:
|
||||
return false, errBadComparisonType
|
||||
case floatKind:
|
||||
truth = v1.Float() < v2.Float()
|
||||
case intKind:
|
||||
truth = v1.Int() < v2.Int()
|
||||
case stringKind:
|
||||
truth = v1.String() < v2.String()
|
||||
case uintKind:
|
||||
truth = v1.Uint() < v2.Uint()
|
||||
default:
|
||||
panic("invalid kind")
|
||||
}
|
||||
return truth, nil
|
||||
}
|
||||
|
||||
// le evaluates the comparison <= b.
|
||||
func le(arg1, arg2 interface{}) (bool, error) {
|
||||
// <= is < or ==.
|
||||
lessThan, err := lt(arg1, arg2)
|
||||
if lessThan || err != nil {
|
||||
return lessThan, err
|
||||
}
|
||||
return eq(arg1, arg2)
|
||||
// <= is < or ==.
|
||||
lessThan, err := lt(arg1, arg2)
|
||||
if lessThan || err != nil {
|
||||
return lessThan, err
|
||||
}
|
||||
return eq(arg1, arg2)
|
||||
}
|
||||
|
||||
// gt evaluates the comparison a > b.
|
||||
func gt(arg1, arg2 interface{}) (bool, error) {
|
||||
// > is the inverse of <=.
|
||||
lessOrEqual, err := le(arg1, arg2)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return !lessOrEqual, nil
|
||||
// > is the inverse of <=.
|
||||
lessOrEqual, err := le(arg1, arg2)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return !lessOrEqual, nil
|
||||
}
|
||||
|
||||
func getCol(cols map[string]*xorm.Column, name string) *xorm.Column {
|
||||
return cols[name]
|
||||
return cols[name]
|
||||
}
|
||||
|
||||
func formatGo(src string) (string, error) {
|
||||
source, err := format.Source([]byte(src))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(source), nil
|
||||
source, err := format.Source([]byte(src))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(source), nil
|
||||
}
|
||||
|
||||
func genGoImports(tables []*xorm.Table) map[string]string {
|
||||
imports := make(map[string]string)
|
||||
imports := make(map[string]string)
|
||||
|
||||
for _, table := range tables {
|
||||
for _, col := range table.Columns {
|
||||
if typestring(col) == "time.Time" {
|
||||
imports["time"] = "time"
|
||||
}
|
||||
}
|
||||
}
|
||||
return imports
|
||||
for _, table := range tables {
|
||||
for _, col := range table.Columns {
|
||||
if typestring(col) == "time.Time" {
|
||||
imports["time"] = "time"
|
||||
}
|
||||
}
|
||||
}
|
||||
return imports
|
||||
}
|
||||
|
||||
func typestring(col *xorm.Column) string {
|
||||
st := col.SQLType
|
||||
if col.IsPrimaryKey {
|
||||
return "int64"
|
||||
}
|
||||
t := xorm.SQLType2Type(st)
|
||||
s := t.String()
|
||||
if s == "[]uint8" {
|
||||
return "[]byte"
|
||||
}
|
||||
return s
|
||||
st := col.SQLType
|
||||
if col.IsPrimaryKey {
|
||||
return "int64"
|
||||
}
|
||||
t := xorm.SQLType2Type(st)
|
||||
s := t.String()
|
||||
if s == "[]uint8" {
|
||||
return "[]byte"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func tag(table *xorm.Table, col *xorm.Column) string {
|
||||
isNameId := (mapper.Table2Obj(col.Name) == "Id")
|
||||
res := make([]string, 0)
|
||||
if !col.Nullable {
|
||||
if !isNameId {
|
||||
res = append(res, "not null")
|
||||
}
|
||||
}
|
||||
if col.IsPrimaryKey {
|
||||
if !isNameId {
|
||||
res = append(res, "pk")
|
||||
}
|
||||
}
|
||||
if col.Default != "" {
|
||||
res = append(res, "default "+col.Default)
|
||||
}
|
||||
if col.IsAutoIncrement {
|
||||
if !isNameId {
|
||||
res = append(res, "autoincr")
|
||||
}
|
||||
}
|
||||
if col.IsCreated {
|
||||
res = append(res, "created")
|
||||
}
|
||||
if col.IsUpdated {
|
||||
res = append(res, "updated")
|
||||
}
|
||||
for name, _ := range col.Indexes {
|
||||
index := table.Indexes[name]
|
||||
var uistr string
|
||||
if index.Type == xorm.UniqueType {
|
||||
uistr = "unique"
|
||||
} else if index.Type == xorm.IndexType {
|
||||
uistr = "index"
|
||||
}
|
||||
if len(index.Cols) > 1 {
|
||||
uistr += "(" + index.Name + ")"
|
||||
}
|
||||
res = append(res, uistr)
|
||||
}
|
||||
isNameId := (mapper.Table2Obj(col.Name) == "Id")
|
||||
res := make([]string, 0)
|
||||
if !col.Nullable {
|
||||
if !isNameId {
|
||||
res = append(res, "not null")
|
||||
}
|
||||
}
|
||||
if col.IsPrimaryKey {
|
||||
if !isNameId {
|
||||
res = append(res, "pk")
|
||||
}
|
||||
}
|
||||
if col.Default != "" {
|
||||
res = append(res, "default "+col.Default)
|
||||
}
|
||||
if col.IsAutoIncrement {
|
||||
if !isNameId {
|
||||
res = append(res, "autoincr")
|
||||
}
|
||||
}
|
||||
if col.IsCreated {
|
||||
res = append(res, "created")
|
||||
}
|
||||
if col.IsUpdated {
|
||||
res = append(res, "updated")
|
||||
}
|
||||
for name, _ := range col.Indexes {
|
||||
index := table.Indexes[name]
|
||||
var uistr string
|
||||
if index.Type == xorm.UniqueType {
|
||||
uistr = "unique"
|
||||
} else if index.Type == xorm.IndexType {
|
||||
uistr = "index"
|
||||
}
|
||||
if len(index.Cols) > 1 {
|
||||
uistr += "(" + index.Name + ")"
|
||||
}
|
||||
res = append(res, uistr)
|
||||
}
|
||||
|
||||
nstr := col.SQLType.Name
|
||||
if col.Length != 0 {
|
||||
if col.Length2 != 0 {
|
||||
nstr += fmt.Sprintf("(%v, %v)", col.Length, col.Length2)
|
||||
} else {
|
||||
nstr += fmt.Sprintf("(%v)", col.Length)
|
||||
}
|
||||
}
|
||||
res = append(res, nstr)
|
||||
nstr := col.SQLType.Name
|
||||
if col.Length != 0 {
|
||||
if col.Length2 != 0 {
|
||||
nstr += fmt.Sprintf("(%v, %v)", col.Length, col.Length2)
|
||||
} else {
|
||||
nstr += fmt.Sprintf("(%v)", col.Length)
|
||||
}
|
||||
}
|
||||
res = append(res, nstr)
|
||||
|
||||
var tags []string
|
||||
if genJson {
|
||||
tags = append(tags, "json:\""+col.Name+"\"")
|
||||
}
|
||||
if len(res) > 0 {
|
||||
tags = append(tags, "xorm:\""+strings.Join(res, " ")+"\"")
|
||||
}
|
||||
if len(tags) > 0 {
|
||||
return "`" + strings.Join(tags, " ") + "`"
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
var tags []string
|
||||
if genJson {
|
||||
tags = append(tags, "json:\""+col.Name+"\"")
|
||||
}
|
||||
if len(res) > 0 {
|
||||
tags = append(tags, "xorm:\""+strings.Join(res, " ")+"\"")
|
||||
}
|
||||
if len(tags) > 0 {
|
||||
return "`" + strings.Join(tags, " ") + "`"
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
|
|
|||
68
xorm/lang.go
68
xorm/lang.go
|
|
@ -1,51 +1,51 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/lunny/xorm"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"text/template"
|
||||
"github.com/lunny/xorm"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type LangTmpl struct {
|
||||
Funcs template.FuncMap
|
||||
Formater func(string) (string, error)
|
||||
GenImports func([]*xorm.Table) map[string]string
|
||||
Funcs template.FuncMap
|
||||
Formater func(string) (string, error)
|
||||
GenImports func([]*xorm.Table) map[string]string
|
||||
}
|
||||
|
||||
var (
|
||||
mapper = &xorm.SnakeMapper{}
|
||||
langTmpls = map[string]LangTmpl{
|
||||
"go": GoLangTmpl,
|
||||
"c++": CPlusTmpl,
|
||||
}
|
||||
mapper = &xorm.SnakeMapper{}
|
||||
langTmpls = map[string]LangTmpl{
|
||||
"go": GoLangTmpl,
|
||||
"c++": CPlusTmpl,
|
||||
}
|
||||
)
|
||||
|
||||
func loadConfig(f string) map[string]string {
|
||||
bts, err := ioutil.ReadFile(f)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
configs := make(map[string]string)
|
||||
lines := strings.Split(string(bts), "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimRight(line, "\r")
|
||||
vs := strings.Split(line, "=")
|
||||
if len(vs) == 2 {
|
||||
configs[strings.TrimSpace(vs[0])] = strings.TrimSpace(vs[1])
|
||||
}
|
||||
}
|
||||
return configs
|
||||
bts, err := ioutil.ReadFile(f)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
configs := make(map[string]string)
|
||||
lines := strings.Split(string(bts), "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimRight(line, "\r")
|
||||
vs := strings.Split(line, "=")
|
||||
if len(vs) == 2 {
|
||||
configs[strings.TrimSpace(vs[0])] = strings.TrimSpace(vs[1])
|
||||
}
|
||||
}
|
||||
return configs
|
||||
}
|
||||
|
||||
func unTitle(src string) string {
|
||||
if src == "" {
|
||||
return ""
|
||||
}
|
||||
if src == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
if len(src) == 1 {
|
||||
return strings.ToLower(string(src[0]))
|
||||
} else {
|
||||
return strings.ToLower(string(src[0])) + src[1:]
|
||||
}
|
||||
if len(src) == 1 {
|
||||
return strings.ToLower(string(src[0]))
|
||||
} else {
|
||||
return strings.ToLower(string(src[0])) + src[1:]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
412
xorm/reverse.go
412
xorm/reverse.go
|
|
@ -1,268 +1,268 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
_ "github.com/bylevel/pq"
|
||||
"github.com/dvirsky/go-pylog/logging"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/lunny/xorm"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
_ "github.com/ziutek/mymysql/godrv"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"text/template"
|
||||
"bytes"
|
||||
"fmt"
|
||||
_ "github.com/bylevel/pq"
|
||||
"github.com/dvirsky/go-pylog/logging"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/lunny/xorm"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
_ "github.com/ziutek/mymysql/godrv"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var CmdReverse = &Command{
|
||||
UsageLine: "reverse [-m] driverName datasourceName tmplPath [generatedPath]",
|
||||
Short: "reverse a db to codes",
|
||||
Long: `
|
||||
UsageLine: "reverse [-m] driverName datasourceName tmplPath [generatedPath]",
|
||||
Short: "reverse a db to codes",
|
||||
Long: `
|
||||
according database's tables and columns to generate codes for Go, C++ and etc.
|
||||
|
||||
-m Generated one go file for every table
|
||||
driverName Database driver name, now supported four: mysql mymysql sqlite3 postgres
|
||||
datasourceName Database connection uri, for detail infomation please visit driver's project page
|
||||
tmplPath Template dir for generated. the default templates dir has provide 1 template
|
||||
generatedPath This parameter is optional, if blank, the default value is model, then will
|
||||
generated all codes in model dir
|
||||
-m Generated one go file for every table
|
||||
driverName Database driver name, now supported four: mysql mymysql sqlite3 postgres
|
||||
datasourceName Database connection uri, for detail infomation please visit driver's project page
|
||||
tmplPath Template dir for generated. the default templates dir has provide 1 template
|
||||
generatedPath This parameter is optional, if blank, the default value is model, then will
|
||||
generated all codes in model dir
|
||||
`,
|
||||
}
|
||||
|
||||
func init() {
|
||||
CmdReverse.Run = runReverse
|
||||
CmdReverse.Flags = map[string]bool{
|
||||
"-s": false,
|
||||
"-l": false,
|
||||
}
|
||||
CmdReverse.Run = runReverse
|
||||
CmdReverse.Flags = map[string]bool{
|
||||
"-s": false,
|
||||
"-l": false,
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
genJson bool = false
|
||||
genJson bool = false
|
||||
)
|
||||
|
||||
func printReversePrompt(flag string) {
|
||||
}
|
||||
|
||||
type Tmpl struct {
|
||||
Tables []*xorm.Table
|
||||
Imports map[string]string
|
||||
Model string
|
||||
Tables []*xorm.Table
|
||||
Imports map[string]string
|
||||
Model string
|
||||
}
|
||||
|
||||
func dirExists(dir string) bool {
|
||||
d, e := os.Stat(dir)
|
||||
switch {
|
||||
case e != nil:
|
||||
return false
|
||||
case !d.IsDir():
|
||||
return false
|
||||
}
|
||||
d, e := os.Stat(dir)
|
||||
switch {
|
||||
case e != nil:
|
||||
return false
|
||||
case !d.IsDir():
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
return true
|
||||
}
|
||||
|
||||
func runReverse(cmd *Command, args []string) {
|
||||
num := checkFlags(cmd.Flags, args, printReversePrompt)
|
||||
if num == -1 {
|
||||
return
|
||||
}
|
||||
args = args[num:]
|
||||
num := checkFlags(cmd.Flags, args, printReversePrompt)
|
||||
if num == -1 {
|
||||
return
|
||||
}
|
||||
args = args[num:]
|
||||
|
||||
if len(args) < 3 {
|
||||
fmt.Println("params error, please see xorm help reverse")
|
||||
return
|
||||
}
|
||||
if len(args) < 3 {
|
||||
fmt.Println("params error, please see xorm help reverse")
|
||||
return
|
||||
}
|
||||
|
||||
var isMultiFile bool = true
|
||||
if use, ok := cmd.Flags["-s"]; ok {
|
||||
isMultiFile = !use
|
||||
}
|
||||
var isMultiFile bool = true
|
||||
if use, ok := cmd.Flags["-s"]; ok {
|
||||
isMultiFile = !use
|
||||
}
|
||||
|
||||
curPath, err := os.Getwd()
|
||||
if err != nil {
|
||||
fmt.Println(curPath)
|
||||
return
|
||||
}
|
||||
curPath, err := os.Getwd()
|
||||
if err != nil {
|
||||
fmt.Println(curPath)
|
||||
return
|
||||
}
|
||||
|
||||
var genDir string
|
||||
var model string
|
||||
if len(args) == 4 {
|
||||
var genDir string
|
||||
var model string
|
||||
if len(args) == 4 {
|
||||
|
||||
genDir, err = filepath.Abs(args[3])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
model = path.Base(genDir)
|
||||
} else {
|
||||
model = "model"
|
||||
genDir = path.Join(curPath, model)
|
||||
}
|
||||
genDir, err = filepath.Abs(args[3])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
model = path.Base(genDir)
|
||||
} else {
|
||||
model = "model"
|
||||
genDir = path.Join(curPath, model)
|
||||
}
|
||||
|
||||
dir, err := filepath.Abs(args[2])
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return
|
||||
}
|
||||
dir, err := filepath.Abs(args[2])
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if !dirExists(dir) {
|
||||
logging.Error("Template %v path is not exist", dir)
|
||||
return
|
||||
}
|
||||
if !dirExists(dir) {
|
||||
logging.Error("Template %v path is not exist", dir)
|
||||
return
|
||||
}
|
||||
|
||||
var langTmpl LangTmpl
|
||||
var ok bool
|
||||
var lang string = "go"
|
||||
var langTmpl LangTmpl
|
||||
var ok bool
|
||||
var lang string = "go"
|
||||
|
||||
cfgPath := path.Join(dir, "config")
|
||||
info, err := os.Stat(cfgPath)
|
||||
var configs map[string]string
|
||||
if err == nil && !info.IsDir() {
|
||||
configs = loadConfig(cfgPath)
|
||||
if l, ok := configs["lang"]; ok {
|
||||
lang = l
|
||||
}
|
||||
if j, ok := configs["genJson"]; ok {
|
||||
genJson, err = strconv.ParseBool(j)
|
||||
}
|
||||
}
|
||||
cfgPath := path.Join(dir, "config")
|
||||
info, err := os.Stat(cfgPath)
|
||||
var configs map[string]string
|
||||
if err == nil && !info.IsDir() {
|
||||
configs = loadConfig(cfgPath)
|
||||
if l, ok := configs["lang"]; ok {
|
||||
lang = l
|
||||
}
|
||||
if j, ok := configs["genJson"]; ok {
|
||||
genJson, err = strconv.ParseBool(j)
|
||||
}
|
||||
}
|
||||
|
||||
if langTmpl, ok = langTmpls[lang]; !ok {
|
||||
fmt.Println("Unsupported programing language", lang)
|
||||
return
|
||||
}
|
||||
if langTmpl, ok = langTmpls[lang]; !ok {
|
||||
fmt.Println("Unsupported programing language", lang)
|
||||
return
|
||||
}
|
||||
|
||||
os.MkdirAll(genDir, os.ModePerm)
|
||||
os.MkdirAll(genDir, os.ModePerm)
|
||||
|
||||
Orm, err := xorm.NewEngine(args[0], args[1])
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return
|
||||
}
|
||||
Orm, err := xorm.NewEngine(args[0], args[1])
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
tables, err := Orm.DBMetas()
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return
|
||||
}
|
||||
tables, err := Orm.DBMetas()
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return
|
||||
}
|
||||
|
||||
filepath.Walk(dir, func(f string, info os.FileInfo, err error) error {
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
filepath.Walk(dir, func(f string, info os.FileInfo, err error) error {
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if info.Name() == "config" {
|
||||
return nil
|
||||
}
|
||||
if info.Name() == "config" {
|
||||
return nil
|
||||
}
|
||||
|
||||
bs, err := ioutil.ReadFile(f)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
bs, err := ioutil.ReadFile(f)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
t := template.New(f)
|
||||
t.Funcs(langTmpl.Funcs)
|
||||
t := template.New(f)
|
||||
t.Funcs(langTmpl.Funcs)
|
||||
|
||||
tmpl, err := t.Parse(string(bs))
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
tmpl, err := t.Parse(string(bs))
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
var w *os.File
|
||||
fileName := info.Name()
|
||||
newFileName := fileName[:len(fileName)-4]
|
||||
ext := path.Ext(newFileName)
|
||||
var w *os.File
|
||||
fileName := info.Name()
|
||||
newFileName := fileName[:len(fileName)-4]
|
||||
ext := path.Ext(newFileName)
|
||||
|
||||
if !isMultiFile {
|
||||
w, err = os.OpenFile(path.Join(genDir, newFileName), os.O_RDWR|os.O_CREATE, 0600)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
if !isMultiFile {
|
||||
w, err = os.OpenFile(path.Join(genDir, newFileName), os.O_RDWR|os.O_CREATE, 0600)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
imports := langTmpl.GenImports(tables)
|
||||
imports := langTmpl.GenImports(tables)
|
||||
|
||||
tbls := make([]*xorm.Table, 0)
|
||||
for _, table := range tables {
|
||||
tbls = append(tbls, table)
|
||||
}
|
||||
tbls := make([]*xorm.Table, 0)
|
||||
for _, table := range tables {
|
||||
tbls = append(tbls, table)
|
||||
}
|
||||
|
||||
newbytes := bytes.NewBufferString("")
|
||||
newbytes := bytes.NewBufferString("")
|
||||
|
||||
t := &Tmpl{Tables: tbls, Imports: imports, Model: model}
|
||||
err = tmpl.Execute(newbytes, t)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
t := &Tmpl{Tables: tbls, Imports: imports, Model: model}
|
||||
err = tmpl.Execute(newbytes, t)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
tplcontent, err := ioutil.ReadAll(newbytes)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
var source string
|
||||
if langTmpl.Formater != nil {
|
||||
source, err = langTmpl.Formater(string(tplcontent))
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
source = string(tplcontent)
|
||||
}
|
||||
tplcontent, err := ioutil.ReadAll(newbytes)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
var source string
|
||||
if langTmpl.Formater != nil {
|
||||
source, err = langTmpl.Formater(string(tplcontent))
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
source = string(tplcontent)
|
||||
}
|
||||
|
||||
w.WriteString(source)
|
||||
w.Close()
|
||||
} else {
|
||||
for _, table := range tables {
|
||||
// imports
|
||||
tbs := []*xorm.Table{table}
|
||||
imports := langTmpl.GenImports(tbs)
|
||||
w.WriteString(source)
|
||||
w.Close()
|
||||
} else {
|
||||
for _, table := range tables {
|
||||
// imports
|
||||
tbs := []*xorm.Table{table}
|
||||
imports := langTmpl.GenImports(tbs)
|
||||
|
||||
w, err := os.OpenFile(path.Join(genDir, unTitle(mapper.Table2Obj(table.Name))+ext), os.O_RDWR|os.O_CREATE, 0600)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
w, err := os.OpenFile(path.Join(genDir, unTitle(mapper.Table2Obj(table.Name))+ext), os.O_RDWR|os.O_CREATE, 0600)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
newbytes := bytes.NewBufferString("")
|
||||
newbytes := bytes.NewBufferString("")
|
||||
|
||||
t := &Tmpl{Tables: tbs, Imports: imports, Model: model}
|
||||
err = tmpl.Execute(newbytes, t)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
t := &Tmpl{Tables: tbs, Imports: imports, Model: model}
|
||||
err = tmpl.Execute(newbytes, t)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
tplcontent, err := ioutil.ReadAll(newbytes)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
var source string
|
||||
if langTmpl.Formater != nil {
|
||||
source, err = langTmpl.Formater(string(tplcontent))
|
||||
if err != nil {
|
||||
logging.Error("%v-%v", err, string(tplcontent))
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
source = string(tplcontent)
|
||||
}
|
||||
tplcontent, err := ioutil.ReadAll(newbytes)
|
||||
if err != nil {
|
||||
logging.Error("%v", err)
|
||||
return err
|
||||
}
|
||||
var source string
|
||||
if langTmpl.Formater != nil {
|
||||
source, err = langTmpl.Formater(string(tplcontent))
|
||||
if err != nil {
|
||||
logging.Error("%v-%v", err, string(tplcontent))
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
source = string(tplcontent)
|
||||
}
|
||||
|
||||
w.WriteString(source)
|
||||
w.Close()
|
||||
}
|
||||
}
|
||||
w.WriteString(source)
|
||||
w.Close()
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
|
|
|||
238
xorm/shell.go
238
xorm/shell.go
|
|
@ -1,147 +1,147 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/lunny/xorm"
|
||||
"strings"
|
||||
"fmt"
|
||||
"github.com/lunny/xorm"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var CmdShell = &Command{
|
||||
UsageLine: "shell driverName datasourceName",
|
||||
Short: "a general shell to operate all kinds of database",
|
||||
Long: `
|
||||
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
|
||||
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{}
|
||||
CmdShell.Run = runShell
|
||||
CmdShell.Flags = map[string]bool{}
|
||||
}
|
||||
|
||||
var engine *xorm.Engine
|
||||
|
||||
func help() {
|
||||
fmt.Println(`
|
||||
show tables show all tables
|
||||
columns <table_name> show table's column info
|
||||
indexes <table_name> show table's index info
|
||||
exit exit shell
|
||||
source <sql_file> exec sql file to current database
|
||||
dump [-nodata] <sql_file> dump structs or records to sql file
|
||||
help show this document
|
||||
<statement> SQL statement
|
||||
`)
|
||||
fmt.Println(`
|
||||
show tables show all tables
|
||||
columns <table_name> show table's column info
|
||||
indexes <table_name> show table's index info
|
||||
exit exit shell
|
||||
source <sql_file> exec sql file to current database
|
||||
dump [-nodata] <sql_file> dump structs or records to sql file
|
||||
help show this document
|
||||
<statement> SQL statement
|
||||
`)
|
||||
}
|
||||
|
||||
func runShell(cmd *Command, args []string) {
|
||||
if len(args) != 2 {
|
||||
fmt.Println("params error, please see xorm help shell")
|
||||
return
|
||||
}
|
||||
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 err error
|
||||
engine, err = xorm.NewEngine(args[0], args[1])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = engine.Ping()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = engine.Ping()
|
||||
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)
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
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 if lcmd == "show tables;" {
|
||||
tables, err := engine.DBMetas()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
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 if lcmd == "show tables;" {
|
||||
tables, err := engine.DBMetas()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
|
||||
}
|
||||
} else {
|
||||
cnt, err := engine.Exec(scmd)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Printf("%d records changed.\n", cnt)
|
||||
}
|
||||
}
|
||||
scmd = ""
|
||||
fmt.Print("xorm$ ")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cnt, err := engine.Exec(scmd)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Printf("%d records changed.\n", cnt)
|
||||
}
|
||||
}
|
||||
scmd = ""
|
||||
fmt.Print("xorm$ ")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
160
xorm/xorm.go
160
xorm/xorm.go
|
|
@ -1,16 +1,16 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/dvirsky/go-pylog/logging"
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"text/template"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
"fmt"
|
||||
"github.com/dvirsky/go-pylog/logging"
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"text/template"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// +build go1.1
|
||||
|
|
@ -21,59 +21,59 @@ const go11tag = true
|
|||
// Commands lists the available commands and help topics.
|
||||
// The order here is the order in which they are printed by 'gopm help'.
|
||||
var commands = []*Command{
|
||||
CmdReverse,
|
||||
CmdShell,
|
||||
CmdReverse,
|
||||
CmdShell,
|
||||
}
|
||||
|
||||
func init() {
|
||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||
}
|
||||
|
||||
func main() {
|
||||
logging.SetLevel(logging.ALL)
|
||||
// Check length of arguments.
|
||||
args := os.Args[1:]
|
||||
if len(args) < 1 {
|
||||
usage()
|
||||
return
|
||||
}
|
||||
logging.SetLevel(logging.ALL)
|
||||
// Check length of arguments.
|
||||
args := os.Args[1:]
|
||||
if len(args) < 1 {
|
||||
usage()
|
||||
return
|
||||
}
|
||||
|
||||
// Show help documentation.
|
||||
if args[0] == "help" {
|
||||
help(args[1:])
|
||||
return
|
||||
}
|
||||
// Show help documentation.
|
||||
if args[0] == "help" {
|
||||
help(args[1:])
|
||||
return
|
||||
}
|
||||
|
||||
// Check commands and run.
|
||||
for _, comm := range commands {
|
||||
if comm.Name() == args[0] && comm.Run != nil {
|
||||
comm.Run(comm, args[1:])
|
||||
exit()
|
||||
return
|
||||
}
|
||||
}
|
||||
// Check commands and run.
|
||||
for _, comm := range commands {
|
||||
if comm.Name() == args[0] && comm.Run != nil {
|
||||
comm.Run(comm, args[1:])
|
||||
exit()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "xorm: unknown subcommand %q\nRun 'xorm help' for usage.\n", args[0])
|
||||
setExitStatus(2)
|
||||
exit()
|
||||
fmt.Fprintf(os.Stderr, "xorm: unknown subcommand %q\nRun 'xorm help' for usage.\n", args[0])
|
||||
setExitStatus(2)
|
||||
exit()
|
||||
}
|
||||
|
||||
var exitStatus = 0
|
||||
var exitMu sync.Mutex
|
||||
|
||||
func setExitStatus(n int) {
|
||||
exitMu.Lock()
|
||||
if exitStatus < n {
|
||||
exitStatus = n
|
||||
}
|
||||
exitMu.Unlock()
|
||||
exitMu.Lock()
|
||||
if exitStatus < n {
|
||||
exitStatus = n
|
||||
}
|
||||
exitMu.Unlock()
|
||||
}
|
||||
|
||||
var usageTemplate = `xorm is a database tool based xorm package.
|
||||
|
||||
Usage:
|
||||
|
||||
xorm command [arguments]
|
||||
xorm command [arguments]
|
||||
|
||||
The commands are:
|
||||
{{range .}}{{if .Runnable}}
|
||||
|
|
@ -96,66 +96,66 @@ var helpTemplate = `{{if .Runnable}}usage: xorm {{.UsageLine}}
|
|||
|
||||
// tmpl executes the given template text on data, writing the result to w.
|
||||
func tmpl(w io.Writer, text string, data interface{}) {
|
||||
t := template.New("top")
|
||||
t.Funcs(template.FuncMap{"trim": strings.TrimSpace, "capitalize": capitalize})
|
||||
template.Must(t.Parse(text))
|
||||
if err := t.Execute(w, data); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
t := template.New("top")
|
||||
t.Funcs(template.FuncMap{"trim": strings.TrimSpace, "capitalize": capitalize})
|
||||
template.Must(t.Parse(text))
|
||||
if err := t.Execute(w, data); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func capitalize(s string) string {
|
||||
if s == "" {
|
||||
return s
|
||||
}
|
||||
r, n := utf8.DecodeRuneInString(s)
|
||||
return string(unicode.ToTitle(r)) + s[n:]
|
||||
if s == "" {
|
||||
return s
|
||||
}
|
||||
r, n := utf8.DecodeRuneInString(s)
|
||||
return string(unicode.ToTitle(r)) + s[n:]
|
||||
}
|
||||
|
||||
func printUsage(w io.Writer) {
|
||||
tmpl(w, usageTemplate, commands)
|
||||
tmpl(w, usageTemplate, commands)
|
||||
}
|
||||
|
||||
func usage() {
|
||||
printUsage(os.Stderr)
|
||||
os.Exit(2)
|
||||
printUsage(os.Stderr)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
// help implements the 'help' command.
|
||||
func help(args []string) {
|
||||
if len(args) == 0 {
|
||||
printUsage(os.Stdout)
|
||||
// not exit 2: succeeded at 'gopm help'.
|
||||
return
|
||||
}
|
||||
if len(args) != 1 {
|
||||
fmt.Fprintf(os.Stderr, "usage: xorm help command\n\nToo many arguments given.\n")
|
||||
os.Exit(2) // failed at 'gopm help'
|
||||
}
|
||||
if len(args) == 0 {
|
||||
printUsage(os.Stdout)
|
||||
// not exit 2: succeeded at 'gopm help'.
|
||||
return
|
||||
}
|
||||
if len(args) != 1 {
|
||||
fmt.Fprintf(os.Stderr, "usage: xorm help command\n\nToo many arguments given.\n")
|
||||
os.Exit(2) // failed at 'gopm help'
|
||||
}
|
||||
|
||||
arg := args[0]
|
||||
arg := args[0]
|
||||
|
||||
for _, cmd := range commands {
|
||||
if cmd.Name() == arg {
|
||||
tmpl(os.Stdout, helpTemplate, cmd)
|
||||
// not exit 2: succeeded at 'gopm help cmd'.
|
||||
return
|
||||
}
|
||||
}
|
||||
for _, cmd := range commands {
|
||||
if cmd.Name() == arg {
|
||||
tmpl(os.Stdout, helpTemplate, cmd)
|
||||
// not exit 2: succeeded at 'gopm help cmd'.
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run 'xorm help'.\n", arg)
|
||||
os.Exit(2) // failed at 'gopm help cmd'
|
||||
fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run 'xorm help'.\n", arg)
|
||||
os.Exit(2) // failed at 'gopm help cmd'
|
||||
}
|
||||
|
||||
var atexitFuncs []func()
|
||||
|
||||
func atexit(f func()) {
|
||||
atexitFuncs = append(atexitFuncs, f)
|
||||
atexitFuncs = append(atexitFuncs, f)
|
||||
}
|
||||
|
||||
func exit() {
|
||||
for _, f := range atexitFuncs {
|
||||
f()
|
||||
}
|
||||
os.Exit(exitStatus)
|
||||
for _, f := range atexitFuncs {
|
||||
f()
|
||||
}
|
||||
os.Exit(exitStatus)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue