bug fixed

This commit is contained in:
Lunny Xiao 2014-01-25 10:31:07 +08:00
parent e77fca31ae
commit 56700ee336
14 changed files with 114 additions and 103 deletions

View File

@ -515,7 +515,7 @@ func mappingTable(t reflect.Type, tableMapper core.IMapper, colMapper core.IMapp
if col.Length2 == 0 { if col.Length2 == 0 {
col.Length2 = col.SQLType.DefaultLength2 col.Length2 = col.SQLType.DefaultLength2
} }
fmt.Println("======", col) //fmt.Println("======", col)
if col.Name == "" { if col.Name == "" {
col.Name = colMapper.Obj2Table(t.Field(i).Name) col.Name = colMapper.Obj2Table(t.Field(i).Name)
} }

View File

@ -2,9 +2,11 @@ package main
import ( import (
"fmt" "fmt"
"github.com/lunny/xorm"
_ "github.com/mattn/go-sqlite3"
"os" "os"
"github.com/lunny/xorm"
"github.com/lunny/xorm/caches"
_ "github.com/mattn/go-sqlite3"
) )
type User struct { type User struct {
@ -22,7 +24,7 @@ func main() {
return return
} }
Orm.ShowSQL = true Orm.ShowSQL = true
cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000) cacher := xorm.NewLRUCacher(caches.NewMemoryStore(), 1000)
Orm.SetDefaultCacher(cacher) Orm.SetDefaultCacher(cacher)
err = Orm.CreateTables(&User{}) err = Orm.CreateTables(&User{})

View File

@ -3,9 +3,10 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"github.com/lunny/xorm" "github.com/lunny/xorm"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"os"
) )
type Status struct { type Status struct {
@ -47,7 +48,7 @@ func main() {
f := "conversion.db" f := "conversion.db"
os.Remove(f) os.Remove(f)
Orm, err := NewEngine("sqlite3", f) Orm, err := xorm.NewEngine("sqlite3", f)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return

View File

@ -2,9 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"os"
"github.com/lunny/xorm" "github.com/lunny/xorm"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"os"
) )
type User struct { type User struct {
@ -27,7 +28,7 @@ func main() {
f := "derive.db" f := "derive.db"
os.Remove(f) os.Remove(f)
Orm, err := NewEngine("sqlite3", f) Orm, err := xorm.NewEngine("sqlite3", f)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return

View File

@ -2,11 +2,12 @@ package main
import ( import (
"fmt" "fmt"
"os"
"runtime"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/lunny/xorm" "github.com/lunny/xorm"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"os"
"runtime"
) )
type User struct { type User struct {
@ -38,14 +39,14 @@ func test(engine *xorm.Engine) {
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
go func(x int) { go func(x int) {
//x := i //x := i
err := engine.Test() err := engine.Ping()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} else { } else {
err = engine.Map(u) /*err = engine.(u)
if err != nil { if err != nil {
fmt.Println("Map user failed") fmt.Println("Map user failed")
} else { } else {*/
for j := 0; j < 10; j++ { for j := 0; j < 10; j++ {
if x+j < 2 { if x+j < 2 {
_, err = engine.Get(u) _, err = engine.Get(u)
@ -66,7 +67,7 @@ func test(engine *xorm.Engine) {
} }
} }
fmt.Printf("%v success!\n", x) fmt.Printf("%v success!\n", x)
} //}
} }
queue <- x queue <- x
}(i) }(i)
@ -82,7 +83,7 @@ func test(engine *xorm.Engine) {
} }
func main() { func main() {
runtime.GOMAXPROCS(2) runtime.GOMAXPROCS(1)
fmt.Println("-----start sqlite go routines-----") fmt.Println("-----start sqlite go routines-----")
engine, err := sqliteEngine() engine, err := sqliteEngine()
if err != nil { if err != nil {

View File

@ -2,12 +2,12 @@ package main
import ( import (
"fmt" "fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/lunny/xorm"
xorm "github.com/lunny/xorm"
_ "github.com/mattn/go-sqlite3"
"os" "os"
"runtime" "runtime"
_ "github.com/go-sql-driver/mysql"
"github.com/lunny/xorm"
_ "github.com/mattn/go-sqlite3"
) )
type User struct { type User struct {
@ -41,14 +41,14 @@ func test(engine *xorm.Engine) {
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
go func(x int) { go func(x int) {
//x := i //x := i
err := engine.Test() err := engine.Ping()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} else { } else {
err = engine.Map(u) /*err = engine.Map(u)
if err != nil { if err != nil {
fmt.Println("Map user failed") fmt.Println("Map user failed")
} else { } else {*/
for j := 0; j < 10; j++ { for j := 0; j < 10; j++ {
if x+j < 2 { if x+j < 2 {
_, err = engine.Get(u) _, err = engine.Get(u)
@ -69,7 +69,7 @@ func test(engine *xorm.Engine) {
} }
} }
fmt.Printf("%v success!\n", x) fmt.Printf("%v success!\n", x)
} //}
} }
queue <- x queue <- x
}(i) }(i)

View File

@ -2,9 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"os"
"github.com/lunny/xorm" "github.com/lunny/xorm"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"os"
) )
type User struct { type User struct {
@ -16,12 +17,12 @@ func main() {
f := "pool.db" f := "pool.db"
os.Remove(f) os.Remove(f)
Orm, err := NewEngine("sqlite3", f) Orm, err := xorm.NewEngine("sqlite3", f)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
err = Orm.SetPool(NewSimpleConnectPool()) err = Orm.SetPool(xorm.NewSimpleConnectPool())
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return

View File

@ -2,9 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"os"
"github.com/lunny/xorm" "github.com/lunny/xorm"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"os"
) )
type User struct { type User struct {
@ -26,7 +27,7 @@ func main() {
f := "singleMapping.db" f := "singleMapping.db"
os.Remove(f) os.Remove(f)
Orm, err := NewEngine("sqlite3", f) Orm, err := xorm.NewEngine("sqlite3", f)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return

View File

@ -588,7 +588,7 @@ func (statement *Statement) convertIdSql(sqlStr string) string {
if len(sqls) != 2 { if len(sqls) != 2 {
return "" return ""
} }
fmt.Println("-----", col) //fmt.Println("-----", col)
newsql := fmt.Sprintf("SELECT %v.%v FROM %v", statement.Engine.Quote(statement.TableName()), newsql := fmt.Sprintf("SELECT %v.%v FROM %v", statement.Engine.Quote(statement.TableName()),
statement.Engine.Quote(col.Name), sqls[1]) statement.Engine.Quote(col.Name), sqls[1])
return newsql return newsql

View File

@ -16,7 +16,7 @@ import (
) )
const ( const (
Version string = "0.3.1" Version string = "0.4"
) )
func close(engine *Engine) { func close(engine *Engine) {
@ -75,3 +75,7 @@ func NewLRUCacher(store core.CacheStore, max int) *caches.LRUCacher {
func NewLRUCacher2(store core.CacheStore, expired time.Duration, max int) *caches.LRUCacher { func NewLRUCacher2(store core.CacheStore, expired time.Duration, max int) *caches.LRUCacher {
return caches.NewLRUCacher(store, expired, 0, max) return caches.NewLRUCacher(store, expired, 0, max)
} }
func NewMemoryStore() *caches.MemoryStore {
return caches.NewMemoryStore()
}

View File

@ -2,9 +2,10 @@ package main
import ( import (
//"fmt" //"fmt"
"github.com/lunny/xorm"
"strings" "strings"
"text/template" "text/template"
"github.com/lunny/xorm/core"
) )
var ( var (
@ -18,27 +19,27 @@ var (
} }
) )
func cPlusTypeStr(col *xorm.Column) string { func cPlusTypeStr(col *core.Column) string {
tp := col.SQLType tp := col.SQLType
name := strings.ToUpper(tp.Name) name := strings.ToUpper(tp.Name)
switch name { switch name {
case xorm.Bit, xorm.TinyInt, xorm.SmallInt, xorm.MediumInt, xorm.Int, xorm.Integer, xorm.Serial: case core.Bit, core.TinyInt, core.SmallInt, core.MediumInt, core.Int, core.Integer, core.Serial:
return "int" return "int"
case xorm.BigInt, xorm.BigSerial: case core.BigInt, core.BigSerial:
return "__int64" return "__int64"
case xorm.Char, xorm.Varchar, xorm.TinyText, xorm.Text, xorm.MediumText, xorm.LongText: case core.Char, core.Varchar, core.TinyText, core.Text, core.MediumText, core.LongText:
return "tstring" return "tstring"
case xorm.Date, xorm.DateTime, xorm.Time, xorm.TimeStamp: case core.Date, core.DateTime, core.Time, core.TimeStamp:
return "time_t" return "time_t"
case xorm.Decimal, xorm.Numeric: case core.Decimal, core.Numeric:
return "tstring" return "tstring"
case xorm.Real, xorm.Float: case core.Real, core.Float:
return "float" return "float"
case xorm.Double: case core.Double:
return "double" return "double"
case xorm.TinyBlob, xorm.Blob, xorm.MediumBlob, xorm.LongBlob, xorm.Bytea: case core.TinyBlob, core.Blob, core.MediumBlob, core.LongBlob, core.Bytea:
return "tstring" return "tstring"
case xorm.Bool: case core.Bool:
return "bool" return "bool"
default: default:
return "tstring" return "tstring"
@ -46,11 +47,11 @@ func cPlusTypeStr(col *xorm.Column) string {
return "" return ""
} }
func genCPlusImports(tables []*xorm.Table) map[string]string { func genCPlusImports(tables []*core.Table) map[string]string {
imports := make(map[string]string) imports := make(map[string]string)
for _, table := range tables { for _, table := range tables {
for _, col := range table.Columns { for _, col := range table.Columns() {
switch cPlusTypeStr(col) { switch cPlusTypeStr(col) {
case "time_t": case "time_t":
imports[`<time.h>`] = `<time.h>` imports[`<time.h>`] = `<time.h>`

View File

@ -3,11 +3,11 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/lunny/xorm"
"go/format" "go/format"
"reflect" "reflect"
"strings" "strings"
"text/template" "text/template"
"github.com/lunny/xorm/core"
) )
var ( var (
@ -157,7 +157,7 @@ func gt(arg1, arg2 interface{}) (bool, error) {
return !lessOrEqual, nil return !lessOrEqual, nil
} }
func getCol(cols map[string]*xorm.Column, name string) *xorm.Column { func getCol(cols map[string]*core.Column, name string) *core.Column {
return cols[name] return cols[name]
} }
@ -169,11 +169,11 @@ func formatGo(src string) (string, error) {
return string(source), nil return string(source), nil
} }
func genGoImports(tables []*xorm.Table) map[string]string { func genGoImports(tables []*core.Table) map[string]string {
imports := make(map[string]string) imports := make(map[string]string)
for _, table := range tables { for _, table := range tables {
for _, col := range table.Columns { for _, col := range table.Columns() {
if typestring(col) == "time.Time" { if typestring(col) == "time.Time" {
imports["time"] = "time" imports["time"] = "time"
} }
@ -182,12 +182,9 @@ func genGoImports(tables []*xorm.Table) map[string]string {
return imports return imports
} }
func typestring(col *xorm.Column) string { func typestring(col *core.Column) string {
st := col.SQLType st := col.SQLType
/*if col.IsPrimaryKey { t := core.SQLType2Type(st)
return "int64"
}*/
t := xorm.SQLType2Type(st)
s := t.String() s := t.String()
if s == "[]uint8" { if s == "[]uint8" {
return "[]byte" return "[]byte"
@ -195,7 +192,7 @@ func typestring(col *xorm.Column) string {
return s return s
} }
func tag(table *xorm.Table, col *xorm.Column) string { func tag(table *core.Table, col *core.Column) string {
isNameId := (mapper.Table2Obj(col.Name) == "Id") isNameId := (mapper.Table2Obj(col.Name) == "Id")
isIdPk := isNameId && typestring(col) == "int64" isIdPk := isNameId && typestring(col) == "int64"
@ -227,9 +224,9 @@ func tag(table *xorm.Table, col *xorm.Column) string {
for name, _ := range col.Indexes { for name, _ := range col.Indexes {
index := table.Indexes[name] index := table.Indexes[name]
var uistr string var uistr string
if index.Type == xorm.UniqueType { if index.Type == core.UniqueType {
uistr = "unique" uistr = "unique"
} else if index.Type == xorm.IndexType { } else if index.Type == core.IndexType {
uistr = "index" uistr = "index"
} }
if len(index.Cols) > 1 { if len(index.Cols) > 1 {

View File

@ -1,20 +1,20 @@
package main package main
import ( import (
"github.com/lunny/xorm"
"io/ioutil" "io/ioutil"
"strings" "strings"
"text/template" "text/template"
"github.com/lunny/xorm/core"
) )
type LangTmpl struct { type LangTmpl struct {
Funcs template.FuncMap Funcs template.FuncMap
Formater func(string) (string, error) Formater func(string) (string, error)
GenImports func([]*xorm.Table) map[string]string GenImports func([]*core.Table) map[string]string
} }
var ( var (
mapper = &xorm.SnakeMapper{} mapper = &core.SnakeMapper{}
langTmpls = map[string]LangTmpl{ langTmpls = map[string]LangTmpl{
"go": GoLangTmpl, "go": GoLangTmpl,
"c++": CPlusTmpl, "c++": CPlusTmpl,

View File

@ -3,18 +3,20 @@ package main
import ( import (
"bytes" "bytes"
"fmt" "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" "io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"text/template" "text/template"
_ "github.com/bylevel/pq"
"github.com/dvirsky/go-pylog/logging"
_ "github.com/go-sql-driver/mysql"
"github.com/lunny/xorm"
"github.com/lunny/xorm/core"
_ "github.com/mattn/go-sqlite3"
_ "github.com/ziutek/mymysql/godrv"
) )
var CmdReverse = &Command{ var CmdReverse = &Command{
@ -48,7 +50,7 @@ func printReversePrompt(flag string) {
} }
type Tmpl struct { type Tmpl struct {
Tables []*xorm.Table Tables []*core.Table
Imports map[string]string Imports map[string]string
Model string Model string
} }
@ -188,7 +190,7 @@ func runReverse(cmd *Command, args []string) {
imports := langTmpl.GenImports(tables) imports := langTmpl.GenImports(tables)
tbls := make([]*xorm.Table, 0) tbls := make([]*core.Table, 0)
for _, table := range tables { for _, table := range tables {
tbls = append(tbls, table) tbls = append(tbls, table)
} }
@ -223,7 +225,7 @@ func runReverse(cmd *Command, args []string) {
} else { } else {
for _, table := range tables { for _, table := range tables {
// imports // imports
tbs := []*xorm.Table{table} tbs := []*core.Table{table}
imports := langTmpl.GenImports(tbs) imports := langTmpl.GenImports(tbs)
w, err := os.OpenFile(path.Join(genDir, unTitle(mapper.Table2Obj(table.Name))+ext), os.O_RDWR|os.O_CREATE, 0600) w, err := os.OpenFile(path.Join(genDir, unTitle(mapper.Table2Obj(table.Name))+ext), os.O_RDWR|os.O_CREATE, 0600)