GithubHelp home page GithubHelp logo

stmtcacher's Introduction

stmtcacher

Prepared statement caching for go's sql.DB.

Godoc

CachingWrapper

Wrapper around DB with additional functions that explicitly reuse cached prepared statements. All sql.DB query functions are wrapped and available with the suffix "Prepared", e.g. QueryPrepared, ExecPrepared

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

wrapper := stmtcacher.NewCachingWrapper(db)

query := "SELECT * FROM table where id = ?"

// By default, sql.DB will perform 3 statements and roundtrips
db.Query(query, "foo")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt1, "foo"
// CLOSE stmt1
db.Query(query, "bar")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt2, "bar"
// CLOSE

// With stmtcacher, they're cached
wrapper.QueryPrepared(query, "foo")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt1, "foo"
wrapper.QueryPrepared(query, "bar")
// EXECUTE stmt1, "bar"
wrapper.QueryPrepared(query, "baz")
// EXECUTE stmt1, "baz"

// sql.DB is embedded and available to
// easily opt-in or out of the behavior
wrapper.Query(query, "bar")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt2, "bar"
// CLOSE

CachingProxy

Caching proxy and wrapper around sql.DB. Exec, Query, QueryRow, ExecContext, QueryContext, and QueryRowContext are each wrapped to cache and reuse prepared statements

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

proxy := stmtcacher.NewCachingProxy(db)

query := "SELECT * FROM table where name = ?"

// By default, sql.DB will perform 3 statements and roundtrips
db.Query(query, "foo")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt1, "foo"
// CLOSE stmt1
db.Query(query, "bar")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt2, "bar"
// CLOSE

// With stmtcacher, they're cached
proxy.Query(query, "foo")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt1, "foo"
proxy.Query(query, "bar")
// EXECUTE stmt1, "bar"
proxy.Query(query, "baz")
// EXECUTE stmt1, "baz"

Comparison

Inspired by: https://github.com/Masterminds/squirrel

Compared to squirrel, this pkg has:

  • Better error handling
  • Doesn't enforce squirrel-specific types like RowScanner
  • Easier use with other pkgs that wrap or work with sql.DB
  • Offers both a wrapper with additional fns, as well as caching proxy

stmtcacher's People

Contributors

danielstjules avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.