-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.go
More file actions
49 lines (40 loc) · 1.39 KB
/
adapter.go
File metadata and controls
49 lines (40 loc) · 1.39 KB
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
package sqlite
import (
"database/sql"
"github.com/tinywasm/orm"
"github.com/tinywasm/fmt"
_ "modernc.org/sqlite" // SQLite driver
)
// Open creates a new sqlite connection and wraps it in an orm.DB.
func Open(dsn string) (*orm.DB, error) {
db, err := sql.Open("sqlite", dsn)
if err != nil {
return nil, fmt.Errf("failed to open sqlite database: %v", err)
}
if err := db.Ping(); err != nil {
return nil, fmt.Errf("failed to ping sqlite database: %v", err)
}
// SQLite does not support concurrent writers. In-memory databases (:memory:)
// are per-connection — each new connection sees an empty database. Limiting
// to a single connection prevents both "database is locked" and
// "no such table" errors when multiple goroutines share the same orm.DB.
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
exec := &sqliteExecutor{db: db}
compiler := sqliteCompiler{}
return orm.New(exec, compiler), nil
}
// Close closes the database connection associated with the orm.DB.
func Close(db *orm.DB) error {
if db == nil || db.RawExecutor() == nil {
return fmt.Err("database instance or executor is nil")
}
return db.Close()
}
// ExecSQL executes raw SQL. Useful for testing or migrations.
func ExecSQL(db *orm.DB, query string, args ...any) error {
if db == nil || db.RawExecutor() == nil {
return fmt.Err("database instance or executor is nil")
}
return db.RawExecutor().Exec(query, args...)
}