|
10 | 10 | - **Python Versions**: 3.10+ |
11 | 11 | - **Key Dependencies**: pybind11, azure-identity, Microsoft ODBC Driver 18 |
12 | 12 |
|
| 13 | +## Development Workflows |
| 14 | + |
| 15 | +This repository includes detailed prompt files for common tasks. Reference these with `#`: |
| 16 | + |
| 17 | +| Task | Prompt | When to Use | |
| 18 | +|------|--------|-------------| |
| 19 | +| First-time setup | `#setup-dev-env` | New machine, fresh clone | |
| 20 | +| Build C++ extension | `#build-ddbc` | After modifying .cpp/.h files | |
| 21 | +| Run tests | `#run-tests` | Validating changes | |
| 22 | +| Create PR | `#create-pr` | Ready to submit changes | |
| 23 | + |
| 24 | +**Workflow order for new contributors:** |
| 25 | +1. `#setup-dev-env` → Set up venv and dependencies |
| 26 | +2. `#build-ddbc` → Build native extension |
| 27 | +3. Make your changes |
| 28 | +4. `#run-tests` → Validate |
| 29 | +5. `#create-pr` → Submit |
| 30 | + |
13 | 31 | ## Usage Examples (For Suggesting to Users) |
14 | 32 |
|
15 | 33 | ### Basic Connection and Query |
@@ -68,11 +86,13 @@ conn.commit() |
68 | 86 |
|
69 | 87 | ### Transaction Handling |
70 | 88 | ```python |
| 89 | +from mssql_python import DatabaseError |
| 90 | + |
71 | 91 | try: |
72 | 92 | cursor.execute("UPDATE accounts SET balance = balance - 100 WHERE id = ?", (from_id,)) |
73 | 93 | cursor.execute("UPDATE accounts SET balance = balance + 100 WHERE id = ?", (to_id,)) |
74 | 94 | conn.commit() |
75 | | -except Exception: |
| 95 | +except DatabaseError: |
76 | 96 | conn.rollback() |
77 | 97 | raise |
78 | 98 | ``` |
@@ -234,6 +254,55 @@ The `ddbc_bindings.py` module implements sophisticated architecture detection: |
234 | 254 | - **macOS**: Runtime architecture detection, always loads from universal2 binary |
235 | 255 | - **Linux**: Maps `x64/amd64` → `x86_64`, `arm64/aarch64` → `arm64` |
236 | 256 |
|
| 257 | +## Exception Hierarchy |
| 258 | + |
| 259 | +Critical for error handling guidance: |
| 260 | + |
| 261 | +``` |
| 262 | +Error (base) |
| 263 | +├── DatabaseError |
| 264 | +│ ├── InterfaceError # Driver/interface issues |
| 265 | +│ ├── OperationalError # Connection/timeout issues |
| 266 | +│ ├── IntegrityError # Constraint violations |
| 267 | +│ ├── ProgrammingError # SQL syntax errors |
| 268 | +│ └── DataError # Invalid data processing |
| 269 | +└── Warning |
| 270 | +``` |
| 271 | + |
| 272 | +## Critical Anti-Patterns (DO NOT) |
| 273 | + |
| 274 | +- **NEVER** hardcode connection strings - always use `DB_CONNECTION_STRING` env var for tests |
| 275 | +- **NEVER** use `pyodbc` imports - this driver doesn't require external ODBC |
| 276 | +- **NEVER** modify files in `mssql_python/libs/` - these are pre-built binaries |
| 277 | +- **NEVER** skip `conn.commit()` after INSERT/UPDATE/DELETE operations |
| 278 | +- **NEVER** use bare `except:` blocks - always catch specific exceptions |
| 279 | +- **NEVER** leave connections open - use context managers or explicit `close()` |
| 280 | + |
| 281 | +## When Modifying Code |
| 282 | + |
| 283 | +### Python Changes |
| 284 | +- Preserve existing error handling patterns from `exceptions.py` |
| 285 | +- Use context managers (`with`) for all connection/cursor operations |
| 286 | +- Update `__all__` exports if adding public API |
| 287 | +- Add corresponding test in `tests/test_*.py` |
| 288 | +- Follow Black formatting (line length 100) |
| 289 | + |
| 290 | +### C++ Changes |
| 291 | +- Follow RAII patterns for resource management |
| 292 | +- Use `py::gil_scoped_release` for blocking ODBC operations |
| 293 | +- Update `mssql_python.pyi` type stubs if changing Python API |
| 294 | +- Follow `.clang-format` style (Google style, 100 column limit) |
| 295 | + |
| 296 | +## Debugging Quick Reference |
| 297 | + |
| 298 | +| Error | Cause | Solution | |
| 299 | +|-------|-------|----------| |
| 300 | +| `ImportError: ddbc_bindings` | Extension not built | Run `#build-ddbc` | |
| 301 | +| Connection timeout | Missing env var | Set `DB_CONNECTION_STRING` | |
| 302 | +| `dylib not found` (macOS) | Library paths | Run `configure_dylibs.sh` | |
| 303 | +| `ODBC Driver not found` | Missing driver | Install Microsoft ODBC Driver 18 | |
| 304 | +| `ModuleNotFoundError` | Not in venv | Run `#setup-dev-env` | |
| 305 | + |
237 | 306 | ## Contributing Guidelines |
238 | 307 |
|
239 | 308 | ### PR Requirements |
|
0 commit comments