Files
ResolveSpec/pkg/security/lookup/database.go
T
Hein c9fa8c60f2 refactor(security): move all database access into pkg/security/lookup
pkg/security no longer contains SQL. Every provider calls a store interface
from lookup, implemented by a procedure backend (Postgres stored procedures,
the default there) and a direct backend (dialect-driven SQL for postgres,
sqlite, mysql and mssql with configurable table and column names).

- add sectypes, lookup, lookup/{dialect,procedure,direct,backends,ddl,conformance}
- split totp and providers sub packages out of the core package
- replace SQLNames/TableNames/QueryMode with lookup.Config (see breaking_changes.md)
- direct backend now covers column/row security and API-key login
- move txsettings SQL to lookup.ApplyTxSettings; remove password.go
- move schema scripts under lookup/, add reference DDL per dialect
- add a shared conformance suite; run it on sqlite, and on Postgres in a
  podman/docker container (RESOLVESPEC_TEST_CONTAINERS=1)
- fix procedure schema bugs found on real Postgres: duplicate p_data
  parameter, JSON null arrays, expires_at timezone casts, passkey list
  GROUP BY, missing resolvespec_passkey_login; accept zone-less timestamps
2026-10-01 13:19:44 +02:00

45 lines
1.4 KiB
Go

package lookup
import (
"database/sql"
"fmt"
"github.com/bitechdev/ResolveSpec/pkg/common"
"github.com/bitechdev/ResolveSpec/pkg/security/lookup/dialect"
)
// FromDatabase extracts the *sql.DB and the dialect name from an application's
// common.Database (bun, gorm or pgsql adapter), so callers do not have to dig the
// connection out or set Config.Dialect by hand. The returned name is the adapter's
// normalised DriverName ("postgres", "sqlite", "mssql", "mysql") and is empty when
// the adapter reports a driver the dialect registry does not know; set
// Config.Dialect explicitly in that case.
//
// Transaction adapters do not expose a *sql.DB and are rejected.
func FromDatabase(db common.Database) (*sql.DB, string, error) {
if db == nil {
return nil, "", fmt.Errorf("lookup: nil database")
}
p, ok := db.(common.SQLDBProvider)
if !ok {
return nil, "", fmt.Errorf("lookup: %T does not expose a *sql.DB (transaction adapter or unsupported adapter)", db)
}
sqlDB := p.SQLDB()
if sqlDB == nil {
return nil, "", fmt.Errorf("lookup: %T has no *sql.DB", db)
}
name := db.DriverName()
if _, err := dialect.Get(name); err != nil {
name = ""
}
return sqlDB, name, nil
}
// ResolveDialect returns the dialect for db: the configured one, or detected from the driver.
func (c Config) ResolveDialect(db *sql.DB) (dialect.Dialect, error) {
if c.Dialect != "" {
return dialect.Get(c.Dialect)
}
return dialect.Detect(db)
}