builder

package module
v0.0.0-...-a7b1189 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2018 License: MIT Imports: 4 Imported by: 0

README

go-sqlbuilder

Build Status Go Report Card Coverage Status MIT licensed

(unfinished) a very simple sql builder for golang.

TODO

  • dialect support for mysql/postgresql/sqlite etc... Only support mysql now. (escape character and placeholder were hard coded)

    • escape character (mysql:`) (postgres:")
    • placeholder for params binding (mysql:?) (postgres:$index)
    • etc...
  • more statement support (group/having/join etc...)

  • more examples (you could find some in test file now)

  • more helpful usages

  • Long-term planning:

    Maybe this package could be a micro ORM?Or add a simple scanner or DAO adapter?

    I have already done a veeeeeery simple and crude demo but it's too simple.

    In general, I hope this package as simple as possible. Waiting for above unitl I have enough free time (long after...).

Installation

go get -u github.com/DropFan/go-sqlbuilder

Usage (unfinished)

// TODO

Click here to get examples.

import (
    builder "github.com/DropFan/go-sqlbuilder"
)

var (
    b = builder.New()
    q *Query
)

b.Select(fields...).
    From("user").
    Where(ageGT1, nameInNames).
    And(sexEqFemale).
    And().
    Or(ageBetweenCond).
    Or(nameEqCoder).
    OrderBy(ageDesc, nameAsc).
    Limit(0, 100)
q, err = b.Build()

Contacts

Author: Tiger(DropFan)

Email: DropFan@Gmail.com

Wechat: DropFan

Telegram: DropFan

https://about.me/DropFan

License

MIT

Documentation

Index

Constants

View Source
const (
	// RawSQL ...
	RawSQL = iota + 1
	// SelectSQL ...
	SelectSQL
	// InsertSQL ...
	InsertSQL
	// UpdateSQL ...
	UpdateSQL
	// DeleteSQL ...
	DeleteSQL
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	ErrList []error
	// contains filtered or unexported fields
}

Builder ...

func New

func New() *Builder

New builder

func (*Builder) And

func (b *Builder) And(conditions ...*Condition) *Builder

And ...

func (*Builder) Append

func (b *Builder) Append(s string, args ...interface{}) *Builder

Append append query & query args to origin query

func (*Builder) AppendPre

func (b *Builder) AppendPre(s string, args ...interface{}) *Builder

AppendPre append query & query args to ahead of origin query

func (*Builder) Build

func (b *Builder) Build(queries ...interface{}) (q *Query, err error)

Build ...

func (*Builder) Clear

func (b *Builder) Clear() *Builder

Clear clear current query & query args

func (*Builder) Delete

func (b *Builder) Delete(tableName string) *Builder

Delete ...

func (*Builder) From

func (b *Builder) From(tables ...string) *Builder

From ...

func (*Builder) Insert

func (b *Builder) Insert(tableName string, fields ...string) *Builder

Insert ...

func (*Builder) Into

func (b *Builder) Into(fields ...string) *Builder

Into ... for insert or replace

func (*Builder) LastQueries

func (b *Builder) LastQueries() []*Query

LastQueries return last queries

func (*Builder) Limit

func (b *Builder) Limit(limitOffset ...int) *Builder

Limit ...

func (*Builder) Or

func (b *Builder) Or(conditions ...*Condition) *Builder

Or ...

func (*Builder) OrderBy

func (b *Builder) OrderBy(conditions ...*Condition) *Builder

OrderBy ...

func (*Builder) Query

func (b *Builder) Query() string

Query return current sql

func (*Builder) QueryArgs

func (b *Builder) QueryArgs() []interface{}

QueryArgs return current query args

func (*Builder) Raw

func (b *Builder) Raw(s string, args ...interface{}) *Builder

Raw ...

func (*Builder) Replace

func (b *Builder) Replace(tableName string, fields ...string) *Builder

Replace ...

func (*Builder) Select

func (b *Builder) Select(fields ...string) *Builder

Select ...

func (*Builder) Set

func (b *Builder) Set(fvals ...*FieldValue) *Builder

Set ...

func (*Builder) Update

func (b *Builder) Update(tableName string, fvals ...*FieldValue) *Builder

Update ...

func (*Builder) Values

func (b *Builder) Values(valsGroup ...[]interface{}) *Builder

Values ...

func (*Builder) Where

func (b *Builder) Where(conditions ...*Condition) *Builder

Where ...

type Condition

type Condition struct {
	Field    string        // field name
	Asc      bool          // true for `ASC`, false for `DESC`
	AndOr    bool          // true for `AND`, false for `OR`
	Operator string        // where operator
	Values   []interface{} // query args
}

Condition for Where & Order Cluse

func And

func And(field string, op string, values ...interface{}) *Condition

And return and condition

func Asc

func Asc(field string) *Condition

Asc ...

func Desc

func Desc(field string) *Condition

Desc ...

func NewConditionGroup

func NewConditionGroup(conds ...*Condition) (cg []*Condition)

NewConditionGroup ...

func Or

func Or(field string, op string, values ...interface{}) *Condition

Or return or condition

func OrderBy

func OrderBy(conds ...*Condition) (by []*Condition)

OrderBy new orderby conditions

type FieldValue

type FieldValue struct {
	Name  string
	Value interface{}
}

FieldValue ...

func NewFV

func NewFV(name string, value interface{}) *FieldValue

NewFV ...

func NewFieldValue

func NewFieldValue(name string, value interface{}) *FieldValue

NewFieldValue ...

func NewKV

func NewKV(name string, value interface{}) *FieldValue

NewKV ...

type Query

type Query struct {
	Query string
	Args  []interface{}
}

Query ...

func NewQuery

func NewQuery(q string, args ...interface{}) *Query

NewQuery ...

func (*Query) String

func (q *Query) String() (str string)

type SQLType

type SQLType int

SQLType ...

type Values

type Values []interface{}

Values ...

Jump to

Keyboard shortcuts

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