Skip to content

Commit c587cc8

Browse files
passrenPeng Ren
andauthored
0.2.0 (#2)
* enhance Connection * Add trigger for feature branch * Fix code smell * Update readme * Fix test cases * Refactor test cases * Fixed test case * Add more cases for query * Fix code formatting issue * Fixed something bugs * Add sqlalchemy support * Update readme * Update README * Update README * Fixed bugs * Fix test cases which can run on remote server * Enhance CI Test to run tests against sqlalchemy 1.x and 2.x * Clean up the folders * Splitted requirements.txt * Register dialect * Add supports for Superset --------- Co-authored-by: Peng Ren <[email protected]>
1 parent 5bafbd5 commit c587cc8

File tree

13 files changed

+1993
-81
lines changed

13 files changed

+1993
-81
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
matrix:
1515
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13', '3.14']
1616
mongodb-version: ['7.0', '8.0']
17+
# Test against representative SQLAlchemy series (1.x and 2.x)
18+
sqlalchemy-version: ['1.4.*', '2.*']
1719

1820
services:
1921
mongodb:
@@ -41,9 +43,9 @@ jobs:
4143
uses: actions/cache@v3
4244
with:
4345
path: ~/.cache/pip
44-
key: ${{ runner.os }}-py${{ matrix.python-version }}-mongo${{ matrix.mongodb-version }}-pip-${{ hashFiles('**/requirements-test.txt', 'pyproject.toml') }}
46+
key: ${{ runner.os }}-py${{ matrix.python-version }}-mongo${{ matrix.mongodb-version }}-sqlalchemy-${{ matrix.sqlalchemy-version }}-pip-${{ hashFiles('**/requirements-test.txt', 'pyproject.toml') }}
4547
restore-keys: |
46-
${{ runner.os }}-py${{ matrix.python-version }}-mongo${{ matrix.mongodb-version }}-pip-
48+
${{ runner.os }}-py${{ matrix.python-version }}-mongo${{ matrix.mongodb-version }}-sqlalchemy-${{ matrix.sqlalchemy-version }}-pip-
4749
4850
- name: Install MongoDB shell
4951
run: |
@@ -55,6 +57,9 @@ jobs:
5557
- name: Install dependencies
5658
run: |
5759
python -m pip install --upgrade pip
60+
# Install the target SQLAlchemy version for this matrix entry first to ensure
61+
# tests run against both 1.x and 2.x series.
62+
pip install "SQLAlchemy==${{ matrix.sqlalchemy-version }}"
5863
pip install -r requirements-test.txt
5964
pip install black isort
6065

MANIFEST.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ include requirements-test.txt
88

99
# Include configuration files
1010
include pyproject.toml
11-
include .flake8
1211

1312
# Exclude unnecessary files
1413
global-exclude *.pyc

README.md

Lines changed: 15 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
11
# PyMongoSQL
22

3+
[![PyPI](https://img.shields.io/pypi/v/pymongosql)](https://pypi.org/project/pymongosql/)
34
[![Test](https://github.com/passren/PyMongoSQL/actions/workflows/ci.yml/badge.svg)](https://github.com/passren/PyMongoSQL/actions/workflows/ci.yml)
45
[![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
6+
[![codecov](https://codecov.io/gh/passren/PyMongoSQL/branch/main/graph/badge.svg?token=2CTRL80NP2)](https://codecov.io/gh/passren/PyMongoSQL)
57
[![License: MIT](https://img.shields.io/badge/License-MIT-purple.svg)](https://github.com/passren/PyMongoSQL/blob/0.1.2/LICENSE)
68
[![Python Version](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/)
79
[![MongoDB](https://img.shields.io/badge/MongoDB-7.0+-green.svg)](https://www.mongodb.com/)
10+
[![SQLAlchemy](https://img.shields.io/badge/SQLAlchemy-1.4+_2.0+-darkgreen.svg)](https://www.sqlalchemy.org/)
811

9-
PyMongoSQL is a Python [DB API 2.0 (PEP 249)](https://www.python.org/dev/peps/pep-0249/) client for [MongoDB](https://www.mongodb.com/). It provides a familiar SQL interface to MongoDB, allowing developers to use SQL queries to interact with MongoDB collections.
12+
PyMongoSQL is a Python [DB API 2.0 (PEP 249)](https://www.python.org/dev/peps/pep-0249/) client for [MongoDB](https://www.mongodb.com/). It provides a familiar SQL interface to MongoDB, allowing developers to use SQL to interact with MongoDB collections.
1013

1114
## Objectives
1215

1316
PyMongoSQL implements the DB API 2.0 interfaces to provide SQL-like access to MongoDB. The project aims to:
1417

15-
- Bridge the gap between SQL and NoSQL by providing SQL query capabilities for MongoDB
18+
- Bridge the gap between SQL and NoSQL by providing SQL capabilities for MongoDB
1619
- Support standard SQL DQL (Data Query Language) operations including SELECT statements with WHERE, ORDER BY, and LIMIT clauses
1720
- Provide seamless integration with existing Python applications that expect DB API 2.0 compliance
1821
- Enable easy migration from traditional SQL databases to MongoDB
19-
- Support field aliasing and projection mapping for flexible result set handling
20-
- Maintain high performance through direct `db.command()` execution instead of high-level APIs
2122

2223
## Features
2324

2425
- **DB API 2.0 Compliant**: Full compatibility with Python Database API 2.0 specification
26+
- **SQLAlchemy Integration**: Complete ORM and Core support with dedicated MongoDB dialect
2527
- **SQL Query Support**: SELECT statements with WHERE conditions, field selection, and aliases
26-
- **MongoDB Native Integration**: Direct `db.command()` execution for optimal performance
2728
- **Connection String Support**: MongoDB URI format for easy configuration
28-
- **Result Set Handling**: Support for `fetchone()`, `fetchmany()`, and `fetchall()` operations
29-
- **Field Aliasing**: SQL-style field aliases with automatic projection mapping
30-
- **Context Manager Support**: Automatic resource management with `with` statements
31-
- **Transaction Ready**: Architecture designed for future DML operation support (INSERT, UPDATE, DELETE)
3229

3330
## Requirements
3431

3532
- **Python**: 3.9, 3.10, 3.11, 3.12, 3.13+
36-
- **MongoDB**: 4.0+
33+
- **MongoDB**: 7.0+
3734

3835
## Dependencies
3936

@@ -43,6 +40,11 @@ PyMongoSQL implements the DB API 2.0 interfaces to provide SQL-like access to Mo
4340
- **ANTLR4** (SQL Parser Runtime)
4441
- antlr4-python3-runtime >= 4.13.0
4542

43+
### Optional Dependencies
44+
45+
- **SQLAlchemy** (for ORM/Core support)
46+
- sqlalchemy >= 1.4.0 (SQLAlchemy 1.4+ and 2.0+ supported)
47+
4648
## Installation
4749

4850
```bash
@@ -67,7 +69,7 @@ from pymongosql import connect
6769
# Connect to MongoDB
6870
connection = connect(
6971
host="mongodb://localhost:27017",
70-
database="test_db"
72+
database="database"
7173
)
7274

7375
cursor = connection.cursor()
@@ -97,44 +99,19 @@ for row in cursor:
9799
```python
98100
from pymongosql import connect
99101

100-
with connect(host="mongodb://localhost:27017", database="mydb") as conn:
102+
with connect(host="mongodb://localhost:27017/database") as conn:
101103
with conn.cursor() as cursor:
102104
cursor.execute('SELECT COUNT(*) as total FROM users')
103105
result = cursor.fetchone()
104106
print(f"Total users: {result['total']}")
105107
```
106108

107-
### Field Aliases and Projections
108-
109-
```python
110-
from pymongosql import connect
111-
112-
connection = connect(host="mongodb://localhost:27017", database="ecommerce")
113-
cursor = connection.cursor()
114-
115-
# Use field aliases for cleaner result sets
116-
cursor.execute('''
117-
SELECT
118-
name AS product_name,
119-
price AS cost,
120-
category AS product_type
121-
FROM products
122-
WHERE in_stock = true
123-
ORDER BY price DESC
124-
LIMIT 10
125-
''')
126-
127-
products = cursor.fetchall()
128-
for product in products:
129-
print(f"{product['product_name']}: ${product['cost']}")
130-
```
131-
132109
### Query with Parameters
133110

134111
```python
135112
from pymongosql import connect
136113

137-
connection = connect(host="mongodb://localhost:27017", database="blog")
114+
connection = connect(host="mongodb://localhost:27017/database")
138115
cursor = connection.cursor()
139116

140117
# Parameterized queries for security
@@ -159,7 +136,6 @@ while users:
159136
### SELECT Statements
160137
- Field selection: `SELECT name, age FROM users`
161138
- Wildcards: `SELECT * FROM products`
162-
- Field aliases: `SELECT name AS user_name, age AS user_age FROM users`
163139

164140
### WHERE Clauses
165141
- Equality: `WHERE name = 'John'`
@@ -171,15 +147,6 @@ while users:
171147
- LIMIT: `LIMIT 10`
172148
- Combined: `ORDER BY created_at DESC LIMIT 5`
173149

174-
## Architecture
175-
176-
PyMongoSQL uses a multi-layer architecture:
177-
178-
1. **SQL Parser**: Built with ANTLR4 for robust SQL parsing
179-
2. **Query Planner**: Converts SQL AST to MongoDB query plans
180-
3. **Command Executor**: Direct `db.command()` execution for performance
181-
4. **Result Processor**: Handles projection mapping and result set iteration
182-
183150
## Connection Options
184151

185152
```python
@@ -201,31 +168,6 @@ print(conn.database_name) # Database name
201168
print(conn.is_connected) # Connection status
202169
```
203170

204-
## Error Handling
205-
206-
```python
207-
from pymongosql import connect
208-
from pymongosql.error import ProgrammingError, SqlSyntaxError
209-
210-
try:
211-
connection = connect(host="mongodb://localhost:27017", database="test")
212-
cursor = connection.cursor()
213-
cursor.execute("INVALID SQL SYNTAX")
214-
except SqlSyntaxError as e:
215-
print(f"SQL syntax error: {e}")
216-
except ProgrammingError as e:
217-
print(f"Programming error: {e}")
218-
```
219-
220-
## Development Status
221-
222-
PyMongoSQL is currently focused on DQL (Data Query Language) operations. Future releases will include:
223-
224-
- **DML Operations**: INSERT, UPDATE, DELETE statements
225-
- **Advanced SQL Features**: JOINs, subqueries, aggregations
226-
- **Schema Operations**: CREATE/DROP collection commands
227-
- **Transaction Support**: Multi-document ACID transactions
228-
229171
## Contributing
230172

231173
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

pymongosql/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
if TYPE_CHECKING:
77
from .connection import Connection
88

9-
__version__: str = "0.1.2"
9+
__version__: str = "0.2.0"
1010

1111
# Globals https://www.python.org/dev/peps/pep-0249/#globals
1212
apilevel: str = "2.0"
@@ -40,3 +40,15 @@ def connect(*args, **kwargs) -> "Connection":
4040
from .connection import Connection
4141

4242
return Connection(*args, **kwargs)
43+
44+
45+
# SQLAlchemy integration (optional)
46+
# For SQLAlchemy functionality, import from pymongosql.sqlalchemy_mongodb:
47+
# from pymongosql.sqlalchemy_mongodb import create_engine_url, create_engine_from_mongodb_uri
48+
try:
49+
from .sqlalchemy_mongodb import __sqlalchemy_version__, __supports_sqlalchemy_2x__, __supports_sqlalchemy__
50+
except ImportError:
51+
# SQLAlchemy integration not available
52+
__sqlalchemy_version__ = None
53+
__supports_sqlalchemy__ = False
54+
__supports_sqlalchemy_2x__ = False

0 commit comments

Comments
 (0)