All Projects → andrewpillar → query

andrewpillar / query

Licence: MIT license
Simple Query Builder for PostgreSQL - WIP

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to query

SqExpress
SqExpress is a sql query builder which allows creating SQL expressions directly in C# code with strong typing and intellisense.
Stars: ✭ 80 (+21.21%)
Mutual labels:  sql-builder
sql-builder
A simple SQL builder for generate SQL for non-ActiveRecord supports databases
Stars: ✭ 34 (-48.48%)
Mutual labels:  sql-builder
Jooq
jOOQ is the best way to write SQL in Java
Stars: ✭ 4,695 (+7013.64%)
Mutual labels:  sql-builder
Grdb.swift
A toolkit for SQLite databases, with a focus on application development
Stars: ✭ 4,637 (+6925.76%)
Mutual labels:  sql-builder
AbacusUtil
Release the power in Java programming
Stars: ✭ 77 (+16.67%)
Mutual labels:  sql-builder
sqb
Extensible, multi-dialect SQL query builder and Database connection framework for NodeJS
Stars: ✭ 14 (-78.79%)
Mutual labels:  sql-builder
RapidORM
Quick solutions for Android ORM
Stars: ✭ 24 (-63.64%)
Mutual labels:  sql-builder

query

Note: This library is still very much a work in progress, expect breaking changes.

query is a simple PostgreSQL query builder for Go. It works by using first class functions to allow for queries to be built up. This does not support the entire dialect for PostgreSQL, only a subset that would be necessary for CRUD operations.

For full examples as to how queries of different types can be built up, see the query_test.go file.

Let's look at how a simple SELECT query can be built up using this library,

q := query.Select(
    query.Columns("*"),
    query.From("posts"),
    query.Where("user_id", "=", query.Arg(10)),
    query.OrderDesc("created_at"),
)

we can then use the above to pass a query to our database driver, along with that arguments passed to the query,

rows, err := db.Query(q.Build(), q.Args()...)

Calling Build on the query will build up the query string and correctly set the parameter arguments in the query. Args will return an interface slice containing the arguments given to the query via query.Arg.

We can do even more complex SELECT queries too,

q := query.Select(
    query.Columns("*"),
    query.From("posts"),
    query.Where("id", "IN",
        query.Select(
            query.Columns("id"),
            query.From("post_tags"),
            query.Where("name", "LIKE", query.Arg("%sql%")),
        ),
    ),
    query.OrderDesc("created_at"),
)

This library makes use of the type Option which is a first class function, which takes the current Query an Option is being applied to and returns it.

type Option func(Query) Query

With this we can define our own Option functions to clean up some of the queries we want to build.

func Search(col, pattern string) query.Option {
    return func(q query.Query) query.Query {
        if pattern == "" {
            return q
        }
        return query.Where(col, "LIKE", query.Arg("%" + pattern + "%"))(q)
    }
}

q := query.Select(
    query.Columns("*"),
    query.From("posts"),
    Search("title", "query builder"),
    query.OrderDesc("created_at"),
)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].