Skip to content

Conversation

@jahnvi480
Copy link
Contributor

@jahnvi480 jahnvi480 commented Aug 22, 2025

Work Item / Issue Reference

AB#34932


Summary

This pull request adds support for retrieving foreign key metadata from SQL Server via ODBC by implementing the foreignKeys method in the Cursor class and wiring up the necessary C++ bindings for the ODBC SQLForeignKeys API. The changes ensure that Python users can now fetch foreign key relationships in a way similar to other database drivers.

Python interface enhancements:

  • Added a new foreignKeys method to the Cursor class in cursor.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 as Row objects with appropriate metadata.
  • Updated imports in cursor.py to include the ProgrammingError exception, used for input validation in the new method.

C++ DDBC bindings:

  • Added a new function pointer type (SQLForeignKeysFunc) and corresponding global pointer (SQLForeignKeys_ptr) for the ODBC SQLForeignKeys API in ddbc_bindings.h and initialized it in ddbc_bindings.cpp.
  • Implemented the SQLForeignKeys_wrap function in ddbc_bindings.cpp to wrap the ODBC call, handling both Windows and Unix platforms, and exposed it to Python as DDBCSQLForeignKeys.
  • Updated the driver loading logic to require SQLForeignKeys and fail if it is not present, ensuring the new functionality is always available.

@github-actions github-actions bot added the pr-size: large Substantial code update label Aug 22, 2025
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Aug 22, 2025
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Sep 12, 2025
jahnvi480 and others added 2 commits September 15, 2025 10:14
### 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 <[email protected]>
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: large Substantial code update labels Sep 15, 2025
@jahnvi480 jahnvi480 merged commit 44b7ffc into jahnvi/cursor_procedures Sep 15, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-size: large Substantial code update

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants