When using xorm, you can create multiple orm engines, an engine means a databse. So you can:
import (
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
)
var engine *xorm.Engine
func main() {
var err error
engine, err = xorm.NewEngine("mysql", "root:123@/test?charset=utf8")
}
or
import (
_ "github.com/mattn/go-sqlite3"
"github.com/go-xorm/xorm"
)
var engine *xorm.Engine
func main() {
var err error
engine, err = xorm.NewEngine("sqlite3", "./test.db")
}
You can create many engines for different databases.Generally, you just need create only one engine. Engine supports run on go routines.
When you want to manually close then engine, call engine.Close
. Generally, we don't need to do this, because engine will be closed automatically when application exit.
xorm supports four drivers now:
MyMysql: github.com/ziutek/mymysql
SQLite: github.com/mattn/go-sqlite3
Postgres: github.com/lib/pq
MsSql: github.com/lunny/godbc
NewEngine's parameters are the same as sql.Open
. So you should read the drivers' document for parameters' usage.
After engine created, you can do some settings.
1.Logs
engine.ShowSQL = true
, Show SQL statement on standard output;engine.ShowDebug = true
, Show debug infomation on standard output;engine.ShowError = true
, Show error infomation on standard output;engine.ShowWarn = true
, Show warnning information on standard output;2.If want to record infomation with another method: use engine.Logger
as io.Writer
:
f, err := os.Create("sql.log")
if err != nil {
println(err.Error())
return
}
engine.Logger = xorm.NewSimpleLogger(f)
3.Engine provide DB connection pool settings.
engine.SetMaxIdleConns()
to set idle connections.engine.SetMaxOpenConns()
to set Max connections. This methods support only Go 1.2+.