Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 103 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,118 @@
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;")

Check notice

Code scanning / devskim

Accessing localhost could indicate debug code, or could hinder scaling. Note

Do not leave debug code in production

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:
import mssql_rust_bindings as rust
import mssql_core_tds

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")

# Create mssql_core_tds connection
print("\n[5] Creating mssql_core_tds connection...")
conn_dict = {
'server': 'localhost',
'database': 'master',
'user_name': 'sa',
'password': 'uvFvisUxK4En7AAV',
'trust_server_certificate': 'yes'
}
core_conn = mssql_core_tds.DdbcConnection(conn_dict)
print(" ✓ Connection created")

# Check if bulk_copy is available
if not hasattr(core_conn, 'bulk_copy'):
print("\n ⚠ bulk_copy method not yet implemented in mssql_core_tds")
print(" Skipping bulk copy demo")
else:
# Create BulkCopyWrapper and perform bulk copy
print("\n[6] Creating BulkCopyWrapper...")
bulk_wrapper = rust.BulkCopyWrapper(core_conn)
print(" ✓ Wrapper created")

print("\n[7] Performing bulk copy...")
result = bulk_wrapper.bulk_copy('#bulk_copy_demo', test_data)
print(f" ✓ Bulk copy completed: {result}")

# Verify the data
print("\n[8] 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]}")

core_conn.close()
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 Exception as e:
print(f"\n✗ Bulk copy failed: {e}")

print("\n" + "=" * 70)
cursor.close()
conn.close()
conn.close()
print("✓ Connection closed")
64 changes: 64 additions & 0 deletions mssql_python/BCPRustLib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# BCPRust Library Binaries

This directory contains the compiled binaries from the mssql-tds Rust project.

## Contents

### Python Bindings
- **libmssql_core_tds.so** - Core TDS protocol Python extension (PyO3)
- **mssql_py_core-*.whl** - Python wheel package for easy installation

### JavaScript Bindings
- **libmssql_js.so** - Node.js native addon for TDS protocol

### CLI Tool
- **mssql-tds-cli** - Command-line interface for testing TDS connections

## Usage

### Installing Python Bindings

```bash
pip install --break-system-packages mssql_py_core-0.1.0-cp312-cp312-linux_x86_64.whl
```

Or for development:
```bash
python3 -c "import sys; sys.path.insert(0, '.'); import libmssql_core_tds"
```

### Testing the CLI

```bash
./mssql-tds-cli --help
```

## Source

Built from: `~/BCPRust/mssql-tds`

## Build Commands Used

```bash
# Main workspace
cd ~/BCPRust/mssql-tds
cargo build --release

# Python bindings
cd ~/BCPRust/mssql-tds/mssql-py-core
maturin build --release --skip-auditwheel
```

## File Sizes

- libmssql_core_tds.so: ~2.6 MB
- libmssql_js.so: ~3.3 MB
- mssql-tds-cli: ~2.0 MB
- Python wheel: ~978 KB

## Notes

- These are release builds with optimizations enabled
- Symbols are stripped for smaller file sizes
- Built for Linux x86_64 platform
- Python bindings are for CPython 3.12
Binary file added mssql_python/BCPRustLib/mssql-tds-cli
Binary file not shown.
180 changes: 180 additions & 0 deletions mssql_python/rust_bindings/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions mssql_python/rust_bindings/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "mssql_rust_bindings"
version = "1.1.0"
edition = "2021"
authors = ["Microsoft SQL Server Python Team"]
description = "Rust bindings for mssql-python using PyO3"
license = "MIT"

[lib]
name = "mssql_rust_bindings"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.22", features = ["extension-module", "abi3-py38"] }

[profile.release]
opt-level = 3
lto = true
codegen-units = 1
strip = true

[profile.dev]
opt-level = 0
debug = true
Loading
Loading