top

Package xorm is a simple and powerful ORM for Go.

Installation

Make sure you have installed Go 1.1+ and then:

go get github.com/lunny/xorm

Create Engine

Firstly, we should new an engine for a database

engine, err := xorm.NewEngine(driverName, dataSourceName)

Method NewEngine's parameters is the same as sql.Open. It depends drivers' implementation. Generally, one engine is enough. You can set it as package variable.

Raw Methods

Xorm also support raw sql execution:

1. query sql, the returned results is []map[string][]byte

results, err := engine.Query("select * from user")

2. exec sql, the returned results

affected, err := engine.Exec("update user set .... where ...")

ORM Methods

There are 7 major ORM methods and many helpful methods to use to operate database.

1. Insert one or multipe records to database

affected, err := engine.Insert(&struct)
// INSERT INTO struct () values ()
affected, err := engine.Insert(&struct1, &struct2)
// INSERT INTO struct1 () values ()
// INSERT INTO struct2 () values ()
affected, err := engine.Insert(&sliceOfStruct)
// INSERT INTO struct () values (),(),()
affected, err := engine.Insert(&struct1, &sliceOfStruct2)
// INSERT INTO struct1 () values ()
// INSERT INTO struct2 () values (),(),()

2. Query one record from database

has, err := engine.Get(&user)
// SELECT * FROM user LIMIT 1

3. Query multiple records from database

err := engine.Find(...)
// SELECT * FROM user

4. Query multiple records and record by record handle, there two methods, one is Iterate, another is Raws

raws, err := engine.Raws(...)
// SELECT * FROM user
for raws.Next() {
    raws.Scan(bean)
}

err := engine.Iterate(...)
// SELECT * FROM user

5. Update one or more records

affected, err := engine.Update(&user)
// UPDATE user SET

6. Delete one or more records

affected, err := engine.Delete(&user)
// DELETE FROM user Where ...

7. Count records

counts, err := engine.Count(&user)
// SELECT count(*) AS total FROM user

Conditions

The above 7 methods could use with condition methods.

1. Id, In

engine.Id(1).Get(&user)
// SELECT * FROM user WHERE id = 1
engine.In("id", 1, 2, 3).Find(&users)
// SELECT * FROM user WHERE id IN (1, 2, 3)

2. Where, And, Or

engine.Where().And().Or().Find()
// SELECT * FROM user WHERE (.. AND ..) OR ...

3. OrderBy, Asc, Desc

engine.Asc().Desc().Find()
// SELECT * FROM user ORDER BY .. ASC, .. DESC
engine.OrderBy().Find()
// SELECT * FROM user ORDER BY ..

4. Limit, Top

engine.Limit().Find()
// SELECT * FROM user LIMIT .. OFFSET ..
engine.Top().Find()
// SELECT * FROM user LIMIT ..

5. Sql

engine.Sql("select * from user").Find()

6. Cols, Omit, Distinct

engine.Cols("col1, col2").Find()
// SELECT col1, col2 FROM user
engine.Omit("col1").Find()
// SELECT col2, col3 FROM user
engine.Distinct("col1").Find()
// SELECT DISTINCT col1 FROM user

7. Join, GroupBy, Having

engine.GroupBy("name").Having("name='xlw'").Find()
//SELECT * FROM user GROUP BY name HAVING name='xlw'
engine.Join("LEFT", "userdetail", "user.id=userdetail.id").Find()
//SELECT * FROM user LEFT JOIN userdetail ON user.id=userdetail.id

More usage, please visit https://github.com/lunny/xorm/blob/master/docs/QuickStartEn.md

