Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_output_conversion(self):
def convert1(value):
# The value is the raw bytes (as a bytes object) read from the
# database. We'll simply add an X at the beginning at the end.
return 'X' + value.decode('latin1') + 'X'
def convert2(value):
# Same as above, but add a Y at the beginning at the end.
return 'Y' + value.decode('latin1') + 'Y'
self.cursor.execute("create table t1(n int, v varchar(10))")
self.cursor.execute("insert into t1 values (1, '123.45')")
self.cnxn.add_output_converter(pyodbc.SQL_VARCHAR, convert1)
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, 'X123.45X')
# Clear all conversions and try again. There should be no Xs this time.
self.cnxn.clear_output_converters()
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, '123.45')
# Same but clear using remove_output_converter.
self.cnxn.add_output_converter(pyodbc.SQL_VARCHAR, convert1)
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, 'X123.45X')
self.cnxn.remove_output_converter(pyodbc.SQL_VARCHAR)
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, '123.45')
def test_output_conversion(self):
def convert(value):
# `value` will be a string. We'll simply add an X at the beginning at the end.
return 'X' + value + 'X'
self.cnxn.add_output_converter(pyodbc.SQL_VARCHAR, convert)
self.cursor.execute("create table t1(n int, v varchar(10))")
self.cursor.execute("insert into t1 values (1, '123.45')")
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, 'X123.45X')
# Now clear the conversions and try again. There should be no Xs this time.
self.cnxn.clear_output_converters()
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, '123.45')
def test_output_conversion(self):
def convert(value):
# `value` will be a string. We'll simply add an X at the beginning at the end.
return 'X' + value + 'X'
self.cnxn.add_output_converter(pyodbc.SQL_VARCHAR, convert)
self.cursor.execute("create table t1(n int, v varchar(10))")
self.cursor.execute("insert into t1 values (1, '123.45')")
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, 'X123.45X')
# Now clear the conversions and try again. There should be no Xs this time.
self.cnxn.clear_output_converters()
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, '123.45')
def test_output_conversion(self):
def convert(value):
# `value` will be a string. We'll simply add an X at the beginning at the end.
return 'X' + value + 'X'
self.cnxn.add_output_converter(pyodbc.SQL_VARCHAR, convert)
self.cursor.execute("create table t1(n int, v varchar(10))")
self.cursor.execute("insert into t1 values (1, '123.45')")
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, 'X123.45X')
# Now clear the conversions and try again. There should be no Xs this time.
self.cnxn.clear_output_converters()
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, '123.45')
async def test_output_conversion(conn, table):
def convert(value):
# value will be a string. We'll simply add an X at the
# beginning at the end.
if isinstance(value, str):
return 'X' + value + 'X'
return b'X' + value + b'X'
await conn.add_output_converter(pyodbc.SQL_VARCHAR, convert)
cur = await conn.cursor()
await cur.execute("INSERT INTO t1 VALUES (3, '123.45')")
await cur.execute("SELECT v FROM t1 WHERE n=3;")
(value,) = await cur.fetchone()
assert value in (b'X123.45X', 'X123.45X')
# Now clear the conversions and try again. There should be
# no Xs this time.
await conn.clear_output_converters()
await cur.execute("SELECT v FROM t1")
(value,) = await cur.fetchone()
assert value == '123.45'
await cur.close()
def test_output_conversion(self):
def convert(value):
# `value` will be a string. We'll simply add an X at the beginning at the end.
return 'X' + value + 'X'
self.cnxn.add_output_converter(pyodbc.SQL_VARCHAR, convert)
self.cursor.execute("create table t1(n int, v varchar(10))")
self.cursor.execute("insert into t1 values (1, '123.45')")
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, 'X123.45X')
# Now clear the conversions and try again. There should be no Xs this time.
self.cnxn.clear_output_converters()
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, '123.45')
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, 'X123.45X')
self.cnxn.add_output_converter(pyodbc.SQL_VARCHAR, None)
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, '123.45')
# retrieve and temporarily replace converter (get_output_converter)
#
# case_1: converter already registered
self.cnxn.add_output_converter(pyodbc.SQL_VARCHAR, convert1)
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, 'X123.45X')
prev_converter = self.cnxn.get_output_converter(pyodbc.SQL_VARCHAR)
self.assertNotEqual(prev_converter, None)
self.cnxn.add_output_converter(pyodbc.SQL_VARCHAR, convert2)
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, 'Y123.45Y')
self.cnxn.add_output_converter(pyodbc.SQL_VARCHAR, prev_converter)
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, 'X123.45X')
#
# case_2: no converter already registered
self.cnxn.clear_output_converters()
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, '123.45')
prev_converter = self.cnxn.get_output_converter(pyodbc.SQL_VARCHAR)
self.assertEqual(prev_converter, None)
self.cnxn.add_output_converter(pyodbc.SQL_VARCHAR, convert2)
value = self.cursor.execute("select v from t1").fetchone()[0]
self.assertEqual(value, 'Y123.45Y')
self.cnxn.add_output_converter(pyodbc.SQL_VARCHAR, prev_converter)
Database.SQL_DECIMAL: 'DecimalField',
Database.SQL_DOUBLE: 'FloatField',
Database.SQL_FLOAT: 'FloatField',
Database.SQL_GUID: 'TextField',
Database.SQL_INTEGER: 'IntegerField',
Database.SQL_LONGVARBINARY: 'BinaryField',
#Database.SQL_LONGVARCHAR: ,
Database.SQL_NUMERIC: 'DecimalField',
Database.SQL_REAL: 'FloatField',
Database.SQL_SMALLINT: 'SmallIntegerField',
Database.SQL_TINYINT: 'SmallIntegerField',
Database.SQL_TYPE_DATE: 'DateField',
Database.SQL_TYPE_TIME: 'TimeField',
Database.SQL_TYPE_TIMESTAMP: 'DateTimeField',
Database.SQL_VARBINARY: 'BinaryField',
Database.SQL_VARCHAR: 'TextField',
Database.SQL_WCHAR: 'CharField',
Database.SQL_WLONGVARCHAR: 'TextField',
Database.SQL_WVARCHAR: 'TextField',
}
def get_table_list(self, cursor):
"""
Returns a list of table names in the current database.
"""
# TABLES: http://msdn2.microsoft.com/en-us/library/ms186224.aspx
# TODO: Believe the below queries should actually select `TABLE_NAME, TABLE_TYPE`
if cursor.db.limit_table_list:
cursor.execute("SELECT TABLE_NAME, 't' FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'dbo'")
else:
cursor.execute("SELECT TABLE_NAME, 't' FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'")
Database.SQL_DECIMAL: 'DecimalField',
Database.SQL_DOUBLE: 'FloatField',
Database.SQL_FLOAT: 'FloatField',
Database.SQL_GUID: 'TextField',
Database.SQL_INTEGER: 'IntegerField',
#Database.SQL_LONGVARBINARY: ,
#Database.SQL_LONGVARCHAR: ,
Database.SQL_NUMERIC: 'DecimalField',
Database.SQL_REAL: 'FloatField',
Database.SQL_SMALLINT: 'SmallIntegerField',
Database.SQL_TINYINT: 'SmallIntegerField',
Database.SQL_TYPE_DATE: 'DateField',
Database.SQL_TYPE_TIME: 'TimeField',
Database.SQL_TYPE_TIMESTAMP: 'DateTimeField',
#Database.SQL_VARBINARY: ,
Database.SQL_VARCHAR: 'TextField',
Database.SQL_WCHAR: 'CharField',
Database.SQL_WLONGVARCHAR: 'TextField',
Database.SQL_WVARCHAR: 'TextField',
}
def get_table_list(self, cursor):
"""
Returns a list of table names in the current database.
"""
# TABLES: http://msdn2.microsoft.com/en-us/library/ms186224.aspx
cursor.execute("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'")
return [row[0] for row in cursor.fetchall()]
# Or pyodbc specific:
#return [row[2] for row in cursor.tables(tableType='TABLE')]