Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Create temporary tables on connection, closes #692

davidmezzetti9f9ed6d9289f
diff --git a/src/python/txtai/database/client.py b/src/python/txtai/database/client.py
index 638f3720..cfd16985 100644
--- a/src/python/txtai/database/client.py
+++ b/src/python/txtai/database/client.py
@@ -65,7 +65,7 @@ class Client(RDBMS):
def createtables(self):
# Create tables
- Base.metadata.create_all(self.connection.bind)
+ Base.metadata.create_all(self.connection.bind, checkfirst=True)
# Clear existing data - table schema is created upon connecting to database
for table in ["sections", "documents", "objects"]:
@@ -128,7 +128,7 @@ class Client(RDBMS):
class Cursor:
"""
- Implements basic compatability with the Python DB-API.
+ Implements basic compatibility with the Python DB-API.
"""
def __init__(self, connection):
diff --git a/src/python/txtai/database/embedded.py b/src/python/txtai/database/embedded.py
index 467b1334..a71b022b 100644
--- a/src/python/txtai/database/embedded.py
+++ b/src/python/txtai/database/embedded.py
@@ -43,13 +43,9 @@ class Embedded(RDBMS):
self.connection.close()
# Point connection to new connection
- self.connection = connection
- self.cursor = self.getcursor()
+ self.session(connection=connection)
self.path = path
- # Register custom functions
- self.addfunctions()
-
# Paths are equal, commit changes
elif self.path == path:
self.connection.commit()
diff --git a/src/python/txtai/database/rdbms.py b/src/python/txtai/database/rdbms.py
index d10a4976..a9bc03eb 100644
--- a/src/python/txtai/database/rdbms.py
+++ b/src/python/txtai/database/rdbms.py
@@ -32,11 +32,7 @@ class RDBMS(Database):
def load(self, path):
# Load an existing database. Thread locking must be handled externally.
- self.connection = self.connect(path)
- self.cursor = self.getcursor()
-
- # Register custom functions
- self.addfunctions()
+ self.session(path)
def insert(self, documents, index=0):
# Initialize connection if not open
@@ -250,15 +246,31 @@ class RDBMS(Database):
if not self.connection:
# Create temporary database. Thread locking must be handled externally.
- self.connection = self.connect()
- self.cursor = self.getcursor()
-
- # Register custom functions
- self.addfunctions()
+ self.session()
# Create initial table schema
self.createtables()
+ def session(self, path=None, connection=None):
+ """
+ Starts a new database session.
+
+ Args:
+ path: path to database file
+ connection: existing connection to use
+ """
+
+ # Create database connection and cursor
+ self.connection = connection if connection else self.connect(path) if path else self.connect()
+ self.cursor = self.getcursor()
+
+ # Register custom functions - session scope
+ self.addfunctions()
+
+ # Create temporary tables - session scope
+ self.createbatch()
+ self.createscores()
+
def createtables(self):
"""
Creates the initial table schema.
@@ -412,9 +424,6 @@ class RDBMS(Database):
batch: batch index, used when statement has multiple subselects
"""
- # Create or replace batch table
- self.createbatch()
-
# Delete batch when batch id is empty or for batch 0
if not batch:
self.cursor.execute(Statement.DELETE_BATCH)
@@ -448,9 +457,6 @@ class RDBMS(Database):
similarity: similarity results as [(indexid, score)]
"""
- # Create or replace scores table
- self.createscores()
-
# Delete scores
self.cursor.execute(Statement.DELETE_SCORES)