-
Notifications
You must be signed in to change notification settings - Fork 33
FEAT: [POC] BCP implementation in mssql-python driver - #397
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
Draft
subrata-ms
wants to merge
9
commits into
main
Choose a base branch
from
subrata-ms/Rust-Python
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.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
01fb45d
Add PyO3 Rust bindings and BCPRust library integration
subrata-ms 2800534
test added
subrata-ms abba999
core changes
subrata-ms 6e91f3c
cleanup
subrata-ms 4b344e9
Clean up: Remove Rust build artifacts (target/) and update .gitignore
subrata-ms 8f3d8db
Cleanup lib.rs
subrata-ms cf438ed
Remove BCPRustLib from version control - external build artifacts
subrata-ms f8102a4
optimization
subrata-ms 593c405
base logic refactor
subrata-ms 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 |
|---|---|---|
| @@ -1,18 +1,102 @@ | ||
| from mssql_python import connect | ||
| from mssql_python.logging import setup_logging | ||
| import os | ||
| from datetime import datetime | ||
|
|
||
| # Clean one-liner: set level and output mode together | ||
| setup_logging(output="both") | ||
|
|
||
| conn_str = os.getenv("DB_CONNECTION_STRING") | ||
| print("=" * 70) | ||
| print("SQL Server - Bulk Copy Demo") | ||
| print("=" * 70) | ||
|
|
||
| # Use local SQL Server or environment variable | ||
| conn_str = os.getenv("DB_CONNECTION_STRING", | ||
| "Server=localhost,1433;Database=master;UID=sa;PWD=uvFvisUxK4En7AAV;TrustServerCertificate=yes;") | ||
|
|
||
| print("\n[1] Connecting to database...") | ||
| conn = connect(conn_str) | ||
| cursor = conn.cursor() | ||
|
|
||
| # Query databases | ||
| print("[2] Querying sys.databases...") | ||
| cursor.execute("SELECT database_id, name from sys.databases;") | ||
| rows = cursor.fetchall() | ||
|
|
||
| for row in rows: | ||
| print(f"Database ID: {row[0]}, Name: {row[1]}") | ||
| print(f" Database ID: {row[0]}, Name: {row[1]}") | ||
|
|
||
| print(f"\n Total databases: {len(rows)}") | ||
|
|
||
| # Demonstrate bulk copy functionality | ||
| print("\n" + "=" * 70) | ||
| print("Bulk Copy with Rust Bindings") | ||
| print("=" * 70) | ||
|
|
||
| try: | ||
| print("\n[3] Creating temporary table for bulk copy...") | ||
| cursor.execute(""" | ||
| CREATE TABLE #bulk_copy_demo ( | ||
| id INT, | ||
| name NVARCHAR(50), | ||
| value DECIMAL(10, 2), | ||
| created_date DATETIME | ||
| ) | ||
| """) | ||
| conn.commit() | ||
| print(" ✓ Table created") | ||
|
|
||
| # Generate 100 rows of test data | ||
| print("\n[4] Generating 100 rows of test data...") | ||
| test_data = [] | ||
| for i in range(1, 101): | ||
| test_data.append([ | ||
| i, | ||
| f"TestItem_{i}", | ||
| float(i * 10.5), | ||
| datetime.now() | ||
| ]) | ||
| print(f" ✓ Generated {len(test_data)} rows") | ||
|
|
||
| # Perform bulk copy using cursor method | ||
| print("\n[5] Performing bulk copy via cursor...") | ||
| result = cursor.bulk_copy('#bulk_copy_demo', test_data) | ||
| print(f" ✓ Bulk copy completed: {result}") | ||
|
|
||
| # Verify the data | ||
| print("\n[6] Verifying bulk copy results...") | ||
| cursor.execute("SELECT COUNT(*) FROM #bulk_copy_demo") | ||
| count = cursor.fetchone()[0] | ||
| print(f" ✓ Total rows copied: {count}") | ||
|
|
||
| # Show sample data | ||
| cursor.execute("SELECT TOP 5 id, name, value FROM #bulk_copy_demo ORDER BY id") | ||
| sample_rows = cursor.fetchall() | ||
| print("\n Sample data:") | ||
| for row in sample_rows: | ||
| print(f" ID: {row[0]}, Name: {row[1]}, Value: {row[2]}") | ||
|
|
||
| print("\n✓ Bulk copy demo completed successfully!") | ||
|
|
||
| # Cleanup | ||
| cursor.execute("DROP TABLE IF EXISTS #bulk_copy_demo") | ||
| conn.commit() | ||
|
|
||
| except ImportError as e: | ||
| print(f"\n✗ Rust bindings or mssql_core_tds not available: {e}") | ||
| except AttributeError as e: | ||
| print(f"\n⚠ {e}") | ||
| print(" Skipping bulk copy demo") | ||
| # Cleanup | ||
| cursor.execute("DROP TABLE IF EXISTS #bulk_copy_demo") | ||
| conn.commit() | ||
| except Exception as e: | ||
| print(f"\n✗ Bulk copy failed: {e}") | ||
| # Cleanup | ||
| cursor.execute("DROP TABLE IF EXISTS #bulk_copy_demo") | ||
| conn.commit() | ||
|
|
||
| print("\n" + "=" * 70) | ||
| cursor.close() | ||
| conn.close() | ||
| conn.close() | ||
| print("✓ Connection closed") | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
Check notice
Code scanning / devskim
Accessing localhost could indicate debug code, or could hinder scaling. Note