querify

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2022 License: MIT Imports: 6 Imported by: 0

README

go.dev reference Go Report Card golangci-lint GitHub tag (latest SemVer)

querify

Query your data from and to any json compatible source. The query language used is similar to SQL with Postgres dialect.

hobbiesTable := []map[string]interface{}{
    {"id": 1, "name": "Football"},
    {"id": 2, "name": "Basketball"},
    {"id": 3, "name": "Hockey"},
}

usersTable := []map[string]interface{}{
    {"id": 1, "name": "Max"},
    {"id": 2, "name": "Tom"},
    {"id": 3, "name": "Alex"},
}

userHobbiesTable := []map[string]interface{}{
    {"user_id": 1, "hobby_id": 1},
    {"user_id": 1, "hobby_id": 2},
    {"user_id": 2, "hobby_id": 3},
    {"user_id": 3, "hobby_id": 1},
}

type User struct {
    Name    string
    Hobbies []string
}

var users []User

err := querify.From(usersTable).As("users").
    Join(
        querify.LeftJoin{
            Right: querify.From(userHobbiesTable).As("user_hobbies"),
            On:    querify.Equals{querify.Ident("users.id"), querify.Ident("user_hobbies.user_id")},
        },
        querify.LeftJoin{
            Right: querify.From(hobbiesTable).As("hobbies"),
            On:    querify.Equals{querify.Ident("hobbies.id"), querify.Ident("user_hobbies.hobby_id")},
        },
    ).
    GroupBy(querify.Ident("users.name")).
    Select(
        querify.As{
            Name:       "name",
            Expression: querify.Ident("users.name"),
        },
        querify.As{
            Name: "hobbies",
            Expression: querify.ArrayAgg{
                Expression: querify.Ident("hobbies.name"),
            },
        },
    ).Scan(&users)
if err != nil {
    panic(err)
}

fmt.Println(users)
// [{Max [Football Basketball]} {Tom [Hockey]} {Alex [Football]}]

Features

  • Expression:
    • Literal
    • Ident
    • ArrayAgg
    • Concat
    • CountAll
    • Count
    • As
  • GroupBy:
    • Ident
    • Cube
    • GroupingSets
  • Condition:
    • And
    • Or
    • Equals
    • Greater
    • Less
  • OrderBy:
    • Asc
    • Desc
  • Join:
    • LeftJoin
  • Limit
  • Offset

Your required SQL feature isn't yet supported? Implement these interfaces and create a merge request!

Dependencies

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type And

type And []Condition

func (And) Condition

func (a And) Condition(record GroupedRecord) (bool, error)

type ArrayAgg

type ArrayAgg struct {
	Distinct   bool
	Expression Select
}

func (ArrayAgg) Select

func (a ArrayAgg) Select(table SelectedTable) (string, []Value, error)

func (ArrayAgg) Variable

func (a ArrayAgg) Variable(record SelectedRecord) (Value, error)

type As

type As struct {
	Name       string
	Expression Select
}

func (As) Select

func (a As) Select(table SelectedTable) (string, []Value, error)

type Asc

type Asc struct {
	Expression Variable
	NullsLast  bool
}

func (Asc) OrderBy

func (a Asc) OrderBy(i, j SelectedRecord) (int, error)

type Concat

type Concat []Variable

func (Concat) Select

func (c Concat) Select(table SelectedTable) (string, []Value, error)

func (Concat) Variable

func (c Concat) Variable(record SelectedRecord) (Value, error)

type Condition

type Condition interface {
	Condition(record GroupedRecord) (bool, error)
}

type Count

type Count string

func (Count) Select

func (c Count) Select(table SelectedTable) (string, []Value, error)

func (Count) Variable

func (c Count) Variable(record SelectedRecord) (Value, error)

type CountAll

type CountAll struct{}

func (CountAll) Select

func (CountAll) Select(table SelectedTable) (string, []Value, error)

func (CountAll) Variable

func (CountAll) Variable(record SelectedRecord) (Value, error)

type Cube

type Cube []string

func (Cube) GroupBy

func (c Cube) GroupBy() (GroupingSets, error)

type Desc

type Desc struct {
	Expression Variable
	NullsLast  bool
}

func (Desc) OrderBy

func (d Desc) OrderBy(i, j SelectedRecord) (int, error)

type Equals

type Equals [2]Variable

func (Equals) Condition

func (e Equals) Condition(record GroupedRecord) (bool, error)

type Greater

type Greater [2]Variable

func (Greater) Condition

func (g Greater) Condition(record GroupedRecord) (bool, error)

type GroupBy

type GroupBy interface {
	GroupBy() (GroupingSets, error)
}

type GroupedRecord

type GroupedRecord struct {
	Err     error
	Source  Record
	Grouped Table
}

type GroupedTable

type GroupedTable struct {
	Err     error
	Source  Table
	Grouped []Table
}

func (GroupedTable) Copy

func (t GroupedTable) Copy() GroupedTable

func (GroupedTable) Having

