models

package
v0.7.10 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AllUserEmployeesResult

type AllUserEmployeesResult struct {
	User *User
	Age  null.Uint64
	Empl *Employee
	// contains filtered or unexported fields
}

AllUserEmployeesResult is the result of `AllUserEmployees`.

type AllUserEmployeesResultSlice

type AllUserEmployeesResultSlice []*AllUserEmployeesResult

AllUserEmployeesResultSlice is slice of AllUserEmployeesResult.

func AllUserEmployees

func AllUserEmployees(ctx context.Context, q Queryer) (AllUserEmployeesResultSlice, error)

AllUserEmployees is created from:

<stmt name="AllUserEmployees">
  SELECT
    <wc table="user"/>,
    CAST(DATEDIFF(NOW(), birthday)/365 AS UNSIGNED) AS age,
    <wc table="employee" as="empl"/>
  FROM
    user LEFT JOIN employee AS empl ON user.id=empl.user_id
</stmt>

func (*AllUserEmployeesResultSlice) DistinctEmpl

func (slice *AllUserEmployeesResultSlice) DistinctEmpl() []*Employee

DistinctEmpl returns distinct (by primary value) Empl in the slice.

func (*AllUserEmployeesResultSlice) DistinctUser

func (slice *AllUserEmployeesResultSlice) DistinctUser() []*User

DistinctUser returns distinct (by primary value) User in the slice.

func (*AllUserEmployeesResultSlice) GroupByEmpl

GroupByEmpl groups by Empl and returns distinct (by primary value) Empl with their associated sub group of slices.

func (*AllUserEmployeesResultSlice) GroupByUser

func (slice *AllUserEmployeesResultSlice) GroupByUser() ([]*User, []AllUserEmployeesResultSlice)

GroupByUser groups by User and returns distinct (by primary value) User with their associated sub group of slices.

func (*AllUserEmployeesResultSlice) One

One returns a single AllUserEmployeesResult. It panics if the length of slice is not 1.

type ColumnScanner

type ColumnScanner interface {
	// contains filtered or unexported methods
}

ColumnScanner represents a collection of columns that can be scanned into from a row.

type Employee

type Employee struct {
	Id         int32      `json:"id" db:"id"`
	EmployeeSn string     `json:"employee_sn" db:"employee_sn"`
	UserId     int32      `json:"user_id" db:"user_id"`
	SuperiorId null.Int32 `json:"superior_id" db:"superior_id"`
}

Employee represents a row of table `employee`.

func EmployeeByEmployeeSn

func EmployeeByEmployeeSn(ctx context.Context, q Queryer, nxEmployeeSn string, lock bool) (*Employee, error)

EmployeeByEmployeeSn is used to select Employee by `employee_sn` index. It returns nil if no row is found.

func EmployeeByPrimary

func EmployeeByPrimary(ctx context.Context, q Queryer, nxId int32, lock bool) (*Employee, error)

EmployeeByPrimary is used to select Employee by `PRIMARY` index. It returns nil if no row is found.

func EmployeeByUserId

func EmployeeByUserId(ctx context.Context, q Queryer, nxUserId int32, lock bool) (*Employee, error)

EmployeeByUserId is used to select Employee by `user_id` index. It returns nil if no row is found.

func (*Employee) ColumnPointer

func (tr *Employee) ColumnPointer(i int) interface{}

ColumnPointer returns the i-th column's pointer. Implements TableRow interface.

func (*Employee) ColumnValue

func (tr *Employee) ColumnValue(i int) interface{}

ColumnValue returns the i-th column's value. Implements TableRow interface.

func (*Employee) Copy

func (tr *Employee) Copy() *Employee

Copy the row.

func (*Employee) Delete

func (tr *Employee) Delete(ctx context.Context, e Execer) error

Delete is used to delete the row from table.

func (*Employee) EmployeeByFkSuperior

func (tr *Employee) EmployeeByFkSuperior(ctx context.Context, q Queryer, lock bool) (*Employee, error)

EmployeeByFkSuperior returns the associated Employee by `fk_superior` foreign key: (`superior_id`).

func (*Employee) Insert

func (tr *Employee) Insert(ctx context.Context, e Execer) error

Insert is used to insert the row into table. This method will also update the value in auto increment column (if exists).

func (*Employee) InsertEx

func (tr *Employee) InsertEx(ctx context.Context, e Execer, modifier string) error

InsertEx is the same as Insert with extra modifier specified. modifier can be one of:

  • "ignore": "INSERT IGNORE INTO ..."
  • "replace": "REPLACE INTO ..."
  • "": Normal "INSERT INTO ..."

func (*Employee) PrimaryValue

func (tr *Employee) PrimaryValue() interface{}

PrimaryValue returns the primary key value of this row. Implements TableRowWithPrimary interface.

func (*Employee) Reload

func (tr *Employee) Reload(ctx context.Context, q Queryer) error

Reload is used to reload the row from table. It returns error if no row is found.

func (*Employee) ReloadWithLock

func (tr *Employee) ReloadWithLock(ctx context.Context, q Queryer) error

ReloadWithLock is used to reload this row with lock from table. It returns error if no row is found.

func (*Employee) TableMeta

func (tr *Employee) TableMeta() *TableMeta

TableMeta returns the table meta information of table `employee`. Implements TableRow interface.

func (*Employee) Update

func (tr *Employee) Update(ctx context.Context, e Execer, newTr *Employee) error

Update is used to update the row in table. Only columns with different values will be updated. NOTE: This method does not modify content in `tr`/`newTr`. Use Reload to load fresh data from database.

func (*Employee) UserByFkUser

func (tr *Employee) UserByFkUser(ctx context.Context, q Queryer, lock bool) (*User, error)

UserByFkUser returns the associated User by `fk_user` foreign key: (`user_id`).

func (*Employee) Valid

func (tr *Employee) Valid() bool

Valid returns true if this is a valid table row (e.g. not NULL). Implements TableRow interface.

type EmployeeSlice

type EmployeeSlice []*Employee

EmployeeSlice is slice of Employee.

type ExecQueryer

type ExecQueryer interface {
	Execer
	Queryer
}

ExecQueryer = Execer + Queryer

type Execer

type Execer interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

Execer is the common interface to execute a query without returning any row.

type Queryer

