@@ -74,12 +74,14 @@ func GetDB(ctx context.Context) (*sql.DB, error) {
7474 return db , nil
7575}
7676
77+ // setDB stores the shared provisioning connection under write lock.
7778func setDB (db * sql.DB ) {
7879 dbMutex .Lock ()
7980 defer dbMutex .Unlock ()
8081 dbConn = db
8182}
8283
84+ // pingWithTimeout validates liveness by issuing a ping bounded by d.
8385func pingWithTimeout (ctx context.Context , db * sql.DB , d time.Duration ) error {
8486 c , cancel := context .WithTimeout (ctx , d )
8587 defer cancel ()
@@ -93,6 +95,7 @@ func pingWithTimeout(ctx context.Context, db *sql.DB, d time.Duration) error {
9395// Allow only safe characters in generated identifiers (you can tighten/loosen this).
9496var identRe = regexp .MustCompile (`^[a-z0-9\-_.]+$` )
9597
98+ // quoteIdent wraps an identifier in backticks after validating its characters.
9699func quoteIdent (s string ) (string , error ) {
97100 if s == "" {
98101 return "" , errors .New ("empty identifier" )
@@ -104,6 +107,7 @@ func quoteIdent(s string) (string, error) {
104107 return "`" + strings .ReplaceAll (s , "`" , "``" ) + "`" , nil
105108}
106109
110+ // quoteString escapes and quotes a string literal for SQL statements.
107111func quoteString (s string ) (string , error ) {
108112 if strings .ContainsRune (s , '\x00' ) {
109113 return "" , errors .New ("string contains NUL" )
@@ -112,6 +116,7 @@ func quoteString(s string) (string, error) {
112116 return "'" + strings .ReplaceAll (s , "'" , "''" ) + "'" , nil
113117}
114118
119+ // quoteAccount formats a user@host identifier using SQL quoting rules.
115120func quoteAccount (host , user string ) (string , error ) {
116121 u , err := quoteString (user )
117122 if err != nil {
@@ -124,7 +129,7 @@ func quoteAccount(host, user string) (string, error) {
124129 return u + "@" + h , nil
125130}
126131
127- // Exec with a timeout .
132+ // execTimeout executes stmt with a deadline by wrapping the call in a cancelable context .
128133func execTimeout (ctx context.Context , db * sql.DB , d time.Duration , stmt string ) error {
129134 c , cancel := context .WithTimeout (ctx , d )
130135 defer cancel ()
0 commit comments