Skip to content

Insert complex types with cursor.execute + client-side binding #490

Merged
sitingren merged 7 commits intovertica:masterfrom
sitingren:insert-simple-complex
Feb 28, 2023
Merged

Insert complex types with cursor.execute + client-side binding #490
sitingren merged 7 commits intovertica:masterfrom
sitingren:insert-simple-complex

Conversation

@sitingren
Copy link
Contributor

@sitingren sitingren commented Feb 28, 2023

Related to #469.

  • [use_prepared_statements=False only] Add default mappings from python types (list, set, dict) to SQL literals (ARRAY, SET, ROW)
  • Improve error message and debug logging

@sitingren sitingren merged commit 1fd5c16 into vertica:master Feb 28, 2023
@sitingren sitingren deleted the insert-simple-complex branch February 28, 2023 08:42
@sitingren sitingren changed the title Insert complex types with client-side binding Insert complex types with cursor.execute + client-side binding Jul 6, 2023
@Alexxz
Copy link

Alexxz commented Nov 24, 2023

Could someone explain please on how important were the following changes of this MR?

            if parameters and ':' not in operation:
                raise ValueError(f'Invalid SQL: {operation}'
                    "\nHINT: When argument 'parameters' is a dict, variables in SQL should be specified with named (:name) placeholders."
                    " If you use a dict to represent the value of a ROW type column, enclose the dict with brackets('[]') to construct a list.")

Before these changes came into project I could use bag of named parameters as good placeholders for SQL no matter if they are used in the specific query or not.
Something like:

params = dict(date=current_date, password='XXX')
sqls = [
    'TRUNCATE TABLE tab1',
    'CONNECT TO VERTICA ... :password',
    'INSERT INTO tab1 SELECT ... WHERE dt = :date',
]
for sql in sqls:
    v.execute(sql, params)

This fails on TRUNCATE TABLE operation. It looks my approach is forbidden at all. Is my approach bad? Usage of named parameters usually assumes that these parameters are all optionally used.

@sitingren
Copy link
Contributor Author

sitingren commented Nov 28, 2023

@Alexxz You can set environment variable VERTICA_PYTHON_IGNORE_NAMED_PARAMETER_CHECK to 1 to get rid of the exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants