Skip to content

Commit

Permalink
Merge pull request #6 from lifegit/v2
Browse files Browse the repository at this point in the history
提交 fire
  • Loading branch information
lifegit authored Aug 28, 2021
2 parents 703427a + cad7972 commit 0e4aede
Show file tree
Hide file tree
Showing 31 changed files with 131 additions and 2,349 deletions.
14 changes: 5 additions & 9 deletions aliyun/slb.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @Author: TheLife
* @Date: 2020-2-25 9:00 下午
*/
package aliyun

import (
Expand All @@ -26,7 +22,7 @@ func NewSlb(regionId, accessKeyID, accessKeySecret string) (client *AliSlbClient
return &AliSlbClient{c}, err
}

// 删除slb
// DeleteLoadBalancer 删除slb
func (c *AliSlbClient) DeleteLoadBalancer(balancerId string) (response *slb.DeleteLoadBalancerResponse, err error) {
request := slb.CreateDeleteLoadBalancerRequest()
request.Scheme = "https"
Expand All @@ -37,7 +33,7 @@ func (c *AliSlbClient) DeleteLoadBalancer(balancerId string) (response *slb.Dele
return
}

// 创建监听
// CreateLoadBalancerTCPListener 创建监听
func (c *AliSlbClient) CreateLoadBalancerTCPListener(balancerId string, listenerPort int, backendServerPort int) (response *slb.CreateLoadBalancerTCPListenerResponse, err error) {
request := slb.CreateCreateLoadBalancerTCPListenerRequest()
request.Scheme = "https"
Expand All @@ -51,7 +47,7 @@ func (c *AliSlbClient) CreateLoadBalancerTCPListener(balancerId string, listener
return
}

// 开始监听
// StartLoadBalancerListener 开始监听
func (c *AliSlbClient) StartLoadBalancerListener(balancerId string, listenerPort int) (response *slb.StartLoadBalancerListenerResponse, err error) {
request := slb.CreateStartLoadBalancerListenerRequest()
request.Scheme = "https"
Expand All @@ -63,7 +59,7 @@ func (c *AliSlbClient) StartLoadBalancerListener(balancerId string, listenerPort
return
}

// 创建slb
// CreateLoadBalancer 创建slb
func (c *AliSlbClient) CreateLoadBalancer(internetChargeType string, bandwidth int) (response *slb.CreateLoadBalancerResponse, err error) {
request := slb.CreateCreateLoadBalancerRequest()
request.Scheme = "https"
Expand All @@ -76,7 +72,7 @@ func (c *AliSlbClient) CreateLoadBalancer(internetChargeType string, bandwidth i
return
}

// 添加后端服务器
// AddBackendServers 添加后端服务器
func (c *AliSlbClient) AddBackendServers(balancerId string, serverId string) (response *slb.AddBackendServersResponse, err error) {
request := slb.CreateAddBackendServersRequest()
request.Scheme = "https"
Expand Down
6 changes: 1 addition & 5 deletions aliyun/sms.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @Author: TheLife
* @Date: 2020-2-25 9:00 下午
*/
package aliyun

import (
Expand All @@ -19,7 +15,7 @@ func NewSMS(regionId, accessKeyID, accessKeySecret string) (client *AliSmsClient
return &AliSmsClient{c}, err
}

// 发送短信
// Send 发送短信
func (c *AliSmsClient) Send(phoneNumber, signName, templateCode string, templateParam map[string]interface{}) (response *dysmsapi.SendSmsResponse, err error) {
request := dysmsapi.CreateSendSmsRequest()
request.Scheme = "https"
Expand Down
15 changes: 6 additions & 9 deletions pkg/fire/allow.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
/**
* @Author: TheLife
* @Date: 2021/5/26 下午11:21
*/
// Package fire /**
// 这是一个接收请求参数, 过滤到sql条件的漏斗工具。
// 实现开箱即用, 快速匹配筛选条件。

package fire

import (
"reflect"
)

// 这是一个接收请求参数, 过滤到sql条件的漏斗工具。
// 实现开箱即用, 快速匹配筛选条件。

type Allow struct {
// where
Where []string
Expand All @@ -24,7 +21,7 @@ type Allow struct {

type Sort map[string]interface{}

// allowSort
// AllowSort allow sort
func (a *Allow) AllowSort(sort Sort, db *Fire) *Fire {
toCamel2Case(sort)

Expand All @@ -41,7 +38,7 @@ func (a *Allow) AllowSort(sort Sort, db *Fire) *Fire {

type Params map[string]interface{}

// allowParams
// AllowParams allow params
func (a *Allow) AllowParams(params Params, db *Fire) *Fire {
// used Allow.key loop: fixed SQL order, we can put the condition of low energy consumption in the front
// not used Params loop: range map is no order, it may result in different SQL generated each time
Expand Down
8 changes: 2 additions & 6 deletions pkg/fire/allow_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @Author: TheLife
* @Date: 2021/6/24 下午4:59
*/
package fire_test

import (
Expand All @@ -26,7 +22,7 @@ func TestAllow(t *testing.T) {
},
}

user := &[]User{}

_, _ = DBDryRun.OrderByColumn("age", fire.OrderAsc).
Allow(param, fire.Allow{
Where: []string{"age"},
Expand All @@ -35,7 +31,7 @@ func TestAllow(t *testing.T) {
In: []string{"tag"},
Sorts: []string{"age"},
}).
CrudAllPage(User{}, user)
CrudAllPage(User{}, &[]User{})

assert.Equal(t, DBDryRun.Logger.(*Diary).LastSql(2), "SELECT count(*) FROM `user` WHERE `age` = 18 AND (`id` >= 1 AND `id` <= 999) AND `tag` IN ('学生','儿子','青年') AND `name` LIKE '%Mr%'")
assert.Equal(t, DBDryRun.Logger.(*Diary).LastSql(), "SELECT * FROM `user` WHERE `age` = 18 AND (`id` >= 1 AND `id` <= 999) AND `tag` IN ('学生','儿子','青年') AND `name` LIKE '%Mr%' ORDER BY `age` desc LIMIT 20")
Expand Down
13 changes: 6 additions & 7 deletions pkg/fire/crud.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @Author: TheLife
* @Date: 2021/5/27 下午5:08
*/
// Package fire /**
// 常见的curd工具集,快速助力业务开发。

package fire

import (
Expand All @@ -19,15 +18,14 @@ import (
"sync"
)

// 常见的curd工具集,快速助力业务开发。

type validateOnce struct {
*validator.Validate
once sync.Once
}

var validate validateOnce

// CrudCreate
// model support Array,Slice,Struct
// every struct support tag `gormCreate` validator.Struct()
func (d *Fire) CrudCreate(model interface{}, batchSize ...int) (err error) {
Expand Down Expand Up @@ -152,6 +150,7 @@ func (d *Fire) CrudSum(model interface{}, column string) (sum float32, err error

type M map[string]interface{}

// CrudUpdate
// updates support (M or map[string]interface{}) and struct
// support gorm.Db.Select() and gorm.Db.Omit()
// TODO: struct only update non-zero fields
Expand Down Expand Up @@ -232,7 +231,7 @@ func (d *Fire) CrudUpdate(model interface{}, updates ...interface{}) (err error)
return tx.Error
}

// Make sure that all primary keys are not zero when updating
// CrudUpdatePrimaryKey Make sure that all primary keys are not zero when updating
func (d *Fire) CrudUpdatePrimaryKey(model interface{}, updates ...interface{}) (err error) {
sch, err := schema.Parse(model, &sync.Map{}, schema.NamingStrategy{})
if err != nil {
Expand Down
11 changes: 2 additions & 9 deletions pkg/fire/crud_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
/**
* @Author: TheLife
* @Date: 2021/6/23 下午10:30
*/
package fire_test

import (
"fmt"
"github.com/lifegit/go-gulu/v2/pkg/fire"
"github.com/stretchr/testify/assert"
"testing"
Expand Down Expand Up @@ -48,12 +43,12 @@ func TestCrudOne(t *testing.T) {
}

func TestCrudAll(t *testing.T) {
_ = DBDryRun.WhereRange("age", 18, 20).CrudAll(User{}, &User{})
_ = DBDryRun.WhereRange("age", 18, 20).CrudAll(User{}, &[]User{})
assert.Equal(t, DBDryRun.Logger.(*Diary).LastSql(), "SELECT * FROM `user` WHERE `age` >= 18 AND `age` <= 20")
}

func TestCrudAllPage(t *testing.T) {
_, _ = DBDryRun.CrudAllPage(User{}, &User{}, fire.Page{
_, _ = DBDryRun.CrudAllPage(User{}, &[]User{}, fire.Page{
Current: 3,
PageSize: 5,
})
Expand Down Expand Up @@ -98,8 +93,6 @@ func TestCrudOnePreloadAll(t *testing.T) {
Company Company
}
_ = DB.CrudOnePreloadAll(TbUser{User: User{Age: 18}}, &TbUser{})
xx := DB
fmt.Println(xx)
assert.Equal(t, DB.Logger.(*Diary).LastSql(2), "SELECT * FROM `company` WHERE `company`.`id` = 1 AND `company`.`deleted_at` = 0")
assert.Equal(t, DB.Logger.(*Diary).LastSql(), "SELECT * FROM `user` WHERE `user`.`age` = 18 LIMIT 1")
}
Expand Down
22 changes: 13 additions & 9 deletions pkg/fire/fire.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/**
* @Author: TheLife
* @Date: 2021/5/26 下午5:01
*/
// Package fire /**/
// 对 gorm.DB 的补充封装,实现更爽快得使用。属于基础层服务代码。
package fire

import (
Expand All @@ -13,8 +11,6 @@ import (
"unicode"
)

// 对 gorm.DB 的补充封装,实现更爽快的使用。属于基础层服务代码。

type Fire struct {
*gorm.DB
}
Expand All @@ -31,7 +27,6 @@ func NewInstance(db *gorm.DB) *Fire {
return &Fire{DB: db}
}

// 格式化列
func FormatColumn(column string) (res string) {
list := strings.Split(column, ".")
for key, value := range list {
Expand Down Expand Up @@ -61,13 +56,16 @@ func (d *Fire) Close() (err error) {
}

// === SELECT ===

// PreloadAll
// TODO:Multiple SQL, gorm bonding data, so query conditions other than the main table are not supported
func (d *Fire) PreloadAll() *Fire {
tx := d.DB.Preload(clause.Associations)

return NewInstance(tx)
}

// PreloadJoin
// TODO:Single SQL, mysql bonding data, so the conditions of all query tables are supported. use Join you need to pay attention to performance
func (d *Fire) PreloadJoin(model interface{}) *Fire {
if reflect.TypeOf(model).Kind() != reflect.Struct {
Expand All @@ -90,7 +88,6 @@ func (d *Fire) PreloadJoin(model interface{}) *Fire {
return NewInstance(tx)
}

// allow
func (d *Fire) Allow(param Param, allow Allow) *Fire {
tx := NewInstance(d.DB)
tx = allow.AllowParams(param.Params, tx)
Expand Down Expand Up @@ -133,6 +130,7 @@ const (

// === where ===

// WhereCompare
// column CompareEqual ? # column = ?
func (d *Fire) WhereCompare(column string, value interface{}, compare ...CompareType) *Fire {
c := If(compare != nil, compare, []CompareType{CompareEqual})
Expand All @@ -141,6 +139,7 @@ func (d *Fire) WhereCompare(column string, value interface{}, compare ...Compare
return NewInstance(tx)
}

// WhereIn
// column IN(?)
// column NOT IN(?)
func (d *Fire) WhereIn(column string, value interface{}, isNot ...bool) *Fire {
Expand All @@ -150,13 +149,15 @@ func (d *Fire) WhereIn(column string, value interface{}, isNot ...bool) *Fire {
return NewInstance(tx)
}

// WhereLike
// column LIKE %?%
func (d *Fire) WhereLike(column string, value interface{}) *Fire {
tx := d.Where(fmt.Sprintf("%s LIKE ?", FormatColumn(column)), fmt.Sprintf("%%%s%%", value))

return NewInstance(tx)
}

// WhereRange
// column >= start ANd column <= end
func (d *Fire) WhereRange(column string, start interface{}, end interface{}) *Fire {
formatColumn := FormatColumn(column)
Expand All @@ -166,6 +167,7 @@ func (d *Fire) WhereRange(column string, start interface{}, end interface{}) *Fi
}

// === order ===

type OrderType string

const (
Expand All @@ -183,6 +185,7 @@ func (d *Fire) OrderByColumn(column string, order OrderType, many ...bool) *Fire
}

// === update ===

type ArithmeticType string

const (
Expand All @@ -192,7 +195,8 @@ const (
ArithmeticExcept ArithmeticType = "/"
)

//field = field ArithmeticType Number # field = field + 1
// UpdateArithmetic
// field = field ArithmeticType Number # field = field + 1
func UpdateArithmetic(column string, value interface{}, art ArithmeticType) (m map[string]interface{}) {
m = make(map[string]interface{})
m[column] = gorm.Expr(fmt.Sprintf("%s %s ?", FormatColumn(column), art), value)
Expand Down
4 changes: 0 additions & 4 deletions pkg/fire/fire_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @Author: TheLife
* @Date: 2021/5/26 下午6:09
*/
package fire_test

import (
Expand Down
9 changes: 3 additions & 6 deletions pkg/fire/hooks.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/**
* @Author: TheLife
* @Date: 2021/6/23 下午9:37
*/
// Package fire /**
// 重新定义 gorm.Model

package fire

import (
"gorm.io/plugin/soft_delete"
)

// 重新定义 gorm.Model

type TimeFieldsEditModel struct {
// 13位毫秒时间戳
CreatedAt int64 `gorm:"column:created_at;autoCreateTime:milli;type:bigint(13);unsigned;comment:创建时间" json:"createdAt"`
Expand Down
4 changes: 0 additions & 4 deletions pkg/fire/hooks_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @Author: TheLife
* @Date: 2021/6/23 下午10:46
*/
package fire_test

import (
Expand Down
Loading

0 comments on commit 0e4aede

Please sign in to comment.