Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ The module's options (`database`, `timeout`, `busy_timeout`, `foreign_keys`,
environment variables (`SQLITE_*`) or per-option `get_<key>` / `set_<key>`
accessor builtins, and serve as defaults for `connect` / `connect_remote`. Two
opt-in host levers (`max_rows` and `NewModuleWithFileAccess`) bound what an
untrusted script can reach; both default to off. See the
untrusted script can reach; both default to off. `timeout` bounds each database
operation (a per-query deadline, and a cancellation point tied to the script
thread) — this is what stops an unreachable remote from hanging the host. The
connection PRAGMAs (`foreign_keys`, `journal_mode`, …) are applied to every
pooled connection, and an in-memory database is served from a single connection
so its schema and data persist across queries. See the
[Configuration section of docs/API.md](docs/API.md#configuration) for the full
option table, defaults, accessors, and host-hardening details.

Expand Down
91 changes: 72 additions & 19 deletions database.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package sqlite

import (
"context"
"database/sql"
"fmt"
"time"

"github.com/1set/starlet/dataconv"
"github.com/starpkg/base/util"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)
Expand All @@ -19,14 +22,23 @@ type database struct {
db *sql.DB
maxRows int
restrictFileAccess bool
opTimeout time.Duration // per-operation deadline (0 = none)
}

// opContext derives the context bounding a single operation from the calling
// script thread (so a cancelled thread aborts the query) plus the configured
// per-operation timeout. The caller must invoke the returned cancel func.
func (db *database) opContext(thread *starlark.Thread) (context.Context, context.CancelFunc) {
return util.OpContext(thread, db.opTimeout)
}

// newDatabaseInstance creates a new Starlark database instance.
func newDatabaseInstance(db *sql.DB, maxRows int, restrictFileAccess bool) *starlarkstruct.Module {
func newDatabaseInstance(db *sql.DB, maxRows int, restrictFileAccess bool, opTimeout time.Duration) *starlarkstruct.Module {
dbi := &database{
db: db,
maxRows: maxRows,
restrictFileAccess: restrictFileAccess,
opTimeout: opTimeout,
}

// Create dictionary of methods
Expand Down Expand Up @@ -108,7 +120,9 @@ func (db *database) execute(thread *starlark.Thread, fn *starlark.Builtin, args
}

// Execute the query
result, err := db.db.Exec(sqlQuery.query, sqlQuery.params...)
ctx, cancel := db.opContext(thread)
defer cancel()
result, err := db.db.ExecContext(ctx, sqlQuery.query, sqlQuery.params...)
if err != nil {
return nil, fmt.Errorf("failed to execute query: %w", err)
}
Expand All @@ -132,7 +146,9 @@ func (db *database) batch(thread *starlark.Thread, fn *starlark.Builtin, args st
}

// Begin transaction
tx, err := db.db.Begin()
ctx, cancel := db.opContext(thread)
defer cancel()
tx, err := db.db.BeginTx(ctx, nil)
if err != nil {
return nil, fmt.Errorf("failed to begin transaction: %w", err)
}
Expand Down Expand Up @@ -205,7 +221,7 @@ func (db *database) batch(thread *starlark.Thread, fn *starlark.Builtin, args st
}

// Execute the query within transaction
result, err := tx.Exec(sqlQuery.query, sqlQuery.params...)
result, err := tx.ExecContext(ctx, sqlQuery.query, sqlQuery.params...)
if err != nil {
tx.Rollback()
return nil, fmt.Errorf("query %d: failed to execute: %w", i, err)
Expand Down Expand Up @@ -255,7 +271,9 @@ func (db *database) query(thread *starlark.Thread, fn *starlark.Builtin, args st
}

// Execute the query
rows, err := db.db.Query(sqlQuery.query, sqlQuery.params...)
ctx, cancel := db.opContext(thread)
defer cancel()
rows, err := db.db.QueryContext(ctx, sqlQuery.query, sqlQuery.params...)
if err != nil {
return nil, fmt.Errorf("failed to execute query: %w", err)
}
Expand All @@ -282,7 +300,9 @@ func (db *database) queryOne(thread *starlark.Thread, fn *starlark.Builtin, args
}

// Execute the query
rows, err := db.db.Query(sqlQuery.query, sqlQuery.params...)
ctx, cancel := db.opContext(thread)
defer cancel()
rows, err := db.db.QueryContext(ctx, sqlQuery.query, sqlQuery.params...)
if err != nil {
return nil, fmt.Errorf("failed to execute query: %w", err)
}
Expand All @@ -297,14 +317,31 @@ func (db *database) begin(thread *starlark.Thread, fn *starlark.Builtin, args st
return nil, err
}

// Begin transaction
tx, err := db.db.Begin()
// Acquire the connection under a DEADLINE (the per-operation timeout), so that
// acquiring it can't block the host indefinitely when the pool is busy — e.g.
// a single-connection in-memory database whose sole connection is held by
// another open transaction.
acquireCtx, acquireCancel := db.opContext(thread)
defer acquireCancel()
conn, err := db.db.Conn(acquireCtx)
if err != nil {
return nil, fmt.Errorf("failed to begin transaction: %w", err)
}
// The transaction spans multiple later calls (execute → commit), so its
// context must live until commit/rollback and must NOT carry the per-operation
// deadline (which would abort a legitimately long transaction). Cancellation-
// only still propagates a cancelled script thread; the cancel func and the
// dedicated connection are handed to the transaction and released on finish.
lifeCtx, lifeCancel := util.OpContext(thread, 0)
tx, err := conn.BeginTx(lifeCtx, nil)
if err != nil {
lifeCancel()
conn.Close()
return nil, fmt.Errorf("failed to begin transaction: %w", err)
}

// Create transaction object
return newTransactionInstance(tx, db.maxRows), nil
return newTransactionInstance(tx, db.maxRows, db.opTimeout, conn, lifeCancel), nil
}

// attach attaches another database with an alias.
Expand All @@ -324,7 +361,9 @@ func (db *database) attach(thread *starlark.Thread, fn *starlark.Builtin, args s

// Execute ATTACH DATABASE statement
query := fmt.Sprintf("ATTACH DATABASE ? AS %s", quoteName(alias))
_, err := db.db.Exec(query, database)
ctx, cancel := db.opContext(thread)
defer cancel()
_, err := db.db.ExecContext(ctx, query, database)
if err != nil {
return nil, fmt.Errorf("failed to attach database: %w", err)
}
Expand All @@ -343,7 +382,9 @@ func (db *database) detach(thread *starlark.Thread, fn *starlark.Builtin, args s

// Execute DETACH DATABASE statement
query := fmt.Sprintf("DETACH DATABASE %s", quoteName(alias))
_, err := db.db.Exec(query)
ctx, cancel := db.opContext(thread)
defer cancel()
_, err := db.db.ExecContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("failed to detach database: %w", err)
}
Expand All @@ -358,7 +399,9 @@ func (db *database) tables(thread *starlark.Thread, fn *starlark.Builtin, args s
}

// Query tables
rows, err := db.db.Query("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
ctx, cancel := db.opContext(thread)
defer cancel()
rows, err := db.db.QueryContext(ctx, "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
if err != nil {
return nil, fmt.Errorf("failed to query tables: %w", err)
}
Expand Down Expand Up @@ -394,7 +437,9 @@ func (db *database) tableInfo(thread *starlark.Thread, fn *starlark.Builtin, arg
}

// Query table info
rows, err := db.db.Query(fmt.Sprintf("PRAGMA table_info(%s)", quoteName(table)))
ctx, cancel := db.opContext(thread)
defer cancel()
rows, err := db.db.QueryContext(ctx, fmt.Sprintf("PRAGMA table_info(%s)", quoteName(table)))
if err != nil {
return nil, fmt.Errorf("failed to query table info: %w", err)
}
Expand Down Expand Up @@ -459,7 +504,9 @@ func (db *database) indices(thread *starlark.Thread, fn *starlark.Builtin, args
}

// Query indices
rows, err := db.db.Query("SELECT name, sql FROM sqlite_master WHERE type='index' AND tbl_name=?", table)
ctx, cancel := db.opContext(thread)
defer cancel()
rows, err := db.db.QueryContext(ctx, "SELECT name, sql FROM sqlite_master WHERE type='index' AND tbl_name=?", table)
if err != nil {
return nil, fmt.Errorf("failed to query indices: %w", err)
}
Expand Down Expand Up @@ -504,7 +551,9 @@ func (db *database) tableExists(thread *starlark.Thread, fn *starlark.Builtin, a

// Query to check if table exists
var count int
err := db.db.QueryRow("SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?", table).Scan(&count)
ctx, cancel := db.opContext(thread)
defer cancel()
err := db.db.QueryRowContext(ctx, "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?", table).Scan(&count)
if err != nil {
return nil, fmt.Errorf("failed to check if table exists: %w", err)
}
Expand All @@ -522,13 +571,15 @@ func (db *database) prepare(thread *starlark.Thread, fn *starlark.Builtin, args
}

// Prepare statement
stmt, err := db.db.Prepare(query)
ctx, cancel := db.opContext(thread)
defer cancel()
stmt, err := db.db.PrepareContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("failed to prepare statement: %w", err)
}

// Create prepared statement object
return newPreparedStatementInstance(stmt, db.maxRows), nil
return newPreparedStatementInstance(stmt, db.maxRows, db.opTimeout), nil
}

// prepareQuery prepares a SQL query statement.
Expand All @@ -541,11 +592,13 @@ func (db *database) prepareQuery(thread *starlark.Thread, fn *starlark.Builtin,
}

// Prepare statement
stmt, err := db.db.Prepare(query)
ctx, cancel := db.opContext(thread)
defer cancel()
stmt, err := db.db.PrepareContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("failed to prepare query: %w", err)
}

// Create prepared query object
return newPreparedQueryInstance(stmt, db.maxRows), nil
return newPreparedQueryInstance(stmt, db.maxRows, db.opTimeout), nil
}
4 changes: 2 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ parameter omitted falls back to the corresponding module config option.
**Parameters:**

- `database` (string): Database path or `:memory:` for in-memory (default: module config)
- `timeout` (float): Connection timeout in seconds (default: module config)
- `timeout` (float): Per-operation deadline in seconds — each query/statement is bounded by it and cancels with the script thread. Omit or pass 0 to use the module default; set the module `timeout` config (or `SQLITE_TIMEOUT`) to 0 to disable the deadline.
- `busy_timeout` (float): Busy timeout in seconds (default: module config)
- `foreign_keys` (bool): Enable foreign key constraints (default: module config)
- `journal_mode` (string): Journal mode (default: module config)
Expand Down Expand Up @@ -998,7 +998,7 @@ accessor — never a getter — but this module has none.)
| Option | Getter | Setter | Type | Env var | Default | Description |
|--------|--------|--------|------|---------|---------|-------------|
| `database` | `get_database` | `set_database` | string | `SQLITE_DATABASE` | `:memory:` | Path to the SQLite database (use `:memory:` for in-memory) |
| `timeout` | `get_timeout` | `set_timeout` | float | `SQLITE_TIMEOUT` | `30.0` | Connection timeout in seconds |
| `timeout` | `get_timeout` | `set_timeout` | float | `SQLITE_TIMEOUT` | `30.0` | Per-operation deadline in seconds (per-query; cancels with the script thread; 0 = none) |
| `busy_timeout` | `get_busy_timeout` | `set_busy_timeout` | float | `SQLITE_BUSY_TIMEOUT` | `5.0` | Busy timeout in seconds |
| `foreign_keys` | `get_foreign_keys` | `set_foreign_keys` | bool | `SQLITE_FOREIGN_KEYS` | `true` | Enable foreign key constraints |
| `journal_mode` | `get_journal_mode` | `set_journal_mode` | string | `SQLITE_JOURNAL_MODE` | `DELETE` | Journal mode (WAL, DELETE, TRUNCATE, PERSIST, MEMORY, OFF) |
Expand Down
Loading
Loading