|
6 | 6 | if TYPE_CHECKING: |
7 | 7 | from .connection import Connection |
8 | 8 |
|
9 | | -__version__: str = "0.4.3" |
| 9 | +__version__: str = "0.4.4" |
10 | 10 |
|
11 | 11 | # Globals https://www.python.org/dev/peps/pep-0249/#globals |
12 | 12 | apilevel: str = "2.0" |
@@ -36,6 +36,115 @@ def __hash__(self): |
36 | 36 | return frozenset.__hash__(self) |
37 | 37 |
|
38 | 38 |
|
| 39 | +# DB API 2.0 Type Objects for MongoDB Data Types |
| 40 | +# https://www.python.org/dev/peps/pep-0249/#type-objects-and-constructors |
| 41 | +# Mapping of MongoDB BSON types to DB API 2.0 type objects |
| 42 | + |
| 43 | +# Null/None type |
| 44 | +NULL = DBAPITypeObject(("null", "Null", "NULL")) |
| 45 | + |
| 46 | +# String types |
| 47 | +STRING = DBAPITypeObject(("string", "str", "String", "VARCHAR", "CHAR", "TEXT")) |
| 48 | + |
| 49 | +# Numeric types - Integer |
| 50 | +BINARY = DBAPITypeObject(("binary", "Binary", "BINARY", "VARBINARY", "BLOB", "ObjectId")) |
| 51 | + |
| 52 | +# Numeric types - Integer |
| 53 | +NUMBER = DBAPITypeObject(("int", "integer", "long", "int32", "int64", "Integer", "BIGINT", "INT")) |
| 54 | + |
| 55 | +# Numeric types - Decimal/Float |
| 56 | +FLOAT = DBAPITypeObject(("double", "decimal", "float", "Double", "DECIMAL", "FLOAT", "NUMERIC")) |
| 57 | + |
| 58 | +# Boolean type |
| 59 | +BOOLEAN = DBAPITypeObject(("bool", "boolean", "Bool", "BOOLEAN")) |
| 60 | + |
| 61 | +# Date/Time types |
| 62 | +DATE = DBAPITypeObject(("date", "Date", "DATE")) |
| 63 | +TIME = DBAPITypeObject(("time", "Time", "TIME")) |
| 64 | +DATETIME = DBAPITypeObject(("datetime", "timestamp", "Timestamp", "DATETIME", "TIMESTAMP")) |
| 65 | + |
| 66 | +# Aggregate types |
| 67 | +ARRAY = DBAPITypeObject(("array", "Array", "ARRAY", "list")) |
| 68 | +OBJECT = DBAPITypeObject(("object", "Object", "OBJECT", "struct", "dict", "document")) |
| 69 | + |
| 70 | +# Special MongoDB types |
| 71 | +OBJECTID = DBAPITypeObject(("objectid", "ObjectId", "OBJECTID", "oid")) |
| 72 | +REGEX = DBAPITypeObject(("regex", "Regex", "REGEX", "regexp")) |
| 73 | + |
| 74 | +# Map MongoDB BSON type codes to DB API type objects |
| 75 | +# This mapping helps cursor.description identify the correct type for each column |
| 76 | +_MONGODB_TYPE_MAP = { |
| 77 | + "null": NULL, |
| 78 | + "string": STRING, |
| 79 | + "int": NUMBER, |
| 80 | + "integer": NUMBER, |
| 81 | + "long": NUMBER, |
| 82 | + "int32": NUMBER, |
| 83 | + "int64": NUMBER, |
| 84 | + "double": FLOAT, |
| 85 | + "decimal": FLOAT, |
| 86 | + "float": FLOAT, |
| 87 | + "bool": BOOLEAN, |
| 88 | + "boolean": BOOLEAN, |
| 89 | + "date": DATE, |
| 90 | + "datetime": DATETIME, |
| 91 | + "timestamp": DATETIME, |
| 92 | + "array": ARRAY, |
| 93 | + "object": OBJECT, |
| 94 | + "document": OBJECT, |
| 95 | + "bson.objectid": OBJECTID, |
| 96 | + "objectid": OBJECTID, |
| 97 | + "regex": REGEX, |
| 98 | + "binary": BINARY, |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +def get_type_code(value: object) -> str: |
| 103 | + """Get the type code for a MongoDB value. |
| 104 | +
|
| 105 | + Maps a MongoDB/Python value to its corresponding DB API type code string. |
| 106 | +
|
| 107 | + Args: |
| 108 | + value: The value to determine the type for |
| 109 | +
|
| 110 | + Returns: |
| 111 | + A string representing the DB API type code |
| 112 | + """ |
| 113 | + if value is None: |
| 114 | + return "null" |
| 115 | + elif isinstance(value, bool): |
| 116 | + return "bool" |
| 117 | + elif isinstance(value, int): |
| 118 | + return "int" |
| 119 | + elif isinstance(value, float): |
| 120 | + return "double" |
| 121 | + elif isinstance(value, str): |
| 122 | + return "string" |
| 123 | + elif isinstance(value, bytes): |
| 124 | + return "binary" |
| 125 | + elif isinstance(value, dict): |
| 126 | + return "object" |
| 127 | + elif isinstance(value, list): |
| 128 | + return "array" |
| 129 | + elif hasattr(value, "__class__") and value.__class__.__name__ == "ObjectId": |
| 130 | + return "objectid" |
| 131 | + else: |
| 132 | + return "object" |
| 133 | + |
| 134 | + |
| 135 | +def get_type_object(value: object) -> DBAPITypeObject: |
| 136 | + """Get the DB API type object for a MongoDB value. |
| 137 | +
|
| 138 | + Args: |
| 139 | + value: The value to get type information for |
| 140 | +
|
| 141 | + Returns: |
| 142 | + A DBAPITypeObject representing the value's type |
| 143 | + """ |
| 144 | + type_code = get_type_code(value) |
| 145 | + return _MONGODB_TYPE_MAP.get(type_code, OBJECT) |
| 146 | + |
| 147 | + |
39 | 148 | def connect(*args, **kwargs) -> "Connection": |
40 | 149 | from .connection import Connection |
41 | 150 |
|
|
0 commit comments