-
Notifications
You must be signed in to change notification settings - Fork 2
/
queries.go
136 lines (117 loc) · 3.22 KB
/
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
128
129
130
131
132
133
134
135
136
package mysql
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"os"
"path/filepath"
gotemplate "text/template"
"github.com/nuveo/log"
"github.com/prest/adapter-mysql/internal/connection"
"github.com/prest/adapters"
"github.com/prest/adapters/scanner"
"github.com/prest/config"
"github.com/prest/template"
)
// GetScript get SQL template file
func (adapter *MySQL) GetScript(verb, folder, scriptName string) (script string, err error) {
verbs := map[string]string{
"GET": ".read.sql",
"POST": ".write.sql",
"PATCH": ".update.sql",
"PUT": ".update.sql",
"DELETE": ".delete.sql",
}
sufix, ok := verbs[verb]
if !ok {
err = fmt.Errorf("invalid http method %s", verb)
return
}
script = filepath.Join(config.PrestConf.QueriesPath, folder, fmt.Sprint(scriptName, sufix))
if _, err = os.Stat(script); os.IsNotExist(err) {
err = fmt.Errorf("could not load %s", script)
return
}
return
}
// ParseScript use values sent by users and add on script
func (adapter *MySQL) ParseScript(scriptPath string, queryURL url.Values) (sqlQuery string, values []interface{}, err error) {
_, tplName := filepath.Split(scriptPath)
q := make(map[string]interface{})
for key, value := range queryURL {
if len(value) == 1 {
q[key] = value[0]
continue
}
q[key] = value
}
funcs := &template.FuncRegistry{TemplateData: q}
tpl := gotemplate.New(tplName).Funcs(funcs.RegistryAllFuncs())
tpl, err = tpl.ParseFiles(scriptPath)
if err != nil {
err = fmt.Errorf("could not parse file %s: %+v", scriptPath, err)
return
}
var buff bytes.Buffer
err = tpl.Execute(&buff, funcs.TemplateData)
if err != nil {
err = fmt.Errorf("could not execute template %v", err)
return
}
sqlQuery = buff.String()
return
}
// WriteSQL perform INSERT's, UPDATE's, DELETE's operations
func WriteSQL(sql string, values []interface{}) (sc adapters.Scanner) {
db, err := connection.Get()
if err != nil {
log.Println(err)
sc = &scanner.PrestScanner{Error: err}
return
}
stmt, err := Prepare(db, sql)
if err != nil {
log.Printf("could not prepare sql: %s\n Error: %v\n", sql, err)
sc = &scanner.PrestScanner{Error: err}
return
}
valuesAux := make([]interface{}, 0, len(values))
for i := 0; i < len(values); i++ {
valuesAux = append(valuesAux, values[i])
}
result, err := stmt.Exec(valuesAux...)
if err != nil {
log.Printf("sql = %+v\n", sql)
err = fmt.Errorf("could not peform sql: %v", err)
sc = &scanner.PrestScanner{Error: err}
return
}
rowsAffected, err := result.RowsAffected()
if err != nil {
err = fmt.Errorf("could not rows affected: %v", err)
sc = &scanner.PrestScanner{Error: err}
return
}
data := make(map[string]interface{})
data["rows_affected"] = rowsAffected
var resultByte []byte
resultByte, err = json.Marshal(data)
sc = &scanner.PrestScanner{
Error: err,
Buff: bytes.NewBuffer(resultByte),
}
return
}
// ExecuteScripts run sql templates created by users
func (adapter *MySQL) ExecuteScripts(method, sql string, values []interface{}) (sc adapters.Scanner) {
switch method {
case "GET":
sc = adapter.Query(sql, values...)
case "POST", "PUT", "PATCH", "DELETE":
sc = WriteSQL(sql, values)
default:
sc = &scanner.PrestScanner{Error: fmt.Errorf("invalid method %s", method)}
}
return
}