sqlf

package module
v0.0.0-...-9d0d147 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2021 License: MIT Imports: 6 Imported by: 2

README

sqlf

Testing Go Report Card

SQLf is a SQL Fluent generation for Go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	QuestionPlaceholder = &questionPlaceholderFactory{}
	DollarPlaceholder   = &dollarPlaceholderFactory{
		pool: sync.Pool{
			New: func() interface{} {
				return &dollarPlaceholder{}
			},
		},
	}
)
View Source
var (
	// ErrMismatchFieldsAndValuesCount is returned when the length of values is not compatible with the length
	// of fields.
	//
	// This means that the len(values) is not multiple of len(fields). (Check the `Insert.Values` method documentation).
	ErrMismatchFieldsAndValuesCount = errors.New("the amount values is not compatible with the amount of fields")
)
View Source
var (
	ErrUpdateInvalidFieldValuePairCount = errors.New("invalid field and value pair count")
)

Functions

func Placeholders

func Placeholders(count int) string

Placeholders returns a sequence of "?" separated by comma.

func RenderInterfaceAsArg

func RenderInterfaceAsArg(sb SQLWriter, args *[]interface{}, element interface{}) error

RenderInterfaceAsArg renders the input element into the `SQLWriter` according with its type considering the input as an argument.

func RenderInterfaceAsSQL

func RenderInterfaceAsSQL(sb SQLWriter, args *[]interface{}, element interface{}) error

RenderInterfaceAsSQL renders the input element into the `sb`(`strings.Builder`) according with its type, considering the input was a SQL.

Sqlizer types are welcome and, if args are present they will be appended to the given `args` pointer.

Types

type Builder

type Builder interface {
	Placeholder(format PlaceholderFormatFactory) Builder
	Select(fields ...string) Select
	Insert(tableName string, fields ...interface{}) Insert
	Delete(tableName ...string) Delete
	Update(tableName ...string) Update
}

Builder is responsible to build SelectStatements with a default configuration.

func NewBuilder

func NewBuilder() Builder

NewBuilder returns a new instance of the default implementation of the `Builder`.

type Delete

type Delete interface {
	Sqlizer
	FastSqlizer

	// Placeholder defines the placeholder format that should be used for this delete statement.
	Placeholder(placeholder PlaceholderFormatFactory) Delete

	// Cascade enables the CASCADE option.
	Cascade() Delete

	// From defines what table will be deleted.
	From(tableName ...string) Delete

	// Where appends a condition. If called multiples, the conditions will be appended.
	//
	// The conditions added will use the AND operator.
	Where(condition string, args ...interface{}) Delete

	// WhereClause appends any Sqlizer to serve as where.
	//
	// The conditions added will use the AND operator.
	WhereClause(conditions ...FastSqlizer) Delete

	// Suffix adds a suffix to the DELETE statement. That can be useful for
	// extending the SQL for uncovered database technologies.
	Suffix(suffix string) Delete
}

Delete describes how a DELETE will behave into the sqlf.

type DeleteStatement

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

func (*DeleteStatement) Cascade

func (d *DeleteStatement) Cascade() Delete

Cascade enables the CASCADE option.

func (*DeleteStatement) From

func (d *DeleteStatement) From(tableName ...string) Delete

From defines what table will be deleted.

func (*DeleteStatement) Placeholder

func (d *DeleteStatement) Placeholder(placeholder PlaceholderFormatFactory) Delete

Placeholder defines the placeholder format that should be used for this delete statement.

func (*DeleteStatement) Suffix

func (d *DeleteStatement) Suffix(suffix string) Delete

Suffix adds a suffix to the DELETE statement. That can be useful for extending the SQL for uncovered database technologies.

func (*DeleteStatement) ToSQL

func (d *DeleteStatement) ToSQL() (string, []interface{}, error)

ToSQL generates the SQL and returns it, alongside its params.

func (*DeleteStatement) ToSQLFast

func (d *DeleteStatement) ToSQLFast(sb SQLWriter, args *[]interface{}) error

ToSQLFast generates the SQL and returns it, alongside its params.

func (*DeleteStatement) Where

func (d *DeleteStatement) Where(condition string, args ...interface{}) Delete

Where appends a condition. If called multiples, the conditions will be appended.

The conditions added will use the AND operator.

func (*DeleteStatement) WhereClause

func (d *DeleteStatement) WhereClause(conditions ...FastSqlizer) Delete

WhereClause appends any Sqlizer to serve as where.

The conditions added will use the AND operator.

type FastSqlizer

type FastSqlizer interface {
	// ToSQLFast generates the SQL and returns it, alongside its params.
	ToSQLFast(SQLWriter, *[]interface{}) error
}

FastSqlizer define anything that outputs a SQL.

func And

func And(conditions ...interface{}) FastSqlizer

And receive many conditions and generates their SQL with the AND operator between the conditions.

func Condition

func Condition(sql string, args ...interface{}) FastSqlizer

func Not

func Not(condition FastSqlizer) FastSqlizer

Not negates whatever conditions are passed returning a rendered SQL.

func Or

func Or(conditions ...interface{}) FastSqlizer

Or receive many conditions and generates their SQL with the OR operator between the conditions.

type GroupBy

type GroupBy interface {
	FastSqlizer

	// Fields defines the fields that the SQL GROUP BY will group.
	Fields(fields ...interface{}) GroupBy

	// Having defines the SQL HAVING clause.
	Having(condition string, params ...interface{}) GroupBy

	// Having defines the SQL HAVING clause.
	HavingClause(criteria ...FastSqlizer) GroupBy
}

GroupBy represents a SQL GROUP BY clause.

type GroupByClause

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

func (*GroupByClause) Fields

func (groupBy *GroupByClause) Fields(fields ...interface{}) GroupBy

Fields defines the fields that the SQL GROUP BY will group.

func (*GroupByClause) Having

func (groupBy *GroupByClause) Having(condition string, args ...interface{}) GroupBy

Having defines the SQL HAVING clause.

func (*GroupByClause) HavingClause

func (groupBy *GroupByClause) HavingClause(criteria ...FastSqlizer) GroupBy

HavingClause defines the SQL HAVING clause.

func (*GroupByClause) ToSQLFast

func (groupBy *GroupByClause) ToSQLFast(sb SQLWriter, args *[]interface{}) error

ToSQLFast generates the SQL and returns it, alongside its params.

type Insert

type Insert interface {
	Sqlizer
	FastSqlizer

	// Placeholder defines the placeholder format that should be used for this insert statement.
	Placeholder(placeholder PlaceholderFormatFactory) Insert

	// Into defines what table the data will be inserted on. `fields` are the same as `Fields` method.
	Into(tableName string, fields ...interface{}) Insert

	// Fields define what fields will be defined into the insert. If there is a set of fields already defined
	// this method will replace them.
	Fields(fields ...interface{}) Insert

	// AddFields will append the fields to the current list.
	AddFields(fields ...interface{}) Insert

	// Values define what data will be inserted. Values will always append the information.
	//
	// If you are inserting multiple records at once, just call Values as many times you want to. The only requirement
	// is that the len of the total values should be multiple of the amount of fields that were defined.
	//
	// Example:
	//
	//     i := new(InsertStatement)
	//     i.Into("users", "name", "email", "password").Values("Name 1", "email1@email.com", "12345") // Adding one record
	//     i.Values("Name 2", "email2@email.com", "54321") // Adding another record
	//     i.Values("Name 3", "email3@email.com", "13425", "Name 4", "email4@email.com", "52431") // Adding more two records
	//
	Values(values ...interface{}) Insert

	// Select defines a select that will be inserted.
	//
	// Below an example of how this would be used in plain SQL. Ex:
	//
	//     INSERT INTO users (name, email, password) SELECT name, email, "12345" FROM employees;
	//
	// Select and Values are not compatible. Whenever there is a select defined, Values should be ignored.
	//
	Select(callback func(Select)) Insert

	// Returning defines the RETURNING clause defined for Postgres.
	Returning(fields ...interface{}) Insert

	// OnConflict defines the ON CONFLICT clause for Postgres.
	OnConflict(callback func(InsertConflict)) Insert

	// Suffix defines a suffix that will be appended at the end of the insert clause. This can be used to extend the
	// uses (like "ON DUPLICATE KEY" on MySQL) for other database technologies.
	Suffix(suffix string, args ...interface{}) Insert
}

Insert describes how a insert will behave into the sqlf.

type InsertConflict

type InsertConflict interface {
	Target(target interface{}) InsertConflict
	DoNothing() InsertConflict
	Update(callback func(InsertUpdate)) InsertConflict
}

InsertConflict describes the conflict statement for insertion.

type InsertStatement

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

InsertStatement is the default implementation of the `Insert` interface.

func (*InsertStatement) AddFields

func (insert *InsertStatement) AddFields(fields ...interface{}) Insert

AddFields will append the fields to the current list.

func (*InsertStatement) Fields

func (insert *InsertStatement) Fields(fields ...interface{}) Insert

Fields define what fields will be defined into the insert. If there is a set of fields already defined this method will replace them.

func (*InsertStatement) Into

func (insert *InsertStatement) Into(tableName string, fields ...interface{}) Insert

Into defines what table the data will be inserted on. `fields` are the same as `Fields` method.

func (*InsertStatement) OnConflict

func (insert *InsertStatement) OnConflict(callback func(InsertConflict)) Insert

OnConflict defines the ON CONFLICT clause for Postgres.

func (*InsertStatement) Placeholder

func (insert *InsertStatement) Placeholder(placeholder PlaceholderFormatFactory) Insert

Placeholder defines the placeholder format that should be used for this insert statement.

func (*InsertStatement) Returning

func (insert *InsertStatement) Returning(fields ...interface{}) Insert

Returning defines the RETURNING clause defined for Postgres.

func (*InsertStatement) Select

func (insert *InsertStatement) Select(callback func(Select)) Insert

Select defines a select that will be inserted.

Below an example of how this would be used in plain SQL. Ex:

INSERT INTO users (name, email, password) SELECT name, email, "12345" FROM employees;

Select and Values are not compatible. Whenever there is a select defined, Values should be ignored.

func (*InsertStatement) Suffix

func (insert *InsertStatement) Suffix(suffix string, args ...interface{}) Insert

Suffix defines a suffix that will be appended at the end of the insert clause. This can be used to extend the uses (like "ON DUPLICATE KEY" on MySQL) for other database technologies.

func (*InsertStatement) ToSQL

func (insert *InsertStatement) ToSQL() (string, []interface{}, error)

ToSQL generates the SQL and returns it, alongside its params.

func (*InsertStatement) ToSQLFast

func (insert *InsertStatement) ToSQLFast(sb SQLWriter, args *[]interface{}) error

ToSQLFast generates the SQL and returns it, alongside its params.

func (*InsertStatement) Values

func (insert *InsertStatement) Values(values ...interface{}) Insert

Values define what data will be inserted. Values will always append the information.

If you are inserting multiple records at once, just call Values as many times you want to. The only requirement is that the len of the total values should be multiple of the amount of fields that were defined.

Example:

i := new(InsertStatement)
i.Into("users", "name", "email", "password").Values("Name 1", "email1@email.com", "12345") // Adding one record
i.Values("Name 2", "email2@email.com", "54321") // Adding another record
i.Values("Name 3", "email3@email.com", "13425", "Name 4", "email4@email.com", "52431") // Adding more two records

type InsertUpdate

type InsertUpdate interface {
	Set(fieldsAndValues ...interface{}) InsertUpdate
	Where(condition string, args ...interface{}) InsertUpdate
	WhereClause(conditions ...FastSqlizer) InsertUpdate
}

type Join

type Join interface {
	FastSqlizer

	// Type defines the type of the Join. Ex: INNER, LEFT, OUTER, etc.
	Type(joinType string) Join

	// On define the on criteria based on a condition.
	On(condition string, params ...interface{}) Select

	// OnClause define the on criteria based on Sqlizers.
	OnClause(criteria ...FastSqlizer) Select

	// Using defines the using directive.
	Using(fields ...interface{}) Select
}

