MCPcopy
hub / github.com/Masterminds/squirrel

github.com/Masterminds/squirrel @v1.5.4 sqlite

repository ↗ · DeepWiki ↗ · release v1.5.4 ↗
491 symbols 2,010 edges 40 files 191 documented · 39%
README

Stability: Maintenance

Squirrel is "complete".

Bug fixes will still be merged (slowly). Bug reports are welcome, but I will not necessarily respond to them. If another fork (or substantially similar project) actively improves on what Squirrel does, let me know and I may link to it here.

Squirrel - fluent SQL generator for Go

import "github.com/Masterminds/squirrel"

GoDoc Build Status

Squirrel is not an ORM. For an application of Squirrel, check out structable, a table-struct mapper

Squirrel helps you build SQL queries from composable parts:

import sq "github.com/Masterminds/squirrel"

users := sq.Select("*").From("users").Join("emails USING (email_id)")

active := users.Where(sq.Eq{"deleted_at": nil})

sql, args, err := active.ToSql()

sql == "SELECT * FROM users JOIN emails USING (email_id) WHERE deleted_at IS NULL"
sql, args, err := sq.
    Insert("users").Columns("name", "age").
    Values("moe", 13).Values("larry", sq.Expr("? + 5", 12)).
    ToSql()

sql == "INSERT INTO users (name,age) VALUES (?,?),(?,? + 5)"

Squirrel can also execute queries directly:

stooges := users.Where(sq.Eq{"username": []string{"moe", "larry", "curly", "shemp"}})
three_stooges := stooges.Limit(3)
rows, err := three_stooges.RunWith(db).Query()

// Behaves like:
rows, err := db.Query("SELECT * FROM users WHERE username IN (?,?,?,?) LIMIT 3",
                      "moe", "larry", "curly", "shemp")

Squirrel makes conditional query building a breeze:

if len(q) > 0 {
    users = users.Where("name LIKE ?", fmt.Sprint("%", q, "%"))
}

Squirrel wants to make your life easier:

// StmtCache caches Prepared Stmts for you
dbCache := sq.NewStmtCache(db)

// StatementBuilder keeps your syntax neat
mydb := sq.StatementBuilder.RunWith(dbCache)
select_users := mydb.Select("*").From("users")

Squirrel loves PostgreSQL:

psql := sq.StatementBuilder.PlaceholderFormat(sq.Dollar)

// You use question marks for placeholders...
sql, _, _ := psql.Select("*").From("elephants").Where("name IN (?,?)", "Dumbo", "Verna").ToSql()

/// ...squirrel replaces them using PlaceholderFormat.
sql == "SELECT * FROM elephants WHERE name IN ($1,$2)"


/// You can retrieve id ...
query := sq.Insert("nodes").
    Columns("uuid", "type", "data").
    Values(node.Uuid, node.Type, node.Data).
    Suffix("RETURNING \"id\"").
    RunWith(m.db).
    PlaceholderFormat(sq.Dollar)

query.QueryRow().Scan(&node.id)

You can escape question marks by inserting two question marks:

SELECT * FROM nodes WHERE meta->'format' ??| array[?,?]

will generate with the Dollar Placeholder:

SELECT * FROM nodes WHERE meta->'format' ?| array[$1,$2]

FAQ

  • How can I build an IN query on composite keys / tuples, e.g. WHERE (col1, col2) IN ((1,2),(3,4))? (#104)

    Squirrel does not explicitly support tuples, but you can get the same effect with e.g.:

    go sq.Or{ sq.Eq{"col1": 1, "col2": 2}, sq.Eq{"col1": 3, "col2": 4}}

    sql WHERE (col1 = 1 AND col2 = 2) OR (col1 = 3 AND col2 = 4)

    (which should produce the same query plan as the tuple version)

  • Why doesn't Eq{"mynumber": []uint8{1,2,3}} turn into an IN query? (#114)

    Values of type []byte are handled specially by database/sql. In Go, byte is just an alias of uint8, so there is no way to distinguish []uint8 from []byte.

  • Some features are poorly documented!

    This isn't a frequent complaints section!

  • Some features are poorly documented?

    Yes. The tests should be considered a part of the documentation; take a look at those for ideas on how to express more complex queries.

License

Squirrel is released under the MIT License.

Extension points exported contracts — how you extend this code

ExecerContext (Interface)
ExecerContext is the interface that wraps the ExecContext method. Exec executes the given query as implemented by datab [6 …
squirrel_ctx.go
Sqlizer (Interface)
Sqlizer is the interface that wraps the ToSql method. ToSql returns a SQL representation of the Sqlizer, along with a s [9 …
squirrel.go
PlaceholderFormat (Interface)
PlaceholderFormat is the interface that wraps the ReplacePlaceholders method. ReplacePlaceholders takes a SQL statement [4 …
placeholder.go
PreparerContext (Interface)
PrepareerContext is the interface that wraps the Prepare and PrepareContext methods. Prepare executes the given query a [2 …
stmtcacher_ctx.go
Preparer (Interface)
Prepareer is the interface that wraps the Prepare method. Prepare executes the given query as implemented by database/s [2 …
stmtcacher.go
RowScanner (Interface)
RowScanner is the interface that wraps the Scan method. Scan behaves like database/sql.Row.Scan. [2 implementers]
row.go
QueryerContext (Interface)
QueryerContext is the interface that wraps the QueryContext method. QueryContext executes the given query as implemente [6 …
squirrel_ctx.go
Execer (Interface)
Execer is the interface that wraps the Exec method. Exec executes the given query as implemented by database/sql.Exec. [7 …
squirrel.go

Core symbols most depended-on inside this repo

ToSql
called by 90
squirrel.go
Select
called by 62
statement.go
Where
called by 57
update.go
From
called by 55
update.go
PlaceholderFormat
called by 39
update.go
Expr
called by 38
expr.go
Set
called by 37
update.go
RunWith
called by 25
update.go

Shape

Method 226
Function 200
Struct 23
Interface 21
TypeAlias 21

Languages

Go100%

Modules by API surface

select.go43 symbols
expr.go40 symbols
select_test.go34 symbols
expr_test.go30 symbols
update.go29 symbols
insert.go28 symbols
squirrel_test.go27 symbols
squirrel.go24 symbols
delete.go21 symbols
squirrel_ctx.go18 symbols
placeholder.go18 symbols
statement.go15 symbols

Dependencies from manifests, versioned

github.com/davecgh/go-spewv1.1.1 · 1×
github.com/lann/builderv0.0.0-2018080220072 · 1×
github.com/lann/psv0.0.0-2015081015235 · 1×
github.com/lib/pqv1.2.0 · 1×
github.com/mattn/go-sqlite3v1.13.0 · 1×
github.com/pmezard/go-difflibv1.0.0 · 1×
google.golang.org/appenginev1.6.5 · 1×

Datastores touched

squirrelDatabase · 1 repos

For agents

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

⬇ download graph artifact