Giter Club home page Giter Club logo

pgqb's Introduction

pgqb

Warning! This is a work in progress.

pgqb (Postgres Query Builder) is a small utility library to build PostgreSQL query strings in Go. This library is purely used for SQL string generation and paired with something like jackc/pgx.

Features:

  • Simple, natural syntax
  • Supported commands: SELECT, FROM, WHERE, JOIN (INNER, LEFT, and RIGHT), ORDER BY, GROUP BY, OFFSET, LIMIT
  • SubQueries builder
  • Nested conditionals builder

Please be aware that the queries are not sanitized by default. There's a helper function to do it, Sanitize.

Examples

Basic SELECT query

builder := pgqb.Builder{}
builder.Select("one", "two").From("some_table")
SELECT one,two FROM some_table

Multiple Joins

builder := pgqb.Builder{}
builder.
    Select("one", "two").
    From("some_table").
    InnerJoin("other_table", "other_table.id = some_table.other_id").
	InnerJoin("another_table", "another_table.id = other_table.another_id")
SELECT one, two FROM some_table INNER JOIN other_table ON other_table.id = some_table.other_id INNER JOIN another_table ON another_table.id = other_table.another_id

Joins with SubQuery

builder := pgqb.Builder{}
builder.
    Select("one", "two").
    From("some_table").
    InnerJoin("other_table", "other_table.id = some_table.other_id").
    InnerJoin(pgqb.Sub(func(builder *pgqb.Builder) {
        builder.
            Select("sqcol").
            From("sqtable"). 
            Where("sqcol2 = 'abc'").
            Limit(1)
    }, "sq"), "sq.sqcol = 'x'").
    Limit(20).
    OrderBy("id", "ASC")
SELECT one, two FROM some_table INNER JOIN other_table ON other_table.id = some_table.other_id INNER JOIN ( SELECT sqcol FROM sqtable WHERE sqcol2 = 'abc' LIMIT 1 ) AS sq ON sq.sqcol = 'x' ORDER BY id ASC LIMIT 20  

Nested conditionals

builder := pgqb.Builder{}
builder.
    Select("one", "two").
    From("some_table").
    Where(pgqb.Condition(func(builder *pgqb.ConditionBuilder) {
        builder.Or("a = 1")
        builder.Or("b > 10")
        builder.Or(pgqb.Condition(func(builder *pgqb.ConditionBuilder) {
            builder.And("c = 0")
            builder.And("d = 0")
        }))
    })).
    Where("z = 0")
SELECT one, two FROM some_table WHERE ( a = 1 OR b > 10 OR ( c = 0 AND d = 0 ) ) AND z = 0 

pgqb's People

Watchers

 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.