Skip to content

Commit 862e496

Browse files
authored
Merge pull request #1721 from breml/generate-database-abstract-dbconn
generate-database: Abstract db connection / db transaction
2 parents 70c9316 + c8f5962 commit 862e496

36 files changed

+535
-552
lines changed

cmd/generate-database/db/constants.go

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package db
44

55
// Imports is a list of the package imports every generated source file has.
66
var Imports = []string{
7+
"context",
78
"database/sql",
89
"fmt",
10+
"strings",
911
}

cmd/generate-database/db/method.go

+45-45
Large diffs are not rendered by default.

cmd/generate-database/file/boilerplate/boilerplate.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import (
77
"fmt"
88
)
99

10+
type dbtx interface {
11+
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
12+
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
13+
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
14+
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
15+
}
16+
1017
// RegisterStmt register a SQL statement.
1118
//
1219
// Registered statements will be prepared upfront and re-used, to speed up
@@ -42,13 +49,18 @@ var stmts = map[int]string{} // Statement code to statement SQL text.
4249
var PreparedStmts = map[int]*sql.Stmt{}
4350

4451
// Stmt prepares the in-memory prepared statement for the transaction.
45-
func Stmt(tx *sql.Tx, code int) (*sql.Stmt, error) {
52+
func Stmt(db dbtx, code int) (*sql.Stmt, error) {
4653
stmt, ok := PreparedStmts[code]
4754
if !ok {
4855
return nil, fmt.Errorf("No prepared statement registered with code %d", code)
4956
}
5057

51-
return tx.Stmt(stmt), nil
58+
tx, ok := db.(*sql.Tx)
59+
if ok {
60+
return tx.Stmt(stmt), nil
61+
}
62+
63+
return stmt, nil
5264
}
5365

5466
// StmtString returns the in-memory query string with the given code.
@@ -138,8 +150,8 @@ func selectObjects(ctx context.Context, stmt *sql.Stmt, rowFunc dest, args ...an
138150

139151
// scan runs a query with inArgs and provides the rowFunc with the scan function for each row.
140152
// It handles closing the rows and errors from the result set.
141-
func scan(ctx context.Context, tx *sql.Tx, sqlStmt string, rowFunc dest, inArgs ...any) error {
142-
rows, err := tx.QueryContext(ctx, sqlStmt, inArgs...)
153+
func scan(ctx context.Context, db dbtx, sqlStmt string, rowFunc dest, inArgs ...any) error {
154+
rows, err := db.QueryContext(ctx, sqlStmt, inArgs...)
143155
if err != nil {
144156
return err
145157
}

internal/server/db/cluster/certificate_projects.interface.mapper.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@
22

33
package cluster
44

5-
import (
6-
"context"
7-
"database/sql"
8-
)
5+
import "context"
96

107
// CertificateProjectGenerated is an interface of generated methods for CertificateProject.
118
type CertificateProjectGenerated interface {
129
// GetCertificateProjects returns all available Projects for the Certificate.
1310
// generator: certificate_project GetMany
14-
GetCertificateProjects(ctx context.Context, tx *sql.Tx, certificateID int) ([]Project, error)
11+
GetCertificateProjects(ctx context.Context, db dbtx, certificateID int) ([]Project, error)
1512

1613
// DeleteCertificateProjects deletes the certificate_project matching the given key parameters.
1714
// generator: certificate_project DeleteMany
18-
DeleteCertificateProjects(ctx context.Context, tx *sql.Tx, certificateID int) error
15+
DeleteCertificateProjects(ctx context.Context, db dbtx, certificateID int) error
1916

2017
// CreateCertificateProjects adds a new certificate_project to the database.
2118
// generator: certificate_project Create
22-
CreateCertificateProjects(ctx context.Context, tx *sql.Tx, objects []CertificateProject) error
19+
CreateCertificateProjects(ctx context.Context, db dbtx, objects []CertificateProject) error
2320

2421
// UpdateCertificateProjects updates the certificate_project matching the given key parameters.
2522
// generator: certificate_project Update
26-
UpdateCertificateProjects(ctx context.Context, tx *sql.Tx, certificateID int, projectNames []string) error
23+
UpdateCertificateProjects(ctx context.Context, db dbtx, certificateID int, projectNames []string) error
2724
}

internal/server/db/cluster/certificate_projects.mapper.go

+13-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/server/db/cluster/certificates.interface.mapper.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package cluster
44

55
import (
66
"context"
7-
"database/sql"
87

98
"github.com/lxc/incus/v6/internal/server/certificate"
109
)
@@ -13,33 +12,33 @@ import (
1312
type CertificateGenerated interface {
1413
// GetCertificates returns all available certificates.
1514
// generator: certificate GetMany
16-
GetCertificates(ctx context.Context, tx *sql.Tx, filters ...CertificateFilter) ([]Certificate, error)
15+
GetCertificates(ctx context.Context, db dbtx, filters ...CertificateFilter) ([]Certificate, error)
1716

1817
// GetCertificate returns the certificate with the given key.
1918
// generator: certificate GetOne
20-
GetCertificate(ctx context.Context, tx *sql.Tx, fingerprint string) (*Certificate, error)
19+
GetCertificate(ctx context.Context, db dbtx, fingerprint string) (*Certificate, error)
2120

2221
// GetCertificateID return the ID of the certificate with the given key.
2322
// generator: certificate ID
24-
GetCertificateID(ctx context.Context, tx *sql.Tx, fingerprint string) (int64, error)
23+
GetCertificateID(ctx context.Context, db dbtx, fingerprint string) (int64, error)
2524

2625
// CertificateExists checks if a certificate with the given key exists.
2726
// generator: certificate Exists
28-
CertificateExists(ctx context.Context, tx *sql.Tx, fingerprint string) (bool, error)
27+
CertificateExists(ctx context.Context, db dbtx, fingerprint string) (bool, error)
2928

3029
// CreateCertificate adds a new certificate to the database.
3130
// generator: certificate Create
32-
CreateCertificate(ctx context.Context, tx *sql.Tx, object Certificate) (int64, error)
31+
CreateCertificate(ctx context.Context, db dbtx, object Certificate) (int64, error)
3332

3433
// DeleteCertificate deletes the certificate matching the given key parameters.
3534
// generator: certificate DeleteOne-by-Fingerprint
36-
DeleteCertificate(ctx context.Context, tx *sql.Tx, fingerprint string) error
35+
DeleteCertificate(ctx context.Context, db dbtx, fingerprint string) error
3736

3837
// DeleteCertificates deletes the certificate matching the given key parameters.
3938
// generator: certificate DeleteMany-by-Name-and-Type
40-
DeleteCertificates(ctx context.Context, tx *sql.Tx, name string, certificateType certificate.Type) error
39+
DeleteCertificates(ctx context.Context, db dbtx, name string, certificateType certificate.Type) error
4140

4241
// UpdateCertificate updates the certificate matching the given key parameters.
4342
// generator: certificate Update
44-
UpdateCertificate(ctx context.Context, tx *sql.Tx, fingerprint string, object Certificate) error
43+
UpdateCertificate(ctx context.Context, db dbtx, fingerprint string, object Certificate) error
4544
}

0 commit comments

Comments
 (0)