-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.go
More file actions
152 lines (127 loc) · 3.51 KB
/
set.go
File metadata and controls
152 lines (127 loc) · 3.51 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package gosql
//todo: add methods for `CREATE INDEX` and `DROP INDEX`: https://www.w3schools.com/sql/sql_create_index.asp
// Set will INSERT or UPDATE values FROM table
//
// If a where query exists, this method will only use UPDATE.
//
// If the unique arg exists, this method will check if the database contains
// all matching key = value pairs. If it finds and, it will use UPDATE, and
// if nothing is found, it will use INSERT.
//
// If no unique args or where query exists, this method will default to INSERT.
func (query *Query) Set(values map[string]any, unique ...string) error {
if len(values) == 0 {
return nil
}
valList := []any{}
// UPDATE if where query
if query.where != "" {
q := `UPDATE ` + query.table + ` SET `
for key, val := range values {
q += toAlphaNumeric(key) + ` = ?, `
valList = append(valList, val)
}
q = q[:len(q)-2]
q += ` ` + query.where
valList = append(valList, query.whereValue...)
st, err := query.db.Prepare(q)
if err != nil {
return err
}
_, err = st.Exec(valList...)
return err
}
// UPDATE if unique keys found with matching values
if len(unique) != 0 {
where := `WHERE `
whereValue := []any{}
// build where query
hasVal := false
for _, key := range unique {
key = toAlphaNumeric(key)
if val, ok := values[key]; ok {
if hasVal {
where += ` AND `
}
hasVal = true
where += key + ` = ?`
whereValue = append(whereValue, val)
}
}
// check if table contains existing rows
if hasVal {
if rows, err := query.db.Query(`SELECT * FROM `+query.table+` `+where, whereValue...); err == nil && rows.Next() {
rows.Close()
// UPDATE values in existing rows
q := `UPDATE ` + query.table + ` SET `
for key, val := range values {
q += toAlphaNumeric(key) + ` = ?, `
valList = append(valList, val)
}
q = q[:len(q)-2]
q += ` ` + where
valList = append(valList, whereValue...)
st, err := query.db.Prepare(q)
if err != nil {
return err
}
_, err = st.Exec(valList...)
return err
}
}
}
// INSERT values into table
qKey := ``
qVal := ``
for key, val := range values {
qKey += toAlphaNumeric(key) + `, `
qVal += `?, `
valList = append(valList, val)
}
qKey = qKey[:len(qKey)-2]
qVal = qVal[:len(qVal)-2]
st, err := query.db.Prepare(`INSERT INTO ` + query.table + ` (` + qKey + `) VALUES (` + qVal + `)`)
if err != nil {
return err
}
st.Exec(valList...)
return nil
}
// Delete will remove a row from the database table
//
// ! Warning: setting @force to true, will allow the database to delete all rows from a table, if its missing a `where` query
func (query *Query) Delete(force ...bool) error {
if query.where == "" {
if len(force) != 0 && force[0] {
st, err := query.db.Prepare(`DELETE FROM ` + query.table)
if err != nil {
return err
}
st.Exec()
return nil
}
return Error_UnsafeQuery
}
st, err := query.db.Prepare(`DELETE FROM ` + query.table + ` ` + query.where)
if err != nil {
return err
}
st.Exec(query.whereValue...)
return nil
}
// Drop will drop an entire table from the database, deleting everything
//
// ! Warning: This method will Delete the entire table, and will ignore any `where` queries
func (query *Query) Drop(force bool) error {
if !force {
return Error_UnsafeQuery
}
// Note: query.db.SQL will bypass the default safety checks,
// since the `DROP` keyword will be denied by safety checks.
st, err := query.db.SQL.Prepare(`DROP TABLE ` + query.table)
if err != nil {
return err
}
st.Exec()
return nil
}