gosql

package module
v0.9.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2016 License: MIT Imports: 3 Imported by: 0

README

go-sql-kit

v0.9.3

This project is still in development :)

Overview

  • JSON格式
  • 只需通过字符串(string),即可实现条件筛选(WHERE)/结果排序(ORDER BY)/**结果分页(LIMIT)**等常用功能
  • 提供快速便捷方案,对接开发前端(JS)模块(Developing...),实现上述筛选/排序/分页功能

Usage

go get -u github.com/suboat/go-sql-kit

Documents

  • 当前规则均基于JSON格式
  1. 独立模块
    1. Query(条件筛选(WHERE))
    2. Order(结果排序(ORDER BY))
    3. Limit(结果分页(LIMIT))
    4. Rule
  2. 组合模块
    1. Protocol(组合格式)
    2. Demo
Query

./query.go

关键字
QueryKeyAnd        string = "%and"  // 与
QueryKeyOr                = "%or"   // 或

QueryKeyEq         string = "%eq"   // 等于
QueryKeyNe                = "%ne"   // 不等于
QueryKeyLt                = "%lt"   // 小于
QueryKeyLte               = "%lte"  // 小于等于
QueryKeyGt                = "%gt"   // 大于
QueryKeyGte               = "%gte"  // 大于等于
QueryKeyLike              = "%like" // 模糊搜索
QueryKeyIn                = "%in"   // 在...之中
QueryKeyBetween           = "%bt"   // 在...之间
QueryKeyNotBetween        = "%nbt"  // 不在...之间
  • 关键字"%and"和"%or"需继续包含关键字
实例说明
  • 实例1: key1 == "A12"
{"%and":{"%eq":{"key1":"A12"}}}
// 或者简化为
{"%eq":{"key1":"A12"}}
  • 实例2: (key1 == "A12" && key2 == "B23") && (key3 != "C34" && key4 != "D45")
{"%and":{"%eq":{"key1":"A12","key2":"B23"},"%ne":{"key3":"C34","key4":"D45"}}}
  • 实例3: (key1 < 12 && key2 < 23) || (key3 >= 34 && key4 >= 45)
{"%or":{"%lt":{"key1":12,"key2":23},"%gte":{"key3":34,"key4":45}}}
  • 实例4: key1 == "11" && key2 = "12" && (key3 >= 31 && key3 <= 32) && !(key4 <= 43 && key4 >= 44) && (key5 == 51 || key5 == 52)
{"%and":{"%eq":{"key1":"11","key2":12},"%bt":{"key3":[31,32]},"%nbt":{"key4":[43,44]},"%in":{"key5":[51,52]}}}
Order

./order.go

关键字
OrderKey string = "%o"

OrderKeyASC  string = "+" // 正序
OrderKeyDESC        = "-" // 反序
  • 正序缺省可以不加关键字
  • 正序: 例如对字段"key1"正向排序,可写为"+key1",也可以"key1"
  • 反序: 例如对字段"key4"反向排序,需写为"-key4"
实例说明
  • 实例1: 正序("key1", "key2", "key3"),反序("key4", "key5")
{"%o":["key1", "+key2", "+key3", "-key4", "-key5"]}
Limit

./limit.go

关键字
LimitKeyLimit string = "%l" // 数量限制
LimitKeySkip         = "%s" // 位移数量
LimitKeyPage         = "%p" // 页数,从0开始
  • 若使用Limit,其中"%l"不允许缺省
  • 值必须为整型数字
  • (TODO: 后续计划可能允许缺省"%l",允许值为字符串)
实例说明
  • 实例1: 忽略最前面的13个值,返回最多5个值 (忽略最前面的3个,并返回第3页的值,每页最多5个值)
{"%l":5,"%s":3,"%p":2}
Rule

./rule.go

Protocol
Demo
  • 实例1:
[
    {
        "%and":{
            "%eq":{
                "key1":"A12",
                "key2":"B23"
            },
            "%ne":{
                "key3":"C34",
                "key4":"D45"
            }
        }
    },
    {
        "%o":[
            "+key1",
            "-key2"
        ]
    },
    {
        "%l":5,
        "%s":12,
        "%p":1
    }
]

TODO

  • 开发前端(JS)模块
  • 不断完善文档说明

Documentation

Index

Constants

View Source
const (
	LimitKeyLimit string = "%l" // 数量限制
	LimitKeySkip         = "%s" // 位移数量
	LimitKeyPage         = "%p" // 页数,从0开始
)
View Source
const (
	OrderKeyASC  string = "+" // 正序
	OrderKeyDESC        = "-" // 反序
)
View Source
const (
	QueryKeyAnd string = "%and" // AND 与
	QueryKeyOr         = "%or"  // OR  或
)
View Source
const (
	QueryKeyEq         string = "%eq"   // 等于
	QueryKeyNe                = "%ne"   // 不等于
	QueryKeyLt                = "%lt"   // 小于
	QueryKeyLte               = "%lte"  // 小于等于
	QueryKeyGt                = "%gt"   // 大于
	QueryKeyGte               = "%gte"  // 大于等于
	QueryKeyLike              = "%like" // 模糊搜索
	QueryKeyIn                = "%in"   // 在...之中
	QueryKeyBetween           = "%bt"   // 在...之间
	QueryKeyNotBetween        = "%nbt"  // 不在...之间
)
View Source
const (
	OrderKey string = "%o"
)

Variables

View Source
var (
	ErrTypeNil    error = errors.New("gosql: interface{} must not be nil")
	ErrTypeMap          = errors.New("gosql: interface{} must be type of map[string]interface{}")
	ErrTypeString       = errors.New("gosql: interface{} must be type of string")
	ErrTypeInt          = errors.New("gosql: interface{} must be type of int")
	ErrTypeValue        = errors.New("gosql: invalid value")
)

Functions

func IsLimitKey

func IsLimitKey(str string) bool

func IsOrderKey

func IsOrderKey(str string) bool

func IsQueryAnonymousKey

func IsQueryAnonymousKey(str string) bool

func IsQueryKey

func IsQueryKey(str string) bool

Types

type ILimit

type ILimit interface {
	IsLimited() bool
}

type IOrder

type IOrder interface {
	IsASC() bool
	IsDESC() bool
}

type IParser

type IParser interface {
	ParseJSONString(string) error
	Parse(map[string]interface{}) error
}

type IQuery

type IQuery interface {
	IsAnonymous() bool
}

type IRuleLimit

type IRuleLimit interface {
	SetMaxLimit(int) IRuleLimit
	GetLimit(int) int
}

type IRuleMapping

type IRuleMapping interface {
	Allow(...string) IRuleMapping
	Disallow(...string) IRuleMapping
	IsAllowed(string) bool
	SetMapping(string, string) IRuleMapping
	GetMapping(string) string
}

type LimitRoot

type LimitRoot struct {
	Values []ILimit
}

func (*LimitRoot) IsLimited

func (l *LimitRoot) IsLimited() bool

func (*LimitRoot) Parse

func (l *LimitRoot) Parse(m map[string]interface{}) error

func (*LimitRoot) ParseJSONString

func (l *LimitRoot) ParseJSONString(str string) error

type LimitValue

type LimitValue struct {
	Key   string
	Value int
}

func (*LimitValue) IsLimited

func (l *LimitValue) IsLimited() bool

func (*LimitValue) Parse

func (l *LimitValue) Parse(obj interface{}) error

type LimitValues

type LimitValues struct {
	Limit int
	Skip  int
	Page  int
}

func (*LimitValues) GetSkip

func (l *LimitValues) GetSkip() int

func (*LimitValues) GetValues

func (l *LimitValues) GetValues() (int, int, int)

func (*LimitValues) IsLimited

func (l *LimitValues) IsLimited() bool

type OrderRoot

type OrderRoot struct {
	Values []IOrder
}

func (*OrderRoot) IsASC

func (o *OrderRoot) IsASC() bool

func (*OrderRoot) IsDESC

func (o *OrderRoot) IsDESC() bool

func (*OrderRoot) Parse

func (o *OrderRoot) Parse(m map[string]interface{}) error

func (*OrderRoot) ParseJSONString

func (o *OrderRoot) ParseJSONString(str string) error

type OrderValue

type OrderValue struct {
	Key   string
	Field string
}

func (*OrderValue) IsASC

func (o *OrderValue) IsASC() bool

func (*OrderValue) IsDESC

func (o *OrderValue) IsDESC() bool

func (*OrderValue) Parse

func (o *OrderValue) Parse(obj interface{}) error

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

func (*Parser) Add

func (p *Parser) Add(v IParser)

func (*Parser) Parse

func (p *Parser) Parse(m map[string]interface{}) error

func (*Parser) ParseJSONString

func (p *Parser) ParseJSONString(str string) error

type QueryElem

type QueryElem struct {
	RelKey string
	Key    string
	Values []IQuery
	// contains filtered or unexported fields
}

func (*QueryElem) IsAnonymous

func (q *QueryElem) IsAnonymous() bool

func (*QueryElem) Parse

func (q *QueryElem) Parse(obj interface{}) error

type QueryRoot

type QueryRoot struct {
	Values []IQuery
}

func (*QueryRoot) IsAnonymous

func (q *QueryRoot) IsAnonymous() bool

func (*QueryRoot) Parse

func (q *QueryRoot) Parse(m map[string]interface{}) error

func (*QueryRoot) ParseJSONString

func (o *QueryRoot) ParseJSONString(str string) error

type QueryValue

type QueryValue struct {
	Key   string
	Field string
	Value interface{}
}

func (*QueryValue) IsAnonymous

func (q *QueryValue) IsAnonymous() bool

func (*QueryValue) Parse

func (q *QueryValue) Parse(obj interface{}) error

type RuleLimit

type RuleLimit struct {
	// contains filtered or unexported fields
}

func (*RuleLimit) GetLimit

func (l *RuleLimit) GetLimit(lmt int) int

func (*RuleLimit) SetMaxLimit

func (l *RuleLimit) SetMaxLimit(lmt int) IRuleLimit

type RuleMapping

type RuleMapping struct {
	// contains filtered or unexported fields
}

func (*RuleMapping) Allow

func (r *RuleMapping) Allow(keys ...string) IRuleMapping

func (*RuleMapping) Disallow

func (r *RuleMapping) Disallow(keys ...string) IRuleMapping

func (*RuleMapping) GetMapping

func (r *RuleMapping) GetMapping(value string) string

func (*RuleMapping) IsAllowed

func (r *RuleMapping) IsAllowed(key string) bool

func (*RuleMapping) SetMapping

func (r *RuleMapping) SetMapping(value, mapping string) IRuleMapping

Directories

Path Synopsis
sql

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL