MCPcopy
hub / github.com/gocraft/dbr

github.com/gocraft/dbr @v2.7.7 sqlite

repository ↗ · DeepWiki ↗ · release v2.7.7 ↗
389 symbols 1,498 edges 52 files 167 documented · 43%
README

gocraft/dbr (database records)

GoDoc FOSSA Status Go Report Card CircleCI

gocraft/dbr provides additions to Go's database/sql for super fast performance and convenience.

$ go get -u github.com/gocraft/dbr/v2
import "github.com/gocraft/dbr/v2"

Driver support

  • MySQL
  • PostgreSQL
  • SQLite3
  • MsSQL

Examples

See godoc for more examples.

Open connections

// create a connection (e.g. "postgres", "mysql", or "sqlite3")
conn, _ := Open("postgres", "...", nil)
conn.SetMaxOpenConns(10)

// create a session for each business unit of execution (e.g. a web request or goworkers job)
sess := conn.NewSession(nil)

// create a tx from sessions
sess.Begin()

Create and use Tx

sess := mysqlSession
tx, err := sess.Begin()
if err != nil {
    return
}
defer tx.RollbackUnlessCommitted()

// do stuff...

tx.Commit()

SelectStmt loads data into structs

// columns are mapped by tag then by field
type Suggestion struct {
    ID  int64       // id, will be autoloaded by last insert id
    Title   NullString  `db:"subject"`  // subjects are called titles now
    Url string      `db:"-"`    // ignored
    secret  string      // ignored
}

// By default gocraft/dbr converts CamelCase property names to snake_case column_names.
// You can override this with struct tags, just like with JSON tags.
// This is especially helpful while migrating from legacy systems.
var suggestions []Suggestion
sess := mysqlSession
sess.Select("*").From("suggestions").Load(&suggestions)

SelectStmt with where-value interpolation

// database/sql uses prepared statements, which means each argument
// in an IN clause needs its own question mark.
// gocraft/dbr, on the other hand, handles interpolation itself
// so that you can easily use a single question mark paired with a
// dynamically sized slice.

sess := mysqlSession
ids := []int64{1, 2, 3, 4, 5}
sess.Select("*").From("suggestions").Where("id IN ?", ids)

SelectStmt with joins

sess := mysqlSession
sess.Select("*").From("suggestions").
    Join("subdomains", "suggestions.subdomain_id = subdomains.id")

sess.Select("*").From("suggestions").
    LeftJoin("subdomains", "suggestions.subdomain_id = subdomains.id")

// join multiple tables
sess.Select("*").From("suggestions").
    Join("subdomains", "suggestions.subdomain_id = subdomains.id").
    Join("accounts", "subdomains.accounts_id = accounts.id")

SelectStmt with raw SQL

SelectBySql("SELECT `title`, `body` FROM `suggestions` ORDER BY `id` ASC LIMIT 10")

InsertStmt adds data from struct

type Suggestion struct {
    ID      int64
    Title       NullString
    CreatedAt   time.Time
}
sugg := &Suggestion{
    Title:      NewNullString("Gopher"),
    CreatedAt:  time.Now(),
}
sess := mysqlSession
sess.InsertInto("suggestions").
    Columns("title").
    Record(sugg).
    Exec()

// id is set automatically
fmt.Println(sugg.ID)

InsertStmt adds data from value

sess := mysqlSession
sess.InsertInto("suggestions").
    Pair("title", "Gopher").
    Pair("body", "I love go.")

Benchmark (2018-05-11)

BenchmarkLoadValues/sqlx_10-8               5000        407318 ns/op        3913 B/op        164 allocs/op
BenchmarkLoadValues/dbr_10-8                5000        372940 ns/op        3874 B/op        123 allocs/op
BenchmarkLoadValues/sqlx_100-8              2000        584197 ns/op       30195 B/op       1428 allocs/op
BenchmarkLoadValues/dbr_100-8               3000        558852 ns/op       22965 B/op        937 allocs/op
BenchmarkLoadValues/sqlx_1000-8             1000       2319101 ns/op      289339 B/op      14031 allocs/op
BenchmarkLoadValues/dbr_1000-8              1000       2310441 ns/op      210092 B/op       9040 allocs/op
BenchmarkLoadValues/sqlx_10000-8             100      17004716 ns/op     3193997 B/op     140043 allocs/op
BenchmarkLoadValues/dbr_10000-8              100      16150062 ns/op     2394698 B/op      90051 allocs/op
BenchmarkLoadValues/sqlx_100000-8             10     170068209 ns/op    31679944 B/op    1400053 allocs/op
BenchmarkLoadValues/dbr_100000-8              10     147202536 ns/op    23680625 B/op     900061 allocs/op

Thanks & Authors

Inspiration from these excellent libraries: * sqlx - various useful tools and utils for interacting with database/sql. * Squirrel - simple fluent query builder.

Authors: * Jonathan Novak -- https://github.com/cypriss * Tai-Lin Chu -- https://github.com/taylorchu * Sponsored by UserVoice

Contributors: * Paul Bergeron -- https://github.com/dinedal - SQLite dialect

License

FOSSA Status

Extension points exported contracts — how you extend this code

Builder (Interface)
Builder builds SQL in Dialect like MySQL, and PostgreSQL. The raw SQL and values are stored in Buffer. The core of gocr [7 …
builder.go
Dialect (Interface)
Dialect abstracts database driver differences in encoding types, and placeholders. [4 implementers]
dialect.go
TracingEventReceiver (Interface)
TracingEventReceiver is an optional interface an EventReceiver type can implement to allow tracing instrumentation [2 …
event.go
CaseBuilder (Interface)
CaseBuilder interface that includes AS for aliasing CASE statements. [2 implementers]
branch.go
SessionRunner (Interface)
SessionRunner can do anything that a Session can except start a transaction. Both Session and Tx implements this interfa [2 …
dbr.go
Iterator (Interface)
Iterator is an interface to iterate over the result of a sql query and scan each row one at a time instead of getting al [1 …
iterator.go
Buffer (Interface)
Buffer collects strings, and values that are ready to be interpolated. This is used internally to efficiently build SQL
buffer.go
BuildFunc (FuncType)
BuildFunc implements Builder.
builder.go

Core symbols most depended-on inside this repo

WriteString
called by 158
buffer.go
Build
called by 43
builder.go
From
called by 38
select.go
Eq
called by 31
condition.go
Exec
called by 29
update.go
Select
called by 29
dbr.go
Values
called by 24
insert.go
QuoteIdent
called by 21
dialect.go

Shape

Method 212
Function 123
Struct 37
Interface 9
TypeAlias 6
FuncType 2

Languages

Go100%

Modules by API surface

select.go38 symbols
types.go23 symbols
update.go21 symbols
dbr.go21 symbols
insert.go19 symbols
example_test.go19 symbols
event.go19 symbols
iterator.go13 symbols
delete.go13 symbols
condition.go13 symbols
select_return.go12 symbols
util.go10 symbols

Used by 2 indexed graphs manifest dependencies, hub-wide

Dependencies from manifests, versioned

github.com/davecgh/go-spewv1.1.0 · 1×
github.com/golang-sql/civilv0.0.0-2019071916385 · 1×
github.com/lib/pqv1.10.4 · 1×
github.com/mattn/go-sqlite3v1.14.8 · 1×
github.com/opentracing/opentracing-gov1.2.0 · 1×
github.com/pmezard/go-difflibv1.0.0 · 1×
golang.org/x/cryptov0.24.0 · 1×

For agents

$ claude mcp add dbr \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact