squeeze

package module
v0.0.0-...-709588c Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2019 License: MIT Imports: 4 Imported by: 0

README

squeeze

SQL template manager

Writing SQL is nice, embedding SQL in application code is not. Squeeze provides an easy way to keep your SQL query templates separate.

go get go.soon.build/squeeze
  1. Write templates
-- ./templates/user/user.sql
{{define "byID"}}
SELECT *
FROM "{{.Table}}"
WHERE id = $1
{{end}}
  1. Load templates into squeeze store
import (
    "go.soon.build/squeeze"
    "go.soon.build/squeeze/source"
)

sqt, err := squeeze.New(source.NewDirectory("./templates"))
if err != nil {
    // handle err
}
  1. Build a query from a template
qry := sqt.Parse("user.byID", struct{
    Table string
}, {
    Table: "page",
})

Sourcing Templates

The directory source, as used above, reads templates from a directory tree. To source templates from alternative locations (eg. bundled static files) implement the squeeze.Sourcer interface to add templates to the store or use one of the existing source implementations:

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NamespaceSeperator = "."

NamespaceSeperator seperates templates in different directories

Functions

This section is empty.

Types

type ErrTemplateDefined

type ErrTemplateDefined struct {
	Name string
}

A ErrTemplateDefined is returned when a template of the same name already exists in the Store

func (ErrTemplateDefined) Error

func (e ErrTemplateDefined) Error() string

Error implements the error interface

type ErrTemplateNotFound

type ErrTemplateNotFound struct {
	Name string
}

A ErrTemplateNotFound is returned when a template is not found be Parse

func (ErrTemplateNotFound) Error

func (e ErrTemplateNotFound) Error() string

Error implements the error interface

type SourceFunc

type SourceFunc func(*Store) error

The SourceFunc is an adapter allowing regular functions to act as Sourcers

func (SourceFunc) Source

func (fn SourceFunc) Source(store *Store) error

Source implements the Sourcer interface calling the wrapped function

type Sourcer

type Sourcer interface {
	Source(s *Store) error
}

A Sourcer can Source templates and add them to the Store

func String

func String(name, sql string) Sourcer

String adds a template to the store from a raw SQL string

type Store

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

A Store stores parsed SQL templates that can be executed by their name

func New

func New(sources ...Sourcer) (*Store, error)

New constructs a New store

func (*Store) Add

func (s *Store) Add(name string, tpl *template.Template) error

Add adds a template to the store, if a template already exists an ErrTemplateDefined will be returned

func (*Store) From

func (s *Store) From(sources ...Sourcer) error

From loads templates in from any number of sources that implement the Sourcer interface

func (*Store) Parse

func (s *Store) Parse(path string, v interface{}) (string, error)

Parse parses a template foo.bar.list

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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