database-wrapper-mssql 0.2.7__tar.gz → 0.2.9__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 (16) hide show
  1. {database_wrapper_mssql-0.2.7 → database_wrapper_mssql-0.2.9}/PKG-INFO +2 -2
  2. {database_wrapper_mssql-0.2.7 → database_wrapper_mssql-0.2.9}/database_wrapper_mssql/__init__.py +2 -0
  3. database_wrapper_mssql-0.2.9/database_wrapper_mssql/mssql_introspector.py +109 -0
  4. database_wrapper_mssql-0.2.9/database_wrapper_mssql/type_mapping.py +66 -0
  5. {database_wrapper_mssql-0.2.7 → database_wrapper_mssql-0.2.9}/database_wrapper_mssql.egg-info/PKG-INFO +2 -2
  6. {database_wrapper_mssql-0.2.7 → database_wrapper_mssql-0.2.9}/database_wrapper_mssql.egg-info/SOURCES.txt +2 -0
  7. database_wrapper_mssql-0.2.9/database_wrapper_mssql.egg-info/requires.txt +2 -0
  8. {database_wrapper_mssql-0.2.7 → database_wrapper_mssql-0.2.9}/pyproject.toml +2 -2
  9. database_wrapper_mssql-0.2.7/database_wrapper_mssql.egg-info/requires.txt +0 -2
  10. {database_wrapper_mssql-0.2.7 → database_wrapper_mssql-0.2.9}/README.md +0 -0
  11. {database_wrapper_mssql-0.2.7 → database_wrapper_mssql-0.2.9}/database_wrapper_mssql/connector.py +0 -0
  12. {database_wrapper_mssql-0.2.7 → database_wrapper_mssql-0.2.9}/database_wrapper_mssql/db_wrapper_mssql.py +0 -0
  13. {database_wrapper_mssql-0.2.7 → database_wrapper_mssql-0.2.9}/database_wrapper_mssql/py.typed +0 -0
  14. {database_wrapper_mssql-0.2.7 → database_wrapper_mssql-0.2.9}/database_wrapper_mssql.egg-info/dependency_links.txt +0 -0
  15. {database_wrapper_mssql-0.2.7 → database_wrapper_mssql-0.2.9}/database_wrapper_mssql.egg-info/top_level.txt +0 -0
  16. {database_wrapper_mssql-0.2.7 → database_wrapper_mssql-0.2.9}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: database_wrapper_mssql
3
- Version: 0.2.7
3
+ Version: 0.2.9
4
4
  Summary: database_wrapper for MSSQL database
5
5
  Author-email: Gints Murans <gm@gm.lv>
6
6
  License: GNU General Public License v3.0 (GPL-3.0)
@@ -32,7 +32,7 @@ Classifier: Topic :: Software Development
32
32
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
33
33
  Requires-Python: >=3.8
34
34
  Description-Content-Type: text/markdown
35
- Requires-Dist: database_wrapper==0.2.7
35
+ Requires-Dist: database_wrapper==0.2.9
36
36
  Requires-Dist: pymssql>=2.2.10
37
37
 
38
38
  # database_wrapper_mssql
@@ -10,6 +10,7 @@ import logging
10
10
 
11
11
  from .db_wrapper_mssql import DBWrapperMSSQL
12
12
  from .connector import MsConfig, MSSQL, MssqlConnection, MssqlCursor
13
+ from .mssql_introspector import MSSQLIntrospector
13
14
 
14
15
  # Set the logger to a quiet default, can be enabled if needed
15
16
  logger = logging.getLogger("database_wrapper_mssql")
@@ -27,4 +28,5 @@ __all__ = [
27
28
  "MssqlCursor",
28
29
  # Helpers
29
30
  "MsConfig",
31
+ "MSSQLIntrospector",
30
32
  ]
