Documentation ¶
Index ¶
- Constants
- Variables
- func DeleteModelByPrimaryKey(mi ModelInterface, db *sqlx.DB, resultSinglePtr interface{}) (found bool, err error)
- func DeleteModelsWhere(mi ModelInterface, db *sqlx.DB, where *sqlPart, resultListPtr interface{}) (numRowsAffected uint64, err error)
- func InsertNewModel(mi ModelInterface, db *sqlx.DB, resultSinglePtr interface{}) error
- func SelectModelByPrimaryKey(mi ModelInterface, db *sqlx.DB, resultSinglePtr interface{}) (found bool, err error)
- func SelectModelWhere(mi ModelInterface, db *sqlx.DB, where *sqlPart, resultSinglePtr interface{}) (found bool, err error)
- func SelectModelsWhere(mi ModelInterface, db *sqlx.DB, where *sqlPart, limit *uint64, ...) (numRowsAffected uint64, err error)
- func SetConfig(c *Config)
- func SetConfigDefault()
- func UpdateModelByPrimaryKey(mi ModelInterface, db *sqlx.DB, resultSinglePtr interface{}) (found bool, err error)
- func UpdateSetMapWhere(mi ModelInterface, db *sqlx.DB, m map[string]interface{}, where *sqlPart, ...) (numRowsAffected uint64, err error)
- func Where(pred interface{}, args ...interface{}) *sqlPart
- type Config
- type ModelInterface
- type Query
- func (q *Query) BuildSqlDelete() squirrel.DeleteBuilder
- func (q *Query) BuildSqlDeleteModelByPrimaryKey() (squirrel.DeleteBuilder, error)
- func (q *Query) BuildSqlInsert() squirrel.InsertBuilder
- func (q *Query) BuildSqlInsertColumnsAndValues(columns []string, values []interface{}) squirrel.InsertBuilder
- func (q *Query) BuildSqlInsertModel() (squirrel.InsertBuilder, error)
- func (q *Query) BuildSqlSelect(columns ...string) squirrel.SelectBuilder
- func (q *Query) BuildSqlSelectStar() squirrel.SelectBuilder
- func (q *Query) BuildSqlUpdate() squirrel.UpdateBuilder
- func (q *Query) BuildSqlUpdateModelByPrimaryKey() (squirrel.UpdateBuilder, error)
- func (q *Query) BuildSqlUpdateSetMap(m map[string]interface{}) squirrel.UpdateBuilder
- func (q *Query) Execute(db *sqlx.DB) (numRowsAffected uint64, err error)
- func (q *Query) ExecuteThenParseList(db *sqlx.DB, resultListPtr interface{}) (numRowsAffected uint64, err error)
- func (q *Query) ExecuteThenParseSingle(db *sqlx.DB, resultSinglePtr interface{}) (found bool, err error)
- func (q *Query) SetSqlBuilder(sqlBuilder SqlBuilderInterface)
- func (q *Query) ToSql() (sql string, args []interface{}, err error)
- type SQLPlaceholderFormat
- type SqlBuilderInterface
- type SquirrelStatementBuilder
Constants ¶
const ( // STAR provides a hard-coded constant string representing the common star/* character in SQL. STAR string = "*" // RETURNING_STAR provides a hard-coded constant string representing the common "RETURNING *" // clause in SQL. RETURNING_STAR string = "RETURNING *" )
Variables ¶
var SQLPlaceholderFormatDollar = squirrel.Dollar
SQLPlaceholderFormatDollar aliases squirrel.Dollar.
var SQLPlaceholderFormatQuestion = squirrel.Question
SQLPlaceholderFormatQuestion aliases squirrel.Question.
Functions ¶
func DeleteModelByPrimaryKey ¶
func DeleteModelByPrimaryKey(mi ModelInterface, db *sqlx.DB, resultSinglePtr interface{}) (found bool, err error)
DeleteModelByPrimaryKey builds and executes a SQL statement on the db similar to the following SQL, scanning the result into resultSinglePtr. This is essentially a convenience wrapper around BuildSqlDeleteModelByPrimaryKey and ExecuteThenParseSingle.
`DELETE FROM <table> WHERE <primary field key> = <primary field value> RETURNING * ;`
func DeleteModelsWhere ¶
func DeleteModelsWhere(mi ModelInterface, db *sqlx.DB, where *sqlPart, resultListPtr interface{}) (numRowsAffected uint64, err error)
DeleteModelsWhere builds and executes a SQL statement on the db similar to the following SQL, scanning the result into resultListPtr. This is essentially a convenience wrapper around BuildSqlDelete and ExecuteThenParseList, applying the WHERE clause accordingly.
Note that where is a pointer; if no where is desired, pass nil instead. When desired, this is typically something like a squirrel.Eq instance, etc. For your convenience, you can use lore.Where to build a single Where-ish object around a squirrel.Eq/squirrel.Gt/etc.
`DELETE FROM <table> WHERE <where conditions> RETURNING * ;`
func InsertNewModel ¶
func InsertNewModel(mi ModelInterface, db *sqlx.DB, resultSinglePtr interface{}) error
InsertNewModel builds and executes a SQL statement on the db similar to the following SQL, scanning the result into resultSinglePtr. This is essentially a convenience wrapper around BuildSqlInsertModel and ExecuteThenParseSingle.
`INSERT INTO <table> (<columns from DbFieldMap>) VALUES (<values from DbFieldMap>) RETURNING * ;`
func SelectModelByPrimaryKey ¶
func SelectModelByPrimaryKey(mi ModelInterface, db *sqlx.DB, resultSinglePtr interface{}) (found bool, err error)
SelectModelByPrimaryKey builds and executes a SQL statement on the db similar to the following SQL, scanning the result into resultSinglePtr. This is essentially a convenience wrapper around BuildSqlSelectStar and ExecuteThenParseSingle.
`SELECT * FROM <table> WHERE <primary field key> = <primary field value> LIMIT 1;`
func SelectModelWhere ¶
func SelectModelWhere(mi ModelInterface, db *sqlx.DB, where *sqlPart, resultSinglePtr interface{}) (found bool, err error)
SelectModelWhere builds and executes a SQL statement on the db similar to the following SQL, scanning the result into resultSinglePtr. This is essentially a convenience wrapper around BuildSqlSelectStar and ExecuteThenParseSingle, applying the WHERE clause accordingly.
Note that where is a pointer; if no where is desired, pass nil instead. When desired, this is typically something like a squirrel.Eq instance, etc. For your convenience, you can use lore.Where to build a single Where-ish object around a squirrel.Eq/squirrel.Gt/etc.
`SELECT * FROM <table> WHERE <where conditions> LIMIT 1;`
func SelectModelsWhere ¶
func SelectModelsWhere(mi ModelInterface, db *sqlx.DB, where *sqlPart, limit *uint64, resultListPtr interface{}) (numRowsAffected uint64, err error)
SelectModelsWhere is analogous to SelectModelWhere, but wraps ExecuteThenParseList instead of ExecuteThenParseSingle, and allows appying a LIMIT clause too.
Note that limit is a pointer here - if nil is supplied, no limit is set on the SQL statement; otherwise, the underlying limit uint64 is applied.
`SELECT * FROM <table> WHERE <where conditions> LIMIT <limit>;`
func SetConfig ¶
func SetConfig(c *Config)
SetConfig sets the current config for all future LORE queries.
func SetConfigDefault ¶
func SetConfigDefault()
SetConfigDefault sets a default config for all future LORE queries.
func UpdateModelByPrimaryKey ¶
func UpdateModelByPrimaryKey(mi ModelInterface, db *sqlx.DB, resultSinglePtr interface{}) (found bool, err error)
UpdateModelByPrimaryKey builds and executes a SQL statement on the db similar to the following SQL, scanning the result into resultSinglePtr. This is essentially a convenience wrapper around BuildSqlUpdateModelByPrimaryKey and ExecuteThenParseSingle.
`UPDATE <table> SET <columns and values from DbFieldMap>
WHERE <primary field key> = <primary field value> RETURNING * ;`
func UpdateSetMapWhere ¶
func UpdateSetMapWhere(mi ModelInterface, db *sqlx.DB, m map[string]interface{}, where *sqlPart, resultListPtr interface{}) (numRowsAffected uint64, err error)
UpdateSetMapWhere builds and executes a SQL statement on the db similar to the following SQL, scanning the result into resultListPtr. This is essentially a convenience wrapper around BuildSqlUpdateSetMap and ExecuteThenParseList, applying the WHERE clause accordingly.
Note that where is a pointer; if no where is desired, pass nil instead. When desired, this is typically something like a squirrel.Eq instance, etc. For your convenience, you can use lore.Where to build a single Where-ish object around a squirrel.Eq/squirrel.Gt/etc.
`UPDATE <table> SET <columns and values from map> WHERE <where conditions> RETURNING * ;`
Types ¶
type Config ¶
type Config struct { /* DB driver's placeholder format for injecting query parameters via SQL. */ SQLPlaceholderFormat SQLPlaceholderFormat }
Config provides a struct for configuring all LORE queries.
func GetConfig ¶
func GetConfig() *Config
GetConfig returns the current config object. If no config already exists, a default is given.
func GetConfigDefault ¶
func GetConfigDefault() *Config
GetConfigDefault returns the default config object.
type ModelInterface ¶
type ModelInterface interface { /* DbTableName provides the name of the corresponding table in the database for this model. */ DbTableName() string /* DbFieldMap builds a map for this model instance from field/column name to this instance's current value for that field/column. Note that this should NOT include auto-managed db fields, such as SERIAL/AUTO-INCREMENTING KEYS, unless you want to try to specifically override these each time (typically not the case). This is used to provide Insert and Update functionality convenience by providing all of the columns and values to write into the table row. */ DbFieldMap() map[string]interface{} /* DbPrimaryFieldKey returns a string indicating the column name of the primary field of the model. This is used for Update and Delete queries for the WHERE condition. If the implementing model does not have such a field, an empty string can be returned, which will result in an error being thrown if any methods are called that require a non-empty field (such as an UPDATE that should have a non-empty WHERE condition for updating by primary key). If multiple columns define the primary field, you will have to implement the condition yourself instead. */ DbPrimaryFieldKey() string /* DbPrimaryFieldValue returns the current value of the primary field of the model. This is used for Update and Delete queries for the WHERE condition in conjunction with DbPrimaryFieldKey. If the implementing model does not have such a primary field, nil can be returned. Note that an error will be thrown if the DbPrimaryFieldKey method returns an empty string (see DbPrimaryFieldKey), but an error is not necessarily thrown if DbPrimaryFieldKey is non-empty and DbPrimaryFieldValue is nil (i.e., nil is a valid value to set for a primary field value if you really want). */ DbPrimaryFieldValue() interface{} }
ModelInterface provides a generic interface for enabling external models to interface with internal machinery here.
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query provides a generic, chainable query instance for callers to configure a specific query.
func NewQuery ¶
func NewQuery(modelInterface ModelInterface) *Query
NewQuery instantiates a new Query instance based on the given ModelInterface.
func (*Query) BuildSqlDelete ¶
func (q *Query) BuildSqlDelete() squirrel.DeleteBuilder
BuildSqlDelete provides the entrypoint for specializing a generic Query as a DELETE query on the table for the given ModelInterface. This directly returns a new squirrel.DeleteBuilder that can be placed back into the Query instance via SetSqlBuilder; the underlying SQL has the form: `DELETE FROM <DbTableName>`.
func (*Query) BuildSqlDeleteModelByPrimaryKey ¶
func (q *Query) BuildSqlDeleteModelByPrimaryKey() (squirrel.DeleteBuilder, error)
BuildSqlDeleteModelByPrimaryKey wraps BuildSqlDelete to perform the delete on the table row with the matching primary key/value of this Query's ModelInterface's model instance. Alias for `query.BuildSqlDelete().Where(<primary key and value>)`.
func (*Query) BuildSqlInsert ¶
func (q *Query) BuildSqlInsert() squirrel.InsertBuilder
BuildSqlInsert provides the entrypoint for specializing a generic Query as an INSERT query on the table for the given ModelInterface. This directly returns a new squirrel.InsertBuilder that can be placed back into the Query instance via SetSqlBuilder; the underlying SQL has the form: "INSERT INTO <DbTableName>".
func (*Query) BuildSqlInsertColumnsAndValues ¶
func (q *Query) BuildSqlInsertColumnsAndValues(columns []string, values []interface{}) squirrel.InsertBuilder
BuildSqlInsertColumnsAndValues wraps BuildSqlInsert with the given columns and values. Alias for query.BuildSqlInsert().Columns(<columns...>).Values(<values...>).
func (*Query) BuildSqlInsertModel ¶
func (q *Query) BuildSqlInsertModel() (squirrel.InsertBuilder, error)
BuildSqlInsertModel wraps BuildSqlInsert with the given ModelInterface's DbFieldMap as the INSERT query's columns and values. Uses the ModelInterface the Query was originally created with. Alias for query.BuildSqlInsertColumnsAndValues(<model's DbFieldMap keys>, <model's DbFieldMap values>).
func (*Query) BuildSqlSelect ¶
func (q *Query) BuildSqlSelect(columns ...string) squirrel.SelectBuilder
BuildSqlSelect provides the entrypoint for specializing a generic Query as a SELECT query on the table for the given ModelInterface. This directly returns a new squirrel.SelectBuilder that can be placed back into the Query instance via SetSqlBuilder; the underlying SQL has the form: "SELECT <columns> FROM <DbTableName>".
func (*Query) BuildSqlSelectStar ¶
func (q *Query) BuildSqlSelectStar() squirrel.SelectBuilder
BuildSqlSelectStar wraps BuildSqlSelect as a SELECT * query. Alias for query.BuildSqlSelect("*").
func (*Query) BuildSqlUpdate ¶
func (q *Query) BuildSqlUpdate() squirrel.UpdateBuilder
BuildSqlUpdate provides the entrypoint for specializing a generic Query as an UPDATE query on the table for the given ModelInterface. This directly returns a new squirrel.UpdateBuilder that can be placed back into the Query instance via SetSqlBuilder; the underlying SQL has the form: `UPDATE <DbTableName>`.
func (*Query) BuildSqlUpdateModelByPrimaryKey ¶
func (q *Query) BuildSqlUpdateModelByPrimaryKey() (squirrel.UpdateBuilder, error)
BuildSqlUpdateModelByPrimaryKey wraps BuildSqlUpdate to perform the update with the given columns and values defined by the Query's ModelInterface's DbFieldMap on the table row with the matching primary key/value for this ModelInterface's model instance. Alias for `query.BuildSqlUpdate().Where(<primary key and value>).Set(<columns and values according to DbFieldMap>)`.
func (*Query) BuildSqlUpdateSetMap ¶
func (q *Query) BuildSqlUpdateSetMap(m map[string]interface{}) squirrel.UpdateBuilder
BuildSqlUpdateSetMap wraps BuildSqlUpdate with the columns and values in the given map. Alias for `query.BuildSqlUpdate().SetMap(<map of columns to values>)`.
func (*Query) Execute ¶
Execute wraps sqlx.DB.Exec, and does not parse the result into any struct. However, it still returns the basic info of numRowsAffected IN SOME CASES (see below).
NOTE: As mentioned at http://jmoiron.github.io/sqlx/#exec access to numRowsAffected (via sqlx.Result.RowsAffected) is db-driver-dependent.
func (*Query) ExecuteThenParseList ¶
func (q *Query) ExecuteThenParseList(db *sqlx.DB, resultListPtr interface{}) (numRowsAffected uint64, err error)
ExecuteThenParseList wraps sqlx.DB.Select to execute the query and then parse the result into the given list of structs in resultSinglePtr. Scanning the db results into the result interface is done according to typical sqlx scanning behavior for sqlx.DB.Select.
Note that resultListPtr should be a POINTER TO A LIST OF STRUCTS that you want to scan the results into. An error will be returned if resultListPtr is not detected as a pointer to a list.
This function returns numRowsAffected equal to the length derived from the result slice after the query has been run.
NOTE: Any callers of this function should ensure the underlying query is appropriately bounded, as sqlx.DB.Select will load the entire result set into memory at once; see http://jmoiron.github.io/sqlx/#getAndSelect for more information.
func (*Query) ExecuteThenParseSingle ¶
func (q *Query) ExecuteThenParseSingle(db *sqlx.DB, resultSinglePtr interface{}) (found bool, err error)
ExecuteThenParseSingle wraps sqlx.DB.Get to execute the query and then parse the result into the given single struct in resultSinglePtr. Scanning the db results into the result interface is done according to typical sqlx scanning behavior for sqlx.DB.Get.
When sql.ErrNoRows is encountered by the underlying sqlx.DB.Get query (any time no matching row is found for this query), this function returns with found equal to false (naturally), but DOES NOT RETURN ANY ERROR (even though the sql package did).
Note that resultSinglePtr should be a POINTER TO A SINGLE STRUCT that you want to scan the results into. An error will be returned if resultSinglePtr is not detected as a pointer to a single struct.
func (*Query) SetSqlBuilder ¶
func (q *Query) SetSqlBuilder(sqlBuilder SqlBuilderInterface)
SetSqlBuilder sets the Query instance's internal sqlBuilder to the given SqlBuilderInterface instance.
type SQLPlaceholderFormat ¶
type SQLPlaceholderFormat squirrel.PlaceholderFormat
SQLPlaceholderFormat aliases squirrel.PlaceholderFormat.
type SqlBuilderInterface ¶
SqlBuilderInterface provides a generic interface for SQL-izing. This mirrors the squirrel.Sqlizer interface.
type SquirrelStatementBuilder ¶
type SquirrelStatementBuilder SqlBuilderInterface
SquirrelStatementBuilder provides a generic interface for squirrel statement builders. Wraps SqlBuilderInterface.