sqllib

package module
v0.0.0-...-7a0f823 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2018 License: MIT Imports: 3 Imported by: 1

README

sqllib

Maintain a library of prepared SQL statements (*sql.Stmt)

Build Status

GoDoc

WARNING

This repository has been moved to github.com/lestrrat-go/sqllib. This repository exists so that libraries pointing to this URL will keep functioning, but this repository will NOT be updated in the future. Please use the new import path.

SYNOPSIS

import (
  "github.com/lestrrat/go-sqllib"
  "github.com/pkg/errors"
)

var lib *sqllib.Library
var db *sql.DB

func InitializeDB() {
  db, _ = sql.Open(...)

  lib = sqllib.New(db)

  // Register some SQL queries by name
  lib.Register("Simple Select", "SELECT foo FROM bar WHERE a = ?")
}

func SomeFunc(tx *sql.Tx, arg string) error {
  // When you access the SQL query again, you can ask for an
  // already prepared statement.
  stmt, err := lib.GetStmt("Simple Select")
  if err != nil {
    return errors.Wrap(err, "failed to get statement")
  }

  // Don't forget to call (*sql.Tx).Stmt on it to make a 
  // transaction-specific statement
  rows, err := tx.Stmt(stmt).Query(arg)
  ...
}

DESCRIPTION

Using prepared statements repeatedly is usually better for performance.

Keeping prepared statements around for reuse is fairly painful. This library is a very small utility to store SQL queries and refer to them by name to get back already prepared statement.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB interface {
	Prepare(string) (*sql.Stmt, error)
}

DB is declared so we can mock-test. You do not really have to think about this. Just assume it's the value returned from database/sql.Open

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

Entry is an SQL statement registered to the library. This holds a reference to the prepared SQL statement (*sql.Stmt), but the statement is only prepared lazily.

type Library

type Library struct {
	// contains filtered or unexported fields
}

Library represents the top-level structure that holds the SQL statements.

func New

func New(db DB) *Library

func (*Library) GetStmt

func (l *Library) GetStmt(key interface{}) (*sql.Stmt, error)

func (*Library) Register

func (l *Library) Register(key interface{}, sql string) error

Jump to

Keyboard shortcuts

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