Join represents a SQL Join.

func NewJoinClause

func NewJoinClause(table ...string) Join

type JoinClause

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

JoinClause is the default implementation for the Join interface.

func (*JoinClause) On

func (join *JoinClause) On(condition string, params ...interface{}) Select

On define the on criteria.

func (*JoinClause) OnClause

func (join *JoinClause) OnClause(criteria ...FastSqlizer) Select

OnClause define the on criteria.

func (*JoinClause) ToSQLFast

func (join *JoinClause) ToSQLFast(sb SQLWriter, args *[]interface{}) error

ToSQLFast generates the SQL and returns it, alongside its params.

func (*JoinClause) Type

func (join *JoinClause) Type(joinType string) Join

Type defines the type of the Join. Ex: INNER, LEFT, OUTER, etc.

func (*JoinClause) Using

func (join *JoinClause) Using(fields ...interface{}) Select

Using defines the using directive.

type OrderBy

type OrderBy interface {
	FastSqlizer

	// Asc adds fields to the SQL ORDER BY clause on an ascending order.
	Asc(fields ...interface{}) OrderBy

	// Desc adds fields to the SQL ORDER BY clause on an descending order.
	Desc(fields ...interface{}) OrderBy
}

OrderBy represents a SQL GROUP BY clause.

type OrderByClause

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

func (*OrderByClause) Asc

func (orderBy *OrderByClause) Asc(fields ...interface{}) OrderBy

Asc adds fields to the SQL ORDER BY clause on an ascending order.

func (*OrderByClause) Desc

func (orderBy *OrderByClause) Desc(fields ...interface{}) OrderBy

Desc adds fields to the SQL ORDER BY clause on an descending order.

func (*OrderByClause) ToSQLFast

func (orderBy *OrderByClause) ToSQLFast(sb SQLWriter, args *[]interface{}) error

ToSQLFast generates the SQL and returns it, alongside its params.

type PlaceholderFormatFactory

type PlaceholderFormatFactory interface {
	// Wrap wraps the given `SQLWriter` into the placeholder replacer.
	Wrap(writer SQLWriter) SQLWriter

	// Put aims to return the wrapper to a pool. Of course, it depends on the factory implementation.
	Put(writer SQLWriter)
}

PlaceholderFormatFactory enables the system to use different placeholder formats. It wraps the SQLWriter into another that will replace ? by the desired placeholder.

type SQLWriter

type SQLWriter interface {
	io.Writer
	io.ByteWriter
	io.StringWriter
	fmt.Stringer
}

SQLWriter is an interface that abstracts a buffer used to write SQLs. This approach is used to enable placeholder replacers to be used by wrapping this `SQLWriter` into another structure. Check `placeholders.go` for more information.

type Select