Imported by 85 package(s)

  1. git.oschina.net/Unknown/gowalker.git/models
  2. github.com/CyberLight/gowalker/models
  3. github.com/GoSteelProgrammers/talk-review-orms/xorm
  4. github.com/MartinGarton/gogs/models
  5. github.com/MartinGarton/gogs/routers
  6. github.com/Neeke/xorm/xorm
  7. github.com/Raysmond/RaysGo/models
  8. github.com/RoyDong/xorm/examples
  9. github.com/RoyDong/xorm/xorm
  10. github.com/ZCLanner/WebZhihuNews
  11. github.com/axcoto/gowalker/models
  12. github.com/badoet/orm-benchmark/benchs
  13. github.com/beego/orm-benchmark/benchs
  14. github.com/changboter/gogs/models
  15. github.com/chaobj001/mzr/model
  16. github.com/chaobj001/mzr/testing
  17. github.com/ckrissun/xorm/examples
  18. github.com/easykoo/gogs/models
  19. github.com/easykoo/xorm/examples
  20. github.com/easykoo/xorm/xorm
  21. github.com/eruca/marti/models
  22. github.com/godaily/exercise
  23. github.com/godaily/exercise/actions
  24. github.com/gogenerate/rest/models
  25. github.com/goghcrow/easygo
  26. github.com/govc/godaily/actions
  27. github.com/ifq/gowalker/models
  28. github.com/imjoez/gobuild/database
  29. github.com/insionng/sdc/model
  30. github.com/irwinai/blog
  31. github.com/irwinai/blog/actions
  32. github.com/irwinai/godaily
  33. github.com/irwinai/godaily/actions
  34. github.com/irwinai/xweb/examples/simple
  35. github.com/jartek/goapi/models
  36. github.com/jartek/goapi/routes
  37. github.com/jijeshmohan/go-url-shortener/app/controllers
  38. github.com/jijeshmohan/go-url-shortener/app/db
  39. github.com/jijeshmohan/go-url-shortener/app/models
  40. github.com/jimmykuu/xorm/examples
  41. github.com/jimmykuu/xorm/xorm
  42. github.com/jszwedko/gsp-go-orms/xorm
  43. github.com/juju2013/gowalker/models
  44. github.com/kjfcpua/easygo
  45. github.com/linkeddata/conexus
  46. github.com/linkeddata/webizen
  47. github.com/lunny/mzr/client/mgr
  48. github.com/lunny/mzr/model
  49. github.com/lunny/mzr/testing
  50. github.com/lunny/xorm/examples
  51. github.com/lunny/xorm/xorm
  52. github.com/lunny/xweb/examples/simple
  53. github.com/lzmuhioin/gowalker/models
  54. github.com/lzy7199/xorm/examples
  55. github.com/lzy7199/xorm/xorm
  56. github.com/matrixik/gogs/models
  57. github.com/matrixik/gogs/routers
  58. github.com/mattbaird/gogs/models
  59. github.com/matyhtf/easygo
  60. github.com/mnhkahn/cInphone-server/controllers
  61. github.com/mnhkahn/short_url/models
  62. github.com/mr-kelly/toropress/models
  63. github.com/mr-kelly/toropress/old-version/toropress-qbs-version-1st/models
  64. github.com/mr-kelly/toropress/old-version/toropress-xorm-version-2st/models
  65. github.com/nashtsai/xorm/examples
  66. github.com/nashtsai/xorm/xorm
  67. github.com/omni360/gogs/models
  68. github.com/omni360/gogs/routers
  69. github.com/pengux/gogs/models
  70. github.com/pengux/gogs/routers
  71. github.com/roydong/xorm/examples
  72. github.com/roydong/xorm/xorm
  73. github.com/safawo/sdc/model
  74. github.com/scottkiss/xorm/examples
  75. github.com/scottkiss/xorm/xorm
  76. github.com/semicarry/gowalker/models
  77. github.com/soulinfo/veryhour/models
  78. github.com/soulinfo/veryhour/testing
  79. github.com/soulinfo/xweb/examples/simple
  80. github.com/umisama/xorm/examples
  81. github.com/umisama/xorm/xorm
  82. github.com/zackb/code/go/src/app
  83. github.com/zackb/code/go/src/grab/db
  84. gopkg.in/lunny/xorm.v0/examples
  85. gopkg.in/lunny/xorm.v0/xorm

Imported only in test by 8 package(s)

  1. github.com/Julianzz/gogs/models
  2. github.com/acsellers/gogs/models
  3. github.com/clee/gogs/models
  4. github.com/jsutlovic/gogs/models
  5. github.com/juju2013/gogs/models
  6. github.com/julianzz/gogs/models
  7. github.com/kristofer/gogs/models
  8. github.com/oren/gogs/models

Test imports 4 package(s)

  1. github.com/mattn/go-sqlite3
  2. github.com/lib/pq
  3. github.com/go-sql-driver/mysql
  4. github.com/ziutek/mymysql/godrv