mongo-2-sql 0.1.3__tar.gz → 0.1.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/LICENSE +2 -2
  2. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/PKG-INFO +112 -3
  3. mongo_2_sql-0.1.3/src/mongo_2_sql.egg-info/PKG-INFO → mongo_2_sql-0.1.5/README.md +577 -494
  4. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/pyproject.toml +9 -2
  5. mongo_2_sql-0.1.5/src/mongo_2_sql/core/cte_manager.py +72 -0
  6. mongo_2_sql-0.1.5/src/mongo_2_sql/core/cte_stage_base.py +101 -0
  7. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/core/expr_base.py +4 -4
  8. mongo_2_sql-0.1.5/src/mongo_2_sql/core/expr_parser_base.py +358 -0
  9. mongo_2_sql-0.1.5/src/mongo_2_sql/core/expression_resolver.py +101 -0
  10. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/core/stage_base.py +17 -15
  11. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/core/stage_loader.py +1 -0
  12. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/main.py +1 -1
  13. mongo_2_sql-0.1.5/src/mongo_2_sql/renderer/sqlite.py +234 -0
  14. mongo_2_sql-0.1.5/src/mongo_2_sql/stages/add_fields/__init__.py +9 -0
  15. mongo_2_sql-0.1.5/src/mongo_2_sql/stages/add_fields/expr.py +220 -0
  16. mongo_2_sql-0.1.5/src/mongo_2_sql/stages/add_fields/stage.py +137 -0
  17. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/group/expr.py +7 -41
  18. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/group/stage.py +57 -12
  19. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/match/expr.py +9 -40
  20. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/project/expr.py +174 -62
  21. mongo_2_sql-0.1.5/src/mongo_2_sql/stages/project/stage.py +198 -0
  22. mongo_2_sql-0.1.3/README.md → mongo_2_sql-0.1.5/src/mongo_2_sql.egg-info/PKG-INFO +603 -473
  23. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql.egg-info/SOURCES.txt +8 -1
  24. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql.egg-info/requires.txt +1 -0
  25. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/tests/test.py +2 -1
  26. mongo_2_sql-0.1.5/tests/test1.py +49 -0
  27. mongo_2_sql-0.1.3/src/mongo_2_sql/renderer/sqlite.py +0 -131
  28. mongo_2_sql-0.1.3/src/mongo_2_sql/stages/project/stage.py +0 -85
  29. mongo_2_sql-0.1.3/tests/test_all_examples.py +0 -133
  30. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/setup.cfg +0 -0
  31. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/__init__.py +0 -0
  32. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/converter.py +0 -0
  33. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/core/__init__.py +0 -0
  34. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/core/ast_base.py +0 -0
  35. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/core/expr_registry.py +0 -0
  36. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/core/pipeline_runtime.py +0 -0
  37. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/renderer/__init__.py +0 -0
  38. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/__init__.py +0 -0
  39. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/group/__init__.py +0 -0
  40. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/limit/__init__.py +0 -0
  41. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/limit/stage.py +0 -0
  42. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/lookup/__init__.py +0 -0
  43. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/lookup/stage.py +0 -0
  44. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/match/__init__.py +0 -0
  45. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/match/stage.py +0 -0
  46. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/project/__init__.py +0 -0
  47. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/sort/__init__.py +0 -0
  48. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/sort/stage.py +0 -0
  49. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/unwind/__init__.py +0 -0
  50. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/stages/unwind/stage.py +0 -0
  51. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql/utils.py +0 -0
  52. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql.egg-info/dependency_links.txt +0 -0
  53. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql.egg-info/entry_points.txt +0 -0
  54. {mongo_2_sql-0.1.3 → mongo_2_sql-0.1.5}/src/mongo_2_sql.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2026 Bao Bingbo
3
+ Copyright (c) 2026 baobingbo
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
@@ -1,9 +1,13 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mongo-2-sql
3
- Version: 0.1.3
3
+ Version: 0.1.5
4
4
  Summary: A library to convert MongoDB aggregation pipelines to SQL parser
5
5
  Author-email: Bao Bingbo <baob2@outlook.com>
6
6
  License: MIT License
7
+ Project-URL: Homepage, https://github.com/baobingbo/mongo-2-sql
8
+ Project-URL: Documentation, https://github.com/baobingbo/mongo-2-sql#readme
9
+ Project-URL: Repository, https://github.com/baobingbo/mongo-2-sql.git
10
+ Project-URL: Issues, https://github.com/baobingbo/mongo-2-sql/issues
7
11
  Classifier: Development Status :: 4 - Beta
8
12
  Classifier: Intended Audience :: Developers
9
13
  Classifier: License :: OSI Approved :: MIT License
@@ -17,12 +21,29 @@ Requires-Python: >=3.10
17
21
  Description-Content-Type: text/markdown
18
22
  License-File: LICENSE
19
23
  Requires-Dist: typing-extensions>=4.0.0; python_version < "3.10"
24
+ Requires-Dist: sqlparse>=0.5.5
20
25
  Dynamic: license-file
21
26
 
22
27
  # Mongo to SQL
23
28
 
24
29
  Mongo to SQL 是一个将 MongoDB 聚合管道转换为 SQL 查询语句的工具。
25
30
 
