/README.md

http://github.com/astaxie/beedb · Markdown · 219 lines · 161 code · 58 blank · 0 comment · 0 complexity · 88274debfa03f2540550d859d6d90121 MD5 · raw file

  1. Beedb
  2. =====
  3. :exclamation: **IMPORTANT:** Beedb is being deprecated in favor of [Beego.orm](https://github.com/astaxie/beego/tree/master/orm) :exclamation:
  4. Beedb is an ORM for Go. It lets you map Go structs to tables in a database. It's intended to be very lightweight, doing very little beyond what you really want. For example, when fetching data, instead of re-inventing a query syntax, we just delegate your query to the underlying database, so you can write the "where" clause of your SQL statements directly. This allows you to have more flexibility while giving you a convenience layer. But beedb also has some smart defaults, for those times when complex queries aren't necessary.
  5. Right now, it interfaces with Mysql/SQLite/PostgreSQL/DB2/MS ADODB/ODBC/Oracle. The goal however is to add support for other databases in the future, including maybe MongoDb or NoSQL?
  6. Relationship-support is not implemented, for this we will recommend you to use [Beego.orm](https://github.com/astaxie/beego/tree/master/orm).
  7. All in all, it's not entirely ready for advanced use yet, but it's getting there.
  8. Drivers for Go's sql package which support database/sql includes:
  9. Mysql:[github.com/ziutek/mymysql/godrv](https://github.com/ziutek/mymysql/godrv)`[*]`
  10. Mysql:[github.com/Go-SQL-Driver/MySQL](https://github.com/Go-SQL-Driver/MySQL)`[*]`
  11. PostgreSQL:[github.com/bmizerany/pq](https://github.com/bmizerany/pq)`[*]`
  12. SQLite:[github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)`[*]`
  13. DB2: [bitbucket.org/phiggins/go-db2-cli](https://bitbucket.org/phiggins/go-db2-cli)
  14. MS ADODB: [github.com/mattn/go-adodb](https://github.com/mattn/go-adodb)`[*]`
  15. ODBC: [bitbucket.org/miquella/mgodbc](https://bitbucket.org/miquella/mgodbc)`[*]`
  16. Oracle: [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8)
  17. Drivers marked with a `[*]` are tested with Beedb
  18. ### API Interface
  19. [wiki/API-Interface](https://github.com/astaxie/beedb/wiki/API-Interface)
  20. ### Installing Beedb
  21. go get github.com/astaxie/beedb
  22. ### How do we use it?
  23. Open a database link(may be will support ConnectionPool in the future)
  24. ```go
  25. db, err := sql.Open("mymysql", "test/xiemengjun/123456")
  26. if err != nil {
  27. panic(err)
  28. }
  29. orm := beedb.New(db)
  30. ```
  31. with PostgreSQL,
  32. ```go
  33. orm := beedb.New(db, "pg")
  34. ```
  35. Open Debug log, turn on the debug
  36. ```go
  37. beedb.OnDebug=true
  38. ```
  39. Model a struct after a table in the db
  40. ```go
  41. type Userinfo struct {
  42. Uid int `beedb:"PK" sql:"UID" tname:"USER_INFO"` //if the table's PrimaryKey is not "Id", use this tag
  43. Username string `sql:"USERNAME"`
  44. Departname string `sql:"DEPARTNAME"`
  45. Created time.Time `sql:"CREATED"`
  46. }
  47. ```
  48. ###***Caution***
  49. The structs Name 'UserInfo' will turn into the table name 'USER_INFO', as defined by the **tname** tag.
  50. If the key 'UserName' will turn into the select colum 'USERNAME' because of the **sql** tag.
  51. Create an object and save it
  52. ```go
  53. var saveone Userinfo
  54. saveone.Username = "Test Add User"
  55. saveone.Departname = "Test Add Departname"
  56. saveone.Created = time.Now()
  57. orm.Save(&saveone)
  58. ```
  59. Saving new and existing objects
  60. ```go
  61. saveone.Username = "Update Username"
  62. saveone.Departname = "Update Departname"
  63. saveone.Created = time.Now()
  64. orm.Save(&saveone) //now saveone has the primarykey value it will update
  65. ```
  66. Fetch a single object
  67. ```go
  68. var user Userinfo
  69. orm.Where("uid=?", 27).Find(&user)
  70. var user2 Userinfo
  71. orm.Where(3).Find(&user2) // this is shorthand for the version above
  72. var user3 Userinfo
  73. orm.Where("name = ?", "john").Find(&user3) // more complex query
  74. var user4 Userinfo
  75. orm.Where("name = ? and age < ?", "john", 88).Find(&user4) // even more complex
  76. ```
  77. Fetch multiple objects
  78. ```go
  79. var allusers []Userinfo
  80. err := orm.Where("id > ?", "3").Limit(10,20).FindAll(&allusers) //Get id>3 limit 10 offset 20
  81. var tenusers []Userinfo
  82. err := orm.Where("id > ?", "3").Limit(10).FindAll(&tenusers) //Get id>3 limit 10 if omit offset the default is 0
  83. var everyone []Userinfo
  84. err := orm.FindAll(&everyone)
  85. ```
  86. Find result as Map
  87. ```go
  88. //Original SQL Backinfo resultsSlice []map[string][]byte
  89. //default PrimaryKey id
  90. a, _ := orm.SetTable("userinfo").SetPK("uid").Where(2).Select("uid,username").FindMap()
  91. ```
  92. Update with Map
  93. ```go
  94. t := make(map[string]interface{})
  95. var j interface{}
  96. j = "astaxie"
  97. t["username"] = j
  98. //update one
  99. orm.SetTable("userinfo").SetPK("uid").Where(2).Update(t)
  100. ```
  101. Update batch with Map
  102. ```go
  103. orm.SetTable("userinfo").Where("uid>?", 3).Update(t)
  104. ```
  105. Insert data with Map
  106. ```go
  107. add := make(map[string]interface{})
  108. j = "astaxie"
  109. add["username"] = j
  110. j = "cloud develop"
  111. add["departname"] = j
  112. j = "2012-12-02"
  113. add["created"] = j
  114. orm.SetTable("userinfo").Insert(add)
  115. ```
  116. Insert batch with map
  117. ```go
  118. addslice := make([]map[string]interface{})
  119. add:=make(map[string]interface{})
  120. add2:=make(map[string]interface{})
  121. j = "astaxie"
  122. add["username"] = j
  123. j = "cloud develop"
  124. add["departname"] = j
  125. j = "2012-12-02"
  126. add["created"] = j
  127. j = "astaxie2"
  128. add2["username"] = j
  129. j = "cloud develop2"
  130. add2["departname"] = j
  131. j = "2012-12-02"
  132. add2["created"] = j
  133. addslice =append(addslice, add, add2)
  134. orm.SetTable("userinfo").Insert(addslice)
  135. ```
  136. Join Table
  137. ```go
  138. a, _ := orm.SetTable("userinfo").Join("LEFT", "userdeatail", "userinfo.uid=userdeatail.uid").Where("userinfo.uid=?", 1).Select("userinfo.uid,userinfo.username,userdeatail.profile").FindMap()
  139. ```
  140. Group By And Having
  141. ```go
  142. a, _ := orm.SetTable("userinfo").GroupBy("username").Having("username='astaxie'").FindMap()
  143. ```
  144. Nesting Models (inline)
  145. ```go
  146. type SQLModel struct {
  147. Id int `beedb:"PK" sql:"id"`
  148. Created time.Time `sql:"created"`
  149. Modified time.Time `sql:"modified"`
  150. }
  151. type User struct {
  152. SQLModel `sql:",inline"`
  153. Name string `sql:"name" tname:"fn_group"`
  154. Auth int `sql:"auth"`
  155. }
  156. // the SQL table has the columns: id, name, auth, created, modified
  157. // They are marshalled and unmarshalled automatically because of the inline keyword
  158. ```
  159. ## LICENSE
  160. BSD License
  161. [http://creativecommons.org/licenses/BSD/](http://creativecommons.org/licenses/BSD/)