From 1c3935af2ac8ceeae51f81c7c4fd85a5f5c0e022 Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Sun, 31 Jul 2022 03:05:32 -0400
Subject: [PATCH] Correct potential SELECT statement problems with NULL and
 reserved words

---
 py_dbcn/connectors/core/display.py     |  8 ++++++--
 tests/connectors/mysql/test_records.py | 12 +++++++++++-
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/py_dbcn/connectors/core/display.py b/py_dbcn/connectors/core/display.py
index 72acd26..b258845 100644
--- a/py_dbcn/connectors/core/display.py
+++ b/py_dbcn/connectors/core/display.py
@@ -233,7 +233,7 @@ class RecordDisplay:
             for table_col in table_cols:
                 col_len = len(table_col)
                 record_len = self._base.query.execute(
-                    'SELECT MAX(LENGTH({0})) FROM {1};'.format(table_col, table_name)
+                    'SELECT MAX(LENGTH(`{0}`)) FROM {1};'.format(table_col, table_name)
                 )[0][0]
                 length = max(col_len, record_len or 0)
                 col_len_array.append(length)
@@ -255,7 +255,11 @@ class RecordDisplay:
             record_str = ''
             for record in results:
                 for index in range(len(record)):
-                    record_str += ('| {0:<' + '{0}'.format(col_len_array[index]) + '} ').format(record[index])
+                    if record[index] is None:
+                        col_str = 'NULL'
+                    else:
+                        col_str = str(record[index])
+                    record_str += ('| {0:<' + '{0}'.format(col_len_array[index]) + '} ').format(col_str)
                 record_str += '|\n'
 
             # Combine final string.
diff --git a/tests/connectors/mysql/test_records.py b/tests/connectors/mysql/test_records.py
index c77953c..a2dce2d 100644
--- a/tests/connectors/mysql/test_records.py
+++ b/tests/connectors/mysql/test_records.py
@@ -52,7 +52,7 @@ class TestMysqlRecords(TestMysqlDatabaseParent):
             self.assertGreaterEqual(len(results), 0)
             self.assertIn(table_name, results)
 
-        with self.subTest('SHOW query when table has records'):
+        with self.subTest('SELECT query when table has records'):
             # Run test query.
             row = (1, 'test_name_1', 'test_desc_1')
             self.connector.query.execute('INSERT INTO {0} VALUES {1};'.format(table_name, row))
@@ -73,6 +73,16 @@ class TestMysqlRecords(TestMysqlDatabaseParent):
 
         # Works for 0, 1, and 2. Assume works for all further n+1 values.
 
+        with self.subTest('SELECT query when table column uses "reserved keyword"'):
+            # "Group" is considered a MySQL keyword. As long as this doesn't raise an error, it worked.
+            self.connector.tables.add_column(table_name, '`group` VARCHAR(100)')
+            results = self.connector.records.select(table_name)
+
+            # Verify two records returned, now with an extra "group" field that shows null.
+            row = row + (None,)
+            self.assertEqual(len(results), 2)
+            self.assertIn(row, results)
+
     def test__insert__success(self):
         """
         Test `INSERT` query when table does not exist.
-- 
GitLab