ql

package module
v0.0.0-...-f85a315 Latest Latest
Warning

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

Go to latest
Published: May 17, 2017 License: Apache-2.0 Imports: 3 Imported by: 0

README

QL GoDoc Build Status Go Report Card

An experimental Go library for adapting generic JSON database queries to different backend databases.

Documentation

Overview

Package ql contains functions for adapting generic JSON database queries to different backend databases

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CQL

func CQL(q *Query) (string, []interface{})

CQL returns a CQL query and arguments.

Example
package main

import (
	"fmt"

	"github.com/jmank88/ql"
)

func main() {
	q := &ql.Query{
		Fields: []string{"a", "b", "c", "d"},
		Target: "target",
		Filter: ql.And(
			ql.Compare("a", "=", "test"),
			ql.Or(
				ql.Compare("b", "<", 2),
				ql.Compare("c", "!=", 5),
			),
			ql.Compare("d", ">", 0),
		),
		Sort: []ql.Sort{
			{
				Field:      "a",
				Descending: true,
			},
		},
		Max: newInt(20),
	}
	query, args := ql.MYSQL(q)
	// session.Query(query, args...)
	fmt.Println(query)
	fmt.Println(args)

}

func newInt(i int) *int {
	return &i
}
Output:

SELECT a, b, c, d FROM target WHERE (a = ? AND (b < ? OR c != ?) AND d > ?) ORDER BY a DESC LIMIT 20
[test 2 5 0]

func MYSQL

func MYSQL(q *Query) (string, []interface{})

MYSQL returns a MYSQL compatible SQL query and arguments.

Example
package main

import (
	"fmt"

	"github.com/jmank88/ql"
)

func main() {
	q := &ql.Query{
		Fields: []string{"a", "b", "c", "d"},
		Target: "target",
		Filter: ql.And(
			ql.Compare("a", "=", "test"),
			ql.Or(
				ql.Compare("b", "<", 2),
				ql.Compare("c", "!=", 5),
			),
			ql.Compare("d", ">", 0),
		),
		Sort: []ql.Sort{
			{
				Field:      "a",
				Descending: true,
			},
		},
		Max: newInt(100),
	}
	query, args := ql.MYSQL(q)
	// db.Query(query, args...)
	fmt.Println(query)
	fmt.Println(args)

}

func newInt(i int) *int {
	return &i
}
Output:

SELECT a, b, c, d FROM target WHERE (a = ? AND (b < ? OR c != ?) AND d > ?) ORDER BY a DESC LIMIT 100
[test 2 5 0]

func POSTGRESQL

func POSTGRESQL(q *Query) (string, []interface{})

POSTGRESQL returns a POSTGRESQL compatible SQL query and arguments.

Example
package main

import (
	"fmt"

	"github.com/jmank88/ql"
)

func main() {
	q := &ql.Query{
		Fields: []string{"a", "b", "c", "d"},
		Target: "target",
		Filter: ql.And(
			ql.Compare("a", "=", "test"),
			ql.Or(
				ql.Compare("b", "<", 2),
				ql.Compare("c", "!=", 5),
			),
			ql.Compare("d", ">", 0),
		),
		Sort: []ql.Sort{
			{
				Field:      "a",
				Descending: true,
			},
		},
		Max: newInt(100),
	}
	query, args := ql.POSTGRESQL(q)
	// db.Query(query, args...)
	fmt.Println(query)
	fmt.Println(args)

}

func newInt(i int) *int {
	return &i
}
Output:

SELECT a, b, c, d FROM target WHERE (a = $1 AND (b < $2 OR c != $3) AND d > $4) ORDER BY a DESC LIMIT 100
[test 2 5 0]

Types

type Comparison

type Comparison struct {
	// One of: =, !=, <, >, <=, >=.
	Comparator string      `json:"comparator,omitempty"`
	Field      string      `json:"field,omitempty"`
	Value      interface{} `json:"value,omitempty"`
}

A Comparison filter compares a field to a value.

type Composite

type Composite struct {
	// One of (case-insensitive): AND, OR.
	Operator string    `json:"operator,omitempty"`
	Filters  []*Filter `json:"filters,omitempty"`
}

An Composite filter logically combines two or more Filters.

type Filter

type Filter struct {
	Comparison
	Composite
}

Filter is a union of filter types. Only one embedded struct should have set fields.

func And

func And(filters ...*Filter) *Filter

And creates an 'AND' Composite Filter.

func Compare

func Compare(field, comparator string, value interface{}) *Filter

Compare creates a Comparison Filter.

func Or

func Or(filters ...*Filter) *Filter

Or creates an 'OR' Composite Filter.

type Query

type Query struct {
	// Target of the query.
	Target string `json:"target"`
	// Optional names of fields to fetch.
	Fields []string `json:"fields,omitempty"`
	// Optional filter.
	Filter *Filter `json:"filter,omitempty"`
	// Optional sort order.
	Sort []Sort `json:"sort,omitempty"`
	// Optional maximum records to fetch.
	Max *int `json:"max,omitempty"`
}

Query is a generic, JSON-serializable query.

Example
package main

import (
	"encoding/json"
	"os"

	"github.com/jmank88/ql"
)

func main() {
	e := json.NewEncoder(os.Stdout)
	e.SetIndent("", "\t")
	_ = e.Encode(&ql.Query{
		Fields: []string{"a", "b", "c", "d"},
		Target: "target",
		Filter: ql.And(
			ql.Compare("a", "=", "test"),
			ql.Or(
				ql.Compare("b", "<", json.Number("2")),
				ql.Compare("c", "!=", json.Number("5")),
			),
			ql.Compare("d", ">", json.Number("0")),
		),
		Sort: []ql.Sort{
			{
				Field:      "a",
				Descending: true,
			},
		},
	})

}
Output:

{
	"target": "target",
	"fields": [
		"a",
		"b",
		"c",
		"d"
	],
	"filter": {
		"operator": "AND",
		"filters": [
			{
				"comparator": "=",
				"field": "a",
				"value": "test"
			},
			{
				"operator": "OR",
				"filters": [
					{
						"comparator": "\u003c",
						"field": "b",
						"value": 2
					},
					{
						"comparator": "!=",
						"field": "c",
						"value": 5
					}
				]
			},
			{
				"comparator": "\u003e",
				"field": "d",
				"value": 0
			}
		]
	},
	"sort": [
		{
			"field": "a",
			"descending": true
		}
	]
}

type Sort

type Sort struct {
	Field      string `json:"field"`
	Descending bool   `json:"descending"`
}

Sort defines a sort ordering for a Field.

func Asc

func Asc(field string) Sort

Asc creates an ascending Sort.

func Desc

func Desc(field string) Sort

Desc creates a descending Sort.

Jump to

Keyboard shortcuts

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