type Queryer interface {
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

Queryer is the common interface to execute a query returning row(s).

type Slicer

type Slicer interface {
	// contains filtered or unexported methods
}

Slicer represents an appendable slice.

type SubordinatesBySuperiorsResult

type SubordinatesBySuperiorsResult struct {
	Superior    *Employee
	Subordinate *Employee
	// contains filtered or unexported fields
}

SubordinatesBySuperiorsResult is the result of `SubordinatesBySuperiors`.

type SubordinatesBySuperiorsResultSlice

type SubordinatesBySuperiorsResultSlice []*SubordinatesBySuperiorsResult

SubordinatesBySuperiorsResultSlice is slice of SubordinatesBySuperiorsResult.

func SubordinatesBySuperiors

func SubordinatesBySuperiors(ctx context.Context, q Queryer, id ...int) (SubordinatesBySuperiorsResultSlice, error)

SubordinatesBySuperiors is created from:

<stmt name="SubordinatesBySuperiors">
  <a name="id" type="...int"/>
  <v in_query="1"/>
  SELECT
    <wc table="employee" as="superior"/>,
    <wc table="employee" as="subordinate"/>
  FROM
    employee AS superior LEFT JOIN employee AS subordinate ON subordinate.superior_id=superior.id
  WHERE
    superior.id IN (<b name="id"/>)
</stmt>

func (*SubordinatesBySuperiorsResultSlice) DistinctSubordinate

func (slice *SubordinatesBySuperiorsResultSlice) DistinctSubordinate() []*Employee

DistinctSubordinate returns distinct (by primary value) Subordinate in the slice.

func (*SubordinatesBySuperiorsResultSlice) DistinctSuperior

func (slice *SubordinatesBySuperiorsResultSlice) DistinctSuperior() []*Employee

DistinctSuperior returns distinct (by primary value) Superior in the slice.

func (*SubordinatesBySuperiorsResultSlice) GroupBySubordinate

GroupBySubordinate groups by Subordinate and returns distinct (by primary value) Subordinate with their associated sub group of slices.

func (*SubordinatesBySuperiorsResultSlice) GroupBySuperior

GroupBySuperior groups by Superior and returns distinct (by primary value) Superior with their associated sub group of slices.

func (*SubordinatesBySuperiorsResultSlice) One

One returns a single SubordinatesBySuperiorsResult. It panics if the length of slice is not 1.

type TableMeta

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

TableMeta contains meta information of a database table.

func NewTableMeta

func NewTableMeta(tableName string, columnNames []string, opts ...TableMetaOption) *TableMeta

NewTableMeta creates a new TableMeta.

func (*TableMeta) TableName

func (meta *TableMeta) TableName() string

TableName returns the table name.

type TableMetaOption

type TableMetaOption func(*TableMeta)

TableMetaOption is used in creating TableMeta.

func OptAutoIncColumn

func OptAutoIncColumn(columnName string) TableMetaOption

OptAutoIncColumn sets the auto increment column.

func OptColumnsWithDefault

func OptColumnsWithDefault(columnNames ...string) TableMetaOption

OptColumnsWithDefault sets the columns that have not NULL server side default, including:

  • AUTO_INCREMENT
  • NOW()
  • Other not NULL constant defaults.

func OptPrimaryColumns

func OptPrimaryColumns(columnNames ...string) TableMetaOption

OptPrimaryColumns sets the primary key columns.

type TableRow

type TableRow interface {
	ColumnScanner

	// TableMeta returns the table's meta information.
	TableMeta() *TableMeta

	// Valid returns true if this is a valid table row (e.g. not NULL).
	Valid() bool

	// ColumnValue returns the i-th column value.
	ColumnValue(i int) interface{}

	// ColumnPointer returns the i-th column's pointer.
	ColumnPointer(i int) interface{}
}

TableRow represents a table's row.

type TableRowWithPrimary

type TableRowWithPrimary interface {
	TableRow

	// PrimaryValue returns the primary key value of this row.
	// The returned value is comparable by values.
	PrimaryValue() interface{}
}

TableRowWithPrimary represents row of table with primary key.

type User

type User struct {
	Id       int32     `json:"id" db:"id"`
	Name     string    `json:"name" db:"name"`
	Female   null.Bool `json:"female" db:"female"`
	Birthday null.Time `json:"birthday" db:"birthday"`
}

User represents a row of table `user`.

func UserByPrimary

func UserByPrimary(ctx context.Context, q Queryer, nxId int32, lock bool) (*User, error)

UserByPrimary is used to select User by `PRIMARY` index. It returns nil if no row is found.

func (*User) ColumnPointer

func (tr *User) ColumnPointer(i int) interface{}

ColumnPointer returns the i-th column's pointer. Implements TableRow interface.

func (*User) ColumnValue

func (tr *User) ColumnValue(i int) interface{}

ColumnValue returns the i-th column's value. Implements TableRow interface.

func (*User) Copy

func (tr *User) Copy() *User

Copy the row.

func (*User) Delete

func (tr *User) Delete(ctx context.Context, e Execer) error

Delete is used to delete the row from table.

func (*User) Insert

func (tr *User) Insert(ctx context.Context, e Execer) error

Insert is used to insert the row into table. This method will also update the value in auto increment column (if exists).

func (*User) InsertEx

func (tr *User) InsertEx(ctx context.Context, e Execer, modifier string) error

InsertEx is the same as Insert with extra modifier specified. modifier can be one of:

  • "ignore": "INSERT IGNORE INTO ..."
  • "replace": "REPLACE INTO ..."
  • "": Normal "INSERT INTO ..."

func (*User) PrimaryValue

func (tr *User) PrimaryValue() interface{}

PrimaryValue returns the primary key value of this row. Implements TableRowWithPrimary interface.

func (*User) Reload

func (tr *User) Reload(ctx context.Context, q Queryer) error

Reload is used to reload the row from table. It returns error if no row is found.

func (*User) ReloadWithLock

func (tr *User) ReloadWithLock(ctx context.Context, q Queryer) error

ReloadWithLock is used to reload this row with lock from table. It returns error if no row is found.

func (*User) TableMeta

func (tr *User) TableMeta() *TableMeta

TableMeta returns the table meta information of table `user`. Implements TableRow interface.

func (*User) Update

func (tr *User) Update(ctx context.Context, e Execer, newTr *User) error

Update is used to update the row in table. Only columns with different values will be updated. NOTE: This method does not modify content in `tr`/`newTr`. Use Reload to load fresh data from database.

func (*User) Valid

func (tr *User) Valid() bool

Valid returns true if this is a valid table row (e.g. not NULL). Implements TableRow interface.

type UserSlice

type UserSlice []*User

UserSlice is slice of User.

func UsersByCond

func UsersByCond(ctx context.Context, q Queryer, id int, name string, birthday time.Time, limit int) (UserSlice, error)

UsersByCond is created from:

<stmt name="UsersByCond">
  <v use_template="1"/>
  <a name="id" type="int"/>
  <a name="name" type="string"/>
  <a name="birthday" type="time.Time"/>
  <a name="limit" type="int"/>
  SELECT
    <wc table="user"/>
  FROM
    user
  WHERE
    <t>{{ if ne .id 0 }}</t>
      id=<b name="id"/> AND
    <t>{{ end }}</t>

    <t>{{ if ne (len .name) 0 }}</t>
      name=<b name="name"/> AND
    <t>{{ end }}</t>

    <t>{{ if not .birthday.IsZero }}</t>
      birthday=<b name="birthday"/> AND
    <t>{{ end }}</t>
    1
  LIMIT <b name="limit">10</b>
</stmt>

Jump to

Keyboard shortcuts

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