FEAT: Adding foreignkey API#195
Merged
jahnvi480 merged 5 commits intojahnvi/cursor_proceduresfrom Sep 15, 2025
Merged
Conversation
…ft/mssql-python into jahnvi/cursor_foreignkey
sumitmsft
approved these changes
Sep 11, 2025
sumitmsft
approved these changes
Sep 12, 2025
bewithgaurav
approved these changes
Sep 15, 2025
### Work Item / Issue Reference <!-- IMPORTANT: Please follow the PR template guidelines below. For mssql-python maintainers: Insert your ADO Work Item ID below (e.g. AB#37452) For external contributors: Insert Github Issue number below (e.g. #149) Only one reference is required - either GitHub issue OR ADO Work Item. --> <!-- mssql-python maintainers: ADO Work Item --> > [AB#34931](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/34931) ------------------------------------------------------------------- ### Summary This pull request adds support for retrieving primary key information from database tables using a new `primaryKeys` method on the `cursor` object. It introduces the necessary C++/ODBC bindings, Python method, and a comprehensive set of tests to validate the new functionality. **Primary Key Metadata Support** * Added a `primaryKeys` method to the `cursor` class in `cursor.py` to allow users to retrieve primary key columns for a specified table, including support for catalog and schema filtering. The method handles ODBC API requirements and result formatting. **ODBC/C++ Binding Enhancements** * Introduced the `SQLPrimaryKeysFunc` function pointer and associated logic in the C++ ODBC bindings (`ddbc_bindings.cpp` and `ddbc_bindings.h`), including function pointer loading, wrapper implementation, and Python module export, enabling Python to call the ODBC `SQLPrimaryKeys` API. **Testing Improvements** * Added a suite of tests in `test_004_cursor.py` to verify the behavior of the new `primaryKeys` method, including setup and cleanup of test tables, validation for single and composite primary keys, column metadata correctness, handling of non-existent tables, and catalog filtering. --------- Co-authored-by: Jahnvi Thakkar <jathakkar@microsoft.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Work Item / Issue Reference
Summary
This pull request adds support for retrieving foreign key metadata from SQL Server via ODBC by implementing the
foreignKeysmethod in theCursorclass and wiring up the necessary C++ bindings for the ODBCSQLForeignKeysAPI. The changes ensure that Python users can now fetch foreign key relationships in a way similar to other database drivers.Python interface enhancements:
foreignKeysmethod to theCursorclass incursor.py, allowing users to retrieve foreign key metadata for a specified table or referenced table. The method performs input validation, calls the new C++ binding, and returns results asRowobjects with appropriate metadata.cursor.pyto include theProgrammingErrorexception, used for input validation in the new method.C++ DDBC bindings:
SQLForeignKeysFunc) and corresponding global pointer (SQLForeignKeys_ptr) for the ODBCSQLForeignKeysAPI inddbc_bindings.hand initialized it inddbc_bindings.cpp.SQLForeignKeys_wrapfunction inddbc_bindings.cppto wrap the ODBC call, handling both Windows and Unix platforms, and exposed it to Python asDDBCSQLForeignKeys.SQLForeignKeysand fail if it is not present, ensuring the new functionality is always available.