New creates a new Stmt object for executing SQL queries. It caches the Stmt object for future use and handles preparation and error states. Parameters: ctx: Context for the request, used to carry deadlines, cancellation signals, etc. key: The key representing the SQL query, used for caching and p
(ctx context.Context, key string, isTransaction bool, conn ConnPool, locker sync.Locker)
| 156 | // *Stmt: A newly created statement object for executing SQL operations. |
| 157 | // error: An error if the statement preparation fails. |
| 158 | func (s *lruStore) New(ctx context.Context, key string, isTransaction bool, conn ConnPool, locker sync.Locker) (_ *Stmt, err error) { |
| 159 | // Create a Stmt object and set its Transaction property. |
| 160 | // The prepared channel is used to synchronize the statement preparation state. |
| 161 | cacheStmt := &Stmt{ |
| 162 | Transaction: isTransaction, |
| 163 | prepared: make(chan struct{}), |
| 164 | } |
| 165 | // Cache the Stmt object with the associated key. |
| 166 | s.Set(key, cacheStmt) |
| 167 | // Unlock after completing initialization to prevent deadlocks. |
| 168 | locker.Unlock() |
| 169 | |
| 170 | // Ensure the prepared channel is closed after the function execution completes. |
| 171 | defer close(cacheStmt.prepared) |
| 172 | |
| 173 | // Prepare the SQL statement using the provided connection. |
| 174 | cacheStmt.Stmt, err = conn.PrepareContext(ctx, key) |
| 175 | if err != nil { |
| 176 | // If statement preparation fails, record the error and remove the invalid Stmt object from the cache. |
| 177 | cacheStmt.prepareErr = err |
| 178 | s.Delete(key) |
| 179 | return &Stmt{}, err |
| 180 | } |
| 181 | |
| 182 | // Return the successfully prepared Stmt object. |
| 183 | return cacheStmt, nil |
| 184 | } |
nothing calls this directly
no test coverage detected