forked from doug-martin/goqu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_actions.go
74 lines (65 loc) · 2.76 KB
/
dataset_actions.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
package goqu
//Generates the SELECT sql for this dataset and uses Exec#ScanStructs to scan the results into a slice of structs
//
//i: A pointer to a slice of structs
func (me *Dataset) ScanStructs(i interface{}) error {
sql, args, err := me.ToSql()
return newCrudExec(me.database, err, sql, args...).ScanStructs(i)
}
//Generates the SELECT sql for this dataset and uses Exec#ScanStruct to scan the result into a slice of structs
//
//i: A pointer to a structs
func (me *Dataset) ScanStruct(i interface{}) (bool, error) {
sql, args, err := me.Limit(1).ToSql()
return newCrudExec(me.database, err, sql, args...).ScanStruct(i)
}
//Generates the SELECT sql for this dataset and uses Exec#ScanVals to scan the results into a slice of primitive values
//
//i: A pointer to a slice of primitive values
func (me *Dataset) ScanVals(i interface{}) error {
sql, args, err := me.ToSql()
return newCrudExec(me.database, err, sql, args...).ScanVals(i)
}
//Generates the SELECT sql for this dataset and uses Exec#ScanVal to scan the result into a primitive value
//
//i: A pointer to a primitive value
func (me *Dataset) ScanVal(i interface{}) (bool, error) {
sql, args, err := me.Limit(1).ToSql()
return newCrudExec(me.database, err, sql, args...).ScanVal(i)
}
//Generates the SELECT COUNT(*) sql for this dataset and uses Exec#ScanVal to scan the result into an int64.
func (me *Dataset) Count() (int64, error) {
var count int64
_, err := me.Select(COUNT(Star()).As("count")).ScanVal(&count)
return count, err
}
//Generates the SELECT sql only selecting the passed in column and uses Exec#ScanVals to scan the result into a slice of primitive values.
//
//i: A slice of primitive values
//
//col: The column to select when generative the SQL
func (me *Dataset) Pluck(i interface{}, col string) error {
return me.Select(col).ScanVals(i)
}
//Generates the UPDATE sql, and returns an Exec struct with the sql set to the UPDATE statement
// db.From("test").Update(Record{"name":"Bob", update: time.Now()}).Exec()
//
//See Dataset#UpdateSql for arguments
func (me *Dataset) Update(i interface{}) *CrudExec {
sql, args, err := me.ToUpdateSql(i)
return newCrudExec(me.database, err, sql, args...)
}
//Generates the UPDATE sql, and returns an Exec struct with the sql set to the INSERT statement
// db.From("test").Insert(Record{"name":"Bob").Exec()
//
//See Dataset#InsertSql for arguments
func (me *Dataset) Insert(i ...interface{}) *CrudExec {
sql, args, err := me.ToInsertSql(i...)
return newCrudExec(me.database, err, sql, args...)
}
//Generates the DELETE sql, and returns an Exec struct with the sql set to the DELETE statement
// db.From("test").Where(I("id").Gt(10)).Exec()
func (me *Dataset) Delete() *CrudExec {
sql, args, err := me.ToDeleteSql()
return newCrudExec(me.database, err, sql, args...)
}