Documentation
¶
Index ¶
- Variables
- func FieldDataType(fieldName, fieldType string) (dataType string)
- func StringWithArg(str string, arg interface{}) stringWithArg
- func ToPlural(in string) string
- func ToPluralUnderscore(in string) string
- func ToTableName(object interface{}) (name string)
- func ToUnderscore(str string) string
- type Changes
- type DeleteSQL
- func (s *DeleteSQL) Reload() *DeleteSQL
- func (s *DeleteSQL) Returning(expressions ...string) *DeleteSQL
- func (s *DeleteSQL) String() string
- func (s *DeleteSQL) Tap(funcs ...func(*DeleteSQL) *DeleteSQL) *DeleteSQL
- func (s *DeleteSQL) Using(list ...string) *DeleteSQL
- func (s *DeleteSQL) WHERE(args ...interface{}) *DeleteSQL
- func (s *DeleteSQL) Where(condition string, args ...interface{}) *DeleteSQL
- type Field
- type InsertSQL
- func (s *InsertSQL) DoNothing() *InsertSQL
- func (s *InsertSQL) DoUpdate(expressions ...string) *InsertSQL
- func (s *InsertSQL) DoUpdateAll() *InsertSQL
- func (s *InsertSQL) DoUpdateAllExcept(fields ...string) *InsertSQL
- func (s *InsertSQL) OnConflict(targets ...string) *InsertSQL
- func (s *InsertSQL) Returning(expressions ...string) *InsertSQL
- func (s InsertSQL) String() string
- func (s *InsertSQL) Tap(funcs ...func(*InsertSQL) *InsertSQL) *InsertSQL
- type Model
- func (m Model) AddTableName(fields ...string) []string
- func (m Model) Assign(target interface{}, lotsOfChanges ...interface{}) (out []interface{}, err error)
- func (m Model) Changes(in RawChanges) (out Changes)
- func (m *Model) Clone() *Model
- func (m Model) ColumnDataTypes() map[string]string
- func (m Model) Columns() []string
- func (m *Model) Connection() db.DB
- func (m Model) Count(optional ...string) (count int, err error)
- func (m Model) CreatedAt() Changes
- func (m Model) Delete() *DeleteSQL
- func (m Model) DropSchema() string
- func (m Model) Exists() (exists bool, err error)
- func (m Model) FieldByName(name string) *Field
- func (m Model) FieldChanges(in RawChanges) (out Changes)
- func (m Model) Fields() []string
- func (m Model) Find(options ...interface{}) *SelectSQL
- func (m Model) Insert(lotsOfChanges ...interface{}) *InsertSQL
- func (m Model) JSONBFields() []string
- func (m Model) Join(expressions ...string) *SelectSQL
- func (m Model) MustAssign(i interface{}, lotsOfChanges ...interface{}) []interface{}
- func (m Model) MustCount(optional ...string) int
- func (m Model) MustExists() bool
- func (m Model) MustTransaction(block TransactionBlock)
- func (m Model) MustTransactionCtx(ctx context.Context, block TransactionBlock)
- func (m Model) New() reflect.Value
- func (m Model) NewSQL(sql string, values ...interface{}) *SQL
- func (m Model) NewSlice() reflect.Value
- func (m Model) Permit(fieldNames ...string) *ModelWithPermittedFields
- func (m Model) PermitAllExcept(fieldNames ...string) *ModelWithPermittedFields
- func (m *Model) Quiet() *Model
- func (m Model) Schema() string
- func (m Model) Select(fields ...string) *SelectSQL
- func (m *Model) SetColumnNamer(namer func(string) string) *Model
- func (m *Model) SetConnection(db db.DB) *Model
- func (m *Model) SetLogger(logger logger.Logger) *Model
- func (m *Model) SetOptions(options ...interface{}) *Model
- func (m Model) String() string
- func (m Model) TableName() string
- func (mi Model) ToColumnName(in string) string
- func (m Model) Transaction(block TransactionBlock) error
- func (m Model) TransactionCtx(ctx context.Context, block TransactionBlock) (err error)
- func (m Model) TypeName() string
- func (m Model) Update(lotsOfChanges ...interface{}) *UpdateSQL
- func (m Model) UpdatedAt() Changes
- func (m Model) WHERE(args ...interface{}) *SelectSQL
- func (m Model) Where(condition string, args ...interface{}) *SelectSQL
- func (m *Model) WithoutFields(fieldNames ...string) *Model
- type ModelWithPermittedFields
- func (m ModelWithPermittedFields) Bind(ctx interface{ ... }, target interface{}) (Changes, error)
- func (m ModelWithPermittedFields) Filter(inputs ...interface{}) (out Changes)
- func (m ModelWithPermittedFields) MustBind(ctx interface{ ... }, target interface{}) Changes
- func (m ModelWithPermittedFields) PermittedFields() (out []string)
- func (mi ModelWithPermittedFields) ToColumnName(in string) string
- type RawChanges
- type SQL
- func (s SQL) AsDelete() *DeleteSQL
- func (s SQL) AsInsert(fields ...string) *InsertSQL
- func (s SQL) AsSelect(fields ...string) *SelectSQL
- func (s SQL) AsUpdate(changes ...interface{}) *UpdateSQL
- func (s SQL) Execute(dest ...interface{}) error
- func (s SQL) ExecuteCtx(ctx context.Context, dest ...interface{}) error
- func (s SQL) ExecuteCtxTx(ctx context.Context, tx Tx, dest ...interface{}) error
- func (s SQL) MustExecute(dest ...interface{})
- func (s SQL) MustExecuteCtx(ctx context.Context, dest ...interface{})
- func (s SQL) MustExecuteCtxTx(ctx context.Context, tx Tx, dest ...interface{})
- func (s SQL) MustQuery(target interface{})
- func (s SQL) MustQueryCtx(ctx context.Context, target interface{})
- func (s SQL) MustQueryCtxTx(ctx context.Context, tx Tx, target interface{})
- func (s SQL) MustQueryRow(dest ...interface{})
- func (s SQL) MustQueryRowCtx(ctx context.Context, dest ...interface{})
- func (s SQL) MustQueryRowCtxTx(ctx context.Context, tx Tx, dest ...interface{})
- func (s SQL) Query(target interface{}) error
- func (s SQL) QueryCtx(ctx context.Context, target interface{}) error
- func (s SQL) QueryCtxTx(ctx context.Context, tx Tx, target interface{}) error
- func (s SQL) QueryRow(dest ...interface{}) error
- func (s SQL) QueryRowCtx(ctx context.Context, dest ...interface{}) error
- func (s SQL) QueryRowCtxTx(ctx context.Context, tx Tx, dest ...interface{}) error
- func (s SQL) String() string
- func (s *SQL) Tap(funcs ...func(*SQL) *SQL) *SQL
- func (s SQL) Values() []interface{}
- type SelectSQL
- func (s *SelectSQL) Count(optional ...string) (count int, err error)
- func (s *SelectSQL) Delete() *DeleteSQL
- func (s *SelectSQL) Exists() (exists bool, err error)
- func (s *SelectSQL) Find(options ...interface{}) *SelectSQL
- func (s *SelectSQL) GroupBy(expressions ...string) *SelectSQL
- func (s *SelectSQL) Having(condition string, args ...interface{}) *SelectSQL
- func (s *SelectSQL) Join(expressions ...string) *SelectSQL
- func (s *SelectSQL) Limit(count interface{}) *SelectSQL
- func (s *SelectSQL) MustCount(optional ...string) int
- func (s *SelectSQL) MustExists() bool
- func (s *SelectSQL) Offset(start interface{}) *SelectSQL
- func (s *SelectSQL) OrderBy(expressions ...string) *SelectSQL
- func (s *SelectSQL) Reload() *SelectSQL
- func (s *SelectSQL) ReplaceSelect(old, new string) *SelectSQL
- func (s *SelectSQL) ResetJoin(expressions ...string) *SelectSQL
- func (s *SelectSQL) ResetSelect(expressions ...string) *SelectSQL
- func (s *SelectSQL) Select(expressions ...string) *SelectSQL
- func (s *SelectSQL) String() string
- func (s *SelectSQL) Tap(funcs ...func(*SelectSQL) *SelectSQL) *SelectSQL
- func (s *SelectSQL) Update(lotsOfChanges ...interface{}) *UpdateSQL
- func (s *SelectSQL) WHERE(args ...interface{}) *SelectSQL
- func (s *SelectSQL) Where(condition string, args ...interface{}) *SelectSQL
- type String
- type TransactionBlock
- type Tx
- type UpdateSQL
- func (s *UpdateSQL) Reload() *UpdateSQL
- func (s *UpdateSQL) Returning(expressions ...string) *UpdateSQL
- func (s *UpdateSQL) String() string
- func (s *UpdateSQL) Tap(funcs ...func(*UpdateSQL) *UpdateSQL) *UpdateSQL
- func (s *UpdateSQL) WHERE(args ...interface{}) *UpdateSQL
- func (s *UpdateSQL) Where(condition string, args ...interface{}) *UpdateSQL
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidTarget = errors.New("target must be pointer of a struct, slice or map") ErrNoConnection = errors.New("no connection") ErrNoSQL = errors.New("no sql statements to execute") ErrTypeAssertionFailed = errors.New("type assertion failed") )
var ( // DefaultColumnNamer is default column naming function used when // calling NewModel. Default is null, which uses field name as column // name. DefaultColumnNamer func(string) string = nil // DefaultColumnNamer is default table naming function used when // calling NewModel. Default is ToPlural, which converts table name to // its plural form. DefaultTableNamer func(string) string = ToPlural )
var AddTableName fieldsFunc = func(fields []string, tableName string) (out []string) { for _, field := range fields { if strings.Contains(field, ".") { out = append(out, field) continue } out = append(out, tableName+"."+field) } return }
Can be used in Find(), add table name to all field names.
var (
ErrMustBePointer = errors.New("must be pointer")
)
Functions ¶
func FieldDataType ¶ added in v1.7.0
FieldDataType generates PostgreSQL data type based on struct's field name and type. This is default function used when calling ColumnDataTypes() or Schema(). To use custom data type function, define "FieldDataType(fieldName, fieldType string) (dataType string)" function for your connection.
func StringWithArg ¶ added in v1.12.2
func StringWithArg(str string, arg interface{}) stringWithArg
func ToPlural ¶ added in v1.8.0
Convert a word to its plural form. Add "es" for "s" or "o" ending, "y" ending will be replaced with "ies", for other endings, add "s". For example, "product" will be converted to "products".
func ToPluralUnderscore ¶ added in v1.8.0
Convert a "CamelCase" word to its plural "snake_case" (underscore) form. For example, "PostComment" will be converted to "post_comments".
func ToTableName ¶
func ToTableName(object interface{}) (name string)
ToTableName returns table name of a struct. If struct has "TableName() string" receiver method, its return value is used. If name is empty and struct has a __TABLE_NAME__ field, its tag value is used. If it is still empty, struct's name is used. If name is still empty, "error_no_table_name" is returned.
func ToUnderscore ¶ added in v1.8.0
Convert "CamelCase" word to its "snake_case" (underscore) form. For example, "FullName" will be converted to "full_name".
Types ¶
type DeleteSQL ¶ added in v1.3.0
type DeleteSQL struct { *SQL // contains filtered or unexported fields }
DeleteSQL can be created with Model.NewSQL().AsDelete()
func (*DeleteSQL) Reload ¶ added in v1.3.0
Update SQL and values in the DeleteSQL object due to changes of conditions.
func (*DeleteSQL) WHERE ¶ added in v1.9.0
WHERE adds conditions to DELETE statement from variadic inputs.
The args parameter contains field name, operator, value tuples with each tuple consisting of three consecutive elements: the field name as a string, an operator symbol as a string (e.g. "=", ">", "<="), and the value to match against that field.
To generate a WHERE clause matching multiple fields, use more than one set of field/operator/value tuples in the args array. For example, WHERE("A", "=", 1, "B", "!=", 2) means "WHERE (A = 1) AND (B != 2)".
type Field ¶
type Field struct { Name string // struct field name ColumnName string // column name (or jsonb key name) in database ColumnType string // column type JsonName string // key name in json input and output Jsonb string // jsonb column name in database DataType string // data type in database Exported bool // false if field name is lower case (unexported) Strict bool // jsonb: raise json unmarshal error if set to true Parent string // parent struct name if anonymous is set }
type InsertSQL ¶ added in v1.3.0
type InsertSQL struct { *SQL // contains filtered or unexported fields }
InsertSQL can be created with Model.NewSQL().AsInsert()
func (*InsertSQL) DoNothing ¶ added in v1.3.0
Used with OnConflict(), adds ON CONFLICT DO NOTHING clause to INSERT INTO statement.
func (*InsertSQL) DoUpdate ¶ added in v1.3.0
Used with OnConflict(), adds custom expressions ON CONFLICT ... DO UPDATE SET ... clause to INSERT INTO statement.
func (*InsertSQL) DoUpdateAll ¶ added in v1.3.0
DoUpdateAll is like DoUpdate but update every field.
func (*InsertSQL) DoUpdateAllExcept ¶ added in v1.10.3
DoUpdateAllExcept is like DoUpdateAll but except some field names.
func (*InsertSQL) OnConflict ¶ added in v1.3.0
Used with DoNothing(), DoUpdate() or DoUpdateAll().
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is a database table and it is created from struct. Table name is inferred from struct's name, and converted to its the plural form using psql.TransformTableName by default. To use a different table name, define a __TABLE_NAME__ field in the struct and set the tag value as the table name, or add a "TableName() string" receiver method for the struct.
Column names are inferred from struct field names. To use a different column name, set the "column" tag for the field, or use SetColumnNamer() to define column namer function to transform all field names in this model.
func NewModel ¶
func NewModel(object interface{}, options ...interface{}) (m *Model)
Initialize a Model from a struct. For available options, see SetOptions().
func NewModelTable ¶
Initialize a Model by defining table name only. Useful if you are calling functions that don't need fields, for example:
psql.NewModelTable("users", conn).MustCount()
For available options, see SetOptions().
func (Model) AddTableName ¶ added in v1.12.0
AddTableName adds table name to fields.
func (Model) Assign ¶
func (m Model) Assign(target interface{}, lotsOfChanges ...interface{}) (out []interface{}, err error)
Assign changes to target object. Useful if you want to validate your struct.
func create(c echo.Context) error { var user models.User m := psql.NewModel(user, conn) changes := m.MustAssign( &user, m.Permit("Name").Filter(c.Request().Body), ) if err := c.Validate(user); err != nil { panic(err) } var id int m.Insert(changes...).Returning("id").MustQueryRow(&id) // ... }
func (Model) Changes ¶
func (m Model) Changes(in RawChanges) (out Changes)
Convert RawChanges to Changes. Keys are JSON key names. See FieldChanges().
m := psql.NewModel(struct { Age *int `json:"age"` }{}) m.Changes(map[string]interface{}{ "age": 99, })
func (Model) ColumnDataTypes ¶ added in v1.4.0
func (*Model) Connection ¶ added in v1.0.1
Return database connection for the Model.
func (Model) Delete ¶
Delete builds a DELETE statement. You can add extra clause (like WHERE, RETURNING) to the statement as the first argument. The rest arguments are for any placeholder parameters in the statement.
var ids []int psql.NewModelTable("reports", conn).Delete().Returning("id").MustQuery(&ids)
func (Model) DropSchema ¶
Generate DROP TABLE ("DROP TABLE IF EXISTS <table_name>;") SQL statement from a Model.
func (Model) Exists ¶
Create and execute a SELECT 1 AS one statement. Returns true if record exists, false if not exists.
func (Model) FieldByName ¶
Get field by struct field name, nil will be returned if no such field.
func (Model) FieldChanges ¶ added in v1.1.0
func (m Model) FieldChanges(in RawChanges) (out Changes)
Convert RawChanges to Changes. Keys are field names. See Changes().
func (Model) Fields ¶ added in v1.12.0
Fields returns field names of the Model. For JSONB fields, see JSONBFields().
func (Model) Find ¶
Create a SELECT query statement with all fields of a Model. If you want to use other data type than the type of struct passed in NewModel(), see Select().
// put results into a slice var users []models.User psql.NewModel(models.User{}, conn).Find().MustQuery(&users) // put results into a struct var user models.User psql.NewModel(models.User{}, conn).Find().Where("id = $1", 1).MustQuery(&user)
You can pass options to modify Find(). For example, Find(psql.AddTableName) adds table name to every field.
func (Model) Insert ¶
Insert builds an INSERT INTO statement with fields and values in the changes.
var id int m.Insert(changes...).Returning("id").MustQueryRow(&id)
Changes can be a list of field name and value pairs and can also be obtained from methods like Changes(), FieldChanges(), Assign(), Bind(), Filter().
m.Insert("FieldA", 123, "FieldB", "other").MustExecute()
func (Model) JSONBFields ¶ added in v1.12.0
JSONBFields returns JSONB field names of the Model.
func (Model) MustAssign ¶
func (m Model) MustAssign(i interface{}, lotsOfChanges ...interface{}) []interface{}
MustAssign is like Assign but panics if assign operation fails.
func (Model) MustExists ¶
MustExists is like Exists but panics if existence check operation fails. Returns true if record exists, false if not exists.
func (Model) MustTransaction ¶ added in v1.6.0
func (m Model) MustTransaction(block TransactionBlock)
MustTransaction starts a transaction, uses context.Background() internally and panics if transaction fails.
func (Model) MustTransactionCtx ¶ added in v1.6.0
func (m Model) MustTransactionCtx(ctx context.Context, block TransactionBlock)
MustTransactionCtx starts a transaction and panics if transaction fails.
func (Model) New ¶ added in v1.10.0
New returns a reflect.Value representing a pointer to a new zero value for model's struct type.
func (Model) NewSQL ¶ added in v1.3.0
Create new SQL with SQL statement as first argument, The rest arguments are for any placeholder parameters in the statement.
func (Model) NewSlice ¶ added in v1.10.0
NewSlice returns a reflect.Value representing a pointer to a new zero-initialized slice value for model's struct type.
func (Model) Permit ¶
func (m Model) Permit(fieldNames ...string) *ModelWithPermittedFields
Permits list of field names of a Model to limit Filter() which fields should be allowed for mass updating. If no field names are provided ("Permit()"), no fields are permitted.
func (Model) PermitAllExcept ¶
func (m Model) PermitAllExcept(fieldNames ...string) *ModelWithPermittedFields
Permits all available fields except provided of a Model to limit Filter() which fields should be allowed for mass updating. If no field names are provided ("PermitAllExcept()"), all available fields are permitted.
func (Model) Schema ¶
Generate CREATE TABLE SQL statement from a Model.
| Go Type | PostgreSQL Data Type | |------------------------------------------------|----------------------| | int8 / int16 / int32 / uint8 / uint16 / uint32 | integer | | int64 / uint64 / int / uint | bigint | | time.Time | timestamptz | | float32 / float64 / decimal.Decimal | numeric | | bool | boolean | | other | text |
You can use "dataType" tag to customize the data type. "NOT NULL" is added if the struct field is not a pointer. You can also set SQL statements before or after this statement by defining "BeforeCreateSchema() string" (for example the CREATE EXTENSION statement) or "AfterCreateSchema() string" (for example the CREATE INDEX statement) function for the struct. Set dataType to "-" to ignore this field in migration.
psql.NewModel(struct { __TABLE_NAME__ string `users` Id int Name string Age *int Numbers []int CreatedAt time.Time DeletedAt *time.Time `dataType:"timestamptz"` FullName string `jsonb:"meta"` NickName string `jsonb:"meta"` }{}).Schema() // CREATE TABLE users ( // id SERIAL PRIMARY KEY, // name text DEFAULT ''::text NOT NULL, // age bigint DEFAULT 0, // numbers bigint[] DEFAULT '{}' NOT NULL, // created_at timestamptz DEFAULT NOW() NOT NULL, // deleted_at timestamptz, // meta jsonb DEFAULT '{}'::jsonb NOT NULL // );
func (Model) Select ¶
Select is like Find but can choose what columns to retrieve.
To put results into a slice of strings:
var names []string psql.NewModelTable("users", conn).Select("name").OrderBy("id ASC").MustQuery(&names)
To put results into a slice of custom struct:
var users []struct { name string id int } psql.NewModelTable("users", conn).Select("name", "id").OrderBy("id ASC").MustQuery(&users)
To group results by the key:
var id2name map[int]string psql.NewModelTable("users", conn).Select("id", "name").MustQuery(&id2name)
If it is one-to-many, use slice as map's value:
var users map[[2]string][]struct { id int name string } psql.NewModelTable("users", conn).Select("country, city, id, name").MustQuery(&users)
func (*Model) SetColumnNamer ¶ added in v1.8.0
Change the column namer function for the Model.
func (*Model) SetConnection ¶
Set a database connection for the Model. ErrNoConnection is returned if no connection is set.
func (*Model) SetLogger ¶
Set the logger for the Model. Use logger.StandardLogger if you want to use Go's built-in standard logging package. By default, no logger is used, so the SQL statements are not printed to the console.
func (*Model) SetOptions ¶
SetOptions sets database connection (see SetConnection()) and/or logger (see SetLogger()).
func (Model) ToColumnName ¶ added in v1.8.0
Function to convert field name to name used in database.
func (Model) Transaction ¶ added in v1.6.0
func (m Model) Transaction(block TransactionBlock) error
Transaction starts a transaction, uses context.Background() internally.
func (Model) TransactionCtx ¶ added in v1.6.0
func (m Model) TransactionCtx(ctx context.Context, block TransactionBlock) (err error)
TransactionCtx starts a transaction.
func (Model) Update ¶
Update builds an UPDATE statement with fields and values in the changes.
var rowsAffected int m.Update(changes...).Where("user_id = $1", 1).MustExecute(&rowsAffected)
Changes can be a list of field name and value pairs and can also be obtained from methods like Changes(), FieldChanges(), Assign(), Bind(), Filter().
m.Update("FieldA", 123, "FieldB", "other").MustExecute()
func (Model) WHERE ¶ added in v1.9.0
Create a SELECT query statement with condition.
The args parameter contains field name, operator, value tuples with each tuple consisting of three consecutive elements: the field name as a string, an operator symbol as a string (e.g. "=", ">", "<="), and the value to match against that field.
To generate a WHERE clause matching multiple fields, use more than one set of field/operator/value tuples in the args array. For example, WHERE("A", "=", 1, "B", "!=", 2) means "WHERE (A = 1) AND (B != 2)".
func (Model) Where ¶ added in v1.3.0
Create a SELECT query statement with condition. Arguments should use positonal parameters like $1, $2. If only one argument is provided, "$?" in the condition will be replaced with the correct positonal parameter.
func (*Model) WithoutFields ¶ added in v1.8.3
WithoutFields returns a copy of the model without given fields.
type ModelWithPermittedFields ¶
type ModelWithPermittedFields struct { *Model // contains filtered or unexported fields }
func (ModelWithPermittedFields) Bind ¶
func (m ModelWithPermittedFields) Bind(ctx interface{ Bind(interface{}) error }, target interface{}) (Changes, error)
Bind data of permitted fields to target structure using echo.Context#Bind function. The "target" must be a pointer to struct.
// request with ?name=x&age=10 func list(c echo.Context) error { obj := struct { Name string `query:"name"` Age int `query:"age"` }{} m := psql.NewModel(obj) fmt.Println(m.Permit("Name").Bind(c, &obj)) fmt.Println(obj) // "Name" is "x" and "Age" is 0 (default), because only "Name" is permitted to change // ... }
func (ModelWithPermittedFields) Filter ¶
func (m ModelWithPermittedFields) Filter(inputs ...interface{}) (out Changes)
Filter keeps data of permitted fields set by Permit() from multiple inputs. Inputs can be RawChanges (map[string]interface{}) or JSON-encoded data (string, []byte or io.Reader), their keys must be fields' JSON names. Input can also be a struct. The "Changes" outputs can be arguments for Insert() or Update().
m := psql.NewModel(struct { Age *int `json:"age"` }{}) m.Permit("Age").Filter( psql.RawChanges{ "age": 10, }, map[string]interface{}{ "age": 20, }, `{"age": 30}`, []byte(`{"age": 40}`), strings.NewReader(`{"age": 50}`), struct{ Age int }{60}, ) // Age is 60
func (ModelWithPermittedFields) MustBind ¶
func (m ModelWithPermittedFields) MustBind(ctx interface{ Bind(interface{}) error }, target interface{}) Changes
MustBind is like Bind but panics if bind operation fails.
func (ModelWithPermittedFields) PermittedFields ¶
func (m ModelWithPermittedFields) PermittedFields() (out []string)
Returns list of permitted field names.
func (ModelWithPermittedFields) ToColumnName ¶ added in v1.8.0
Function to convert field name to name used in database.
type RawChanges ¶
type RawChanges map[string]interface{}
type SQL ¶ added in v1.3.0
type SQL struct {
// contains filtered or unexported fields
}
SQL can be created with Model.NewSQL()
func (SQL) AsInsert ¶ added in v1.3.0
Convert SQL to InsertSQL. The optional fields will be used in DoUpdateAll().
func (SQL) AsSelect ¶ added in v1.3.0
Convert SQL to SelectSQL. The optional fields will be used in Select().
func (SQL) AsUpdate ¶ added in v1.3.0
Convert SQL to UpdateSQL. The optional changes will be used in Reload().
func (SQL) Execute ¶ added in v1.3.0
Execute executes a query without returning any rows by an UPDATE, INSERT, or DELETE. You can get number of rows affected by providing pointer of int or int64 to the optional dest. For use cases, see Update().
func (SQL) ExecuteCtx ¶ added in v1.7.0
ExecuteCtx executes a query without returning any rows by an UPDATE, INSERT, or DELETE. You can get number of rows affected by providing pointer of int or int64 to the optional dest. For use cases, see Update().
func (SQL) ExecuteCtxTx ¶ added in v1.6.0
ExecuteCtxTx executes a query without returning any rows by an UPDATE, INSERT, or DELETE. You can get number of rows affected by providing pointer of int or int64 to the optional dest. For use cases, see Update().
func (SQL) MustExecute ¶ added in v1.3.0
func (s SQL) MustExecute(dest ...interface{})
MustExecute is like Execute but panics if execute operation fails.
func (SQL) MustExecuteCtx ¶ added in v1.7.0
MustExecuteCtx is like ExecuteCtx but panics if execute operation fails.
func (SQL) MustExecuteCtxTx ¶ added in v1.6.0
MustExecuteCtxTx is like ExecuteCtxTx but panics if execute operation fails.
func (SQL) MustQuery ¶ added in v1.3.0
func (s SQL) MustQuery(target interface{})
MustQuery is like Query but panics if query operation fails.
func (SQL) MustQueryCtx ¶ added in v1.7.0
MustQueryCtx is like QueryCtx but panics if query operation fails.
func (SQL) MustQueryCtxTx ¶ added in v1.6.0
MustQueryCtxTx is like QueryCtxTx but panics if query operation fails.
func (SQL) MustQueryRow ¶ added in v1.3.0
func (s SQL) MustQueryRow(dest ...interface{})
MustQueryRow is like QueryRow but panics if query row operation fails.
func (SQL) MustQueryRowCtx ¶ added in v1.7.0
MustQueryRowCtx is like QueryRowCtx but panics if query row operation fails.
func (SQL) MustQueryRowCtxTx ¶ added in v1.6.0
MustQueryRowCtxTx is like QueryRowCtxTx but panics if query row operation fails.
func (SQL) Query ¶ added in v1.3.0
Query executes the SQL query and put the results into the target. Target must be a pointer to a struct, a slice or a map. For use cases, see Find() and Select().
func (SQL) QueryCtx ¶ added in v1.7.0
QueryCtx executes the SQL query and put the results into the target. Target must be a pointer to a struct, a slice or a map. For use cases, see Find() and Select().
func (SQL) QueryCtxTx ¶ added in v1.6.0
QueryCtxTx executes the SQL query and put the results into the target. Target must be a pointer to a struct, a slice or a map. For use cases, see Find() and Select().
func (SQL) QueryRow ¶ added in v1.3.0
QueryRow gets results from the first row, and put values of each column to corresponding dest. For use cases, see Insert().
var u struct { name string id int } psql.NewModelTable("users", conn).Select("name, id").MustQueryRow(&u.name, &u.id)
func (SQL) QueryRowCtx ¶ added in v1.7.0
QueryRowCtx gets results from the first row, and put values of each column to corresponding dest. For use cases, see Insert().
func (SQL) QueryRowCtxTx ¶ added in v1.6.0
QueryRowCtxTx gets results from the first row, and put values of each column to corresponding dest. For use cases, see Insert().
type SelectSQL ¶ added in v1.3.0
type SelectSQL struct { *SQL // contains filtered or unexported fields }
SelectSQL can be created with Model.NewSQL().AsSelect()
func (*SelectSQL) Count ¶ added in v1.3.0
Create and execute a SELECT COUNT(*) statement, return number of rows. To count in a different way: Count("COUNT(DISTINCT authors.id)").
func (*SelectSQL) Exists ¶ added in v1.3.0
Create and execute a SELECT 1 AS one statement. Returns true if record exists, false if not exists.
func (*SelectSQL) Find ¶ added in v1.3.0
Create a SELECT query statement with all fields of a Model. Options can be funtions like AddTableName or strings like "--no-reset" (use Select instead of ResetSelect).
func (*SelectSQL) Having ¶ added in v1.3.0
Adds HAVING to SELECT statement. Arguments should use positonal parameters like $1, $2. If only one argument is provided, "$?" in the condition will be replaced with the correct positonal parameter.
func (*SelectSQL) MustCount ¶ added in v1.3.0
MustCount is like Count but panics if count operation fails.
func (*SelectSQL) MustExists ¶ added in v1.3.0
MustExists is like Exists but panics if existence check operation fails. Returns true if record exists, false if not exists.
func (*SelectSQL) Reload ¶ added in v1.3.0
Update SQL and values in the DeleteSQL object due to changes of conditions.
func (*SelectSQL) ReplaceSelect ¶ added in v1.10.5
Replace old field names in existing SELECT statement with new.
func (*SelectSQL) ResetJoin ¶ added in v1.5.0
Clears existing JOIN statements and set new JOIN statements.
func (*SelectSQL) ResetSelect ¶ added in v1.3.0
Set expressions to SELECT statement.
func (*SelectSQL) Select ¶ added in v1.3.0
Add expressions to SELECT statement, before any existing jsonb columns.
func (*SelectSQL) WHERE ¶ added in v1.9.0
WHERE adds conditions to SELECT statement from variadic inputs.
The args parameter contains field name, operator, value tuples with each tuple consisting of three consecutive elements: the field name as a string, an operator symbol as a string (e.g. "=", ">", "<="), and the value to match against that field.
To generate a WHERE clause matching multiple fields, use more than one set of field/operator/value tuples in the args array. For example, WHERE("A", "=", 1, "B", "!=", 2) means "WHERE (A = 1) AND (B != 2)".
type UpdateSQL ¶ added in v1.3.0
type UpdateSQL struct { *SQL // contains filtered or unexported fields }
UpdateSQL can be created with Model.NewSQL().AsUpdate()
func (*UpdateSQL) Reload ¶ added in v1.3.0
Update SQL and values in the UpdateSQL object due to changes of columns and conditions.
func (*UpdateSQL) WHERE ¶ added in v1.9.0
WHERE adds conditions to UPDATE statement from variadic inputs.
The args parameter contains field name, operator, value tuples with each tuple consisting of three consecutive elements: the field name as a string, an operator symbol as a string (e.g. "=", ">", "<="), and the value to match against that field.
To generate a WHERE clause matching multiple fields, use more than one set of field/operator/value tuples in the args array. For example, WHERE("A", "=", 1, "B", "!=", 2) means "WHERE (A = 1) AND (B != 2)".