func (t GroupedTable) Having(condition Condition) GroupedTable

func (GroupedTable) Select

func (t GroupedTable) Select(selects ...Select) SelectedTable

func (GroupedTable) UnionAll

func (t GroupedTable) UnionAll(table GroupedTable) GroupedTable

type GroupingSets

type GroupingSets [][]string

func (GroupingSets) Cart

func (GroupingSets) GroupBy

func (gs GroupingSets) GroupBy() (GroupingSets, error)

type Ident

type Ident string

func (Ident) GroupBy

func (i Ident) GroupBy() (GroupingSets, error)

func (Ident) Select

func (i Ident) Select(table SelectedTable) (string, []Value, error)

func (Ident) Variable

func (i Ident) Variable(record SelectedRecord) (Value, error)

type Join

type Join interface {
	Join(left Query) Table
}

type LeftJoin

type LeftJoin struct {
	Right Query
	On    Condition
}

func (LeftJoin) Join

func (lj LeftJoin) Join(left Query) Table

type Less

type Less [2]Variable

func (Less) Condition

func (l Less) Condition(record GroupedRecord) (bool, error)

type Literal

type Literal struct {
	Value Value
}

func (Literal) Select

func (l Literal) Select(table SelectedTable) (string, []Value, error)

func (Literal) Variable

func (l Literal) Variable(record SelectedRecord) (Value, error)

type Or

type Or [2]Condition

func (Or) Condition

func (o Or) Condition(record GroupedRecord) (bool, error)

type OrderBy

type OrderBy interface {
	OrderBy(i, j SelectedRecord) (int, error)
}

type Query

type Query interface {
	Query() Table
}

type Record

type Record struct {
	Err     error
	Columns []string
	Values  []Value
}

func (Record) MarshalJSON

func (r Record) MarshalJSON() ([]byte, error)

func (Record) Scan

func (r Record) Scan(dest interface{}) error

func (Record) ScanColumn

func (r Record) ScanColumn(column string, dest interface{}) error

func (Record) Set added in v0.1.1

func (r Record) Set(column string, value Value) Record

func (*Record) UnmarshalJSON

func (r *Record) UnmarshalJSON(b []byte) error

type Select

type Select interface {
	Select(table SelectedTable) (string, []Value, error)
}

type SelectedRecord

type SelectedRecord struct {
	Err      error
	Source   Record
	Grouped  Table
	Selected Record
}

type SelectedTable

type SelectedTable struct {
	Err      error
	Source   Table
	Grouped  []Table
	Selected Table
}

func (SelectedTable) Distinct

func (t SelectedTable) Distinct() SelectedTable

func (SelectedTable) First

func (t SelectedTable) First() Record

func (SelectedTable) Limit

func (t SelectedTable) Limit(limit uint64) SelectedTable

func (SelectedTable) Offset

func (t SelectedTable) Offset(offset uint64) SelectedTable

func (SelectedTable) OrderBy

func (t SelectedTable) OrderBy(orders ...OrderBy) SelectedTable

func (SelectedTable) Query

func (t SelectedTable) Query() Table

func (SelectedTable) Record

func (t SelectedTable) Record(index int) SelectedRecord

func (SelectedTable) Scan

func (t SelectedTable) Scan(dest interface{}) error

func (SelectedTable) ScanColumn

func (t SelectedTable) ScanColumn(column string, dest interface{}) error

type Table

type Table struct {
	Err     error
	Columns []string
	Data    [][]Value
}

func From

func From(table interface{}) Table

func (Table) As

func (t Table) As(name string) Table

func (Table) Copy

func (t Table) Copy() Table

func (Table) Delete added in v0.1.1

func (t Table) Delete(where Condition) Table

func (Table) GroupBy

func (t Table) GroupBy(groups ...GroupBy) GroupedTable

func (Table) Insert added in v0.1.1

func (t Table) Insert(record Record) Table

func (Table) Join

func (t Table) Join(joins ...Join) Table

func (Table) MarshalJSON

func (t Table) MarshalJSON() ([]byte, error)

func (Table) Query

func (t Table) Query() Table

func (Table) Record added in v0.1.1

func (t Table) Record(index int) Record

func (Table) Records

func (t Table) Records() []Record

func (Table) Scan

func (t Table) Scan(dest interface{}) error

func (Table) ScanColumn

func (t Table) ScanColumn(column string, dest interface{}) error

func (Table) Select

func (t Table) Select(selects ...Select) SelectedTable

func (Table) UnionAll

func (t Table) UnionAll(table Table) Table

func (*Table) UnmarshalJSON

func (t *Table) UnmarshalJSON(b []byte) error

func (Table) Update added in v0.1.1

func (t Table) Update(record Record, where Condition) Table

func (Table) Where

func (t Table) Where(condition Condition) Table

type Value

type Value interface{}

func Copy

func Copy(value Value) (Value, error)

type Variable

type Variable interface {
	Variable(record SelectedRecord) (Value, error)
}

Jump to

Keyboard shortcuts

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