query

package
v1.1.17 Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: MIT Imports: 3 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 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
}
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

func New

func New(q string) *Query

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

func (*Query) CrossJoin added in v1.1.0

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

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

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

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

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

func (*Query) String

func (q *Query) String() string

String returns the full SQL query string

func (*Query) Where

func (q *Query) Where() *Where

Where returns the WHERE clause for adding conditions

type Where

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

The Where struct represents the WHERE clause in a SQL query. It contains a chunks slice to hold the individual WHERE conditions.

func (*Where) AND

func (w *Where) AND(args ...string) *Where

AND adds an AND condition to the WHERE clause.

func (*Where) EQ

func (w *Where) EQ(fieldName string, value any) *Where

EQ adds an equality condition to the WHERE clause.

func (*Where) IS added in v1.1.17

func (w *Where) IS(fieldName string, value any) *Where

func (*Where) LIKE

func (w *Where) LIKE(fieldName string, value any) *Where

LIKE adds a LIKE condition to the WHERE clause.

func (*Where) OR

func (w *Where) OR(args ...string) *Where

OR adds an OR condition to the WHERE clause.

Jump to

Keyboard shortcuts

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