query

package
v1.1.24 Latest Latest
Warning

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

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

README

Query Builder

Simple query builder for SQL.

Example

  query := New("SELECT * FROM test_table").LeftJoin("test_posts", "test_table.id=test_posts.user_id").Order("name", DESC).Limit("5").Offset("1")
  query.Where().
    OR(EQ("name", "testname"), EQ("age", "12")).
    AND(EQ("id", "123"), EQ("email", "test@mail.com")).
    LIKE("name", "%testname")
  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 LIKE '%testname' ORDER BY name DESC LIMIT 5 OFFSET 1

Documentation

Index

Examples

Constants

View Source
const (
	// DESC is descending order
	DESC = "DESC"
	// ASC is ascending order
	ASC = "ASC"
)

Variables

This section is empty.

Functions

func AND

func AND(args ...string) string

AND creates an AND condition string from the provided arguments.

func EQ

func EQ(fieldName string, value any) string

EQ creates an equality condition string.

func IN added in v1.1.23

func IN(fieldName string, values ...any) string

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 IS added in v1.1.17

func IS(fieldName string, value any) string

IS creates an IS condition string from the provided arguments.

func LIKE

func LIKE(fieldName string, value any) string

LIKE creates a LIKE condition string.

func OR

func OR(args ...string) string

OR creates an OR condition string from the provided arguments.

func ValidOrderType

func ValidOrderType(val string) bool

ValidOrderType checks if the given string is a valid ordering type

Types

type Query

type Query struct {
	// contains filtered or unexported fields
}

func New

func New(q string) *Query

New creates a new Query with the given base SQL query string

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) CrossJoin added in v1.1.0

func (q *Query) CrossJoin(table string) *Query

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) FullJoin added in v1.1.0

func (q *Query) FullJoin(table string, on string) *Query

func (*Query) GroupBy added in v1.1.0

func (q *Query) GroupBy(columns ...string) *Query

GroupBy adds a GROUP BY clause to the query

func (*Query) Having added in v1.1.0

func (q *Query) Having(condition string) *Query

Having adds a HAVING clause to the query

func (*Query) IN added in v1.1.23

func (w *Query) IN(fieldName string, values ...any) WhereClause

func (*Query) IS added in v1.1.21

func (w *Query) IS(fieldName string, value any) WhereClause

func (*Query) InnerJoin added in v1.1.0

func (q *Query) InnerJoin(table string, on string) *Query

func (*Query) InsertColumns added in v1.1.21

func (q *Query) InsertColumns(columns ...string) *Query

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) LeftJoin added in v1.1.0

func (q *Query) LeftJoin(table string, on string) *Query

func (*Query) Limit

func (q *Query) Limit(val string) *Query

Limit adds a LIMIT 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) Offset

func (q *Query) Offset(val string) *Query

Offset adds an OFFSET clause

func (*Query) Order

func (q *Query) Order(by string, orderType string) *Query

Order adds an ORDER BY clause

func (*Query) Query added in v1.1.21

func (q *Query) Query() *Query

func (*Query) Returning added in v1.1.21

func (q *Query) Returning(fields ...string) *Query

func (*Query) RightJoin added in v1.1.0

func (q *Query) RightJoin(table string, on string) *Query

func (*Query) Set added in v1.1.21

func (q *Query) Set(name string, value any) *Query
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

func (q *Query) String() 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

func (q *Query) Values(values ...any) *Query

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
}

Jump to

Keyboard shortcuts

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