@@ -0,0 +1,109 @@
1
+ import re
2
+ from typing import Any
3
+ import pyodbc # pip install pyodbc
4
+
5
+ from database_wrapper import ColumnMetaIntrospector, DBIntrospector
6
+ from .type_mapping import map_db_type # your existing mapper
7
+
8
+
9
+ class MSSQLIntrospector(DBIntrospector):
10
+ conn: pyodbc.Connection
11
+
12
+ def map_db_type(self, db_type: str):
13
+ return map_db_type(db_type)
14
+
15
+ def get_table_columns(self, schema: str, table: str) -> list[ColumnMetaIntrospector]:
16
+ """
17
+ Returns ColumnMetaIntrospector for SQL Server:
18
+ - col_name
19
+ - db_type (sys.types.name; e.g., int, bigint, nvarchar, datetime2, bit, numeric, etc.)
20
+ - is_nullable
21
+ - has_default + default_expr (sys.default_constraints.definition)
22
+ - enum_labels (best-effort from CHECK ... IN ('a','b',...))
23
+ """
24
+ # Main column metadata
25
+ q = """
26
+ SELECT
27
+ c.name AS col_name,
28
+ t.name AS typname,
29
+ c.is_nullable AS is_nullable,
30
+ dc.definition AS default_expr,
31
+ c.column_id AS column_id,
32
+ c.max_length AS max_length,
33
+ c.precision AS precision,
34
+ c.scale AS scale
35
+ FROM sys.columns c
36
+ JOIN sys.tables tb ON tb.object_id = c.object_id
37
+ JOIN sys.schemas s ON s.schema_id = tb.schema_id
38
+ JOIN sys.types t ON t.user_type_id = c.user_type_id
39
+ LEFT JOIN sys.default_constraints dc
40
+ ON dc.parent_object_id = c.object_id
41
+ AND dc.parent_column_id = c.column_id
42
+ WHERE s.name = %s AND tb.name = %s
43
+ ORDER BY c.column_id;
44
+ """
45
+
46
+ # Enum-like labels via CHECK constraints (best-effort)
47
+ # We collect constraint definitions per column and later parse IN ('a','b',...)
48
+ q_checks = """
49
+ SELECT
50
+ c.name AS col_name,
51
+ cc.definition AS definition
52
+ FROM sys.check_constraints cc
53
+ JOIN sys.tables tb ON tb.object_id = cc.parent_object_id
54
+ JOIN sys.schemas s ON s.schema_id = tb.schema_id
55
+ JOIN sys.columns c ON c.object_id = cc.parent_object_id
56
+ WHERE s.name = %s AND tb.name = %s
57
+ AND cc.definition LIKE '% IN (%'
58
+ """
59
+
60
+ with self.conn.cursor() as cur:
61
+ cur.execute(q, (schema, table))
62
+ col_rows = cur.fetchall()
63
+
64
+ cur.execute(q_checks, (schema, table))
65
+ check_rows = cur.fetchall()
66
+
67
+ # Build enum-like labels per column by parsing CHECK defs that reference that column
68
+ # Example def: ([Status] IN ('New','Closed','On Hold'))
69
+ col_to_enum_labels: dict[str, list[str]] = {}
70
+ in_clause_re = re.compile(r"\bIN\s*\((.*?)\)", re.IGNORECASE | re.DOTALL)
71
+ quoted_re = re.compile(r"'((?:''|[^'])*)'")
72
+
73
+ for r in check_rows:
74
+ col_name = r["col_name"]
75
+ definition = r["definition"] or ""
76
+ # Heuristic: ensure constraint references the column (with or without brackets)
77
+ if f"[{col_name}]" not in definition and col_name not in definition:
78
+ continue
79
+ m = in_clause_re.search(definition)
80
+ if not m:
81
+ continue
82
+ inner = m.group(1)
83
+ labels = [q.replace("''", "'") for q in quoted_re.findall(inner)]
84
+ if labels:
85
+ # keep unique, stable
86
+ seen = set()
87
+ uniq = [x for x in labels if not (x in seen or seen.add(x))]
88
+ col_to_enum_labels[col_name] = uniq
89
+
90
+ out: list[ColumnMetaIntrospector] = []
91
+ for row in col_rows:
92
+ col_name = row["col_name"]
93
+ typname = row["typname"] # short type token
94
+ is_nullable = bool(row["is_nullable"])
95
+ default_expr = row["default_expr"]
96
+ enum_labels = col_to_enum_labels.get(col_name)
97
+
98
+ out.append(
99
+ ColumnMetaIntrospector(
100
+ col_name=col_name,
101
+ db_type=typname,
102
+ is_nullable=is_nullable,
103
+ has_default=(default_expr is not None),
104
+ default_expr=default_expr,
105
+ enum_labels=enum_labels,
106
+ )
107
+ )
108
+
109
+ return out
@@ -0,0 +1,66 @@
1
+ # type_mapping_mssql.py
2
+ from typing import Any, Optional, Tuple
3
+ from decimal import Decimal
4
+ import datetime
5
+ from database_wrapper import SerializeType
6
+
7
+ USE_DECIMAL = False
8
+
9
+ _MSSQL_TO_PY_BASE: dict[str, Tuple[type, Optional[SerializeType]]] = {
10
+ # integers
11
+ "tinyint": (int, None), # 0..255 (note!)
12
+ "smallint": (int, None),
13
+ "int": (int, None),
14
+ "bigint": (int, None),
15
+ # floats
16
+ "float": (float, None), # double precision
17
+ "real": (float, None),
18
+ # bits
19
+ "bit": (bool, None),
20
+ # character
21
+ "char": (str, None),
22
+ "nchar": (str, None),
23
+ "varchar": (str, None),
24
+ "nvarchar": (str, None),
25
+ "text": (str, None),
26
+ "ntext": (str, None),
27
+ # binary
28
+ "binary": (bytes, None),
29
+ "varbinary": (bytes, None),
30
+ "image": (bytes, None),
31
+ # temporal
32
+ "date": (datetime.date, SerializeType.DATE),
33
+ "time": (datetime.time, SerializeType.TIME),
34
+ "smalldatetime": (datetime.datetime, SerializeType.DATETIME),
35
+ "datetime": (datetime.datetime, SerializeType.DATETIME),
36
+ "datetime2": (datetime.datetime, SerializeType.DATETIME),
37
+ "datetimeoffset": (datetime.datetime, SerializeType.DATETIME), # has TZ info
38
+ # uniqueidentifier
39
+ "uniqueidentifier": (str, None), # or uuid.UUID if you prefer
40
+ }
41
+
42
+
43
+ def map_db_type(
44
+ db_type: str, *, length: int | None = None, precision: int | None = None, scale: int | None = None
45
+ ) -> tuple[type, Optional[SerializeType]]:
46
+ t = db_type.lower()
47
+
48
+ # decimal/numeric with precision/scale
49
+ if t in ("decimal", "numeric"):
50
+ if USE_DECIMAL:
51
+ return (Decimal, SerializeType.DECIMAL)
52
+ return (float, None)
53
+
54
+ # xml? treat as str (or dict with custom parser)
55
+ if t == "xml":
56
+ return (str, None)
57
+
58
+ # geography/geometry -> str (or custom WKT/WKB class if you have one)
59
+ if t in ("geography", "geometry"):
60
+ return (str, None)
61
+
62
+ # JSON in SQL Server isn’t a native type; teams store it in nvarchar(max) + ISJSON() checks.
63
+ # If you detect that at introspection-time, you can override to JSON here.
64
+ # e.g., if typ == nvarchar and col_has_isjson_flag: return (dict[str, Any], SerializeType.JSON)
65
+
66
+ return _MSSQL_TO_PY_BASE.get(t, (str, None))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: database_wrapper_mssql
3
- Version: 0.2.7
3
+ Version: 0.2.9
4
4
  Summary: database_wrapper for MSSQL database
