sqlhelpers

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

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

View Source
const SQLImportPath = "database/sql"

SQLImportPath is the standard library import path for database/sql.

Variables

This section is empty.

Functions

func DetectSQLExecutionCall

func DetectSQLExecutionCall(stmt dst.Stmt, dbName string) string

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

func DetectSQLOpen(stmt dst.Stmt) (varName string, driverArg *dst.BasicLit)

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

func FindLastUsageOfExecutionResult(stmts []dst.Stmt, varName string, startIndex int) int

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

func ReplaceSQLMethodWithContext(stmt dst.Stmt, ctxName string)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址