lore: github.com/abrahambotros/lore Index | Files

package lore

import "github.com/abrahambotros/lore"

Index

Package Files

config.go model_interface.go query.go query_execute.go query_sql.go query_sql_and_execute.go query_sql_delete.go query_sql_insert.go query_sql_select.go query_sql_update.go query_sql_where.go sql_builder_interface.go sql_placeholder_formats.go squirrel_part.go squirrel_statement_builder.go utils.go

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.

func DeleteModelByPrimaryKey Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

func SetConfig(c *Config)

SetConfig sets the current config for all future LORE queries.

func SetConfigDefault Uses

func SetConfigDefault()

SetConfigDefault sets a default config for all future LORE queries.

func UpdateModelByPrimaryKey Uses

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 Uses

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 * ;`

func Where Uses

func Where(pred interface{}, args ...interface{}) *sqlPart

Where returns a new part as a Sqlizer interface, allowing this to be called in typical WHERE clauses via Squirrel.

type Config Uses

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 Uses

func GetConfig() *Config

GetConfig returns the current config object. If no config already exists, a default is given.

func GetConfigDefault Uses

func GetConfigDefault() *Config

GetConfigDefault returns the default config object.

type ModelInterface Uses

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 Uses

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

Query provides a generic, chainable query instance for callers to configure a specific query.

func NewQuery Uses

func NewQuery(modelInterface ModelInterface) *Query

NewQuery instantiates a new Query instance based on the given ModelInterface.

func (*Query) BuildSqlDelete Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

func (q *Query) BuildSqlSelectStar() squirrel.SelectBuilder

BuildSqlSelectStar wraps BuildSqlSelect as a SELECT * query. Alias for query.BuildSqlSelect("*").

func (*Query) BuildSqlUpdate Uses

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 Uses

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 Uses

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 Uses

func (q *Query) Execute(db *sqlx.DB) (numRowsAffected uint64, err error)

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 Uses

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 Uses

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 Uses

func (q *Query) SetSqlBuilder(sqlBuilder SqlBuilderInterface)

SetSqlBuilder sets the Query instance's internal sqlBuilder to the given SqlBuilderInterface instance.

func (*Query) ToSql Uses

func (q *Query) ToSql() (sql string, args []interface{}, err error)

ToSql wraps the ToSql method of the internal sqlBuilder, returning the SQL-with-args form of the query according to the internal sqlBuilder.

This requires that SetSqlBuilder has already been called, typically with a sqlBuilder/squirrel builder. If not, an error is returned.

type SQLPlaceholderFormat Uses

type SQLPlaceholderFormat squirrel.PlaceholderFormat

SQLPlaceholderFormat aliases squirrel.PlaceholderFormat.

type SqlBuilderInterface Uses

type SqlBuilderInterface interface {
    ToSql() (sql string, args []interface{}, err error)
}

SqlBuilderInterface provides a generic interface for SQL-izing. This mirrors the squirrel.Sqlizer interface.

type SquirrelStatementBuilder Uses

type SquirrelStatementBuilder SqlBuilderInterface

SquirrelStatementBuilder provides a generic interface for squirrel statement builders. Wraps SqlBuilderInterface.

Package lore imports 6 packages (graph). Updated 2017-10-23. Refresh now. Tools for package owners.