Categorygithub.com/ryym/goq
modulepackage
0.0.1
Repository: https://github.com/ryym/goq.git
Documentation: pkg.go.dev

# README

🌱 Beta version

Goq

circleci appveyor

SQL-based DB access library for Gophers

Features

SQL-based API

Goq provides the low-level API which just wraps SQL clauses as Go methods, Instead of abstracting a way of query construction as an opinionated API like typical frameworks. That is, you already know most of the Goq API if you know SQL.

Flexible Result Mapping

Using Goq, you can collect result rows fetched from DB into various format structures: a slice of your model, single map, map of slices, combination of them, etc.

Custom Query Builder Generation

Goq can generate your custom query builder by go generate based on your models mapped to DB tables. This helps you write a query with more type safety and readability.

What does it look like?

import (
    "fmt"

    _ "github.com/lib/pq"
    "github.com/ryym/goq"
)

func main() {
    // Connect to DB.
    db, err := goq.Open("postgres", conn)
    panicIf(err)
    defer db.Close()

    // Initialize your builder.
    q := NewBuilder(db.Dialect())

    // Write a query.
    query := q.Select(
        q.Countries.ID,
        q.Countries.Name,
        q.Cities.All(),
    ).From(
        q.Countries,
    ).Joins(
        q.InnerJoin(q.Cities).On(
            q.Cities.CountryID.Eq(q.Countries.ID),
        ),
    ).Where(
        q.Countries.Population.Lte(500000),
        q.Cities.Name.Like("New%"),
    ).OrderBy(
        q.Countries.Name,
        q.Cities.Name,
    )

    var countries []Country
    var citiesByCountry map[int][]City

    // Collect results.
    err = db.Query(query).Collect(
        q.Countries.ToUniqSlice(&countries),
        q.Cities.ToSliceMap(&citiesByCountry).By(q.Countries.ID),
    )
    panicIf(err)

    fmt.Println("Complete!", countries, citiesByCountry)
}

Out of Scope Features

Goq is not a DB management framework so does not support any of these:

  • schema migration
  • schema generation from Go code
  • model generation from schema

Resources

Inspiration

Goq is inspired by ScalikeJDBC which is a Scala library providing SQL-based API.

# Packages

No description provided by the author
Package dialect defines some dialects per RDB.
Package gen provides a method to generate custom query builder.
No description provided by the author
Package util is an internal utility.

# Functions

ExecCollectorsForTest executes given collectors for given rows and selects.
InitCollectors initializes collectors.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewInitConf creates a initConf for collectors.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Open opens a database.

# Constants

Join types.
Join types.
Join types.
Join types.
Orders.
Orders.

# Variables

No description provided by the author

# Structs

Builder provides query builder methods and result collector methods.
CaseEelseExpr represents a 'CASE' expression with 'ELSE' clause.
CaseExpr represents a 'CASE' expression without 'ELSE' clause.
CollectorMaker provides methods to create generic collectors.
Column has data about the column and its table.
No description provided by the author
ColumnList is a special expression that holds multiple columns.
ColumnMaker is a utility to make several columns of the same table easily.
ColumnMeta is a meta information about a column.
DB is a database handle which wraps *sql.DB.
Delete constructs a 'DELETE' statement.
No description provided by the author
ElemCollector scans a first row into an arbitrary struct.
Insert constructs an 'INSERT' statement.
InsertMaker constructs Insert struct.
No description provided by the author
JoinDef defines how to join a table.
No description provided by the author
MapCollector collects rows into a map of struct.
MapCollectorMaker creates a MapCollector with the given key column.
ModelCollectorMaker provides methods to create model collectors.
ElemCollector scans a first row into a model struct.
ModelMapCollector collects rows into a map of models whose key is a primary key.
ModelSliceCollector collects rows into a slice of models.
ModelSliceMapCollector collects rows into a map of slices.
ModelSliceMapCollectorMaker creates a ModelSliceMapCollector with the given key column.
ModelUniqSliceCollector collects rows into a slice of models uniquely.
ModelUniqSliceMapCollector collects rows into a map of slices.
ModelUniqSliceMapCollectorMaker creates a ModelUniqSliceMapCollector with the given key column.
Ordering is a combination of an expression and an order.
Query is a result query constructed by the query builder.
QueryBuilder is a core query builder.
RawExpr is a raw string expression.
RowMapCollector scans a first row into a map.
RowMapSliceCollector collects rows into a slice of maps.
Runner runs a query with given collectors to collect result rows.
Selection provides information about a selected expression.
SliceCollector collects rows into a slice of structs.
SliceMapCollector collects rows into a map of slices.
SliceMapCollectorMaker creates a SliceMapCollector with the given key column.
No description provided by the author
Tx is an in-progress database transaction which wraps *sql.Tx.
Update constructs an 'UPDATE' statement.
UpdateMaker constructs Update struct.
WhenClause constructs a 'WHEN' clause used for case expressions.
Where constructs a 'WHERE' clause.

# Interfaces

Aliased is an aliased expression.
AnonExpr represents an anonymous expression that does not have an alias.
Clauses has 'JOIN', 'WHERE', and 'GROUP BY' clauses.
Collector defines methods to collect query results.
DBContext abstructs query syntax differences among RDBs.
Expr is the interface of expressions that provide basic operators.
GroupByClause is a 'GROUP BY' clause.
No description provided by the author
ListCollector interface represents a collector which collects rows into a collection data.
Orderer is the interface of expressions for 'ORDER BY'.
PredExpr represents a predicate expression.
Queryable represents an interface to issue an query.
QueryApplier is the interface to append query parts.
QueryExpr is the interface of a query to select data.
QueryRoot is the interface of query statements such as 'SELECT', 'CREATE', 'UPDATE', and so on.
QueryTable represents a query as a table.
SchemaTable represents a database table.
Selectable represents a selectable expression.
SelectClause is a 'SELECT' clause.
SingleCollector interface represents a collector which scans a first row.
TableLike represents an expression that can be a table.

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author