Issue #2875 resolved

attempt to improve the system by which engine_from_config locates keys w/ special values

Michael Bayer
repo owner created an issue

without resorting to just detecting ints, "True"/ "False" etc., if dialects could somehow register additional keywords to be used by the _coerce_config() method, that would help a lot. I think just let them register their words and we kind of assume two dialects either don't use the same kw, or if they do, they have the same type. We're dealing mostly with booleans, should be OK.

Comments (3)

  1. Michael Bayer reporter

    add it to the default strategy once we have the dialect class:

    diff --git a/lib/sqlalchemy/engine/__init__.py b/lib/sqlalchemy/engine/__init__.py
    index 16d2141..128c4e8 100644
    --- a/lib/sqlalchemy/engine/__init__.py
    +++ b/lib/sqlalchemy/engine/__init__.py
    @@ -348,10 +348,13 @@ def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs):
         arguments.
         """
    
    -    opts = util._coerce_config(configuration, prefix)
    -    opts.update(kwargs)
    -    url = opts.pop('url')
    -    return create_engine(url, **opts)
    +    options = dict((key[len(prefix):](len(prefix):), configuration[key](key))
    +                   for key in configuration
    +                   if key.startswith(prefix))
    +    options['_coerce_config']('_coerce_config') = True
    +    options.update(kwargs)
    +    url = options.pop('url')
    +    return create_engine(url, **options)
    
    
     __all__ = (
    diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py
    index 8fb7c3b..007a3ab 100644
    --- a/lib/sqlalchemy/engine/default.py
    +++ b/lib/sqlalchemy/engine/default.py
    @@ -59,6 +59,18 @@ class DefaultDialect(interfaces.Dialect):
    
         supports_simple_order_by_label = True
    
    +    engine_config_types = dict([       ('convert_unicode', util.bool_or_str('force')),
    +        ('pool_timeout', int),
    +        ('echo', util.bool_or_str('debug')),
    +        ('echo_pool', util.bool_or_str('debug')),
    +        ('pool_recycle', int),
    +        ('pool_size', int),
    +        ('max_overflow', int),
    +        ('pool_threadlocal', bool),
    +        ('use_native_unicode', bool),
    +    ](
    +))
    +
         # if the NUMERIC type
         # returns decimal.Decimal.
         # *not* the FLOAT type however.
    diff --git a/lib/sqlalchemy/engine/strategies.py b/lib/sqlalchemy/engine/strategies.py
    index ab9d370..aea2fc3 100644
    --- a/lib/sqlalchemy/engine/strategies.py
    +++ b/lib/sqlalchemy/engine/strategies.py
    @@ -49,11 +49,16 @@ class DefaultEngineStrategy(EngineStrategy):
    
             dialect_cls = u.get_dialect()
    
    +        coerce_config = kwargs.pop('_coerce_config', False)
    +
             dialect_args = {}
             # consume dialect arguments from kwargs
             for k in util.get_cls_kwargs(dialect_cls):
                 if k in kwargs:
    -                dialect_args[k](k) = kwargs.pop(k)
    +                value = kwargs.pop(k)
    +                if coerce_config and k in dialect_cls.engine_config_types:
    +                    value = dialect_cls.engine_config_types[k](k)(value)
    +                dialect_args[k](k) = value
    
             dbapi = kwargs.pop('module', None)
             if dbapi is None:
    
  2. Log in to comment