|
@@ -1,83 +1,47 @@
|
|
|
|
+/*
|
|
|
|
+When using xorm, you can create multiple orm engines, an engine means a database.
|
|
|
|
+ */
|
|
package main
|
|
package main
|
|
|
|
|
|
import (
|
|
import (
|
|
- _ "github.com/mattn/go-sqlite3"
|
|
|
|
- "github.com/go-xorm/xorm"
|
|
|
|
- "os"
|
|
|
|
|
|
+ // Core.
|
|
"fmt"
|
|
"fmt"
|
|
- "github.com/go-xorm/core"
|
|
|
|
- "github.com/davecgh/go-spew/spew"
|
|
|
|
-)
|
|
|
|
-
|
|
|
|
-type User struct {
|
|
|
|
- Id int64
|
|
|
|
- Name string `xorm:"varchar(25) not null unique 'usr_name'"`
|
|
|
|
-}
|
|
|
|
|
|
+ "os"
|
|
|
|
|
|
-type Article struct {
|
|
|
|
- Id int64
|
|
|
|
- Title string `xorm:"varchar(25) not null unique 'art_title'"`
|
|
|
|
-}
|
|
|
|
|
|
+ // SQLite 3
|
|
|
|
+ _ "github.com/mattn/go-sqlite3"
|
|
|
|
|
|
-func configureOrm(orm *xorm.Engine) {
|
|
|
|
- // Show SQL statement on standard output
|
|
|
|
- orm.ShowSQL = true
|
|
|
|
- // Show debug infomation on standard output
|
|
|
|
- orm.ShowDebug = true
|
|
|
|
- // Show error infomation on standard output;
|
|
|
|
- orm.ShowErr = true
|
|
|
|
- // Show warnning information on standard output;
|
|
|
|
- orm.ShowWarn = true
|
|
|
|
- // TODO Show which info ?
|
|
|
|
- orm.ShowInfo = true
|
|
|
|
|
|
+ // MySQL. Use either driver.
|
|
|
|
+ // _ "github.com/go-sql-driver/mysql"
|
|
|
|
+ // _ "github.com/ziutek/mymysql"
|
|
|
|
|
|
- f, err := os.Create("sql.log")
|
|
|
|
- if err != nil {
|
|
|
|
- println(err.Error())
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- orm.Logger = xorm.NewSimpleLogger(f)
|
|
|
|
|
|
+ // PostgreSQL
|
|
|
|
+ // _ "github.com/lib/pq"
|
|
|
|
|
|
- orm.SetMaxIdleConns(1)
|
|
|
|
- orm.SetMaxOpenConns(10)
|
|
|
|
|
|
+ // SQL Server
|
|
|
|
+ // _ "github.com/lunny/godbc"
|
|
|
|
|
|
- // Set mapper: GonicMapper, SameMapper, SnakeMapper (default).
|
|
|
|
- orm.SetMapper(core.SnakeMapper{})
|
|
|
|
-}
|
|
|
|
|
|
+ // XORM.
|
|
|
|
+ "github.com/go-xorm/xorm"
|
|
|
|
|
|
-func addData(orm *xorm.Engine) {
|
|
|
|
- u1 := new(User)
|
|
|
|
- u1.Name = "FGM";
|
|
|
|
- x, err := orm.Insert(u1)
|
|
|
|
- if err != nil {
|
|
|
|
- fmt.Println(err)
|
|
|
|
- os.Exit(3)
|
|
|
|
- }
|
|
|
|
- spew.Dump(x, u1)
|
|
|
|
-}
|
|
|
|
|
|
+ // Others.
|
|
|
|
+ "github.com/davecgh/go-spew/spew"
|
|
|
|
+)
|
|
|
|
|
|
-func check(e error) {
|
|
|
|
- if e != nil {
|
|
|
|
- panic(e)
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
|
|
+var engine *xorm.Engine
|
|
|
|
|
|
func main() {
|
|
func main() {
|
|
- orm, err := xorm.NewEngine("sqlite3", "./test.db")
|
|
|
|
|
|
+ var err error
|
|
|
|
+
|
|
|
|
+ // NewEngine parameters are those of the SQL driver: driver and DSN.
|
|
|
|
+ engine, err := xorm.NewEngine("sqlite3", "./test.db")
|
|
if err != nil {
|
|
if err != nil {
|
|
panic(err)
|
|
panic(err)
|
|
os.Exit(1)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
fmt.Println("ORM loaded")
|
|
fmt.Println("ORM loaded")
|
|
- configureOrm(orm)
|
|
|
|
-
|
|
|
|
- err = orm.Sync(new(User), new(Article))
|
|
|
|
- if err != nil {
|
|
|
|
- panic(err)
|
|
|
|
- os.Exit(2)
|
|
|
|
- }
|
|
|
|
- fmt.Println("ORM synced")
|
|
|
|
|
|
|
|
- addData(orm)
|
|
|
|
- orm.Close()
|
|
|
|
|
|
+ spew.Dump(engine)
|
|
|
|
+ // Not needed: called automatically on exit.
|
|
|
|
+ engine.Close()
|
|
}
|
|
}
|