This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_queries.go
127 lines (116 loc) · 3.4 KB
/
model_queries.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package simpledb
import (
"database/sql"
"errors"
"strconv"
)
// Filter returns a QuerySet with the given filters applied.
func (d *Database) Filter(model Model, filter Filters, include []string) ModelSet {
return d.FilterWithLimit(model, filter, d.LIMIT, include)
}
// See Filter.
// Also allows you to specify a limit on the number of results returned.
func (d *Database) FilterWithLimit(model Model, filters Filters, limit int, include []string) ModelSet {
var query string = `SELECT * FROM ` + model.TableName()
f_query, values := filters.Query(false)
if f_query == "" {
return nil
}
query += f_query
query += " ORDER BY id DESC"
query += ` LIMIT ` + strconv.Itoa(limit)
results, err := d.Query(query, values...)
if err != nil {
panic(err)
// return nil
}
return ScanRows(results, model, include)
}
// AllQ returns a query that will return all rows in the table.
func (d *Database) AllQ(model Model, exclude []string) string {
cols := Columns(model)
cols = Exclude(cols, exclude)
query := "SELECT "
for i, col := range cols {
query += col
if i < len(cols)-1 {
query += ", "
}
}
query += " FROM " + model.TableName()
query += " ORDER BY id DESC"
query += " LIMIT " + strconv.Itoa(d.LIMIT)
return query
}
// Insert a model into the database.
// Takes a pointer to a model and a sql.Row and scans the ID of result into the model
func (d *Database) InsertModel(model Model) error {
columns := Columns(model)
values := make([]interface{}, len(columns))
for i, column := range columns {
values[i] = GetValue(model, column)
}
id, err := d.ExecInsert(model.TableName(), columns, values)
if err != nil {
return err
}
SetValue(model, "id", id)
return nil
}
// Update a model in the database.
func (d *Database) UpdateModel(model Model) (Model, error) {
columns := Columns(model)
values := make([]interface{}, len(columns))
for i, column := range columns {
values[i] = GetValue(model, column)
}
id, err := d.ExecUpdate(model.TableName(), columns, values, "id = ?", GetValue(model, "id"))
if err != nil {
return nil, err
}
// Set the ID of the model to the ID of the updated model
SetValue(model, "id", id)
// Return the updated model
return model, nil
}
// All models from a table
func (d *Database) AllModel(model Model, include []string) ModelSet {
query := d.AllQ(model, include)
rows, err := d.Query(query)
if err != nil {
return nil
}
ms := ScanRows(rows, model, include)
rows.Close()
return ms
}
// Scan rows into models, put those into a ModelSet
func ScanRows(rows *sql.Rows, model Model, include []string) ModelSet {
var models []Model
for rows.Next() {
model := NewModel(model)
if err := Scan(model, rows, include); err != nil {
panic(err)
}
models = append(models, model)
}
return models
}
// Scan a row into a model
func ScanRow(row *sql.Row, model Model, include []string) (Model, error) {
model = NewModel(model)
fields, err := modelFields(model, include)
if err != nil {
return nil, errors.New("modelFields: " + err.Error())
}
err = row.Scan(fields...)
if err != nil {
err = errors.New("no results found: " + err.Error())
}
return model, err
}
// Delete a model from the database
func (d *Database) DeleteModel(model Model) error {
_, err := d.Exec("DELETE FROM "+model.TableName()+" WHERE id = ?", GetValue(model, "id"))
return err
}