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 80 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/jimmykuu/xorm/examples
  38. github.com/jimmykuu/xorm/xorm
  39. github.com/jszwedko/gsp-go-orms/xorm
  40. github.com/juju2013/gowalker/models
  41. github.com/kjfcpua/easygo
  42. github.com/linkeddata/conexus
  43. github.com/linkeddata/webizen
  44. github.com/lunny/mzr/model
  45. github.com/lunny/mzr/testing
  46. github.com/lunny/xorm/examples
  47. github.com/lunny/xorm/xorm
  48. github.com/lunny/xweb/examples/simple
  49. github.com/lzmuhioin/gowalker/models
  50. github.com/lzy7199/xorm/examples
  51. github.com/lzy7199/xorm/xorm
  52. github.com/matrixik/gogs/models
  53. github.com/matrixik/gogs/routers
  54. github.com/mattbaird/gogs/models
  55. github.com/matyhtf/easygo
  56. github.com/mnhkahn/cInphone-server/controllers
  57. github.com/mnhkahn/short_url/models
  58. github.com/mr-kelly/toropress/models
  59. github.com/mr-kelly/toropress/old-version/toropress-qbs-version-1st/models
  60. github.com/mr-kelly/toropress/old-version/toropress-xorm-version-2st/models
  61. github.com/nashtsai/xorm/examples
  62. github.com/nashtsai/xorm/xorm
  63. github.com/omni360/gogs/models
  64. github.com/omni360/gogs/routers
  65. github.com/pengux/gogs/models
  66. github.com/pengux/gogs/routers
  67. github.com/roydong/xorm/examples
  68. github.com/roydong/xorm/xorm
  69. github.com/safawo/sdc/model
  70. github.com/scottkiss/xorm/examples
  71. github.com/scottkiss/xorm/xorm
  72. github.com/semicarry/gowalker/models
  73. github.com/soulinfo/veryhour/models
  74. github.com/soulinfo/veryhour/testing
  75. github.com/umisama/xorm/examples
  76. github.com/umisama/xorm/xorm
  77. github.com/zackb/code/go/src/app
  78. github.com/zackb/code/go/src/grab/db
  79. gopkg.in/lunny/xorm.v0/examples
  80. 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