Skip to content

Commit 804d64c

Browse files
committed
refactor: define CatalogDB and CatalogTx interfaces with SQLite implementation
1 parent e250af0 commit 804d64c

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

mdl/catalog/catalogdb.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package catalog
4+
5+
import "database/sql"
6+
7+
// CatalogDB abstracts the database layer used by Catalog and LintContext.
8+
// The primary implementation wraps modernc.org/sqlite for native builds;
9+
// a js/wasm build will supply a host-backed implementation.
10+
type CatalogDB interface {
11+
Query(query string, args ...any) (*sql.Rows, error)
12+
QueryRow(query string, args ...any) *sql.Row
13+
Exec(query string, args ...any) (sql.Result, error)
14+
Begin() (CatalogTx, error)
15+
Close() error
16+
}
17+
18+
// CatalogTx abstracts a database transaction. Builder uses this for bulk
19+
// inserts with prepared statements.
20+
type CatalogTx interface {
21+
Prepare(query string) (*sql.Stmt, error)
22+
Exec(query string, args ...any) (sql.Result, error)
23+
QueryRow(query string, args ...any) *sql.Row
24+
Commit() error
25+
Rollback() error
26+
}

mdl/catalog/catalogdb_sqlite.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
//go:build !js
4+
5+
package catalog
6+
7+
import (
8+
"database/sql"
9+
10+
_ "modernc.org/sqlite"
11+
)
12+
13+
// SqliteCatalogDB wraps *sql.DB from modernc.org/sqlite.
14+
type SqliteCatalogDB struct {
15+
db *sql.DB
16+
}
17+
18+
// NewSqliteCatalogDB opens an in-memory SQLite database.
19+
func NewSqliteCatalogDB() (*SqliteCatalogDB, error) {
20+
db, err := sql.Open("sqlite", ":memory:")
21+
if err != nil {
22+
return nil, err
23+
}
24+
if _, err := db.Exec("PRAGMA foreign_keys = ON"); err != nil {
25+
db.Close()
26+
return nil, err
27+
}
28+
return &SqliteCatalogDB{db: db}, nil
29+
}
30+
31+
// NewSqliteCatalogDBFromFile opens a file-based SQLite database.
32+
func NewSqliteCatalogDBFromFile(path string) (*SqliteCatalogDB, error) {
33+
db, err := sql.Open("sqlite", path)
34+
if err != nil {
35+
return nil, err
36+
}
37+
if _, err := db.Exec("PRAGMA foreign_keys = ON"); err != nil {
38+
db.Close()
39+
return nil, err
40+
}
41+
return &SqliteCatalogDB{db: db}, nil
42+
}
43+
44+
func (s *SqliteCatalogDB) Query(query string, args ...any) (*sql.Rows, error) {
45+
return s.db.Query(query, args...)
46+
}
47+
48+
func (s *SqliteCatalogDB) QueryRow(query string, args ...any) *sql.Row {
49+
return s.db.QueryRow(query, args...)
50+
}
51+
52+
func (s *SqliteCatalogDB) Exec(query string, args ...any) (sql.Result, error) {
53+
return s.db.Exec(query, args...)
54+
}
55+
56+
func (s *SqliteCatalogDB) Begin() (CatalogTx, error) {
57+
return s.db.Begin()
58+
}
59+
60+
func (s *SqliteCatalogDB) Close() error {
61+
return s.db.Close()
62+
}
63+
64+
// RawDB returns the underlying *sql.DB. Used only for SQLite-specific
65+
// operations like VACUUM INTO in SaveToFile.
66+
func (s *SqliteCatalogDB) RawDB() *sql.DB {
67+
return s.db
68+
}

0 commit comments

Comments
 (0)