Giter Club home page Giter Club logo

sqlt's Introduction

sqlt

Build Status Coverage Status Go Report Card Godoc license

Description

Simple SQL template for 2 way SQL.

Sample (PostgreSQL)

Template

Template is compatible with SQL.
Use /*% and %*/ as delimiter instead of {{ and }} for processing template.

SELECT *
FROM users
WHERE id IN /*% in "ids" %*/(1, 2)
AND name = /*% p "name" %*/'John Doe'
/*%- if get "onlyMale" %*/
AND sex = 'MALE'
/*%- end %*/
ORDER BY /*% out "order" %*/id

Go code

  • func param or p replace to placeholder by name.
  • func in deploy slice values to parentheses and placeholders.
  • func time returns current time and cache it, this func always same time in same template.
  • func now returns current time each calling.
  • func escape, prefix, inffix, suffix replace to placeholder with escape for LIKE keyword.
  • If you want to use value for building SQL only or embedding value to SQL directly, you must use get or out func. This func check that value contains prohibited character(s) for avoiding SQL injection.out is annotative, but get is not annotative.
    Prohibited characters are:
    • Single quotation
    • Semi colon
    • Line comment (--)
    • Block comment (/* or */)
  • If database driver that you use supports sql.NamedArg, you should call ExecNamed func.
// query is generated SQL from template.
// args are arguments for generated SQL.
query, args, err := sqlt.New(sqlt.Postgres).Exec(s, map[string]interface{}{
	"ids":      []int{1, 2, 3},
	"order":    "name DESC",
	"onlyMale": false,
	"name":     "Alex",
})
rows, err := db.Query(query, args...)

options

  • TimeFunc: For using customized time in template.
  • Annotation: Output meta data for debugging to rendered SQL.

Generated SQL

call Exec

SELECT *
FROM users
WHERE id IN ($1, $2, $3)
AND name = $4
ORDER BY name DESC

call ExecNamed

Currently there are also many drivers who do not support sql.NamedArg.
In future, driver support sql.NamedArg, you only need to change Exec to ExecNamed.

SELECT *
FROM users
WHERE id IN (:ids__1, :ids__2, :ids__3)
AND name = :name
ORDER BY name DESC

Install

$ go get github.com/pinzolo/sqlt

Suppor

Go version

Go 1.9 or later

Databases

  • PostgreSQL
  • MySQL
  • Oracle
  • SQL Server

Contribution

  1. Fork (https://github.com/pinzolo/sqlt/fork)
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the go test ./... command and confirm that it passes
  6. Run gofmt -s
  7. Create a new Pull Request

Author

pinzolo

sqlt's People

Contributors

pinzolo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

fxk2006

sqlt's Issues

Add prefix, suffix, infix template funcs

-- template
SELECT *
FROM items
WHERE note1 LIKE /*% infix "note1" %*/''
OR note2 LIKE /*% prefix "note2" %*/''
OR note2 LIKE /*% suffix "note3" %*/''
-- translated
SELECT *
FROM items
WHERE note1 LIKE '%' || $1 || '%' ESCAPE '\'
OR note2 LIKE '%' || $2 ESCAPE '\'
OR note2 LIKE $3 || '%' ESCAPE '\'

points

  • Oracle does not have default escapce char, so always use ESCAPE.
  • Oracle uses '%' (full width percent) and '_' (full width underscore) as wildcard.

`range` in template

Problem

/*% range $i, $v := get "foo.items" %*/
AND name = /*% p "???" %*/'' -- How do I write?
/*% end %*/

Answer

/*% range $i, $v := get "foo.items" %*/
AND name = /*% p (name "foo.items" $i) %*/''
/*% end %*/

Struct value

Currently sqlt does not accept struct param value.
I want to apply struct param value to p and 'in` func.

type Foo struct {
    Value string
}

s := `SELECT *
FROM users
WHERE name = /*% p "foo.Value" %*/''
`

foo := Foo{
    Value: "bar"
}
sql, args, err := sqlt.New(sqlt.Postgres).Exec(s, map[string]interface{}{
    "foo": foo,
}

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.