Documentation
¶
Overview ¶
Package chuck provides database engine abstractions for multi-dialect SQL DDL. It allows switching between database engines (e.g., MSSQL for production, SQLite for development) while keeping SQL visible and explicit.
Index ¶
- func QuoteColumns(d Identifier, columns string) string
- type DDLWriter
- type Dialect
- type Engine
- type Identifier
- type Inspector
- type MSSQLDialect
- func (MSSQLDialect) AutoIncrement() string
- func (MSSQLDialect) BigIntType() string
- func (MSSQLDialect) BoolType() string
- func (d MSSQLDialect) Concat(parts ...string) string
- func (d MSSQLDialect) CreateIndexIfNotExists(indexName, table, columns string) string
- func (d MSSQLDialect) CreateTableIfNotExists(table, body string) string
- func (MSSQLDialect) DecimalType(precision, scale int) string
- func (d MSSQLDialect) DropTableIfExists(table string) string
- func (MSSQLDialect) Engine() Engine
- func (MSSQLDialect) FloatType() string
- func (d MSSQLDialect) InsertOrIgnore(table, columns, values string) string
- func (MSSQLDialect) IntType() string
- func (d MSSQLDialect) IsNull(col, fallback string) string
- func (MSSQLDialect) JSONType() string
- func (MSSQLDialect) LastInsertIDQuery() string
- func (MSSQLDialect) NormalizeIdentifier(name string) string
- func (MSSQLDialect) Now() string
- func (MSSQLDialect) Pagination() string
- func (MSSQLDialect) Placeholder(n int) string
- func (d MSSQLDialect) QuoteIdentifier(name string) string
- func (MSSQLDialect) ReturningClause(_ string) string
- func (MSSQLDialect) StringType(maxLen int) string
- func (MSSQLDialect) SupportsLastInsertID() bool
- func (MSSQLDialect) TableColumnsQuery() string
- func (MSSQLDialect) TableExistsQuery() string
- func (MSSQLDialect) TextType() string
- func (MSSQLDialect) TimestampType() string
- func (MSSQLDialect) UUIDType() string
- func (d MSSQLDialect) Upsert(table, columns, values, conflictColumns, updateSet string) string
- func (MSSQLDialect) VarcharType(maxLen int) string
- type PostgresDialect
- func (PostgresDialect) AutoIncrement() string
- func (PostgresDialect) BigIntType() string
- func (PostgresDialect) BoolType() string
- func (d PostgresDialect) Concat(parts ...string) string
- func (d PostgresDialect) CreateIndexIfNotExists(indexName, table, columns string) string
- func (d PostgresDialect) CreateTableIfNotExists(table, body string) string
- func (PostgresDialect) DecimalType(precision, scale int) string
- func (d PostgresDialect) DropTableIfExists(table string) string
- func (PostgresDialect) Engine() Engine
- func (PostgresDialect) FloatType() string
- func (d PostgresDialect) InsertOrIgnore(table, columns, values string) string
- func (PostgresDialect) IntType() string
- func (d PostgresDialect) IsNull(col, fallback string) string
- func (PostgresDialect) JSONType() string
- func (PostgresDialect) LastInsertIDQuery() string
- func (PostgresDialect) NormalizeIdentifier(name string) string
- func (PostgresDialect) Now() string
- func (PostgresDialect) Pagination() string
- func (PostgresDialect) Placeholder(n int) string
- func (PostgresDialect) QuoteIdentifier(name string) string
- func (PostgresDialect) ReturningClause(columns string) string
- func (PostgresDialect) StringType(_ int) string
- func (PostgresDialect) SupportsLastInsertID() bool
- func (PostgresDialect) TableColumnsQuery() string
- func (PostgresDialect) TableExistsQuery() string
- func (PostgresDialect) TextType() string
- func (PostgresDialect) TimestampType() string
- func (PostgresDialect) UUIDType() string
- func (d PostgresDialect) Upsert(table, columns, values, conflictColumns, updateSet string) string
- func (PostgresDialect) VarcharType(maxLen int) string
- type QueryWriter
- type SQLiteDialect
- func (SQLiteDialect) AutoIncrement() string
- func (SQLiteDialect) BigIntType() string
- func (SQLiteDialect) BoolType() string
- func (d SQLiteDialect) Concat(parts ...string) string
- func (d SQLiteDialect) CreateIndexIfNotExists(indexName, table, columns string) string
- func (d SQLiteDialect) CreateTableIfNotExists(table, body string) string
- func (SQLiteDialect) DecimalType(_, _ int) string
- func (d SQLiteDialect) DropTableIfExists(table string) string
- func (SQLiteDialect) Engine() Engine
- func (SQLiteDialect) FloatType() string
- func (d SQLiteDialect) InsertOrIgnore(table, columns, values string) string
- func (SQLiteDialect) IntType() string
- func (d SQLiteDialect) IsNull(col, fallback string) string
- func (SQLiteDialect) JSONType() string
- func (SQLiteDialect) LastInsertIDQuery() string
- func (SQLiteDialect) NormalizeIdentifier(name string) string
- func (SQLiteDialect) Now() string
- func (SQLiteDialect) Pagination() string
- func (SQLiteDialect) Placeholder(_ int) string
- func (SQLiteDialect) QuoteIdentifier(name string) string
- func (SQLiteDialect) ReturningClause(columns string) string
- func (SQLiteDialect) StringType(_ int) string
- func (SQLiteDialect) SupportsLastInsertID() bool
- func (SQLiteDialect) TableColumnsQuery() string
- func (SQLiteDialect) TableExistsQuery() string
- func (SQLiteDialect) TextType() string
- func (SQLiteDialect) TimestampType() string
- func (SQLiteDialect) UUIDType() string
- func (d SQLiteDialect) Upsert(table, columns, values, conflictColumns, updateSet string) string
- func (SQLiteDialect) VarcharType(_ int) string
- type TypeMapper
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func QuoteColumns ¶
func QuoteColumns(d Identifier, columns string) string
QuoteColumns splits a comma-separated column list, normalizes and quotes each identifier. Sort direction suffixes (ASC, DESC) are preserved and re-appended after quoting.
Example ¶
package main
import (
"fmt"
"github.com/catgoose/chuck"
)
func main() {
d, _ := chuck.New(chuck.Postgres)
fmt.Println(chuck.QuoteColumns(d, "CreatedAt, Title DESC"))
}
Output: "created_at", "title" DESC
Types ¶
type DDLWriter ¶
type DDLWriter interface {
CreateTableIfNotExists(table, body string) string
DropTableIfExists(table string) string
CreateIndexIfNotExists(indexName, table, columns string) string
InsertOrIgnore(table, columns, values string) string
Upsert(table, columns, values, conflictColumns, updateSet string) string
ReturningClause(columns string) string
}
DDLWriter generates DDL statements.
type Dialect ¶
type Dialect interface {
TypeMapper
DDLWriter
QueryWriter
Identifier
Inspector
// Engine returns the engine identifier (used as the driver name for sql.Open).
Engine() Engine
}
Dialect provides engine-specific SQL fragments. It composes all sub-interfaces, so any value that implements Dialect also satisfies TypeMapper, DDLWriter, QueryWriter, Identifier, and Inspector. Implementations return raw SQL strings that callers compose into full queries.
func New ¶
New returns a Dialect for the given engine.
Example ¶
package main
import (
"fmt"
"github.com/catgoose/chuck"
)
func main() {
d, err := chuck.New(chuck.Postgres)
if err != nil {
panic(err)
}
fmt.Println(d.AutoIncrement())
fmt.Println(d.TimestampType())
fmt.Println(d.Now())
fmt.Println(d.Placeholder(1))
fmt.Println(d.Pagination())
}
Output: SERIAL PRIMARY KEY TIMESTAMPTZ NOW() $1 LIMIT @Limit OFFSET @Offset
Example (ColumnTypes) ¶
package main
import (
"fmt"
"github.com/catgoose/chuck"
)
func main() {
d, _ := chuck.New(chuck.Postgres)
fmt.Println(d.StringType(255))
fmt.Println(d.VarcharType(100))
fmt.Println(d.IntType())
fmt.Println(d.BigIntType())
fmt.Println(d.FloatType())
fmt.Println(d.DecimalType(10, 2))
fmt.Println(d.TextType())
fmt.Println(d.UUIDType())
fmt.Println(d.JSONType())
}
Output: TEXT VARCHAR(100) INTEGER BIGINT DOUBLE PRECISION NUMERIC(10,2) TEXT UUID JSONB
Example (Mssql) ¶
package main
import (
"fmt"
"github.com/catgoose/chuck"
)
func main() {
d, err := chuck.New(chuck.MSSQL)
if err != nil {
panic(err)
}
fmt.Println(d.AutoIncrement())
fmt.Println(d.Pagination())
fmt.Println(d.QuoteIdentifier("users"))
}
Output: INT PRIMARY KEY IDENTITY(1,1) OFFSET @Offset ROWS FETCH NEXT @Limit ROWS ONLY [users]
Example (NormalizeIdentifier) ¶
package main
import (
"fmt"
"github.com/catgoose/chuck"
)
func main() {
pg, _ := chuck.New(chuck.Postgres)
sq, _ := chuck.New(chuck.SQLite)
fmt.Println(pg.NormalizeIdentifier("CreatedAt"))
fmt.Println(pg.NormalizeIdentifier("UserID"))
fmt.Println(sq.NormalizeIdentifier("CreatedAt"))
}
Output: created_at user_id CreatedAt
Example (Sqlite) ¶
package main
import (
"fmt"
"github.com/catgoose/chuck"
)
func main() {
d, err := chuck.New(chuck.SQLite)
if err != nil {
panic(err)
}
fmt.Println(d.AutoIncrement())
fmt.Println(d.TimestampType())
fmt.Println(d.Now())
fmt.Println(d.BoolType())
}
Output: INTEGER PRIMARY KEY AUTOINCREMENT TIMESTAMP CURRENT_TIMESTAMP INTEGER
func OpenSQLite ¶
OpenSQLite opens a SQLite database at the given path with standard settings: WAL journal mode, 30s busy timeout, and conservative pool settings (1 conn). Returns the raw *sql.DB and the SQLite Dialect.
func OpenURL ¶
OpenURL opens a database connection from a URL string. The scheme determines the driver and dialect:
postgres://user:pass@host:5432/dbname?sslmode=disable sqlite:///path/to/db.sqlite or sqlite:///:memory: sqlserver://user:pass@host:1433?database=dbname
Returns the raw *sql.DB and the matching Dialect for SQL generation.
type Engine ¶
type Engine string
Engine identifies a database engine.
func ParseEngine ¶
ParseEngine converts a string to an Engine, returning an error for unknown values.
Example ¶
package main
import (
"fmt"
"github.com/catgoose/chuck"
)
func main() {
e, err := chuck.ParseEngine("postgres")
if err != nil {
panic(err)
}
fmt.Println(e)
_, err = chuck.ParseEngine("mysql")
fmt.Println(err)
}
Output: postgres unknown database engine: "mysql" (expected sqlserver, mssql, sqlite3, sqlite, postgres, or postgresql)
type Identifier ¶
type Identifier interface {
NormalizeIdentifier(name string) string
QuoteIdentifier(name string) string
}
Identifier handles SQL identifier formatting.
type MSSQLDialect ¶
type MSSQLDialect struct{}
MSSQLDialect implements Dialect for Microsoft SQL Server.
func (MSSQLDialect) AutoIncrement ¶
func (MSSQLDialect) AutoIncrement() string
func (MSSQLDialect) BigIntType ¶
func (MSSQLDialect) BigIntType() string
func (MSSQLDialect) BoolType ¶
func (MSSQLDialect) BoolType() string
func (MSSQLDialect) Concat ¶ added in v0.1.10
func (d MSSQLDialect) Concat(parts ...string) string
func (MSSQLDialect) CreateIndexIfNotExists ¶
func (d MSSQLDialect) CreateIndexIfNotExists(indexName, table, columns string) string
func (MSSQLDialect) CreateTableIfNotExists ¶
func (d MSSQLDialect) CreateTableIfNotExists(table, body string) string
func (MSSQLDialect) DecimalType ¶
func (MSSQLDialect) DecimalType(precision, scale int) string
func (MSSQLDialect) DropTableIfExists ¶
func (d MSSQLDialect) DropTableIfExists(table string) string
func (MSSQLDialect) Engine ¶
func (MSSQLDialect) Engine() Engine
func (MSSQLDialect) FloatType ¶
func (MSSQLDialect) FloatType() string
func (MSSQLDialect) InsertOrIgnore ¶
func (d MSSQLDialect) InsertOrIgnore(table, columns, values string) string
func (MSSQLDialect) IntType ¶
func (MSSQLDialect) IntType() string
func (MSSQLDialect) IsNull ¶ added in v0.1.10
func (d MSSQLDialect) IsNull(col, fallback string) string
func (MSSQLDialect) JSONType ¶
func (MSSQLDialect) JSONType() string
func (MSSQLDialect) LastInsertIDQuery ¶
func (MSSQLDialect) LastInsertIDQuery() string
func (MSSQLDialect) NormalizeIdentifier ¶
func (MSSQLDialect) NormalizeIdentifier(name string) string
func (MSSQLDialect) Now ¶
func (MSSQLDialect) Now() string
func (MSSQLDialect) Pagination ¶
func (MSSQLDialect) Pagination() string
func (MSSQLDialect) Placeholder ¶
func (MSSQLDialect) Placeholder(n int) string
func (MSSQLDialect) QuoteIdentifier ¶
func (d MSSQLDialect) QuoteIdentifier(name string) string
func (MSSQLDialect) ReturningClause ¶
func (MSSQLDialect) ReturningClause(_ string) string
func (MSSQLDialect) StringType ¶
func (MSSQLDialect) StringType(maxLen int) string
func (MSSQLDialect) SupportsLastInsertID ¶
func (MSSQLDialect) SupportsLastInsertID() bool
func (MSSQLDialect) TableColumnsQuery ¶
func (MSSQLDialect) TableColumnsQuery() string
func (MSSQLDialect) TableExistsQuery ¶
func (MSSQLDialect) TableExistsQuery() string
func (MSSQLDialect) TextType ¶
func (MSSQLDialect) TextType() string
func (MSSQLDialect) TimestampType ¶
func (MSSQLDialect) TimestampType() string
func (MSSQLDialect) UUIDType ¶
func (MSSQLDialect) UUIDType() string
func (MSSQLDialect) Upsert ¶ added in v0.1.10
func (d MSSQLDialect) Upsert(table, columns, values, conflictColumns, updateSet string) string
func (MSSQLDialect) VarcharType ¶
func (MSSQLDialect) VarcharType(maxLen int) string
type PostgresDialect ¶
type PostgresDialect struct{}
PostgresDialect implements Dialect for PostgreSQL.
func (PostgresDialect) AutoIncrement ¶
func (PostgresDialect) AutoIncrement() string
func (PostgresDialect) BigIntType ¶
func (PostgresDialect) BigIntType() string
func (PostgresDialect) BoolType ¶
func (PostgresDialect) BoolType() string
func (PostgresDialect) Concat ¶ added in v0.1.10
func (d PostgresDialect) Concat(parts ...string) string
func (PostgresDialect) CreateIndexIfNotExists ¶
func (d PostgresDialect) CreateIndexIfNotExists(indexName, table, columns string) string
func (PostgresDialect) CreateTableIfNotExists ¶
func (d PostgresDialect) CreateTableIfNotExists(table, body string) string
func (PostgresDialect) DecimalType ¶
func (PostgresDialect) DecimalType(precision, scale int) string
func (PostgresDialect) DropTableIfExists ¶
func (d PostgresDialect) DropTableIfExists(table string) string
func (PostgresDialect) Engine ¶
func (PostgresDialect) Engine() Engine
func (PostgresDialect) FloatType ¶
func (PostgresDialect) FloatType() string
func (PostgresDialect) InsertOrIgnore ¶
func (d PostgresDialect) InsertOrIgnore(table, columns, values string) string
func (PostgresDialect) IntType ¶
func (PostgresDialect) IntType() string
func (PostgresDialect) IsNull ¶ added in v0.1.10
func (d PostgresDialect) IsNull(col, fallback string) string
func (PostgresDialect) JSONType ¶
func (PostgresDialect) JSONType() string
func (PostgresDialect) LastInsertIDQuery ¶
func (PostgresDialect) LastInsertIDQuery() string
func (PostgresDialect) NormalizeIdentifier ¶
func (PostgresDialect) NormalizeIdentifier(name string) string
func (PostgresDialect) Now ¶
func (PostgresDialect) Now() string
func (PostgresDialect) Pagination ¶
func (PostgresDialect) Pagination() string
func (PostgresDialect) Placeholder ¶
func (PostgresDialect) Placeholder(n int) string
func (PostgresDialect) QuoteIdentifier ¶
func (PostgresDialect) QuoteIdentifier(name string) string
func (PostgresDialect) ReturningClause ¶
func (PostgresDialect) ReturningClause(columns string) string
func (PostgresDialect) StringType ¶
func (PostgresDialect) StringType(_ int) string
func (PostgresDialect) SupportsLastInsertID ¶
func (PostgresDialect) SupportsLastInsertID() bool
func (PostgresDialect) TableColumnsQuery ¶
func (PostgresDialect) TableColumnsQuery() string
func (PostgresDialect) TableExistsQuery ¶
func (PostgresDialect) TableExistsQuery() string
func (PostgresDialect) TextType ¶
func (PostgresDialect) TextType() string
func (PostgresDialect) TimestampType ¶
func (PostgresDialect) TimestampType() string
func (PostgresDialect) UUIDType ¶
func (PostgresDialect) UUIDType() string
func (PostgresDialect) Upsert ¶ added in v0.1.10
func (d PostgresDialect) Upsert(table, columns, values, conflictColumns, updateSet string) string
func (PostgresDialect) VarcharType ¶
func (PostgresDialect) VarcharType(maxLen int) string
type QueryWriter ¶
type QueryWriter interface {
Placeholder(n int) string
Pagination() string
Now() string
LastInsertIDQuery() string
SupportsLastInsertID() bool
// IsNull returns a dialect-specific expression that evaluates to fallback
// when col is NULL (e.g. COALESCE, IFNULL, ISNULL).
IsNull(col, fallback string) string
// Concat returns a dialect-specific concatenation of the given parts.
// Parts wrapped in single quotes (string literals) are passed through as-is;
// bare identifiers are normalized and quoted.
Concat(parts ...string) string
}
QueryWriter generates query fragments.
type SQLiteDialect ¶
type SQLiteDialect struct{}
SQLiteDialect implements Dialect for SQLite.
func (SQLiteDialect) AutoIncrement ¶
func (SQLiteDialect) AutoIncrement() string
func (SQLiteDialect) BigIntType ¶
func (SQLiteDialect) BigIntType() string
func (SQLiteDialect) BoolType ¶
func (SQLiteDialect) BoolType() string
func (SQLiteDialect) Concat ¶ added in v0.1.10
func (d SQLiteDialect) Concat(parts ...string) string
func (SQLiteDialect) CreateIndexIfNotExists ¶
func (d SQLiteDialect) CreateIndexIfNotExists(indexName, table, columns string) string
func (SQLiteDialect) CreateTableIfNotExists ¶
func (d SQLiteDialect) CreateTableIfNotExists(table, body string) string
func (SQLiteDialect) DecimalType ¶
func (SQLiteDialect) DecimalType(_, _ int) string
func (SQLiteDialect) DropTableIfExists ¶
func (d SQLiteDialect) DropTableIfExists(table string) string
func (SQLiteDialect) Engine ¶
func (SQLiteDialect) Engine() Engine
func (SQLiteDialect) FloatType ¶
func (SQLiteDialect) FloatType() string
func (SQLiteDialect) InsertOrIgnore ¶
func (d SQLiteDialect) InsertOrIgnore(table, columns, values string) string
func (SQLiteDialect) IntType ¶
func (SQLiteDialect) IntType() string
func (SQLiteDialect) IsNull ¶ added in v0.1.10
func (d SQLiteDialect) IsNull(col, fallback string) string
func (SQLiteDialect) JSONType ¶
func (SQLiteDialect) JSONType() string
func (SQLiteDialect) LastInsertIDQuery ¶
func (SQLiteDialect) LastInsertIDQuery() string
func (SQLiteDialect) NormalizeIdentifier ¶
func (SQLiteDialect) NormalizeIdentifier(name string) string
func (SQLiteDialect) Now ¶
func (SQLiteDialect) Now() string
func (SQLiteDialect) Pagination ¶
func (SQLiteDialect) Pagination() string
func (SQLiteDialect) Placeholder ¶
func (SQLiteDialect) Placeholder(_ int) string
func (SQLiteDialect) QuoteIdentifier ¶
func (SQLiteDialect) QuoteIdentifier(name string) string
func (SQLiteDialect) ReturningClause ¶
func (SQLiteDialect) ReturningClause(columns string) string
func (SQLiteDialect) StringType ¶
func (SQLiteDialect) StringType(_ int) string
func (SQLiteDialect) SupportsLastInsertID ¶
func (SQLiteDialect) SupportsLastInsertID() bool
func (SQLiteDialect) TableColumnsQuery ¶
func (SQLiteDialect) TableColumnsQuery() string
func (SQLiteDialect) TableExistsQuery ¶
func (SQLiteDialect) TableExistsQuery() string
func (SQLiteDialect) TextType ¶
func (SQLiteDialect) TextType() string
func (SQLiteDialect) TimestampType ¶
func (SQLiteDialect) TimestampType() string
func (SQLiteDialect) UUIDType ¶
func (SQLiteDialect) UUIDType() string
func (SQLiteDialect) Upsert ¶ added in v0.1.10
func (d SQLiteDialect) Upsert(table, columns, values, conflictColumns, updateSet string) string
func (SQLiteDialect) VarcharType ¶
func (SQLiteDialect) VarcharType(_ int) string
type TypeMapper ¶
type TypeMapper interface {
IntType() string
BigIntType() string
TextType() string
StringType(maxLen int) string
VarcharType(maxLen int) string
BoolType() string
FloatType() string
DecimalType(precision, scale int) string
TimestampType() string
UUIDType() string
JSONType() string
AutoIncrement() string
}
TypeMapper maps Go types to SQL column type strings.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package dbrepo provides composable SQL fragment helpers for building queries.
|
Package dbrepo provides composable SQL fragment helpers for building queries. |
|
driver
|
|
|
mssql
Package mssql registers the Microsoft SQL Server database driver.
|
Package mssql registers the Microsoft SQL Server database driver. |
|
postgres
Package postgres registers the PostgreSQL database driver.
|
Package postgres registers the PostgreSQL database driver. |
|
sqlite
Package sqlite registers the SQLite database driver.
|
Package sqlite registers the SQLite database driver. |
|
Package schema provides a table definition DSL for declaring database schemas in Go.
|
Package schema provides a table definition DSL for declaring database schemas in Go. |
