Giter Club home page Giter Club logo

jet's Introduction

Jet is a super-flexible lightweight SQL interface (GoDoc)

Features:

  • En/decode custom types using ComplexValue
  • Prepared statement LRU cache
  • Unpack query results to any format
  • Can expand queries for map and slice arguments (e.g. $1 to $1, $2, $3, useful for hstore or set membership queries)
  • Serializes hstore columns to maps
  • Simple migration API
  • Customizable column name mapper

Open

db, err := jet.Open("postgres", "...")

Insert Rows

db.Query(`INSERT INTO "fruits" ( "name", "price" ) VALUES ( $1, $2 )`, "banana", 2.99).Run()

Run is Jet's Exec equivalent and is used instead of Rows() when no return values are expected

Query Rows

var rows []*struct{
  Name  string
  Price int
}
db.Query(`SELECT * FROM "fruits"`).Rows(&rows)

Jet's column mapper is very powerful. It tries to map the columns to any value you provide. You're not required to use a fixed output format. In this case rows could be anything e.g struct, *struct, []struct, []*struct, Type, *Type, []Type, []*Type even map[string]interface{} or just simple values like int or *int. You get the idea.

Hstore

Jet can also deserialize hstore columns for you. In this case the header column is a hstore value.

var out struct{
  Header  map[string]interface{}
  Body    string
}
db.Query(`SELECT * FROM "emails"`).Rows(&out)

Map and Slice Expansion

If you want to do e.g. hstore inserts or set membership queries, Jet can automatically expand the query and adjust the argument list for you.

  • Maps expand to k1, v1, k2, v2, ...
  • Slices expand to v1, v2, ...

Passing in a map argument

db.Query(`INSERT INTO "emails" ( "header", "body" ) VALUES ( hstore(ARRAY[ $1 ]), $2 )`, aMap, aBody).Run()

will expanded the query to

INSERT INTO "emails" ( "header", "body" ) VALUES ( hstore(ARRAY[ $1, $2, $3, $4 ... ]), $5 )

Passing in a slice argument

db.Query(`SELECT * FROM "files" WHERE "files"."name" IN ( $1 )`, aSlice)

will expand the query to

SELECT * FROM "files" WHERE "files"."name" IN ( $1, $2, $3, ... )

jet's People

Contributors

hebo avatar tcolar avatar pzduniak avatar

Watchers

James Cloos avatar

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.