Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Change schema statements to use bind parameters #829

davidmezzetti89410d3661e8
diff --git a/src/python/txtai/ann/pgvector.py b/src/python/txtai/ann/pgvector.py
index b86bb578..f5cc737f 100644
--- a/src/python/txtai/ann/pgvector.py
+++ b/src/python/txtai/ann/pgvector.py
@@ -38,12 +38,6 @@ class PGVector(ANN):
self.sqldialect(text("CREATE EXTENSION IF NOT EXISTS vector"))
self.database.commit()
- # Set default schema, if necessary
- schema = self.setting("schema")
- if schema:
- self.sqldialect(CreateSchema(schema, if_not_exists=True))
- self.sqldialect(text(f"SET search_path TO {schema},public"))
-
# Table instance
self.table = None
@@ -110,6 +104,12 @@ class PGVector(ANN):
recreate: Recreates the database tables if True
"""
+ # Set default schema, if necessary
+ schema = self.setting("schema")
+ if schema:
+ self.sqldialect(CreateSchema(schema, if_not_exists=True))
+ self.sqldialect(text("SET search_path TO :schema,public"), {"schema": schema})
+
# Table name
table = self.setting("table", "vectors")
@@ -149,12 +149,14 @@ class PGVector(ANN):
return {"m": self.setting("m", 16), "ef_construction": self.setting("efconstruction", 200)}
- def sqldialect(self, sql):
+ def sqldialect(self, sql, parameters=None):
"""
Executes a SQL statement based on the current SQL dialect.
Args:
sql: SQL to execute
+ parameters: optional bind parameters
"""
- self.database.execute(sql if self.engine.dialect.name == "postgresql" else text("SELECT 1"))
+ args = (sql, parameters) if self.engine.dialect.name == "postgresql" else (text("SELECT 1"),)
+ self.database.execute(*args)
diff --git a/src/python/txtai/database/client.py b/src/python/txtai/database/client.py
index c08e043b..4bdad38d 100644
--- a/src/python/txtai/database/client.py
+++ b/src/python/txtai/database/client.py
@@ -122,7 +122,7 @@ class Client(RDBMS):
schema = self.config.get("schema")
if schema:
self.sqldialect(database, engine, CreateSchema(schema, if_not_exists=True))
- self.sqldialect(database, engine, textsql(f"SET search_path TO {schema}"))
+ self.sqldialect(database, engine, textsql("SET search_path TO :schema"), {"schema": schema})
return database
@@ -135,7 +135,7 @@ class Client(RDBMS):
def addfunctions(self):
return
- def sqldialect(self, database, engine, sql):
+ def sqldialect(self, database, engine, sql, parameters=None):
"""
Executes a SQL statement based on the current SQL dialect.
@@ -143,9 +143,11 @@ class Client(RDBMS):
database: current database
engine: database engine
sql: SQL to execute
+ parameters: optional bind parameters
"""
- database.execute(sql if engine.dialect.name == "postgresql" else textsql("SELECT 1"))
+ args = (sql, parameters) if engine.dialect.name == "postgresql" else (textsql("SELECT 1"),)
+ database.execute(*args)
class Cursor:
diff --git a/src/python/txtai/scoring/pgtext.py b/src/python/txtai/scoring/pgtext.py
index 9d5b5ad2..3cb6c2aa 100644
--- a/src/python/txtai/scoring/pgtext.py
+++ b/src/python/txtai/scoring/pgtext.py
@@ -89,16 +89,19 @@ class PGText(Scoring):
def load(self, path):
# Reset database to original checkpoint
- self.database.rollback()
+ if self.database:
+ self.database.rollback()
# Initialize tables
self.initialize()
def save(self, path):
- self.database.commit()
+ if self.database:
+ self.database.commit()
def close(self):
- self.database.close()
+ if self.database:
+ self.database.close()
def hasterms(self):
return True
@@ -123,7 +126,7 @@ class PGText(Scoring):
schema = self.config.get("schema")
if schema:
self.sqldialect(CreateSchema(schema, if_not_exists=True))
- self.sqldialect(text(f"SET search_path TO {schema}"))
+ self.sqldialect(text("SET search_path TO :schema"), {"schema": schema})
# Table name
table = self.config.get("table", "scoring")
@@ -157,12 +160,14 @@ class PGText(Scoring):
self.table.create(self.engine, checkfirst=True)
index.create(self.engine, checkfirst=True)
- def sqldialect(self, sql):
+ def sqldialect(self, sql, parameters=None):
"""
Executes a SQL statement based on the current SQL dialect.
Args:
sql: SQL to execute
+ parameters: optional bind parameters
"""
- self.database.execute(sql if self.engine.dialect.name == "postgresql" else text("SELECT 1"))
+ args = (sql, parameters) if self.engine.dialect.name == "postgresql" else (text("SELECT 1"),)
+ self.database.execute(*args)