-
Notifications
You must be signed in to change notification settings - Fork 1
/
if-db.go
74 lines (67 loc) · 2.8 KB
/
if-db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"context"
"database/sql"
)
const NoPartition DBPartition = ""
// DB is a parallel database connection
// - DB applies to any database implementation
// - psql provides implementation with caching of:
// - — DB objects and
// - — prepared statements
// - DB is obtained via new function like [DBFactory.NewDB].
// Such returned DB can use:
// - — its data-source namer to handle partitioning
// - — delegation to its underlying possibly partitioned DB implementation
// - — caching of DB implementation-objects and prepared statements
// - — its schema function to bootstrap and migrate databases
type DB interface {
// Exec executes a query not returning any rows
// - ExecResult contains last inserted ID if any and rows affected
Exec(partition DBPartition, query string, ctx context.Context,
args ...any) (execResult ExecResult, err error)
// Query executes a query returning zero or more rows
Query(partition DBPartition, query string, ctx context.Context,
args ...any) (sqlRows *sql.Rows, err error)
// Query executes a query known to return exactly one row
// - zero rows returns error: sql: no rows in result set
QueryRow(partition DBPartition, query string, ctx context.Context,
args ...any) (sqlRow *sql.Row, err error)
// Query executes a query known to return exactly one row and first column a string value
QueryString(partition DBPartition, query string, ctx context.Context,
args ...any) (value string, err error)
// Query executes a query known to return exactly one row and first column an int value
QueryInt(partition DBPartition, query string, ctx context.Context,
args ...any) (value int, err error)
// Close closes the database connection
Close() (err error)
}
// ExecResult is the result from [DB.Exec], a query not returning rows
type ExecResult interface {
// - ID is last inserted ID if any
// - rows is number of rows affected
Get() (ID int64, rows int64)
// “sql.Result: ID afe3… rows: 123”
String() (s string)
}
// DBPartition is partition reference for a partitioned database
// - partition is typically one table per year
// - DBPartition applies to any database implementation
type DBPartition string
// DBFactory is a standardized way to obtain DB objects
// - DBFactory applies to any database implementation
type DBFactory interface {
// NewDB returns a DB object implementation.
// The schema function executes application-specific SQL initialization for
// a new datasource
// - executes CREATE of tables and indexes
// - configures database-specific referential integrity and journaling
NewDB(
dsnr DataSourceNamer,
schema func(dataSource DataSource, ctx context.Context) (err error),
) (db DB)
}