lsql

package module
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: MIT Imports: 3 Imported by: 0

README

litsql-db lsql

Test Status GitHub go.mod Go version Go Reference GitHub tag (latest SemVer)

Golang stdlib sql DB wrappers for litsql.

Installation

go get -u github.com/rrgmc/litsql-db/lsql

Examples

Query
func ExampleDB() {
    ctx := context.Background()

    db, err := sql.Open("test", ":memory:")
    if err != nil {
        panic(err)
    }

    // wrap *sql.DB instance
    ddb := lsql.NewDB(db)

    query := psql.Select(
        sm.Columns("film_id", "title", "length"),
        sm.From("film"),
        sm.WhereClause("length > ?", sq.NamedArg("length")),
        sm.Limit(10),
    )

    // generate SQL string from litsql and execute it, replacing named parameters.
    rows, err := ddb.Query(ctx, query, map[string]any{
        "length": 90,
    })
    if err != nil {
        panic(err)
    }
    defer rows.Close()

    for rows.Next() {
        var film_id, length int
        var title string
        err = rows.Scan(&film_id, &title, &length)
        if err != nil {
            panic(err)
        }
    }

    if rows.Err() != nil {
        panic(err)
    }
}
Prepared statement
func ExampleStmt() {
    ctx := context.Background()

    db, err := sql.Open("test", ":memory:")
    if err != nil {
        panic(err)
    }

    // wrap *sql.DB instance
    ddb := lsql.NewDB(db)

    query := psql.Select(
        sm.Columns("film_id", "title", "length"),
        sm.From("film"),
        sm.WhereClause("length > ?", sq.NamedArg("length")),
        sm.Limit(10),
    )

    // generate SQL string from litsql and prepare it, storing the named parameters to be replaced later
    dstmt, err := ddb.Prepare(ctx, query)
    if err != nil {
        panic(err)
    }

    // execute prepared query, replacing named parameters
    rows, err := dstmt.Query(ctx, map[string]any{
        "length": 90,
    })
    if err != nil {
        panic(err)
    }
    defer rows.Close()

    for rows.Next() {
        var film_id, length int
        var title string
        err = rows.Scan(&film_id, &title, &length)
        if err != nil {
            panic(err)
        }
    }

    if rows.Err() != nil {
        panic(err)
    }
}

Author

Rangel Reale (rangelreale@gmail.com)

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB = lsqlt.DB[*sql.DB]

DB wraps a sql.DB.

Example
package main

import (
	"context"
	"database/sql"

	"github.com/rrgmc/litsql-db/lsql"
	"github.com/rrgmc/litsql/dialect/psql"
	"github.com/rrgmc/litsql/dialect/psql/sm"
	"github.com/rrgmc/litsql/sq"
)

func main() {
	ctx := context.Background()

	db, err := sql.Open("test", ":memory:")
	if err != nil {
		panic(err)
	}

	// wrap *sql.DB instance
	ddb := lsql.NewDB(db)

	query := psql.Select(
		sm.Columns("film_id", "title", "length"),
		sm.From("film"),
		sm.WhereClause("length > ?", sq.NamedArg("length")),
		sm.Limit(10),
	)

	// generate SQL string from litsql and execute it, replacing named parameters.
	rows, err := ddb.Query(ctx, query, sq.MapArgValues{
		"length": 90,
	})
	if err != nil {
		panic(err)
	}
	defer rows.Close()

	for rows.Next() {
		var film_id, length int
		var title string
		err = rows.Scan(&film_id, &title, &length)
		if err != nil {
			panic(err)
		}
	}

	if rows.Err() != nil {
		panic(err)
	}
}
Output:

func NewDB

func NewDB(db *sql.DB, options ...Option) *DB

NewDB wraps a sql.DB.

type Option

type Option = lsqlt.Option

func WithQueryHandler

func WithQueryHandler(queryHandler sq.Handler) Option

type Querier

type Querier = lsqlt.Querier

type QuerierDB

type QuerierDB = lsqlt.QuerierDB

type QuerierStmt

type QuerierStmt = lsqlt.QuerierStmt

type QuerierTx

type QuerierTx = lsqlt.QuerierTx

type Stmt

type Stmt = lsqlt.Stmt[*sql.Stmt]

Stmt wraps a sql.Stmt.

Example
package main

import (
	"context"
	"database/sql"

	"github.com/rrgmc/litsql-db/lsql"
	"github.com/rrgmc/litsql/dialect/psql"
	"github.com/rrgmc/litsql/dialect/psql/sm"
	"github.com/rrgmc/litsql/sq"
)

func main() {
	ctx := context.Background()

	db, err := sql.Open("test", ":memory:")
	if err != nil {
		panic(err)
	}

	// wrap *sql.DB instance
	ddb := lsql.NewDB(db)

	query := psql.Select(
		sm.Columns("film_id", "title", "length"),
		sm.From("film"),
		sm.WhereClause("length > ?", sq.NamedArg("length")),
		sm.Limit(10),
	)

	// generate SQL string from litsql and prepare it, storing the named parameters to be replaced later
	dstmt, err := ddb.Prepare(ctx, query)
	if err != nil {
		panic(err)
	}

	// execute prepared query, replacing named parameters
	rows, err := dstmt.Query(ctx, sq.MapArgValues{
		"length": 90,
	})
	if err != nil {
		panic(err)
	}
	defer rows.Close()

	for rows.Next() {
		var film_id, length int
		var title string
		err = rows.Scan(&film_id, &title, &length)
		if err != nil {
			panic(err)
		}
	}

	if rows.Err() != nil {
		panic(err)
	}
}
Output:

func NewStmt

func NewStmt(stmt *sql.Stmt, args []any, options ...Option) *Stmt

NewStmt wraps a sql.Stmt.

type Tx

type Tx = lsqlt.Tx[*sql.Tx]

Tx wraps a sql.Tx.

func NewTx

func NewTx(tx *sql.Tx, options ...Option) *Tx

NewTx wraps a sql.Tx.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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