### Install sqlx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Use this command to install the sqlx package. ```go go get github.com/jmoiron/sqlx ``` -------------------------------- ### BeginTxx Transaction with Context Source: https://pkg.go.dev/github.com/jmoiron/sqlx Starts a transaction with context support, returning an *sqlx.Tx. The context controls the transaction's lifecycle. ```go func (db *DB) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) ``` -------------------------------- ### Connect and Setup Database Schema Source: https://pkg.go.dev/github.com/jmoiron/sqlx Establishes a database connection using sqlx.Connect and executes a schema definition. Ensure the database driver is imported. ```go package main import ( "database/sql" "fmt" "log" _ "github.com/lib/pq" "github.com/jmoiron/sqlx" ) var schema = ` CREATE TABLE person ( first_name text, last_name text, email text ); CREATE TABLE place ( country text, city text NULL, telcode integer )` type Person struct { FirstName string `db:"first_name"` LastName string `db:"last_name"` Email string } type Place struct { Country string City sql.NullString TelCode int } func main() { // this Pings the database trying to connect // use sqlx.Open() for sql.Open() semantics db, err := sqlx.Connect("postgres", "user=foo dbname=bar sslmode=disable") if err != nil { log.Fatalln(err) } // exec the schema or fail; multi-statement Exec behavior varies between // database drivers; pq will exec them all, sqlite3 won't, ymmv db.MustExec(schema) tx := db.MustBegin() tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "Jason", "Moiron", "jmoiron@jmoiron.net") tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "John", "Doe", "johndoeDNE@gmail.net") tx.MustExec("INSERT INTO place (country, city, telcode) VALUES ($1, $2, $3)", "United States", "New York", "1") tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Hong Kong", "852") tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Singapore", "65") // Named queries can use structs, so if you have an existing struct (i.e. person := &Person{}) that you have populated, you can pass it in as &person tx.NamedExec("INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)", &Person{"Jane", "Citizen", "jane.citzen@example.com"}) tx.Commit() // Query the database, storing results in a []Person (wrapped in []interface{}) people := []Person{} db.Select(&people, "SELECT * FROM person ORDER BY first_name ASC") jason, john := people[0], people[1] fmt.Printf("%#v\n%#v", jason, john) // Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"} // Person{FirstName:"John", LastName:"Doe", Email:"johndoeDNE@gmail.net"} // You can also get a single result, a la QueryRow jason = Person{} err = db.Get(&jason, "SELECT * FROM person WHERE first_name=$1", "Jason") fmt.Printf("%#v\n", jason) // Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"} // if you have null fields and use SELECT *, you must use sql.Null* in your struct places := []Place{} err = db.Select(&places, "SELECT * FROM place ORDER BY telcode ASC") if err != nil { fmt.Println(err) return } usa, singsing, honkers := places[0], places[1], places[2] fmt.Printf("%#v\n%#v\n%#v\n", usa, singsing, honkers) // Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1} // Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65} // Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852} // Loop through rows using only one struct place := Place{} rows, err := db.Queryx("SELECT * FROM place") for rows.Next() { err := rows.StructScan(&place) if err != nil { log.Fatalln(err) } fmt.Printf("%#v\n", place) } // Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1} // Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852} // Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65} // Named queries, using `:name` as the bindvar. Automatic bindvar support // which takes into account the dbtype based on the driverName on sqlx.Open/Connect _, err = db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`, map[string]interface{}{ "first": "Bin", "last": "Smuth", "email": "bensmith@allblacks.nz", }) // Selects Mr. Smith from the database rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:fn`, map[string]interface{}{"fn": "Bin"}) // Named queries can also use structs. Their bind names follow the same rules // as the name -> db mapping, so struct fields are lowercased and the `db` tag // is taken into consideration. rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, jason) // batch insert // batch insert with structs personStructs := []Person{ {FirstName: "Ardie", LastName: "Savea", Email: "asavea@ab.co.nz"}, {FirstName: "Sonny Bill", LastName: "Williams", Email: "sbw@ab.co.nz"}, ``` -------------------------------- ### func (*DB) Get Source: https://pkg.go.dev/github.com/jmoiron/sqlx Get using this DB. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty. ```APIDOC ## func (*DB) Get ### Description Get using this DB. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty. ### Signature ```go func (db *DB) Get(dest interface{}, query string, args ...interface{}) error ``` ``` -------------------------------- ### MustBeginTx Transaction with Context Source: https://pkg.go.dev/github.com/jmoiron/sqlx Starts a transaction with context support and panics on error. Returns an *sqlx.Tx. The context controls the transaction's lifecycle. ```go func (db *DB) MustBeginTx(ctx context.Context, opts *sql.TxOptions) *Tx ``` -------------------------------- ### Get Source: https://pkg.go.dev/github.com/jmoiron/sqlx Get using this NamedStmt. Any named placeholder parameters are replaced with fields from arg. ```APIDOC ## Get ### Description Get using this NamedStmt. Any named placeholder parameters are replaced with fields from arg. ### Signature ```go func (n *NamedStmt) Get(dest interface{}, arg interface{}) error ``` ``` -------------------------------- ### MustBegin Transaction Source: https://pkg.go.dev/github.com/jmoiron/sqlx Starts a transaction and panics on error. Returns an *sqlx.Tx instead of an *sql.Tx. ```go func (db *DB) MustBegin() *Tx ``` -------------------------------- ### Beginx Transaction Source: https://pkg.go.dev/github.com/jmoiron/sqlx Starts a transaction and returns an *sqlx.Tx instead of an *sql.Tx. ```go func (db *DB) Beginx() (*Tx, error) ``` -------------------------------- ### Get Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query and scans the first row into the destination struct. ```APIDOC ## Get get(q Queryer, dest interface{}, query string, args ...interface{}) error ### Description Executes a query that is expected to return at most one row and scans the first row into the destination struct. If no rows are returned, `sql.ErrNoRows` is returned. ### Parameters #### Path Parameters - **q** (Queryer) - Required - An object that implements the `Queryer` interface. - **dest** (interface{}) - Required - A pointer to the struct where the row data will be scanned. - **query** (string) - Required - The SQL query to execute. - **args** (...interface{}) - Optional - Arguments to be passed to the query. ``` -------------------------------- ### DB.Connx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Gets a connection from the DB pool with context. ```APIDOC ## Connx connx(db *DB, ctx context.Context) (*Conn, error) ### Description Gets a connection from the DB pool with the given context. This allows for managing connections within a transaction or specific operation. ### Parameters #### Path Parameters - **db** (*DB) - Required - The DB object. - **ctx** (context.Context) - Required - The context for acquiring the connection. ``` -------------------------------- ### func (*DB) MustBegin Source: https://pkg.go.dev/github.com/jmoiron/sqlx MustBegin starts a transaction, and panics on error. Returns an *sqlx.Tx instead of an *sql.Tx. ```APIDOC ## func (*DB) MustBegin ### Description MustBegin starts a transaction, and panics on error. Returns an *sqlx.Tx instead of an *sql.Tx. ### Signature ```go func (db *DB) MustBegin() *Tx ``` ``` -------------------------------- ### GetContext Source: https://pkg.go.dev/github.com/jmoiron/sqlx Context-aware version of Get. Executes a query and scans a single row into the destination with context support. ```APIDOC ## GetContext ### Description GetContext does a QueryRow using the provided Queryer, and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return sql.ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty. ### Method func GetContext(ctx context.Context, q QueryerContext, dest interface{}, query string, args ...interface{}) error ### Parameters #### Path Parameters - **ctx** (context.Context) - Required - The context for the query. - **q** (QueryerContext) - Required - The queryer context interface. - **dest** (interface{}) - Required - The destination to scan the row into. - **query** (string) - Required - The SQL query to execute. - **args** (...interface{}) - Optional - Arguments to be used with the query. ``` -------------------------------- ### func (*DB) MustBeginTx Source: https://pkg.go.dev/github.com/jmoiron/sqlx MustBeginTx starts a transaction, and panics on error. Returns an *sqlx.Tx instead of an *sql.Tx. The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to MustBeginContext is canceled. ```APIDOC ## func (*DB) MustBeginTx ### Description MustBeginTx starts a transaction, and panics on error. Returns an *sqlx.Tx instead of an *sql.Tx. The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to MustBeginContext is canceled. ### Signature ```go func (db *DB) MustBeginTx(ctx context.Context, opts *sql.TxOptions) *Tx ``` ``` -------------------------------- ### NamedStmt Get Source: https://pkg.go.dev/github.com/jmoiron/sqlx Get retrieves a single row from a named statement into the destination. ```APIDOC ## NamedStmt.Get ### Description Retrieves a single row from a named statement into the destination. ### Method func (n *NamedStmt) Get(dest interface{}, arg interface{}) error ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **dest** (interface{}) - Required - The destination to scan the row into. - **arg** (interface{}) - Required - The arguments for the named statement. ### Request Example ```go // Example usage would depend on the context of NamedStmt creation ``` ### Response #### Success Response (200) - **error** - An error if the retrieval failed. #### Response Example ```go // Example response would be nil if successful, or an error object ``` ``` ```APIDOC ## NamedStmt.GetContext ### Description GetContext retrieves a single row from a named statement with context into the destination. ### Method func (n *NamedStmt) GetContext(ctx context.Context, dest interface{}, arg interface{}) error ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **ctx** (context.Context) - Required - The context for the operation. - **dest** (interface{}) - Required - The destination to scan the row into. - **arg** (interface{}) - Required - The arguments for the named statement. ### Request Example ```go // Example usage would depend on the context of NamedStmt creation ``` ### Response #### Success Response (200) - **error** - An error if the retrieval failed. #### Response Example ```go // Example response would be nil if successful, or an error object ``` ``` -------------------------------- ### Connect to a Database Source: https://pkg.go.dev/github.com/jmoiron/sqlx Establishes a connection to a database using the provided driver name and data source name, verifying with a ping. ```go func Connect(driverName, dataSourceName string) (*DB, error) ``` -------------------------------- ### Get Single Row with Context (Go) Source: https://pkg.go.dev/github.com/jmoiron/sqlx Similar to Get, but includes context for cancellation and timeouts. Scans a single row into the destination, returning sql.ErrNoRows if no rows are found. ```go func GetContext(ctx context.Context, q QueryerContext, dest interface{}, query string, args ...interface{}) error ``` -------------------------------- ### Begin Transaction with Context (sqlx.Conn) Source: https://pkg.go.dev/github.com/jmoiron/sqlx Begins a transaction using a Conn, returning an sqlx.Tx instead of sql.Tx. The provided context is used until commit or rollback. Context cancellation will roll back the transaction. ```go func (c *Conn) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) ``` -------------------------------- ### Conn.BeginTxx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Begins a transaction with context and options. ```APIDOC ## BeginTxx beginTxx(c *Conn, ctx context.Context, opts *sql.TxOptions) (*Tx, error) ### Description Begins a transaction with the given context and transaction options. Returns a `Tx` object that can be used to execute queries within the transaction. ### Parameters #### Path Parameters - **c** (*Conn) - Required - The connection object. - **ctx** (context.Context) - Required - The context for the transaction. - **opts** (*sql.TxOptions) - Optional - Options for the transaction. ``` -------------------------------- ### DB.BeginTxx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Begins a transaction with context and options on a DB object. ```APIDOC ## BeginTxx beginTxx(db *DB, ctx context.Context, opts *sql.TxOptions) (*Tx, error) ### Description Begins a transaction with the given context and transaction options on a `DB` object. Returns a `Tx` object that can be used to execute queries within the transaction. ### Parameters #### Path Parameters - **db** (*DB) - Required - The DB object. - **ctx** (context.Context) - Required - The context for the transaction. - **opts** (*sql.TxOptions) - Optional - Options for the transaction. ``` -------------------------------- ### ConnectContext to a Database Source: https://pkg.go.dev/github.com/jmoiron/sqlx Establishes a connection to a database with context support, verifying with a ping. ```go func ConnectContext(ctx context.Context, driverName, dataSourceName string) (*DB, error) ``` -------------------------------- ### Open Database Connection Source: https://pkg.go.dev/github.com/jmoiron/sqlx Opens a database connection, similar to sql.Open, but returns an *sqlx.DB. ```go func Open(driverName, dataSourceName string) (*DB, error) ``` -------------------------------- ### Connx Get SQLx Connection Source: https://pkg.go.dev/github.com/jmoiron/sqlx Retrieves an *sqlx.Conn from the database connection pool, supporting context. ```go func (db *DB) Connx(ctx context.Context) (*Conn, error) ``` -------------------------------- ### NewDb Wrapper for Existing DB Source: https://pkg.go.dev/github.com/jmoiron/sqlx Creates a new sqlx DB wrapper around a pre-existing *sql.DB. Requires the driverName for named query support. ```go func NewDb(db *sql.DB, driverName string) *DB ``` -------------------------------- ### Deref Function Source: https://pkg.go.dev/github.com/jmoiron/sqlx/reflectx Deref is an indirect function for reflect.Types, simplifying the process of getting the underlying type. ```APIDOC ## func Deref ### Description Deref is Indirect for reflect.Types ### Signature ```go func Deref(t reflect.Type) reflect.Type ``` ``` -------------------------------- ### DB.MustBeginTx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Begins a transaction with context and options on a DB object, panicking on error. ```APIDOC ## MustBeginTx mustBeginTx(db *DB, ctx context.Context, opts *sql.TxOptions) *Tx ### Description Begins a transaction with the given context and transaction options on a `DB` object. It panics if an error occurs during the transaction initiation. ### Parameters #### Path Parameters - **db** (*DB) - Required - The DB object. - **ctx** (context.Context) - Required - The context for the transaction. - **opts** (*sql.TxOptions) - Optional - Options for the transaction. ``` -------------------------------- ### Deref reflect.Type Source: https://pkg.go.dev/github.com/jmoiron/sqlx/reflectx Indirects reflect.Types to get the underlying type. Useful for dereferencing pointers or interfaces. ```go func Deref(t reflect.Type) reflect.Type ``` -------------------------------- ### Get Transaction Driver Name Source: https://pkg.go.dev/github.com/jmoiron/sqlx Returns the driver name used by the database connection that initiated this transaction. ```go func (tx *Tx) DriverName() string ``` -------------------------------- ### Prepare Statement with Context (sqlx.Conn) Source: https://pkg.go.dev/github.com/jmoiron/sqlx Prepares a statement using the Conn, returning an sqlx.Stmt instead of a sql.Stmt. The context is used for preparation, not execution. ```go func (c *Conn) PreparexContext(ctx context.Context, query string) (*Stmt, error) ``` -------------------------------- ### Execute QueryxContext with Statement Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query using a prepared statement with context support. Placeholder parameters are replaced with supplied arguments. ```go func (s *Stmt) QueryxContext(ctx context.Context, args ...interface{}) (*Rows, error) ``` -------------------------------- ### Preparex Function Source: https://pkg.go.dev/github.com/jmoiron/sqlx Prepares a statement using the Preparer interface. ```go func Preparex(p Preparer, query string) (*Stmt, error) ``` -------------------------------- ### Get Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query and scans a single row into the destination. Returns sql.ErrNoRows if the result set is empty. ```APIDOC ## Get ### Description Get does a QueryRow using the provided Queryer, and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return sql.ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty. ### Method func Get(q Queryer, dest interface{}, query string, args ...interface{}) error ### Parameters #### Path Parameters - **q** (Queryer) - Required - The queryer interface. - **dest** (interface{}) - Required - The destination to scan the row into. - **query** (string) - Required - The SQL query to execute. - **args** (...interface{}) - Optional - Arguments to be used with the query. ``` -------------------------------- ### DB.ConnectContext Source: https://pkg.go.dev/github.com/jmoiron/sqlx Establishes a database connection with context. ```APIDOC ## ConnectContext connectContext(ctx context.Context, driverName, dataSourceName string) (*DB, error) ### Description Establishes a database connection with the given context, driver name, and data source name. This function allows for connection timeouts and cancellations. ### Parameters #### Path Parameters - **ctx** (context.Context) - Required - The context for the connection. - **driverName** (string) - Required - The name of the database driver (e.g., "postgres", "mysql"). - **dataSourceName** (string) - Required - The connection string for the database. ``` -------------------------------- ### Get Named Statement within Transaction Source: https://pkg.go.dev/github.com/jmoiron/sqlx Returns a version of the named prepared statement that operates within the context of this transaction. ```go func (tx *Tx) NamedStmt(stmt *NamedStmt) *NamedStmt ``` -------------------------------- ### func Open Source: https://pkg.go.dev/github.com/jmoiron/sqlx Open is the same as sql.Open, but returns an *sqlx.DB instead. ```APIDOC ## func Open ### Description Open is the same as sql.Open, but returns an *sqlx.DB instead. ### Signature ```go func Open(driverName, dataSourceName string) (*DB, error) ``` ``` -------------------------------- ### Execute Queryx with Statement Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query using a prepared statement and returns multiple rows. Placeholder parameters are replaced with supplied arguments. ```go func (s *Stmt) Queryx(args ...interface{}) (*Rows, error) ``` -------------------------------- ### func Connect Source: https://pkg.go.dev/github.com/jmoiron/sqlx Connect to a database and verify with a ping. ```APIDOC ## func Connect ### Description Connect to a database and verify with a ping. ### Signature ```go func Connect(driverName, dataSourceName string) (*DB, error) ``` ``` -------------------------------- ### NamedExecContext Source: https://pkg.go.dev/github.com/jmoiron/sqlx Uses BindStruct to get a query executable by the driver and then runs Exec on the result. Returns an error from the binding or the query execution itself. ```APIDOC ## NamedExecContext ### Description NamedExecContext uses BindStruct to get a query executable by the driver and then runs Exec on the result. Returns an error from the binding or the query execution itself. ### Signature ```go func NamedExecContext(ctx context.Context, e ExtContext, query string, arg interface{}) (sql.Result, error) ``` ``` -------------------------------- ### NamedExec Source: https://pkg.go.dev/github.com/jmoiron/sqlx Uses BindStruct to get a query executable by the driver and then runs Exec on the result. Returns an error from the binding or the query execution itself. ```APIDOC ## NamedExec ### Description NamedExec uses BindStruct to get a query executable by the driver and then runs Exec on the result. Returns an error from the binding or the query execution itself. ### Signature ```go func NamedExec(e Ext, query string, arg interface{}) (sql.Result, error) ``` ``` -------------------------------- ### Execute QueryRowxContext with Statement Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query using a prepared statement with context support. Placeholder parameters are replaced with supplied arguments. ```go func (s *Stmt) QueryRowxContext(ctx context.Context, args ...interface{}) *Row ``` -------------------------------- ### MustOpen Database Connection Source: https://pkg.go.dev/github.com/jmoiron/sqlx Opens a database connection, similar to sql.Open, but returns an *sqlx.DB and panics on error. ```go func MustOpen(driverName, dataSourceName string) *DB ``` -------------------------------- ### Execute QueryRowx with Statement Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query using a prepared statement and returns a single row. Placeholder parameters are replaced with supplied arguments. ```go func (s *Stmt) QueryRowx(args ...interface{}) *Row ``` -------------------------------- ### Load SQL Statements from File with Context (Go) Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes all SQL statements from a file with context support for cancellation and timeouts. Similar to LoadFile, it reads the entire file into memory and may return a nil result on error. ```go func LoadFileContext(ctx context.Context, e ExecerContext, path string) (*sql.Result, error) ``` -------------------------------- ### Get Named Statement within Transaction with Context Source: https://pkg.go.dev/github.com/jmoiron/sqlx Returns a version of the named prepared statement that operates within the context of this transaction and the provided context. ```go func (tx *Tx) NamedStmtContext(ctx context.Context, stmt *NamedStmt) *NamedStmt ``` -------------------------------- ### Preparex Function in SQLx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Prepares a statement and returns an sqlx.Stmt instead of a standard sql.Stmt. Use this for prepared statements that benefit from SQLx enhancements. ```go func (db *DB) Preparex(query string) (*Stmt, error) ``` -------------------------------- ### Get Single Row with SQLx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query and scans the single resulting row into the destination interface. Returns an error if the result set is empty. ```go func (db *DB) Get(dest interface{}, query string, args ...interface{}) error ``` -------------------------------- ### Get Single Row in Transaction Source: https://pkg.go.dev/github.com/jmoiron/sqlx Retrieves a single row from a query within a transaction. An error is returned if the result set is empty. Placeholder parameters are replaced with supplied arguments. ```go func (tx *Tx) Get(dest interface{}, query string, args ...interface{}) error ``` -------------------------------- ### PreparexContext Function in SQLx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Prepares a statement with context, returning an sqlx.Stmt. The context is used for preparation, not execution. ```go func (db *DB) PreparexContext(ctx context.Context, query string) (*Stmt, error) ``` -------------------------------- ### Get Database Driver Bind Type (Go) Source: https://pkg.go.dev/github.com/jmoiron/sqlx Retrieves the bind type configured for a given database driver name. This helps in understanding how query parameters are expected by the database. ```go func BindType(driverName string) int ``` -------------------------------- ### Conn.BeginTxx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Begins a transaction using the Conn, returning an *sqlx.Tx instead of *sql.Tx. The provided context is used until commit or rollback. Canceled contexts result in transaction rollback. ```APIDOC ## (*Conn) BeginTxx ### Description BeginTxx begins a transaction and returns an *sqlx.Tx instead of an *sql.Tx. The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginxContext is canceled. ### Signature ```go func (c *Conn) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) ``` ``` -------------------------------- ### Get Row with Context (sqlx.Conn) Source: https://pkg.go.dev/github.com/jmoiron/sqlx Retrieves a single row using the Conn, with context support. Replaces placeholder parameters with supplied arguments. Returns an error if the result set is empty. ```go func (c *Conn) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error ``` -------------------------------- ### Get Single Row in Transaction with Context Source: https://pkg.go.dev/github.com/jmoiron/sqlx Retrieves a single row from a query within a transaction and context. An error is returned if the result set is empty. Placeholder parameters are replaced with supplied arguments. ```go func (tx *Tx) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error ``` -------------------------------- ### DB.NewDb Source: https://pkg.go.dev/github.com/jmoiron/sqlx Creates a new DB instance from an existing `sql.DB`. ```APIDOC ## NewDb newDb(db *sql.DB, driverName string) *DB ### Description Creates a new `DB` instance from an existing `sql.DB` object and a driver name. This allows you to use sqlx with an already opened database connection. ### Parameters #### Path Parameters - **db** (*sql.DB) - Required - An existing `sql.DB` object. - **driverName** (string) - Required - The name of the database driver. ``` -------------------------------- ### QueryxContext for Database Queries Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query that returns rows, accepting context and arguments. Returns an *sqlx.Rows for flexible data retrieval. ```go func (c *Conn) QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) ``` -------------------------------- ### Get Single Row (Go) Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query and scans the resulting single row into the provided destination. Handles both scannable types and struct scanning. Returns sql.ErrNoRows if the result set is empty. ```go func Get(q Queryer, dest interface{}, query string, args ...interface{}) error ``` -------------------------------- ### PreparexContext Source: https://pkg.go.dev/github.com/jmoiron/sqlx Prepares a statement with context and returns an sqlx.Stmt instead of a standard sql.Stmt. The provided context is used for the preparation of the statement, not for the execution of the statement. ```APIDOC ## PreparexContext ### Description Prepares a statement with context and returns an sqlx.Stmt instead of a standard sql.Stmt. The provided context is used for the preparation of the statement, not for the execution of the statement. ### Method `func (db *DB) PreparexContext(ctx context.Context, query string) (*Stmt, error)` ### Parameters - **ctx** (context.Context) - The context for preparing the statement. - **query** (string) - The SQL query to prepare. ### Returns - **(*Stmt)** - A pointer to an sqlx.Stmt. - **error** - An error if the statement preparation fails. ``` -------------------------------- ### Preparex Source: https://pkg.go.dev/github.com/jmoiron/sqlx Prepares a statement and returns an sqlx.Stmt instead of a standard sql.Stmt. ```APIDOC ## Preparex ### Description Prepares a statement and returns an sqlx.Stmt instead of a standard sql.Stmt. ### Method `func (db *DB) Preparex(query string) (*Stmt, error)` ### Parameters - **query** (string) - The SQL query to prepare. ### Returns - **(*Stmt)** - A pointer to an sqlx.Stmt. - **error** - An error if the statement preparation fails. ``` -------------------------------- ### Conn.PreparexContext Source: https://pkg.go.dev/github.com/jmoiron/sqlx Prepares a statement with context using a connection. ```APIDOC ## PreparexContext preparexContext(c *Conn, ctx context.Context, query string) (*Stmt, error) ### Description Prepares a statement with context using a connection. Returns a `Stmt` object that can be used to execute the prepared statement multiple times. ### Parameters #### Path Parameters - **c** (*Conn) - Required - The connection object. - **ctx** (context.Context) - Required - The context for preparing the statement. - **query** (string) - Required - The SQL query to prepare. ``` -------------------------------- ### Execute Query and Panic on Error (Go) Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query using the provided Execer and panics if an error occurs. Placeholder parameters are replaced with the supplied arguments. ```go func MustExec(e Execer, query string, args ...interface{}) sql.Result ``` -------------------------------- ### Select Data with Statement and Context Source: https://pkg.go.dev/github.com/jmoiron/sqlx Selects data using a prepared statement with context support, mapping results to the destination interface. Placeholder parameters are replaced with supplied arguments. ```go func (s *Stmt) SelectContext(ctx context.Context, dest interface{}, args ...interface{}) error ``` -------------------------------- ### DB.MustConnect Source: https://pkg.go.dev/github.com/jmoiron/sqlx Establishes a database connection, panicking on error. ```APIDOC ## MustConnect mustConnect(driverName, dataSourceName string) *DB ### Description Establishes a database connection. It panics if an error occurs during the connection process. ### Parameters #### Path Parameters - **driverName** (string) - Required - The name of the database driver (e.g., "postgres", "mysql"). - **dataSourceName** (string) - Required - The connection string for the database. ``` -------------------------------- ### func (*Conn) SelectContext Source: https://pkg.go.dev/github.com/jmoiron/sqlx SelectContext using this Conn. Any placeholder parameters are replaced with supplied args. ```APIDOC ## func (*Conn) SelectContext ### Description SelectContext using this Conn. Any placeholder parameters are replaced with supplied args. ### Signature ```go func (c *Conn) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error ``` ``` -------------------------------- ### Select Data with Statement Source: https://pkg.go.dev/github.com/jmoiron/sqlx Selects data using a prepared statement, mapping results to the destination interface. Placeholder parameters are replaced with supplied arguments. ```go func (s *Stmt) Select(dest interface{}, args ...interface{}) error ``` -------------------------------- ### Stmt.Get Method Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query using the prepared statement, replacing placeholders with supplied args. Returns an error if the result set is empty. ```go func (s *Stmt) Get(dest interface{}, args ...interface{}) error ``` -------------------------------- ### Preparex Source: https://pkg.go.dev/github.com/jmoiron/sqlx Preparex prepares a statement with named parameters. ```APIDOC ## Preparex ### Description Prepares a statement with named parameters. ### Method func Preparex(p Preparer, query string) (*Stmt, error) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **p** (Preparer) - Required - The database preparer. - **query** (string) - Required - The SQL query with named parameters. ### Request Example ```go // Example usage would depend on the database preparer ``` ### Response #### Success Response (200) - **Stmt** - The prepared statement. - **error** - An error if preparation failed. #### Response Example ```go // Example response would be a *Stmt object or an error ``` ``` ```APIDOC ## PreparexContext ### Description PreparexContext prepares a statement with named parameters and context. ### Method func PreparexContext(ctx context.Context, p PreparerContext, query string) (*Stmt, error) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **ctx** (context.Context) - Required - The context for the operation. - **p** (PreparerContext) - Required - The database preparer with context. - **query** (string) - Required - The SQL query with named parameters. ### Request Example ```go // Example usage would depend on the database preparer ``` ### Response #### Success Response (200) - **Stmt** - The prepared statement. - **error** - An error if preparation failed. #### Response Example ```go // Example response would be a *Stmt object or an error ``` ``` -------------------------------- ### LoadFile Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes all SQL statements in a given file. Suitable for schema initialization or index loading, but not for large data dumps. ```APIDOC ## LoadFile ### Description LoadFile exec's every statement in a file (as a single call to Exec). LoadFile may return a nil *sql.Result if errors are encountered locating or reading the file at path. LoadFile reads the entire file into memory, so it is not suitable for loading large data dumps, but can be useful for initializing schemas or loading indexes. ### Method func LoadFile(e Execer, path string) (*sql.Result, error) ### Parameters #### Path Parameters - **e** (Execer) - Required - The execer interface. - **path** (string) - Required - The path to the SQL file. ### Returns - **(*sql.Result)**: The result of the executed statements, or nil if errors occurred. - **error**: An error if the file cannot be read or statements fail to execute. ``` -------------------------------- ### DB.MustOpen Source: https://pkg.go.dev/github.com/jmoiron/sqlx Opens a database connection, panicking on error. ```APIDOC ## MustOpen mustOpen(driverName, dataSourceName string) *DB ### Description Opens a database connection. It panics if an error occurs during the opening process. This is a convenience function that wraps `sql.Open` and `NewDb`. ### Parameters #### Path Parameters - **driverName** (string) - Required - The name of the database driver (e.g., "postgres", "mysql"). - **dataSourceName** (string) - Required - The connection string for the database. ``` -------------------------------- ### Query Row with Context (sqlx.Conn) Source: https://pkg.go.dev/github.com/jmoiron/sqlx Queries the database using the Conn and returns an sqlx.Row, with context support. Replaces placeholder parameters with supplied arguments. ```go func (c *Conn) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *Row ``` -------------------------------- ### PreparexContext Function Source: https://pkg.go.dev/github.com/jmoiron/sqlx Prepares a statement using the PreparerContext interface. The provided context is used for preparation, not execution. ```go func PreparexContext(ctx context.Context, p PreparerContext, query string) (*Stmt, error) ``` -------------------------------- ### DB.Beginx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Begins a transaction on a DB object. ```APIDOC ## Beginx beginx(db *DB) (*Tx, error) ### Description Begins a transaction on a `DB` object. Returns a `Tx` object that can be used to execute queries within the transaction. ### Parameters #### Path Parameters - **db** (*DB) - Required - The DB object. ``` -------------------------------- ### DB.MustBegin Source: https://pkg.go.dev/github.com/jmoiron/sqlx Begins a transaction on a DB object, panicking on error. ```APIDOC ## MustBegin mustBegin(db *DB) *Tx ### Description Begins a transaction on a `DB` object. It panics if an error occurs during the transaction initiation. ### Parameters #### Path Parameters - **db** (*DB) - Required - The DB object. ``` -------------------------------- ### Select Source: https://pkg.go.dev/github.com/jmoiron/sqlx Select using this NamedStmt. Any named placeholder parameters are replaced with fields from arg. ```APIDOC ## Select ### Description Select using this NamedStmt. Any named placeholder parameters are replaced with fields from arg. ### Signature ```go func (n *NamedStmt) Select(dest interface{}, arg interface{}) error ``` ``` -------------------------------- ### Prepare Statement in Transaction with Context Source: https://pkg.go.dev/github.com/jmoiron/sqlx Prepares a standard statement to be executed within this transaction, using the provided context for preparation. ```go func (tx *Tx) PreparexContext(ctx context.Context, query string) (*Stmt, error) ``` -------------------------------- ### func (*DB) Beginx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Beginx begins a transaction and returns an *sqlx.Tx instead of an *sql.Tx. ```APIDOC ## func (*DB) Beginx ### Description Beginx begins a transaction and returns an *sqlx.Tx instead of an *sql.Tx. ### Signature ```go func (db *DB) Beginx() (*Tx, error) ``` ``` -------------------------------- ### LoadFile Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes SQL statements from a file. ```APIDOC ## LoadFile loadFile(e Execer, path string) (*sql.Result, error) ### Description Executes SQL statements contained within a file. This is useful for running migration scripts or seeding data. ### Parameters #### Path Parameters - **e** (Execer) - Required - An object that implements the `Execer` interface. - **path** (string) - Required - The file path to the SQL script. ``` -------------------------------- ### func MustOpen Source: https://pkg.go.dev/github.com/jmoiron/sqlx MustOpen is the same as sql.Open, but returns an *sqlx.DB instead and panics on error. ```APIDOC ## func MustOpen ### Description MustOpen is the same as sql.Open, but returns an *sqlx.DB instead and panics on error. ### Signature ```go func MustOpen(driverName, dataSourceName string) *DB ``` ``` -------------------------------- ### DB.Preparex Source: https://pkg.go.dev/github.com/jmoiron/sqlx Prepares a statement using a DB object. ```APIDOC ## Preparex preparex(db *DB, query string) (*Stmt, error) ### Description Prepares a statement using a `DB` object. Returns a `Stmt` object that can be used to execute the prepared statement multiple times. ### Parameters #### Path Parameters - **db** (*DB) - Required - The DB object. - **query** (string) - Required - The SQL query to prepare. ``` -------------------------------- ### Execute Query and Panic on Error (Context) Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query with context and panics if an error occurs. Useful for operations where failure is unrecoverable. ```go func MustExecContext(ctx context.Context, e ExecerContext, query string, args ...interface{}) sql.Result ``` -------------------------------- ### Load SQL Statements from File (Go) Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes all SQL statements within a given file as a single operation. Not suitable for large data dumps due to reading the entire file into memory. May return a nil result if file errors occur. ```go func LoadFile(e Execer, path string) (*sql.Result, error) ``` -------------------------------- ### DB.Open Source: https://pkg.go.dev/github.com/jmoiron/sqlx Opens a database connection. ```APIDOC ## Open open(driverName, dataSourceName string) (*DB, error) ### Description Opens a database connection using the provided driver name and data source name. Returns a `DB` object that can be used to interact with the database. ### Parameters #### Path Parameters - **driverName** (string) - Required - The name of the database driver (e.g., "postgres", "mysql"). - **dataSourceName** (string) - Required - The connection string for the database. ``` -------------------------------- ### DB.PreparexContext Source: https://pkg.go.dev/github.com/jmoiron/sqlx Prepares a statement with context using a DB object. ```APIDOC ## PreparexContext preparexContext(db *DB, ctx context.Context, query string) (*Stmt, error) ### Description Prepares a statement with context using a `DB` object. Returns a `Stmt` object that can be used to execute the prepared statement multiple times. ### Parameters #### Path Parameters - **db** (*DB) - Required - The DB object. - **ctx** (context.Context) - Required - The context for preparing the statement. - **query** (string) - Required - The SQL query to prepare. ``` -------------------------------- ### Queryx Function in SQLx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Queries the database and returns multiple rows as an *sqlx.Rows. Placeholder parameters are replaced with supplied arguments. ```go func (db *DB) Queryx(query string, args ...interface{}) (*Rows, error) ``` -------------------------------- ### QueryxContext Source: https://pkg.go.dev/github.com/jmoiron/sqlx Queries the database with context and returns an *sqlx.Rows. Any placeholder parameters are replaced with supplied args. ```APIDOC ## QueryxContext ### Description Queries the database with context and returns an *sqlx.Rows. Any placeholder parameters are replaced with supplied args. ### Method `func (db *DB) QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)` ### Parameters - **ctx** (context.Context) - The context for the query. - **query** (string) - The SQL query to execute. - **args** (...interface{}) - Arguments to replace placeholder parameters in the query. ### Returns - **(*Rows)** - A pointer to sqlx.Rows containing the query results. - **error** - An error if the query execution fails. ``` -------------------------------- ### Stmt.GetContext Method Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query with context using the prepared statement, replacing placeholders with supplied args. Returns an error if the result set is empty. ```go func (s *Stmt) GetContext(ctx context.Context, dest interface{}, args ...interface{}) error ``` -------------------------------- ### MustExecContext Query Execution with Context (Panic) Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a query with context support that does not return rows, panicking on error. Any placeholder parameters are replaced with supplied args. ```go func (db *DB) MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result ``` -------------------------------- ### QueryxContext Function in SQLx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Queries the database with context and returns multiple rows as an *sqlx.Rows. Placeholder parameters are replaced with supplied arguments. ```go func (db *DB) QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) ``` -------------------------------- ### func (*DB) GetContext Source: https://pkg.go.dev/github.com/jmoiron/sqlx GetContext using this DB. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty. ```APIDOC ## func (*DB) GetContext ### Description GetContext using this DB. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty. ### Signature ```go func (db *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error ``` ``` -------------------------------- ### Stmt.MustExecContext Method Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes a statement with context, panicking on error. Placeholder parameters are replaced with supplied args. The query portion of the error output will be blank. ```go func (s *Stmt) MustExecContext(ctx context.Context, args ...interface{}) sql.Result ``` -------------------------------- ### LoadFileContext Source: https://pkg.go.dev/github.com/jmoiron/sqlx Executes SQL statements from a file with context. ```APIDOC ## LoadFileContext loadFileContext(ctx context.Context, e ExecerContext, path string) (*sql.Result, error) ### Description Executes SQL statements contained within a file with context support, allowing for cancellation and timeouts. ### Parameters #### Path Parameters - **ctx** (context.Context) - Required - The context for the operation. - **e** (ExecerContext) - Required - An object that implements the `ExecerContext` interface. - **path** (string) - Required - The file path to the SQL script. ``` -------------------------------- ### Preparex Functions Source: https://pkg.go.dev/github.com/jmoiron/sqlx Functions for preparing SQL statements with extended functionality. ```APIDOC ## Preparex Functions ### Description These functions prepare SQL statements with extended capabilities, returning a `Stmt` object. ### Functions - `Preparex(p Preparer, query string) (*Stmt, error)`: Prepares a statement using the provided `Preparer`. - `PreparexContext(ctx context.Context, p PreparerContext, query string) (*Stmt, error)`: Prepares a statement with context support using the provided `PreparerContext`. The context is used for the preparation itself, not for statement execution. ``` -------------------------------- ### func (*DB) BeginTxx Source: https://pkg.go.dev/github.com/jmoiron/sqlx BeginTxx begins a transaction and returns an *sqlx.Tx instead of an *sql.Tx. The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginxContext is canceled. ```APIDOC ## func (*DB) BeginTxx ### Description BeginTxx begins a transaction and returns an *sqlx.Tx instead of an *sql.Tx. The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginxContext is canceled. ### Signature ```go func (db *DB) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) ``` ``` -------------------------------- ### Conn.PreparexContext Source: https://pkg.go.dev/github.com/jmoiron/sqlx Prepares a statement using this Conn, returning an sqlx.Stmt instead of a sql.Stmt. The provided context is used for statement preparation, not execution. ```APIDOC ## (*Conn) PreparexContext ### Description PreparexContext returns an sqlx.Stmt instead of a sql.Stmt. The provided context is used for the preparation of the statement, not for the execution of the statement. ### Signature ```go func (c *Conn) PreparexContext(ctx context.Context, query string) (*Stmt, error) ``` ``` -------------------------------- ### PrepareNamedContext Function in SQLx Source: https://pkg.go.dev/github.com/jmoiron/sqlx Prepares a named statement with context. Returns an sqlx.NamedStmt, allowing for context-aware preparation. ```go func (db *DB) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error) ```