Переглянути джерело

Configure column/table name mapping.

Frederic G. MARAND 9 роки тому
батько
коміт
24a0c59482
1 змінених файлів з 28 додано та 7 видалено
  1. 28 7
      main.go

+ 28 - 7
main.go

@@ -1,6 +1,6 @@
 /*
 When using xorm, you can create multiple orm engines, an engine means a database.
- */
+*/
 package main
 
 import (
@@ -26,6 +26,7 @@ import (
 
 	// Others.
 	// "github.com/davecgh/go-spew/spew"
+	"github.com/go-xorm/core"
 )
 
 func check(err error) {
@@ -43,7 +44,7 @@ func NewOrm() *xorm.Engine {
 	return engine
 }
 
-func setupLogging(engine *xorm.Engine)  {
+func setupLogging(engine *xorm.Engine) {
 	// Enable built-in Logging features.
 	engine.ShowSQL = true
 	engine.ShowDebug = true
@@ -56,16 +57,36 @@ func setupLogging(engine *xorm.Engine)  {
 	engine.SetLogger(xorm.NewSimpleLogger(f))
 }
 
-func main() {
-	var engine *xorm.Engine = NewOrm()
-
-	setupLogging(engine)
-
+func setupConnections(engine *xorm.Engine) {
 	// Optional: limit connections.
 	const CONNECTION_LIMIT = 4
+
 	engine.SetMaxOpenConns(CONNECTION_LIMIT)
 	// The number of idle connections is bound by the number of open ones.
 	engine.SetMaxIdleConns(CONNECTION_LIMIT)
+}
+
+func setupMapping(engine *xorm.Engine) {
+	same := core.SameMapper{}
+	gonic := core.GonicMapper{}
+	snake := core.SnakeMapper{}
+
+	// Global mapping. Default is snake.
+	engine.SetMapper(same)
+
+	// Table-specific mapping.
+	engine.SetTableMapper(gonic)
+
+	// Column-specific mapping.
+	engine.SetColumnMapper(snake)
+}
+
+func main() {
+	var engine *xorm.Engine = NewOrm()
+
+	setupLogging(engine)
+	setupConnections(engine)
+	setupMapping(engine)
 
 	// Not needed: called automatically on exit.
 	engine.Close()