2013-05-06 08:01:17 +00:00
# xorm
===========
2013-05-03 07:26:51 +00:00
2013-05-03 07:32:52 +00:00
[中文 ](README_CN.md )
xorm is an ORM for Go. It lets you map Go structs to tables in a database.
2013-05-03 07:26:51 +00:00
Right now, it interfaces with Mysql/SQLite. The goal however is to add support for PostgreSQL/DB2/MS ADODB/ODBC/Oracle in the future.
All in all, it's not entirely ready for advanced use yet, but it's getting there.
Drivers for Go's sql package which support database/sql includes:
2013-05-06 08:01:17 +00:00
Mysql: [github.com/ziutek/mymysql/godrv ](https://github.com/ziutek/mymysql/godrv )
2013-05-03 07:26:51 +00:00
2013-05-06 08:01:17 +00:00
Mysql: [github.com/Go-SQL-Driver/MySQL ](https://github.com/Go-SQL-Driver/MySQL )
2013-05-03 07:26:51 +00:00
2013-05-06 08:01:17 +00:00
SQLite: [github.com/mattn/go-sqlite3 ](https://github.com/mattn/go-sqlite3 )
2013-05-03 07:26:51 +00:00
2013-05-06 08:01:17 +00:00
## Installing xorm
go get github.com/lunny/xorm
2013-05-03 07:26:51 +00:00
2013-05-06 08:01:17 +00:00
## Quick Start
2013-05-03 07:26:51 +00:00
2013-05-06 08:01:17 +00:00
1.Create a database engine (for example: mysql)
engine := xorm.Create("mysql://root:123@localhost/test")
2013-05-03 07:26:51 +00:00
2013-05-03 07:41:50 +00:00
2.Define your struct
2013-05-03 07:26:51 +00:00
2013-05-06 08:01:17 +00:00
type User struct {
Id int
Name string
Age int `xorm:"-"`
}
2013-05-03 07:26:51 +00:00
for Simple Task, just use engine's functions:
2013-05-06 08:01:17 +00:00
before beginning, you should create a database in mysql and then we will create the tables.
err := engine.CreateTables(& User{})
2013-05-03 07:26:51 +00:00
then, insert an struct to table
2013-05-06 08:01:17 +00:00
id, err := engine.Insert(& User{Name:"lunny"})
2013-05-03 07:26:51 +00:00
or you want to update this struct
2013-05-06 08:01:17 +00:00
user := User{Id:1, Name:"xlw"}
rows, err := engine.Update(& user)
2013-05-03 07:26:51 +00:00
2013-05-03 07:41:50 +00:00
3.Fetch a single object by user
2013-05-03 07:26:51 +00:00
2013-05-06 08:01:17 +00:00
var user = User{Id:27}
engine.Get(& user)
2013-05-03 07:26:51 +00:00
2013-05-06 08:01:17 +00:00
var user = User{Name:"xlw"}
engine.Get(& user)
##Deep Use
2013-05-03 07:26:51 +00:00
for deep use, you should create a session, this func will create a connection to db
2013-05-06 08:01:17 +00:00
session, err := engine.MakeSession()
defer session.Close()
if err != nil {
return
}
2013-05-03 07:26:51 +00:00
2013-05-03 07:41:50 +00:00
1.Fetch a single object by where
2013-05-03 07:26:51 +00:00
2013-05-06 08:01:17 +00:00
var user Userinfo
session.Where("id=?", 27).Get(& user)
2013-05-03 07:26:51 +00:00
2013-05-06 08:01:17 +00:00
var user2 Userinfo
session.Where(3).Get(& user2) // this is shorthand for the version above
var user3 Userinfo
session.Where("name = ?", "john").Get(& user3) // more complex query
var user4 Userinfo
session.Where("name = ? and age < ?", "john", 88).Get(& user4) // even more complex
2013-05-03 07:26:51 +00:00
2013-05-03 07:41:50 +00:00
2.Fetch multiple objects
2013-05-03 07:26:51 +00:00
2013-05-06 08:01:17 +00:00
var allusers []Userinfo
err := session.Where("id > ?", "3").Limit(10,20).Find(& allusers) //Get id>3 limit 10 offset 20
var tenusers []Userinfo
err := session.Where("id > ?", "3").Limit(10).Find(& tenusers) //Get id>3 limit 10 if omit offset the default is 0
var everyone []Userinfo
err := session.Find(& everyone)
2013-05-03 07:26:51 +00:00
2013-05-06 08:01:17 +00:00
##***Mapping Rules***
2013-05-03 07:41:50 +00:00
1.Struct and struct's fields name should be Pascal style, and the table and column's name default is us
2013-05-03 07:26:51 +00:00
for example:
The structs Name 'UserInfo' will turn into the table name 'user_info', the same as the keyname.
If the keyname is 'UserName' will turn into the select colum 'user_name'
2013-05-03 07:41:50 +00:00
2.You have two method to change the rule. One is implement your own Map interface according IMapper, you can find the interface in mapper.go and set it to engine.Mapper
2013-05-03 07:26:51 +00:00
another is use field tag, field tag support the below keywords:
2013-05-06 08:01:17 +00:00
* [name] column name
* pk the field is a primary key
* int(11)/varchar(50) column type
* autoincr auto incrment
* [not ]null if column can be null value
* unique unique
* \- this field is not map as a table column
##FAQ
1.How the xorm tag use both with json?
use space
type User struct {
User string `json:"user" orm:"user_id"`
}
2013-05-03 07:26:51 +00:00
## LICENSE
BSD License
[http://creativecommons.org/licenses/BSD/ ](http://creativecommons.org/licenses/BSD/ )