-
Notifications
You must be signed in to change notification settings - Fork 31
FIX: Row Object support #75
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ad905ce
Bug fixing
jahnvi480 f5a17e2
BUGFIX: Fetchone fix
jahnvi480 e65900b
Changing testcases to handle namedtuple
jahnvi480 8a4d4ad
Adding same for fetchmany and fetchall
jahnvi480 84e0af1
Resolving comments
jahnvi480 a86b99e
Resolving comments
jahnvi480 1f4d74f
Merge branch 'main' of https://github.com/microsoft/mssql-python into…
jahnvi480 919f940
Reverting changes
jahnvi480 dfc55e6
Rewriting the logic
jahnvi480 2892f23
adding line
jahnvi480 409f2fa
Adding todo for cursor_description
jahnvi480 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| class Row: | ||
jahnvi480 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| A row of data from a cursor fetch operation. Provides both tuple-like indexing | ||
| and attribute access to column values. | ||
| Example: | ||
| row = cursor.fetchone() | ||
| print(row[0]) # Access by index | ||
| print(row.column_name) # Access by column name | ||
| """ | ||
|
|
||
| def __init__(self, values, cursor_description): | ||
| """ | ||
| Initialize a Row object with values and cursor description. | ||
| Args: | ||
| values: List of values for this row | ||
| cursor_description: The cursor description containing column metadata | ||
| """ | ||
| self._values = values | ||
|
|
||
| # TODO: ADO task - Optimize memory usage by sharing column map across rows | ||
jahnvi480 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Instead of storing the full cursor_description in each Row object: | ||
| # 1. Build the column map once at the cursor level after setting description | ||
| # 2. Pass only this map to each Row instance | ||
| # 3. Remove cursor_description from Row objects entirely | ||
|
|
||
| # Create mapping of column names to indices | ||
| self._column_map = {} | ||
| for i, desc in enumerate(cursor_description): | ||
| if desc and desc[0]: # Ensure column name exists | ||
| self._column_map[desc[0]] = i | ||
|
|
||
| def __getitem__(self, index): | ||
| """Allow accessing by numeric index: row[0]""" | ||
| return self._values[index] | ||
|
|
||
| def __getattr__(self, name): | ||
| """Allow accessing by column name as attribute: row.column_name""" | ||
| if name in self._column_map: | ||
| return self._values[self._column_map[name]] | ||
| raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'") | ||
|
|
||
| def __eq__(self, other): | ||
| """ | ||
| Support comparison with lists for test compatibility. | ||
| This is the key change needed to fix the tests. | ||
| """ | ||
| if isinstance(other, list): | ||
| return self._values == other | ||
| elif isinstance(other, Row): | ||
| return self._values == other._values | ||
| return super().__eq__(other) | ||
|
|
||
| def __len__(self): | ||
| """Return the number of values in the row""" | ||
| return len(self._values) | ||
|
|
||
| def __iter__(self): | ||
| """Allow iteration through values""" | ||
| return iter(self._values) | ||
|
|
||
| def __repr__(self): | ||
| """Return a string representation of the row""" | ||
| return f"Row{tuple(self._values)}" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.