Push the rest

This commit is contained in:
2026-05-11 10:58:46 +02:00
parent adb5c1a439
commit 0031caf16c
94 changed files with 11777 additions and 3474 deletions

View File

@@ -0,0 +1,41 @@
package db
import (
"context"
"database/sql"
"fmt"
"log/slog"
_ "github.com/microsoft/go-mssqldb"
)
// Open creates an MSSQL connection pool and validates connectivity.
func Open(ctx context.Context, dsn, name string) (*sql.DB, error) {
pool, err := sql.Open("sqlserver", dsn)
if err != nil {
return nil, fmt.Errorf("open %s: %w", name, err)
}
pool.SetMaxOpenConns(15)
pool.SetMaxIdleConns(5)
if err := pool.PingContext(ctx); err != nil {
return nil, fmt.Errorf("ping %s: %w", name, err)
}
slog.Info("mssql connected", "db", name)
return pool, nil
}
// QueryFirst runs each SQL query in order, returning rows from the first one
// that succeeds. Used for schema-fallback queries.
func QueryFirst(ctx context.Context, pool *sql.DB, queries []string) (*sql.Rows, error) {
var lastErr error
for _, q := range queries {
rows, err := pool.QueryContext(ctx, q)
if err != nil {
slog.Warn("query variant failed, trying next", "err", err)
lastErr = err
continue
}
return rows, nil
}
return nil, fmt.Errorf("all query variants failed: %w", lastErr)
}