gosqler

package module
v0.0.0-...-471b843 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2019 License: MIT Imports: 5 Imported by: 0

README

go-sqler

Easy and safe to build sql 更简单更安全的创建sql 语句

Build SELECT SQL 创建基本的select 语句

import (
    gosqler "github.com/tingxin/go-sqler"
    "fmt"
)

query := gosqler.NewSelect()
query.Select("name", "age", "sex", "birthday", "is_employee")
query.From("users")
query.Where("age", "=", 15)
quey.AndWhere("name", "!=", "barry")
query.OrWhere("is_employee", "=", true)
query.OrWhere("birthday", "=", nil)
query.Orderby("birthday", true)
query.Orderby("name", false)
query.Select("address")
query.Limit(10)
query.Offset(36)
query.Where("name", "in", []string{"edwin", "leo", "jack", "stacy"})
query.Where("age", "not in", []int8{16, 24, 32})
str := query.String()
fmt.Println(str)

output:

SELECT name,age,sex,birthday,is_employee,address
FROM users
WHERE age = 15 AND name != "barry" OR is_employee = true OR birthday IS null
AND name in ("edwin","leo","jack","stacy")
AND age not in (16,24,32) ORDER BY birthday DESC,name ASC LIMIT 36, 10

Build INSERT SQL 创建基本的 insert 语句

import (
    gosqler "github.com/tingxin/go-sqler"
    "fmt"
)

query := gosqler.NewInsert("users")
query.AddColumns("name", "age", "sex", "birthday", "is_employee")
query.AddValues("barry", 30, "male", "1987-01-15", true)
query.AddValues("edwin", 35, "male", "1982-01-15", true)
query.AddValues("stacy", 32.5, "female", "1986-01-15", true)

str := query.String()
fmt.Println(str)

output:

INSERT INTO users ( name,age,sex,birthday,is_employee )
VALUES ("barry",30,"male","1987-01-15",true),
("edwin",35,"male","1982-01-15",true),
("stacy",32.5,"female","1986-01-15",true)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrorEmptyParams occur when param is empty
	ErrorEmptyParams = errors.New("params should not be empty")
)

Functions

This section is empty.

Types

type Insert

type Insert interface {
	AddColumns(fields ...string) error
	AddValues(values ...interface{}) error
	ClearValues() error
	String() string
}

Insert used to express insert sql

func NewInsert

func NewInsert(tableName string) Insert

NewInsert used to create where object

type Select

type Select interface {
	sql.Wherer
	sql.Haver
	Select(fields ...string)
	Choice(field string)
	From(tableNames ...string)
	Join(table string, conditions ...string)
	LeftJoin(table string, conditions ...string)
	RightJoin(table string, conditions ...string)
	FullJoin(table string, conditions ...string)
	Orderby(field string, desc bool)
	GroupBy(fields ...string)
	BeginWhere()
	EndWhere()
	BeginHaving()
	EndHaving()
	Limit(count int)
	Offset(offset int)
	String() string
}

Select used to express select sql

func NewSelect

func NewSelect() Select

NewSelect used to create where object

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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