Skip to content

Commit 4e685c0

Browse files
sanitize sql
1 parent 62e7944 commit 4e685c0

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

vectorstore/vectordb.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ def create_index(
252252
with connection.cursor() as curr:
253253
# Insert index metadata into indexes table
254254
is_protected = deletion_protection == DeletionProtection.ENABLED
255-
curr.execute(f"""
255+
curr.execute(
256+
f"""
256257
INSERT INTO {INDEXES_TABLE_NAME}(
257258
{INDEX_NAME_FIELD},
258259
{INDEX_DIMENSION_FIELD},
@@ -261,16 +262,18 @@ def create_index(
261262
{INDEX_USE_VECTOR_INDEX_FIELD},
262263
{INDEX_VECTOR_INDEX_OPTIONS_FIELD},
263264
{INDEX_TAGS_FIELD}
264-
) VALUES(
265-
'{name}',
266-
{dimension},
267-
'{metric.value}',
268-
{is_protected},
269-
{use_vector_index},
270-
'{json.dumps(vector_index_options)}',
271-
'{json.dumps(tags)}'
272-
);
273-
""")
265+
) VALUES(%s, %s, %s, %s, %s, %s, %s);
266+
""",
267+
[
268+
name,
269+
dimension,
270+
metric.value,
271+
is_protected,
272+
use_vector_index,
273+
json.dumps(vector_index_options),
274+
json.dumps(tags),
275+
],
276+
)
274277

275278
# Prepare vector index options
276279
index_options = f"INDEX_OPTIONS '{json.dumps(vector_index_options)}'"
@@ -343,7 +346,8 @@ def delete_index(self, name: str) -> None:
343346

344347
# Remove the index metadata
345348
curr.execute(
346-
f"DELETE FROM {INDEXES_TABLE_NAME} WHERE {INDEX_NAME_FIELD}='{name}';"
349+
f"DELETE FROM {INDEXES_TABLE_NAME} WHERE {INDEX_NAME_FIELD}=%s;",
350+
[name],
347351
)
348352
finally:
349353
self._close_connection_if_needed(connection)
@@ -410,7 +414,8 @@ def describe_index(self, name: str) -> IndexModel:
410414
try:
411415
with connection.cursor() as curr:
412416
# Query the index metadata
413-
curr.execute(f"""
417+
curr.execute(
418+
f"""
414419
SELECT
415420
{INDEX_NAME_FIELD},
416421
{INDEX_DIMENSION_FIELD},
@@ -420,8 +425,10 @@ def describe_index(self, name: str) -> IndexModel:
420425
{INDEX_VECTOR_INDEX_OPTIONS_FIELD},
421426
{INDEX_TAGS_FIELD}
422427
FROM {INDEXES_TABLE_NAME}
423-
WHERE {INDEX_NAME_FIELD}='{name}'
424-
""")
428+
WHERE {INDEX_NAME_FIELD}= %s
429+
""",
430+
[name],
431+
)
425432
result = curr.fetchone()
426433

427434
# Check if the index exists
@@ -460,7 +467,8 @@ def has_index(self, name: str) -> bool:
460467
with conn.cursor() as curr:
461468
curr.execute(
462469
f"SELECT {INDEX_NAME_FIELD} FROM {INDEXES_TABLE_NAME} "
463-
f"WHERE {INDEX_NAME_FIELD}='{name}'"
470+
f"WHERE {INDEX_NAME_FIELD}= %s",
471+
[name],
464472
)
465473
result = curr.fetchone()
466474
return result is not None
@@ -500,6 +508,7 @@ def configure_index(
500508

501509
# Build update parameters
502510
update_params = []
511+
update_values = []
503512

504513
# Handle deletion protection
505514
if deletion_protection is not None:
@@ -509,27 +518,31 @@ def configure_index(
509518
f"Must be one of {list(DeletionProtection)}"
510519
)
511520
is_protected = deletion_protection == DeletionProtection.ENABLED
512-
update_params.append(f"{INDEX_DELETION_PROTECT_FIELD}={is_protected}")
521+
update_params.append(f"{INDEX_DELETION_PROTECT_FIELD}=%s")
522+
update_values.append(is_protected)
513523

514524
# Handle tags
515525
if tags is not None:
516526
tags_json = json.dumps(tags)
517-
update_params.append(f"{INDEX_TAGS_FIELD}='{tags_json}'")
527+
update_params.append(f"{INDEX_TAGS_FIELD}=%s")
528+
update_values.append(tags_json)
518529

519530
# Initialize vector index options
520531
vector_index_options = dict(vector_index_options or {})
521532

522533
# Add vector index settings
523534
if use_vector_index is not None:
524-
update_params.append(f"{INDEX_USE_VECTOR_INDEX_FIELD}={use_vector_index}")
535+
update_params.append(f"{INDEX_USE_VECTOR_INDEX_FIELD}=%s")
536+
update_values.append(use_vector_index)
525537
# Add required metric type to options
526538
distance_strategy = self._get_distance_strategy(index.metric)
527539
vector_index_options[METRIC_TYPE] = distance_strategy.value
528540

529541
# Add vector index options to update parameters if provided
530542
if vector_index_options:
531543
options_json = json.dumps(vector_index_options)
532-
update_params.append(f"{INDEX_VECTOR_INDEX_OPTIONS_FIELD}='{options_json}'")
544+
update_params.append(f"{INDEX_VECTOR_INDEX_OPTIONS_FIELD}=%s")
545+
update_values.append(options_json)
533546

534547
# Execute updates if there are parameters to update
535548
if update_params:
@@ -540,9 +553,9 @@ def configure_index(
540553
update_sql = (
541554
f"UPDATE {INDEXES_TABLE_NAME} "
542555
f"SET {', '.join(update_params)} "
543-
f"WHERE {INDEX_NAME_FIELD}='{name}'"
556+
f"WHERE {INDEX_NAME_FIELD}=%s"
544557
)
545-
curr.execute(update_sql)
558+
curr.execute(update_sql, update_values + [name])
546559

547560
# Handle vector index changes if requested
548561
if use_vector_index is not None:

0 commit comments

Comments
 (0)