fetchTransactionPrecisionFromDB is a placeholder for fetching precision from the database. In a real application, this would query your database. It returns the precision, a boolean indicating if found, and an error.
(db *sql.DB, transactionID string)
| 261 | // In a real application, this would query your database. |
| 262 | // It returns the precision, a boolean indicating if found, and an error. |
| 263 | func fetchTransactionPrecisionFromDB(db *sql.DB, transactionID string) (float64, bool, error) { |
| 264 | // Check cache first |
| 265 | if precision, found := precisionCache[transactionID]; found { |
| 266 | logrus.WithFields(logrus.Fields{ |
| 267 | "transaction_id": transactionID, |
| 268 | "precision": precision, |
| 269 | }).Debug("cache hit for transaction precision") |
| 270 | return precision, true, nil |
| 271 | } |
| 272 | |
| 273 | var precision float64 |
| 274 | // First try to find precision by transaction_id |
| 275 | query := ` |
| 276 | SELECT precision |
| 277 | FROM ledgerforge.transactions |
| 278 | WHERE transaction_id = $1 |
| 279 | OR parent_transaction = $1 |
| 280 | LIMIT 1` |
| 281 | err := db.QueryRow(query, transactionID).Scan(&precision) |
| 282 | if err == sql.ErrNoRows { |
| 283 | return 0, false, nil |
| 284 | } |
| 285 | if err != nil { |
| 286 | logrus.WithError(err).WithField("transaction_id", transactionID).Error("error querying precision") |
| 287 | return 0, false, err |
| 288 | } |
| 289 | if precision <= 0 { // Or any other validation for valid precision |
| 290 | logrus.WithFields(logrus.Fields{ |
| 291 | "transaction_id": transactionID, |
| 292 | "precision": precision, |
| 293 | }).Warn("invalid precision found in DB") |
| 294 | return 0, false, nil // Treat invalid precision as not found for fallback logic |
| 295 | } |
| 296 | |
| 297 | // Store in cache |
| 298 | precisionCache[transactionID] = precision |
| 299 | logrus.WithFields(logrus.Fields{ |
| 300 | "transaction_id": transactionID, |
| 301 | "precision": precision, |
| 302 | }).Debug("cache miss, fetched precision from DB and cached") |
| 303 | return precision, true, nil |
| 304 | } |
| 305 | |
| 306 | // ApplyPrecisionWithDBLookup attempts to fetch precision from the database |
| 307 | // and then applies it to the transaction. Falls back to transaction-defined |
no test coverage detected