sqlargs

package module
v0.0.0-...-fef82e8 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2019 License: MIT Imports: 10 Imported by: 2

README

sqlargs Build Status

A vet analyzer which checks sql(only Postgres!) queries for correctness.

Background

Let's assume you have a query like:

db.Exec("insert into table (c1, c2, c3, c4) values ($1, $2, $3, $4)", p1, p2, p3, p4).

It's the middle of the night and you need to add a new column. You quickly change the query to -

db.Exec("insert into table (c1, c2, c3, c4, c5) values ($1, $2, $3, $4)", p1, p2, p3, p4, p5).

Everything compiles fine. Except it's not ! A $5 is missing. It can even go the other way round; you add the $5 but forget to add c5.

This is a semantic error which will eventually get caught while running the app. Not to mention, if there are tests. But sometimes I get lazy and don't write tests for all my sql queries. 😅

sqlargs will statically check for semantic errors like these and flag them beforehand.

Quick start

This is written using the go/analysis API. So you can plug this directly into go vet, or you can run it as a standalone tool too.

Install:

go get github.com/agnivade/sqlargs/cmd/sqlargs

And then run it on your repo:

go vet -vettool $(which sqlargs) ./... # Has to be >= 1.12
OR
sqlargs ./...

P.S.: This only works for Postgres queries. So if your codebase has queries which do not match with the postgres query parser, it might flag incorrect errors.

Documentation

Index

Constants

View Source
const Doc = `` /* 275-byte string literal not displayed */

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:             "sqlargs",
	Doc:              Doc,
	Run:              run,
	Requires:         []*analysis.Analyzer{inspect.Analyzer},
	RunDespiteErrors: true,
}

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd
sqlargs
package main runs the sqlargs analyzer.
package main runs the sqlargs analyzer.

Jump to

Keyboard shortcuts

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