doc.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 8 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. var sliceOfStructs []Struct
  39. err := engine.Find(&sliceOfStructs)
  40. // SELECT * FROM user
  41. var mapOfStructs = make(map[int64]Struct)
  42. err := engine.Find(&mapOfStructs)
  43. // SELECT * FROM user
  44. var int64s []int64
  45. err := engine.Table("user").Cols("id").Find(&int64s)
  46. // SELECT id FROM user
  47. 4. Query multiple records and record by record handle, there two methods, one is Iterate,
  48. another is Rows
  49. err := engine.Iterate(...)
  50. // SELECT * FROM user
  51. rows, err := engine.Rows(...)
  52. // SELECT * FROM user
  53. defer rows.Close()
  54. bean := new(Struct)
  55. for rows.Next() {
  56. err = rows.Scan(bean)
  57. }
  58. 5. Update one or more records
  59. affected, err := engine.Id(...).Update(&user)
  60. // UPDATE user SET ...
  61. 6. Delete one or more records, Delete MUST has condition
  62. affected, err := engine.Where(...).Delete(&user)
  63. // DELETE FROM user Where ...
  64. 7. Count records
  65. counts, err := engine.Count(&user)
  66. // SELECT count(*) AS total FROM user
  67. 8. Sum records
  68. sumFloat64, err := engine.Sum(&user, "id")
  69. // SELECT sum(id) from user
  70. sumFloat64s, err := engine.Sums(&user, "id1", "id2")
  71. // SELECT sum(id1), sum(id2) from user
  72. sumInt64s, err := engine.SumsInt(&user, "id1", "id2")
  73. // SELECT sum(id1), sum(id2) from user
  74. Conditions
  75. The above 8 methods could use with condition methods chainable.
  76. Attention: the above 8 methods should be the last chainable method.
  77. 1. ID, In
  78. engine.ID(1).Get(&user) // for single primary key
  79. // SELECT * FROM user WHERE id = 1
  80. engine.ID(core.PK{1, 2}).Get(&user) // for composite primary keys
  81. // SELECT * FROM user WHERE id1 = 1 AND id2 = 2
  82. engine.In("id", 1, 2, 3).Find(&users)
  83. // SELECT * FROM user WHERE id IN (1, 2, 3)
  84. engine.In("id", []int{1, 2, 3}).Find(&users)
  85. // SELECT * FROM user WHERE id IN (1, 2, 3)
  86. 2. Where, And, Or
  87. engine.Where().And().Or().Find()
  88. // SELECT * FROM user WHERE (.. AND ..) OR ...
  89. 3. OrderBy, Asc, Desc
  90. engine.Asc().Desc().Find()
  91. // SELECT * FROM user ORDER BY .. ASC, .. DESC
  92. engine.OrderBy().Find()
  93. // SELECT * FROM user ORDER BY ..
  94. 4. Limit, Top
  95. engine.Limit().Find()
  96. // SELECT * FROM user LIMIT .. OFFSET ..
  97. engine.Top(5).Find()
  98. // SELECT TOP 5 * FROM user // for mssql
  99. // SELECT * FROM user LIMIT .. OFFSET 0 //for other databases
  100. 5. SQL, let you custom SQL
  101. var users []User
  102. engine.SQL("select * from user").Find(&users)
  103. 6. Cols, Omit, Distinct
  104. var users []*User
  105. engine.Cols("col1, col2").Find(&users)
  106. // SELECT col1, col2 FROM user
  107. engine.Cols("col1", "col2").Where().Update(user)
  108. // UPDATE user set col1 = ?, col2 = ? Where ...
  109. engine.Omit("col1").Find(&users)
  110. // SELECT col2, col3 FROM user
  111. engine.Omit("col1").Insert(&user)
  112. // INSERT INTO table (non-col1) VALUES ()
  113. engine.Distinct("col1").Find(&users)
  114. // SELECT DISTINCT col1 FROM user
  115. 7. Join, GroupBy, Having
  116. engine.GroupBy("name").Having("name='xlw'").Find(&users)
  117. //SELECT * FROM user GROUP BY name HAVING name='xlw'
  118. engine.Join("LEFT", "userdetail", "user.id=userdetail.id").Find(&users)
  119. //SELECT * FROM user LEFT JOIN userdetail ON user.id=userdetail.id
  120. More usage, please visit http://xorm.io/docs
  121. */
  122. package xorm