Skip to content

Commit aa78497

Browse files
fix: add current_user_expr() for backend-agnostic user retrieval
- Add current_user_expr() abstract method to BaseAdapter - MySQL: returns "user()" - PostgreSQL: returns "current_user" - Update connection.get_user() to use adapter method Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3c34d31 commit aa78497

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

src/datajoint/adapters/base.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,19 @@ def interval_expr(self, value: int, unit: str) -> str:
879879
"""
880880
...
881881

882+
@abstractmethod
883+
def current_user_expr(self) -> str:
884+
"""
885+
SQL expression to get the current user.
886+
887+
Returns
888+
-------
889+
str
890+
SQL expression for current user (e.g., 'user()' for MySQL,
891+
'current_user' for PostgreSQL).
892+
"""
893+
...
894+
882895
@abstractmethod
883896
def json_path_expr(self, column: str, path: str, return_type: str | None = None) -> str:
884897
"""

src/datajoint/adapters/mysql.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,10 @@ def interval_expr(self, value: int, unit: str) -> str:
783783
# MySQL uses singular unit names
784784
return f"INTERVAL {value} {unit.upper()}"
785785

786+
def current_user_expr(self) -> str:
787+
"""MySQL current user expression."""
788+
return "user()"
789+
786790
def json_path_expr(self, column: str, path: str, return_type: str | None = None) -> str:
787791
"""
788792
Generate MySQL json_value() expression.

src/datajoint/adapters/postgres.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,10 @@ def interval_expr(self, value: int, unit: str) -> str:
907907
unit_plural = unit.lower() + "s" if not unit.endswith("s") else unit.lower()
908908
return f"INTERVAL '{value} {unit_plural}'"
909909

910+
def current_user_expr(self) -> str:
911+
"""PostgreSQL current user expression."""
912+
return "current_user"
913+
910914
def json_path_expr(self, column: str, path: str, return_type: str | None = None) -> str:
911915
"""
912916
Generate PostgreSQL jsonb_extract_path_text() expression.

src/datajoint/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ def get_user(self) -> str:
451451
str
452452
User name and host as ``'user@host'``.
453453
"""
454-
return self.query("SELECT user()").fetchone()[0]
454+
return self.query(f"SELECT {self.adapter.current_user_expr()}").fetchone()[0]
455455

456456
# ---------- transaction processing
457457
@property

0 commit comments

Comments
 (0)