Templates for the "gnorm" database management engine.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
5.8 KiB

4 years ago
  1. // Code generated by gnorm, DO NOT EDIT!
  2. package {{.Params.RootPkg}}
  3. import (
  4. "database/sql"
  5. "database/sql/driver"
  6. "encoding/json"
  7. "errors"
  8. "strconv"
  9. "strings"
  10. )
  11. // DB is the common interface for database operations.
  12. //
  13. // This should work with database/sql.DB and database/sql.Tx.
  14. type DB interface {
  15. Exec(string, ...interface{}) (sql.Result, error)
  16. Query(string, ...interface{}) (*sql.Rows, error)
  17. QueryRow(string, ...interface{}) *sql.Row
  18. }
  19. // Bytes is an wrapper for []byte for storing bytes in postgres
  20. type Bytes []byte
  21. // Value implements the driver Valuer interface.
  22. func (b Bytes) Value() (driver.Value, error) {
  23. return []byte(b), nil
  24. }
  25. // Scan implements the Scanner interface.
  26. func (b *Bytes) Scan(value interface{}) error {
  27. bytes, ok := value.([]byte)
  28. if !ok {
  29. return errors.New("Type assertion .([]byte) failed")
  30. }
  31. *b = Bytes(bytes)
  32. return nil
  33. }
  34. // NullBytes is an wrapper for []byte for storing bytes in postgres
  35. type NullBytes struct {
  36. Bytes []byte
  37. Valid bool
  38. }
  39. // Value implements the driver Valuer interface.
  40. func (nb NullBytes) Value() (driver.Value, error) {
  41. if !nb.Valid {
  42. return nil, nil
  43. }
  44. return nb.Bytes, nil
  45. }
  46. // Scan implements the Scanner interface.
  47. func (nb *NullBytes) Scan(value interface{}) error {
  48. if value == nil {
  49. nb.Bytes, nb.Valid = []byte(""), false
  50. return nil
  51. }
  52. nb.Valid = true
  53. var ok bool
  54. nb.Bytes, ok = value.([]byte)
  55. if !ok {
  56. return errors.New("Type assertion .([]byte) failed")
  57. }
  58. return nil
  59. }
  60. // Jsonb is a wrapper for map[string]interface{} for storing json into postgres
  61. type Jsonb map[string]interface{}
  62. // Value marshals the json into the database
  63. func (j Jsonb) Value() (driver.Value, error) {
  64. return json.Marshal(j)
  65. }
  66. // Scan Unmarshalls the bytes[] back into a Jsonb object
  67. func (j *Jsonb) Scan(src interface{}) error {
  68. source, ok := src.([]byte)
  69. if !ok {
  70. return errors.New("Type assertion .([]byte) failed")
  71. }
  72. var i interface{}
  73. err := json.Unmarshal(source, &i)
  74. if err != nil {
  75. return err
  76. }
  77. if i == nil {
  78. return nil
  79. }
  80. *j, ok = i.(map[string]interface{})
  81. if !ok {
  82. return errors.New("reading from DB into Jsonb, failed to convert to map[string]interface{}")
  83. }
  84. return nil
  85. }
  86. // UnOrdered is a convenience value to make it clear you're not sorting a query.
  87. var UnOrdered = OrderBy{}
  88. // OrderByDesc returns a sort order descending by the given field.
  89. func OrderByDesc(field string) OrderBy {
  90. return OrderBy{
  91. Field: field,
  92. Order: OrderDesc,
  93. }
  94. }
  95. // OrderByAsc returns a sort order ascending by the given field.
  96. func OrderByAsc(field string) OrderBy {
  97. return OrderBy{
  98. Field: field,
  99. Order: OrderAsc,
  100. }
  101. }
  102. // OrderBy indicates how rows should be sorted.
  103. type OrderBy struct {
  104. Field string
  105. Order SortOrder
  106. }
  107. func (o OrderBy) String() string {
  108. if o.Order == OrderNone {
  109. return ""
  110. }
  111. return " ORDER BY " + o.Field + " " + o.Order.String() + " "
  112. }
  113. // SortOrder defines how to order rows returned.
  114. type SortOrder int
  115. // Defined sort orders for not sorted, descending and ascending.
  116. const (
  117. OrderNone SortOrder = iota
  118. OrderDesc
  119. OrderAsc
  120. )
  121. // String returns the sql string representation of this sort order.
  122. func (s SortOrder) String() string {
  123. switch s {
  124. case OrderDesc:
  125. return "DESC"
  126. case OrderAsc:
  127. return "ASC"
  128. }
  129. return ""
  130. }
  131. // WhereClause has a String function should return a properly formatted where
  132. // clause (not including the WHERE) for positional arguments starting at idx.
  133. type WhereClause interface {
  134. String(idx *int) string
  135. Values() []interface{}
  136. }
  137. // Comparison is used by WhereClauses to create valid sql.
  138. type Comparison string
  139. // Comparison types.
  140. const (
  141. CompEqual Comparison = " = "
  142. CompGreater Comparison = " > "
  143. CompLess Comparison = " < "
  144. CompGTE Comparison = " >= "
  145. CompLTE Comparison = " <= "
  146. CompNE Comparison = " <> "
  147. )
  148. type Where struct {
  149. Field string
  150. Comp Comparison
  151. Value interface{}
  152. }
  153. func (w Where) String(idx *int) string {
  154. ret := w.Field + string(w.Comp) + "$" + strconv.Itoa(*idx)
  155. (*idx)++
  156. return ret
  157. }
  158. func (w Where) Values() []interface{} {
  159. return []interface{}{w.Value}
  160. }
  161. // NullClause is a clause that checks for a column being null or not.
  162. type NullClause struct {
  163. Field string
  164. Null bool
  165. }
  166. func (n NullClause) String(idx *int) string {
  167. if n.Null {
  168. return n.Field + " IS NULL "
  169. }
  170. return n.Field + " IS NOT NULL "
  171. }
  172. func (n NullClause) Values() []interface{} {
  173. return []interface{}{}
  174. }
  175. // AndClause returns a WhereClause that serializes to the AND
  176. // of all the given where clauses.
  177. func AndClause(wheres ...WhereClause) WhereClause {
  178. return andClause(wheres)
  179. }
  180. type andClause []WhereClause
  181. func (a andClause) String(idx *int) string {
  182. wheres := make([]string, len(a))
  183. for x := 0; x < len(a); x++ {
  184. wheres[x] = a[x].String(idx)
  185. }
  186. return strings.Join(wheres, " AND ")
  187. }
  188. func (a andClause) Values() []interface{} {
  189. vals := make([]interface{}, 0, len(a))
  190. for x := 0; x < len(a); x++ {
  191. vals = append(vals, a[x].Values()...)
  192. }
  193. return vals
  194. }
  195. // OrClause returns a WhereClause that serializes to the OR
  196. // of all the given where clauses.
  197. func OrClause(wheres ...WhereClause) WhereClause {
  198. return orClause(wheres)
  199. }
  200. type orClause []WhereClause
  201. func (o orClause) String(idx *int) string {
  202. wheres := make([]string, len(o))
  203. for x := 0; x < len(wheres); x++ {
  204. wheres[x] = o[x].String(idx)
  205. }
  206. return strings.Join(wheres, " OR ")
  207. }
  208. func (o orClause) Values() []interface{} {
  209. vals := make([]interface{}, len(o))
  210. for x := 0; x < len(o); x++ {
  211. vals = append(vals, o[x].Values()...)
  212. }
  213. return vals
  214. }
  215. // InClause takes a slice of values that it matches against.
  216. type InClause struct {
  217. Field string
  218. Vals []interface{}
  219. }
  220. func (in InClause) String(idx *int) string {
  221. ret := in.Field + " in ("
  222. for x := range in.Vals {
  223. if x != 0 {
  224. ret += ", "
  225. }
  226. ret += "$" + strconv.Itoa(*idx)
  227. (*idx)++
  228. }
  229. ret += ")"
  230. return ret
  231. }
  232. func (in InClause) Values() []interface{} {
  233. return in.Vals
  234. }