5
5
  Author-email: Gints Murans <gm@gm.lv>
6
6
  License: GNU General Public License v3.0 (GPL-3.0)
@@ -32,7 +32,7 @@ Classifier: Topic :: Software Development
32
32
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
33
33
  Requires-Python: >=3.8
34
34
  Description-Content-Type: text/markdown
35
- Requires-Dist: database_wrapper==0.2.7
35
+ Requires-Dist: database_wrapper==0.2.9
36
36
  Requires-Dist: pymssql>=2.2.10
37
37
 
38
38
  # database_wrapper_mssql
@@ -3,7 +3,9 @@ pyproject.toml
3
3
  database_wrapper_mssql/__init__.py
4
4
  database_wrapper_mssql/connector.py
5
5
  database_wrapper_mssql/db_wrapper_mssql.py
6
+ database_wrapper_mssql/mssql_introspector.py
6
7
  database_wrapper_mssql/py.typed
8
+ database_wrapper_mssql/type_mapping.py
7
9
  database_wrapper_mssql.egg-info/PKG-INFO
8
10
  database_wrapper_mssql.egg-info/SOURCES.txt
9
11
  database_wrapper_mssql.egg-info/dependency_links.txt
@@ -0,0 +1,2 @@
1
+ database_wrapper==0.2.9
2
+ pymssql>=2.2.10
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "database_wrapper_mssql"
7
- version = "0.2.7"
7
+ version = "0.2.9"
8
8
  description = "database_wrapper for MSSQL database"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.8"
@@ -35,7 +35,7 @@ classifiers = [
35
35
  ]
36
36
  keywords = ["database", "wrapper", "python", "mssql", "sqlserver"]
37
37
  dependencies = [
38
- "database_wrapper == 0.2.7",
38
+ "database_wrapper == 0.2.9",
39
39
  "pymssql >= 2.2.10",
40
40
  ]
41
41
 
@@ -1,2 +0,0 @@
1
- database_wrapper==0.2.7
2
- pymssql>=2.2.10