From fcfb112c15e229f00abb80919027c61158fa963d Mon Sep 17 00:00:00 2001 From: Mitch Gallman Date: Fri, 3 Sep 2021 12:54:39 -0400 Subject: [PATCH 1/7] Add trim_char_fields connection argumen --- sqlalchemy_ibmi/base.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sqlalchemy_ibmi/base.py b/sqlalchemy_ibmi/base.py index e66134b..030865b 100644 --- a/sqlalchemy_ibmi/base.py +++ b/sqlalchemy_ibmi/base.py @@ -47,6 +47,7 @@ * ``use_system_naming`` - If ``True``, the connection is set to use the System naming convention, otherwise it will use the SQL naming convention. Defaults to ``False``. +* ``trim_char_fields`` - If ``True``, all character fields will be returned with trailing spaces truncated. create-engine arguments: @@ -791,7 +792,7 @@ def create_connect_args(self, url): opts.update(url.query) allowed_opts = {'system', 'user', 'password', 'autocommit', 'readonly', 'timeout', 'database', 'use_system_naming', - 'library_list', 'current_schema' + 'library_list', 'current_schema', 'trim_char_fields' } if not allowed_opts.issuperset(opts.keys()): @@ -811,6 +812,13 @@ def create_connect_args(self, url): opts['DefaultLibraries'] += ','.join( opts.pop('library_list', '')) + if 'trim_char_fields' in opts: + try: + opts['TrimCharFields'] = str(util.strtobool(opts['trim_char_fields'])) + opts.pop('trim_char_fields', '') + except (ValueError, KeyError): + opts.pop('trim_char_fields', '') + return [["Driver={%s}; UNICODESQL=1; TRUEAUTOCOMMIT=1;" % ( self.pyodbc_driver_name)], opts] From 705685b3ec44548f9d090ee29115819e587ed58d Mon Sep 17 00:00:00 2001 From: Mitch Gallman Date: Wed, 8 Sep 2021 14:26:51 -0400 Subject: [PATCH 2/7] Extend comment for trim_char_fields indicating it defaults to False Co-authored-by: Kevin Adler --- sqlalchemy_ibmi/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlalchemy_ibmi/base.py b/sqlalchemy_ibmi/base.py index 030865b..28ec6f4 100644 --- a/sqlalchemy_ibmi/base.py +++ b/sqlalchemy_ibmi/base.py @@ -47,7 +47,7 @@ * ``use_system_naming`` - If ``True``, the connection is set to use the System naming convention, otherwise it will use the SQL naming convention. Defaults to ``False``. -* ``trim_char_fields`` - If ``True``, all character fields will be returned with trailing spaces truncated. +* ``trim_char_fields`` - If ``True``, all character fields will be returned with trailing spaces truncated. Defaults to ``False``. create-engine arguments: From 2c6d07d88b3637066fc406421c1410682ae401af Mon Sep 17 00:00:00 2001 From: Mitch Gallman Date: Wed, 8 Sep 2021 15:37:04 -0400 Subject: [PATCH 3/7] Revise trim_char_fields & use_system_naming for exceptions Pulled in proposed trim_char_fields suggestions from Kevin. Split trim_char_fields exception handling for KeyError vs ValueError; if KeyError (key not found on pop) then ignore, if ValueError (bad value specified) then raise ValueError. Split use_system_naming exception handling; change ValueError's to raise an error instead of defaulting to '0'. --- sqlalchemy_ibmi/base.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/sqlalchemy_ibmi/base.py b/sqlalchemy_ibmi/base.py index 28ec6f4..5941850 100644 --- a/sqlalchemy_ibmi/base.py +++ b/sqlalchemy_ibmi/base.py @@ -801,9 +801,19 @@ def create_connect_args(self, url): try: opts['Naming'] = str(util.strtobool(opts['use_system_naming'])) - except (ValueError, KeyError): + except (KeyError): opts['Naming'] = '0' + except (ValueError) + raise ValueError("Invalid value specified for use_system_naming") + try: + trim_char_fields = opts.pop('trim_char_fields') + opts['TrimCharFields'] = str(util.strtobool(trim_char_fields)) + except (KeyError): + pass + except (ValueError): + raise ValueError("Invalid value specified for trim_char_fields") + if 'current_schema' in opts or 'library_list' in opts: opts['DefaultLibraries'] = opts.pop('current_schema', '') + ',' if isinstance(opts["DefaultLibraries"], str): @@ -812,13 +822,6 @@ def create_connect_args(self, url): opts['DefaultLibraries'] += ','.join( opts.pop('library_list', '')) - if 'trim_char_fields' in opts: - try: - opts['TrimCharFields'] = str(util.strtobool(opts['trim_char_fields'])) - opts.pop('trim_char_fields', '') - except (ValueError, KeyError): - opts.pop('trim_char_fields', '') - return [["Driver={%s}; UNICODESQL=1; TRUEAUTOCOMMIT=1;" % ( self.pyodbc_driver_name)], opts] From 679ec148c975796a61e2e78f994f76a0bcccd080 Mon Sep 17 00:00:00 2001 From: Mitch Gallman Date: Wed, 8 Sep 2021 15:40:05 -0400 Subject: [PATCH 4/7] Missed colon --- sqlalchemy_ibmi/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlalchemy_ibmi/base.py b/sqlalchemy_ibmi/base.py index 5941850..c12ed0a 100644 --- a/sqlalchemy_ibmi/base.py +++ b/sqlalchemy_ibmi/base.py @@ -803,7 +803,7 @@ def create_connect_args(self, url): opts['Naming'] = str(util.strtobool(opts['use_system_naming'])) except (KeyError): opts['Naming'] = '0' - except (ValueError) + except (ValueError): raise ValueError("Invalid value specified for use_system_naming") try: From 307dfc53095e63fe3be0bd48bd4ef5d6d9ff5137 Mon Sep 17 00:00:00 2001 From: Mitch Gallman Date: Wed, 8 Sep 2021 16:51:22 -0400 Subject: [PATCH 5/7] Remove parenthesis from single condition exceptions Co-authored-by: Kevin Adler --- sqlalchemy_ibmi/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sqlalchemy_ibmi/base.py b/sqlalchemy_ibmi/base.py index c12ed0a..d93489f 100644 --- a/sqlalchemy_ibmi/base.py +++ b/sqlalchemy_ibmi/base.py @@ -809,9 +809,9 @@ def create_connect_args(self, url): try: trim_char_fields = opts.pop('trim_char_fields') opts['TrimCharFields'] = str(util.strtobool(trim_char_fields)) - except (KeyError): + except KeyError: pass - except (ValueError): + except ValueError: raise ValueError("Invalid value specified for trim_char_fields") if 'current_schema' in opts or 'library_list' in opts: From 118b506241a062c80a57aebb8fe7d37655c24f15 Mon Sep 17 00:00:00 2001 From: Mitch Gallman Date: Wed, 8 Sep 2021 17:32:54 -0400 Subject: [PATCH 6/7] Fix wrapping for for 80 column Co-authored-by: Kevin Adler --- sqlalchemy_ibmi/base.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sqlalchemy_ibmi/base.py b/sqlalchemy_ibmi/base.py index d93489f..66da138 100644 --- a/sqlalchemy_ibmi/base.py +++ b/sqlalchemy_ibmi/base.py @@ -47,7 +47,8 @@ * ``use_system_naming`` - If ``True``, the connection is set to use the System naming convention, otherwise it will use the SQL naming convention. Defaults to ``False``. -* ``trim_char_fields`` - If ``True``, all character fields will be returned with trailing spaces truncated. Defaults to ``False``. +* ``trim_char_fields`` - If ``True``, all character fields will be returned + with trailing spaces truncated. Defaults to ``False``. create-engine arguments: From 6f440481fec666e745947e574b56577cba19bd65 Mon Sep 17 00:00:00 2001 From: Mitch Gallman Date: Wed, 8 Sep 2021 17:50:17 -0400 Subject: [PATCH 7/7] Removed parenthesis on single value exception for use_system_naming --- sqlalchemy_ibmi/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sqlalchemy_ibmi/base.py b/sqlalchemy_ibmi/base.py index 66da138..ce77f77 100644 --- a/sqlalchemy_ibmi/base.py +++ b/sqlalchemy_ibmi/base.py @@ -802,9 +802,9 @@ def create_connect_args(self, url): try: opts['Naming'] = str(util.strtobool(opts['use_system_naming'])) - except (KeyError): + except KeyError: opts['Naming'] = '0' - except (ValueError): + except ValueError: raise ValueError("Invalid value specified for use_system_naming") try: