diff --git a/engine.go b/engine.go index dc3fd637..7639310d 100644 --- a/engine.go +++ b/engine.go @@ -515,7 +515,7 @@ func mappingTable(t reflect.Type, tableMapper core.IMapper, colMapper core.IMapp if col.Length2 == 0 { col.Length2 = col.SQLType.DefaultLength2 } - fmt.Println("======", col) + //fmt.Println("======", col) if col.Name == "" { col.Name = colMapper.Obj2Table(t.Field(i).Name) } diff --git a/examples/cache.go b/examples/cache.go index d543cad2..8fd8f4a8 100644 --- a/examples/cache.go +++ b/examples/cache.go @@ -2,9 +2,11 @@ package main import ( "fmt" - "github.com/lunny/xorm" - _ "github.com/mattn/go-sqlite3" "os" + + "github.com/lunny/xorm" + "github.com/lunny/xorm/caches" + _ "github.com/mattn/go-sqlite3" ) type User struct { @@ -22,7 +24,7 @@ func main() { return } Orm.ShowSQL = true - cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000) + cacher := xorm.NewLRUCacher(caches.NewMemoryStore(), 1000) Orm.SetDefaultCacher(cacher) err = Orm.CreateTables(&User{}) diff --git a/examples/conversion.go b/examples/conversion.go index b74207ba..f16869c4 100644 --- a/examples/conversion.go +++ b/examples/conversion.go @@ -3,9 +3,10 @@ package main import ( "errors" "fmt" + "os" + "github.com/lunny/xorm" _ "github.com/mattn/go-sqlite3" - "os" ) type Status struct { @@ -47,7 +48,7 @@ func main() { f := "conversion.db" os.Remove(f) - Orm, err := NewEngine("sqlite3", f) + Orm, err := xorm.NewEngine("sqlite3", f) if err != nil { fmt.Println(err) return diff --git a/examples/derive.go b/examples/derive.go index f3480521..826b4271 100644 --- a/examples/derive.go +++ b/examples/derive.go @@ -2,9 +2,10 @@ package main import ( "fmt" + "os" + "github.com/lunny/xorm" _ "github.com/mattn/go-sqlite3" - "os" ) type User struct { @@ -27,7 +28,7 @@ func main() { f := "derive.db" os.Remove(f) - Orm, err := NewEngine("sqlite3", f) + Orm, err := xorm.NewEngine("sqlite3", f) if err != nil { fmt.Println(err) return diff --git a/examples/goroutine.go b/examples/goroutine.go index 16c0b242..6d45c924 100644 --- a/examples/goroutine.go +++ b/examples/goroutine.go @@ -2,11 +2,12 @@ package main import ( "fmt" + "os" + "runtime" + _ "github.com/go-sql-driver/mysql" "github.com/lunny/xorm" _ "github.com/mattn/go-sqlite3" - "os" - "runtime" ) type User struct { @@ -38,35 +39,35 @@ func test(engine *xorm.Engine) { for i := 0; i < size; i++ { go func(x int) { //x := i - err := engine.Test() + err := engine.Ping() if err != nil { fmt.Println(err) } else { - err = engine.Map(u) + /*err = engine.(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 - } + } 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) } + fmt.Printf("%v success!\n", x) + //} } queue <- x }(i) @@ -82,7 +83,7 @@ func test(engine *xorm.Engine) { } func main() { - runtime.GOMAXPROCS(2) + runtime.GOMAXPROCS(1) fmt.Println("-----start sqlite go routines-----") engine, err := sqliteEngine() if err != nil { diff --git a/examples/maxconnect.go b/examples/maxconnect.go index e3fbfea7..e383de13 100644 --- a/examples/maxconnect.go +++ b/examples/maxconnect.go @@ -2,12 +2,12 @@ package main import ( "fmt" - _ "github.com/go-sql-driver/mysql" - "github.com/lunny/xorm" - xorm "github.com/lunny/xorm" - _ "github.com/mattn/go-sqlite3" "os" "runtime" + + _ "github.com/go-sql-driver/mysql" + "github.com/lunny/xorm" + _ "github.com/mattn/go-sqlite3" ) type User struct { @@ -41,35 +41,35 @@ func test(engine *xorm.Engine) { for i := 0; i < size; i++ { go func(x int) { //x := i - err := engine.Test() + err := engine.Ping() if err != nil { fmt.Println(err) } else { - err = engine.Map(u) + /*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 - } + } 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) } + fmt.Printf("%v success!\n", x) + //} } queue <- x }(i) diff --git a/examples/pool.go b/examples/pool.go index d6ec0de7..746cdf8d 100644 --- a/examples/pool.go +++ b/examples/pool.go @@ -2,9 +2,10 @@ package main import ( "fmt" + "os" + "github.com/lunny/xorm" _ "github.com/mattn/go-sqlite3" - "os" ) type User struct { @@ -16,12 +17,12 @@ func main() { f := "pool.db" os.Remove(f) - Orm, err := NewEngine("sqlite3", f) + Orm, err := xorm.NewEngine("sqlite3", f) if err != nil { fmt.Println(err) return } - err = Orm.SetPool(NewSimpleConnectPool()) + err = Orm.SetPool(xorm.NewSimpleConnectPool()) if err != nil { fmt.Println(err) return diff --git a/examples/singlemapping.go b/examples/singlemapping.go index 932a0589..6954d7ee 100644 --- a/examples/singlemapping.go +++ b/examples/singlemapping.go @@ -2,9 +2,10 @@ package main import ( "fmt" + "os" + "github.com/lunny/xorm" _ "github.com/mattn/go-sqlite3" - "os" ) type User struct { @@ -26,7 +27,7 @@ func main() { f := "singleMapping.db" os.Remove(f) - Orm, err := NewEngine("sqlite3", f) + Orm, err := xorm.NewEngine("sqlite3", f) if err != nil { fmt.Println(err) return diff --git a/session.go b/session.go index 382370c7..dae0f935 100644 --- a/session.go +++ b/session.go @@ -588,7 +588,7 @@ func (statement *Statement) convertIdSql(sqlStr string) string { if len(sqls) != 2 { return "" } - fmt.Println("-----", col) + //fmt.Println("-----", col) newsql := fmt.Sprintf("SELECT %v.%v FROM %v", statement.Engine.Quote(statement.TableName()), statement.Engine.Quote(col.Name), sqls[1]) return newsql diff --git a/xorm.go b/xorm.go index 7ea51473..a1f6a5d8 100644 --- a/xorm.go +++ b/xorm.go @@ -16,7 +16,7 @@ import ( ) const ( - Version string = "0.3.1" + Version string = "0.4" ) 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 { return caches.NewLRUCacher(store, expired, 0, max) } + +func NewMemoryStore() *caches.MemoryStore { + return caches.NewMemoryStore() +} diff --git a/xorm/c++.go b/xorm/c++.go index ed094060..bbfdd3b1 100644 --- a/xorm/c++.go +++ b/xorm/c++.go @@ -2,9 +2,10 @@ package main import ( //"fmt" - "github.com/lunny/xorm" "strings" "text/template" + + "github.com/lunny/xorm/core" ) var ( @@ -18,27 +19,27 @@ var ( } ) -func cPlusTypeStr(col *xorm.Column) string { +func cPlusTypeStr(col *core.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: + case core.Bit, core.TinyInt, core.SmallInt, core.MediumInt, core.Int, core.Integer, core.Serial: return "int" - case xorm.BigInt, xorm.BigSerial: + case core.BigInt, core.BigSerial: 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" - case xorm.Date, xorm.DateTime, xorm.Time, xorm.TimeStamp: + case core.Date, core.DateTime, core.Time, core.TimeStamp: return "time_t" - case xorm.Decimal, xorm.Numeric: + case core.Decimal, core.Numeric: return "tstring" - case xorm.Real, xorm.Float: + case core.Real, core.Float: return "float" - case xorm.Double: + case core.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" - case xorm.Bool: + case core.Bool: return "bool" default: return "tstring" @@ -46,11 +47,11 @@ func cPlusTypeStr(col *xorm.Column) string { return "" } -func genCPlusImports(tables []*xorm.Table) map[string]string { +func genCPlusImports(tables []*core.Table) map[string]string { imports := make(map[string]string) for _, table := range tables { - for _, col := range table.Columns { + for _, col := range table.Columns() { switch cPlusTypeStr(col) { case "time_t": imports[``] = `` diff --git a/xorm/go.go b/xorm/go.go index 3f46322d..880f902e 100644 --- a/xorm/go.go +++ b/xorm/go.go @@ -3,11 +3,11 @@ package main import ( "errors" "fmt" - "github.com/lunny/xorm" "go/format" "reflect" "strings" "text/template" + "github.com/lunny/xorm/core" ) var ( @@ -157,7 +157,7 @@ func gt(arg1, arg2 interface{}) (bool, error) { 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] } @@ -169,11 +169,11 @@ func formatGo(src string) (string, error) { 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) for _, table := range tables { - for _, col := range table.Columns { + for _, col := range table.Columns() { if typestring(col) == "time.Time" { imports["time"] = "time" } @@ -182,12 +182,9 @@ func genGoImports(tables []*xorm.Table) map[string]string { return imports } -func typestring(col *xorm.Column) string { +func typestring(col *core.Column) string { st := col.SQLType - /*if col.IsPrimaryKey { - return "int64" - }*/ - t := xorm.SQLType2Type(st) + t := core.SQLType2Type(st) s := t.String() if s == "[]uint8" { return "[]byte" @@ -195,7 +192,7 @@ func typestring(col *xorm.Column) string { 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") isIdPk := isNameId && typestring(col) == "int64" @@ -227,9 +224,9 @@ func tag(table *xorm.Table, col *xorm.Column) string { for name, _ := range col.Indexes { index := table.Indexes[name] var uistr string - if index.Type == xorm.UniqueType { + if index.Type == core.UniqueType { uistr = "unique" - } else if index.Type == xorm.IndexType { + } else if index.Type == core.IndexType { uistr = "index" } if len(index.Cols) > 1 { diff --git a/xorm/lang.go b/xorm/lang.go index 17d09a98..a4785647 100644 --- a/xorm/lang.go +++ b/xorm/lang.go @@ -1,20 +1,20 @@ package main import ( - "github.com/lunny/xorm" "io/ioutil" "strings" "text/template" + "github.com/lunny/xorm/core" ) type LangTmpl struct { Funcs template.FuncMap Formater func(string) (string, error) - GenImports func([]*xorm.Table) map[string]string + GenImports func([]*core.Table) map[string]string } var ( - mapper = &xorm.SnakeMapper{} + mapper = &core.SnakeMapper{} langTmpls = map[string]LangTmpl{ "go": GoLangTmpl, "c++": CPlusTmpl, diff --git a/xorm/reverse.go b/xorm/reverse.go index 9eae48d2..8c87ee27 100644 --- a/xorm/reverse.go +++ b/xorm/reverse.go @@ -3,18 +3,20 @@ 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" + + _ "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{ @@ -48,7 +50,7 @@ func printReversePrompt(flag string) { } type Tmpl struct { - Tables []*xorm.Table + Tables []*core.Table Imports map[string]string Model string } @@ -188,7 +190,7 @@ func runReverse(cmd *Command, args []string) { imports := langTmpl.GenImports(tables) - tbls := make([]*xorm.Table, 0) + tbls := make([]*core.Table, 0) for _, table := range tables { tbls = append(tbls, table) } @@ -223,7 +225,7 @@ func runReverse(cmd *Command, args []string) { } else { for _, table := range tables { // imports - tbs := []*xorm.Table{table} + tbs := []*core.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)