Documentation ¶
Overview ¶
Package loukoum provides a simple SQL Query Builder. At the moment, only PostgreSQL is supported.
If you have to generate complex queries, which rely on various contexts, loukoum is the right tool for you. It helps you generate SQL queries from composable parts. However, keep in mind it's not an ORM or a Mapper so you have to use a SQL connector (like "database/sql" or "sqlx", for example) to execute queries.
If you're afraid to slip a tiny SQL injection manipulating fmt (or a byte buffer...) when you append conditions, loukoum is here to protect you against yourself.
For further informations, you can read this documentation: https://github.com/DzananGanic/loukoum/blob/master/README.md
Or you can discover loukoum with these examples. An "insert" can be generated like that:
builder := loukoum.Insert("comments"). Set( loukoum.Pair("email", comment.Email), loukoum.Pair("status", "waiting"), loukoum.Pair("message", comment.Message), loukoum.Pair("created_at", loukoum.Raw("NOW()")), ). Returning("id")
Also, if you need an upsert, you can define a "on conflict" clause:
builder := loukoum.Insert("comments"). Set( loukoum.Pair("email", comment.Email), loukoum.Pair("status", "waiting"), loukoum.Pair("message", comment.Message), loukoum.Pair("created_at", loukoum.Raw("NOW()")), ). OnConflict("email", loukoum.DoUpdate( loukoum.Pair("message", comment.Message), loukoum.Pair("status", "waiting"), loukoum.Pair("created_at", loukoum.Raw("NOW()")), loukoum.Pair("deleted_at", nil), )). Returning("id")
Updating a news is also simple:
builder := loukoum.Update("news"). Set( loukoum.Pair("published_at", loukoum.Raw("NOW()")), loukoum.Pair("status", "published"), ). Where(loukoum.Condition("id").Equal(news.ID)). And(loukoum.Condition("deleted_at").IsNull(true)). Returning("published_at")
You can remove a specific user:
builder := loukoum.Delete("users"). Where(loukoum.Condition("id").Equal(user.ID))
Or select a list of users...
builder := loukoum.Select("id", "first_name", "last_name", "email"). From("users"). Where(loukoum.Condition("deleted_at").IsNull(true))
Index ¶
- Constants
- func And(left stmt.Expression, right stmt.Expression) stmt.InfixExpression
- func AndOn(left stmt.OnExpression, right stmt.OnExpression) stmt.OnExpression
- func Column(name string) stmt.Column
- func Condition(column string) stmt.Identifier
- func Count(value string) stmt.Count
- func Delete(from interface{}) builder.Delete
- func DoNothing() stmt.ConflictNoAction
- func DoUpdate(args ...interface{}) stmt.ConflictUpdateAction
- func Exists(value interface{}) stmt.Exists
- func Insert(into interface{}) builder.Insert
- func Max(value string) stmt.Max
- func Min(value string) stmt.Min
- func NotExists(value interface{}) stmt.NotExists
- func On(left string, right string) stmt.OnClause
- func Or(left stmt.Expression, right stmt.Expression) stmt.InfixExpression
- func OrOn(left stmt.OnExpression, right stmt.OnExpression) stmt.OnExpression
- func Order(column string, option ...types.OrderType) stmt.Order
- func Pair(key, value interface{}) types.Pair
- func Raw(value string) stmt.Raw
- func Select(columns ...interface{}) builder.Select
- func Sum(value string) stmt.Sum
- func Table(name string) stmt.Table
- func Update(table interface{}) builder.Update
- func With(name string, value interface{}) stmt.WithQuery
- type Map
Constants ¶
const ( // InnerJoin is used for "INNER JOIN" in join statement. InnerJoin = types.InnerJoin // LeftJoin is used for "LEFT JOIN" in join statement. LeftJoin = types.LeftJoin // RightJoin is used for "RIGHT JOIN" in join statement. RightJoin = types.RightJoin // LeftOuterJoin is used for "LEFT OUTER JOIN" in join statement. LeftOuterJoin = types.LeftOuterJoin // RightOuterJoin is used for "RIGHT OUTER JOIN" in join statement. RightOuterJoin = types.RightOuterJoin // Asc is used for "ORDER BY" statement. Asc = types.Asc // Desc is used for "ORDER BY" statement. Desc = types.Desc )
Variables ¶
This section is empty.
Functions ¶
func And ¶
func And(left stmt.Expression, right stmt.Expression) stmt.InfixExpression
And is a wrapper to create a new InfixExpression statement.
func AndOn ¶
func AndOn(left stmt.OnExpression, right stmt.OnExpression) stmt.OnExpression
AndOn is a wrapper to create a new On statement using an infix expression.
func Condition ¶
func Condition(column string) stmt.Identifier
Condition is a wrapper to create a new Identifier statement.
func DoNothing ¶
func DoNothing() stmt.ConflictNoAction
DoNothing is a wrapper to create a new ConflictNoAction statement.
func DoUpdate ¶
func DoUpdate(args ...interface{}) stmt.ConflictUpdateAction
DoUpdate is a wrapper to create a new ConflictUpdateAction statement.
func Or ¶
func Or(left stmt.Expression, right stmt.Expression) stmt.InfixExpression
Or is a wrapper to create a new InfixExpression statement.
func OrOn ¶
func OrOn(left stmt.OnExpression, right stmt.OnExpression) stmt.OnExpression
OrOn is a wrapper to create a new On statement using an infix expression.
Types ¶
Directories ¶
Path | Synopsis |
---|---|
Package builder receives user input and generates an AST using "stmt" package.
|
Package builder receives user input and generates an AST using "stmt" package. |
examples
|
|
Package format escape various types for types.RawContext.
|
Package format escape various types for types.RawContext. |
Package stmt defines various statements and clauses that are used to generate queries.
|
Package stmt defines various statements and clauses that are used to generate queries. |
Package types defines some internal types that are handled by the "builder" and "stmt" package.
|
Package types defines some internal types that are handled by the "builder" and "stmt" package. |