Documentation
¶
Overview ¶
Package sqlhelpers contains driver-agnostic helpers shared across SQL integrations (nrmysql, nrpq, ...). Anything tied to a specific driver string belongs in the driver integration; anything that operates on the database/sql shape belongs here.
Index ¶
- Constants
- func DetectSQLExecutionCall(stmt dst.Stmt, dbName string) string
- func DetectSQLOpen(stmt dst.Stmt) (varName string, driverArg *dst.BasicLit)
- func FindLastUsageOfExecutionResult(stmts []dst.Stmt, varName string, startIndex int) int
- func ReplaceSQLMethodWithContext(stmt dst.Stmt, ctxName string)
Constants ¶
const SQLImportPath = "database/sql"
SQLImportPath is the standard library import path for database/sql.
Variables ¶
This section is empty.
Functions ¶
func DetectSQLExecutionCall ¶
DetectSQLExecutionCall reports whether stmt is a SQL execution call on dbName. Returns the method name ("QueryRow", "Query", or "Exec") if found, otherwise "".
Example: row := db.QueryRow("SELECT count(*) from tables")
func DetectSQLOpen ¶
DetectSQLOpen recognizes `varName, err := sql.Open(<driver>, <connStr>)` and returns the LHS variable name plus a pointer to the driver-name BasicLit. Callers can mutate the returned BasicLit's Value to swap drivers in place (e.g. `"postgres"` -> `"nrpq"`). If the statement is not a sql.Open call the returned varName is "" and driverArg is nil.
A varName of "_" (blank identifier) is returned as-is — the open is still a real sql.Open call worth swapping the driver on, but downstream callers should treat "_" as "no usable handle" when looking for execution calls on the DB.
func FindLastUsageOfExecutionResult ¶
FindLastUsageOfExecutionResult scans stmts after startIndex for the last statement that references varName, and returns that index. Returns startIndex if varName is never used after startIndex, or if varName is empty / the blank identifier (in which case there is no handle to follow).
func ReplaceSQLMethodWithContext ¶
ReplaceSQLMethodWithContext rewrites a SQL execution call to its context-aware variant and prepends ctxName as the first argument:
row := db.QueryRow(...) -> row := db.QueryRowContext(ctx, ...) rows, _ := db.Query(...) -> rows, _ := db.QueryContext(ctx, ...) res, _ := db.Exec(...) -> res, _ := db.ExecContext(ctx, ...)
No-op if stmt is not a recognized SQL execution call.
Types ¶
This section is empty.