Skip to content

FEAT: Row string-key indexing #589

Open
jahnvi480 wants to merge 8 commits into
mainfrom
jahnvi/row_index_582
Open

FEAT: Row string-key indexing #589
jahnvi480 wants to merge 8 commits into
mainfrom
jahnvi/row_index_582

Conversation

@jahnvi480
Copy link
Copy Markdown
Contributor

@jahnvi480 jahnvi480 commented May 19, 2026

Work Item / Issue Reference

AB#45119

GitHub Issue: #582


Summary

This pull request enhances the usability of the Row class by allowing access to row values using column names as string keys (e.g., row["col"]), in addition to existing integer index and attribute access. It also introduces case-insensitive string-key access when the cursor's lowercase attribute is enabled. Comprehensive tests have been added to ensure correct behavior for these new access patterns.

Enhancements to Row Access Patterns

  • Updated the Row.__getitem__ method in mssql_python/row.py to support accessing values by column name as a string key, including case-insensitive lookup when the cursor's lowercase attribute is set.

Testing Additions

  • Added test_row_string_key_indexing and test_row_string_key_case_insensitive_with_lowercase to tests/test_001_globals.py to verify string-key and case-insensitive access, as well as backward compatibility with integer and slice access.
  • Added test_row_string_key_indexing to tests/test_004_cursor.py to test string-key access on real query results, ensuring consistency with index and attribute access and correct error handling for missing keys.

Copilot AI review requested due to automatic review settings May 19, 2026 08:08
@github-actions github-actions Bot added the pr-size: medium Moderate update size label May 19, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR restores/extends Row usability by allowing row["ColumnName"] access (in addition to positional and attribute access), addressing the regression reported in #582.

Changes:

  • Updated Row.__getitem__ to accept string keys that map to column names.
  • Added unit and integration tests covering string-key access and expected error behavior.
  • Added a test intending to cover case-insensitive string-key lookup when lowercase mode is enabled.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
mssql_python/row.py Adds string-key handling to Row.__getitem__ (including an intended case-insensitive path).
tests/test_001_globals.py Adds unit tests for string-key indexing and a case-insensitive variant.
tests/test_004_cursor.py Adds an integration test verifying string-key indexing against real query results.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread mssql_python/row.py
Comment on lines +165 to +170
if hasattr(self._cursor, "lowercase") and self._cursor.lowercase:
index_lower = index.lower()
for col_name in self._column_map:
if col_name.lower() == index_lower:
return self._values[self._column_map[col_name]]
raise KeyError(f"Row has no column '{index}'")
Comment thread tests/test_001_globals.py
Comment on lines +1030 to +1048
"""Test Row string-key indexing is case-insensitive when cursor.lowercase is True."""
from mssql_python.row import Row
from unittest.mock import Mock

mock_cursor = Mock()
mock_cursor.lowercase = True
mock_cursor.connection._output_converters = None

row = Row(
[1, "bar"],
{"productid": 0, "name": 1},
cursor=mock_cursor,
)

# Exact match
assert row["productid"] == 1
# Case-insensitive match
assert row["ProductID"] == 1
assert row["NAME"] == "bar"
Comment thread tests/test_004_cursor.py
Comment on lines +2910 to +2911
cursor.execute("DROP TABLE #pytest_row_strkey")
db_connection.commit()
@github-actions
Copy link
Copy Markdown

📊 Code Coverage Report

🔥 Diff Coverage

100%


🎯 Overall Coverage

25%


📈 Total Lines Covered: 7027 out of 27143
📁 Project: mssql-python


Diff Coverage

Diff: main...HEAD, staged and unstaged changes

  • mssql_python/row.py (100%)

Summary

  • Total: 10 lines
  • Missing: 0 lines
  • Coverage: 100%

📋 Files Needing Attention

📉 Files with overall lowest coverage (click to expand)
mssql_python.pybind.build._deps.simdutf-src.src.haswell.implementation.cpp: 0.4%
mssql_python.pybind.build._deps.simdutf-src.src.implementation.cpp: 6.7%
mssql_python.pybind.build._deps.simdutf-src.include.simdutf.implementation.h: 10.4%
mssql_python.pybind.build._deps.simdutf-src.include.simdutf.scalar.utf16_to_utf8.utf16_to_utf8.h: 25.3%
mssql_python.pybind.logger_bridge.cpp: 59.2%
mssql_python.pybind.ddbc_bindings.h: 59.7%
mssql_python.pybind.build._deps.simdutf-src.include.simdutf.internal.isadetection.h: 65.3%
mssql_python.pybind.logger_bridge.hpp: 70.8%
mssql_python.pybind.ddbc_bindings.cpp: 74.2%
mssql_python.__init__.py: 77.3%

🔗 Quick Links

⚙️ Build Summary 📋 Coverage Details

View Azure DevOps Build

Browse Full Coverage Report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-size: medium Moderate update size

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants