query

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2025 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Query is SQL Helper Query package contains helper functions to generate SQL statements query.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTypeIsNotStruct              = fmt.Errorf("type is not a struct")
	ErrWhereClauseRequiredForUpdate = fmt.Errorf("where clause should be set in the Update statement")
)

Exported errors

Functions

func Args

func Args(row any, forWrite bool) ([]any, error)

Args returns the arguments array for the given struct type. The given struct may be a pointer to struct or struct.

The forWrite parameter controls the behavior:

  • If forWrite is true, it returns a slice of values for INSERT/UPDATE, skipping autoincrement fields.
  • If forWrite is false, it returns a slice of pointers to copies of field values for SELECT (for sql.Scan). These pointers are then used with ArgsAppay to populate the struct.

func ArgsAppay

func ArgsAppay(row any, args []any) (err error)

ArgsAppay sets fields values of the given pointer to struct row from the args array.

It loops through the given struct fields and sets field values from the corresponding arguments in the given args array. Supported types are string, float64, time.Time, int64 and bool. If unsupported type is found, it returns an error.

func Count

func Count[T any](attr *SelectAttr) (string, error)

Count returns a SQL COUNT statement for the given struct type.

The struct may be tagged with "db" tags to specify the database field names. If the "db" tag is not specified, the field name will be used as the database field name. The returned string is a SQL statement that can be executed directly.

The wheres parameter is an optional list of where clauses. If specified, the where clauses will be joined with " and " and added to the SQL statement.

func Delete

func Delete[T any](wheres ...string) (string, error)

Delete returns a SQL DELETE statement for the given struct type.

The struct may be tagged with "db" tags to specify the database field names. If the "db" tag is not specified, the field name will be used as the database field name. The returned string is a SQL statement that can be executed directly.

The wheres parameter is an optional list of where clauses. If specified, the where clauses will be joined with " and " and added to the SQL statement.

func Insert

func Insert[T any]() (string, error)

Insert returns a SQL INSERT statement for the given struct type.

The struct may be tagged with "db" tags to specify the database field names. If the "db" tag is not specified, the field name will be used as the database field name. The returned string is a SQL statement that can be executed directly.

func Select

func Select[T any](attr *SelectAttr) (string, error)

Select returns a SQL SELECT statement for the given struct type.

The struct may be tagged with "db" tags to specify the database field names. If the "db" tag is not specified, the field name will be used as the database field name. The returned string is a SQL statement that can be executed directly.

The wheres parameter is an optional list of where clauses. If specified, the where clauses will be joined with " and " and added to the SQL statement.

func Table

func Table[T any]() (string, error)

Table returns a SQL CREATE TABLE statement for the given struct type.

The table is created if it does not already exist. The function returns an error if the given type is not a struct.

Example:

// Iput struct
type Astuct struct {
	ID   int	`db:"id" db_type:"integer" db_key:"not null primary key"`
	Name string
}

// Output CREATE TABLE statement
"CREATE TABLE IF NOT EXISTS astuct (id integer not null primary key, name text)"

Struct tagas are used to map database fields to struct fields. The tag is optional. Next tags may be used:

  • db:"some_field_name" - set database field name
  • db_type:"text" - set database field type
  • db_key:"not null primary key" - set database field key

func Update

func Update[T any](wheres ...string) (string, error)

Update returns a SQL UPDATE statement for the given struct type.

The wheres parameter is an optional list of where clauses. If specified, the where clauses will be joined with " and " and added to the SQL statement.

Types

type Paginator

type Paginator struct {
	// Get list of rows from this position. In other words: skip the specified
	// number of rows before starting to output rows.
	Offset int

	// Number of rows to get. If 0, all rows will be returned.
	Limit int
}

Paginator defines attributes for SELECT statement.

type SelectAttr

type SelectAttr struct {
	Paginator *Paginator // Offset and limit (optional)
	Wheres    []string   // Where clauses (optional)
	OrderBy   string     // Order by (optional)
}

SelectAttr defines attributes for SELECT statement.

Jump to

Keyboard shortcuts

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