-
Notifications
You must be signed in to change notification settings - Fork 159
feat: return affected row count for INSERT/UPDATE/DELETE #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,43 @@ export function extractRowsFromMultiStatement(results: any): any[] { | |
| return allRows; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts total affected rows from query results. | ||
| * | ||
| * For INSERT/UPDATE/DELETE operations, returns the sum of affectedRows. | ||
| * For SELECT operations, returns the number of rows. | ||
| * | ||
| * @param results - Raw results from the database driver | ||
| * @returns Total number of affected/returned rows | ||
| */ | ||
| export function extractAffectedRows(results: any): number { | ||
| // Handle metadata object (single INSERT/UPDATE/DELETE) | ||
| if (isMetadataObject(results)) { | ||
| return results.affectedRows || 0; | ||
| } | ||
|
|
||
| // Handle non-array results | ||
| if (!Array.isArray(results)) { | ||
| return 0; | ||
| } | ||
|
|
||
| // Check if this is a multi-statement result | ||
| if (isMultiStatementResult(results)) { | ||
| let totalAffected = 0; | ||
| for (const result of results) { | ||
| if (isMetadataObject(result)) { | ||
| totalAffected += result.affectedRows || 0; | ||
| } else if (Array.isArray(result)) { | ||
| totalAffected += result.length; | ||
| } | ||
| } | ||
| return totalAffected; | ||
|
Comment on lines
+92
to
+101
|
||
| } | ||
|
|
||
| // Single statement result - results is the rows array directly | ||
| return results.length; | ||
| } | ||
|
|
||
| /** | ||
| * Parses database query results, handling both single and multi-statement queries. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rowCount calculation for multi-statement queries is inconsistent with single-statement behavior. For single statements, rowCount represents either affected rows (for writes) OR returned rows (for reads), but not both. However, here it sums totalChanges (affected rows from writes) and allRows.length (returned rows from reads), mixing two different semantics.
This inconsistency could be confusing when displaying "N rows affected" in the frontend, as it would show a misleading count for mixed queries. Consider either: