Skip to content

Commit e2f9a15

Browse files
committed
first
1 parent 1b00908 commit e2f9a15

File tree

6 files changed

+143
-1
lines changed

6 files changed

+143
-1
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
17+
.idea

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# apijson-go
1+
# apijson-go
2+
3+
## 0.0.1 实现单表查询

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/keepfoo/apijson
2+
3+
go 1.16
4+
5+
require (
6+
github.com/go-sql-driver/mysql v1.6.0 // indirect
7+
github.com/jmoiron/sqlx v1.3.1 // indirect
8+
)

go.sum

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
2+
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
3+
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
4+
github.com/jmoiron/sqlx v1.3.1 h1:aLN7YINNZ7cYOPK3QC83dbM6KT0NMqVMw961TqrejlE=
5+
github.com/jmoiron/sqlx v1.3.1/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
6+
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
7+
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=

main.go

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
_ "github.com/go-sql-driver/mysql"
7+
"github.com/jmoiron/sqlx"
8+
"io/ioutil"
9+
"log"
10+
"net/http"
11+
"strings"
12+
)
13+
14+
var db *sqlx.DB
15+
16+
func main() {
17+
http.HandleFunc("/get", GetHandler)
18+
http.ListenAndServe("127.0.0.1:8000", nil)
19+
}
20+
21+
func init() {
22+
database, err := sqlx.Open("mysql", "apijson:1234qqqq@tcp(y.tadev.cn:53306)/sys")
23+
if err != nil {
24+
log.Fatal("db connect error", err)
25+
}
26+
db = database
27+
}
28+
29+
func GetHandler(w http.ResponseWriter, r *http.Request) {
30+
if data, err := ioutil.ReadAll(r.Body); err != nil {
31+
log.Println("read request body error", err)
32+
w.WriteHeader(http.StatusBadRequest)
33+
} else {
34+
var bodyMap map[string]interface{}
35+
if err := json.Unmarshal(data, &bodyMap); err != nil {
36+
log.Println("parse request body json error", err)
37+
w.WriteHeader(http.StatusBadRequest)
38+
return
39+
}
40+
respMap := make(map[string]interface{})
41+
for table, fields := range bodyMap {
42+
if fields != nil {
43+
respMap[table] = QueryTable(table, fields)
44+
}
45+
log.Println("get:query table: ", table, ", fields: ", fields)
46+
}
47+
if respBody, err := json.Marshal(respMap); err != nil {
48+
w.WriteHeader(http.StatusInternalServerError)
49+
} else {
50+
w.WriteHeader(http.StatusOK)
51+
w.Write(respBody)
52+
}
53+
}
54+
}
55+
56+
func QueryTable(table string, fields interface{}) interface{} {
57+
var buffer bytes.Buffer
58+
buffer.WriteString("select * from ")
59+
buffer.WriteString(table)
60+
buffer.WriteString(" where ")
61+
if fieldMap, ok := fields.(map[string]interface{}); !ok {
62+
return "fields error, only support object."
63+
} else {
64+
size := len(fieldMap)
65+
cols := make([]string, size)
66+
values := make([]interface{}, size)
67+
i := 0
68+
for col, value := range fieldMap {
69+
if value == nil {
70+
return "field value error, " + col + " is nil"
71+
}
72+
cols[i] = col + "=?"
73+
values[i] = value
74+
}
75+
buffer.WriteString(strings.Join(cols, " and "))
76+
sql := buffer.String()
77+
if rows, err := db.Query(sql, values...); err != nil {
78+
return err.Error()
79+
} else {
80+
if rows.Next() {
81+
if columns, err := rows.Columns(); err != nil {
82+
return "get rows error: " + err.Error()
83+
} else {
84+
values := make([]interface{}, len(columns))
85+
for k := range columns {
86+
str := ""
87+
values[k] = &str
88+
}
89+
err = rows.Scan(values...)
90+
if err != nil {
91+
return "rows.Scan error: " + err.Error()
92+
}
93+
resultMap := make(map[string]interface{})
94+
for k, colName := range columns {
95+
resultMap[colName] = values[k]
96+
}
97+
return resultMap
98+
}
99+
} else {
100+
return ""
101+
}
102+
}
103+
}
104+
}

test.http

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
POST 127.0.0.1:8000/get
2+
Content-Type: application/json
3+
4+
{
5+
"Document": {
6+
"id": 1
7+
}
8+
}
9+
10+
###
11+
12+
POST 127.0.0.1:8000/get
13+
Content-Type: application/json
14+
15+
{
16+
"document": {
17+
"name": "TommyLemon"
18+
}
19+
}

0 commit comments

Comments
 (0)