]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Promoted mysql's dburl query string helper to util + fixed
authorJason Kirtland <jek@discorporate.us>
Thu, 19 Apr 2007 23:44:26 +0000 (23:44 +0000)
committerJason Kirtland <jek@discorporate.us>
Thu, 19 Apr 2007 23:44:26 +0000 (23:44 +0000)
- Coercing sqlite connect args provided in query string to their expected type
  (e.g. 'timeout' as float, fixes #544)
- Coerce mysql's client_flag to int too

lib/sqlalchemy/databases/mysql.py
lib/sqlalchemy/databases/sqlite.py
lib/sqlalchemy/util.py

index 21f8bb3984fa97873048181c81382ac7913b2357..52aa03003d36365876f330ddd081535047a50ae7 100644 (file)
@@ -10,6 +10,7 @@ from sqlalchemy import sql,engine,schema,ansisql
 from sqlalchemy.engine import default
 import sqlalchemy.types as sqltypes
 import sqlalchemy.exceptions as exceptions
+import sqlalchemy.util as util
 from array import array
 
 
@@ -285,16 +286,13 @@ class MySQLDialect(ansisql.ANSIDialect):
     def create_connect_args(self, url):
         opts = url.translate_connect_args(['host', 'db', 'user', 'passwd', 'port'])
         opts.update(url.query)
-        def coercetype(param, type):
-            if param in opts and type(param) is not type:
-                if type is bool:
-                    opts[param] = bool(int(opts[param]))
-                else:
-                    opts[param] = type(opts[param])
-        coercetype('compress', bool)
-        coercetype('connect_timeout', int)
-        coercetype('use_unicode', bool)   # this could break SA Unicode type
-        coercetype('charset', str)        # this could break SA Unicode type
+
+        util.coerce_kw_type(opts, 'compress', bool)
+        util.coerce_kw_type(opts, 'connect_timeout', int)
+        util.coerce_kw_type(opts, 'client_flag', int)
+        # note: these two could break SA Unicode type
+        util.coerce_kw_type(opts, 'use_unicode', bool)   
+        util.coerce_kw_type(opts, 'charset', str)
         # TODO: what about options like "ssl", "cursorclass" and "conv" ?
 
         client_flag = opts.get('client_flag', 0)
index 0222496f83719b9c0a8aa1aa016d8c4decc41963..6c12345428120affdcf157e34482955f1b050478 100644 (file)
@@ -10,6 +10,7 @@ import sys, StringIO, string, types, re
 from sqlalchemy import sql, engine, schema, ansisql, exceptions, pool, PassiveDefault
 import sqlalchemy.engine.default as default
 import sqlalchemy.types as sqltypes
+import sqlalchemy.util as util
 import datetime,time
 
     
@@ -179,7 +180,15 @@ class SQLiteDialect(ansisql.ANSIDialect):
 
     def create_connect_args(self, url):
         filename = url.database or ':memory:'
-        return ([filename], url.query)
+
+        opts = url.query.copy()
+        util.coerce_kw_type(opts, 'timeout', float)
+        util.coerce_kw_type(opts, 'isolation_level', str)
+        util.coerce_kw_type(opts, 'detect_types', int)
+        util.coerce_kw_type(opts, 'check_same_thread', bool)
+        util.coerce_kw_type(opts, 'cached_statements', int)
+
+        return ([filename], opts)
 
     def type_descriptor(self, typeobj):
         return sqltypes.adapt_type(typeobj, colspecs)
index 238f12493fc8aaa2b0a38068989247eae60faa6b..08b281684cc91280a06b1fc16e301b82ac545465 100644 (file)
@@ -98,6 +98,17 @@ def get_func_kwargs(func):
     """Return the full set of legal kwargs for the given `func`."""
     return [vn for vn in func.func_code.co_varnames]
 
+def coerce_kw_type(kw, key, type_, flexi_bool=True):
+    """If 'key' is present in dict 'kw', coerce its value to type 'type_' if
+    necessary.  If 'flexi_bool' is True, the string '0' is considered false
+    when coercing to boolean.
+    """
+    if key in kw and type(kw[key]) is not type_ and kw[key] is not None:
+        if type_ is bool and flexi_bool and kw[key] == '0':
+            kw[key] = False
+        else:
+            kw[key] = type_(kw[key])
+
 class SimpleProperty(object):
     """A *default* property accessor."""