type Select interface {
	FastSqlizer
	Sqlizer

	// Select defines the fields that will be returned by the query.
	Select(fields ...interface{}) Select

	// AddSelect adds fields to the existing list of fields that will be selected.
	AddSelect(fields ...interface{}) Select

	// Distinct enables the SQL SELECT DISTINCT clause.
	Distinct() Select

	// From defines the SQL SELECT FROM clause.
	From(table ...string) Select

	// As defines alias for the table.
	As(tableAlias string) Select

	// JoinClause adds a JOIN to the select.
	JoinClause(joinType string, tableName ...string) Join

	// InnerJoin adds a INNER JOIN to the select.
	InnerJoin(tableName ...string) Join

	// OuterJoin adds a OUTER JOIN to the select.
	OuterJoin(tableName ...string) Join

	// LeftJoin adds a LEFT JOIN to the select.
	LeftJoin(tableName ...string) Join

	// RightJoin adds a LEFT JOIN to the select.
	RightJoin(tableName ...string) Join

	// Where adds a criteria for the select.
	Where(condition string, args ...interface{}) Select

	// WhereCriteria adds a criteria for the select.
	WhereCriteria(criteria ...FastSqlizer) Select

	// GroupBy adds a SQL GROUP BY clause and returns the Query itself. For more options (like HAVING) use `GroupByX`.
	GroupBy(fields ...interface{}) Select

	// GroupByX adds a SQL GROUP BY clause and returns the GroupBy itself for further configuration.
	GroupByX(callback func(groupBy GroupBy)) Select

	// OrderBy adds a SQL GROUP BY clause and returns the Query itself. For more options (like HAVING) use `OrderByX`.
	OrderBy(fields ...interface{}) Select

	// OrderByX adds a SQL GROUP BY clause and returns the OrderBy itself for further configuration.
	OrderByX(callback func(orderBy OrderBy)) Select

	// Limit defines the SQL LIMIT clause.
	Limit(limits ...interface{}) Select

	// Offset defines the SQL OFFSET clause.
	Offset(offset interface{}) Select

	// Placeholder defines what placeholder format is going to be used for this query.
	//
	// Usually it will be automatically defined by the `Builder`.
	Placeholder(placeholder PlaceholderFormatFactory) Select

	// CountQuery copies the current `Select` replacing all fields by `count`. If no `count` is given, it uses
	// `COUNT(*)` as default.
	//
	// The returned `Select` also has their `Limit` and `Offset` reset to none.
	CountQuery(count ...interface{}) Select
}

Select represents a SQL SELECT statement.

type SelectStatement

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

func (*SelectStatement) AddSelect

func (s *SelectStatement) AddSelect(fields ...interface{}) Select

AddSelect adds fields to the existing list of fields that will be selected.

func (*SelectStatement) As

func (s *SelectStatement) As(tableAlias string) Select

As defines alias for the table.

func (*SelectStatement) CountQuery

func (s *SelectStatement) CountQuery(count ...interface{}) Select

CountQuery copies the current `Select` replacing all fields by `count`. If no `count` is given, it uses `COUNT(*)` as default.g

The returned `Select` also has their `Limit` and `Offset` reset to none.

func (*SelectStatement) Distinct

func (s *SelectStatement) Distinct() Select

Distinct enables the SQL SELECT DISTINCT clause.

func (*SelectStatement) From

func (s *SelectStatement) From(table ...string) Select

From defines the SQL SELECT FROM clause.

func (*SelectStatement) GroupBy

func (s *SelectStatement) GroupBy(fields ...interface{}) Select

GroupBy adds a SQL GROUP BY clause and returns the Query itself. For more options (like HAVING) use `GroupByX`.

func (*SelectStatement) GroupByX

func (s *SelectStatement) GroupByX(callback func(GroupBy)) Select

GroupByX adds a SQL GROUP BY clause and returns the GroupBy itself for further configuration.

func (*SelectStatement) InnerJoin

func (s *SelectStatement) InnerJoin(tableName ...string) Join

InnerJoin adds a INNER JOIN to the select.

func (*SelectStatement) JoinClause

func (s *SelectStatement) JoinClause(joinType string, tableName ...string) Join

JoinClause adds a JOIN to the select.

func (*SelectStatement) LeftJoin

func (s *SelectStatement) LeftJoin(tableName ...string) Join

LeftJoin adds a LEFT JOIN to the select.

func (*SelectStatement) Limit

func (s *SelectStatement) Limit(limits ...interface{}) Select

Limit defines the SQL LIMIT clause.

func (*SelectStatement) Offset

func (s *SelectStatement) Offset(offset interface{}) Select

Offset defines the SQL OFFSET clause.

func (*SelectStatement) OrderBy

func (s *SelectStatement) OrderBy(fields ...interface{}) Select

OrderBy adds a SQL GROUP BY clause and returns the Query itself. For more options (like HAVING) use `OrderByX`.

func (*SelectStatement) OrderByX

func (s *SelectStatement) OrderByX(callback func(orderBy OrderBy)) Select

OrderByX adds a SQL GROUP BY clause and returns the OrderBy itself for further configuration.

func (*SelectStatement) OuterJoin

func (s *SelectStatement) OuterJoin(tableName ...string) Join