31
+ ## 项目仓库
32
+
33
+ - **GitHub**: [baobingbo/mongo-2-sql](https://github.com/baobingbo/mongo-2-sql)
34
+
35
+ ## 版本更新 v0.1.5
36
+
37
+ ### 新增功能
38
+
39
+ #### 1. $addFields 阶段表达式与字面量语义
40
+ - `$addFields` 使用独立的 `AddFieldsExpressionParser`,不再复用 `$project` 的解析逻辑,避免跨阶段耦合
41
+ - `$addFields` 支持三类值:
42
+ - **字面量值**: 如 `{"score": 1}`, `{ "flag": true }`, `{ "text": "hello" }`,会直接转换为 `1 AS score`、`TRUE AS flag`、`'hello' AS text`
43
+ - **字段引用**: 如 `{ "cat": "$category" }`,会转换为 `t.category AS cat`
44
+ - **表达式**: 如 `{ "total": { "$add": [1, "$price"] } }` 或 `{ "isBig": { "$gt": ["$amount", 100] } }`
45
+ - **仅 `$project` 阶段中**,`{"field": 1}` / `{"field": 0}` 仍然表示“包含/排除字段”;其他阶段(包括 `$addFields`)中,`1` 始终被视为字面量数值 1
46
+
26
47
  ## 功能特性
27
48
 
28
49
  - **阶段基础方法支持**: 支持 `$match`, `$project`, `$group`, `$sort`, `$limit`, `$lookup`, `$unwind` 等常用阶段
@@ -111,6 +132,52 @@ ORDER BY count DESC
111
132
  LIMIT 10
112
133
  ```
113
134
 
135
+ ### CTE 处理(复杂聚合管道)
136
+
137
+ 当管道包含多次 $project、$group 或 $addFields 阶段时,系统会自动使用 CTE 方式处理:
138
+
139
+ ```python
140
+ from mongo_2_sql import convert_mongo_pipeline_to_sql
141
+
142
+ # 复杂管道:多次投影和分组
143
+ pipeline = [
144
+ { "$match": { "status": "active", "age": { "$gte": 18 } } },
145
+ { "$project": { "name": 1, "category": 1, "amount": 1 } },
146
+ { "$group": { "_id": "$category", "totalAmount": { "$sum": "$amount" }, "count": { "$sum": 1 } } },
147
+ { "$project": { "_id": 1, "totalAmount": 1, "count": 1 } },
148
+ { "$match": { "totalAmount": { "$gte": 18 } } },
149
+ { "$sort": { "totalAmount": -1 } },
150
+ { "$limit": 5 }
151
+ ]
152
+
153
+ sql, metadata = convert_mongo_pipeline_to_sql(pipeline, "transactions")
154
+ print(sql)
155
+ ```
156
+
157
+ 输出(自动使用 CTE 格式化):
158
+ ```sql
159
+ WITH cte_1 AS
160
+ (SELECT t.category AS _id,
161
+ SUM(t.amount) AS totalAmount,
162
+ COUNT(*) AS COUNT
163
+ FROM transactions AS t
164
+ WHERE 1=1
165
+ AND (t.status = 'active')
166
+ AND (t.age >= 18)
167
+ GROUP BY t.category),
168
+ cte_2 AS
169
+ (SELECT t._id AS _id,
170
+ t.totalAmount AS totalAmount,
171
+ t.count AS COUNT
172
+ FROM cte_1 AS t)
173
+ SELECT *
174
+ FROM cte_2 AS t
175
+ WHERE 1=1
176
+ AND (t.totalAmount >= 18)
177
+ ORDER BY t.totalAmount DESC
178
+ LIMIT 5
179
+ ```
180
+
114
181
  ### 面向对象用法
115
182
 
116
183
  ```python
@@ -297,7 +364,27 @@ FROM products AS t
297
364
  GROUP BY t.category
298
365
  ```
299
366
 
300
- ### 示例 5: 字符串连接
367
+ ### 示例 5: 字段重命名
368
+
369
+ ```python
370
+ pipeline = [
371
+ {
372
+ "$project": {
373
+ "product_name": "$name",
374
+ "total_value": { "$multiply": ["$price", "$quantity"] }
375
+ }
376
+ }
377
+ ]
378
+ ```
379
+
380
+ ```sql
381
+ SELECT
382
+ t.name AS product_name,
383
+ (t.price * t.quantity) AS total_value
384
+ FROM products AS t
385
+ ```
386
+
387
+ ### 示例 6: 字符串连接
301
388
 
302
389
  ```python
303
390
  pipeline = [
@@ -319,7 +406,7 @@ SELECT
319
406
  FROM users AS t
320
407
  ```
321
408
 
322
- ### 示例 6: JOIN 查询
409
+ ### 示例 7: JOIN 查询
323
410
 
324
411
  ```python
325
412
  pipeline = [
@@ -479,6 +566,28 @@ MIT License - 详见 [LICENSE](LICENSE) 文件
479
566
 
480
567
  ## 变更日志
481
568
 
569
+ ### v0.1.5 (2025-02-23)
570
+
571
+ **新增功能:**
572
+ - addFields阶段独立解析器(AddFieldsExpressionParser),与 project 解耦
573
+ - 支持字面量值、字段引用、表达式三类语义
574
+ - 统一数值 1 为字面量(仅 $project 阶段保留包含/排除语义)
575
+
576
+ ### v0.1.4 (2025-02-13)
577
+
578
+ **新增功能:**
579
+ - CTE 统一处理框架,支持 $addFields、$project、$group 阶段切片
580
+ - ExpressionResolver 统一表达式解析器,避免 stage 间耦合
581
+ - BaseExpressionParser 基础解析器类,减少代码重复
582
+ - SQL 格式化输出(使用 sqlparse)
583
+ - Project 阶段字段重命名支持
584
+ - Stage 解耦架构设计
585
+
586
+ **问题修复:**
587
+ - 修复 `_id` 字段双引号问题
588
+ - 修复聚合后字段引用错误
589
+ - 修复模块导入路径问题
590
+
482
591
  ### v0.1.3 (2025-02-07)
483
592
 
484
593
  **改进:**