Commits

Mike Bayer committed 1510e54

implemented query string support in db urls, gets sent to dialect **kwargs, [ticket:196]

  • Participants
  • Parent commits ebca0e0

Comments (0)

Files changed (4)

 - fix to docs, removed incorrect info that close() is unsafe to use
 with threadlocal strategy (its totally safe !)
 - create_engine() can take URLs as string or unicode [ticket:188]
+- firebird support !  thanks to James Ralston and Brad Clements for their
+efforts.
 
 0.2.1
 - "pool" argument to create_engine() properly propigates

lib/sqlalchemy/engine/strategies.py

         u = url.make_url(name_or_url)
         module = u.get_module()
 
-        dialect = module.dialect(**kwargs)
+        args = u.query.copy()
+        args.update(kwargs)
+        dialect = module.dialect(**args)
 
         poolargs = {}
         for key in (('echo_pool', 'echo'), ('pool_size', 'pool_size'), ('max_overflow', 'max_overflow'), ('poolclass', 'poolclass'), ('pool_timeout','timeout'), ('pool', 'pool')):
         poolargs['use_threadlocal'] = False
         provider = default.PoolConnectionProvider(dialect, u, **poolargs)
 
-        return base.ComposedSQLEngine(provider, dialect, **kwargs)
+        return base.ComposedSQLEngine(provider, dialect, **args)
 PlainEngineStrategy()
 
 class ThreadLocalEngineStrategy(EngineStrategy):
         u = url.make_url(name_or_url)
         module = u.get_module()
 
-        dialect = module.dialect(**kwargs)
+        args = u.query.copy()
+        args.update(kwargs)
+        dialect = module.dialect(**args)
 
         poolargs = {}
         for key in (('echo_pool', 'echo'), ('pool_size', 'pool_size'), ('max_overflow', 'max_overflow'), ('poolclass', 'poolclass'), ('pool_timeout','timeout'), ('pool', 'pool')):
         poolargs['use_threadlocal'] = True
         provider = threadlocal.TLocalConnectionProvider(dialect, u, **poolargs)
 
-        return threadlocal.TLEngine(provider, dialect, **kwargs)
+        return threadlocal.TLEngine(provider, dialect, **args)
 ThreadLocalEngineStrategy()
 
 

lib/sqlalchemy/engine/url.py

 import sqlalchemy.exceptions as exceptions
 
 class URL(object):
-    def __init__(self, drivername, username=None, password=None, host=None, port=None, database=None):
+    def __init__(self, drivername, username=None, password=None, host=None, port=None, database=None, query=None):
         self.drivername = drivername
         self.username = username
         self.password = password
         self.host = host
         self.port = port
         self.database= database
+        self.query = query or {}
     def __str__(self):
         s = self.drivername + "://"
         if self.username is not None:
             s += ':' + self.port
         if self.database is not None:
             s += '/' + self.database
+        if len(self.query):
+            keys = self.query.keys()
+            keys.sort()
+            s += '?' + "&".join("%s=%s" % (k, self.query[k]) for k in keys)
         return s
     def get_module(self):
         return getattr(__import__('sqlalchemy.databases.%s' % self.drivername).databases, self.drivername)
     m = pattern.match(name)
     if m is not None:
         (name, username, password, host, port, database) = m.group(1, 2, 3, 4, 5, 6)
-        opts = {'username':username,'password':password,'host':host,'port':port,'database':database}
+        if database is not None:
+            tokens = database.split(r"?", 2)
+            database = tokens[0]
+            query = (len(tokens) > 1 and dict( cgi.parse_qsl(tokens[1]) ) or None)
+        else:
+            query = None
+        opts = {'username':username,'password':password,'host':host,'port':port,'database':database, 'query':query}
         return URL(name, **opts)
     else:
         raise exceptions.ArgumentError("Could not parse rfc1738 URL from string '%s'" % name)

test/parseconnect.py

             'dbtype://username:password@127.0.0.1:1521',
             'dbtype://hostspec/database',
             'dbtype://hostspec',
+            'dbtype://hostspec/?arg1=val1&arg2=val2',
             'dbtype:///database',
             'dbtype:///:memory:',
             'dbtype:///foo/bar/im/a/file',
             'dbtype:///E:/work/src/LEM/db/hello.db',
+            'dbtype:///E:/work/src/LEM/db/hello.db?foo=bar&hoho=lala',
             'dbtype://',
             'dbtype://username:password@/db'
         ):