You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tooling.apache.org by sb...@apache.org on 2025/04/17 13:52:15 UTC
(tooling-trusted-release) branch main updated: Add database methods for easy access to the value table
This is an automated email from the ASF dual-hosted git repository.
sbp pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tooling-trusted-release.git
The following commit(s) were added to refs/heads/main by this push:
new 9bfe517 Add database methods for easy access to the value table
9bfe517 is described below
commit 9bfe517f24b2dd8a4bc81fd900dc97104b63a14e
Author: Sean B. Palmer <se...@miscoranda.com>
AuthorDate: Thu Apr 17 14:51:46 2025 +0100
Add database methods for easy access to the value table
---
atr/db/__init__.py | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/atr/db/__init__.py b/atr/db/__init__.py
index 54a79de..23ac469 100644
--- a/atr/db/__init__.py
+++ b/atr/db/__init__.py
@@ -22,6 +22,7 @@ import os
from typing import TYPE_CHECKING, Any, Final, Generic, TypeGuard, TypeVar
import sqlalchemy
+import sqlalchemy.dialects.sqlite
import sqlalchemy.ext.asyncio
import sqlalchemy.orm as orm
import sqlalchemy.sql as sql
@@ -186,6 +187,37 @@ class Session(sqlalchemy.ext.asyncio.AsyncSession):
return Query(self, query)
+ async def ns_text_del(self, ns: str, key: str, commit: bool = True) -> None:
+ stmt = sql.delete(models.TextValue).where(
+ validate_instrumented_attribute(models.TextValue.ns) == ns,
+ validate_instrumented_attribute(models.TextValue.key) == key,
+ )
+ await self.execute(stmt)
+ if commit is True:
+ await self.commit()
+
+ async def ns_text_get(self, ns: str, key: str) -> str | None:
+ stmt = sql.select(models.TextValue).where(
+ validate_instrumented_attribute(models.TextValue.ns) == ns,
+ validate_instrumented_attribute(models.TextValue.key) == key,
+ )
+ result = await self.execute(stmt)
+ match result.scalar_one_or_none():
+ case models.TextValue(value=value):
+ return value
+ case None:
+ return None
+
+ async def ns_text_set(self, ns: str, key: str, value: str, commit: bool = True) -> None:
+ # Don't use sql.insert(), it won't give on_conflict_do_update()
+ stmt = sqlalchemy.dialects.sqlite.insert(models.TextValue).values((ns, key, value))
+ stmt = stmt.on_conflict_do_update(
+ index_elements=[models.TextValue.ns, models.TextValue.key], set_=dict(value=value)
+ )
+ await self.execute(stmt)
+ if commit is True:
+ await self.commit()
+
def package(
self,
artifact_sha3: Opt[str] = _NOT_SET,
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tooling.apache.org
For additional commands, e-mail: commits-help@tooling.apache.org