Compare commits
No commits in common. "main" and "v0.1.0" have entirely different histories.
151
fts5/fts5.go
151
fts5/fts5.go
@ -1,151 +0,0 @@
|
||||
package fts5
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"iter"
|
||||
"strings"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type FTS5 struct {
|
||||
colNames []string
|
||||
db *sql.DB
|
||||
|
||||
insertStmt *sql.Stmt
|
||||
matchStmt *sql.Stmt
|
||||
}
|
||||
|
||||
func OpenMem(columnNames ...string) (*FTS5, error) {
|
||||
db, err := sql.Open("sqlite3", ":memory:")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newInternal(db, columnNames)
|
||||
}
|
||||
|
||||
func Open(path string, columnNames ...string) (*FTS5, error) {
|
||||
db, err := sql.Open("sqlite3", path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newInternal(db, columnNames)
|
||||
}
|
||||
|
||||
func newInternal(db *sql.DB, columnNames []string) (*FTS5, error) {
|
||||
_, err := db.Exec(fmt.Sprintf(
|
||||
`CREATE VIRTUAL TABLE IF NOT EXISTS search USING fts5(%s, tokenize='porter')`,
|
||||
strings.Join(columnNames, ",")))
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
insertStmt, err := prepareInsertStmt(db, columnNames)
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
matchStmt, err := db.Prepare(`SELECT rowid,rank FROM search WHERE search=? ` +
|
||||
`ORDER BY rank ` +
|
||||
`LIMIT ? OFFSET ?`)
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &FTS5{
|
||||
colNames: columnNames,
|
||||
db: db,
|
||||
insertStmt: insertStmt,
|
||||
matchStmt: matchStmt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (fts *FTS5) Close() error {
|
||||
return fts.db.Close()
|
||||
}
|
||||
|
||||
func (fts *FTS5) Upsert(id int64, data map[string]string) error {
|
||||
return execInsertStmt(fts.insertStmt, fts.colNames, id, data)
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
ID int64
|
||||
Data map[string]string
|
||||
}
|
||||
|
||||
func (fts *FTS5) UpsertBulk(src iter.Seq2[Item, error]) error {
|
||||
tx, err := fts.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
insertStmt, err := prepareInsertStmt(tx, fts.colNames)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
for item, err := range src {
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
if err := execInsertStmt(insertStmt, fts.colNames, item.ID, item.Data); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (fts *FTS5) Delete(id int64) (bool, error) {
|
||||
result, err := fts.db.Exec(`DELETE FROM search WHERE rowid=?`, id)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
count, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if count > 1 {
|
||||
panic("multiple rows deleted")
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func (fts *FTS5) DeleteAll() error {
|
||||
_, err := fts.db.Exec(`DELETE FROM search`)
|
||||
return err
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
ID int64
|
||||
Rank float64
|
||||
}
|
||||
|
||||
func (fts *FTS5) Match(query string, limit, offset int) ([]Result, error) {
|
||||
results := make([]Result, 0, limit)
|
||||
|
||||
rows, err := fts.matchStmt.Query(query, limit, offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
result := Result{}
|
||||
if err := rows.Scan(&result.ID, &result.Rank); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, result)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
package fts5
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMatch(t *testing.T) {
|
||||
fts, err := OpenMem("Title", "Text")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
fts.Upsert(100, map[string]string{
|
||||
"Title": "Peter Rabbit",
|
||||
"Text": "Peter Rabbit is a fictional animal character in various children's stories by English author Beatrix Potter.",
|
||||
})
|
||||
|
||||
fts.Upsert(200, map[string]string{
|
||||
"Title": "Baloo",
|
||||
"Text": "Fictional bear from the Jungle Book.",
|
||||
})
|
||||
|
||||
fts.Upsert(300, map[string]string{
|
||||
"Title": "Barney Bear",
|
||||
"Text": "A grumpy brown bear.",
|
||||
})
|
||||
|
||||
fts.Upsert(400, map[string]string{
|
||||
"Title": "Peter the Panda",
|
||||
"Text": "A secret agent panda from Seattle.",
|
||||
})
|
||||
|
||||
// Should have 2 results for "peter".
|
||||
results, err := fts.Match("peter", 3, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(results) != 2 {
|
||||
t.Fatal(results)
|
||||
}
|
||||
|
||||
// Try limit.
|
||||
results, err = fts.Match("peter", 1, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatal(results)
|
||||
}
|
||||
|
||||
// Try offset.
|
||||
results, err = fts.Match("peter", 3, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatal(results)
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package fts5
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type preparer interface {
|
||||
Prepare(string) (*sql.Stmt, error)
|
||||
}
|
||||
|
||||
func prepareInsertStmt(
|
||||
db preparer,
|
||||
columnNames []string,
|
||||
) (*sql.Stmt, error) {
|
||||
insertQuery := fmt.Sprintf(`INSERT INTO search(rowid,%s) VALUES (?%s)`,
|
||||
strings.Join(columnNames, ","),
|
||||
strings.Repeat(",?", len(columnNames)))
|
||||
return db.Prepare(insertQuery)
|
||||
}
|
||||
|
||||
func execInsertStmt(
|
||||
stmt *sql.Stmt,
|
||||
colNames []string,
|
||||
id int64,
|
||||
data map[string]string,
|
||||
) error {
|
||||
values := make([]any, len(colNames)+1)
|
||||
values[0] = id
|
||||
for i, col := range colNames {
|
||||
values[i+1] = data[col]
|
||||
}
|
||||
_, err := stmt.Exec(values...)
|
||||
return err
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
//go:build !fts5
|
||||
|
||||
package fts5
|
||||
|
||||
import "log"
|
||||
|
||||
func init() {
|
||||
log.Fatal("You must provide build tag `-tags fts5`.")
|
||||
}
|
22
go.mod
22
go.mod
@ -1,22 +0,0 @@
|
||||
module git.crumpington.com/lib/go
|
||||
|
||||
go 1.23.2
|
||||
|
||||
require (
|
||||
github.com/lib/pq v1.10.9
|
||||
github.com/mailgun/mailgun-go/v4 v4.18.5
|
||||
github.com/mattn/go-sqlite3 v1.14.24
|
||||
golang.org/x/crypto v0.29.0
|
||||
golang.org/x/net v0.31.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/go-chi/chi/v5 v5.0.8 // indirect
|
||||
github.com/json-iterator/go v1.1.10 // indirect
|
||||
github.com/mailgun/errors v0.3.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
|
||||
github.com/sirupsen/logrus v1.9.0 // indirect
|
||||
golang.org/x/sys v0.27.0 // indirect
|
||||
golang.org/x/text v0.20.0 // indirect
|
||||
)
|
44
go.sum
44
go.sum
@ -1,44 +0,0 @@
|
||||
github.com/ahmetb/go-linq v3.0.0+incompatible h1:qQkjjOXKrKOTy83X8OpRmnKflXKQIL/mC/gMVVDMhOA=
|
||||
github.com/ahmetb/go-linq v3.0.0+incompatible/go.mod h1:PFffvbdbtw+QTB0WKRP0cNht7vnCfnGlEpak/DVg5cY=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
|
||||
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
|
||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mailgun/errors v0.3.0 h1:g8R8lodkwqk5WIVMAClyUqt0PSd5JTVgobB+H7C2sLs=
|
||||
github.com/mailgun/errors v0.3.0/go.mod h1:+ltknP+jhv3gZ1StKY6ugoQECcPxDCaSdmYesqTZcLQ=
|
||||
github.com/mailgun/mailgun-go/v4 v4.18.5 h1:wZnTutW/fzxNyJVBMa6O4LLGW/hO+IGfKpuqBT4nbVs=
|
||||
github.com/mailgun/mailgun-go/v4 v4.18.5/go.mod h1:+d4FCswFAukgYc1XtKK2IxOYaVxjVm8AN2z/5TBiT8M=
|
||||
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
|
||||
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
|
||||
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
|
||||
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
|
||||
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
|
||||
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
|
||||
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
|
||||
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
@ -1,43 +0,0 @@
|
||||
package mgmailer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/mailgun/mailgun-go/v4"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
MailgunDomain string
|
||||
MailgunAPIKey string
|
||||
SenderEmail string
|
||||
SenderName string
|
||||
}
|
||||
|
||||
type Mailer struct {
|
||||
conf atomic.Pointer[Config]
|
||||
mg atomic.Pointer[mailgun.MailgunImpl]
|
||||
}
|
||||
|
||||
func (m *Mailer) Update(conf Config) {
|
||||
m.conf.Store(&conf)
|
||||
m.mg.Store(mailgun.NewMailgun(conf.MailgunDomain, conf.MailgunAPIKey))
|
||||
}
|
||||
|
||||
func (m *Mailer) Send(subject, body, to string) error {
|
||||
mg := m.mg.Load()
|
||||
conf := m.conf.Load()
|
||||
message := mailgun.NewMessage(
|
||||
fmt.Sprintf("%s <%s>", conf.SenderName, conf.SenderEmail),
|
||||
subject,
|
||||
body,
|
||||
to)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
|
||||
_, _, err := mg.Send(ctx, message)
|
||||
return err
|
||||
}
|
@ -14,7 +14,7 @@ func ErrIsForeignKey(err error) bool {
|
||||
return ErrHasCode(err, "23503")
|
||||
}
|
||||
|
||||
func ErrIsSerializationFailure(err error) bool {
|
||||
func ErrIsSerializationFaiilure(err error) bool {
|
||||
return ErrHasCode(err, "40001")
|
||||
}
|
||||
|
||||
|
@ -60,7 +60,7 @@ func (r SerialTxRunner) WithTx(db *sql.DB, fn func(*sql.Tx) error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if timeout > r.MaxTimeout || !ErrIsSerializationFailure(err) {
|
||||
if timeout > r.MaxTimeout || !ErrIsSerializationFaiilure(err) {
|
||||
return err
|
||||
}
|
||||
sleepTimeout := timeout + time.Duration(rand.Int63n(int64(timeout/10)))
|
||||
|
@ -5,19 +5,14 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
DRIVER_SQLITE = "sqlite"
|
||||
DRIVER_POSTGRES = "postgres"
|
||||
)
|
||||
|
||||
func Main() {
|
||||
usage := func() {
|
||||
fmt.Fprintf(os.Stderr, `
|
||||
%s DRIVER DEFS_PATH OUTPUT_PATH
|
||||
|
||||
Drivers are one of: %s %s
|
||||
Drivers are one of: sqlite, postgres
|
||||
`,
|
||||
os.Args[0], DRIVER_SQLITE, DRIVER_POSTGRES)
|
||||
os.Args[0])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@ -26,20 +21,21 @@ Drivers are one of: %s %s
|
||||
}
|
||||
|
||||
var (
|
||||
template string
|
||||
driver = os.Args[1]
|
||||
defsPath = os.Args[2]
|
||||
outputPath = os.Args[3]
|
||||
)
|
||||
|
||||
switch driver {
|
||||
case DRIVER_SQLITE, DRIVER_POSTGRES:
|
||||
// OK
|
||||
case "sqlite":
|
||||
template = sqliteTemplate
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Unknown driver: %s", driver)
|
||||
usage()
|
||||
}
|
||||
|
||||
err := render(driver, defsPath, outputPath)
|
||||
err := render(template, defsPath, outputPath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v", err)
|
||||
os.Exit(1)
|
||||
|
@ -6,15 +6,15 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func parsePath(driver, filePath string) (*schema, error) {
|
||||
func parsePath(filePath string) (*schema, error) {
|
||||
fileBytes, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return parseBytes(driver, fileBytes)
|
||||
return parseBytes(fileBytes)
|
||||
}
|
||||
|
||||
func parseBytes(driver string, fileBytes []byte) (*schema, error) {
|
||||
func parseBytes(fileBytes []byte) (*schema, error) {
|
||||
s := string(fileBytes)
|
||||
for _, c := range []string{",", "(", ")", ";"} {
|
||||
s = strings.ReplaceAll(s, c, " "+c+" ")
|
||||
@ -29,7 +29,7 @@ func parseBytes(driver string, fileBytes []byte) (*schema, error) {
|
||||
for len(tokens) > 0 {
|
||||
switch tokens[0] {
|
||||
case "TABLE":
|
||||
tokens, err = parseTable(driver, schema, tokens)
|
||||
tokens, err = parseTable(schema, tokens)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -42,7 +42,7 @@ func parseBytes(driver string, fileBytes []byte) (*schema, error) {
|
||||
return schema, nil
|
||||
}
|
||||
|
||||
func parseTable(driver string, schema *schema, tokens []string) ([]string, error) {
|
||||
func parseTable(schema *schema, tokens []string) ([]string, error) {
|
||||
tokens = tokens[1:]
|
||||
if len(tokens) < 3 {
|
||||
return tokens, errors.New("incomplete table definition")
|
||||
@ -52,9 +52,8 @@ func parseTable(driver string, schema *schema, tokens []string) ([]string, error
|
||||
}
|
||||
|
||||
table := &table{
|
||||
driver: driver,
|
||||
Name: tokens[0],
|
||||
Type: tokens[2],
|
||||
Name: tokens[0],
|
||||
Type: tokens[2],
|
||||
}
|
||||
schema.Tables = append(schema.Tables, table)
|
||||
|
||||
|
@ -22,7 +22,7 @@ func TestParse(t *testing.T) {
|
||||
|
||||
for _, defPath := range paths {
|
||||
t.Run(filepath.Base(defPath), func(t *testing.T) {
|
||||
parsed, err := parsePath("", defPath)
|
||||
parsed, err := parsePath(defPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ type schema struct {
|
||||
}
|
||||
|
||||
type table struct {
|
||||
driver string //
|
||||
Name string // Name in SQL
|
||||
Type string // Go type
|
||||
NoInsert bool
|
||||
@ -30,19 +29,6 @@ type column struct {
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
func (t *table) translateQuery(in string) string {
|
||||
if t.driver == DRIVER_SQLITE {
|
||||
return in
|
||||
}
|
||||
|
||||
i := 1
|
||||
for strings.Contains(in, "?") {
|
||||
in = strings.Replace(in, "?", fmt.Sprintf("$%d", i), 1)
|
||||
i++
|
||||
}
|
||||
return in
|
||||
}
|
||||
|
||||
func (t *table) colSQLNames() []string {
|
||||
names := make([]string, len(t.Columns))
|
||||
for i := range names {
|
||||
@ -86,7 +72,7 @@ func (t *table) InsertQuery() string {
|
||||
b.WriteString(`?`)
|
||||
}
|
||||
b.WriteString(`)`)
|
||||
return t.translateQuery(b.String())
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (t *table) InsertArgs() string {
|
||||
@ -130,7 +116,7 @@ func (t *table) UpdateQuery() string {
|
||||
b.WriteString(` ` + c.SqlName + `=?`)
|
||||
}
|
||||
|
||||
return t.translateQuery(b.String())
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (t *table) UpdateArgs() string {
|
||||
@ -180,7 +166,7 @@ func (t *table) UpdateFullQuery() string {
|
||||
b.WriteString(` ` + c.SqlName + `=?`)
|
||||
}
|
||||
|
||||
return t.translateQuery(b.String())
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (t *table) UpdateFullArgs() string {
|
||||
@ -236,7 +222,7 @@ func (t *table) DeleteQuery() string {
|
||||
b.WriteString(col.SqlName)
|
||||
b.WriteString(`=?`)
|
||||
}
|
||||
return t.translateQuery(b.String())
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (t *table) DeleteArgs() string {
|
||||
@ -262,7 +248,7 @@ func (t *table) GetQuery() string {
|
||||
}
|
||||
b.WriteString(col.SqlName + `=?`)
|
||||
}
|
||||
return t.translateQuery(b.String())
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (t *table) ScanArgs() string {
|
||||
|
@ -48,29 +48,27 @@ func {{.Type}}_Insert(
|
||||
func {{.Type}}_Update(
|
||||
tx TX,
|
||||
row *{{.Type}},
|
||||
) (err error) {
|
||||
) (found bool, err error) {
|
||||
{{.Type}}_Sanitize(row)
|
||||
if err = {{.Type}}_Validate(row); err != nil {
|
||||
return err
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := tx.Exec("{{.UpdateQuery}}", {{.UpdateArgs}})
|
||||
if err != nil {
|
||||
return err
|
||||
return false, err
|
||||
}
|
||||
|
||||
n, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
switch n {
|
||||
case 0:
|
||||
return sql.ErrNoRows
|
||||
case 1:
|
||||
return nil
|
||||
default:
|
||||
|
||||
if n > 1 {
|
||||
panic("multiple rows updated")
|
||||
}
|
||||
|
||||
return n != 0, nil
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
@ -79,29 +77,27 @@ func {{.Type}}_Update(
|
||||
func {{.Type}}_UpdateFull(
|
||||
tx TX,
|
||||
row *{{.Type}},
|
||||
) (err error) {
|
||||
) (found bool, err error) {
|
||||
{{.Type}}_Sanitize(row)
|
||||
if err = {{.Type}}_Validate(row); err != nil {
|
||||
return err
|
||||
return false, err
|
||||
}
|
||||
|
||||
result, err := tx.Exec("{{.UpdateFullQuery}}", {{.UpdateFullArgs}})
|
||||
if err != nil {
|
||||
return err
|
||||
return false, err
|
||||
}
|
||||
|
||||
n, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
switch n {
|
||||
case 0:
|
||||
return sql.ErrNoRows
|
||||
case 1:
|
||||
return nil
|
||||
default:
|
||||
|
||||
if n > 1 {
|
||||
panic("multiple rows updated")
|
||||
}
|
||||
|
||||
return n != 0, nil
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
@ -113,24 +109,22 @@ func {{.Type}}_UpdateFull(
|
||||
func {{.Type}}_Delete(
|
||||
tx TX,
|
||||
{{.PKFunctionArgs -}}
|
||||
) (err error) {
|
||||
) (found bool, err error) {
|
||||
result, err := tx.Exec("{{.DeleteQuery}}", {{.DeleteArgs}})
|
||||
if err != nil {
|
||||
return err
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
n, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
switch n {
|
||||
case 0:
|
||||
return sql.ErrNoRows
|
||||
case 1:
|
||||
return nil
|
||||
default:
|
||||
|
||||
if n > 1 {
|
||||
panic("multiple rows deleted")
|
||||
}
|
||||
|
||||
return n != 0, nil
|
||||
}
|
||||
|
||||
{{- end}}
|
@ -8,16 +8,16 @@ import (
|
||||
"text/template"
|
||||
)
|
||||
|
||||
//go:embed gen.go.tmpl
|
||||
var fileTemplate string
|
||||
//go:embed sqlite.go.tmpl
|
||||
var sqliteTemplate string
|
||||
|
||||
func render(driver, schemaPath, outputPath string) error {
|
||||
sch, err := parsePath(driver, schemaPath)
|
||||
func render(templateStr, schemaPath, outputPath string) error {
|
||||
sch, err := parsePath(schemaPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tmpl := template.Must(template.New("").Parse(fileTemplate))
|
||||
tmpl := template.Must(template.New("").Parse(templateStr))
|
||||
fOut, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -2,7 +2,9 @@
|
||||
|
||||
## Transactions
|
||||
|
||||
Simplify transactions using `WithTx`.
|
||||
Simplify postgres transactions using `WithTx` for serializable transactions,
|
||||
or `WithTxDefault` for the default isolation level. Use the `SerialTxRunner`
|
||||
type to get automatic retries of serialization errors.
|
||||
|
||||
## Migrations
|
||||
|
||||
@ -18,3 +20,26 @@ func init() {
|
||||
Migrate(db, migrations) // Check the error, of course.
|
||||
}
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
In order to test this packge, we need to create a test user and database:
|
||||
|
||||
```
|
||||
sudo su postgres
|
||||
psql
|
||||
|
||||
CREATE DATABASE test;
|
||||
CREATE USER test WITH ENCRYPTED PASSWORD 'test';
|
||||
GRANT ALL PRIVILEGES ON DATABASE test TO test;
|
||||
|
||||
use test
|
||||
|
||||
GRANT ALL ON SCHEMA public TO test;
|
||||
```
|
||||
|
||||
Check that you can connect via the command line:
|
||||
|
||||
```
|
||||
psql -h 127.0.0.1 -U test --password test
|
||||
```
|
||||
|
@ -1,8 +0,0 @@
|
||||
package sqliteutil
|
||||
|
||||
import "github.com/mattn/go-sqlite3"
|
||||
|
||||
func ErrIsConstraint(err error) bool {
|
||||
e, ok := err.(sqlite3.Error)
|
||||
return ok && e.Code == 19
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package webutil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
var ErrUnsupportedType = errors.New("unsupported type")
|
||||
|
||||
type FormScanner struct {
|
||||
form url.Values
|
||||
err error
|
||||
}
|
||||
|
||||
func NewFormScanner(form url.Values) *FormScanner {
|
||||
return &FormScanner{form: form}
|
||||
}
|
||||
|
||||
func (s *FormScanner) Scan(name string, val any) *FormScanner {
|
||||
if s.err != nil {
|
||||
return s
|
||||
}
|
||||
|
||||
switch v := val.(type) {
|
||||
case *bool:
|
||||
*v = s.form.Has(name)
|
||||
default:
|
||||
if err := scan(s.form.Get(name), v); err != nil {
|
||||
s.err = fmt.Errorf("Error in field %s: %w", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *FormScanner) Error() error {
|
||||
return s.err
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package webutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type PathScanner struct {
|
||||
r *http.Request
|
||||
err error
|
||||
}
|
||||
|
||||
func NewPathScanner(r *http.Request) *PathScanner {
|
||||
return &PathScanner{r: r}
|
||||
}
|
||||
|
||||
func (s *PathScanner) Scan(name string, val any) *PathScanner {
|
||||
if s.err != nil {
|
||||
return s
|
||||
}
|
||||
|
||||
if err := scan(s.r.PathValue(name), val); err != nil {
|
||||
s.err = fmt.Errorf("Error in field %s: %w", name, err)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *PathScanner) Error() error {
|
||||
return s.err
|
||||
}
|
@ -1,85 +0,0 @@
|
||||
package webutil
|
||||
|
||||
import "strconv"
|
||||
|
||||
func scan(raw string, val any) error {
|
||||
switch v := val.(type) {
|
||||
case *string:
|
||||
*v = raw
|
||||
case *int:
|
||||
if i, err := strconv.ParseInt(raw, 10, 64); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*v = int(i)
|
||||
}
|
||||
case *int8:
|
||||
if i, err := strconv.ParseInt(raw, 10, 8); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*v = int8(i)
|
||||
}
|
||||
case *int16:
|
||||
if i, err := strconv.ParseInt(raw, 10, 16); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*v = int16(i)
|
||||
}
|
||||
case *int32:
|
||||
if i, err := strconv.ParseInt(raw, 10, 32); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*v = int32(i)
|
||||
}
|
||||
case *int64:
|
||||
if i, err := strconv.ParseInt(raw, 10, 64); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*v = int64(i)
|
||||
}
|
||||
case *uint:
|
||||
if i, err := strconv.ParseUint(raw, 10, 64); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*v = uint(i)
|
||||
}
|
||||
case *uint8:
|
||||
if i, err := strconv.ParseUint(raw, 10, 8); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*v = uint8(i)
|
||||
}
|
||||
case *uint16:
|
||||
if i, err := strconv.ParseUint(raw, 10, 16); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*v = uint16(i)
|
||||
}
|
||||
case *uint32:
|
||||
if i, err := strconv.ParseUint(raw, 10, 32); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*v = uint32(i)
|
||||
}
|
||||
case *uint64:
|
||||
if i, err := strconv.ParseUint(raw, 10, 64); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*v = uint64(i)
|
||||
}
|
||||
case *float32:
|
||||
if f, err := strconv.ParseFloat(raw, 32); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*v = float32(f)
|
||||
}
|
||||
case *float64:
|
||||
if f, err := strconv.ParseFloat(raw, 64); err != nil {
|
||||
return err
|
||||
} else {
|
||||
*v = float64(f)
|
||||
}
|
||||
default:
|
||||
return ErrUnsupportedType
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user