sqlc

package module
v0.0.0-...-99db660 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2014 License: MIT Imports: 2 Imported by: 0

README

SQL Composer

wercker status GoDoc

SQL Composer (sqlc) makes it easier to join together bits of SQL programatically.

It is very, very similar to squirrel but has less features.

Most SQL is:

SELECT * FROM Users WHERE Name=?

SQL Composer isn't for that. You should keep using string literals for that.

SQL Composer is for when you are putting together many different bits of a query together programatically, and where you'd usually use some kind of intelligent string replacement. In our use case, we often do this for searches. SQL Composer lets you do this slightly differently:

s := sqlc.Statement{}
s.Select("u.*").From("Users u").Where("u.Name = ?", name)

if search.Surname != "" {
  s = s.Where("u.Surname = ?", search.Surname)
}
if search.Role != "" {
  s = s.Join("JOIN Roles r ON u.role_id = r.id")
  s = s.Where("r.Name = ?", search.Role)
}

db.Exec(s.SQL(), s.Args()...)

Assuming that Surname and Role are supplied, calling s.SQL() gives you the sql:

SELECT u.*
FROM Users u
JOIN Roles r ON u.role_id = r.id
WHERE (u.Name = ?) AND (u.Surname = ?) AND (r.Name = ?)

And calling s.Args() gives you name, search.Surname and search.Role.

Features

Postgres Positional Arguments

PostgreSQL, unlike MySQL and sqlite, uses $1, $2, $3 in favour of ?, ?, ? for its positional arguments. SQL Composer will manage this for you:

s := sqlc.Statement{PostgreSQL: true}
s.Where("Foo = ?", foo).Where("Bar = ?", bar)

gives

WHERE (Foo = $1) AND (Bar = $2)
Statement Re-Use

Pass-by-value and method chaining allows you to use one base statement in many roles without modifying the original.

For example:

s = sqlc.Statement{}
s.Select("*").From("Users")

topFiveRows := s.Limit(5).SQL() // SELECT * FROM Users LIMIT 5
allRows := s.SQL()              // SELECT * FROM Users

Documentation

Overview

Package sqlc helps you combine arbitrary bits of SQL

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Statement

type Statement struct {
	// PostgreSQL replaces "?, ?" with "$1, $2" if true
	PostgreSQL bool
	// contains filtered or unexported fields
}

Statement is a SQL string being built

func (Statement) Args

func (s Statement) Args() []interface{}

Args returns positional arguments in the order they will appear in the SQL.

func (Statement) From

func (s Statement) From(partial string, args ...interface{}) Statement

From adds a FROM stanza, joined by commas

func (Statement) Group

func (s Statement) Group(partial string, args ...interface{}) Statement

Group adds a GROUP BY stanza, joined by commas

func (Statement) Having

func (s Statement) Having(partial string, args ...interface{}) Statement

Having adds a HAVING stanza, wrapped in brackets and joined by AND

func (Statement) Join

func (s Statement) Join(partial string, args ...interface{}) Statement

Join adds a JOIN stanza, joined by spaces Unlike other stanzas, you need to specify the JOIN/INNER JOIN/LEFT JOIN bit yourself.

func (Statement) Limit

func (s Statement) Limit(partial string, args ...interface{}) Statement

Limit sets or overwrites the LIMIT stanza

func (Statement) Order

func (s Statement) Order(partial string, args ...interface{}) Statement

Order adds an ORDER BY stanza, joined by commas

func (Statement) SQL

func (s Statement) SQL() string

SQL joins your stanzas, returning the composed SQL.

func (Statement) Select

func (s Statement) Select(partial string, args ...interface{}) Statement

Select adds a SELECT stanza, joined by commas

func (Statement) Where

func (s Statement) Where(partial string, args ...interface{}) Statement

Where adds a WHERE stanza, wrapped in brackets and joined by AND

Jump to

Keyboard shortcuts

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