Menu

#28 Named parameters fail if the sql ends on a parameter

v1.0_(example)
closed
5
2019-11-26
2019-11-02
John Clymer
No

changeNamedToQmark will error out if the parameter name is the last character on the sql string.

Recommend the following change which checks to ensure it's not trying to grab "c" from beyond the end of the string.

def changeNamedToQmark(op): #convert from 'named' paramstyle to ADO required '?'mark parameters
outOp = ''
outparms=[]
chunks = op.split("'") #quote all literals -- odd numbered list results are literals.
inQuotes = False
for chunk in chunks:
if inQuotes: # this is inside a quote
if chunk == '': # double apostrophe to quote one apostrophe
outOp = outOp[:-1] # so take one away
else:
outOp += "'"+chunk+"'" # else pass the quoted string as is.
else: # is SQL code -- look for a :namedParameter
while chunk: # some SQL string remains
sp = chunk.split(':',1)
outOp += sp[0] # concat the part up to the :
s = ''
try:
chunk = sp[1]
except IndexError:
chunk = None
if chunk: # there was a parameter - parse it out
i = 0
c = chunk[0]
while c.isalnum() or c == '_':
i += 1
if i<len(chunk):
c = chunk[i]
else:
break

s = chunk[:i]
chunk = chunk[i:]
if s:
outparms.append(s) # list the parameters in order
outOp += '?' # put in the Qmark
inQuotes = not inQuotes
return outOp, outparms

Discussion

  • Vernon Cole

    Vernon Cole - 2019-11-22

    Thank you for reporting this.
    I am in the process of moving the master copy of the source code a sub-directory of pywin32 so that it will not get lost when deploying there.

    A different version of your suggested fix has been implemented there. I used a try - except block, because, in Python, the "it's easier to get forgiveness that permission" technique saves testing for an error which will usually not happen. The resulting code is easier to read and usually runs faster.

    I will try to move a copy of the updated version back to sourceforge soon.

     
  • Vernon Cole

    Vernon Cole - 2019-11-26
    • status: open --> closed
    • assigned_to: Vernon Cole
     
  • Vernon Cole

    Vernon Cole - 2019-11-26

    Fixed in adodbapi 2.6.2. Thanks for reporting and analyzing this.

     

Log in to post a comment.