query

package
v0.0.0-...-1039f8e Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2017 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Go write SQL

import . "github.com/cxr29/scrud/query"

// Create
query, args, err := Insert("Table1").Columns(
	"Column1", "Column2", "Column3", ...
).Values(
	false, 0, "hi", ...
).Values(
	true, 1, "cxr", ...
).Expand(new(MySQL))

// Retrieve
query, args, err = Select(
	"Column1",
	"Table1.Column2",                             // `Table1`.`Column1`
	Expr("COUNT(DISTINCT `Column3`) AS `Count`"), // -- expression also ok
	...
).From("Table1", ...).LeftJoin("Table2", // LEFT JOIN `Table2` USING (`Column2`, ...)
	"Column2", // -- join using when string
	...
).RightJoin("Table3", // RIGHT JOIN `Table3` ON `Column4`=? AND `Table1`.`Column1`+`Table3`.`Column1`>? ... -- true, 1
	Eq("Column4", true),                            // -- join on when Condition
	Cond("`Table1.Column1`+`Table3.Column1`>?", 1), // -- AND
	...
).FullJoin(
	As(Select().From(`Table4`), "a"), // (SELECT * FROM `Table4`) AS `a` -- join expression as subquery
	...
).Where( // -- AND
	In("Column5", 2, 3, 4),    // `Column5` IN (?,?,?) -- 2,3,4
	Contains("Column6", "_"), // `Column6` LIKE ? -- %\_%
	Cond("`Table3.Column3` > `a.Column4`"),
	...
).GroupBy(
	"Column1",
	Expr("EXTRACT(YEAR FROM Column7`)"), // -- expression also ok
).Having( // -- same as Where
	...
).OrderBy(
	Desc("Column1"),                     // `Column1` DESC
	Expr("EXTRACT(YEAR FROM Column7`)"), // -- expression also ok
).Limit(5).Offset(6).Expand(new(Postgres))

// Update
query, args, err = Update("Table1").Set(
	"Column1", true,
).Set( // -- expression also ok
	"Column2", Expr("Column2+?", 1), // `Column2`=`Column2`+? -- 1
).Where(
	... // -- same as Retrieve Where
).OrderBy(
	... // -- same as Retrieve OrderBy
).Limit(...).Expand(new(Sqlite))

// Delete
query, args, err = Delete("Table1").Where(
	... // -- same as Retrieve Where
).OrderBy(
	... // -- same as Retrieve OrderBy
).Limit(...).Expand(...)

See https://github.com/cxr29/scrud for more details

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BackQuote

func BackQuote(s string) string

func Count

func Count() *retrieve

func Delete

func Delete(table string) *delete

delete clause generator

func DoubleQuote

func DoubleQuote(s string) string

func EscapeLike

func EscapeLike(s string) string

func EscapeRegexp

func EscapeRegexp(s string) string

func Insert

func Insert(table string) *create

insert clause generator

func Ints2Interfaces

func Ints2Interfaces(a []int) (b []interface{})

func RepeatMarker

func RepeatMarker(n int) string

func Select

func Select(a ...interface{}) *retrieve

select clause generator

string or expression

func Strings2Interfaces

func Strings2Interfaces(a []string) (b []interface{})

func Update

func Update(table string) *update

update clause generator

Types

type Condition

type Condition interface {
	Expression
	And(...Condition) Condition
	Or(...Condition) Condition
	Not() Condition
}

expression with logical operation for join on, where and having

func And

func And(a ...Condition) Condition

logical and conditions

func Between

func Between(k string, start, end interface{}) Condition

`k` BETWEEN ? AND ?

func Cond

func Cond(format string, args ...interface{}) Condition

same as Expr

func Contains

func Contains(k, v string) Condition

`k` LIKE %?%

func Eq

func Eq(k string, v interface{}) Condition

`k`=?

func Ge

func Ge(k string, v interface{}) Condition

`k`>=?

func Gt

func Gt(k string, v interface{}) Condition

`k`>?

func HasPrefix

func HasPrefix(k, v string) Condition

`k` LIKE ?%

func HasSuffix

func HasSuffix(k, v string) Condition

`k` LIKE %?

func In

func In(k string, a ...interface{}) Condition

`k` IN (?,...)

func InInts

func InInts(k string, a ...int) Condition

`k` IN (1,2,3,...)

func IsNull

func IsNull(k string) Condition

`k` IS NULL

func Le

func Le(k string, v interface{}) Condition

`k`<=?

func Like

func Like(k, v string) Condition

`k` LIKE ?

func Lt

func Lt(k string, v interface{}) Condition

`k`<?

func Not

func Not(format string, args ...interface{}) Condition

same as Cond, but logical not

func Or

func Or(a ...Condition) Condition

logical or conditions

type Expression

type Expression interface {
	Err() error
	Expand(Starter) (string, []interface{}, error)
}

sql segments contains identifier and placeholder with arguments

func As

func As(v interface{}, k string) Expression

(?) AS `k`

func Asc

func Asc(k string) Expression

`k` ASC

func Desc

func Desc(k string) Expression

`k` DESC

func Expr

func Expr(format string, args ...interface{}) Expression

marker: question mark(?) as placeholder, recursive expand if corresponding argument is expression

identifier: the name in back quote(`) such as `column` and `table.column`

double back quote(`) and dot sign(.) to include it as literal in identifier

double back quote(`) and question mark(?) to include it as literal in expression

type MySQL

type MySQL int

mysql starter to expand expression

func (*MySQL) DriverName

func (_ *MySQL) DriverName() string

func (*MySQL) FormatName

func (_ *MySQL) FormatName(s string) string

func (*MySQL) NextMarker

func (_ *MySQL) NextMarker() string

type Postgres

type Postgres int

postgres starter to expand expression

func (*Postgres) DriverName

func (_ *Postgres) DriverName() string

func (*Postgres) FormatName

func (_ *Postgres) FormatName(s string) string

func (*Postgres) NextMarker

func (x *Postgres) NextMarker() string

type Sqlite

type Sqlite int

sqlite starter to expand expression

func (*Sqlite) DriverName

func (_ *Sqlite) DriverName() string

func (*Sqlite) FormatName

func (_ *Sqlite) FormatName(s string) string

func (*Sqlite) NextMarker

func (_ *Sqlite) NextMarker() string

type Starter

type Starter interface {
	DriverName() string
	NextMarker() string
	FormatName(string) string
}

starter expand expression, format the identifier and replace the placeholder

Jump to

Keyboard shortcuts

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