dotsql

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2015 License: GPL-2.0 Imports: 8 Imported by: 0

README

dotsql Build Status

A Golang library for using SQL.

It is not an ORM, it is not a query builder. Dotsql is a library that helps you keep sql files in one place and use it with ease.

Dotsql is heavily inspired by yesql.

Installation

Simple install the package to your $GOPATH with the go tool from shell:

$ go get github.com/gchaincl/dotsql

Make sure Git is installed on your machine and in your system's $PATH

Usage GoDoc

First of all, you need to define queries into a file:

-- name: create-users-table
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name VARCHAR(255),
email VARCHAR(255)
);
-- name: create-user
INSERT INTO users (name, email) VALUES(?, ?)
-- name: find-one-user-by-email
SELECT id,name,email FROM users WHERE email = ? LIMIT 1
--name: drop-users-table
DROP TABLE users

Notice that every query has a name tag (--name:<some name>), this will be helpful for referring to a specific query

Then you should be able to run something like:

// Get a database handle
db, err := sql.Open("sqlite3", ":memory:")

// Loads queries from file
dot, err := dotsql.LoadFromFile("queries.sql")

// Run queries
res, err := dot.Exec(db, "create-users-table")
res, err := dot.Exec(db, "create-user", "User Name", "main@example.com")
rows, err := dot.Query(db, "find-one-user-by-email", "main@example.com")

stmt, err := dot.Prepare(db, "drop-users-table")
result, err := stmt.Exec()

For a complete example please refer to integration_test.go and test_schema.sql

Development

Dotsql is in a very early stage so api may change. Contributions are welcome! Integration tests are tagged with +integration, so if you want to run them you should:

go test -tags=integration

If integration tests takes too long remember to go install code.google.com/p/go-sqlite/go1/sqlite3

Otherwise just run:

go test

Documentation

Overview

Package dotsql provides a way to separate your code from SQL queries.

It is not an ORM, it is not a query builder. Dotsql is a library that helps you keep sql files in one place and use it with ease.

For more usage examples see https://github.com/gchaincl/dotsql

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DotSql

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

DotSql represents a dotSQL queries holder.

func Load

func Load(r io.Reader) (*DotSql, error)

Load imports sql queries from any io.Reader.

func LoadFromFile

func LoadFromFile(sqlFile string) (*DotSql, error)

LoadFromFile imports SQL queries from the file.

Example
package main

import (
	"database/sql"
	"github.com/gchaincl/dotsql"
)

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

	dot, err := dotsql.LoadFromFile("queries/users.sql")
	if err != nil {
		panic(err)
	}

	/* users.sql looks like:
	--name: create-user
	INSERT INTO users (email) VALUES(?)

	--name: find-user-by-email
	SELECT email FROM users WHERE email = ?
	*/

	if _, err := dot.Exec(db, "create-user", "user@example.com"); err != nil {
		panic(err)
	}

	rows, err := dot.Query(db, "find-user-by-email", "user@example.com")
	if err != nil {
		panic(err)
	}
	defer rows.Close()
}
Output:

func LoadFromString

func LoadFromString(sql string) (*DotSql, error)

LoadFromString imports SQL queries from the string.

func Merge

func Merge(dots ...*DotSql) *DotSql

Merge takes one or more *DotSql and merge its queries It's in-order, so the last source will override queries with the same name in the previous arguments if any.

func (DotSql) Exec

func (d DotSql) Exec(db Execer, name string, args ...interface{}) (sql.Result, error)

Exec is a wrapper for database/sql's Exec(), using dotsql named query.

func (DotSql) Prepare

func (d DotSql) Prepare(db Preparer, name string) (*sql.Stmt, error)

Query is a wrapper for database/sql's Prepare(), using dotsql named query.

func (DotSql) Query

func (d DotSql) Query(db Queryer, name string, args ...interface{}) (*sql.Rows, error)

Query is a wrapper for database/sql's Query(), using dotsql named query.

func (DotSql) QueryMap

func (d DotSql) QueryMap() map[string]string

QueryMap returns a map[string]string of loaded queries

func (DotSql) Raw

func (d DotSql) Raw(name string) (string, error)

Raw returns the query, everything after the --name tag

type Execer

type Execer interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
}

Execer is an interface used by Exec.

type Preparer

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

Preparer is an interface used by Prepare.

type Queryer

type Queryer interface {
	Query(query string, args ...interface{}) (*sql.Rows, error)
}

Queryer is an interface used by Query.

type Scanner

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

func (*Scanner) Run

func (s *Scanner) Run(io *bufio.Scanner) map[string]string

Jump to

Keyboard shortcuts

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