Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named Parameters, NULL handling and RAISERROR #118

Open
jholladay10 opened this issue Feb 1, 2021 · 1 comment
Open

Named Parameters, NULL handling and RAISERROR #118

jholladay10 opened this issue Feb 1, 2021 · 1 comment

Comments

@jholladay10
Copy link

I'm sending multiple statements using the named parameters mechanism. I like to use RAISERROR, which does sprintf substitution. So my %d in my RAISERROR statement is tripping up the operation % rename in pytds.

It also looks like the code is attempting to get around not being able to infer the type from the parameter values if the value is NONE. Unless I'm missing something, my named parameters would all have to be %()s in the sql in order for this to work. I'm thinking that's unnecessary.

My suggestion is to allow the caller to optionally specify a type with a value in a tuple. Infer the type if one isn't provided explicitly. We're planning to do this. I can provide code if you want.

@jholladay10
Copy link
Author

jholladay10 commented Feb 1, 2021

I now see that it should be possible to pass a Column object in as a value. Is that the intended solution? If so, it would be nice if the operation % rename only occurred if the rename was actually required, giving the caller the opportunity to avoid the rename consequences altogether if it's unnecessary.

What about something like this?

        elif isinstance(params, dict):
            # prepend names with @
            rename = {}
            for name, value in params.items():
                if value is None:
                    rename[name] = 'NULL'
                else:
                    if name.startswith('@'):
                        mssql_name = name
                    else:
                        mssql_name = '@{0}'.format(name)
                        rename[name] = mssql_name
                    named_params[mssql_name] = value
            if rename:
                operation = operation % rename

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

No branches or pull requests

1 participant