Documentation
¶
Index ¶
- Constants
- func AND(args ...string) string
- func EQ(fieldName string, value any) string
- func IN(fieldName string, values ...any) string
- func IS(fieldName string, value any) string
- func LIKE(fieldName string, value any) string
- func OR(args ...string) string
- func ValidOrderType(val string) bool
- type Query
- func (w *Query) AND(args ...string) WhereClause
- func (q *Query) CrossJoin(table string) *Query
- func (w *Query) EQ(fieldName string, value any) WhereClause
- func (q *Query) FullJoin(table string, on string) *Query
- func (q *Query) GroupBy(columns ...string) *Query
- func (q *Query) Having(condition string) *Query
- func (w *Query) IN(fieldName string, values ...any) WhereClause
- func (w *Query) IS(fieldName string, value any) WhereClause
- func (q *Query) InnerJoin(table string, on string) *Query
- func (q *Query) InsertColumns(columns ...string) *Query
- func (w *Query) LIKE(fieldName string, value any) WhereClause
- func (q *Query) LeftJoin(table string, on string) *Query
- func (q *Query) Limit(val string) *Query
- func (w *Query) OR(args ...string) WhereClause
- func (q *Query) Offset(val string) *Query
- func (q *Query) Order(by string, orderType string) *Query
- func (q *Query) Query() *Query
- func (q *Query) Returning(fields ...string) *Query
- func (q *Query) RightJoin(table string, on string) *Query
- func (q *Query) Set(name string, value any) *Query
- func (q *Query) String() string
- func (q *Query) Values(values ...any) *Query
- func (q *Query) Where() WhereClause
- type WhereClause
Examples ¶
Constants ¶
const ( // DESC is descending order DESC = "DESC" // ASC is ascending order ASC = "ASC" )
Variables ¶
This section is empty.
Functions ¶
func IN ¶ added in v1.1.23
IN creates an IN condition string from the provided field name and values. If the values slice is empty, an empty string is returned. The values are wrapped using the wrapValue function before being included in the IN clause.
func ValidOrderType ¶
ValidOrderType checks if the given string is a valid ordering type
Types ¶
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
func (*Query) AND ¶ added in v1.1.21
func (w *Query) AND(args ...string) WhereClause
AND adds an AND condition to the WHERE clause.
func (*Query) EQ ¶ added in v1.1.21
func (w *Query) EQ(fieldName string, value any) WhereClause
EQ adds an equality condition to the WHERE clause.
func (*Query) InsertColumns ¶ added in v1.1.21
Sets the columns to be inserted in an INSERT statement.
Example ¶
query := New("INSERT INTO table_name").InsertColumns("name", "age").
Values("John", 12).Values("Doe", 13).Values("Jane", 14).
Returning("id", "name", "age")
fmt.Println(query.String())
Output: INSERT INTO table_name (name, age) VALUES ('John', 12), ('Doe', 13), ('Jane', 14) RETURNING id, name, age
func (*Query) LIKE ¶ added in v1.1.21
func (w *Query) LIKE(fieldName string, value any) WhereClause
LIKE adds a LIKE condition to the WHERE clause.
func (*Query) OR ¶ added in v1.1.21
func (w *Query) OR(args ...string) WhereClause
OR adds an OR condition to the WHERE clause.
func (*Query) Set ¶ added in v1.1.21
Example ¶
query := New("UPDATE table_name").Set("name", "John").Where().EQ("age", 12).Query()
fmt.Println(query.String())
query.Set("age", 20)
fmt.Println(query.String())
query.Set("country", "USA").Set("city", "New York").Set("accepted", true).Set("updated_at", "now()")
fmt.Println(query.String())
Output: UPDATE table_name SET name = 'John' WHERE age = 12 UPDATE table_name SET name = 'John', age = 20 WHERE age = 12 UPDATE table_name SET name = 'John', age = 20, country = 'USA', city = 'New York', accepted = true, updated_at = NOW() WHERE age = 12
func (*Query) String ¶
Returns the full SQL query string
Example ¶
query := New("SELECT * FROM test_table").LeftJoin("test_posts", "test_table.id=test_posts.user_id").Order("name", DESC).Limit("5").Offset("1").Where().
OR(EQ("name", "testname"), EQ("age", "12")).
AND(EQ("id", "123"), EQ("email", "test@mail.com")).
IN("name", "John", "Doe", "Jane").
LIKE("name", "%testname").Query()
fmt.Println(query.String())
Output: SELECT * FROM test_table LEFT JOIN test_posts ON test_table.id=test_posts.user_id WHERE (name = 'testname' OR age = '12') AND (id = '123' AND email = 'test@mail.com') AND name IN ('John','Doe','Jane') AND name LIKE '%testname' ORDER BY name DESC LIMIT 5 OFFSET 1
func (*Query) Values ¶ added in v1.1.21
Appends the provided values to the list of values to be inserted in an INSERT statement.
func (*Query) Where ¶
func (q *Query) Where() WhereClause
Where returns the WHERE clause for adding conditions
Example ¶
query := New("SELECT * FROM test_table").Where().OR(EQ("name", "testname"), EQ("age", "12")).Query()
fmt.Println(query.String())
query.Where().AND(EQ("id", "123"), EQ("email", "test@mail.com"))
fmt.Println(query.String())
query.Where().LIKE("name", "%testname").Query()
fmt.Println(query.String())
Output: SELECT * FROM test_table WHERE (name = 'testname' OR age = '12') SELECT * FROM test_table WHERE (name = 'testname' OR age = '12') AND (id = '123' AND email = 'test@mail.com') SELECT * FROM test_table WHERE (name = 'testname' OR age = '12') AND (id = '123' AND email = 'test@mail.com') AND name LIKE '%testname'
type WhereClause ¶ added in v1.1.21
type WhereClause interface {
// EQ creates an equality condition for the specified field and value
EQ(fieldName string, value any) WhereClause
// LIKE creates a LIKE condition for the specified field and value
LIKE(fieldName string, value any) WhereClause
// OR creates an OR condition with the given arguments
OR(args ...string) WhereClause
// AND creates an AND condition with the given arguments
AND(args ...string) WhereClause
// IS creates an IS condition for the specified field and value
IS(fieldName string, value any) WhereClause
// IN creates an IN condition for the specified field and values
IN(fieldName string, values ...any) WhereClause
// Query returns the Query object associated with this WhereClause
Query() *Query
}