db query
This commit is contained in:
parent
57ace3e333
commit
9d1d56c08c
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
.idea
|
||||
wp-go.iml
|
||||
wp-go.iml
|
||||
config.yaml
|
17
config.example.yaml
Normal file
17
config.example.yaml
Normal file
@ -0,0 +1,17 @@
|
||||
mysql:
|
||||
dsn:
|
||||
host: localhost
|
||||
port: 3306
|
||||
db: wordpress
|
||||
user: root
|
||||
password: root
|
||||
charset: utf8mb4
|
||||
pool:
|
||||
# 最长空闲时间
|
||||
connMaxIdleTime: 60
|
||||
# 最大连接数
|
||||
maxOpenConn: 100
|
||||
# 最大空闲连接数
|
||||
maxIdleConn: 10
|
||||
# 连接的生命时长
|
||||
connMaxLifetime: 236
|
31
db/db.go
Normal file
31
db/db.go
Normal file
@ -0,0 +1,31 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github/fthvgb1/wp-go/vars"
|
||||
)
|
||||
|
||||
var Db *sqlx.DB
|
||||
|
||||
func InitDb() error {
|
||||
dsn := vars.Conf.Mysql.Dsn.GetDsn()
|
||||
var err error
|
||||
Db, err = sqlx.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if vars.Conf.Mysql.Pool.ConnMaxIdleTime != 0 {
|
||||
Db.SetConnMaxIdleTime(vars.Conf.Mysql.Pool.ConnMaxLifetime)
|
||||
}
|
||||
if vars.Conf.Mysql.Pool.MaxIdleConn != 0 {
|
||||
Db.SetMaxIdleConns(vars.Conf.Mysql.Pool.MaxIdleConn)
|
||||
}
|
||||
if vars.Conf.Mysql.Pool.MaxOpenConn != 0 {
|
||||
Db.SetMaxOpenConns(vars.Conf.Mysql.Pool.MaxOpenConn)
|
||||
}
|
||||
if vars.Conf.Mysql.Pool.ConnMaxLifetime != 0 {
|
||||
Db.SetConnMaxLifetime(vars.Conf.Mysql.Pool.ConnMaxLifetime)
|
||||
}
|
||||
return err
|
||||
}
|
12
go.mod
12
go.mod
@ -1,3 +1,15 @@
|
||||
module github/fthvgb1/wp-go
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/go-sql-driver/mysql v1.6.0
|
||||
github.com/jmoiron/sqlx v1.3.5
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/kr/pretty v0.3.0 // indirect
|
||||
github.com/rogpeppe/go-internal v1.8.0 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
)
|
||||
|
24
main.go
24
main.go
@ -1,7 +1,27 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"github/fthvgb1/wp-go/db"
|
||||
"github/fthvgb1/wp-go/models"
|
||||
"github/fthvgb1/wp-go/vars"
|
||||
)
|
||||
|
||||
func init() {
|
||||
err := vars.InitDbConfig()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = db.InitDb()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, World!")
|
||||
T, err := models.WpPostsM.FindOneById(3)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Println(T)
|
||||
}
|
||||
|
95
models/model.go
Normal file
95
models/model.go
Normal file
@ -0,0 +1,95 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github/fthvgb1/wp-go/db"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type mod interface {
|
||||
PrimaryKey() string
|
||||
Table() string
|
||||
}
|
||||
|
||||
type model[T mod] struct {
|
||||
}
|
||||
|
||||
type SimpleWhere [][]string
|
||||
|
||||
func (w SimpleWhere) parseWhere() (string, []interface{}) {
|
||||
var s strings.Builder
|
||||
args := make([]interface{}, 0, len(w))
|
||||
for _, ss := range w {
|
||||
if len(ss) == 2 {
|
||||
s.WriteString("`")
|
||||
s.WriteString(ss[0])
|
||||
s.WriteString("`=? and ")
|
||||
args = append(args, ss[1])
|
||||
}
|
||||
if len(ss) == 3 {
|
||||
s.WriteString("`")
|
||||
s.WriteString(ss[0])
|
||||
s.WriteString("`")
|
||||
s.WriteString(ss[1])
|
||||
s.WriteString("? and ")
|
||||
args = append(args, ss[2])
|
||||
}
|
||||
}
|
||||
return strings.TrimRight(s.String(), "and "), args
|
||||
}
|
||||
|
||||
func (m model[T]) FindOneById(id int) (T, error) {
|
||||
var r T
|
||||
sql := fmt.Sprintf("select * from `%s` where `%s`=?", r.Table(), r.PrimaryKey())
|
||||
err := db.Db.Get(&r, sql, id)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (m model[T]) FirstOne(where SimpleWhere, fields string) (T, error) {
|
||||
var r T
|
||||
w, args := where.parseWhere()
|
||||
tp := "select %s from %s where %s"
|
||||
sql := fmt.Sprintf(tp, fields, r.Table(), w)
|
||||
err := db.Db.Get(&r, sql, args...)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (m model[T]) FindMany(where SimpleWhere, fields string) ([]T, error) {
|
||||
var r []T
|
||||
var rr T
|
||||
w, args := where.parseWhere()
|
||||
tp := "select %s from %s where %s"
|
||||
sql := fmt.Sprintf(tp, fields, rr.Table(), w)
|
||||
err := db.Db.Select(&r, sql, args...)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (m model[T]) Get(sql string, params ...interface{}) (T, error) {
|
||||
var r T
|
||||
sql = strings.Replace(sql, "%table%", r.Table(), -1)
|
||||
err := db.Db.Get(&r, sql, params...)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (m model[T]) Select(sql string, params ...interface{}) ([]T, error) {
|
||||
var r []T
|
||||
var rr T
|
||||
sql = strings.Replace(sql, "%table%", rr.Table(), -1)
|
||||
err := db.Db.Select(&r, sql, params...)
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
return r, nil
|
||||
}
|
32
models/wp_comments.go
Normal file
32
models/wp_comments.go
Normal file
@ -0,0 +1,32 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
var WpCommentsM = WpComments{}
|
||||
|
||||
type WpComments struct {
|
||||
model[WpComments]
|
||||
CommentId uint64 `gorm:"column:comment_ID" db:"comment_ID" json:"comment_ID" form:"comment_ID"`
|
||||
CommentPostId uint64 `gorm:"column:comment_post_ID" db:"comment_post_ID" json:"comment_post_ID" form:"comment_post_ID"`
|
||||
CommentAuthor string `gorm:"column:comment_author" db:"comment_author" json:"comment_author" form:"comment_author"`
|
||||
CommentAuthorEmail string `gorm:"column:comment_author_email" db:"comment_author_email" json:"comment_author_email" form:"comment_author_email"`
|
||||
CommentAuthorUrl string `gorm:"column:comment_author_url" db:"comment_author_url" json:"comment_author_url" form:"comment_author_url"`
|
||||
CommentAuthorIp string `gorm:"column:comment_author_IP" db:"comment_author_IP" json:"comment_author_IP" form:"comment_author_IP"`
|
||||
CommentDate time.Time `gorm:"column:comment_date" db:"comment_date" json:"comment_date" form:"comment_date"`
|
||||
CommentDateGmt time.Time `gorm:"column:comment_date_gmt" db:"comment_date_gmt" json:"comment_date_gmt" form:"comment_date_gmt"`
|
||||
CommentContent string `gorm:"column:comment_content" db:"comment_content" json:"comment_content" form:"comment_content"`
|
||||
CommentKarma int `gorm:"column:comment_karma" db:"comment_karma" json:"comment_karma" form:"comment_karma"`
|
||||
CommentApproved string `gorm:"column:comment_approved" db:"comment_approved" json:"comment_approved" form:"comment_approved"`
|
||||
CommentAgent string `gorm:"column:comment_agent" db:"comment_agent" json:"comment_agent" form:"comment_agent"`
|
||||
CommentType string `gorm:"column:comment_type" db:"comment_type" json:"comment_type" form:"comment_type"`
|
||||
CommentParent uint64 `gorm:"column:comment_parent" db:"comment_parent" json:"comment_parent" form:"comment_parent"`
|
||||
UserId uint64 `gorm:"column:user_id" db:"user_id" json:"user_id" form:"user_id"`
|
||||
}
|
||||
|
||||
func (w WpComments) PrimaryKey() string {
|
||||
return "comment_ID"
|
||||
}
|
||||
|
||||
func (w WpComments) Table() string {
|
||||
return "wp_comments"
|
||||
}
|
19
models/wp_options.go
Normal file
19
models/wp_options.go
Normal file
@ -0,0 +1,19 @@
|
||||
package models
|
||||
|
||||
var WpOptionsM = WpOptions{}
|
||||
|
||||
type WpOptions struct {
|
||||
model[WpOptions]
|
||||
OptionId uint64 `gorm:"column:option_id" db:"option_id" json:"option_id" form:"option_id"`
|
||||
OptionName string `gorm:"column:option_name" db:"option_name" json:"option_name" form:"option_name"`
|
||||
OptionValue string `gorm:"column:option_value" db:"option_value" json:"option_value" form:"option_value"`
|
||||
Autoload string `gorm:"column:autoload" db:"autoload" json:"autoload" form:"autoload"`
|
||||
}
|
||||
|
||||
func (w WpOptions) PrimaryKey() string {
|
||||
return "option_id"
|
||||
}
|
||||
|
||||
func (w WpOptions) Table() string {
|
||||
return "wp_options"
|
||||
}
|
40
models/wp_posts.go
Normal file
40
models/wp_posts.go
Normal file
@ -0,0 +1,40 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
var WpPostsM = WpPosts{}
|
||||
|
||||
type WpPosts struct {
|
||||
model[WpPosts]
|
||||
Id uint64 `gorm:"column:ID" db:"ID" json:"ID" form:"ID"`
|
||||
PostAuthor uint64 `gorm:"column:post_author" db:"post_author" json:"post_author" form:"post_author"`
|
||||
PostDate time.Time `gorm:"column:post_date" db:"post_date" json:"post_date" form:"post_date"`
|
||||
PostDateGmt time.Time `gorm:"column:post_date_gmt" db:"post_date_gmt" json:"post_date_gmt" form:"post_date_gmt"`
|
||||
PostContent string `gorm:"column:post_content" db:"post_content" json:"post_content" form:"post_content"`
|
||||
PostTitle string `gorm:"column:post_title" db:"post_title" json:"post_title" form:"post_title"`
|
||||
PostExcerpt string `gorm:"column:post_excerpt" db:"post_excerpt" json:"post_excerpt" form:"post_excerpt"`
|
||||
PostStatus string `gorm:"column:post_status" db:"post_status" json:"post_status" form:"post_status"`
|
||||
CommentStatus string `gorm:"column:comment_status" db:"comment_status" json:"comment_status" form:"comment_status"`
|
||||
PingStatus string `gorm:"column:ping_status" db:"ping_status" json:"ping_status" form:"ping_status"`
|
||||
PostPassword string `gorm:"column:post_password" db:"post_password" json:"post_password" form:"post_password"`
|
||||
PostName string `gorm:"column:post_name" db:"post_name" json:"post_name" form:"post_name"`
|
||||
ToPing string `gorm:"column:to_ping" db:"to_ping" json:"to_ping" form:"to_ping"`
|
||||
Pinged string `gorm:"column:pinged" db:"pinged" json:"pinged" form:"pinged"`
|
||||
PostModified time.Time `gorm:"column:post_modified" db:"post_modified" json:"post_modified" form:"post_modified"`
|
||||
PostModifiedGmt time.Time `gorm:"column:post_modified_gmt" db:"post_modified_gmt" json:"post_modified_gmt" form:"post_modified_gmt"`
|
||||
PostContentFiltered string `gorm:"column:post_content_filtered" db:"post_content_filtered" json:"post_content_filtered" form:"post_content_filtered"`
|
||||
PostParent uint64 `gorm:"column:post_parent" db:"post_parent" json:"post_parent" form:"post_parent"`
|
||||
Guid string `gorm:"column:guid" db:"guid" json:"guid" form:"guid"`
|
||||
MenuOrder int `gorm:"column:menu_order" db:"menu_order" json:"menu_order" form:"menu_order"`
|
||||
PostType string `gorm:"column:post_type" db:"post_type" json:"post_type" form:"post_type"`
|
||||
PostMimeType string `gorm:"column:post_mime_type" db:"post_mime_type" json:"post_mime_type" form:"post_mime_type"`
|
||||
CommentCount int64 `gorm:"column:comment_count" db:"comment_count" json:"comment_count" form:"comment_count"`
|
||||
}
|
||||
|
||||
func (w WpPosts) PrimaryKey() string {
|
||||
return "ID"
|
||||
}
|
||||
|
||||
func (w WpPosts) Table() string {
|
||||
return "wp_posts"
|
||||
}
|
21
models/wp_term_taxonomy.go
Normal file
21
models/wp_term_taxonomy.go
Normal file
@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
var WpTermTaxonomyM = WpTermTaxonomy{}
|
||||
|
||||
type WpTermTaxonomy struct {
|
||||
model[WpTermTaxonomy]
|
||||
TermTaxonomyId uint64 `gorm:"column:term_taxonomy_id" db:"term_taxonomy_id" json:"term_taxonomy_id" form:"term_taxonomy_id"`
|
||||
TermId uint64 `gorm:"column:term_id" db:"term_id" json:"term_id" form:"term_id"`
|
||||
Taxonomy string `gorm:"column:taxonomy" db:"taxonomy" json:"taxonomy" form:"taxonomy"`
|
||||
Description string `gorm:"column:description" db:"description" json:"description" form:"description"`
|
||||
Parent uint64 `gorm:"column:parent" db:"parent" json:"parent" form:"parent"`
|
||||
Count int64 `gorm:"column:count" db:"count" json:"count" form:"count"`
|
||||
}
|
||||
|
||||
func (w WpTermTaxonomy) PrimaryKey() string {
|
||||
return "term_taxonomy_id"
|
||||
}
|
||||
|
||||
func (w WpTermTaxonomy) Table() string {
|
||||
return "wp_term_taxonomy"
|
||||
}
|
18
models/wp_terms.go
Normal file
18
models/wp_terms.go
Normal file
@ -0,0 +1,18 @@
|
||||
package models
|
||||
|
||||
var WpTermsM = WpTerms{}
|
||||
|
||||
type WpTerms struct {
|
||||
model[WpTerms]
|
||||
TermId uint64 `gorm:"column:term_id" db:"term_id" json:"term_id" form:"term_id"`
|
||||
Name string `gorm:"column:name" db:"name" json:"name" form:"name"`
|
||||
Slug string `gorm:"column:slug" db:"slug" json:"slug" form:"slug"`
|
||||
TermGroup int64 `gorm:"column:term_group" db:"term_group" json:"term_group" form:"term_group"`
|
||||
}
|
||||
|
||||
func (t WpTerms) PrimaryKey() string {
|
||||
return "term_id"
|
||||
}
|
||||
func (t WpTerms) Table() string {
|
||||
return "wp_terms"
|
||||
}
|
55
vars/dbconfig.go
Normal file
55
vars/dbconfig.go
Normal file
@ -0,0 +1,55 @@
|
||||
package vars
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Conf Config
|
||||
|
||||
type Config struct {
|
||||
Mysql Mysql `yaml:"mysql"`
|
||||
}
|
||||
|
||||
type Mysql struct {
|
||||
Dsn Dsn `yaml:"dsn"`
|
||||
Pool Pool `yaml:"pool"`
|
||||
}
|
||||
|
||||
func InitDbConfig() error {
|
||||
file, err := ioutil.ReadFile("config.yaml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = yaml.Unmarshal(file, &Conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Dsn struct {
|
||||
Host string `yaml:"host"`
|
||||
Port string `yaml:"port"`
|
||||
Db string `yaml:"db"`
|
||||
User string `yaml:"user"`
|
||||
Password string `yaml:"password"`
|
||||
Charset string `yaml:"charset"`
|
||||
}
|
||||
|
||||
func (m *Dsn) GetDsn() string {
|
||||
if m.Charset == "" {
|
||||
m.Charset = "utf8"
|
||||
}
|
||||
t := "%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local"
|
||||
return fmt.Sprintf(t, m.User, m.Password, m.Host, m.Port, m.Db, m.Charset)
|
||||
}
|
||||
|
||||
type Pool struct {
|
||||
ConnMaxIdleTime time.Duration `yaml:"connMaxIdleTime"`
|
||||
MaxOpenConn int `yaml:"maxOpenConn"`
|
||||
MaxIdleConn int `yaml:"maxIdleConn"`
|
||||
ConnMaxLifetime time.Duration `yaml:"connMaxLifetime"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user