Skip to content

Commit 9d16e1f

Browse files
author
darius BERNARD
committed
adaptation des encodage pour freetds
1 parent edc4319 commit 9d16e1f

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

sql_server/pyodbc/base.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,14 @@ def _cursor(self):
276276
self.features.supports_mixed_date_datetime_comparisons = True
277277

278278
ms_drv_names = re.compile('^((LIB)?SQLN?CLI|LIBMSODBCSQL)')
279+
279280
if self.driver_needs_utf8 is None:
280281
self.driver_needs_utf8 = False
281-
if self.drv_name == 'SQLSRV32.DLL' or ms_drv_names.match(self.drv_name):
282+
if self.drv_name in('SQLSRV32.DLL',) or ms_drv_names.match(self.drv_name):
282283
self.driver_needs_utf8 = False
284+
if self.drv_name in ("LIBTDSODBC.SO",):
285+
self.driver_needs_utf8 = True
286+
283287

284288
# http://msdn.microsoft.com/en-us/library/ms131686.aspx
285289
if self.MARS_Connection and ms_drv_names.match(self.drv_name):
@@ -350,6 +354,7 @@ def format_sql(self, sql, n_params=None):
350354
# yet, so we need to encode the SQL clause itself in utf-8
351355
sql = sql.encode('utf-8')
352356

357+
353358
# pyodbc uses '?' instead of '%s' as parameter placeholder.
354359
if n_params is not None and n_params > 0:
355360
sql = sql % tuple('?' * n_params)
@@ -361,25 +366,35 @@ def format_sql(self, sql, n_params=None):
361366
def format_params(self, params):
362367
fp = []
363368
for p in params:
364-
if isinstance(p, text_type):
365-
if self.driver_needs_utf8:
366-
# FreeTDS (and other ODBC drivers?) doesn't support Unicode
367-
# yet, so we need to encode parameters in utf-8
368-
fp.append(p.encode('utf-8'))
369-
else:
369+
if self.connection.drv_name in ("LIBTDSODBC.SO",):
370+
if isinstance(p, text_type):
371+
if self.driver_needs_utf8:
372+
# FreeTDS (and other ODBC drivers?) doesn't support Unicode
373+
# yet, so we need to encode parameters in utf-8
374+
fp.append(p.encode('utf-8'))
375+
else:
376+
fp.append(p)
377+
378+
elif isinstance(p, binary_type):
370379
fp.append(p)
371380

372-
elif isinstance(p, binary_type):
373-
fp.append(p)
381+
elif isinstance(p, type(True)):
382+
if p:
383+
fp.append(1)
384+
else:
385+
fp.append(0)
374386

375-
elif isinstance(p, type(True)):
376-
if p:
377-
fp.append(1)
378387
else:
379-
fp.append(0)
380-
388+
fp.append(p)
381389
else:
382-
fp.append(p)
390+
if isinstance(p, type(True)):
391+
if p:
392+
fp.append(1)
393+
else:
394+
fp.append(0)
395+
396+
else:
397+
fp.append(p)
383398

384399
return tuple(fp)
385400

0 commit comments

Comments
 (0)