PageRenderTime 1565ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/example_test.go

https://bitbucket.org/foursource/dbr-internal
Go | 162 lines | 120 code | 26 blank | 16 comment | 3 complexity | dca5c953b3ffe7f528b2e347e72742dd MD5 | raw file
  1. package dbr
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func ExampleOpen() {
  7. // create a connection (e.g. "postgres", "mysql", or "sqlite3")
  8. conn, _ := Open("postgres", "...", nil)
  9. conn.SetMaxOpenConns(10)
  10. // create a session for each business unit of execution (e.g. a web request or goworkers job)
  11. sess := conn.NewSession(nil)
  12. // create a tx from sessions
  13. sess.Begin()
  14. }
  15. func ExampleSelect() {
  16. Select("title", "body").
  17. From("suggestions").
  18. OrderBy("id").
  19. Limit(10)
  20. }
  21. func ExampleSelectBySql() {
  22. SelectBySql("SELECT `title`, `body` FROM `suggestions` ORDER BY `id` ASC LIMIT 10")
  23. }
  24. func ExampleSelectStmt_Load() {
  25. // columns are mapped by tag then by field
  26. type Suggestion struct {
  27. ID int64 // id, will be autoloaded by last insert id
  28. Title NullString `db:"subject"` // subjects are called titles now
  29. Url string `db:"-"` // ignored
  30. secret string // ignored
  31. }
  32. // By default gocraft/dbr converts CamelCase property names to snake_case column_names.
  33. // You can override this with struct tags, just like with JSON tags.
  34. // This is especially helpful while migrating from legacy systems.
  35. var suggestions []Suggestion
  36. sess := mysqlSession
  37. sess.Select("*").From("suggestions").Load(&suggestions)
  38. }
  39. func ExampleSelectStmt_Where() {
  40. // database/sql uses prepared statements, which means each argument
  41. // in an IN clause needs its own question mark.
  42. // gocraft/dbr, on the other hand, handles interpolation itself
  43. // so that you can easily use a single question mark paired with a
  44. // dynamically sized slice.
  45. sess := mysqlSession
  46. ids := []int64{1, 2, 3, 4, 5}
  47. sess.Select("*").From("suggestions").Where("id IN ?", ids)
  48. }
  49. func ExampleSelectStmt_Join() {
  50. sess := mysqlSession
  51. sess.Select("*").From("suggestions").
  52. Join("subdomains", "suggestions.subdomain_id = subdomains.id")
  53. sess.Select("*").From("suggestions").
  54. LeftJoin("subdomains", "suggestions.subdomain_id = subdomains.id")
  55. // join multiple tables
  56. sess.Select("*").From("suggestions").
  57. Join("subdomains", "suggestions.subdomain_id = subdomains.id").
  58. Join("accounts", "subdomains.accounts_id = accounts.id")
  59. }
  60. func ExampleSelectStmt_As() {
  61. sess := mysqlSession
  62. sess.Select("count(id)").From(
  63. Select("*").From("suggestions").As("count"),
  64. )
  65. }
  66. func ExampleInsertStmt_Pair() {
  67. sess := mysqlSession
  68. sess.InsertInto("suggestions").
  69. Pair("title", "Gopher").
  70. Pair("body", "I love go.")
  71. }
  72. func ExampleInsertStmt_Record() {
  73. type Suggestion struct {
  74. ID int64
  75. Title NullString
  76. CreatedAt time.Time
  77. }
  78. sugg := &Suggestion{
  79. Title: NewNullString("Gopher"),
  80. CreatedAt: time.Now(),
  81. }
  82. sess := mysqlSession
  83. sess.InsertInto("suggestions").
  84. Columns("id", "title").
  85. Record(&sugg).
  86. Exec()
  87. // id is set automatically
  88. fmt.Println(sugg.ID)
  89. }
  90. func ExampleUpdateStmt() {
  91. sess := mysqlSession
  92. sess.Update("suggestions").
  93. Set("title", "Gopher").
  94. Set("body", "I love go.").
  95. Where("id = ?", 1)
  96. }
  97. func ExampleDeleteStmt() {
  98. sess := mysqlSession
  99. sess.DeleteFrom("suggestions").
  100. Where("id = ?", 1)
  101. }
  102. func ExampleTx() {
  103. sess := mysqlSession
  104. tx, err := sess.Begin()
  105. if err != nil {
  106. return
  107. }
  108. defer tx.RollbackUnlessCommitted()
  109. // do stuff...
  110. tx.Commit()
  111. }
  112. func ExampleAnd() {
  113. And(
  114. Or(
  115. Gt("created_at", "2015-09-10"),
  116. Lte("created_at", "2015-09-11"),
  117. ),
  118. Eq("title", "hello world"),
  119. )
  120. }
  121. func ExampleI() {
  122. // I, identifier, can be used to quote.
  123. I("suggestions.id").As("id") // `suggestions`.`id`
  124. }
  125. func ExampleUnion() {
  126. Union(
  127. Select("*"),
  128. Select("*"),
  129. ).As("subquery")
  130. }
  131. func ExampleUnionAll() {
  132. UnionAll(
  133. Select("*"),
  134. Select("*"),
  135. ).As("subquery")
  136. }