-
Notifications
You must be signed in to change notification settings - Fork 25
FEAT: Adding cursor.lastrowid attribute #167
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
Open
jahnvi480
wants to merge
9
commits into
main
Choose a base branch
from
jahnvi/cursor_lastrowid
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−0
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
982e9e3
FEAT: Adding cursor.lastrowid attribute
jahnvi480 d604ed1
FEAT: Adding cursor.lastrowid attribute
jahnvi480 12d6bd9
Merge branch 'main' of https://github.com/microsoft/mssql-python into…
jahnvi480 bb84e61
Resolving conflicts
jahnvi480 1c775ac
Merge branch 'main' into jahnvi/cursor_lastrowid
jahnvi480 02ca1c4
Merge branch 'main' into jahnvi/cursor_lastrowid
jahnvi480 d0a3898
Resolving conflicts
jahnvi480 0985d77
Resolving conflicts
jahnvi480 aeefa82
Resolving comments
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
Some comments aren't visible on the classic Files Changed page.
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 |
---|---|---|
|
@@ -11409,6 +11409,135 @@ def test_datetime_string_parameter_binding(cursor, db_connection): | |
drop_table_if_exists(cursor, table_name) | ||
db_connection.commit() | ||
|
||
def test_lastrowid_single_insert(cursor, db_connection): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check if these tests are added:
|
||
"""Test lastrowid with single INSERT operation""" | ||
try: | ||
# Create table with identity column | ||
cursor.execute("CREATE TABLE #test_lastrowid (id INT IDENTITY(1,1) PRIMARY KEY, name VARCHAR(50))") | ||
db_connection.commit() | ||
|
||
# Test initial state | ||
assert cursor.lastrowid is None, "lastrowid should initially be None" | ||
|
||
# Single INSERT should set lastrowid | ||
cursor.execute("INSERT INTO #test_lastrowid (name) VALUES (?)", ["test1"]) | ||
db_connection.commit() | ||
|
||
assert cursor.lastrowid is not None, "lastrowid should not be None after INSERT" | ||
assert isinstance(cursor.lastrowid, int), "lastrowid should be an integer" | ||
assert cursor.lastrowid > 0, "lastrowid should be positive" | ||
|
||
# Store the first ID | ||
first_id = cursor.lastrowid | ||
|
||
# Another single INSERT should update lastrowid | ||
cursor.execute("INSERT INTO #test_lastrowid (name) VALUES (?)", ["test2"]) | ||
db_connection.commit() | ||
|
||
assert cursor.lastrowid == first_id + 1, "lastrowid should increment for subsequent INSERTs" | ||
|
||
finally: | ||
try: | ||
cursor.execute("DROP TABLE #test_lastrowid") | ||
db_connection.commit() | ||
except: | ||
pass | ||
|
||
|
||
def test_lastrowid_multiple_insert(cursor, db_connection): | ||
"""Test lastrowid with multiple INSERT operations""" | ||
try: | ||
# Create table with identity column | ||
cursor.execute("CREATE TABLE #test_lastrowid (id INT IDENTITY(1,1) PRIMARY KEY, name VARCHAR(50))") | ||
db_connection.commit() | ||
|
||
# Multiple INSERT should set lastrowid to None (undefined semantics) | ||
cursor.execute("INSERT INTO #test_lastrowid (name) VALUES ('test1'), ('test2'), ('test3')") | ||
db_connection.commit() | ||
|
||
# lastrowid semantics are undefined for multiple inserts, but we set it to None | ||
assert cursor.lastrowid is None, "lastrowid should be None for multiple INSERTs" | ||
|
||
finally: | ||
try: | ||
cursor.execute("DROP TABLE #test_lastrowid") | ||
db_connection.commit() | ||
except: | ||
pass | ||
|
||
|
||
def test_lastrowid_executemany(cursor, db_connection): | ||
"""Test lastrowid with executemany""" | ||
try: | ||
# Create table with identity column | ||
cursor.execute("CREATE TABLE #test_lastrowid (id INT IDENTITY(1,1) PRIMARY KEY, name VARCHAR(50))") | ||
db_connection.commit() | ||
|
||
# executemany should set lastrowid to None (undefined semantics) | ||
data = [("test1",), ("test2",), ("test3",)] | ||
cursor.executemany("INSERT INTO #test_lastrowid (name) VALUES (?)", data) | ||
db_connection.commit() | ||
|
||
assert cursor.lastrowid is None, "lastrowid should be None after executemany" | ||
|
||
finally: | ||
try: | ||
cursor.execute("DROP TABLE #test_lastrowid") | ||
db_connection.commit() | ||
except: | ||
pass | ||
|
||
def test_lastrowid_non_insert_operations(cursor, db_connection): | ||
"""Test lastrowid with non-INSERT operations""" | ||
try: | ||
# Create table with identity column and some data | ||
cursor.execute("CREATE TABLE #test_lastrowid (id INT IDENTITY(1,1) PRIMARY KEY, name VARCHAR(50))") | ||
cursor.execute("INSERT INTO #test_lastrowid (name) VALUES ('initial')") | ||
db_connection.commit() | ||
|
||
# SELECT should not affect lastrowid but should reset it | ||
cursor.execute("SELECT * FROM #test_lastrowid") | ||
assert cursor.lastrowid is None, "lastrowid should be None after SELECT" | ||
|
||
# UPDATE should preserve the last inserted ID | ||
cursor.execute("UPDATE #test_lastrowid SET name = 'updated' WHERE id = 1") | ||
db_connection.commit() | ||
# Accept that lastrowid reflects the ID of the last affected row | ||
assert cursor.lastrowid == 1, "lastrowid should reflect the ID of the updated row" | ||
finally: | ||
# Cleanup code if any | ||
pass | ||
|
||
def test_lastrowid_table_without_identity(cursor, db_connection): | ||
"""Test lastrowid with table that has no identity column""" | ||
try: | ||
# Create table without identity column | ||
cursor.execute("CREATE TABLE #test_no_identity (id INT PRIMARY KEY, name VARCHAR(50))") | ||
db_connection.commit() | ||
|
||
# INSERT into table without identity should not set lastrowid | ||
cursor.execute("INSERT INTO #test_no_identity (id, name) VALUES (1, 'test')") | ||
db_connection.commit() | ||
|
||
assert cursor.lastrowid is None, "lastrowid should be None for table without identity" | ||
|
||
finally: | ||
try: | ||
cursor.execute("DROP TABLE #test_no_identity") | ||
db_connection.commit() | ||
except: | ||
pass | ||
|
||
|
||
def test_lastrowid_readonly(cursor): | ||
"""Test that lastrowid is read-only""" | ||
# lastrowid should be read-only, attempting to set it should raise AttributeError | ||
try: | ||
cursor.lastrowid = 123 | ||
assert False, "Setting lastrowid should raise AttributeError" | ||
except AttributeError: | ||
pass # Expected behavior | ||
|
||
def test_close(db_connection): | ||
"""Test closing the cursor""" | ||
try: | ||
|
Oops, something went wrong.
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.