graphql 从数据库查询
This commit is contained in:
10
bin/main.go
10
bin/main.go
@@ -197,9 +197,7 @@ func main() {
|
|||||||
http.Error(w, result.Errors[0].Error(), 500)
|
http.Error(w, result.Errors[0].Error(), 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 格式化输出
|
|
||||||
rJSON, _ := json.MarshalIndent(result, "", " ")
|
rJSON, _ := json.MarshalIndent(result, "", " ")
|
||||||
fmt.Printf("%s \n", rJSON)
|
|
||||||
w.Write(rJSON)
|
w.Write(rJSON)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -388,8 +386,8 @@ func main() {
|
|||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var image Image
|
var image Image
|
||||||
rows.Scan(&image.Id, &image.Width, &image.Height, &image.Content, &image.UpdateTime, &image.CreateTime, &image.User.Id, &image.Article.Id, &image.ArticleCategoryTopId, &image.PraiseCount, &image.CollectCount)
|
rows.Scan(&image.Id, &image.Width, &image.Height, &image.Content, &image.UpdateTime, &image.CreateTime, &image.User.ID, &image.Article.Id, &image.ArticleCategoryTopId, &image.PraiseCount, &image.CollectCount)
|
||||||
fmt.Println("image", image.User.Id, image.Article.Id)
|
fmt.Println("image", image.User.ID, image.Article.Id)
|
||||||
image.UpdateTime = image.UpdateTime.UTC()
|
image.UpdateTime = image.UpdateTime.UTC()
|
||||||
image.CreateTime = image.CreateTime.UTC()
|
image.CreateTime = image.CreateTime.UTC()
|
||||||
image.Content = regexp.MustCompile(`http:`).ReplaceAllString(image.Content, "https:")
|
image.Content = regexp.MustCompile(`http:`).ReplaceAllString(image.Content, "https:")
|
||||||
@@ -427,7 +425,7 @@ func main() {
|
|||||||
var article_ids []int
|
var article_ids []int
|
||||||
for _, image := range image_list {
|
for _, image := range image_list {
|
||||||
fmt.Println("image", image)
|
fmt.Println("image", image)
|
||||||
user_ids = append(user_ids, image.User.Id)
|
user_ids = append(user_ids, image.User.ID)
|
||||||
article_ids = append(article_ids, image.Article.Id)
|
article_ids = append(article_ids, image.Article.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -435,7 +433,7 @@ func main() {
|
|||||||
users := models.QueryUserList(user_ids)
|
users := models.QueryUserList(user_ids)
|
||||||
for i, image := range image_list {
|
for i, image := range image_list {
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
if image.User.Id == user.Id {
|
if image.User.ID == user.ID {
|
||||||
image_list[i].User = user
|
image_list[i].User = user
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
7
go.mod
7
go.mod
@@ -7,6 +7,9 @@ require (
|
|||||||
github.com/alibabacloud-go/tea v1.2.1
|
github.com/alibabacloud-go/tea v1.2.1
|
||||||
github.com/alibabacloud-go/vod-20170321/v2 v2.16.10
|
github.com/alibabacloud-go/vod-20170321/v2 v2.16.10
|
||||||
github.com/disintegration/imaging v1.6.2
|
github.com/disintegration/imaging v1.6.2
|
||||||
|
github.com/elastic/go-elasticsearch/v8 v8.11.0
|
||||||
|
github.com/graphql-go/graphql v0.8.1
|
||||||
|
github.com/jmoiron/sqlx v1.3.5
|
||||||
github.com/milvus-io/milvus-sdk-go/v2 v2.2.1
|
github.com/milvus-io/milvus-sdk-go/v2 v2.2.1
|
||||||
github.com/spf13/viper v1.15.0
|
github.com/spf13/viper v1.15.0
|
||||||
)
|
)
|
||||||
@@ -21,10 +24,8 @@ require (
|
|||||||
github.com/aliyun/credentials-go v1.1.2 // indirect
|
github.com/aliyun/credentials-go v1.1.2 // indirect
|
||||||
github.com/clbanning/mxj/v2 v2.5.6 // indirect
|
github.com/clbanning/mxj/v2 v2.5.6 // indirect
|
||||||
github.com/elastic/elastic-transport-go/v8 v8.3.0 // indirect
|
github.com/elastic/elastic-transport-go/v8 v8.3.0 // indirect
|
||||||
github.com/elastic/go-elasticsearch/v8 v8.11.0 // indirect
|
|
||||||
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
||||||
github.com/golang/protobuf v1.5.2 // indirect
|
github.com/golang/protobuf v1.5.2 // indirect
|
||||||
github.com/graphql-go/graphql v0.8.1 // indirect
|
|
||||||
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
|
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
|
||||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
@@ -55,7 +56,7 @@ require (
|
|||||||
require (
|
require (
|
||||||
github.com/aliyun/aliyun-oss-go-sdk v2.2.7+incompatible
|
github.com/aliyun/aliyun-oss-go-sdk v2.2.7+incompatible
|
||||||
github.com/chai2010/webp v1.1.1
|
github.com/chai2010/webp v1.1.1
|
||||||
github.com/go-sql-driver/mysql v1.7.0
|
github.com/go-sql-driver/mysql v1.7.1
|
||||||
github.com/sizeofint/gif-to-webp v0.0.0-20210224202734-e9d7ed071591
|
github.com/sizeofint/gif-to-webp v0.0.0-20210224202734-e9d7ed071591
|
||||||
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect
|
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect
|
||||||
)
|
)
|
||||||
|
11
go.sum
11
go.sum
@@ -103,8 +103,9 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2
|
|||||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||||
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||||
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
|
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||||
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
|
||||||
|
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||||
@@ -179,6 +180,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
|||||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||||
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||||
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||||
|
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
|
||||||
|
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
|
||||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
@@ -195,8 +198,12 @@ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
|||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
|
||||||
|
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||||
github.com/milvus-io/milvus-proto/go-api v0.0.0-20230301092744-7efc6eec15fd h1:9ilgTEqZSdEPbJKSrRGB1TIHTaF7DqVDIwn8/azcaBk=
|
github.com/milvus-io/milvus-proto/go-api v0.0.0-20230301092744-7efc6eec15fd h1:9ilgTEqZSdEPbJKSrRGB1TIHTaF7DqVDIwn8/azcaBk=
|
||||||
github.com/milvus-io/milvus-proto/go-api v0.0.0-20230301092744-7efc6eec15fd/go.mod h1:148qnlmZ0Fdm1Fq+Mj/OW2uDoEP25g3mjh0vMGtkgmk=
|
github.com/milvus-io/milvus-proto/go-api v0.0.0-20230301092744-7efc6eec15fd/go.mod h1:148qnlmZ0Fdm1Fq+Mj/OW2uDoEP25g3mjh0vMGtkgmk=
|
||||||
github.com/milvus-io/milvus-sdk-go/v2 v2.2.1 h1:6p/lrxZCGkw5S2p5GPWy/BUmO6mVUNfrczv98uAnhoU=
|
github.com/milvus-io/milvus-sdk-go/v2 v2.2.1 h1:6p/lrxZCGkw5S2p5GPWy/BUmO6mVUNfrczv98uAnhoU=
|
||||||
|
@@ -2,8 +2,10 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/graphql-go/graphql"
|
"github.com/graphql-go/graphql"
|
||||||
|
"github.com/graphql-go/graphql/language/ast"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewSchema() (graphql.Schema, error) {
|
func NewSchema() (graphql.Schema, error) {
|
||||||
@@ -11,41 +13,59 @@ func NewSchema() (graphql.Schema, error) {
|
|||||||
Name: "User",
|
Name: "User",
|
||||||
Fields: graphql.Fields{
|
Fields: graphql.Fields{
|
||||||
"id": &graphql.Field{Type: graphql.Int},
|
"id": &graphql.Field{Type: graphql.Int},
|
||||||
"name": &graphql.Field{Type: graphql.String},
|
"user_name": &graphql.Field{Type: graphql.String},
|
||||||
"age": &graphql.Field{Type: graphql.Int},
|
|
||||||
"info": &graphql.Field{Type: graphql.String},
|
|
||||||
"price": &graphql.Field{Type: graphql.Float},
|
|
||||||
"avatar": &graphql.Field{Type: graphql.String},
|
"avatar": &graphql.Field{Type: graphql.String},
|
||||||
|
"rank": &graphql.Field{Type: graphql.String},
|
||||||
|
"price": &graphql.Field{Type: graphql.Float},
|
||||||
|
"create_time": &graphql.Field{Type: graphql.DateTime},
|
||||||
|
"update_time": &graphql.Field{Type: graphql.DateTime},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
users := &graphql.Field{
|
users := &graphql.Field{
|
||||||
Type: graphql.NewList(user),
|
Type: graphql.NewList(user),
|
||||||
Args: graphql.FieldConfigArgument{
|
Args: graphql.FieldConfigArgument{
|
||||||
"id": &graphql.ArgumentConfig{
|
"id": &graphql.ArgumentConfig{Type: graphql.Int},
|
||||||
Type: graphql.Int,
|
"user_name": &graphql.ArgumentConfig{Type: graphql.String},
|
||||||
},
|
"avatar": &graphql.ArgumentConfig{Type: graphql.String},
|
||||||
|
"rank": &graphql.ArgumentConfig{Type: graphql.String},
|
||||||
|
"create_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
|
||||||
|
"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
|
||||||
},
|
},
|
||||||
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||||
fmt.Println("p.Args:", p.Args)
|
var fields []string
|
||||||
return []interface{}{
|
requestedFields := p.Info.FieldASTs[0].SelectionSet.Selections
|
||||||
map[string]interface{}{
|
for _, field := range requestedFields {
|
||||||
"id": 1,
|
fieldAST, ok := field.(*ast.Field)
|
||||||
"name": "user1",
|
if ok {
|
||||||
"age": 10,
|
fields = append(fields, fieldAST.Name.Value)
|
||||||
"info": "info",
|
}
|
||||||
"price": 1.1,
|
}
|
||||||
"avatar": "",
|
fields_str := strings.Join(fields, ",")
|
||||||
},
|
|
||||||
map[string]interface{}{
|
var where []string
|
||||||
"id": 2,
|
if p.Args["id"] != nil {
|
||||||
"name": "user2",
|
where = append(where, fmt.Sprintf("id=%d", p.Args["id"]))
|
||||||
"age": 20,
|
}
|
||||||
"info": "info",
|
if p.Args["user_name"] != nil {
|
||||||
"price": 2.2,
|
where = append(where, fmt.Sprintf("user_name='%s'", p.Args["user_name"]))
|
||||||
"avatar": "",
|
}
|
||||||
},
|
|
||||||
}, nil
|
// 筛选条件
|
||||||
|
where_str := strings.Join(where, " AND ")
|
||||||
|
fmt.Println(where_str)
|
||||||
|
|
||||||
|
var query strings.Builder
|
||||||
|
query.WriteString(fmt.Sprintf("SELECT %s FROM web_member WHERE %s LIMIT %s", fields_str, where_str, "10"))
|
||||||
|
fmt.Println(query.String())
|
||||||
|
|
||||||
|
var users []User
|
||||||
|
if err := connection.Select(&users, query.String()); err != nil {
|
||||||
|
fmt.Println("获取用户列表失败", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
fmt.Println(users)
|
||||||
|
return users, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,6 +96,9 @@ func NewSchema() (graphql.Schema, error) {
|
|||||||
},
|
},
|
||||||
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||||
fmt.Println("p.Args:", p.Args)
|
fmt.Println("p.Args:", p.Args)
|
||||||
|
|
||||||
|
//connection.Query("SELECT ")
|
||||||
|
|
||||||
return []interface{}{
|
return []interface{}{
|
||||||
map[string]interface{}{
|
map[string]interface{}{
|
||||||
"id": 1,
|
"id": 1,
|
||||||
|
@@ -10,14 +10,29 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var connection *sqlx.DB
|
||||||
|
var connectionx *sql.DB
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var err error
|
||||||
|
user := Viper.Get("mysql.user").(string)
|
||||||
|
password := Viper.Get("mysql.password").(string)
|
||||||
|
host := Viper.Get("mysql.host").(string)
|
||||||
|
port := Viper.Get("mysql.port").(int)
|
||||||
|
database := Viper.Get("mysql.database").(string)
|
||||||
|
connection, err = sqlx.Connect("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, password, host, port, database))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("连接数据库失败", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type MysqlConnection struct {
|
type MysqlConnection struct {
|
||||||
Database *sql.DB
|
Database *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
var connection *sql.DB
|
|
||||||
|
|
||||||
// 初始化数据库连接
|
// 初始化数据库连接
|
||||||
func (m *MysqlConnection) Init() (err error) {
|
func (m *MysqlConnection) Init() (err error) {
|
||||||
user := Viper.Get("mysql.user").(string)
|
user := Viper.Get("mysql.user").(string)
|
||||||
@@ -26,12 +41,12 @@ func (m *MysqlConnection) Init() (err error) {
|
|||||||
port := Viper.Get("mysql.port").(int)
|
port := Viper.Get("mysql.port").(int)
|
||||||
database := Viper.Get("mysql.database").(string)
|
database := Viper.Get("mysql.database").(string)
|
||||||
sqlconf := user + ":" + password + "@tcp(" + host + ":" + strconv.Itoa(port) + ")/" + database + "?charset=utf8mb4&parseTime=True&loc=Local"
|
sqlconf := user + ":" + password + "@tcp(" + host + ":" + strconv.Itoa(port) + ")/" + database + "?charset=utf8mb4&parseTime=True&loc=Local"
|
||||||
connection, err = sql.Open("mysql", sqlconf) // 连接数据库
|
m.Database, err = sql.Open("mysql", sqlconf) // 连接数据库
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("连接数据库失败", err)
|
log.Println("连接数据库失败", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m.Database = connection
|
connectionx = m.Database
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,11 +98,12 @@ func (m *MysqlConnection) GetImages(page int, size int) (images []byte, err erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Id int `json:"id"`
|
ID int `json:"id" db:"id"`
|
||||||
UserName string `json:"user_name"`
|
UserName string `json:"user_name" db:"user_name"`
|
||||||
Avatar string `json:"avatar"`
|
Avatar string `json:"avatar" db:"avatar"`
|
||||||
CreateTime time.Time `json:"create_time"`
|
Rank string `json:"rank" db:"rank"`
|
||||||
UpdateTime time.Time `json:"update_time"`
|
CreateTime time.Time `json:"create_time" db:"create_time"`
|
||||||
|
UpdateTime time.Time `json:"update_time" db:"update_time"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取一组用户信息
|
// 获取一组用户信息
|
||||||
@@ -98,7 +114,7 @@ func QueryUserList(id_list []int) (users []User) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
idstr := strings.Trim(strings.Replace(fmt.Sprint(id_list), " ", ",", -1), "[]")
|
idstr := strings.Trim(strings.Replace(fmt.Sprint(id_list), " ", ",", -1), "[]")
|
||||||
rows, err := connection.Query("SELECT id, user_name, avatar, update_time, create_time FROM web_member WHERE id IN (" + idstr + ") LIMIT " + strconv.Itoa(count))
|
rows, err := connectionx.Query("SELECT id, user_name, avatar, update_time, create_time FROM web_member WHERE id IN (" + idstr + ") LIMIT " + strconv.Itoa(count))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("获取用户列表失败", err)
|
log.Println("获取用户列表失败", err)
|
||||||
return
|
return
|
||||||
@@ -106,7 +122,7 @@ func QueryUserList(id_list []int) (users []User) {
|
|||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var u User
|
var u User
|
||||||
rows.Scan(&u.Id, &u.UserName, &u.Avatar, &u.UpdateTime, &u.CreateTime)
|
rows.Scan(&u.ID, &u.UserName, &u.Avatar, &u.UpdateTime, &u.CreateTime)
|
||||||
u.UpdateTime = u.UpdateTime.UTC()
|
u.UpdateTime = u.UpdateTime.UTC()
|
||||||
u.CreateTime = u.CreateTime.UTC()
|
u.CreateTime = u.CreateTime.UTC()
|
||||||
users = append(users, u)
|
users = append(users, u)
|
||||||
@@ -130,7 +146,7 @@ func QueryArticleList(id_list []int) (articles []Article) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
idstr := strings.Trim(strings.Replace(fmt.Sprint(id_list), " ", ",", -1), "[]")
|
idstr := strings.Trim(strings.Replace(fmt.Sprint(id_list), " ", ",", -1), "[]")
|
||||||
rows, err := connection.Query("SELECT id, title, tags, update_time, create_time FROM web_article WHERE id IN (" + idstr + ") LIMIT " + strconv.Itoa(count))
|
rows, err := connectionx.Query("SELECT id, title, tags, update_time, create_time FROM web_article WHERE id IN (" + idstr + ") LIMIT " + strconv.Itoa(count))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("获取文章列表失败", err)
|
log.Println("获取文章列表失败", err)
|
||||||
return
|
return
|
||||||
|
Reference in New Issue
Block a user