xormmodule

package module
v0.0.0-...-6c86404 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2014 License: BSD-3-Clause Imports: 2 Imported by: 0

README

Revel XORM Module

This is Revel Module to enable XORM in the Revel framework.

Activation

To activate it, add the module to your app.conf:

module.xorm = github.com/nashtsai/xormrevelmodule

Options

This module takes Revel db module options:

db.import = github.com/go-sql-driver/mysql	# golang db driver
db.driver = mysql							# driver name
db.spec = root:@/mydb?charset=utf8			# datasource name

In addition you can set the maximum number of connections in the idle connection pool and maximum number of open connections to the database:

db.maxidleconns = 10
db.maxopenconns = 50

XORM specific options:

xorm.showsql = true 	# show SQL
xorm.showdebug = true	# show XORM debug info

Using XORM controller

Add anonymous xormmodule.XormController or anonymous xormmodule.XormSessionController member to your revel controller struct:

import (
	...
	"github.com/nashtsai/xormrevelmodule"
	"github.com/revel/revel"
	...
)

type MyXormController struct {
	*revel.Controller
	xormmodule.XormController
}

func (c MyXormController) List() revel.Result {
    users := make([]*Userinfo, 0)
    c.Engine.Find(&users)
	return c.Render(users)
}

type MyXormSessionController struct {
	*revel.Controller
	xormmodule.XormSessionController
}

func (c MyXormSessionController) Delete(id int64) revel.Result {
    _, err: = c.XormSession.Delete(&UserInfo{id:id})
	return c.Render(err)
}

Post XORM engine init. handler

Post init. handler after xorm.Engine is initialized:

import (
	"github.com/go-xorm/xorm"
	"github.com/nashtsai/xormrevelmodule"
)

func init() {
	xormmodule.AddPostInitProcessor(func(engine *xorm.Engine){
		// your own init code, i.e., engine.Sync
    })
}

Advanced topics

Go Walker API references

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Engine       *xorm.Engine
	Driver       string
	Spec         string
	MaxIdleConns int
	MaxOpenConns int
	ShowSQL      bool
	ShowDebug    bool
)

Functions

func AddPostInitProcessor

func AddPostInitProcessor(processor PostInitProcessorFunc)

Add post xorm.Engine init. handler. Common use cases are sync domain models, custom mapper, etc.

func Init

func Init()

Types

type PostInitProcessorFunc

type PostInitProcessorFunc func(*xorm.Engine)

type SessionHandlerFunc

type SessionHandlerFunc func(*xorm.Session) error

type XormController

type XormController struct {
	Engine      *xorm.Engine
	XormSession *xorm.Session
}

XormController to be added as anonymous member to the revel controller struct, with XormController.Engine attached.

  type MyXormController struct {
	 	*revel.Controller
	 	xormmodule.XormController
  }

func (*XormController) Attach

func (c *XormController) Attach() revel.Result

Attach XormController.Engine, this is automatically done at revel.BEFORE revel.InterceptMethod step.

func (*XormController) AttachSession

func (c *XormController) AttachSession()

Create xorm.Session and attached to XormSession member if not already attached.

func (*XormController) Commit

func (c *XormController) Commit() revel.Result

Commit and Close attached XormSession, this is automatically done at revel.AFTER revel.InterceptMethod step. Issue panic if commit XormSession is undesired.

func (*XormController) Detach

func (c *XormController) Detach() revel.Result

Detach XormController.Engine, this is automatically done at revel.FINALLY revel.InterceptMethod step.

func (*XormController) DetachSession

func (c *XormController) DetachSession()

Detach XormSession member and call xorm.Session.Close() if has attached XormSession.

func (*XormController) WithNewSession

func (c *XormController) WithNewSession(handler SessionHandlerFunc) error

Create a new xorm.Session and call handler, the xorm.Session will be closed after handler called. Where common use cases being required async DB operation in another thread.

func (c MyXormController) List() revel.Result {
	// async fetch xorm.Session operation
	go c.WithNewSession(func(s *xorm.Session) {
		// fetch list from s
	})
}

func (*XormController) WithNewTx

func (c *XormController) WithNewTx(handler SessionHandlerFunc) error

Begin a SQL transaction and if handler did not return error it will commit the transaction, otherwise rollback the transaction. This will create a new xorm.Session for the handler. Where common use cases being required async DB operation in another thread.

func (*XormController) WithSession

func (c *XormController) WithSession(handler SessionHandlerFunc) error

Attach XormSession and call handler.

func (c MyXormController) List() revel.Result {
	c.WithSession(func(s *xorm.Session) {
		// fetch list from s
	})
}

func (*XormController) WithTx

func (c *XormController) WithTx(handler SessionHandlerFunc) error

Begin a SQL transaction and if handler did not return error it will commit the transaction, otherwise rollback the transaction. This will use attached XormSession if already attached.

type XormSessionController

type XormSessionController struct {
	XormController
}

XormSessionController to be added as anonymous member to the revel controller struct, with XormController.Engine and XormController.XormSession attached.

  type MyXormSessionController struct {
	 	*revel.Controller
	 	xormmodule.XormSessionController
  }

func (*XormSessionController) Attach

func (c *XormSessionController) Attach() revel.Result

Attach XormSessionController.XormSession, this is automatically done at revel.BEFORE revel.InterceptMethod step.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL