sqlbuilder

package module
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: May 7, 2020 License: GPL-3.0 Imports: 5 Imported by: 2

README

sqlbuilder

sqlbuilder is a simple sql query string builder

sqlbuilder its recursive struct call, that you can easy to build sql string

GoDoc

simple example :

    b := sb.NewSQLBuilder("SQLite")
    b.Select("a", "b").
        From("tblA").
        Join("tblB").
        Where("a", "=", 1).
        Where("b", "=", "str").
        Limit(100).
        BuildSelectSQL()
    fmt.Println(b.BuildedSQL())

    b.Release()

more case can see test case

go-model

how to use sqlbuilder with go-model :

package db

import (
	"database/sql"
	"strconv"

	model "github.com/eehsiao/go-models"
	sb "github.com/eehsiao/sqlbuilder"
)

func (dao *Dao) GetExgs(params ...string) (e *[]*Exg, err error) {
	var (
		b    = sb.NewSQLBuilder("SQLite")
		rows *sql.Rows
	)
	e = &[]*Exg{}
	defer func() {
		if rows != nil {
			rows.Close()
		}
		b.Release()
		rows = nil
	}()

	b.Select(model.Inst2Fields(Exg{})...).
		From(TbExgs).
		Where("is_active", "=", 1)
	if len(params) > 0 && params[0] != "" {
		b.Where("exg_code", "=", params[0])
	}
	if len(params) > 1 && params[1] != "" {
		if i, err := strconv.Atoi(params[1]); err == nil && i > 0 {
			b.Limit(i)
		}
	}
	if rows, err = dao.Get(b); err == nil {
		for rows.Next() {
			exg := Exg{}
			if err = rows.Scan(model.Struct4Scan(&exg)...); err == nil {
				*e = append(*e, &exg)
			} else {
				return
			}
		}
	}
	return
}

func (dao *Dao) UpdateOrInsertExg(t *Exg) (r sql.Result, err error) {
	var (
		b          = sb.NewSQLBuilder("SQLite")
		cnt        int64
		withoutKey = []string{"id", "exg_code"}
	)
	defer func() {
		b.Release()
	}()

	b.Set(model.Inst2Set(*t, withoutKey...)).
		From(TbExgs).
		Where("exg_code", "=", t.ExgCode).
		BuildUpdateSQL()
	if r, err = dao.ExecBuilder(b); err == nil {
		if cnt, err = r.RowsAffected(); err == nil && cnt == 0 {
			b.Fields(model.Inst2FieldWithoutID(*t)...).
				Values(model.Inst2Values(*t, "id")...).
				Into(TbExgs).
				BuildInsertSQL()
			r, err = dao.ExecBuilder(b)
		}
	}

	return
}

Documentation

Overview

Package sqlbuilder is a builder that easy to use to build SQL string

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EscapeStr added in v0.0.4

func EscapeStr(value string, isMysql ...bool) string

EscapeStr is a internal function

Types

type SQLBuilder

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

SQLBuilder is the main struct type

func NewSQLBuilder

func NewSQLBuilder(d ...string) (b *SQLBuilder)

NewSQLBuilder can create a sqlbuilder object

func (*SQLBuilder) BuildBulkInsertSQL added in v0.0.12

func (sb *SQLBuilder) BuildBulkInsertSQL() *SQLBuilder

BuildBulkInsertSQL do build the `insert` SQL string with bulk values

func (*SQLBuilder) BuildDeleteSQL

func (sb *SQLBuilder) BuildDeleteSQL() *SQLBuilder

BuildDeleteSQL do build the `delete` SQL string

func (*SQLBuilder) BuildInsertOrReplaceSQL added in v0.0.7

func (sb *SQLBuilder) BuildInsertOrReplaceSQL() *SQLBuilder

BuildInsertOrReplaceSQL do build the `insert or replace into` SQL string only for SQLite

func (*SQLBuilder) BuildInsertSQL

func (sb *SQLBuilder) BuildInsertSQL() *SQLBuilder

BuildInsertSQL do build the `insert` SQL string

func (*SQLBuilder) BuildSelectSQL

func (sb *SQLBuilder) BuildSelectSQL() *SQLBuilder

BuildSelectSQL do build the `select` SQL string

func (*SQLBuilder) BuildUpdateSQL

func (sb *SQLBuilder) BuildUpdateSQL() *SQLBuilder

BuildUpdateSQL do build the `update` SQL string

func (*SQLBuilder) BuildedSQL

func (sb *SQLBuilder) BuildedSQL() (sql string)

BuildedSQL return the builded SQL string

func (*SQLBuilder) CanBuildDelete

func (sb *SQLBuilder) CanBuildDelete() bool

CanBuildDelete is internal function that sqlbuilder to know can build delete string or not

func (*SQLBuilder) CanBuildInsert

func (sb *SQLBuilder) CanBuildInsert() bool

CanBuildInsert is internal function that sqlbuilder to know can build insert string or not

func (*SQLBuilder) CanBuildSelect

func (sb *SQLBuilder) CanBuildSelect() bool

CanBuildSelect is internal function that sqlbuilder to know can build select string or not

func (*SQLBuilder) CanBuildUpdate

func (sb *SQLBuilder) CanBuildUpdate() bool

CanBuildUpdate is internal function that sqlbuilder to know can build update string or not

func (*SQLBuilder) ClearBuilder

func (sb *SQLBuilder) ClearBuilder()

ClearBuilder that reset the sqlbuilder

func (*SQLBuilder) Distinct

func (sb *SQLBuilder) Distinct(b bool) *SQLBuilder

Distinct set builder for `distinct`

func (*SQLBuilder) Fields

func (sb *SQLBuilder) Fields(s ...string) *SQLBuilder

Fields for set update fields

func (*SQLBuilder) From

func (sb *SQLBuilder) From(s ...string) *SQLBuilder

From set builder for `from` params must lest one or more ex : ``` From('tblA', 'tblB', 'tblC') ```

func (*SQLBuilder) FromOne

func (sb *SQLBuilder) FromOne(s string) *SQLBuilder

FromOne set builder for `from` just one param for one table ex : ``` FromOne('tblA') ```

func (*SQLBuilder) FullJoin

func (sb *SQLBuilder) FullJoin(j string) *SQLBuilder

FullJoin the join with natural fileds

func (*SQLBuilder) FullJoinOn added in v0.0.4

func (sb *SQLBuilder) FullJoinOn(j string, s string, o string, v interface{}) *SQLBuilder

FullJoinOn the join with one condition

func (*SQLBuilder) FullJoinOns added in v0.0.16

func (sb *SQLBuilder) FullJoinOns(j string, on ...SubCond) *SQLBuilder

FullJoinOns the join with multi condition

func (*SQLBuilder) GetDbName

func (sb *SQLBuilder) GetDbName() string

GetDbName return the default db name

func (*SQLBuilder) GetFieldsCount

func (sb *SQLBuilder) GetFieldsCount() int

GetFieldsCount is internal function

func (*SQLBuilder) GetTbName

func (sb *SQLBuilder) GetTbName() string

GetTbName return the default table name

func (*SQLBuilder) GroupBy

func (sb *SQLBuilder) GroupBy(s ...string) *SQLBuilder

GroupBy with fileds fileds must be same as select fileds

func (*SQLBuilder) Having

func (sb *SQLBuilder) Having(s string, o string, v interface{}) *SQLBuilder

Having with one condition its will overwrite having section

func (*SQLBuilder) Havings added in v0.0.16

func (sb *SQLBuilder) Havings(h ...SubCond) *SQLBuilder

Havings with multi conditions

func (*SQLBuilder) InnerJoin

func (sb *SQLBuilder) InnerJoin(j string) *SQLBuilder

InnerJoin the join with natural fileds

func (*SQLBuilder) InnerJoinOn added in v0.0.4

func (sb *SQLBuilder) InnerJoinOn(j string, s string, o string, v interface{}) *SQLBuilder

InnerJoinOn the join with one condition

func (*SQLBuilder) InnerJoinOns added in v0.0.16

func (sb *SQLBuilder) InnerJoinOns(j string, on ...SubCond) *SQLBuilder

InnerJoinOns the join with multi condition

func (*SQLBuilder) Into

func (sb *SQLBuilder) Into(s string) *SQLBuilder

Into for set insert table

func (*SQLBuilder) IsDistinct

func (sb *SQLBuilder) IsDistinct() bool

IsDistinct is internal function

func (*SQLBuilder) IsHadBuildedSQL

func (sb *SQLBuilder) IsHadBuildedSQL() bool

IsHadBuildedSQL can know has BuildedSQL string or not

func (*SQLBuilder) IsHasDbName

func (sb *SQLBuilder) IsHasDbName() bool

IsHasDbName is internal function

func (*SQLBuilder) IsHasFields

func (sb *SQLBuilder) IsHasFields() bool

IsHasFields is internal function

func (*SQLBuilder) IsHasFroms

func (sb *SQLBuilder) IsHasFroms() bool

IsHasFroms is internal function

func (*SQLBuilder) IsHasGroups

func (sb *SQLBuilder) IsHasGroups() bool

IsHasGroups is internal function

func (*SQLBuilder) IsHasHavings

func (sb *SQLBuilder) IsHasHavings() bool

IsHasHavings is internal function

func (*SQLBuilder) IsHasInto

func (sb *SQLBuilder) IsHasInto() bool

IsHasInto is internal function

func (*SQLBuilder) IsHasJoins

func (sb *SQLBuilder) IsHasJoins() bool

IsHasJoins is internal function

func (*SQLBuilder) IsHasLimit

func (sb *SQLBuilder) IsHasLimit() bool

IsHasLimit is internal function

func (*SQLBuilder) IsHasOneFroms

func (sb *SQLBuilder) IsHasOneFroms() bool

IsHasOneFroms is internal function

func (*SQLBuilder) IsHasOrders

func (sb *SQLBuilder) IsHasOrders() bool

IsHasOrders is internal function

func (*SQLBuilder) IsHasSelects

func (sb *SQLBuilder) IsHasSelects() bool

IsHasSelects is internal function

func (*SQLBuilder) IsHasSets

func (sb *SQLBuilder) IsHasSets() bool

IsHasSets is internal function

func (*SQLBuilder) IsHasTbName

func (sb *SQLBuilder) IsHasTbName() bool

IsHasTbName is internal function

func (*SQLBuilder) IsHasTop

func (sb *SQLBuilder) IsHasTop() bool

IsHasTop is internal function

func (*SQLBuilder) IsHasValues

func (sb *SQLBuilder) IsHasValues() bool

IsHasValues is internal function

func (*SQLBuilder) IsHasWheres

func (sb *SQLBuilder) IsHasWheres() bool

IsHasWheres is internal function

func (*SQLBuilder) IsMssql

func (sb *SQLBuilder) IsMssql() bool

IsMssql return the builder engine is for mssql

func (*SQLBuilder) IsMysql

func (sb *SQLBuilder) IsMysql() bool

IsMysql return the builder engine is for mysql

func (*SQLBuilder) IsOracle

func (sb *SQLBuilder) IsOracle() bool

IsOracle return the builder engine is for oracle

func (*SQLBuilder) IsPostgresql

func (sb *SQLBuilder) IsPostgresql() bool

IsPostgresql return the builder engine is for postgresql

func (*SQLBuilder) IsSQLite added in v0.0.7

func (sb *SQLBuilder) IsSQLite() bool

IsSQLite return the builder engine is for SQLite

func (*SQLBuilder) Join

func (sb *SQLBuilder) Join(j string) *SQLBuilder

Join is a natural join

func (*SQLBuilder) JoinOn added in v0.0.4

func (sb *SQLBuilder) JoinOn(j string, s string, o string, v interface{}) *SQLBuilder

JoinOn the join with one condition

func (*SQLBuilder) JoinOns added in v0.0.16

func (sb *SQLBuilder) JoinOns(j string, on ...SubCond) *SQLBuilder

JoinOns the join with multi condition

func (*SQLBuilder) LeftJoin

func (sb *SQLBuilder) LeftJoin(j string) *SQLBuilder

LeftJoin the join with natural fileds

func (*SQLBuilder) LeftJoinOn added in v0.0.4

func (sb *SQLBuilder) LeftJoinOn(j string, s string, o string, v interface{}) *SQLBuilder

LeftJoinOn the join with one condition

func (*SQLBuilder) LeftJoinOns added in v0.0.16

func (sb *SQLBuilder) LeftJoinOns(j string, on ...SubCond) *SQLBuilder

LeftJoinOns the join with multi condition

func (*SQLBuilder) Limit

func (sb *SQLBuilder) Limit(i ...int) *SQLBuilder

Limit set builder for `limit` only for Mysql, SQLite

func (*SQLBuilder) OrderBy

func (sb *SQLBuilder) OrderBy(s ...string) *SQLBuilder

OrderBy with fileds default Asc

func (*SQLBuilder) OrderByAsc

func (sb *SQLBuilder) OrderByAsc(s ...string) *SQLBuilder

OrderByAsc with fileds

func (*SQLBuilder) OrderByDesc

func (sb *SQLBuilder) OrderByDesc(s ...string) *SQLBuilder

OrderByDesc with fileds

func (*SQLBuilder) PanicOrErrorLog

func (sb *SQLBuilder) PanicOrErrorLog(s string)

PanicOrErrorLog is a internal function

func (*SQLBuilder) Release added in v0.0.8

func (sb *SQLBuilder) Release()

Release this object

func (*SQLBuilder) RightJoin

func (sb *SQLBuilder) RightJoin(j string) *SQLBuilder

RightJoin the join with natural fileds

func (*SQLBuilder) RightJoinOn added in v0.0.4

func (sb *SQLBuilder) RightJoinOn(j string, s string, o string, v interface{}) *SQLBuilder

RightJoinOn the join with one condition

func (*SQLBuilder) RightJoinOns added in v0.0.16

func (sb *SQLBuilder) RightJoinOns(j string, on ...SubCond) *SQLBuilder

RightJoinOns the join with multi condition

func (*SQLBuilder) Select

func (sb *SQLBuilder) Select(s ...string) *SQLBuilder

Select set builder for `select` params must lest one or more ex : ``` Select('fieldA', 'fieldB', 'fieldC') ```

func (*SQLBuilder) Set

func (sb *SQLBuilder) Set(s []Set) *SQLBuilder

Set with Set{K string, V interface{}} structs

func (*SQLBuilder) SetDbName

func (sb *SQLBuilder) SetDbName(s string)

SetDbName set a default db name

func (*SQLBuilder) SetDriverType

func (sb *SQLBuilder) SetDriverType(t string)

SetDriverType is a internal function

func (*SQLBuilder) SetTbName

func (sb *SQLBuilder) SetTbName(s string)

SetTbName set a default table name

func (*SQLBuilder) SwitchPanicToErrorLog

func (sb *SQLBuilder) SwitchPanicToErrorLog(b bool)

SwitchPanicToErrorLog is a internal function

func (*SQLBuilder) Top

func (sb *SQLBuilder) Top(i int) *SQLBuilder

Top set builder for `top` only for Mssql

func (*SQLBuilder) Values

func (sb *SQLBuilder) Values(s ...interface{}) *SQLBuilder

Values for set update values

func (*SQLBuilder) Where

func (sb *SQLBuilder) Where(s string, o string, v interface{}) *SQLBuilder

Where is equal WhereAnd if this not first time use, its will be `and` condition 3 params : s is mean filed o is a operator, ex : `=`, `>`, ... v is a value , it type interface{} ex : ``` Where('fieldA', '=', 0) ```

func (*SQLBuilder) WhereAnd

func (sb *SQLBuilder) WhereAnd(s string, o string, v interface{}) *SQLBuilder

WhereAnd same as Where

func (*SQLBuilder) WhereAndStr added in v0.0.13

func (sb *SQLBuilder) WhereAndStr(s string) *SQLBuilder

WhereAndStr same as WhereStr

func (*SQLBuilder) WhereOr

func (sb *SQLBuilder) WhereOr(s string, o string, v interface{}) *SQLBuilder

WhereOr if this not first time use, its will be `or` condition 3 params : s is mean filed o is a operator, ex : `=`, `>`, ... v is a value , it type interface{} ex : ``` WhereOr('fieldA', '=', 0) ```

func (*SQLBuilder) WhereOrStr added in v0.0.13

func (sb *SQLBuilder) WhereOrStr(s string) *SQLBuilder

WhereOrStr just one param for add a where string if this not first time use, its will be `or` condition it its first time use, just same as WhereStr ex : ``` WhereOrStr('fieldA = 0') ```

func (*SQLBuilder) WhereStr added in v0.0.13

func (sb *SQLBuilder) WhereStr(s string) *SQLBuilder

WhereStr is equal WhereAndStr just one param for add a where string if this not first time use, its will be `and` condition ex : ``` WhereStr('fieldA = 0') ```

type SQLVar added in v0.0.14

type SQLVar struct {
	VarS string
}

SQLVar can that you sql internal function via NewSQLVar()

func NewSQLVar added in v0.0.14

func NewSQLVar(s string) SQLVar

NewSQLVar gen you want string int builder

func Var added in v0.0.16

func Var(s string) SQLVar

Var same as NewSQLVar

type Set added in v0.0.5

type Set struct {
	K string
	V interface{}
}

Set is a type that define the update set struct

type SubCond added in v0.0.16

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

SubCond struct for join condition

func On added in v0.0.16

func On(s string, o string, v interface{}) SubCond

On return a sub condition for join or having same as OnAnd

func OnAnd added in v0.0.16

func OnAnd(s string, o string, v interface{}) SubCond

OnAnd its will be `and` sub condition

func OnOr added in v0.0.16

func OnOr(s string, o string, v interface{}) SubCond

OnOr its will be `or` sub condition

Jump to

Keyboard shortcuts

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