forked from go-xorm/sqlfiddle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlfiddle.go
192 lines (167 loc) · 4.44 KB
/
sqlfiddle.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2018 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlfiddle
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
const (
Mysql5_6 = 9
Oracle11gR2 = 4
PostgreSQL96 = 17
PostgreSQL93 = 15
SQLite_WebSQL = 7
SQLite_SQLjs = 5
MSSQL2017 = 18
)
/*
{
"_id" : "9_ed3b0c",
"short_code" : "ed3b0c",
"schema_structure" : [ {
"columns" : [ {
"name" : "id",
"type" : "INT(10)"
}, {
"name" : "name",
"type" : "VARCHAR(8)"
}, {
"name" : "birthday",
"type" : "DATETIME(19)"
} ],
"table_name" : "person",
"table_type" : "TABLE"
} ]
}
*/
const (
defaultBaseURL = "http://sqlfiddle.com"
)
type Fiddle struct {
baseURL string
}
func NewFiddle(baseURL string) *Fiddle {
if baseURL == "" {
baseURL = defaultBaseURL
}
return &Fiddle{baseURL}
}
type column struct {
Name string `json:"name"`
Type string `json:"type"`
}
type schemaStructure struct {
Columns []column `json:"columns"`
TableName string `json:"table_name"`
TableType string `json:"table_type"`
}
type response struct {
Id string `json:"_id"`
Code string `json:"short_code"`
Structures []schemaStructure `json:"schema_structure"`
ErrorMsg string `json:"error"`
}
func (f *Fiddle) CreateSchema(dbType int, sql string) (*response, error) {
payload := map[string]interface{}{"statement_separator": ";", "db_type_id": dbType, "ddl": sql}
bs, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", f.baseURL+"/backend/createSchema?_action=create", bytes.NewBuffer(bs))
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var res response
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return nil, err
}
if res.ErrorMsg != "" {
return &res, errors.New(res.ErrorMsg)
}
return &res, nil
}
/*
http://sqlfiddle.com/backend/executeQuery?_action=query
{"db_type_id":9,"schema_short_code":"ed3b0c","statement_separator":";","sql":"select * from person;"}
{
"ID" : 2,
"sets" : [ {
"RESULTS" : {
"COLUMNS" : [ "id", "name", "birthday" ],
"DATA" : [ ]
},
"SUCCEEDED" : true,
"STATEMENT" : "select * from person",
"EXECUTIONTIME" : 2,
"EXECUTIONPLANRAW" : {
"COLUMNS" : [ "id", "select_type", "table", "type", "possible_keys", "key", "key_len", "ref", "rows", "filtered", "Extra" ],
"DATA" : [ [ "1", "SIMPLE", "person", "ALL", null, null, null, null, "1", "100.00", null ] ]
},
"EXECUTIONPLAN" : {
"COLUMNS" : [ "id", "select_type", "table", "type", "possible_keys", "key", "key_len", "ref", "rows", "filtered", "Extra" ],
"DATA" : [ [ "1", "SIMPLE", "person", "ALL", null, null, null, null, "1", "100.00", null ] ]
}
} ]
}
*/
type sqlResult struct {
Statement string `json:"STATEMENT"`
Succeeded bool `json:"SUCCEEDED"`
ErrorMessage string `json:"ERRORMESSAGE"`
ExecutionTime int `json:"EXECUTIONTIME"`
Results struct {
Columns []string `json:"COLUMNS"`
Data [][]interface{} `json:"DATA"`
} `json:"RESULTS"`
ExecutionPlanRaw struct {
Columns []string `json:"COLUMNS"`
Data [][]interface{} `json:"DATA"`
} `json:"EXECUTIONPLANRAW"`
ExecutionPlan struct {
Columns []string `json:"COLUMNS"`
Data [][]interface{} `json:"DATA"`
} `json:"EXECUTIONPLAN"`
}
type sqlResponse struct {
Id int64 `json:"ID"`
Sets []sqlResult
}
func (f *Fiddle) RunSQL(dbType int, code, sql string) (*sqlResponse, error) {
payload := map[string]interface{}{"schema_short_code": code, "statement_separator": ";", "db_type_id": dbType, "sql": sql}
bs, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", f.baseURL+"/backend/executeQuery?_action=query", bytes.NewBuffer(bs))
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var res sqlResponse
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return nil, err
}
for e := range res.Sets {
result := res.Sets[e]
if result.ErrorMessage != "" {
return &res, fmt.Errorf("something wrong with sql %q in pos %v, detail: %q",
result.Statement, e+1, result.ErrorMessage)
}
}
return &res, nil
}