Skip to content

Commit 87e1833

Browse files
authored
fix Special characters (#700)
1 parent cbcf33b commit 87e1833

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/memos/graph_dbs/polardb.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,8 +2179,9 @@ def get_by_metadata(
21792179

21802180
# Format value
21812181
if isinstance(value, str):
2182-
# Escape single quotes in string values
2183-
escaped_str = value.replace("'", "''")
2182+
# Escape single quotes using backslash when inside $$ dollar-quoted strings
2183+
# In $$ delimiters, Cypher string literals can use \' to escape single quotes
2184+
escaped_str = value.replace("'", "\\'")
21842185
escaped_value = f"'{escaped_str}'"
21852186
elif isinstance(value, list):
21862187
# Handle list values - use double quotes for Cypher arrays
@@ -4153,6 +4154,17 @@ def _build_filter_conditions_cypher(
41534154
if filter:
41544155

41554156
def escape_cypher_string(value: str) -> str:
4157+
"""
4158+
Escape single quotes in Cypher string literals.
4159+
4160+
In Cypher, single quotes in string literals are escaped by doubling them: ' -> ''
4161+
However, when inside PostgreSQL's $$ dollar-quoted string, we need to be careful.
4162+
4163+
The issue: In $$ delimiters, Cypher still needs to parse string literals correctly.
4164+
The solution: Use backslash escape \' instead of doubling '' when inside $$.
4165+
"""
4166+
# Use backslash escape for single quotes inside $$ dollar-quoted strings
4167+
# This works because $$ protects the backslash from PostgreSQL interpretation
41564168
return value.replace("'", "\\'")
41574169

41584170
def build_cypher_filter_condition(condition_dict: dict) -> str:

src/memos/memories/textual/tree_text_memory/organize/manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def _add_memories_parallel(
134134
return added_ids
135135

136136
def _add_memories_batch(
137-
self, memories: list[TextualMemoryItem], user_name: str | None = None, batch_size: int = 10
137+
self, memories: list[TextualMemoryItem], user_name: str | None = None, batch_size: int = 50
138138
) -> list[str]:
139139
"""
140140
Add memories using batch database operations (more efficient for large batches).

0 commit comments

Comments
 (0)