OuterJoin adds a OUTER JOIN to the select.

func (*SelectStatement) Placeholder

func (s *SelectStatement) Placeholder(placeholder PlaceholderFormatFactory) Select

Placeholder defines what placeholder format is going to be used for this query.

Usually it will be automatically defined by the `Builder`.

func (*SelectStatement) RightJoin

func (s *SelectStatement) RightJoin(tableName ...string) Join

RightJoin adds a LEFT JOIN to the select.

func (*SelectStatement) Select

func (s *SelectStatement) Select(fields ...interface{}) Select

Select defines the fields that will be returned by the query.

func (*SelectStatement) ToSQL

func (s *SelectStatement) ToSQL() (string, []interface{}, error)

ToSQL generates the SQL and returns it, alongside its params.

func (*SelectStatement) ToSQLFast

func (s *SelectStatement) ToSQLFast(sb SQLWriter, args *[]interface{}) error

ToSQLFast generates the SQL and returns it, alongside its params.

func (*SelectStatement) Where

func (s *SelectStatement) Where(condition string, args ...interface{}) Select

Where adds a criteria for the select.

func (*SelectStatement) WhereCriteria

func (s *SelectStatement) WhereCriteria(criteria ...FastSqlizer) Select

WhereCriteria adds a criteria for the select.

type Sqlizer

type Sqlizer interface {
	// ToSQL generates the SQL and returns it, alongside its params.
	ToSQL() (string, []interface{}, error)
}

Sqlizer define anything that outputs a SQL in a simplified form.

type Update

type Update interface {
	Sqlizer
	FastSqlizer

	// Placeholder defines the placeholder format that should be used for this update statement.
	Placeholder(placeholder PlaceholderFormatFactory) Update

	// Table defines what table will be updated.
	Table(tableName ...string) Update

	// Set define what fields will be updated, alongside its values.
	//
	// The arguments are mixed with the values, in a alternating order. So, the
	// first argument should be the name of the field that will be updated, the second
	// will be its value. The third the name of the second field, the fourth its value
	// and so on. Hence, the number of arguments passed should, always, be even.
	Set(fieldAndValues ...interface{}) Update

	// Where appends a condition. If called multiples, the conditions will be appended.
	//
	// The conditions added will use the AND operator.
	Where(condition string, args ...interface{}) Update

	// WhereClause appends any Sqlizer to serve as where.
	//
	// The conditions added will use the AND operator.
	WhereClause(conditions ...FastSqlizer) Update
}

Update describes how a UPDATE will behave into the sqlf.

type UpdateStatement

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

func (*UpdateStatement) Placeholder

func (update *UpdateStatement) Placeholder(placeholder PlaceholderFormatFactory) Update

Placeholder defines the placeholder format that should be used for this delete statement.

func (*UpdateStatement) Set

func (update *UpdateStatement) Set(fieldAndValues ...interface{}) Update

Set define what fields will be updated, alongside its values.

The arguments are mixed with the values, in a alternating order. So, the first argument should be the name of the field that will be updated, the second will be its value. The third the name of the second field, the fourth its value and so on. Hence, the number of arguments passed should, always, be even.

func (*UpdateStatement) Table

func (update *UpdateStatement) Table(tableName ...string) Update

Table defines what table will be deleted.

func (*UpdateStatement) ToSQL

func (update *UpdateStatement) ToSQL() (string, []interface{}, error)

ToSQL generates the SQL and returns it, alongside its params.

func (*UpdateStatement) ToSQLFast

func (update *UpdateStatement) ToSQLFast(sb SQLWriter, args *[]interface{}) error

ToSQLFast generates the SQL and returns it, alongside its params.

func (*UpdateStatement) Where

func (update *UpdateStatement) Where(condition string, args ...interface{}) Update

Where appends a condition. If called multiples, the conditions will be appended.

The conditions added will use the AND operator.

func (*UpdateStatement) WhereClause

func (update *UpdateStatement) WhereClause(conditions ...FastSqlizer) Update

WhereClause appends any Sqlizer to serve as where.

The conditions added will use the AND operator.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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