@@ -52,100 +52,23 @@ def create_engine_url(
5252 >>> url = create_engine_url("localhost", 27017, "mydb", mode="superset")
5353 >>> engine = sqlalchemy.create_engine(url)
5454 """
55- scheme = "mongodb+superset" if mode == "superset" else "mongodb "
55+ scheme = "mongodb"
5656
5757 params = []
5858 for key , value in kwargs .items ():
5959 params .append (f"{ key } ={ value } " )
6060
61+ # Add mode parameter if not standard
62+ if mode != "standard" :
63+ params .append (f"mode={ mode } " )
64+
6165 param_str = "&" .join (params )
6266 if param_str :
6367 param_str = "?" + param_str
6468
6569 return f"{ scheme } ://{ host } :{ port } /{ database } { param_str } "
6670
6771
68- def create_mongodb_url (mongodb_uri : str ) -> str :
69- """Convert a standard MongoDB URI to work with PyMongoSQL SQLAlchemy dialect.
70-
71- Args:
72- mongodb_uri: Standard MongoDB connection string
73- (e.g., 'mongodb://localhost:27017/mydb' or 'mongodb+srv://...')
74-
75- Returns:
76- SQLAlchemy-compatible URL for PyMongoSQL
77-
78- Example:
79- >>> url = create_mongodb_url("mongodb://user:pass@localhost:27017/mydb")
80- >>> engine = sqlalchemy.create_engine(url)
81- """
82- # Return the MongoDB URI as-is since the dialect now handles MongoDB URLs directly
83- return mongodb_uri
84-
85-
86- def create_engine_from_mongodb_uri (mongodb_uri : str , ** engine_kwargs ):
87- """Create a SQLAlchemy engine from any MongoDB connection string.
88-
89- This function handles mongodb://, mongodb+srv://, and mongodb+superset:// URIs properly.
90- Use this instead of create_engine() directly for special URI schemes.
91-
92- Args:
93- mongodb_uri: MongoDB connection string (supports standard, SRV, and superset modes)
94- **engine_kwargs: Additional arguments passed to create_engine
95-
96- Returns:
97- SQLAlchemy Engine object
98-
99- Example:
100- >>> # For SRV records (Atlas/Cloud)
101- >>> engine = create_engine_from_mongodb_uri("mongodb+srv://user:pass@cluster.net/db")
102- >>> # For standard MongoDB
103- >>> engine = create_engine_from_mongodb_uri("mongodb://localhost:27017/mydb")
104- >>> # For superset mode (with subquery support)
105- >>> engine = create_engine_from_mongodb_uri("mongodb+superset://localhost:27017/mydb")
106- """
107- try :
108- from sqlalchemy import create_engine
109-
110- if mongodb_uri .startswith ("mongodb+srv://" ):
111- # For MongoDB+SRV, convert to standard mongodb:// for SQLAlchemy compatibility
112- # SQLAlchemy doesn't handle the + character in scheme names well
113- converted_uri = mongodb_uri .replace ("mongodb+srv://" , "mongodb://" )
114-
115- # Create engine with converted URI
116- engine = create_engine (converted_uri , ** engine_kwargs )
117-
118- def custom_create_connect_args (url ):
119- # Use original SRV URI for actual MongoDB connection
120- opts = {"host" : mongodb_uri }
121- return [], opts
122-
123- engine .dialect .create_connect_args = custom_create_connect_args
124- return engine
125- elif mongodb_uri .startswith ("mongodb+superset://" ):
126- # For MongoDB+Superset, convert to standard mongodb:// for SQLAlchemy compatibility
127- # but preserve the superset mode by passing it through connection options
128- converted_uri = mongodb_uri .replace ("mongodb+superset://" , "mongodb://" )
129-
130- # Create engine with converted URI
131- engine = create_engine (converted_uri , ** engine_kwargs )
132-
133- def custom_create_connect_args (url ):
134- # Use original superset URI for actual MongoDB connection
135- # This preserves the superset mode for subquery support
136- opts = {"host" : mongodb_uri }
137- return [], opts
138-
139- engine .dialect .create_connect_args = custom_create_connect_args
140- return engine
141- else :
142- # Standard mongodb:// URLs work fine with SQLAlchemy
143- return create_engine (mongodb_uri , ** engine_kwargs )
144-
145- except ImportError :
146- raise ImportError ("SQLAlchemy is required for engine creation" )
147-
148-
14972def register_dialect ():
15073 """Register the PyMongoSQL dialect with SQLAlchemy.
15174
@@ -166,20 +89,7 @@ def register_dialect():
16689 registry .register ("mongodb+srv" , "pymongosql.sqlalchemy_mongodb.sqlalchemy_dialect" , "PyMongoSQLDialect" )
16790 registry .register ("mongodb.srv" , "pymongosql.sqlalchemy_mongodb.sqlalchemy_dialect" , "PyMongoSQLDialect" )
16891 except Exception :
169- # If registration fails we fall back to handling SRV URIs in
170- # create_engine_from_mongodb_uri by converting 'mongodb+srv' to 'mongodb'.
171- pass
172-
173- try :
174- registry .register (
175- "mongodb+superset" , "pymongosql.sqlalchemy_mongodb.sqlalchemy_dialect" , "PyMongoSQLDialect"
176- )
177- registry .register (
178- "mongodb.superset" , "pymongosql.sqlalchemy_mongodb.sqlalchemy_dialect" , "PyMongoSQLDialect"
179- )
180- except Exception :
181- # If registration fails we fall back to handling Superset URIs in
182- # create_engine_from_mongodb_uri by converting 'mongodb+superset' to 'mongodb'.
92+ # If registration fails, users can convert URIs to standard mongodb:// format
18393 pass
18494
18595 return True
@@ -197,8 +107,6 @@ def register_dialect():
197107# Export all SQLAlchemy-related functionality
198108__all__ = [
199109 "create_engine_url" ,
200- "create_mongodb_url" ,
201- "create_engine_from_mongodb_uri" ,
202110 "register_dialect" ,
203111 "__sqlalchemy_version__" ,
204112 "__supports_sqlalchemy__" ,
0 commit comments