Select

SQL SELECT statements can be executed using the genorm.Select or genorm.Pluck functions.

It must start with a genorm.Select function and end with one of Get, GetCtx, GetAll, or GetAllCtx, but the order can be swapped otherwise. However, if the same method other than OrderBy is executed twice in the same method chain, Get, GetCtx, GetAll, and GetAllCtx will return an error without executing the query at run time.

Example

// SELECT `id`, `name`, `created_at` FROM `users`
// userValues: []orm.UserTable
userValues, err := genorm.
    Select(orm.User()).
    GetAll(db)

// SELECT `id`, `name`, `created_at` FROM `users`
// userValues: []orm.UserTable
userValues, err := genorm.
    Select(orm.User()).
    GetAllCtx(context.Background(), db)

// SELECT `id`, `name`, `created_at` FROM `users` WHERE `id` = {{uuid.New()}}
// userValues: []orm.UserTable
userValues, err := genorm.
    Select(orm.User()).
    Where(genorm.EqLit(user.IDExpr, uuid.New())).
    GetAll(db)

// SELECT `name`, `created_at` FROM `users`
// userValues: []orm.UserTable
userValues, err := genorm.
    Select(orm.User()).
    Fields(user.Name, user.CreatedAt).
    GetAll(db)

// SELECT `id`, `name`, `created_at` FROM `users` LIMIT 1
// userValue: orm.UserTable
userValue, err := genorm.
    Select(orm.User()).
    Get(db)

// SELECT `id`, `name`, `created_at` FROM `users` LIMIT 1
// userValue: orm.UserTable
userValue, err := genorm.
    Select(orm.User()).
    GetCtx(context.Background(), db)

// SELECT `id`, `name`, `created_at` FROM `users` ORDER BY `created_at` DESC
// userValues: []orm.UserTable
userValues, err := genorm.
    Select(orm.User()).
    OrderBy(genorm.Desc, user.CreatedAt).
    GetAll(db)

// SELECT `id`, `name`, `created_at` FROM `users` ORDER BY `created_at` DESC, `id` ASC
// userValues: []orm.UserTable
userValues, err := genorm.
    Select(orm.User()).
    OrderBy(genorm.Desc, user.CreatedAt).
    OrderBy(genorm.Asc, user.ID).
    GetAll(db)

// SELECT DISTINCT `id`, `name`, `created_at` FROM `users`
// userValues: []orm.UserTable
userValues, err := genorm.
    Select(orm.User()).
    Distinct().
    GetAll(db)

// SELECT `id`, `name`, `created_at` FROM `users` LIMIT 5 OFFSET 3
// userValues: []orm.UserTable
userValues, err := genorm.
    Select(orm.User()).
    Limit(5).
    Offset(3).
    GetAll(db)

// SELECT `name` FROM `users` GROUP BY `name` HAVING COUNT(`id`) > 10
// userValues: []orm.UserTable
userValues, err := genorm.
    Select(orm.User()).
    Fields(user.Name).
    GroupBy(user.Name).
    Having(genorm.GtLit(genorm.Count(user.IDExpr, false), genorm.Wrap(int64(10)))).
    GetAll(db)

// SELECT `id`, `name`, `created_at` FROM `users` FOR UPDATE
// userValues: []orm.UserTable
userValues, err := genorm.
    Select(orm.User()).
    Lock(genorm.ForUpdate).
    GetAll(db)

// SELECT `id` FROM `users`
// userIDs: []uuid.UUID
userIDs, err := genorm.
    Pluck(orm.User(), user.IDExpr).
    GetAll(db)

// SELECT `id` FROM `users` LIMIT 1
// userID: uuid.UUID
userID, err := genorm.
    Pluck(orm.User(), user.IDExpr).
    Get(db)

// SELECT `users`.`name`, `messages`.`content` FROM `users` INNER JOIN `messages` ON `users`.`id` = `messages`.`user_id`
// messageUserValues: []orm.MessageUserTable
userID := orm.MessageUserParseExpr(user.ID)
userName := orm.MessageUserParse(user.Name)
messageUserID := orm.MessageUserParseExpr(message.UserID)
messageContent := orm.MessageUserParse(message.Content)
messageUserValues, err := genorm.
    Select(orm.User().
        Message().Join(genorm.Eq(userID, messageUserID))).
    Fields(userName, messageContent).
    GetAll(db)

Select

Function to start a method chain that issues a SELECT statement. It takes a table generated by the CLI as its first argument.

Pluck

Function to start a method chain that issues a SELECT statement. It takes a table generated by the CLI as its first argument. The second argument is the column to which the SELECT statement will be applied.

Fields(optional)

Specify the columns to be SELECT target. By default, all columns of the table are the target of SELECT. In the case of Pluck, the target columns are specified at the start of the method chain and cannot be used.

Distinct(optional)

Set DISTINCT to the SELECT statement.

Where(optional)

Sets the WHERE clause in the SELECT statement. The first argument takes an expression corresponding to bool in the Golang.

Lock(optional)

Set FOR UPDATE and FOR SHARE in the SELECT statement. The first argument takes genorm.ForUpdate or genorm.ForShare and sets FOR UPDATE and FOR SHARE respectively. Currently, detailed settings such as locking by table specification, NO WAIT, SKIP LOCKED, etc. are not supported.

GetAll

Executes the query and exits the method chain. The argument is a value satisfying the genorm.DBinterface including *sql.DB/*sql.Tx It takes this and uses it to execute the query.

GetAllCtx

Executes the query and exits the method chain. The second argument is a value satisfying the genorm.DBinterface including *sql.DB/*sql.Tx It takes this and uses it to execute the query. It also receives context.Context as its first argument; when context is canceled, the connection to the database is released, preventing unnecessary use of connections.

Get

Executes the query and exits the method chain. The argument is a value satisfying the genorm.DBinterface including *sql.DB/*sql.Tx It takes this and uses it to execute the query.

It is used to retrieve only one record and is automatically set to LIMIT 1. Therefore, it cannot be used at the same time as the Limit method and will return an error if the Limit method is already used in the method chain.

Returns a genorm.ErrRecordNotFound error if the record was not found.

GetCtx

Executes the query and exits the method chain. The second argument is a value satisfying the genorm.DBinterface including *sql.DB/*sql.Tx It takes this and uses it to execute the query. It also receives context.Context as its first argument. When context is canceled, the connection to the database is released, preventing unnecessary use of connections.

It is used to retrieve only one record and is automatically set to LIMIT 1. Therefore, it cannot be used at the same time as the Limit method and will return an error if the Limit method is already used in the method chain.

Returns a genorm.ErrRecordNotFound error if the record was not found.

results matching ""

    No results matching ""