The database toolkit for go
Go
Latest commit 65275bc Dec 23, 2016 @cdevienne cdevienne committed on GitHub Merge pull request #140 from cdevienne/wrap-errors
Wrap errors
Permalink
Failed to load latest commit information.
dialects [mysql] Document the errors code generation Dec 23, 2016
.gitignore add qb.iml to gitignore Aug 8, 2016
.travis.yml Move dialects to dedicated packages. Dec 20, 2016
LICENSE
README.md Add dialect import in the quickstart example Dec 20, 2016
aggregate.go Document the exported functions Oct 4, 2016
aggregate_test.go
clause.go Remove the old SQLClause Sep 5, 2016
clauses.go Implement Exists Oct 4, 2016
clauses_test.go Implement Exists Oct 4, 2016
column.go Fix single col pkey creation Oct 4, 2016
column_test.go Make column_test dialect agnostic Dec 20, 2016
combiner.go Combiner, Condition and Where implements Clause Sep 5, 2016
combiner_test.go Make combiner tests dialect-agnostic Dec 20, 2016
compiler.go Use a constructor to initialize SQLCompiler Dec 20, 2016
compiler_test.go Add support of "FOR UPDATE" clause Nov 7, 2016
conditional.go
conditional_test.go
constraint.go
constraint_test.go Make constraint tests dialect-agnostic Dec 20, 2016
delete.go Make dialects stateless Oct 4, 2016
delete_test.go Make delete tests dialect-agnostic Dec 20, 2016
dialect.go NewDialect panics if an unknown dialect is passed Dec 23, 2016
dialect_default.go NewDialect panics if an unknown dialect is passed Dec 23, 2016
dialect_test.go NewDialect panics if an unknown dialect is passed Dec 23, 2016
engine.go Translate sql errors with dialect.WrapError Dec 21, 2016
engine_test.go
errors.go Translate postgres error codes Dec 21, 2016
errors_test.go Add a Error type to wrap driver errors Dec 21, 2016
glide.lock up glide Jul 8, 2016
glide.yaml fix #37 Mar 30, 2016
index.go add adapter -> dialect refactor Jun 23, 2016
insert.go Make dialects stateless Oct 4, 2016
insert_test.go Make insert tests dialect-agnostic Dec 20, 2016
logger.go use specific type for logflags Nov 18, 2016
logger_test.go Use sqlite instead of postgres for engine, metadata & logger tests Dec 20, 2016
metadata.go Fix merge conflict between recent changes and #78 Sep 3, 2016
metadata_test.go Use sqlite instead of postgres for engine, metadata & logger tests Dec 20, 2016
qb_logo_128.png add logo. thanks onur Jun 5, 2016
select.go Add support of "FOR UPDATE" clause Nov 7, 2016
select_test.go Make 'select' tests dialect-agnostic Dec 20, 2016
statement.go Rename Clause to SQLClause Sep 5, 2016
statement_test.go Rename Clause to SQLClause Sep 5, 2016
table.go Merge pull request #116 from cdevienne/fix_single_column_primary_key Oct 6, 2016
table_test.go Better coverage in table, type & upsert tests Dec 20, 2016
testutils_test.go
type.go Add BLOB type Sep 12, 2016
type_test.go Better coverage in table, type & upsert tests Dec 20, 2016
update.go Make dialects stateless Oct 4, 2016
update_test.go Make 'update' tests dialect-agnostic Dec 20, 2016
upsert.go Exports Upsert attributes Dec 20, 2016
upsert_test.go
where.go Add WhereClause.And and WhereClause.Or Oct 20, 2016
where_test.go Add WhereClause.And and WhereClause.Or Oct 20, 2016

README.md

alt text

qb - the database toolkit for go

Join the chat at https://gitter.im/aacanakin/qb Build Status Coverage Status License (LGPL version 2.1) Go Report Card GoDoc

This project is currently pre 1.

Currently, it's not feature complete. It can have potential bugs. There are no tests covering concurrency race conditions. It can crash especially in concurrency. Before 1.x releases, each major release could break backwards compatibility.

About qb

qb is a database toolkit for easier db queries in go. It is inspired from python's best orm, namely sqlalchemy. qb is an orm(sqlx) as well as a query builder. It is quite modular in case of using just expression api and query building stuff.

Documentation

The documentation is hosted in readme.io which has great support for markdown docs. Currently, the docs are about 80% - 90% complete. The doc files will be added to this repo soon. Moreover, you can check the godoc from here. Contributions & Feedbacks in docs are welcome.

Features

  • Support for postgres(9.5.+), mysql & sqlite3
  • Powerful expression API for building queries & table ddls
  • Struct to table ddl mapper where initial table migrations can happen
  • Transactional session api that auto map structs to queries
  • Foreign key definitions
  • Single & Composite column indices
  • Relationships (soon.. probably in 0.3 milestone)

Installation

Installation with glide;

glide get github.com/slicebit/qb

0.2 installation with glide;

glide get github.com/slicebit/qb#0.2

Installation using go get;

go get -u github.com/slicebit/qb

If you want to install test dependencies then;

go get -u -t github.com/slicebit/qb

Quick Start

package main

import (
    "fmt"
    "github.com/slicebit/qb"
    _ "github.com/mattn/go-sqlite3"
    _ "github.com/slicebit/qb/dialects/sqlite"
)

type User struct {
    ID       string `db:"id"`
    Email    string `db:"email"`
    FullName string `db:"full_name"`
    Oscars   int    `db:"oscars"`
}

func main() {

    users := qb.Table(
        "users",
        qb.Column("id", qb.Varchar().Size(40)),
        qb.Column("email", qb.Varchar()).NotNull().Unique(),
        qb.Column("full_name", qb.Varchar()).NotNull(),
        qb.Column("oscars", qb.Int()).NotNull().Default(0),
        qb.PrimaryKey("id"),
    )

    db, err := qb.New("sqlite3", "./qb_test.db")
    if err != nil {
        panic(err)
    }

    defer db.Close()

    metadata := qb.MetaData()

    // add table to metadata
    metadata.AddTable(users)

    // create all tables registered to metadata
    metadata.CreateAll(db)
    defer metadata.DropAll(db) // drops all tables

    ins := qb.Insert(users).Values(map[string]interface{}{
        "id":        "b6f8bfe3-a830-441a-a097-1777e6bfae95",
        "email":     "jack@nicholson.com",
        "full_name": "Jack Nicholson",
    })

    _, err = db.Exec(ins)
    if err != nil {
        panic(err)
    }

    // find user
    var user User

    sel := qb.Select(users.C("id"), users.C("email"), users.C("full_name")).
        From(users).
        Where(users.C("id").Eq("b6f8bfe3-a830-441a-a097-1777e6bfae95"))

    err = db.Get(sel, &user)
    fmt.Printf("%+v\n", user)
}

Credits