/
rebecca.go
88 lines (78 loc) · 2.29 KB
/
rebecca.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Package rebecca is lightweight convenience library for work with database
//
// See github README for instructions: https://github.com/waterlink/rebecca#rebecca
//
// See examples: https://godoc.org/github.com/waterlink/rebecca#pkg-examples
//
// Simple example:
//
// type Person struct {
// rebecca.ModelMetadata `tablename:"people"`
//
// ID int `rebecca:"id" rebecca_primary:"true"`
// Name string `rebecca:"name"`
// Age int `rebecca:"age"`
// }
//
// // Create new record
// p := &Person{Name: "John", Age: 34}
// if err := rebecca.Save(p); err != nil {
// // handle error here
// }
// fmt.Print(p)
//
// // Update existing record
// p.Name = "John Smith"
// if err := rebecca.Save(p); err != nil {
// // handle error here
// }
// fmt.Print(p)
//
// // Get record by its primary key
// p = &Person{}
// if err := rebecca.Get(p, 25); err != nil {
// // handle error here
// }
// fmt.Print(p)
package rebecca
import "github.com/waterlink/rebecca/driver"
// This file contains thin exported functions only.
//
// For unexported functions see: helpers.go
//
// For Context see: context.go
// SetupDriver is for configuring database driver
func SetupDriver(d driver.Driver) {
driver.SetupDriver(d)
}
// Get is for fetching one record
func Get(record interface{}, ID interface{}) error {
return get(nil, ID, record)
}
// All is for fetching all records
func All(records interface{}) error {
ctx := &Context{}
return ctx.All(records)
}
// Where is for fetching specific records
func Where(records interface{}, where string, args ...interface{}) error {
ctx := &Context{}
return ctx.Where(records, where, args...)
}
// First is for fetching only one specific record
func First(record interface{}, where string, args ...interface{}) error {
ctx := &Context{}
return ctx.First(record, where, args...)
}
// Save is for saving one record (either creating or updating)
func Save(record interface{}) error {
return save(nil, record)
}
// Remove is for removing the record
func Remove(record interface{}) error {
return remove(nil, record)
}
// Exec is for executing arbitrary query and discarding its result
func Exec(query string, args ...interface{}) error {
return exec(nil, query, args...)
}