asql

package module
v0.0.0-...-3e0fd1c Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: MIT Imports: 5 Imported by: 0

README

Go - Asql

Go Report Card License MIT Go Doc

A Go package that makes it easier to run SQL queries async

Install

go get -u github.com/danielgatis/go-asql

And then import the package in your code:

import "github.com/danielgatis/go-asql"
Usage
package main

import (
	"fmt"

	"github.com/danielgatis/go-asql"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	type TestTable struct {
		ID   int
		Name string
	}

	db, _ := asql.Open("sqlite3", "file::memory:")
	db.Load("testdata/schema.sql")

	rc, _ := db.Query(`select * from test_table`)
	rows := <-rc

	records := make([]TestTable, 0)

	for rows.Next() {
		var record TestTable
		rows.Scan(&record.ID, &record.Name)
		records = append(records, record)
	}

	fmt.Print(records)
}
License

Copyright (c) 2023-present Daniel Gatis

Licensed under MIT License

Buy me a coffee

Liked some of my work? Buy me a coffee (or more likely a beer)

Buy Me A Coffee

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	*sql.DB
}

DB is a wrapper around sql.DB that provides asynchronous methods.

func Open

func Open(driverName, dsn string) (*DB, error)

Open opens a database specified by its database driver name and dsn.

func (*DB) BeginTx

func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

BeginTx starts a transaction.

func (*DB) Close

func (db *DB) Close() error

Close closes the database.

func (*DB) Exec

func (db *DB) Exec(query string, args ...any) (<-chan *Result, context.CancelFunc)

Exec executes a query without returning any rows.

func (*DB) ExecContext

func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (<-chan *Result, context.CancelFunc)

ExecContext executes a query without returning any rows.

func (*DB) Load

func (db *DB) Load(path string) error

Load loads a query from a file and executes it.

func (*DB) Ping

func (db *DB) Ping() error

Ping verifies a connection to the database is still alive.

func (*DB) Prepare

func (db *DB) Prepare(query string) (*Stmt, error)

Prepare creates a prepared statement for later queries or executions.

func (*DB) PrepareContext

func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error)

PrepareContext creates a prepared statement for later queries or executions.

func (*DB) Query

func (db *DB) Query(query string, args ...any) (<-chan *Rows, context.CancelFunc)

Query executes a query that returns rows, typically a SELECT.

func (*DB) QueryContext

func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (<-chan *Rows, context.CancelFunc)

QueryContext executes a query that returns rows, typically a SELECT.

func (*DB) QueryRow

func (db *DB) QueryRow(query string, args ...any) (<-chan *Row, context.CancelFunc)

QueryRow executes a query that is expected to return at most one row.

func (*DB) QueryRowContext

func (db *DB) QueryRowContext(
	ctx context.Context,
	query string,
	args ...any,
) (<-chan *Row, context.CancelFunc)

QueryRowContext executes a query that is expected to return at most one row.

type Result

type Result struct {
	sql.Result
	// contains filtered or unexported fields
}

Result is a wrapper around sql.Result that provides an Err method.

func (*Result) Err

func (r *Result) Err() error

Err returns the error, if any, that was encountered during the query.

type Row

type Row struct {
	*sql.Row
}

Row is a wrapper around sql.Row.

type Rows

type Rows struct {
	*sql.Rows
	// contains filtered or unexported fields
}

Rows is a wrapper around sql.Rows that provides an Err method.

func (*Rows) Err

func (rs *Rows) Err() error

Err returns the error, if any, that was encountered during the query.

type Stmt

type Stmt struct {
	*sql.Stmt
	// contains filtered or unexported fields
}

Stmt is a wrapper around sql.Stmt that provides asynchronous methods.

func (*Stmt) Exec

func (s *Stmt) Exec(args ...any) (<-chan *Result, context.CancelFunc)

Exec executes a prepared statement with the given arguments without returning any rows.

func (*Stmt) ExecContext

func (s *Stmt) ExecContext(ctx context.Context, args ...any) (<-chan *Result, context.CancelFunc)

ExecContext executes a prepared statement with the given arguments without returning any rows.

func (*Stmt) Query

func (s *Stmt) Query(args ...any) (<-chan *Rows, context.CancelFunc)

Query executes a prepared query statement with the given arguments.

func (*Stmt) QueryContext

func (s *Stmt) QueryContext(ctx context.Context, args ...any) (<-chan *Rows, context.CancelFunc)

QueryContext executes a prepared query statement with the given arguments.

func (*Stmt) QueryRow

func (s *Stmt) QueryRow(args ...any) (<-chan *Row, context.CancelFunc)

QueryRow executes a prepared query statement with the given arguments.

func (*Stmt) QueryRowContext

func (s *Stmt) QueryRowContext(ctx context.Context, args ...any) (<-chan *Row, context.CancelFunc)

QueryRowContext executes a prepared query statement with the given arguments.

type Tx

type Tx struct {
	*sql.Tx
	// contains filtered or unexported fields
}

Tx is a wrapper around sql.Tx that provides asynchronous methods.

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit commits the transaction.

func (*Tx) Exec

func (tx *Tx) Exec(query string, args ...any) (<-chan *Result, context.CancelFunc)

Exec executes a query without returning any rows.

func (*Tx) ExecContext

func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (<-chan *Result, context.CancelFunc)

ExecContext executes a query without returning any rows.

func (*Tx) Prepare

func (tx *Tx) Prepare(query string) (*Stmt, error)

Prepare creates a prepared statement for later queries or executions.

func (*Tx) PrepareContext

func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error)

PrepareContext creates a prepared statement for later queries or executions.

func (*Tx) Query

func (tx *Tx) Query(query string, args ...any) (<-chan *Rows, context.CancelFunc)

Query executes a query that returns rows, typically a SELECT.

func (*Tx) QueryContext

func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (<-chan *Rows, context.CancelFunc)

QueryContext executes a query that returns rows, typically a SELECT.

func (*Tx) QueryRow

func (tx *Tx) QueryRow(query string, args ...any) (<-chan *Row, context.CancelFunc)

QueryRow executes a query that is expected to return at most one row.

func (*Tx) QueryRowContext

func (tx *Tx) QueryRowContext(
	ctx context.Context,
	query string,
	args ...any,
) (<-chan *Row, context.CancelFunc)

QueryRowContext executes a query that is expected to return at most one row.

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback aborts the transaction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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