xorm/README.md

345 lines
7.0 KiB
Markdown
Raw Normal View History

2013-05-06 08:01:17 +00:00
# xorm
2013-05-03 07:26:51 +00:00
2013-05-12 05:43:09 +00:00
[中文](https://github.com/lunny/xorm/blob/master/README_CN.md)
2013-05-03 07:26:51 +00:00
2013-05-11 10:38:43 +00:00
xorm is an ORM for Go. It makes dabatabse operating simple.
2013-05-03 07:26:51 +00:00
2013-05-11 10:38:43 +00:00
It's not entirely ready for product use yet, but it's getting there.
## Drivers Support
Drivers for Go's sql package which currently support database/sql includes:
2013-05-03 07:26:51 +00:00
2013-05-11 10:38:43 +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-11 10:38:43 +00:00
* SQLite: [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)
## Changelog
* **v0.1.2** : Insert function now supports both struct and slice pointer parameters, batch inserting and auto transaction
2013-05-11 10:38:43 +00:00
* **v0.1.1** : Add Id, In functions and improved README
* **v0.1.0** : Inital release.
2013-05-09 02:48:30 +00:00
## Features
2013-05-11 10:38:43 +00:00
* Struct<->Table Mapping Supports, both name mapping and filed tags mapping
* Database Transaction Support
2013-05-09 02:48:30 +00:00
2013-05-11 10:38:43 +00:00
* Both ORM and SQL Operation Support
2013-05-09 02:48:30 +00:00
2013-05-11 10:38:43 +00:00
* Simply usage
2013-05-09 02:48:30 +00:00
2013-05-11 10:38:43 +00:00
* Support Id, In, Where, Limit, Join, Having functions and sturct as query conditions
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-11 10:38:43 +00:00
1.Create a database engine just like sql.Open, commonly you just need create once.
2013-05-12 05:43:09 +00:00
```Go
2013-05-11 13:27:36 +00:00
import (
_ "github.com/Go-SQL-Driver/MySQL"
"github.com/lunny/xorm"
)
2013-05-11 10:38:43 +00:00
engine := xorm.Create("mysql", "root:123@/test?charset=utf8")
```
2013-05-09 01:56:58 +00:00
or
```Go
2013-05-11 13:27:36 +00:00
import (
_ "github.com/mattn/go-sqlite3"
"github.com/lunny/xorm"
)
engine = xorm.Create("sqlite3", "./test.db")
2013-05-11 10:38:43 +00:00
```
2013-05-12 06:04:14 +00:00
1.1.If you want to show all generated SQL
```Go
2013-05-12 06:04:14 +00:00
engine.ShowSQL = true
```
2013-05-03 07:26:51 +00:00
2013-05-11 10:38:43 +00:00
2.Define a struct
2013-05-11 13:27:36 +00:00
```Go
2013-05-11 13:27:36 +00:00
type User struct {
Id int
Name string
Age int `xorm:"-"`
}
2013-05-12 06:04:14 +00:00
```
2013-05-12 06:25:36 +00:00
2.1.More mapping rules, please see [Mapping Rules](#mapping)
2013-05-03 07:26:51 +00:00
2013-05-11 10:38:43 +00:00
3.When you set up your program, you can use CreateTables to create database tables.
2013-05-06 08:01:17 +00:00
```Go
2013-05-11 13:27:36 +00:00
err := engine.CreateTables(&User{})
// or err := engine.Map(&User{}, &Article{})
// err = engine.CreateAll()
```
2013-05-06 08:01:17 +00:00
2013-05-11 13:27:36 +00:00
4.then, insert an struct to table
2013-05-06 08:01:17 +00:00
```Go
2013-05-11 13:27:36 +00:00
id, err := engine.Insert(&User{Name:"lunny"})
```
2013-05-03 07:26:51 +00:00
2013-05-11 10:38:43 +00:00
or if you want to update records
2013-05-11 13:27:36 +00:00
```Go
2013-05-11 13:27:36 +00:00
user := User{Name:"xlw"}
rows, err := engine.Update(&user, &User{Id:1})
// or rows, err := engine.Where("id = ?", 1).Update(&user)
// or rows, err := engine.Id(1).Update(&user)
```
2013-05-03 07:26:51 +00:00
2013-05-11 10:38:43 +00:00
5.Fetch a single object by user
2013-05-11 13:27:36 +00:00
```Go
2013-05-11 13:27:36 +00:00
var user = User{Id:27}
err := engine.Get(&user)
// or err := engine.Id(27).Get(&user)
2013-05-03 07:26:51 +00:00
2013-05-11 13:27:36 +00:00
var user = User{Name:"xlw"}
err := engine.Get(&user)
```
2013-05-08 13:42:22 +00:00
2013-05-11 10:38:43 +00:00
6.Fetch multipe objects, use Find
```Go
2013-05-11 13:27:36 +00:00
var everyone []Userinfo
err := engine.Find(&everyone)
```
2013-05-11 10:38:43 +00:00
6.1 also you can use Where, Limit
2013-05-08 13:42:22 +00:00
```Go
2013-05-11 13:27:36 +00:00
var allusers []Userinfo
err := engine.Where("id > ?", "3").Limit(10,20).Find(&allusers) //Get id>3 limit 10 offset 20
```
2013-05-11 10:38:43 +00:00
6.2 or you can use a struct query
2013-05-11 13:27:36 +00:00
```Go
2013-05-11 13:27:36 +00:00
var tenusers []Userinfo
err := engine.Limit(10).Find(&tenusers, &Userinfo{Name:"xlw"}) //Get All Name="xlw" limit 10 offset 0
```
2013-05-08 13:42:22 +00:00
2013-05-11 10:38:43 +00:00
6.3 or In function
2013-05-08 13:42:22 +00:00
```Go
2013-05-11 13:27:36 +00:00
var tenusers []Userinfo
err := engine.In("id", 1, 3, 5).Find(&tenusers) //Get All id in (1, 3, 5)
```
2013-05-11 10:38:43 +00:00
7.Delete
2013-05-08 13:42:22 +00:00
```Go
2013-05-11 13:27:36 +00:00
err := engine.Delete(&User{Id:1})
// or err := engine.Id(1).Delete(&User{})
```
2013-05-11 10:38:43 +00:00
8.Count
```Go
2013-05-11 13:27:36 +00:00
total, err := engine.Count(&User{Name:"xlw"})
```
2013-05-08 13:42:22 +00:00
2013-05-16 06:16:39 +00:00
2013-05-11 10:38:43 +00:00
##Execute SQL
2013-05-16 06:12:27 +00:00
2013-05-11 10:38:43 +00:00
Of course, SQL execution is also provided.
1.if select then use Query
2013-05-08 13:42:22 +00:00
```Go
2013-05-11 13:27:36 +00:00
sql := "select * from userinfo"
results, err := engine.Query(sql)
```
2013-05-11 10:38:43 +00:00
2.if insert, update or delete then use Exec
```Go
2013-05-11 13:27:36 +00:00
sql = "update userinfo set username=? where id=?"
res, err := engine.Exec(sql, "xiaolun", 1)
```
2013-05-16 06:16:39 +00:00
2013-05-06 08:01:17 +00:00
2013-05-12 06:19:38 +00:00
##Advanced Usage
2013-05-16 06:12:27 +00:00
2013-05-11 10:38:43 +00:00
for deep usage, you should create a session, this func will create a database connection immediatelly
2013-05-11 13:27:36 +00:00
```Go
2013-05-11 13:27:36 +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-11 13:27:36 +00:00
```Go
2013-05-11 13:27:36 +00:00
var user Userinfo
session.Where("id=?", 27).Get(&user)
2013-05-03 07:41:50 +00:00
2013-05-11 13:27:36 +00:00
var user2 Userinfo
session.Where("name = ?", "john").Get(&user3) // more complex query
2013-05-03 07:26:51 +00:00
2013-05-11 13:27:36 +00:00
var user3 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-11 13:27:36 +00:00
```Go
2013-05-11 13:27:36 +00:00
var allusers []Userinfo
err := session.Where("id > ?", "3").Limit(10,20).Find(&allusers) //Get id>3 limit 10 offset 20
2013-05-03 07:26:51 +00:00
2013-05-11 13:27:36 +00:00
var tenusers []Userinfo
err := session.Limit(10).Find(&tenusers, &Userinfo{Name:"xlw"}) //Get All Name="xlw" limit 10 if omit offset the default is 0
2013-05-06 08:01:17 +00:00
2013-05-11 13:27:36 +00:00
var everyone []Userinfo
err := session.Find(&everyone)
```
2013-05-08 13:42:22 +00:00
3.Transaction
```Go
2013-05-11 13:27:36 +00:00
// add Begin() before any action
session.Begin()
user1 := Userinfo{Username: "xiaoxiao", Departname: "dev", Alias: "lunny", Created: time.Now()}
_, err = session.Insert(&user1)
if err != nil {
session.Rollback()
return
}
user2 := Userinfo{Username: "yyy"}
_, err = session.Where("id = ?", 2).Update(&user2)
if err != nil {
session.Rollback()
return
}
_, err = session.Delete(&user2)
if err != nil {
session.Rollback()
return
}
// add Commit() after all actions
err = session.Commit()
if err != nil {
return
}
```
2013-05-08 13:42:22 +00:00
4.Mixed Transaction
```Go
2013-05-11 13:27:36 +00:00
// add Begin() before any action
session.Begin()
user1 := Userinfo{Username: "xiaoxiao", Departname: "dev", Alias: "lunny", Created: time.Now()}
_, err = session.Insert(&user1)
if err != nil {
session.Rollback()
return
}
user2 := Userinfo{Username: "yyy"}
_, err = session.Where("id = ?", 2).Update(&user2)
if err != nil {
session.Rollback()
return
}
_, err = session.Exec("delete from userinfo where username = ?", user2.Username)
if err != nil {
session.Rollback()
return
}
// add Commit() after all actions
err = session.Commit()
if err != nil {
return
}
```
2013-05-03 07:26:51 +00:00
2013-05-12 06:19:38 +00:00
##Mapping Rules
2013-05-16 06:12:27 +00:00
2013-05-12 06:25:36 +00:00
<a name="mapping" id="mapping"></a>
2013-05-11 10:38:43 +00:00
1.Struct and struct's fields name should be Pascal style, and the table and column's name default is SQL style.
2013-05-08 13:42:22 +00:00
2013-05-11 10:38:43 +00:00
For example:
2013-05-03 07:26:51 +00:00
2013-05-11 10:38:43 +00:00
The struct's 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:26:51 +00:00
2013-05-11 10:38:43 +00:00
2.If You want change the mapping rules, you have two methods. One is to implement your own Map struct interface according IMapper, you can find the interface in mapper.go and set it to engine.Mapper
Another is use field tag, field tag support the below keywords which split with space:
2013-05-08 13:42:22 +00:00
<table>
<tr>
<td>name</td><td>column name</td>
</tr>
<tr>
<td>pk</td><td>the field is a primary key</td>
</tr>
<tr>
<td>int(11)/varchar(50)</td><td>column type</td>
</tr>
<tr>
<td>autoincr</td><td>auto incrment</td>
</tr>
<tr>
<td>[not ]null</td><td>if column can be null value</td>
</tr>
<tr>
<td>unique</td><td>unique</td>
</tr>
<tr>
<td>-</td><td>this field is not map as a table column</td>
</tr>
</table>
2013-05-11 10:38:43 +00:00
For Example
```Go
2013-05-11 13:27:36 +00:00
type Userinfo struct {
Uid int `xorm:"id pk not null autoincr"`
Username string
Departname string
Alias string `xorm:"-"`
Created time.Time
}
```
2013-05-11 10:38:43 +00:00
2013-05-16 06:16:39 +00:00
2013-05-11 07:45:24 +00:00
##Documents
2013-05-16 06:12:27 +00:00
2013-05-11 07:45:24 +00:00
Please visit [GoWalker](http://gowalker.org/github.com/lunny/xorm)
2013-05-16 06:12:27 +00:00
2013-05-16 06:16:39 +00:00
2013-05-06 08:01:17 +00:00
##FAQ
2013-05-16 06:12:27 +00:00
2013-05-06 08:01:17 +00:00
1.How the xorm tag use both with json?
2013-05-11 10:38:43 +00:00
Use space.
2013-05-06 08:01:17 +00:00
```Go
2013-05-11 13:27:36 +00:00
type User struct {
Name string `json:"name" xorm:"name"`
}
```
2013-05-03 07:26:51 +00:00
## LICENSE
BSD License
[http://creativecommons.org/licenses/BSD/](http://creativecommons.org/licenses/BSD/)