diff options
author | Nathan Wright <thatnateguy@gmail.com> | 2012-03-12 21:31:12 -0700 |
---|---|---|
committer | Nathan Wright <thatnateguy@gmail.com> | 2012-03-12 21:31:12 -0700 |
commit | 55b4295e39454430211a3c30cb8303a58a024f28 (patch) | |
tree | c1bd59a39d6b4dfb587d3675dac77791bed4058a /lib/sqlalchemy/processors.py | |
parent | 754e7290b46d96500aea52da76f7c1230cb46fb8 (diff) | |
download | sqlalchemy-55b4295e39454430211a3c30cb8303a58a024f28.tar.gz |
Improve SQLite DATETIME storage format handling [ticket:2363]
This breaks backwards compatibility with old SQLite DATETIME, DATE,
and TIME storage_format strings. Formatting now occurs with named instead
of positional parameters. The regexp argument can still use positional
arguments, but named groupings are also supported. This means that you can
omit fields and change the order of date fields as desired.
SQLite's DATETIME and TIME also gained a truncate_microseconds argument.
This is shorthand for modifying the format string. Fortunately the
str_to_datetime and str_to_time processors written in C already support
omitting microseconds, so we don't have to resort to python processing
for this case.
Diffstat (limited to 'lib/sqlalchemy/processors.py')
-rw-r--r-- | lib/sqlalchemy/processors.py | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/lib/sqlalchemy/processors.py b/lib/sqlalchemy/processors.py index c4bac2834..a3adbe293 100644 --- a/lib/sqlalchemy/processors.py +++ b/lib/sqlalchemy/processors.py @@ -20,6 +20,7 @@ def str_to_datetime_processor_factory(regexp, type_): rmatch = regexp.match # Even on python2.6 datetime.strptime is both slower than this code # and it does not support microseconds. + has_named_groups = bool(regexp.groupindex) def process(value): if value is None: return None @@ -32,7 +33,12 @@ def str_to_datetime_processor_factory(regexp, type_): if m is None: raise ValueError("Couldn't parse %s string: " "'%s'" % (type_.__name__ , value)) - return type_(*map(int, m.groups(0))) + if has_named_groups: + groups = m.groupdict(0) + return type_(**dict(zip(groups.iterkeys(), + map(int, groups.itervalues())))) + else: + return type_(*map(int, m.groups(0))) return process def boolean_to_int(value): |