Documentation ¶
Index ¶
- type EasySqlite
- func (s *EasySqlite) DoInTx(ctx context.Context, callback func(ctx context.Context) error) error
- func (s *EasySqlite) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (s *EasySqlite) GetContext(ctx context.Context, dest any, query string, args ...any) error
- func (s *EasySqlite) MustExecContext(ctx context.Context, query string, args ...any) sql.Result
- func (s *EasySqlite) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
- func (s *EasySqlite) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (s *EasySqlite) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (s *EasySqlite) SelectContext(ctx context.Context, dest any, query string, args ...any) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EasySqlite ¶
type EasySqlite struct {
// contains filtered or unexported fields
}
func New ¶
New opens the db file or creates it if not exists Migrations should be embedded by the go "embed" package Check out the example of migrations in the cmd/example folder The migration tool used here is https://github.com/pressly/goose
Example ¶
//nolint:testableexamples package main import ( "context" "embed" easysqlite "github.com/pav5000/easy-sqlite" ) // embeding migrations folder into executable binary // the path should be relative to your source file // //go:embed migrations/*.sql var embedMigrations embed.FS func main() { // Creating conn connection conn, err := easysqlite.New("db.sqlite", embedMigrations, "migrations") if err != nil { panic(err) } // Inserting records ctx := context.Background() _, err = conn.ExecContext(ctx, `INSERT INTO users (name,age) VALUES(?,?)`, "John", 23) if err != nil { panic(err) } // User represents one row of the table "users" type User struct { ID int64 `db:"id"` Name string `db:"name"` Age int `db:"age"` } // Getting one row var user User err = conn.GetContext(ctx, &user, `SELECT id,name,age FROM users WHERE id=?`, 1) if err != nil { panic(err) } // Selecting many rows var users []User err = conn.SelectContext(ctx, &users, `SELECT id,name,age FROM users`) if err != nil { panic(err) } }
Output:
func (*EasySqlite) DoInTx ¶
DoInTx starts transaction and guarantees that all queries in easysqlite methods will use it as long as you use the context provided into the callback function.
func (*EasySqlite) ExecContext ¶
func (*EasySqlite) GetContext ¶
func (*EasySqlite) MustExecContext ¶
func (*EasySqlite) PrepareContext ¶
func (*EasySqlite) QueryContext ¶
func (*EasySqlite) QueryRowContext ¶
func (*EasySqlite) SelectContext ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
internal
|
|
errors
errors provides a couple of custom useful error handling functions parts of the code are taken from github.com/pkg/errors the pkg/errors repo is archived and deprecated so I didn't want to import it
|
errors provides a couple of custom useful error handling functions parts of the code are taken from github.com/pkg/errors the pkg/errors repo is archived and deprecated so I didn't want to import it |
Click to show internal directories.
Click to hide internal directories.