Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Accept valid SQL statements with extra whitespace in SQL.issql()

PerryLink6897530dd51f
diff --git a/src/python/txtai/database/sql/base.py b/src/python/txtai/database/sql/base.py
index 6a3f30ed..ed9557ba 100644
--- a/src/python/txtai/database/sql/base.py
+++ b/src/python/txtai/database/sql/base.py
@@ -105,7 +105,7 @@ class SQL:
if isinstance(query, str):
# Reduce query to a lower-cased single line stripped of leading/trailing whitespace
- query = query.lower().strip(";").replace("\n", " ").replace("\t", " ").strip()
+ query = " ".join(query.lower().split()).strip(" ;")
# Detect if this is a valid txtai SQL statement
return query.startswith("select ") and (" from txtai " in query or query.endswith(" from txtai"))
diff --git a/test/python/testdatabase/testsql.py b/test/python/testdatabase/testsql.py
index f8d2595b..3bb19f5f 100644
--- a/test/python/testdatabase/testsql.py
+++ b/test/python/testdatabase/testsql.py
@@ -207,6 +207,16 @@ class TestSQL(unittest.TestCase):
self.assertSql("select", ";select id from txtai", "s.id")
self.assertSql("select", "select id from txtai;", "s.id")
+ def testStatementWhitespace(self):
+ """
+ Test insignificant whitespace around SQL tokens and a trailing semicolon is accepted.
+ """
+
+ self.assertSql("select", "select id from txtai", "s.id")
+ self.assertSql("select", "select id from txtai; ", "s.id")
+ self.assertSql("select", "select id from txtai;\n", "s.id")
+ self.assertSql("select", " ; select id from txtai", "s.id")
+
def testBracket(self):
"""
Test bracket expressions