Coder Social home page Coder Social logo

sqlhooks's Introduction

sqlhooks Build Status Coverage Status

Attach hooks to any database/sql driver.

The purpose of sqlhooks is to provide a way to instrument your sql statements, making really easy to log queries or measure execution time without modifying your actual code.

Install

go get github.com/gchaincl/sqlhooks

Usage GoDoc

	package main

	import (
		"log"
		"time"

		"github.com/gchaincl/sqlhooks"
		_ "github.com/mattn/go-sqlite3"
	)

	// Hooks satisfies sqlhooks.Queryer interface
	type Hooks struct {
		count int
	}

	func (h *Hooks) BeforeQuery(ctx *sqlhooks.Context) error {
		h.count++
		ctx.Set("t", time.Now())
		ctx.Set("id", h.count)
		log.Printf("[query#%d] %s, args: %v", ctx.Get("id").(int), ctx.Query, ctx.Args)
		return nil
	}

	func (h *Hooks) AfterQuery(ctx *sqlhooks.Context) error {
		d := time.Since(ctx.Get("t").(time.Time))
		log.Printf("[query#%d] took %s (err: %v)", ctx.Get("id").(int), d, ctx.Error)
		return ctx.Error
	}

	func main() {
		db, _ := sqlhooks.Open("sqlite3", ":memory:", &Hooks{})

		// Do you're stuff
		db.Exec("CREATE TABLE t (id INTEGER, text VARCHAR(16))")
		db.Exec("INSERT into t (text) VALUES(?), (?)", "foo", "bar")
		db.Query("SELECT id, text FROM t")
		db.Query("Invalid Query")
	}
2016/06/02 14:28:24 [query#1] SELECT id, text FROM t, args: []
2016/06/02 14:28:24 [query#1] took 122.406µs (err: <nil>)
2016/06/02 14:28:24 [query#2] Invalid Query, args: []
2016/06/02 14:28:24 [query#2] took 23.148µs (err: near "Invalid": syntax error)

Benchmark

PASS
BenchmarkExec-4                    	  500000	      4604 ns/op
BenchmarkExecWithSQLHooks-4        	  300000	      5726 ns/op
BenchmarkPreparedExec-4            	 1000000	      1820 ns/op
BenchmarkPreparedExecWithSQLHooks-4	 1000000	      2088 ns/op

sqlhooks's People

Watchers

James Cloos avatar  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.