diff --git a/README.md b/README.md index 895ffa45..6b5b90c6 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,35 @@ # xorm ----------- + [中文](./README_CN.md) -xorm is an ORM for Go. It lets you map Go structs to tables in a database. +xorm is an ORM for Go. It makes dabatabse operating simple. -Right now, it supports Mysql and SQLite. The goal however is to add support for PostgreSQL/DB2/MS ADODB/ODBC/Oracle in the future. +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: -All in all, it's not entirely ready for product use yet, but it's getting there. +* Mysql: [github.com/Go-SQL-Driver/MySQL](https://github.com/Go-SQL-Driver/MySQL) -Drivers for Go's sql package which support database/sql includes: - -Mysql: [github.com/Go-SQL-Driver/MySQL](https://github.com/Go-SQL-Driver/MySQL) - -SQLite: [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3) +* SQLite: [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3) + +## Changelog + +* **v0.1.1** : Add Id, In functions and improved README +* **v0.1.0** : Inital release. + ## Features -1.Struct<->Table Mapping Support Names and Tags -2.Transaction Support +* Struct<->Table Mapping Supports, both name mapping and filed tags mapping -3.SQL Execute Support +* Database Transaction Support -4.Simple usage +* Both ORM and SQL Operation Support -5.Support Id, Where or Sturct as query conditions +* Simply usage + +* Support Id, In, Where, Limit, Join, Having functions and sturct as query conditions ## Installing xorm @@ -30,20 +37,22 @@ SQLite: [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3) ## Quick Start -1.Create a database engine just like sql.Open (for example: mysql) - - import (_ "github.com/Go-SQL-Driver/MySQL" +1.Create a database engine just like sql.Open, commonly you just need create once. +```Go +import (_ "github.com/Go-SQL-Driver/MySQL" "github.com/lunny/xorm") - engine := xorm.Create("mysql", "root:123@/test?charset=utf8") +engine := xorm.Create("mysql", "root:123@/test?charset=utf8") +``` or +```Go import (_ "github.com/mattn/go-sqlite3" "github.com/lunny/xorm") engine = xorm.Create("sqlite3", "./test.db") +``` - -2.Define your struct +2.Define a struct type User struct { @@ -53,22 +62,19 @@ or } -for Simple Task, just use engine's functions: - -before beginning, you should create a database in mysql and then we will create the tables. +3.When you set up your program, you can use CreateTables to create database tables. - err := engine.CreateTables(&User{}) - - -then, insert an struct to table - + err := engine.CreateTables(&User{}) + // or err := engine.Map(&User{}, &Article{}) + // err = engine.CreateAll() +4.then, insert an struct to table + id, err := engine.Insert(&User{Name:"lunny"}) -or you want to update this struct - +or if you want to update records user := User{Name:"xlw"} rows, err := engine.Update(&user, &User{Id:1}) @@ -76,8 +82,7 @@ or you want to update this struct // or rows, err := engine.Id(1).Update(&user) -3.Fetch a single object by user - +5.Fetch a single object by user var user = User{Id:27} err := engine.Get(&user) @@ -86,36 +91,50 @@ or you want to update this struct var user = User{Name:"xlw"} err := engine.Get(&user) -4.Fetch multipe objects, use Find: +6.Fetch multipe objects, use Find: - var allusers []Userinfo - err := engine.Where("id > ?", "3").Limit(10,20).Find(&allusers) //Get id>3 limit 10 offset 20 - - var tenusers []Userinfo - err := engine.Limit(10).Find(&tenusers, &Userinfo{Name:"xlw"}) //Get All Name="xlw" limit 10 if omit offset the default is 0 - var everyone []Userinfo err := engine.Find(&everyone) -5.Delete and Count: +6.1 also you can use Where, Limit + + var allusers []Userinfo + err := engine.Where("id > ?", "3").Limit(10,20).Find(&allusers) //Get id>3 limit 10 offset 20 + +6.2 or you can use a struct query + + var tenusers []Userinfo + err := engine.Limit(10).Find(&tenusers, &Userinfo{Name:"xlw"}) //Get All Name="xlw" limit 10 offset 0 + +6.3 or In function + + var tenusers []Userinfo + err := engine.In("id", 1, 3, 5).Find(&tenusers) //Get All id in (1, 3, 5) + +7.Delete err := engine.Delete(&User{Id:1}) // or err := engine.Id(1).Delete(&User{}) - + +8.Count + total, err := engine.Count(&User{Name:"xlw"}) -##Origin Use -Of course, the basic usage is also provided. +##Execute SQL +Of course, SQL execution is also provided. + +1.if select then use Query sql := "select * from userinfo" results, err := engine.Query(sql) - + +2.if insert, update or delete then use Exec + sql = "update userinfo set username=? where id=?" res, err := engine.Exec(sql, "xiaolun", 1) ##Deep Use -for deep use, you should create a session, this func will create a connection to db - +for deep usage, you should create a session, this func will create a database connection immediatelly session, err := engine.MakeSession() defer session.Close() @@ -206,15 +225,15 @@ for deep use, you should create a session, this func will create a connection to } ##Mapping Rules -1.Struct and struct's fields name should be Pascal style, and the table and column's name default is us +1.Struct and struct's fields name should be Pascal style, and the table and column's name default is SQL style. -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' +For example: -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 +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' -another is use field tag, field tag support the below keywords: +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: