sqlxb

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

README

sqlxb

workflow build Go Report Card

a tool of sql golang builder, build sql for sql.DB, sqlx, or build condition sql for some orm framework

API

Builder: build sql like: SELECT * FROM t_foo WHERE name Like "%xx%" ORDER BY ID DESC
BuilderX: build sql like: SELECT DISTINCT(f.id) AS `f.id` FROM t_foo f INNER JOIN t_bar b ON ....

c := Cat{}
builder := NewBuilder(&c)
builder.Gte("id", 10000)
builder.And(SubCondition().Gte("price", catRo.Price).OR().Eq("is_sold", catRo.IsSold))
....

builderX := NewBuilderX(nil,"") //or, NewBuilderX(&cat,"c")
builderX.ResultKeys( "c.color","p.id","COUNT(DISTINCT c.id) AS `c.id_count`")
....

Builder DEMO


import (
    . "github.com/x-ream/sqlxb"
)

type Cat struct {
	Id       uint64    `db:"id"`
	Name     string    `db:"name"`
	Age      uint      `db:"age"`
	Color    string    `db:"color"`
	Weight   float64   `db:"weight"`
	IsSold   *bool     `db:"is_sold"`
	Price    *float64  `db:"price"`
	CreateAt time.Time `db:"create_at"`
}

func (*Cat) TableName() string {
	return "t_cat"
}

// IsSold, Price, fields can be zero, must be pointer, like Java Boolean....
// sqlxb has func: Bool(true), Int(v) ....
// sqlxb no relect, not support omitempty, should rewrite ro, dto
type CatRo struct {
	Name   string   `json:"name, string"`
	IsSold *bool    `json:"isSold, *bool"`
	Price  *float64 `json:"price, *float64"`
	Age    uint     `json:"age", unit`
}

func main() {
	cat := Cat{
		Id:       100002,
		Name:     "Tuanzi",
		Age:      1,
		Color:    "B",
		Weight:   8.5,
		IsSold:   Bool(true),
		Price:    Float64(10000.00),
		CreateAt: time.Now(),
	}
    // INSERT .....

    // PREPARE TO QUERY
	catRo := CatRo{
		Name:	"Tu",
		IsSold: nil,
		Price:  Float64(5000.00),
		Age:    1,
	}

	preCondition := func() bool {
		if cat.Color == "W" {
			return true
		} else if cat.Weight <= 3 {
			return false
		} else {
			return true
		}
	}

	c := Cat{}
	var builder = NewBuilder(&c)
	builder.LikeRight("name",catRo.Name)
	builder.And(SubCondition().Gte("price", catRo.Price).OR().Gte("age", catRo.Age).OR().Eq("is_sold", catRo.IsSold))
	builder.Bool(preCondition, func(cb *ConditionBuilder) {
		cb.Or(SubCondition().Lt("price", 5000))
	})
	builder.Sort("id", ASC)
	builder.Paged().Rows(10).Last(100)
	vs, dataSql, countSql, _ := builder.Build().Sql()
    // ....

    //dataSql: SELECT * FROM t_cat WHERE id > ? AND name LIKE ? AND (price >= ? OR age >= ?) OR (price < ?)
    //ORDER BY id ASC LIMIT 10

    //countSql: SELECT COUNT(*) FROM t_cat WHERE name LIKE ? AND (price >= ? OR age >= ?) OR (price < ?)
    
    //sqlx: 	err = Db.Select(&catList, dataSql,vs...)
	_, conditionSql := builder.Build().SqlOfCondition()
    
    //conditionSql: name LIKE ? AND (price >= ? OR age >= ?) OR (price < ?)

}

Documentation

Overview

Copyright 2020 io.xream.sqlxb

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	X        = ""
	AND      = "AND"
	OR       = "OR"
	AND_SUB  = AND
	OR_SUB   = OR
	EQ       = "="
	NE       = "<>"
	GT       = ">"
	LT       = "<"
	GTE      = ">="
	LTE      = "<="
	LIKE     = "LIKE"
	NOT_LIKE = "NOT LIKE"
	IN       = "IN"
	NIN      = "NOT IN"
	IS_NULL  = "IS NULL"
	NON_NULL = "IS NOT NULL"
)

Variables

This section is empty.

Functions

func ASC

func ASC() string

func Bool

func Bool(b bool) *bool

func Byte

func Byte(b byte) *byte

func DESC

func DESC() string

func Eq

func Eq() string

func Float32

func Float32(f float32) *float32

func Float64

func Float64(f float64) *float64

func Gt

func Gt() string

func Gte

func Gte() string

func INNER_JOIN

func INNER_JOIN() string

func Int

func Int(v int) *int

func Int8

func Int8(v int8) *int8

func Int16

func Int16(v int16) *int16

func Int32

func Int32(v int32) *int32

func Int64

func Int64(v int64) *int64

func IsNull

func IsNull() string

func LEFT_JOIN

func LEFT_JOIN() string

func Like

func Like() string

func LikeRight

func LikeRight() string

func Lt

func Lt() string

func Lte

func Lte() string

func N2s

func N2s(p interface{}) string

func Ne

func Ne() string

func NilOrNumber

func NilOrNumber(p interface{}) (bool, interface{})

func NonJoin

func NonJoin() string

func NonNull

func NonNull() string

func NotLike

func NotLike() string

func Np2s

func Np2s(p interface{}) (string, bool)

func RIGHT_JOIN

func RIGHT_JOIN() string

func Uint

func Uint(v uint) *uint

func Uint64

func Uint64(v uint64) *uint64

Types

type Bb

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

type BoolFunc

type BoolFunc func() bool

type Builder

type Builder struct {
	ConditionBuilder
	// contains filtered or unexported fields
}

TO build sql, like: SELECT * FROM .... Can add L2Cache

@author Sim

func NewBuilder

func NewBuilder(poOrNil Po) *Builder

func (*Builder) Build

func (builder *Builder) Build() *Built

func (*Builder) Paged

func (builder *Builder) Paged() *PageBuilder

func (*Builder) Sort

func (builder *Builder) Sort(orderBy string, direction Direction) *Builder

type BuilderX

type BuilderX struct {
	Builder
	// contains filtered or unexported fields
}

func NewBuilderX

func NewBuilderX(po Po, alia string) *BuilderX

func Sub

func Sub() *BuilderX

To build sql, like: SELECT DISTINCT f.id FROM foo f INNER JOIN (SELECT foo_id FROM bar) b ON b.foo_id = f.id Sql for MySQL, Clickhouse....

@author Sim

func (*BuilderX) Build

func (builder *BuilderX) Build() *Built

func (*BuilderX) GroupBy

func (x *BuilderX) GroupBy(groupBy string) *BuilderX

func (*BuilderX) Having

func (x *BuilderX) Having(op Op, k string, v interface{}) *BuilderX

func (*BuilderX) ResultKey

func (x *BuilderX) ResultKey(resultKey string) *BuilderX

func (*BuilderX) ResultKeys

func (x *BuilderX) ResultKeys(resultKeys ...string) *BuilderX

func (*BuilderX) Source

func (x *BuilderX) Source(po Po) *BuilderX

func (*BuilderX) SourceBuilder

func (x *BuilderX) SourceBuilder() *SourceBuilder

func (*BuilderX) WithoutOptimization

func (builder *BuilderX) WithoutOptimization() *BuilderX

type Built

type Built struct {
	ResultKeys []string
	ConditionX []Bb
	Sorts      []Sort
	Havings    []Bb
	GroupBys   []string

	Sbs []*SourceBuilder
	Svs []interface{}

	PageCondition *PageCondition

	Po Po
}

func (*Built) Sql

func (built *Built) Sql() ([]interface{}, string, string, map[string]string)

func (*Built) SqlOfCondition

func (built *Built) SqlOfCondition() ([]interface{}, string)

type ConditionBuilder

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

func SubCondition

func SubCondition() *ConditionBuilder

func (*ConditionBuilder) And

func (builder *ConditionBuilder) And(subCondition *ConditionBuilder) *ConditionBuilder

func (*ConditionBuilder) Bool

func (builder *ConditionBuilder) Bool(preCondition BoolFunc, then func(cb *ConditionBuilder)) *ConditionBuilder

func (*ConditionBuilder) Build

func (builder *ConditionBuilder) Build() *Built

func (*ConditionBuilder) Eq

func (builder *ConditionBuilder) Eq(k string, v interface{}) *ConditionBuilder

func (*ConditionBuilder) Gt

func (builder *ConditionBuilder) Gt(k string, v interface{}) *ConditionBuilder

func (*ConditionBuilder) Gte

func (builder *ConditionBuilder) Gte(k string, v interface{}) *ConditionBuilder

func (*ConditionBuilder) In

func (builder *ConditionBuilder) In(k string, vs ...interface{}) *ConditionBuilder

func (*ConditionBuilder) IsNull

func (builder *ConditionBuilder) IsNull(key string) *ConditionBuilder

func (*ConditionBuilder) Like

func (builder *ConditionBuilder) Like(k string, v string) *ConditionBuilder

func (*ConditionBuilder) LikeRight

func (builder *ConditionBuilder) LikeRight(k string, v string) *ConditionBuilder

func (*ConditionBuilder) Lt

func (builder *ConditionBuilder) Lt(k string, v interface{}) *ConditionBuilder

func (*ConditionBuilder) Lte

func (builder *ConditionBuilder) Lte(k string, v interface{}) *ConditionBuilder

func (*ConditionBuilder) Ne

func (builder *ConditionBuilder) Ne(k string, v interface{}) *ConditionBuilder

func (*ConditionBuilder) Nin

func (builder *ConditionBuilder) Nin(k string, vs ...interface{}) *ConditionBuilder

func (*ConditionBuilder) NonNull

func (builder *ConditionBuilder) NonNull(key string) *ConditionBuilder

func (*ConditionBuilder) NotLike

func (builder *ConditionBuilder) NotLike(k string, v string) *ConditionBuilder

func (*ConditionBuilder) OR

func (builder *ConditionBuilder) OR() *ConditionBuilder

func (*ConditionBuilder) Or

func (*ConditionBuilder) X

func (builder *ConditionBuilder) X(k string, vs ...interface{}) *ConditionBuilder

type Direction

type Direction func() string

type JOIN

type JOIN func() string

type Join

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

type LongId

type LongId interface {
	GetId() uint64
}

type On

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

func ON

func ON(key string, targetOrAlia string, targetKey string) *On

type Op

type Op func() string

type PageBuilder

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

func (*PageBuilder) IgnoreTotalRows

func (pb *PageBuilder) IgnoreTotalRows(ignored bool) *PageBuilder

func (*PageBuilder) Last

func (pb *PageBuilder) Last(last uint64) *PageBuilder

*

  • ASC: orderBy > last | DESC: orderBy < last
  • LIMIT rows

func (*PageBuilder) Page

func (pb *PageBuilder) Page(page uint) *PageBuilder

func (*PageBuilder) Paged

func (pb *PageBuilder) Paged() *PageBuilder

func (*PageBuilder) Rows

func (pb *PageBuilder) Rows(rows uint) *PageBuilder

type PageCondition

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

type Po

type Po interface {
	TableName() string
}

type Sort

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

type SourceBuilder

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

func (*SourceBuilder) Alia

func (sb *SourceBuilder) Alia(alia string) *SourceBuilder

func (*SourceBuilder) JoinOn

func (sb *SourceBuilder) JoinOn(join JOIN, on *On) *SourceBuilder

func (*SourceBuilder) JoinUsing

func (sb *SourceBuilder) JoinUsing(join JOIN, key string) *SourceBuilder

func (*SourceBuilder) More

func (sb *SourceBuilder) More(cb *ConditionBuilder)

func (*SourceBuilder) Source

func (sb *SourceBuilder) Source(po Po) *SourceBuilder

func (*SourceBuilder) Sub

func (sb *SourceBuilder) Sub(sub *BuilderX) *SourceBuilder

type StringId

type StringId interface {
	GetId() string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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