michiya / django-pyodbc-azure

Django backend for Microsoft SQL Server and Azure SQL Database using pyodbc
https://pypi.python.org/pypi/django-pyodbc-azure
BSD 3-Clause "New" or "Revised" License
321 stars 140 forks source link

Problems with table names containing character '%', related issue #87 #193

Open atocyo opened 5 years ago

atocyo commented 5 years ago

pyodbc uses '?' instead of '%s' as parameter placeholder. SQL identifiers (consequently table names, columns) can contain special characters if surrounded by brackets '[...]' Replace only those placeholders:'%, %%, %s, %d etc.' which are not surrounded by brackets.
This is not complete solution, there may be problems with string literals in SQL etc. , if non string placeholder is found (example:%d) there should be error raised.

Test: input: ''', test,%ss, [Company$Item].[Percentile %], [Company$Item],%s, [bfbfdb%vbddfb], %, %% [[%]], [%''' result: ''', test,?s, [Company$Item].[Percentile %], [Company$Item],?, [bfbfdb%vbddfb], ?, ? [[%]], [%'''