doc.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2013 - 2016 The XORM Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Package xorm is a simple and powerful ORM for Go.
  6. Installation
  7. Make sure you have installed Go 1.1+ and then:
  8. go get github.com/go-xorm/xorm
  9. Create Engine
  10. Firstly, we should new an engine for a database
  11. engine, err := xorm.NewEngine(driverName, dataSourceName)
  12. Method NewEngine's parameters is the same as sql.Open. It depends
  13. drivers' implementation.
  14. Generally, one engine for an application is enough. You can set it as package variable.
  15. Raw Methods
  16. Xorm also support raw sql execution:
  17. 1. query a SQL string, the returned results is []map[string][]byte
  18. results, err := engine.Query("select * from user")
  19. 2. execute a SQL string, the returned results
  20. affected, err := engine.Exec("update user set .... where ...")
  21. ORM Methods
  22. There are 7 major ORM methods and many helpful methods to use to operate database.
  23. 1. Insert one or multiple records to database
  24. affected, err := engine.Insert(&struct)
  25. // INSERT INTO struct () values ()
  26. affected, err := engine.Insert(&struct1, &struct2)
  27. // INSERT INTO struct1 () values ()
  28. // INSERT INTO struct2 () values ()
  29. affected, err := engine.Insert(&sliceOfStruct)
  30. // INSERT INTO struct () values (),(),()
  31. affected, err := engine.Insert(&struct1, &sliceOfStruct2)
  32. // INSERT INTO struct1 () values ()
  33. // INSERT INTO struct2 () values (),(),()
  34. 2. Query one record from database
  35. has, err := engine.Get(&user)
  36. // SELECT * FROM user LIMIT 1
  37. 3. Query multiple records from database
  38. sliceOfStructs := new(Struct)
  39. err := engine.Find(sliceOfStructs)
  40. // SELECT * FROM user
  41. 4. Query multiple records and record by record handle, there two methods, one is Iterate,
  42. another is Rows
  43. err := engine.Iterate(...)
  44. // SELECT * FROM user
  45. rows, err := engine.Rows(...)
  46. // SELECT * FROM user
  47. defer rows.Close()
  48. bean := new(Struct)
  49. for rows.Next() {
  50. err = rows.Scan(bean)
  51. }
  52. 5. Update one or more records
  53. affected, err := engine.Id(...).Update(&user)
  54. // UPDATE user SET ...
  55. 6. Delete one or more records, Delete MUST has condition
  56. affected, err := engine.Where(...).Delete(&user)
  57. // DELETE FROM user Where ...
  58. 7. Count records
  59. counts, err := engine.Count(&user)
  60. // SELECT count(*) AS total FROM user
  61. Conditions
  62. The above 7 methods could use with condition methods chainable.
  63. Attention: the above 7 methods should be the last chainable method.
  64. 1. Id, In
  65. engine.Id(1).Get(&user) // for single primary key
  66. // SELECT * FROM user WHERE id = 1
  67. engine.Id(core.PK{1, 2}).Get(&user) // for composite primary keys
  68. // SELECT * FROM user WHERE id1 = 1 AND id2 = 2
  69. engine.In("id", 1, 2, 3).Find(&users)
  70. // SELECT * FROM user WHERE id IN (1, 2, 3)
  71. engine.In("id", []int{1, 2, 3})
  72. // SELECT * FROM user WHERE id IN (1, 2, 3)
  73. 2. Where, And, Or
  74. engine.Where().And().Or().Find()
  75. // SELECT * FROM user WHERE (.. AND ..) OR ...
  76. 3. OrderBy, Asc, Desc
  77. engine.Asc().Desc().Find()
  78. // SELECT * FROM user ORDER BY .. ASC, .. DESC
  79. engine.OrderBy().Find()
  80. // SELECT * FROM user ORDER BY ..
  81. 4. Limit, Top
  82. engine.Limit().Find()
  83. // SELECT * FROM user LIMIT .. OFFSET ..
  84. engine.Top(5).Find()
  85. // SELECT TOP 5 * FROM user // for mssql
  86. // SELECT * FROM user LIMIT .. OFFSET 0 //for other databases
  87. 5. Sql, let you custom SQL
  88. var users []User
  89. engine.Sql("select * from user").Find(&users)
  90. 6. Cols, Omit, Distinct
  91. var users []*User
  92. engine.Cols("col1, col2").Find(&users)
  93. // SELECT col1, col2 FROM user
  94. engine.Cols("col1", "col2").Where().Update(user)
  95. // UPDATE user set col1 = ?, col2 = ? Where ...
  96. engine.Omit("col1").Find(&users)
  97. // SELECT col2, col3 FROM user
  98. engine.Omit("col1").Insert(&user)
  99. // INSERT INTO table (non-col1) VALUES ()
  100. engine.Distinct("col1").Find(&users)
  101. // SELECT DISTINCT col1 FROM user
  102. 7. Join, GroupBy, Having
  103. engine.GroupBy("name").Having("name='xlw'").Find(&users)
  104. //SELECT * FROM user GROUP BY name HAVING name='xlw'
  105. engine.Join("LEFT", "userdetail", "user.id=userdetail.id").Find(&users)
  106. //SELECT * FROM user LEFT JOIN userdetail ON user.id=userdetail.id
  107. More usage, please visit http://xorm.io/docs
  108. */
  109. package xorm