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 ¶
- func BackQuote(s string) string
- func Count() *retrieve
- func Delete(table string) *delete
- func DoubleQuote(s string) string
- func EscapeLike(s string) string
- func EscapeRegexp(s string) string
- func Insert(table string) *create
- func Ints2Interfaces(a []int) (b []interface{})
- func RepeatMarker(n int) string
- func Select(a ...interface{}) *retrieve
- func Strings2Interfaces(a []string) (b []interface{})
- func Update(table string) *update
- type Condition
- func And(a ...Condition) Condition
- func Between(k string, start, end interface{}) Condition
- func Cond(format string, args ...interface{}) Condition
- func Contains(k, v string) Condition
- func Eq(k string, v interface{}) Condition
- func Ge(k string, v interface{}) Condition
- func Gt(k string, v interface{}) Condition
- func HasPrefix(k, v string) Condition
- func HasSuffix(k, v string) Condition
- func In(k string, a ...interface{}) Condition
- func InInts(k string, a ...int) Condition
- func IsNull(k string) Condition
- func Le(k string, v interface{}) Condition
- func Like(k, v string) Condition
- func Lt(k string, v interface{}) Condition
- func Not(format string, args ...interface{}) Condition
- func Or(a ...Condition) Condition
- type Expression
- type MySQL
- type Postgres
- type Sqlite
- type Starter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DoubleQuote ¶
func EscapeLike ¶
func EscapeRegexp ¶
func Ints2Interfaces ¶
func Ints2Interfaces(a []int) (b []interface{})
func RepeatMarker ¶
func Strings2Interfaces ¶
func Strings2Interfaces(a []string) (b []interface{})
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
type Expression ¶
sql segments contains identifier and placeholder with arguments
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) FormatName ¶
func (*MySQL) NextMarker ¶
type Postgres ¶
type Postgres int
postgres starter to expand expression
func (*Postgres) DriverName ¶
func (*Postgres) FormatName ¶
func (*Postgres) NextMarker ¶
type Sqlite ¶
type Sqlite int
sqlite starter to expand expression
func (*Sqlite) DriverName ¶
func (*Sqlite) FormatName ¶
func (*Sqlite) NextMarker ¶
Click to show internal directories.
Click to hide internal directories.