sqlspec 0.72.0__cp315-cp315-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 6b29784d6b1cafc66758__mypyc.cp315-win_amd64.pyd +0 -0
- sqlspec/__init__.py +381 -0
- sqlspec/__main__.py +14 -0
- sqlspec/__metadata__.py +14 -0
- sqlspec/_typing.py +775 -0
- sqlspec/adapters/__init__.py +3 -0
- sqlspec/adapters/adbc/__init__.py +16 -0
- sqlspec/adapters/adbc/_typing.py +221 -0
- sqlspec/adapters/adbc/adk/__init__.py +11 -0
- sqlspec/adapters/adbc/adk/store.py +4101 -0
- sqlspec/adapters/adbc/config.py +843 -0
- sqlspec/adapters/adbc/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/adbc/core.py +3209 -0
- sqlspec/adapters/adbc/data_dictionary.py +1448 -0
- sqlspec/adapters/adbc/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/adbc/driver.py +1400 -0
- sqlspec/adapters/adbc/events/__init__.py +5 -0
- sqlspec/adapters/adbc/events/store.py +384 -0
- sqlspec/adapters/adbc/litestar/__init__.py +5 -0
- sqlspec/adapters/adbc/litestar/store.py +962 -0
- sqlspec/adapters/adbc/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/adbc/pool.py +242 -0
- sqlspec/adapters/adbc/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/adbc/type_converter.py +117 -0
- sqlspec/adapters/aiomysql/__init__.py +25 -0
- sqlspec/adapters/aiomysql/_typing.py +134 -0
- sqlspec/adapters/aiomysql/adk/__init__.py +5 -0
- sqlspec/adapters/aiomysql/adk/store.py +1542 -0
- sqlspec/adapters/aiomysql/config.py +634 -0
- sqlspec/adapters/aiomysql/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/aiomysql/core.py +922 -0
- sqlspec/adapters/aiomysql/data_dictionary.py +16 -0
- sqlspec/adapters/aiomysql/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/aiomysql/driver.py +494 -0
- sqlspec/adapters/aiomysql/events/__init__.py +5 -0
- sqlspec/adapters/aiomysql/events/store.py +72 -0
- sqlspec/adapters/aiomysql/litestar/__init__.py +5 -0
- sqlspec/adapters/aiomysql/litestar/store.py +345 -0
- sqlspec/adapters/aiosqlite/__init__.py +43 -0
- sqlspec/adapters/aiosqlite/_typing.py +75 -0
- sqlspec/adapters/aiosqlite/adk/__init__.py +5 -0
- sqlspec/adapters/aiosqlite/adk/store.py +1546 -0
- sqlspec/adapters/aiosqlite/config.py +607 -0
- sqlspec/adapters/aiosqlite/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/aiosqlite/core.py +1155 -0
- sqlspec/adapters/aiosqlite/data_dictionary.py +343 -0
- sqlspec/adapters/aiosqlite/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/aiosqlite/driver.py +559 -0
- sqlspec/adapters/aiosqlite/events/__init__.py +5 -0
- sqlspec/adapters/aiosqlite/events/store.py +54 -0
- sqlspec/adapters/aiosqlite/litestar/__init__.py +5 -0
- sqlspec/adapters/aiosqlite/litestar/store.py +315 -0
- sqlspec/adapters/aiosqlite/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/aiosqlite/pool.py +1323 -0
- sqlspec/adapters/aiosqlite/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/aiosqlite/type_converter.py +114 -0
- sqlspec/adapters/arrow_odbc/__init__.py +32 -0
- sqlspec/adapters/arrow_odbc/_typing.py +131 -0
- sqlspec/adapters/arrow_odbc/adk/__init__.py +5 -0
- sqlspec/adapters/arrow_odbc/adk/store.py +1671 -0
- sqlspec/adapters/arrow_odbc/config.py +446 -0
- sqlspec/adapters/arrow_odbc/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/arrow_odbc/core.py +795 -0
- sqlspec/adapters/arrow_odbc/data_dictionary.py +345 -0
- sqlspec/adapters/arrow_odbc/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/arrow_odbc/driver.py +885 -0
- sqlspec/adapters/arrow_odbc/events/__init__.py +5 -0
- sqlspec/adapters/arrow_odbc/events/store.py +114 -0
- sqlspec/adapters/arrow_odbc/litestar/__init__.py +5 -0
- sqlspec/adapters/arrow_odbc/litestar/store.py +682 -0
- sqlspec/adapters/arrow_odbc/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/arrow_odbc/type_converter.py +95 -0
- sqlspec/adapters/asyncmy/__init__.py +27 -0
- sqlspec/adapters/asyncmy/_typing.py +215 -0
- sqlspec/adapters/asyncmy/adk/__init__.py +5 -0
- sqlspec/adapters/asyncmy/adk/store.py +1544 -0
- sqlspec/adapters/asyncmy/config.py +707 -0
- sqlspec/adapters/asyncmy/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/asyncmy/core.py +917 -0
- sqlspec/adapters/asyncmy/data_dictionary.py +16 -0
- sqlspec/adapters/asyncmy/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/asyncmy/driver.py +529 -0
- sqlspec/adapters/asyncmy/events/__init__.py +5 -0
- sqlspec/adapters/asyncmy/events/store.py +72 -0
- sqlspec/adapters/asyncmy/litestar/__init__.py +5 -0
- sqlspec/adapters/asyncmy/litestar/store.py +348 -0
- sqlspec/adapters/asyncpg/__init__.py +28 -0
- sqlspec/adapters/asyncpg/_typing.py +113 -0
- sqlspec/adapters/asyncpg/adk/__init__.py +5 -0
- sqlspec/adapters/asyncpg/adk/store.py +1145 -0
- sqlspec/adapters/asyncpg/config.py +903 -0
- sqlspec/adapters/asyncpg/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/asyncpg/core.py +797 -0
- sqlspec/adapters/asyncpg/data_dictionary.py +417 -0
- sqlspec/adapters/asyncpg/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/asyncpg/driver.py +927 -0
- sqlspec/adapters/asyncpg/events/__init__.py +6 -0
- sqlspec/adapters/asyncpg/events/_hub.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/asyncpg/events/_hub.py +279 -0
- sqlspec/adapters/asyncpg/events/backend.py +410 -0
- sqlspec/adapters/asyncpg/events/store.py +57 -0
- sqlspec/adapters/asyncpg/litestar/__init__.py +5 -0
- sqlspec/adapters/asyncpg/litestar/store.py +278 -0
- sqlspec/adapters/asyncpg/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/asyncpg/tenant.py +140 -0
- sqlspec/adapters/bigquery/__init__.py +23 -0
- sqlspec/adapters/bigquery/_typing.py +184 -0
- sqlspec/adapters/bigquery/adk/__init__.py +5 -0
- sqlspec/adapters/bigquery/adk/store.py +920 -0
- sqlspec/adapters/bigquery/config.py +631 -0
- sqlspec/adapters/bigquery/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/bigquery/core.py +1433 -0
- sqlspec/adapters/bigquery/data_dictionary.py +502 -0
- sqlspec/adapters/bigquery/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/bigquery/driver.py +1266 -0
- sqlspec/adapters/bigquery/events/__init__.py +5 -0
- sqlspec/adapters/bigquery/events/store.py +158 -0
- sqlspec/adapters/bigquery/litestar/__init__.py +5 -0
- sqlspec/adapters/bigquery/litestar/store.py +359 -0
- sqlspec/adapters/cockroach_asyncpg/__init__.py +32 -0
- sqlspec/adapters/cockroach_asyncpg/_typing.py +54 -0
- sqlspec/adapters/cockroach_asyncpg/adk/__init__.py +9 -0
- sqlspec/adapters/cockroach_asyncpg/adk/store.py +1096 -0
- sqlspec/adapters/cockroach_asyncpg/config.py +653 -0
- sqlspec/adapters/cockroach_asyncpg/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/cockroach_asyncpg/core.py +835 -0
- sqlspec/adapters/cockroach_asyncpg/data_dictionary.py +371 -0
- sqlspec/adapters/cockroach_asyncpg/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/cockroach_asyncpg/driver.py +438 -0
- sqlspec/adapters/cockroach_asyncpg/events/__init__.py +5 -0
- sqlspec/adapters/cockroach_asyncpg/events/store.py +20 -0
- sqlspec/adapters/cockroach_asyncpg/litestar/__init__.py +5 -0
- sqlspec/adapters/cockroach_asyncpg/litestar/store.py +203 -0
- sqlspec/adapters/cockroach_asyncpg/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/cockroach_asyncpg/tenant.py +143 -0
- sqlspec/adapters/cockroach_psycopg/__init__.py +46 -0
- sqlspec/adapters/cockroach_psycopg/_typing.py +271 -0
- sqlspec/adapters/cockroach_psycopg/adk/__init__.py +17 -0
- sqlspec/adapters/cockroach_psycopg/adk/store.py +2274 -0
- sqlspec/adapters/cockroach_psycopg/config.py +1285 -0
- sqlspec/adapters/cockroach_psycopg/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/cockroach_psycopg/core.py +667 -0
- sqlspec/adapters/cockroach_psycopg/data_dictionary.py +622 -0
- sqlspec/adapters/cockroach_psycopg/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/cockroach_psycopg/driver.py +864 -0
- sqlspec/adapters/cockroach_psycopg/events/__init__.py +8 -0
- sqlspec/adapters/cockroach_psycopg/events/store.py +34 -0
- sqlspec/adapters/cockroach_psycopg/litestar/__init__.py +9 -0
- sqlspec/adapters/cockroach_psycopg/litestar/store.py +413 -0
- sqlspec/adapters/cockroach_psycopg/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/cockroach_psycopg/tenant.py +222 -0
- sqlspec/adapters/db2/__init__.py +34 -0
- sqlspec/adapters/db2/_typing.py +357 -0
- sqlspec/adapters/db2/adk/__init__.py +10 -0
- sqlspec/adapters/db2/adk/store.py +1950 -0
- sqlspec/adapters/db2/config.py +550 -0
- sqlspec/adapters/db2/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/db2/core.py +1021 -0
- sqlspec/adapters/db2/data_dictionary.py +493 -0
- sqlspec/adapters/db2/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/db2/driver.py +898 -0
- sqlspec/adapters/db2/events/__init__.py +5 -0
- sqlspec/adapters/db2/events/store.py +77 -0
- sqlspec/adapters/db2/litestar/__init__.py +5 -0
- sqlspec/adapters/db2/litestar/store.py +519 -0
- sqlspec/adapters/db2/migrations.py +221 -0
- sqlspec/adapters/db2/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/db2/pool.py +876 -0
- sqlspec/adapters/db2/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/db2/tenant.py +292 -0
- sqlspec/adapters/duckdb/__init__.py +30 -0
- sqlspec/adapters/duckdb/_typing.py +182 -0
- sqlspec/adapters/duckdb/adk/__init__.py +10 -0
- sqlspec/adapters/duckdb/adk/store.py +1672 -0
- sqlspec/adapters/duckdb/config.py +506 -0
- sqlspec/adapters/duckdb/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/duckdb/core.py +1299 -0
- sqlspec/adapters/duckdb/data_dictionary.py +553 -0
- sqlspec/adapters/duckdb/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/duckdb/driver.py +1214 -0
- sqlspec/adapters/duckdb/events/__init__.py +5 -0
- sqlspec/adapters/duckdb/events/store.py +38 -0
- sqlspec/adapters/duckdb/litestar/__init__.py +5 -0
- sqlspec/adapters/duckdb/litestar/store.py +313 -0
- sqlspec/adapters/duckdb/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/duckdb/pool.py +1121 -0
- sqlspec/adapters/mssql_python/__init__.py +42 -0
- sqlspec/adapters/mssql_python/_typing.py +135 -0
- sqlspec/adapters/mssql_python/adk/__init__.py +5 -0
- sqlspec/adapters/mssql_python/adk/store.py +1278 -0
- sqlspec/adapters/mssql_python/config.py +455 -0
- sqlspec/adapters/mssql_python/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mssql_python/core.py +862 -0
- sqlspec/adapters/mssql_python/data_dictionary.py +417 -0
- sqlspec/adapters/mssql_python/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mssql_python/driver.py +772 -0
- sqlspec/adapters/mssql_python/events/__init__.py +5 -0
- sqlspec/adapters/mssql_python/events/store.py +46 -0
- sqlspec/adapters/mssql_python/litestar/__init__.py +5 -0
- sqlspec/adapters/mssql_python/litestar/store.py +355 -0
- sqlspec/adapters/mssql_python/migrations.py +162 -0
- sqlspec/adapters/mssql_python/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mssql_python/pool.py +165 -0
- sqlspec/adapters/mssql_python/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mssql_python/tenant.py +74 -0
- sqlspec/adapters/mssql_python/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mssql_python/type_converter.py +98 -0
- sqlspec/adapters/mysqlconnector/__init__.py +47 -0
- sqlspec/adapters/mysqlconnector/_typing.py +205 -0
- sqlspec/adapters/mysqlconnector/adk/__init__.py +17 -0
- sqlspec/adapters/mysqlconnector/adk/store.py +2503 -0
- sqlspec/adapters/mysqlconnector/config.py +925 -0
- sqlspec/adapters/mysqlconnector/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mysqlconnector/core.py +1075 -0
- sqlspec/adapters/mysqlconnector/data_dictionary.py +23 -0
- sqlspec/adapters/mysqlconnector/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/mysqlconnector/driver.py +702 -0
- sqlspec/adapters/mysqlconnector/events/__init__.py +8 -0
- sqlspec/adapters/mysqlconnector/events/store.py +89 -0
- sqlspec/adapters/mysqlconnector/litestar/__init__.py +9 -0
- sqlspec/adapters/mysqlconnector/litestar/store.py +524 -0
- sqlspec/adapters/oracledb/__init__.py +159 -0
- sqlspec/adapters/oracledb/_json_handlers.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/_json_handlers.py +208 -0
- sqlspec/adapters/oracledb/_param_types.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/_param_types.py +65 -0
- sqlspec/adapters/oracledb/_plugins.py +137 -0
- sqlspec/adapters/oracledb/_storage.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/_storage.py +138 -0
- sqlspec/adapters/oracledb/_typing.py +468 -0
- sqlspec/adapters/oracledb/_uuid_handlers.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/_uuid_handlers.py +143 -0
- sqlspec/adapters/oracledb/_vector_handlers.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/_vector_handlers.py +252 -0
- sqlspec/adapters/oracledb/adk/__init__.py +27 -0
- sqlspec/adapters/oracledb/adk/store.py +4031 -0
- sqlspec/adapters/oracledb/config.py +1785 -0
- sqlspec/adapters/oracledb/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/core.py +5416 -0
- sqlspec/adapters/oracledb/data_dictionary.py +1264 -0
- sqlspec/adapters/oracledb/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/driver.py +3273 -0
- sqlspec/adapters/oracledb/events/__init__.py +29 -0
- sqlspec/adapters/oracledb/events/_hub.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/events/_hub.py +394 -0
- sqlspec/adapters/oracledb/events/backend.py +311 -0
- sqlspec/adapters/oracledb/events/store.py +842 -0
- sqlspec/adapters/oracledb/litestar/__init__.py +17 -0
- sqlspec/adapters/oracledb/litestar/store.py +862 -0
- sqlspec/adapters/oracledb/migrations.py +650 -0
- sqlspec/adapters/oracledb/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/tenant.py +990 -0
- sqlspec/adapters/oracledb/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/oracledb/type_converter.py +93 -0
- sqlspec/adapters/psqlpy/__init__.py +21 -0
- sqlspec/adapters/psqlpy/_typing.py +180 -0
- sqlspec/adapters/psqlpy/adk/__init__.py +5 -0
- sqlspec/adapters/psqlpy/adk/store.py +1514 -0
- sqlspec/adapters/psqlpy/config.py +828 -0
- sqlspec/adapters/psqlpy/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psqlpy/core.py +1040 -0
- sqlspec/adapters/psqlpy/data_dictionary.py +399 -0
- sqlspec/adapters/psqlpy/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psqlpy/driver.py +631 -0
- sqlspec/adapters/psqlpy/events/__init__.py +12 -0
- sqlspec/adapters/psqlpy/events/_hub.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psqlpy/events/_hub.py +367 -0
- sqlspec/adapters/psqlpy/events/backend.py +415 -0
- sqlspec/adapters/psqlpy/events/store.py +46 -0
- sqlspec/adapters/psqlpy/litestar/__init__.py +5 -0
- sqlspec/adapters/psqlpy/litestar/store.py +270 -0
- sqlspec/adapters/psqlpy/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psqlpy/tenant.py +137 -0
- sqlspec/adapters/psqlpy/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psqlpy/type_converter.py +21 -0
- sqlspec/adapters/psycopg/__init__.py +44 -0
- sqlspec/adapters/psycopg/_typing.py +408 -0
- sqlspec/adapters/psycopg/adk/__init__.py +17 -0
- sqlspec/adapters/psycopg/adk/store.py +2761 -0
- sqlspec/adapters/psycopg/config.py +1522 -0
- sqlspec/adapters/psycopg/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psycopg/core.py +1001 -0
- sqlspec/adapters/psycopg/data_dictionary.py +701 -0
- sqlspec/adapters/psycopg/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psycopg/driver.py +1629 -0
- sqlspec/adapters/psycopg/events/__init__.py +25 -0
- sqlspec/adapters/psycopg/events/_hub.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psycopg/events/_hub.py +646 -0
- sqlspec/adapters/psycopg/events/backend.py +739 -0
- sqlspec/adapters/psycopg/events/store.py +82 -0
- sqlspec/adapters/psycopg/litestar/__init__.py +5 -0
- sqlspec/adapters/psycopg/litestar/store.py +595 -0
- sqlspec/adapters/psycopg/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psycopg/tenant.py +226 -0
- sqlspec/adapters/psycopg/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/psycopg/type_converter.py +94 -0
- sqlspec/adapters/pymssql/__init__.py +29 -0
- sqlspec/adapters/pymssql/_typing.py +156 -0
- sqlspec/adapters/pymssql/adk/__init__.py +5 -0
- sqlspec/adapters/pymssql/adk/store.py +1270 -0
- sqlspec/adapters/pymssql/config.py +299 -0
- sqlspec/adapters/pymssql/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymssql/core.py +527 -0
- sqlspec/adapters/pymssql/data_dictionary.py +417 -0
- sqlspec/adapters/pymssql/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymssql/driver.py +527 -0
- sqlspec/adapters/pymssql/events/__init__.py +5 -0
- sqlspec/adapters/pymssql/events/store.py +46 -0
- sqlspec/adapters/pymssql/litestar/__init__.py +5 -0
- sqlspec/adapters/pymssql/litestar/store.py +350 -0
- sqlspec/adapters/pymssql/migrations.py +162 -0
- sqlspec/adapters/pymssql/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymssql/pool.py +417 -0
- sqlspec/adapters/pymssql/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymssql/tenant.py +73 -0
- sqlspec/adapters/pymysql/__init__.py +27 -0
- sqlspec/adapters/pymysql/_typing.py +105 -0
- sqlspec/adapters/pymysql/adk/__init__.py +5 -0
- sqlspec/adapters/pymysql/adk/store.py +1646 -0
- sqlspec/adapters/pymysql/config.py +545 -0
- sqlspec/adapters/pymysql/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymysql/core.py +909 -0
- sqlspec/adapters/pymysql/data_dictionary.py +16 -0
- sqlspec/adapters/pymysql/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymysql/driver.py +370 -0
- sqlspec/adapters/pymysql/events/__init__.py +5 -0
- sqlspec/adapters/pymysql/events/store.py +42 -0
- sqlspec/adapters/pymysql/litestar/__init__.py +5 -0
- sqlspec/adapters/pymysql/litestar/store.py +299 -0
- sqlspec/adapters/pymysql/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/pymysql/pool.py +480 -0
- sqlspec/adapters/snowflake/__init__.py +36 -0
- sqlspec/adapters/snowflake/_typing.py +62 -0
- sqlspec/adapters/snowflake/adk/__init__.py +5 -0
- sqlspec/adapters/snowflake/adk/store.py +778 -0
- sqlspec/adapters/snowflake/config.py +333 -0
- sqlspec/adapters/snowflake/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/snowflake/core.py +683 -0
- sqlspec/adapters/snowflake/data_dictionary.py +355 -0
- sqlspec/adapters/snowflake/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/snowflake/driver.py +872 -0
- sqlspec/adapters/snowflake/events/__init__.py +5 -0
- sqlspec/adapters/snowflake/events/store.py +28 -0
- sqlspec/adapters/snowflake/litestar/__init__.py +5 -0
- sqlspec/adapters/snowflake/litestar/store.py +162 -0
- sqlspec/adapters/snowflake/migrations.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/snowflake/migrations.py +76 -0
- sqlspec/adapters/snowflake/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/snowflake/pool.py +385 -0
- sqlspec/adapters/snowflake/tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/snowflake/tenant.py +200 -0
- sqlspec/adapters/snowflake/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/snowflake/type_converter.py +33 -0
- sqlspec/adapters/spanner/__init__.py +57 -0
- sqlspec/adapters/spanner/_typing.py +300 -0
- sqlspec/adapters/spanner/adk/__init__.py +22 -0
- sqlspec/adapters/spanner/adk/artifact_store.py +693 -0
- sqlspec/adapters/spanner/adk/store.py +3053 -0
- sqlspec/adapters/spanner/config.py +1578 -0
- sqlspec/adapters/spanner/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/spanner/core.py +1244 -0
- sqlspec/adapters/spanner/data_dictionary.py +706 -0
- sqlspec/adapters/spanner/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/spanner/driver.py +1673 -0
- sqlspec/adapters/spanner/events/__init__.py +5 -0
- sqlspec/adapters/spanner/events/store.py +357 -0
- sqlspec/adapters/spanner/litestar/__init__.py +5 -0
- sqlspec/adapters/spanner/litestar/store.py +570 -0
- sqlspec/adapters/spanner/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/spanner/type_converter.py +466 -0
- sqlspec/adapters/sqlanywhere/__init__.py +40 -0
- sqlspec/adapters/sqlanywhere/_typing.py +139 -0
- sqlspec/adapters/sqlanywhere/adk/__init__.py +5 -0
- sqlspec/adapters/sqlanywhere/adk/store.py +1136 -0
- sqlspec/adapters/sqlanywhere/config.py +359 -0
- sqlspec/adapters/sqlanywhere/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlanywhere/core.py +583 -0
- sqlspec/adapters/sqlanywhere/data_dictionary.py +282 -0
- sqlspec/adapters/sqlanywhere/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlanywhere/driver.py +480 -0
- sqlspec/adapters/sqlanywhere/events/__init__.py +5 -0
- sqlspec/adapters/sqlanywhere/events/store.py +28 -0
- sqlspec/adapters/sqlanywhere/litestar/__init__.py +5 -0
- sqlspec/adapters/sqlanywhere/litestar/store.py +237 -0
- sqlspec/adapters/sqlanywhere/migrations.py +178 -0
- sqlspec/adapters/sqlanywhere/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlanywhere/pool.py +397 -0
- sqlspec/adapters/sqlite/__init__.py +32 -0
- sqlspec/adapters/sqlite/_typing.py +91 -0
- sqlspec/adapters/sqlite/adk/__init__.py +5 -0
- sqlspec/adapters/sqlite/adk/store.py +1628 -0
- sqlspec/adapters/sqlite/config.py +520 -0
- sqlspec/adapters/sqlite/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlite/core.py +1035 -0
- sqlspec/adapters/sqlite/data_dictionary.py +327 -0
- sqlspec/adapters/sqlite/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlite/driver.py +432 -0
- sqlspec/adapters/sqlite/events/__init__.py +5 -0
- sqlspec/adapters/sqlite/events/store.py +54 -0
- sqlspec/adapters/sqlite/litestar/__init__.py +5 -0
- sqlspec/adapters/sqlite/litestar/store.py +367 -0
- sqlspec/adapters/sqlite/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlite/pool.py +935 -0
- sqlspec/adapters/sqlite/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sqlite/type_converter.py +114 -0
- sqlspec/adapters/sybase/__init__.py +37 -0
- sqlspec/adapters/sybase/_typing.py +150 -0
- sqlspec/adapters/sybase/adk/__init__.py +5 -0
- sqlspec/adapters/sybase/adk/store.py +1370 -0
- sqlspec/adapters/sybase/config.py +276 -0
- sqlspec/adapters/sybase/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sybase/core.py +578 -0
- sqlspec/adapters/sybase/data_dictionary.py +358 -0
- sqlspec/adapters/sybase/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sybase/driver.py +497 -0
- sqlspec/adapters/sybase/events/__init__.py +5 -0
- sqlspec/adapters/sybase/events/store.py +88 -0
- sqlspec/adapters/sybase/litestar/__init__.py +5 -0
- sqlspec/adapters/sybase/litestar/store.py +328 -0
- sqlspec/adapters/sybase/migrations.py +178 -0
- sqlspec/adapters/sybase/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/sybase/pool.py +435 -0
- sqlspec/adapters/teradata/__init__.py +59 -0
- sqlspec/adapters/teradata/_typing.py +145 -0
- sqlspec/adapters/teradata/adk/__init__.py +5 -0
- sqlspec/adapters/teradata/adk/store.py +1300 -0
- sqlspec/adapters/teradata/config.py +324 -0
- sqlspec/adapters/teradata/core.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/teradata/core.py +437 -0
- sqlspec/adapters/teradata/data_dictionary.py +316 -0
- sqlspec/adapters/teradata/driver.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/teradata/driver.py +721 -0
- sqlspec/adapters/teradata/events/__init__.py +5 -0
- sqlspec/adapters/teradata/events/store.py +64 -0
- sqlspec/adapters/teradata/litestar/__init__.py +5 -0
- sqlspec/adapters/teradata/litestar/store.py +264 -0
- sqlspec/adapters/teradata/migrations.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/teradata/migrations.py +80 -0
- sqlspec/adapters/teradata/pool.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/teradata/pool.py +377 -0
- sqlspec/adapters/teradata/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/adapters/teradata/type_converter.py +95 -0
- sqlspec/base.cp315-win_amd64.pyd +0 -0
- sqlspec/base.py +1275 -0
- sqlspec/builder/__init__.py +330 -0
- sqlspec/builder/_base.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_base.py +1752 -0
- sqlspec/builder/_column.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_column.py +508 -0
- sqlspec/builder/_ddl.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_ddl.py +4503 -0
- sqlspec/builder/_delete.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_delete.py +75 -0
- sqlspec/builder/_dml.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_dml.py +639 -0
- sqlspec/builder/_explain.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_explain.py +650 -0
- sqlspec/builder/_expression_wrappers.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_expression_wrappers.py +77 -0
- sqlspec/builder/_factory.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_factory.py +1834 -0
- sqlspec/builder/_generation.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_generation.py +55 -0
- sqlspec/builder/_insert.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_insert.py +308 -0
- sqlspec/builder/_join.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_join.py +501 -0
- sqlspec/builder/_locking.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_locking.py +77 -0
- sqlspec/builder/_merge.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_merge.py +1047 -0
- sqlspec/builder/_parsing_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_parsing_utils.py +1197 -0
- sqlspec/builder/_select.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_select.py +1514 -0
- sqlspec/builder/_temporal.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_temporal.py +183 -0
- sqlspec/builder/_update.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_update.py +139 -0
- sqlspec/builder/_values.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_values.py +271 -0
- sqlspec/builder/_vector_distance.cp315-win_amd64.pyd +0 -0
- sqlspec/builder/_vector_distance.py +763 -0
- sqlspec/cli.py +1656 -0
- sqlspec/config.py +3402 -0
- sqlspec/core/__init__.py +542 -0
- sqlspec/core/_cursor.cp315-win_amd64.pyd +0 -0
- sqlspec/core/_cursor.py +564 -0
- sqlspec/core/_operators.cp315-win_amd64.pyd +0 -0
- sqlspec/core/_operators.py +167 -0
- sqlspec/core/_ordering.cp315-win_amd64.pyd +0 -0
- sqlspec/core/_ordering.py +151 -0
- sqlspec/core/_pagination.py +66 -0
- sqlspec/core/_pipeline.cp315-win_amd64.pyd +0 -0
- sqlspec/core/_pipeline.py +343 -0
- sqlspec/core/_pool.cp315-win_amd64.pyd +0 -0
- sqlspec/core/_pool.py +72 -0
- sqlspec/core/cache.cp315-win_amd64.pyd +0 -0
- sqlspec/core/cache.py +1503 -0
- sqlspec/core/capabilities.cp315-win_amd64.pyd +0 -0
- sqlspec/core/capabilities.py +67 -0
- sqlspec/core/compiler.cp315-win_amd64.pyd +0 -0
- sqlspec/core/compiler.py +1667 -0
- sqlspec/core/config_runtime.cp315-win_amd64.pyd +0 -0
- sqlspec/core/config_runtime.py +219 -0
- sqlspec/core/correlation.cp315-win_amd64.pyd +0 -0
- sqlspec/core/correlation.py +135 -0
- sqlspec/core/explain.cp315-win_amd64.pyd +0 -0
- sqlspec/core/explain.py +284 -0
- sqlspec/core/filters.cp315-win_amd64.pyd +0 -0
- sqlspec/core/filters.py +1442 -0
- sqlspec/core/hashing.cp315-win_amd64.pyd +0 -0
- sqlspec/core/hashing.py +394 -0
- sqlspec/core/metrics.cp315-win_amd64.pyd +0 -0
- sqlspec/core/metrics.py +86 -0
- sqlspec/core/parameters/__init__.py +174 -0
- sqlspec/core/parameters/_alignment.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_alignment.py +321 -0
- sqlspec/core/parameters/_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_converter.py +956 -0
- sqlspec/core/parameters/_declared.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_declared.py +154 -0
- sqlspec/core/parameters/_processor.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_processor.py +1646 -0
- sqlspec/core/parameters/_registry.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_registry.py +207 -0
- sqlspec/core/parameters/_transformers.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_transformers.py +318 -0
- sqlspec/core/parameters/_types.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_types.py +658 -0
- sqlspec/core/parameters/_validator.cp315-win_amd64.pyd +0 -0
- sqlspec/core/parameters/_validator.py +699 -0
- sqlspec/core/query_modifiers.cp315-win_amd64.pyd +0 -0
- sqlspec/core/query_modifiers.py +559 -0
- sqlspec/core/result/__init__.py +67 -0
- sqlspec/core/result/_base.cp315-win_amd64.pyd +0 -0
- sqlspec/core/result/_base.py +1772 -0
- sqlspec/core/routing.cp315-win_amd64.pyd +0 -0
- sqlspec/core/routing.py +270 -0
- sqlspec/core/splitter.cp315-win_amd64.pyd +0 -0
- sqlspec/core/splitter.py +1126 -0
- sqlspec/core/sqlcommenter.cp315-win_amd64.pyd +0 -0
- sqlspec/core/sqlcommenter.py +509 -0
- sqlspec/core/stack.cp315-win_amd64.pyd +0 -0
- sqlspec/core/stack.py +167 -0
- sqlspec/core/statement.cp315-win_amd64.pyd +0 -0
- sqlspec/core/statement.py +2253 -0
- sqlspec/core/tenant/__init__.py +64 -0
- sqlspec/core/tenant/base.cp315-win_amd64.pyd +0 -0
- sqlspec/core/tenant/base.py +123 -0
- sqlspec/core/type_converter.cp315-win_amd64.pyd +0 -0
- sqlspec/core/type_converter.py +147 -0
- sqlspec/data_dictionary/__init__.py +237 -0
- sqlspec/data_dictionary/_capabilities.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/_capabilities.py +142 -0
- sqlspec/data_dictionary/_dependencies.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/_dependencies.py +602 -0
- sqlspec/data_dictionary/_index_ddl.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/_index_ddl.py +562 -0
- sqlspec/data_dictionary/_loader.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/_loader.py +344 -0
- sqlspec/data_dictionary/_registry.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/_registry.py +152 -0
- sqlspec/data_dictionary/_types.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/_types.py +1852 -0
- sqlspec/data_dictionary/dialects/__init__.py +34 -0
- sqlspec/data_dictionary/dialects/bigquery/__init__.py +15 -0
- sqlspec/data_dictionary/dialects/bigquery/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/bigquery/config.py +128 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/columns.sql +63 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/constraints.sql +37 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/datasets.sql +14 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/ddl.sql +51 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/dependencies.sql +22 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/foreign_keys.sql +34 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/indexes.sql +59 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/objects.sql +13 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/privileges.sql +13 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/routines.sql +50 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/system.sql +59 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/tables.sql +111 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/bigquery/sql/views.sql +31 -0
- sqlspec/data_dictionary/dialects/cockroachdb/__init__.py +17 -0
- sqlspec/data_dictionary/dialects/cockroachdb/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/cockroachdb/config.py +95 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/columns.sql +163 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/comments.sql +10 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/constraints.sql +67 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/crdb_internal.sql +11 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/ddl.sql +10 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/dependencies.sql +20 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/foreign_keys.sql +62 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/indexes.sql +32 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/objects.sql +15 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/privileges.sql +13 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/schemas.sql +10 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/sequences.sql +14 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/tables.sql +15 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/cockroachdb/sql/views.sql +11 -0
- sqlspec/data_dictionary/dialects/db2/__init__.py +31 -0
- sqlspec/data_dictionary/dialects/db2/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/db2/config.py +172 -0
- sqlspec/data_dictionary/dialects/db2/sql/columns.sql +98 -0
- sqlspec/data_dictionary/dialects/db2/sql/constraints.sql +49 -0
- sqlspec/data_dictionary/dialects/db2/sql/foreign_keys.sql +43 -0
- sqlspec/data_dictionary/dialects/db2/sql/indexes.sql +39 -0
- sqlspec/data_dictionary/dialects/db2/sql/objects.sql +53 -0
- sqlspec/data_dictionary/dialects/db2/sql/schemas.sql +13 -0
- sqlspec/data_dictionary/dialects/db2/sql/tables.sql +77 -0
- sqlspec/data_dictionary/dialects/db2/sql/version.sql +8 -0
- sqlspec/data_dictionary/dialects/db2/sql/views.sql +15 -0
- sqlspec/data_dictionary/dialects/duckdb/__init__.py +5 -0
- sqlspec/data_dictionary/dialects/duckdb/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/duckdb/config.py +66 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/columns.sql +150 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/constraints.sql +34 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/databases.sql +11 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/ddl.sql +221 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/dependencies.sql +50 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/extensions.sql +14 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/foreign_keys.sql +46 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/functions.sql +18 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/indexes.sql +34 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/objects.sql +30 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/schemas.sql +10 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/sequences.sql +19 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/system.sql +21 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/tables.sql +59 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/types.sql +12 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/duckdb/sql/views.sql +15 -0
- sqlspec/data_dictionary/dialects/mariadb/__init__.py +5 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/columns.sql +45 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/constraints.sql +30 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/ddl.sql +27 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/events.sql +29 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/foreign_keys.sql +42 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/indexes.sql +33 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/objects.sql +50 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/partitions.sql +27 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/plugins.sql +16 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/privileges.sql +24 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/routines.sql +21 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/schemas.sql +10 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/system.sql +12 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/tables.sql +64 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/triggers.sql +21 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/version.sql +7 -0
- sqlspec/data_dictionary/dialects/mariadb/sql/views.sql +15 -0
- sqlspec/data_dictionary/dialects/mssql/__init__.py +51 -0
- sqlspec/data_dictionary/dialects/mssql/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/mssql/config.py +558 -0
- sqlspec/data_dictionary/dialects/mssql/sql/columns.sql +120 -0
- sqlspec/data_dictionary/dialects/mssql/sql/constraints.sql +106 -0
- sqlspec/data_dictionary/dialects/mssql/sql/ddl.sql +89 -0
- sqlspec/data_dictionary/dialects/mssql/sql/dependencies.sql +17 -0
- sqlspec/data_dictionary/dialects/mssql/sql/extended_properties.sql +16 -0
- sqlspec/data_dictionary/dialects/mssql/sql/foreign_keys.sql +40 -0
- sqlspec/data_dictionary/dialects/mssql/sql/indexes.sql +86 -0
- sqlspec/data_dictionary/dialects/mssql/sql/modules.sql +19 -0
- sqlspec/data_dictionary/dialects/mssql/sql/objects.sql +16 -0
- sqlspec/data_dictionary/dialects/mssql/sql/permissions.sql +31 -0
- sqlspec/data_dictionary/dialects/mssql/sql/routines.sql +39 -0
- sqlspec/data_dictionary/dialects/mssql/sql/schemas.sql +13 -0
- sqlspec/data_dictionary/dialects/mssql/sql/sequences.sql +18 -0
- sqlspec/data_dictionary/dialects/mssql/sql/storage.sql +22 -0
- sqlspec/data_dictionary/dialects/mssql/sql/system.sql +38 -0
- sqlspec/data_dictionary/dialects/mssql/sql/tables.sql +97 -0
- sqlspec/data_dictionary/dialects/mssql/sql/triggers.sql +21 -0
- sqlspec/data_dictionary/dialects/mssql/sql/version.sql +7 -0
- sqlspec/data_dictionary/dialects/mssql/sql/views.sql +18 -0
- sqlspec/data_dictionary/dialects/mysql/__init__.py +34 -0
- sqlspec/data_dictionary/dialects/mysql/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/mysql/config.py +363 -0
- sqlspec/data_dictionary/dialects/mysql/dictionary.py +650 -0
- sqlspec/data_dictionary/dialects/mysql/sql/columns.sql +45 -0
- sqlspec/data_dictionary/dialects/mysql/sql/components.sql +8 -0
- sqlspec/data_dictionary/dialects/mysql/sql/constraints.sql +30 -0
- sqlspec/data_dictionary/dialects/mysql/sql/ddl.sql +23 -0
- sqlspec/data_dictionary/dialects/mysql/sql/events.sql +29 -0
- sqlspec/data_dictionary/dialects/mysql/sql/foreign_keys.sql +42 -0
- sqlspec/data_dictionary/dialects/mysql/sql/indexes.sql +33 -0
- sqlspec/data_dictionary/dialects/mysql/sql/objects.sql +38 -0
- sqlspec/data_dictionary/dialects/mysql/sql/partitions.sql +27 -0
- sqlspec/data_dictionary/dialects/mysql/sql/plugins.sql +16 -0
- sqlspec/data_dictionary/dialects/mysql/sql/privileges.sql +26 -0
- sqlspec/data_dictionary/dialects/mysql/sql/routines.sql +21 -0
- sqlspec/data_dictionary/dialects/mysql/sql/schemas.sql +10 -0
- sqlspec/data_dictionary/dialects/mysql/sql/system.sql +25 -0
- sqlspec/data_dictionary/dialects/mysql/sql/tables.sql +64 -0
- sqlspec/data_dictionary/dialects/mysql/sql/triggers.sql +21 -0
- sqlspec/data_dictionary/dialects/mysql/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/mysql/sql/views.sql +15 -0
- sqlspec/data_dictionary/dialects/oracle/__init__.py +39 -0
- sqlspec/data_dictionary/dialects/oracle/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/oracle/config.py +266 -0
- sqlspec/data_dictionary/dialects/oracle/sql/arguments.sql +29 -0
- sqlspec/data_dictionary/dialects/oracle/sql/columns.sql +103 -0
- sqlspec/data_dictionary/dialects/oracle/sql/comments.sql +22 -0
- sqlspec/data_dictionary/dialects/oracle/sql/constraints.sql +49 -0
- sqlspec/data_dictionary/dialects/oracle/sql/ddl.sql +4 -0
- sqlspec/data_dictionary/dialects/oracle/sql/dependencies.sql +16 -0
- sqlspec/data_dictionary/dialects/oracle/sql/foreign_keys.sql +48 -0
- sqlspec/data_dictionary/dialects/oracle/sql/grants.sql +34 -0
- sqlspec/data_dictionary/dialects/oracle/sql/indexes.sql +87 -0
- sqlspec/data_dictionary/dialects/oracle/sql/lob_storage.sql +28 -0
- sqlspec/data_dictionary/dialects/oracle/sql/materialized_views.sql +27 -0
- sqlspec/data_dictionary/dialects/oracle/sql/objects.sql +26 -0
- sqlspec/data_dictionary/dialects/oracle/sql/partitions.sql +44 -0
- sqlspec/data_dictionary/dialects/oracle/sql/routines.sql +21 -0
- sqlspec/data_dictionary/dialects/oracle/sql/schemas.sql +13 -0
- sqlspec/data_dictionary/dialects/oracle/sql/sequences.sql +21 -0
- sqlspec/data_dictionary/dialects/oracle/sql/source.sql +13 -0
- sqlspec/data_dictionary/dialects/oracle/sql/system.sql +37 -0
- sqlspec/data_dictionary/dialects/oracle/sql/tables.sql +48 -0
- sqlspec/data_dictionary/dialects/oracle/sql/triggers.sql +29 -0
- sqlspec/data_dictionary/dialects/oracle/sql/version.sql +25 -0
- sqlspec/data_dictionary/dialects/oracle/sql/views.sql +49 -0
- sqlspec/data_dictionary/dialects/postgres/__init__.py +19 -0
- sqlspec/data_dictionary/dialects/postgres/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/postgres/config.py +119 -0
- sqlspec/data_dictionary/dialects/postgres/sql/columns.sql +130 -0
- sqlspec/data_dictionary/dialects/postgres/sql/comments.sql +27 -0
- sqlspec/data_dictionary/dialects/postgres/sql/constraints.sql +53 -0
- sqlspec/data_dictionary/dialects/postgres/sql/ddl.sql +55 -0
- sqlspec/data_dictionary/dialects/postgres/sql/dependencies.sql +42 -0
- sqlspec/data_dictionary/dialects/postgres/sql/extensions.sql +11 -0
- sqlspec/data_dictionary/dialects/postgres/sql/foreign_keys.sql +72 -0
- sqlspec/data_dictionary/dialects/postgres/sql/indexes.sql +84 -0
- sqlspec/data_dictionary/dialects/postgres/sql/materialized_views.sql +17 -0
- sqlspec/data_dictionary/dialects/postgres/sql/objects.sql +25 -0
- sqlspec/data_dictionary/dialects/postgres/sql/partitions.sql +19 -0
- sqlspec/data_dictionary/dialects/postgres/sql/privileges.sql +29 -0
- sqlspec/data_dictionary/dialects/postgres/sql/routines.sql +25 -0
- sqlspec/data_dictionary/dialects/postgres/sql/schemas.sql +13 -0
- sqlspec/data_dictionary/dialects/postgres/sql/sequences.sql +32 -0
- sqlspec/data_dictionary/dialects/postgres/sql/system.sql +53 -0
- sqlspec/data_dictionary/dialects/postgres/sql/tables.sql +67 -0
- sqlspec/data_dictionary/dialects/postgres/sql/triggers.sql +20 -0
- sqlspec/data_dictionary/dialects/postgres/sql/types.sql +21 -0
- sqlspec/data_dictionary/dialects/postgres/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/postgres/sql/views.sql +17 -0
- sqlspec/data_dictionary/dialects/snowflake/__init__.py +31 -0
- sqlspec/data_dictionary/dialects/snowflake/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/snowflake/config.py +321 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/columns.sql +48 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/constraints.sql +64 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/ddl.sql +3 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/foreign_keys.sql +7 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/objects.sql +34 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/routines.sql +79 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/schemas.sql +19 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/sequences.sql +45 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/tables.sql +29 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/snowflake/sql/views.sql +37 -0
- sqlspec/data_dictionary/dialects/spanner/__init__.py +5 -0
- sqlspec/data_dictionary/dialects/spanner/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/spanner/config.py +67 -0
- sqlspec/data_dictionary/dialects/spanner/sql/columns.sql +58 -0
- sqlspec/data_dictionary/dialects/spanner/sql/foreign_keys.sql +91 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/change_streams.sql +28 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/columns.sql +46 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/constraints.sql +63 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/database.sql +9 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/foreign_keys.sql +91 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/indexes.sql +66 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/objects.sql +10 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/placements.sql +8 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/privileges.sql +13 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/property_graphs.sql +11 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/routines.sql +33 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/sequences.sql +14 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/system.sql +28 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/tables.sql +30 -0
- sqlspec/data_dictionary/dialects/spanner/sql/googlesql/views.sql +13 -0
- sqlspec/data_dictionary/dialects/spanner/sql/indexes.sql +30 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/change_streams.sql +28 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/columns.sql +34 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/constraints.sql +63 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/foreign_keys.sql +91 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/indexes.sql +52 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/objects.sql +10 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/sequences.sql +14 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/system.sql +28 -0
- sqlspec/data_dictionary/dialects/spanner/sql/postgresql/tables.sql +22 -0
- sqlspec/data_dictionary/dialects/spanner/sql/tables.sql +9 -0
- sqlspec/data_dictionary/dialects/spanner/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/__init__.py +31 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/config.py +191 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/columns.sql +97 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/constraints.sql +43 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/foreign_keys.sql +45 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/indexes.sql +39 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/objects.sql +18 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/schemas.sql +15 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/tables.sql +86 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlanywhere/sql/views.sql +15 -0
- sqlspec/data_dictionary/dialects/sqlite/__init__.py +19 -0
- sqlspec/data_dictionary/dialects/sqlite/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/sqlite/config.py +412 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/columns.sql +59 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/compile_options.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/constraints.sql +67 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/ddl.sql +37 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/foreign_keys.sql +45 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/functions.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/indexes.sql +76 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/modules.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/objects.sql +13 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/schemas.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/system.sql +7 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/tables.sql +64 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/triggers.sql +10 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/sqlite/sql/views.sql +10 -0
- sqlspec/data_dictionary/dialects/sybase/__init__.py +31 -0
- sqlspec/data_dictionary/dialects/sybase/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/sybase/config.py +187 -0
- sqlspec/data_dictionary/dialects/sybase/sql/columns.sql +137 -0
- sqlspec/data_dictionary/dialects/sybase/sql/constraints.sql +85 -0
- sqlspec/data_dictionary/dialects/sybase/sql/foreign_keys.sql +81 -0
- sqlspec/data_dictionary/dialects/sybase/sql/indexes.sql +51 -0
- sqlspec/data_dictionary/dialects/sybase/sql/objects.sql +21 -0
- sqlspec/data_dictionary/dialects/sybase/sql/schemas.sql +16 -0
- sqlspec/data_dictionary/dialects/sybase/sql/tables.sql +46 -0
- sqlspec/data_dictionary/dialects/sybase/sql/version.sql +3 -0
- sqlspec/data_dictionary/dialects/sybase/sql/views.sql +19 -0
- sqlspec/data_dictionary/dialects/teradata/__init__.py +33 -0
- sqlspec/data_dictionary/dialects/teradata/config.cp315-win_amd64.pyd +0 -0
- sqlspec/data_dictionary/dialects/teradata/config.py +228 -0
- sqlspec/data_dictionary/dialects/teradata/sql/columns.sql +22 -0
- sqlspec/data_dictionary/dialects/teradata/sql/constraints.sql +19 -0
- sqlspec/data_dictionary/dialects/teradata/sql/foreign_keys.sql +9 -0
- sqlspec/data_dictionary/dialects/teradata/sql/indexes.sql +9 -0
- sqlspec/data_dictionary/dialects/teradata/sql/objects.sql +4 -0
- sqlspec/data_dictionary/dialects/teradata/sql/schemas.sql +9 -0
- sqlspec/data_dictionary/dialects/teradata/sql/tables.sql +22 -0
- sqlspec/data_dictionary/dialects/teradata/sql/version.sql +4 -0
- sqlspec/data_dictionary/dialects/teradata/sql/views.sql +4 -0
- sqlspec/dialects/__init__.py +44 -0
- sqlspec/dialects/db2/__init__.py +111 -0
- sqlspec/dialects/db2/_generators.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/db2/_generators.py +376 -0
- sqlspec/dialects/db2/_parsers.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/db2/_parsers.py +657 -0
- sqlspec/dialects/db2/_transforms.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/db2/_transforms.py +175 -0
- sqlspec/dialects/postgres/__init__.py +10 -0
- sqlspec/dialects/postgres/_generators.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/postgres/_generators.py +81 -0
- sqlspec/dialects/postgres/_operators.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/postgres/_operators.py +95 -0
- sqlspec/dialects/postgres/_paradedb.py +44 -0
- sqlspec/dialects/postgres/_pg_textsearch.py +38 -0
- sqlspec/dialects/postgres/_pgvector.py +36 -0
- sqlspec/dialects/spanner/__init__.py +69 -0
- sqlspec/dialects/spanner/_expressions.py +247 -0
- sqlspec/dialects/spanner/_generators.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/spanner/_generators.py +961 -0
- sqlspec/dialects/spanner/_parsers.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/spanner/_parsers.py +871 -0
- sqlspec/dialects/spanner/_spangres.py +43 -0
- sqlspec/dialects/spanner/_spanner.py +57 -0
- sqlspec/dialects/sybase/__init__.py +115 -0
- sqlspec/dialects/sybase/_generators.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/sybase/_generators.py +379 -0
- sqlspec/dialects/sybase/_parsers.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/sybase/_parsers.py +225 -0
- sqlspec/dialects/sybase/_transforms.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/sybase/_transforms.py +68 -0
- sqlspec/dialects/teradata/__init__.py +42 -0
- sqlspec/dialects/teradata/_generators.cp315-win_amd64.pyd +0 -0
- sqlspec/dialects/teradata/_generators.py +203 -0
- sqlspec/driver/__init__.py +151 -0
- sqlspec/driver/_async.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_async.py +3244 -0
- sqlspec/driver/_common.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_common.py +4765 -0
- sqlspec/driver/_deferred.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_deferred.py +232 -0
- sqlspec/driver/_exception_handler.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_exception_handler.py +224 -0
- sqlspec/driver/_routing.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_routing.py +2891 -0
- sqlspec/driver/_session.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_session.py +180 -0
- sqlspec/driver/_sql_helpers.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_sql_helpers.py +140 -0
- sqlspec/driver/_stream.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_stream.py +392 -0
- sqlspec/driver/_sync.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_sync.py +3107 -0
- sqlspec/driver/_tenant.cp315-win_amd64.pyd +0 -0
- sqlspec/driver/_tenant.py +326 -0
- sqlspec/exceptions.cp315-win_amd64.pyd +0 -0
- sqlspec/exceptions.py +568 -0
- sqlspec/extensions/__init__.py +3 -0
- sqlspec/extensions/adk/__init__.py +136 -0
- sqlspec/extensions/adk/_config_utils.py +306 -0
- sqlspec/extensions/adk/_table_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/adk/_table_utils.py +126 -0
- sqlspec/extensions/adk/_types.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/adk/_types.py +59 -0
- sqlspec/extensions/adk/artifact/__init__.py +18 -0
- sqlspec/extensions/adk/artifact/_types.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/adk/artifact/_types.py +41 -0
- sqlspec/extensions/adk/artifact/service.py +657 -0
- sqlspec/extensions/adk/artifact/store.py +444 -0
- sqlspec/extensions/adk/converters.py +230 -0
- sqlspec/extensions/adk/maintenance.py +262 -0
- sqlspec/extensions/adk/memory/__init__.py +36 -0
- sqlspec/extensions/adk/memory/_types.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/adk/memory/_types.py +48 -0
- sqlspec/extensions/adk/memory/converters.py +271 -0
- sqlspec/extensions/adk/memory/service.py +474 -0
- sqlspec/extensions/adk/memory/store.py +586 -0
- sqlspec/extensions/adk/migrations/0001_create_adk_tables.py +181 -0
- sqlspec/extensions/adk/migrations/0002_create_mssql_python_memory.py +51 -0
- sqlspec/extensions/adk/migrations/__init__.py +3 -0
- sqlspec/extensions/adk/migrations/reconcile.py +282 -0
- sqlspec/extensions/adk/service.py +463 -0
- sqlspec/extensions/adk/store.py +1239 -0
- sqlspec/extensions/events/__init__.py +132 -0
- sqlspec/extensions/events/_buffer.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_buffer.py +38 -0
- sqlspec/extensions/events/_channel.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_channel.py +1144 -0
- sqlspec/extensions/events/_hints.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_hints.py +38 -0
- sqlspec/extensions/events/_models.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_models.py +22 -0
- sqlspec/extensions/events/_names.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_names.py +27 -0
- sqlspec/extensions/events/_payload.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_payload.py +139 -0
- sqlspec/extensions/events/_protocols.py +154 -0
- sqlspec/extensions/events/_queue.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/_queue.py +1147 -0
- sqlspec/extensions/events/_store.py +318 -0
- sqlspec/extensions/events/migrations/0001_create_event_queue.py +106 -0
- sqlspec/extensions/events/migrations/__init__.py +3 -0
- sqlspec/extensions/events/migrations/reconcile.py +145 -0
- sqlspec/extensions/events/primitives.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/events/primitives.py +91 -0
- sqlspec/extensions/fastapi/__init__.py +26 -0
- sqlspec/extensions/fastapi/extension.py +360 -0
- sqlspec/extensions/fastapi/providers.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/fastapi/providers.py +1091 -0
- sqlspec/extensions/flask/__init__.py +20 -0
- sqlspec/extensions/flask/_portal.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/flask/_portal.py +212 -0
- sqlspec/extensions/flask/_state.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/flask/_state.py +135 -0
- sqlspec/extensions/flask/_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/flask/_utils.py +171 -0
- sqlspec/extensions/flask/extension.py +1234 -0
- sqlspec/extensions/litestar/__init__.py +52 -0
- sqlspec/extensions/litestar/_tenant_resolution.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/litestar/_tenant_resolution.py +314 -0
- sqlspec/extensions/litestar/_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/litestar/_utils.py +222 -0
- sqlspec/extensions/litestar/channels.py +249 -0
- sqlspec/extensions/litestar/cli.py +100 -0
- sqlspec/extensions/litestar/config.py +5 -0
- sqlspec/extensions/litestar/handlers.py +796 -0
- sqlspec/extensions/litestar/migrations/0001_create_session_table.py +138 -0
- sqlspec/extensions/litestar/migrations/__init__.py +3 -0
- sqlspec/extensions/litestar/migrations/reconcile.py +132 -0
- sqlspec/extensions/litestar/plugin.py +2172 -0
- sqlspec/extensions/litestar/providers.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/litestar/providers.py +1239 -0
- sqlspec/extensions/litestar/store.py +613 -0
- sqlspec/extensions/litestar/tenant.py +292 -0
- sqlspec/extensions/otel/__init__.py +58 -0
- sqlspec/extensions/prometheus/__init__.py +5 -0
- sqlspec/extensions/prometheus/_observer.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/prometheus/_observer.py +152 -0
- sqlspec/extensions/sanic/__init__.py +28 -0
- sqlspec/extensions/sanic/_state.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/sanic/_state.py +102 -0
- sqlspec/extensions/sanic/_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/sanic/_utils.py +234 -0
- sqlspec/extensions/sanic/extension.py +1467 -0
- sqlspec/extensions/starlette/__init__.py +31 -0
- sqlspec/extensions/starlette/_state.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/starlette/_state.py +103 -0
- sqlspec/extensions/starlette/_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/extensions/starlette/_utils.py +267 -0
- sqlspec/extensions/starlette/extension.py +691 -0
- sqlspec/extensions/starlette/middleware.py +845 -0
- sqlspec/loader.cp315-win_amd64.pyd +0 -0
- sqlspec/loader.py +1733 -0
- sqlspec/migrations/__init__.py +104 -0
- sqlspec/migrations/_backup.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/_backup.py +36 -0
- sqlspec/migrations/base.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/base.py +1688 -0
- sqlspec/migrations/commands.py +4002 -0
- sqlspec/migrations/context.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/context.py +158 -0
- sqlspec/migrations/fix.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/fix.py +193 -0
- sqlspec/migrations/loaders.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/loaders.py +835 -0
- sqlspec/migrations/runner.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/runner.py +2144 -0
- sqlspec/migrations/schema.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/schema.py +1266 -0
- sqlspec/migrations/squash.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/squash.py +2452 -0
- sqlspec/migrations/templates.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/templates.py +310 -0
- sqlspec/migrations/tracker.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/tracker.py +1529 -0
- sqlspec/migrations/utils.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/utils.py +534 -0
- sqlspec/migrations/validation.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/validation.py +355 -0
- sqlspec/migrations/version.cp315-win_amd64.pyd +0 -0
- sqlspec/migrations/version.py +455 -0
- sqlspec/observability/__init__.py +122 -0
- sqlspec/observability/_common.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_common.py +307 -0
- sqlspec/observability/_config.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_config.py +362 -0
- sqlspec/observability/_diagnostics.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_diagnostics.py +75 -0
- sqlspec/observability/_dispatcher.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_dispatcher.py +320 -0
- sqlspec/observability/_formatters/__init__.py +13 -0
- sqlspec/observability/_formatters/_aws.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_formatters/_aws.py +90 -0
- sqlspec/observability/_formatters/_azure.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_formatters/_azure.py +84 -0
- sqlspec/observability/_formatters/_base.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_formatters/_base.py +40 -0
- sqlspec/observability/_formatters/_gcp.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_formatters/_gcp.py +119 -0
- sqlspec/observability/_formatting.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_formatting.py +76 -0
- sqlspec/observability/_observer.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_observer.py +445 -0
- sqlspec/observability/_runtime.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_runtime.py +740 -0
- sqlspec/observability/_sampling.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_sampling.py +169 -0
- sqlspec/observability/_spans.cp315-win_amd64.pyd +0 -0
- sqlspec/observability/_spans.py +212 -0
- sqlspec/protocols.py +1235 -0
- sqlspec/py.typed +0 -0
- sqlspec/service/__init__.py +5 -0
- sqlspec/service/_base.py +1051 -0
- sqlspec/service/_core.cp315-win_amd64.pyd +0 -0
- sqlspec/service/_core.py +625 -0
- sqlspec/storage/__init__.py +111 -0
- sqlspec/storage/_arrow_payload.py +802 -0
- sqlspec/storage/_arrow_stream.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/_arrow_stream.py +134 -0
- sqlspec/storage/_ingest.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/_ingest.py +810 -0
- sqlspec/storage/_paths.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/_paths.py +313 -0
- sqlspec/storage/_staging_sql.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/_staging_sql.py +695 -0
- sqlspec/storage/_utils.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/_utils.py +174 -0
- sqlspec/storage/backends/__init__.py +43 -0
- sqlspec/storage/backends/base.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/backends/base.py +399 -0
- sqlspec/storage/backends/fsspec.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/backends/fsspec.py +1390 -0
- sqlspec/storage/backends/local.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/backends/local.py +968 -0
- sqlspec/storage/backends/obstore.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/backends/obstore.py +1974 -0
- sqlspec/storage/errors.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/errors.py +127 -0
- sqlspec/storage/pipeline.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/pipeline.py +1270 -0
- sqlspec/storage/registry.cp315-win_amd64.pyd +0 -0
- sqlspec/storage/registry.py +318 -0
- sqlspec/typing.py +261 -0
- sqlspec/utils/__init__.py +9 -0
- sqlspec/utils/adapters.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/adapters.py +186 -0
- sqlspec/utils/arrow_helpers.py +2157 -0
- sqlspec/utils/config_tools.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/config_tools.py +712 -0
- sqlspec/utils/correlation.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/correlation.py +137 -0
- sqlspec/utils/deprecation.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/deprecation.py +222 -0
- sqlspec/utils/dispatch.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/dispatch.py +104 -0
- sqlspec/utils/env.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/env.py +437 -0
- sqlspec/utils/fixtures.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/fixtures.py +2470 -0
- sqlspec/utils/logging.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/logging.py +267 -0
- sqlspec/utils/module_loader.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/module_loader.py +399 -0
- sqlspec/utils/portal.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/portal.py +411 -0
- sqlspec/utils/routing.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/routing.py +645 -0
- sqlspec/utils/schema.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/schema.py +1050 -0
- sqlspec/utils/serializers/__init__.py +69 -0
- sqlspec/utils/serializers/_json.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/serializers/_json.py +525 -0
- sqlspec/utils/serializers/_numpy.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/serializers/_numpy.py +65 -0
- sqlspec/utils/serializers/_schema.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/serializers/_schema.py +340 -0
- sqlspec/utils/sync_tools.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/sync_tools.py +502 -0
- sqlspec/utils/tenant/__init__.py +71 -0
- sqlspec/utils/tenant/_context.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/tenant/_context.py +550 -0
- sqlspec/utils/tenant/_cursor.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/tenant/_cursor.py +43 -0
- sqlspec/utils/tenant/_mssql.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/tenant/_mssql.py +189 -0
- sqlspec/utils/tenant/_postgres.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/tenant/_postgres.py +142 -0
- sqlspec/utils/text.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/text.py +317 -0
- sqlspec/utils/type_converters.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/type_converters.py +220 -0
- sqlspec/utils/type_guards.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/type_guards.py +1505 -0
- sqlspec/utils/uuids.cp315-win_amd64.pyd +0 -0
- sqlspec/utils/uuids.py +263 -0
- sqlspec-0.72.0.dist-info/METADATA +228 -0
- sqlspec-0.72.0.dist-info/RECORD +1144 -0
- sqlspec-0.72.0.dist-info/WHEEL +4 -0
- sqlspec-0.72.0.dist-info/entry_points.txt +13 -0
- sqlspec-0.72.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,4031 @@
|
|
|
1
|
+
"""Oracle ADK store for Google Agent Development Kit session/event storage."""
|
|
2
|
+
|
|
3
|
+
import array
|
|
4
|
+
import contextlib
|
|
5
|
+
from typing import TYPE_CHECKING, Any, Final, Literal, NoReturn, NotRequired, cast
|
|
6
|
+
|
|
7
|
+
from typing_extensions import TypedDict
|
|
8
|
+
|
|
9
|
+
from sqlspec import SQL
|
|
10
|
+
from sqlspec.adapters.oracledb._storage import oracle_table_feature_report, validate_oracle_identifier
|
|
11
|
+
from sqlspec.adapters.oracledb._typing import DatabaseError as OracleDatabaseError
|
|
12
|
+
from sqlspec.adapters.oracledb.core import (
|
|
13
|
+
coerce_large_parameters_async,
|
|
14
|
+
coerce_large_parameters_sync,
|
|
15
|
+
read_oracle_lob_async,
|
|
16
|
+
read_oracle_lob_sync,
|
|
17
|
+
sanitize_oracle_text_query,
|
|
18
|
+
)
|
|
19
|
+
from sqlspec.adapters.oracledb.data_dictionary import JSONStorageType, OracleVersionInfo, storage_type_from_version
|
|
20
|
+
from sqlspec.builder._vector_distance import build_oracle_hnsw_subquery, build_oracle_hybrid_rrf_sql
|
|
21
|
+
from sqlspec.config import ADKConfig
|
|
22
|
+
from sqlspec.data_dictionary.dialects.oracle import parse_oracle_version_components
|
|
23
|
+
from sqlspec.exceptions import ImproperConfigurationError, SerializationConflictError
|
|
24
|
+
from sqlspec.extensions.adk import (
|
|
25
|
+
BaseAsyncADKStore,
|
|
26
|
+
BaseSyncADKStore,
|
|
27
|
+
StoredEvent,
|
|
28
|
+
StoredSession,
|
|
29
|
+
normalize_session_list_options,
|
|
30
|
+
)
|
|
31
|
+
from sqlspec.extensions.adk.converters import merge_scoped_state
|
|
32
|
+
from sqlspec.extensions.adk.memory.store import BaseAsyncADKMemoryStore, BaseSyncADKMemoryStore
|
|
33
|
+
from sqlspec.extensions.adk.store import adk_primary_read_scope
|
|
34
|
+
from sqlspec.migrations.schema import metadata_has_index
|
|
35
|
+
from sqlspec.utils.logging import get_logger
|
|
36
|
+
from sqlspec.utils.serializers import from_json, to_json
|
|
37
|
+
from sqlspec.utils.type_converters import build_nested_decimal_normalizer
|
|
38
|
+
from sqlspec.utils.type_guards import is_async_readable, is_readable
|
|
39
|
+
|
|
40
|
+
if TYPE_CHECKING:
|
|
41
|
+
from collections.abc import Sequence
|
|
42
|
+
from datetime import datetime, timedelta
|
|
43
|
+
|
|
44
|
+
from sqlspec.adapters.oracledb.config import OracleAsyncConfig, OracleSyncConfig
|
|
45
|
+
from sqlspec.extensions.adk import SessionOrderBy, StoredMemory
|
|
46
|
+
|
|
47
|
+
__all__ = (
|
|
48
|
+
"JSONStorageType",
|
|
49
|
+
"OracleADKCompressionConfig",
|
|
50
|
+
"OracleADKConfig",
|
|
51
|
+
"OracleADKPartitionConfig",
|
|
52
|
+
"OracleAsyncADKMemoryStore",
|
|
53
|
+
"OracleAsyncADKStore",
|
|
54
|
+
"OracleSyncADKMemoryStore",
|
|
55
|
+
"OracleSyncADKStore",
|
|
56
|
+
"coerce_decimal_values",
|
|
57
|
+
"storage_type_from_version",
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
logger = get_logger("sqlspec.adapters.oracledb.adk.store")
|
|
61
|
+
|
|
62
|
+
ORACLE_TABLE_NOT_FOUND_ERROR: Final = 942
|
|
63
|
+
ORACLE_DUPLICATE_KEY_ERROR: Final = 1
|
|
64
|
+
_MAX_STATE_MERGE_ATTEMPTS: Final = 8
|
|
65
|
+
ORACLE_DEFAULT_SESSION_TABLE: Final = "adk_session"
|
|
66
|
+
ORACLE_DEFAULT_EVENTS_TABLE: Final = "adk_event"
|
|
67
|
+
ORACLE_DEFAULT_APP_STATE_TABLE: Final = "adk_app_state"
|
|
68
|
+
ORACLE_DEFAULT_USER_STATE_TABLE: Final = "adk_user_state"
|
|
69
|
+
ORACLE_DEFAULT_METADATA_TABLE: Final = "adk_internal_metadata"
|
|
70
|
+
_EMBEDDING_COLUMN_INDEX: Final = 12
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
_ADK_METADATA_TABLE_DDL_TEMPLATE = (
|
|
74
|
+
"\n"
|
|
75
|
+
" BEGIN\n"
|
|
76
|
+
" EXECUTE IMMEDIATE 'CREATE TABLE {0} (\n"
|
|
77
|
+
" key VARCHAR2(128) PRIMARY KEY,\n"
|
|
78
|
+
" value VARCHAR2(512) NOT NULL\n"
|
|
79
|
+
" )';\n"
|
|
80
|
+
" EXCEPTION\n"
|
|
81
|
+
" WHEN OTHERS THEN\n"
|
|
82
|
+
" IF SQLCODE != -955 THEN\n"
|
|
83
|
+
" RAISE;\n"
|
|
84
|
+
" END IF;\n"
|
|
85
|
+
" END;\n"
|
|
86
|
+
" "
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
_ADK_SESSIONS_TABLE_DDL_FOR_TYPE_TEMPLATE = ", {0}"
|
|
91
|
+
|
|
92
|
+
_ADK_SESSIONS_TABLE_DDL_FOR_TYPE_TEMPLATE_2 = (
|
|
93
|
+
"\n"
|
|
94
|
+
" BEGIN\n"
|
|
95
|
+
" EXECUTE IMMEDIATE 'CREATE TABLE {0} (\n"
|
|
96
|
+
" id VARCHAR2(128) PRIMARY KEY,\n"
|
|
97
|
+
" app_name VARCHAR2(128) NOT NULL,\n"
|
|
98
|
+
" user_id VARCHAR2(128) NOT NULL,\n"
|
|
99
|
+
" {1},\n"
|
|
100
|
+
" create_time TIMESTAMP WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,\n"
|
|
101
|
+
" update_time TIMESTAMP WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL{2}\n"
|
|
102
|
+
" ){3}';\n"
|
|
103
|
+
" EXCEPTION\n"
|
|
104
|
+
" WHEN OTHERS THEN\n"
|
|
105
|
+
" IF SQLCODE != -955 THEN\n"
|
|
106
|
+
" RAISE;\n"
|
|
107
|
+
" END IF;\n"
|
|
108
|
+
" END;\n"
|
|
109
|
+
"\n"
|
|
110
|
+
" BEGIN\n"
|
|
111
|
+
" EXECUTE IMMEDIATE 'CREATE INDEX idx_{4}_app_user\n"
|
|
112
|
+
" ON {5}(app_name, user_id)';\n"
|
|
113
|
+
" EXCEPTION\n"
|
|
114
|
+
" WHEN OTHERS THEN\n"
|
|
115
|
+
" IF SQLCODE != -955 THEN\n"
|
|
116
|
+
" RAISE;\n"
|
|
117
|
+
" END IF;\n"
|
|
118
|
+
" END;\n"
|
|
119
|
+
"\n"
|
|
120
|
+
" BEGIN\n"
|
|
121
|
+
" EXECUTE IMMEDIATE 'CREATE INDEX idx_{6}_update_time\n"
|
|
122
|
+
" ON {7}(update_time DESC)';\n"
|
|
123
|
+
" EXCEPTION\n"
|
|
124
|
+
" WHEN OTHERS THEN\n"
|
|
125
|
+
" IF SQLCODE != -955 THEN\n"
|
|
126
|
+
" RAISE;\n"
|
|
127
|
+
" END IF;\n"
|
|
128
|
+
" END;\n"
|
|
129
|
+
" "
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
_ADK_EVENTS_TABLE_DDL_FOR_TYPE_TEMPLATE = (
|
|
133
|
+
"\n"
|
|
134
|
+
" BEGIN\n"
|
|
135
|
+
" EXECUTE IMMEDIATE 'CREATE TABLE {0} (\n"
|
|
136
|
+
" id VARCHAR2(128) PRIMARY KEY,\n"
|
|
137
|
+
" app_name VARCHAR2(128) NOT NULL,\n"
|
|
138
|
+
" user_id VARCHAR2(128) NOT NULL,\n"
|
|
139
|
+
" session_id VARCHAR2(128) NOT NULL,\n"
|
|
140
|
+
" invocation_id VARCHAR2(256),\n"
|
|
141
|
+
" timestamp TIMESTAMP WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,\n"
|
|
142
|
+
" {1},\n"
|
|
143
|
+
" CONSTRAINT fk_{2}_session FOREIGN KEY (session_id)\n"
|
|
144
|
+
" REFERENCES {3}(id) ON DELETE CASCADE\n"
|
|
145
|
+
" ){4}';\n"
|
|
146
|
+
" EXCEPTION\n"
|
|
147
|
+
" WHEN OTHERS THEN\n"
|
|
148
|
+
" IF SQLCODE != -955 THEN\n"
|
|
149
|
+
" RAISE;\n"
|
|
150
|
+
" END IF;\n"
|
|
151
|
+
" END;\n"
|
|
152
|
+
"\n"
|
|
153
|
+
" BEGIN\n"
|
|
154
|
+
" EXECUTE IMMEDIATE 'CREATE INDEX idx_{5}_session\n"
|
|
155
|
+
" ON {6}(session_id, timestamp ASC)';\n"
|
|
156
|
+
" EXCEPTION\n"
|
|
157
|
+
" WHEN OTHERS THEN\n"
|
|
158
|
+
" IF SQLCODE != -955 THEN\n"
|
|
159
|
+
" RAISE;\n"
|
|
160
|
+
" END IF;\n"
|
|
161
|
+
" END;\n"
|
|
162
|
+
"\n"
|
|
163
|
+
" BEGIN\n"
|
|
164
|
+
" EXECUTE IMMEDIATE 'CREATE INDEX idx_{7}_invocation\n"
|
|
165
|
+
" ON {8}(invocation_id)';\n"
|
|
166
|
+
" EXCEPTION\n"
|
|
167
|
+
" WHEN OTHERS THEN\n"
|
|
168
|
+
" IF SQLCODE != -955 THEN\n"
|
|
169
|
+
" RAISE;\n"
|
|
170
|
+
" END IF;\n"
|
|
171
|
+
" END;\n"
|
|
172
|
+
"\n"
|
|
173
|
+
" BEGIN\n"
|
|
174
|
+
" EXECUTE IMMEDIATE 'CREATE INDEX idx_{9}_timestamp\n"
|
|
175
|
+
" ON {10}(timestamp ASC)';\n"
|
|
176
|
+
" EXCEPTION\n"
|
|
177
|
+
" WHEN OTHERS THEN\n"
|
|
178
|
+
" IF SQLCODE != -955 THEN\n"
|
|
179
|
+
" RAISE;\n"
|
|
180
|
+
" END IF;\n"
|
|
181
|
+
" END;\n"
|
|
182
|
+
"\n"
|
|
183
|
+
" BEGIN\n"
|
|
184
|
+
" EXECUTE IMMEDIATE 'CREATE INDEX idx_{11}_app_timestamp\n"
|
|
185
|
+
" ON {12}(app_name, timestamp ASC)';\n"
|
|
186
|
+
" EXCEPTION\n"
|
|
187
|
+
" WHEN OTHERS THEN\n"
|
|
188
|
+
" IF SQLCODE != -955 THEN\n"
|
|
189
|
+
" RAISE;\n"
|
|
190
|
+
" END IF;\n"
|
|
191
|
+
" END;\n"
|
|
192
|
+
" "
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
_ADK_APP_STATES_TABLE_DDL_FOR_TYPE_TEMPLATE = (
|
|
196
|
+
"\n"
|
|
197
|
+
" BEGIN\n"
|
|
198
|
+
" EXECUTE IMMEDIATE 'CREATE TABLE {0} (\n"
|
|
199
|
+
" app_name VARCHAR2(128) PRIMARY KEY,\n"
|
|
200
|
+
" {1},\n"
|
|
201
|
+
" update_time TIMESTAMP WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL\n"
|
|
202
|
+
" ){2}';\n"
|
|
203
|
+
" EXCEPTION\n"
|
|
204
|
+
" WHEN OTHERS THEN\n"
|
|
205
|
+
" IF SQLCODE != -955 THEN\n"
|
|
206
|
+
" RAISE;\n"
|
|
207
|
+
" END IF;\n"
|
|
208
|
+
" END;\n"
|
|
209
|
+
" "
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
_ADK_USER_STATES_TABLE_DDL_FOR_TYPE_TEMPLATE = (
|
|
213
|
+
"\n"
|
|
214
|
+
" BEGIN\n"
|
|
215
|
+
" EXECUTE IMMEDIATE 'CREATE TABLE {0} (\n"
|
|
216
|
+
" app_name VARCHAR2(128) NOT NULL,\n"
|
|
217
|
+
" user_id VARCHAR2(128) NOT NULL,\n"
|
|
218
|
+
" {1},\n"
|
|
219
|
+
" update_time TIMESTAMP WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,\n"
|
|
220
|
+
" PRIMARY KEY (app_name, user_id)\n"
|
|
221
|
+
" ){2}';\n"
|
|
222
|
+
" EXCEPTION\n"
|
|
223
|
+
" WHEN OTHERS THEN\n"
|
|
224
|
+
" IF SQLCODE != -955 THEN\n"
|
|
225
|
+
" RAISE;\n"
|
|
226
|
+
" END IF;\n"
|
|
227
|
+
" END;\n"
|
|
228
|
+
" "
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
_ADK_MEMORY_TABLE_DDL_FOR_TYPE_TEMPLATE = ",\n {0}"
|
|
232
|
+
|
|
233
|
+
_ADK_MEMORY_TABLE_DDL_FOR_TYPE_TEMPLATE_2 = (
|
|
234
|
+
"\n"
|
|
235
|
+
" BEGIN\n"
|
|
236
|
+
" EXECUTE IMMEDIATE 'CREATE INDEX idx_{0}_fts\n"
|
|
237
|
+
" ON {1}(content_text) INDEXTYPE IS CTXSYS.CONTEXT';\n"
|
|
238
|
+
" EXCEPTION\n"
|
|
239
|
+
" WHEN OTHERS THEN\n"
|
|
240
|
+
" IF SQLCODE != -955 THEN\n"
|
|
241
|
+
" RAISE;\n"
|
|
242
|
+
" END IF;\n"
|
|
243
|
+
" END;\n"
|
|
244
|
+
" "
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
_ADK_MEMORY_TABLE_DDL_FOR_TYPE_TEMPLATE_3 = (
|
|
248
|
+
"\n"
|
|
249
|
+
" BEGIN\n"
|
|
250
|
+
" EXECUTE IMMEDIATE 'CREATE TABLE {0} (\n"
|
|
251
|
+
" id VARCHAR2(128) PRIMARY KEY,\n"
|
|
252
|
+
" session_id VARCHAR2(128) NOT NULL,\n"
|
|
253
|
+
" app_name VARCHAR2(128) NOT NULL,\n"
|
|
254
|
+
" user_id VARCHAR2(128) NOT NULL,\n"
|
|
255
|
+
" scope VARCHAR2(16) DEFAULT ''user'' NOT NULL,\n"
|
|
256
|
+
" event_id VARCHAR2(128) NOT NULL,\n"
|
|
257
|
+
" author VARCHAR2(256){1},\n"
|
|
258
|
+
" timestamp TIMESTAMP WITH TIME ZONE NOT NULL,\n"
|
|
259
|
+
" {11}"
|
|
260
|
+
" {2},\n"
|
|
261
|
+
" content_text CLOB NOT NULL,\n"
|
|
262
|
+
" inserted_at TIMESTAMP WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,\n"
|
|
263
|
+
" UNIQUE (app_name, user_id, event_id)\n"
|
|
264
|
+
" ){3}';\n"
|
|
265
|
+
" EXCEPTION\n"
|
|
266
|
+
" WHEN OTHERS THEN\n"
|
|
267
|
+
" IF SQLCODE != -955 THEN\n"
|
|
268
|
+
" RAISE;\n"
|
|
269
|
+
" END IF;\n"
|
|
270
|
+
" END;\n"
|
|
271
|
+
"\n"
|
|
272
|
+
" BEGIN\n"
|
|
273
|
+
" EXECUTE IMMEDIATE 'CREATE INDEX idx_{4}_app_scope_user_time\n"
|
|
274
|
+
" ON {5}(app_name, scope, user_id, timestamp DESC)';\n"
|
|
275
|
+
" EXCEPTION\n"
|
|
276
|
+
" WHEN OTHERS THEN\n"
|
|
277
|
+
" IF SQLCODE != -955 THEN\n"
|
|
278
|
+
" RAISE;\n"
|
|
279
|
+
" END IF;\n"
|
|
280
|
+
" END;\n"
|
|
281
|
+
"\n"
|
|
282
|
+
" BEGIN\n"
|
|
283
|
+
" EXECUTE IMMEDIATE 'CREATE INDEX idx_{6}_scope\n"
|
|
284
|
+
" ON {7}(app_name, scope)';\n"
|
|
285
|
+
" EXCEPTION\n"
|
|
286
|
+
" WHEN OTHERS THEN\n"
|
|
287
|
+
" IF SQLCODE != -955 THEN\n"
|
|
288
|
+
" RAISE;\n"
|
|
289
|
+
" END IF;\n"
|
|
290
|
+
" END;\n"
|
|
291
|
+
"\n"
|
|
292
|
+
" BEGIN\n"
|
|
293
|
+
" EXECUTE IMMEDIATE 'CREATE INDEX idx_{8}_session\n"
|
|
294
|
+
" ON {9}(session_id)';\n"
|
|
295
|
+
" EXCEPTION\n"
|
|
296
|
+
" WHEN OTHERS THEN\n"
|
|
297
|
+
" IF SQLCODE != -955 THEN\n"
|
|
298
|
+
" RAISE;\n"
|
|
299
|
+
" END IF;\n"
|
|
300
|
+
" END;\n"
|
|
301
|
+
" {10}\n"
|
|
302
|
+
" "
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
_ADK_JSON_COLUMN_DDL_TEMPLATE = "{0} JSON NOT NULL"
|
|
306
|
+
|
|
307
|
+
_ADK_JSON_COLUMN_DDL_TEMPLATE_2 = "{0} BLOB CHECK ({1} IS JSON) NOT NULL"
|
|
308
|
+
|
|
309
|
+
_ADK_JSON_COLUMN_DDL_TEMPLATE_3 = "{0} BLOB NOT NULL"
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
class OracleADKCompressionConfig(TypedDict):
|
|
313
|
+
"""Oracle-specific ADK table compression settings."""
|
|
314
|
+
|
|
315
|
+
enabled: NotRequired[bool]
|
|
316
|
+
"""Enable Oracle table compression clauses."""
|
|
317
|
+
|
|
318
|
+
algorithm: NotRequired[str]
|
|
319
|
+
"""Oracle compression algorithm key."""
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
class OracleADKPartitionConfig(TypedDict):
|
|
323
|
+
"""Oracle-specific ADK table partitioning settings."""
|
|
324
|
+
|
|
325
|
+
strategy: NotRequired[str]
|
|
326
|
+
"""Partitioning strategy, such as ``hash`` or ``range``."""
|
|
327
|
+
|
|
328
|
+
partition_count: NotRequired[int]
|
|
329
|
+
"""Hash partition count."""
|
|
330
|
+
|
|
331
|
+
partitions: NotRequired[int]
|
|
332
|
+
"""Alias for ``partition_count``."""
|
|
333
|
+
|
|
334
|
+
interval: NotRequired[str]
|
|
335
|
+
"""Range partition interval key."""
|
|
336
|
+
|
|
337
|
+
initial_less_than: NotRequired[str]
|
|
338
|
+
"""Initial range partition upper bound expression."""
|
|
339
|
+
|
|
340
|
+
partition_key: NotRequired[str]
|
|
341
|
+
"""Default partition key for all ADK tables."""
|
|
342
|
+
|
|
343
|
+
session_partition_key: NotRequired[str]
|
|
344
|
+
"""Partition key override for the session table."""
|
|
345
|
+
|
|
346
|
+
events_partition_key: NotRequired[str]
|
|
347
|
+
"""Partition key override for the events table."""
|
|
348
|
+
|
|
349
|
+
app_state_partition_key: NotRequired[str]
|
|
350
|
+
"""Partition key override for the app state table."""
|
|
351
|
+
|
|
352
|
+
user_state_partition_key: NotRequired[str]
|
|
353
|
+
"""Partition key override for the user state table."""
|
|
354
|
+
|
|
355
|
+
memory_partition_key: NotRequired[str]
|
|
356
|
+
"""Partition key override for the memory table."""
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
class OracleADKConfig(ADKConfig):
|
|
360
|
+
"""Oracle ADK extension settings consumed by Oracle ADK stores."""
|
|
361
|
+
|
|
362
|
+
vector_dimensions: NotRequired[int]
|
|
363
|
+
"""Dimensionality of embedding vectors (e.g. 768 for gemini-embedding-001 with MRL). Default: 768."""
|
|
364
|
+
|
|
365
|
+
in_memory: NotRequired[bool]
|
|
366
|
+
"""Enable Oracle INMEMORY clauses on ADK tables."""
|
|
367
|
+
|
|
368
|
+
compression: NotRequired[OracleADKCompressionConfig]
|
|
369
|
+
"""Oracle ADK table compression settings."""
|
|
370
|
+
|
|
371
|
+
partitioning: NotRequired[OracleADKPartitionConfig]
|
|
372
|
+
"""Oracle ADK table partitioning settings."""
|
|
373
|
+
|
|
374
|
+
session_table_options: NotRequired[str]
|
|
375
|
+
"""Raw Oracle table options for the ADK session table."""
|
|
376
|
+
|
|
377
|
+
events_table_options: NotRequired[str]
|
|
378
|
+
"""Raw Oracle table options for the ADK events table."""
|
|
379
|
+
|
|
380
|
+
app_state_table_options: NotRequired[str]
|
|
381
|
+
"""Raw Oracle table options for the ADK app state table."""
|
|
382
|
+
|
|
383
|
+
user_state_table_options: NotRequired[str]
|
|
384
|
+
"""Raw Oracle table options for the ADK user state table."""
|
|
385
|
+
|
|
386
|
+
memory_table_options: NotRequired[str]
|
|
387
|
+
"""Raw Oracle table options for the ADK memory table."""
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
def coerce_decimal_values(value: Any) -> Any:
|
|
391
|
+
return _coerce_decimal_values(value)
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
class OracleAsyncADKStore(BaseAsyncADKStore["OracleAsyncConfig"]):
|
|
395
|
+
"""Oracle async ADK store using oracledb async driver.
|
|
396
|
+
|
|
397
|
+
Implements session and event storage for Google Agent Development Kit
|
|
398
|
+
using Oracle Database via the python-oracledb async driver. Provides:
|
|
399
|
+
- Session state management with version-specific JSON storage
|
|
400
|
+
- Full-fidelity event storage via ``event_data`` column
|
|
401
|
+
- Atomic ``append_event_and_update_state`` for durable session mutations
|
|
402
|
+
- TIMESTAMP WITH TIME ZONE for timezone-aware timestamps
|
|
403
|
+
- Foreign key constraints with cascade delete
|
|
404
|
+
- Efficient upserts using MERGE statement
|
|
405
|
+
|
|
406
|
+
Args:
|
|
407
|
+
config: OracleAsyncConfig with extension_config["adk"] settings.
|
|
408
|
+
|
|
409
|
+
Notes:
|
|
410
|
+
- JSON storage type detected by capability (native JSON, BLOB IS JSON, or plain LOB)
|
|
411
|
+
- event_data stored as JSON (21c+) or BLOB (older versions)
|
|
412
|
+
- TIMESTAMP WITH TIME ZONE for timezone-aware timestamps
|
|
413
|
+
- Named parameters using :param_name
|
|
414
|
+
- State merging handled at application level
|
|
415
|
+
- owner_id_column supports NUMBER, VARCHAR2, RAW for Oracle FK types
|
|
416
|
+
- Configuration is read from config.extension_config["adk"]
|
|
417
|
+
"""
|
|
418
|
+
|
|
419
|
+
__slots__ = ("_in_memory",)
|
|
420
|
+
|
|
421
|
+
def __init__(self, config: "OracleAsyncConfig", *, read_config: "OracleAsyncConfig | None" = None) -> None:
|
|
422
|
+
"""Initialize Oracle ADK store.
|
|
423
|
+
|
|
424
|
+
Args:
|
|
425
|
+
read_config: Optional compatible reader for pure reads.
|
|
426
|
+
config: OracleAsyncConfig instance.
|
|
427
|
+
|
|
428
|
+
Notes:
|
|
429
|
+
Configuration is read from config.extension_config["adk"]:
|
|
430
|
+
- session_table: Sessions table name (default: "adk_session")
|
|
431
|
+
- events_table: Events table name (default: "adk_event")
|
|
432
|
+
- owner_id_column: Optional owner FK column DDL (default: None)
|
|
433
|
+
- in_memory: Enable INMEMORY PRIORITY HIGH clause (default: False)
|
|
434
|
+
"""
|
|
435
|
+
super().__init__(config, read_config=read_config)
|
|
436
|
+
_configure_oracle_adk_session_tables(self, config)
|
|
437
|
+
|
|
438
|
+
adk_config = _adk_config(config)
|
|
439
|
+
self._in_memory: bool = bool(adk_config.get("in_memory", False))
|
|
440
|
+
|
|
441
|
+
async def create_tables(self) -> None:
|
|
442
|
+
"""Create the ADK tables that the data dictionary reports as missing.
|
|
443
|
+
|
|
444
|
+
Notes:
|
|
445
|
+
Detects Oracle version to determine optimal JSON storage type.
|
|
446
|
+
Consults ``data_dictionary.get_tables`` so existing tables are left
|
|
447
|
+
untouched instead of relying on an ORA-955 swallow.
|
|
448
|
+
"""
|
|
449
|
+
if not self.create_schema_enabled:
|
|
450
|
+
await self.reconcile_schema()
|
|
451
|
+
return
|
|
452
|
+
|
|
453
|
+
storage_type = await self._detect_json_storage_type()
|
|
454
|
+
logger.debug("Creating ADK tables with storage type: %s", storage_type)
|
|
455
|
+
|
|
456
|
+
async with self._config.provide_session() as driver:
|
|
457
|
+
existing = _existing_table_names(await driver.data_dictionary.get_tables(driver))
|
|
458
|
+
if _bare_table_name(self._session_table) not in existing:
|
|
459
|
+
await driver.execute_script(self._sessions_table_ddl_for_type(storage_type))
|
|
460
|
+
if _bare_table_name(self._events_table) not in existing:
|
|
461
|
+
await driver.execute_script(self._events_table_ddl_for_type(storage_type))
|
|
462
|
+
if _bare_table_name(self._app_state_table) not in existing:
|
|
463
|
+
await driver.execute_script(self._app_states_table_ddl_for_type(storage_type))
|
|
464
|
+
if _bare_table_name(self._user_state_table) not in existing:
|
|
465
|
+
await driver.execute_script(self._user_states_table_ddl_for_type(storage_type))
|
|
466
|
+
if _bare_table_name(self._metadata_table) not in existing:
|
|
467
|
+
await driver.execute_script(await self._metadata_table_ddl())
|
|
468
|
+
await driver.commit()
|
|
469
|
+
|
|
470
|
+
async def create_session(
|
|
471
|
+
self,
|
|
472
|
+
session_id: str,
|
|
473
|
+
app_name: str,
|
|
474
|
+
user_id: str,
|
|
475
|
+
state: "dict[str, Any]",
|
|
476
|
+
owner_id: "Any | None" = None,
|
|
477
|
+
*,
|
|
478
|
+
app_state_delta: "dict[str, Any] | None" = None,
|
|
479
|
+
user_state_delta: "dict[str, Any] | None" = None,
|
|
480
|
+
) -> StoredSession:
|
|
481
|
+
"""Create a new session and optionally merge scoped state deltas.
|
|
482
|
+
|
|
483
|
+
Args:
|
|
484
|
+
session_id: Unique session identifier.
|
|
485
|
+
app_name: Application name.
|
|
486
|
+
user_id: User identifier.
|
|
487
|
+
state: Initial session-scoped state.
|
|
488
|
+
owner_id: Optional owner ID value for owner_id_column (if configured).
|
|
489
|
+
app_state_delta: Optional app-scoped state delta to merge atomically.
|
|
490
|
+
user_state_delta: Optional user-scoped state delta to merge atomically.
|
|
491
|
+
|
|
492
|
+
Returns:
|
|
493
|
+
Created session record with merged scoped state.
|
|
494
|
+
"""
|
|
495
|
+
session_state = dict(state)
|
|
496
|
+
state_data = await self._serialize_state(session_state)
|
|
497
|
+
|
|
498
|
+
if self._owner_id_column_name:
|
|
499
|
+
sql = f"""
|
|
500
|
+
INSERT INTO {self._session_table} (id, app_name, user_id, state, create_time, update_time, {self._owner_id_column_name})
|
|
501
|
+
VALUES (:id, :app_name, :user_id, :state, SYSTIMESTAMP, SYSTIMESTAMP, :owner_id)
|
|
502
|
+
"""
|
|
503
|
+
params = {
|
|
504
|
+
"id": session_id,
|
|
505
|
+
"app_name": app_name,
|
|
506
|
+
"user_id": user_id,
|
|
507
|
+
"state": state_data,
|
|
508
|
+
"owner_id": owner_id,
|
|
509
|
+
}
|
|
510
|
+
else:
|
|
511
|
+
sql = f"""
|
|
512
|
+
INSERT INTO {self._session_table} (id, app_name, user_id, state, create_time, update_time)
|
|
513
|
+
VALUES (:id, :app_name, :user_id, :state, SYSTIMESTAMP, SYSTIMESTAMP)
|
|
514
|
+
"""
|
|
515
|
+
params = {"id": session_id, "app_name": app_name, "user_id": user_id, "state": state_data}
|
|
516
|
+
|
|
517
|
+
async with self._config.provide_connection() as conn:
|
|
518
|
+
try:
|
|
519
|
+
coerced_params = await coerce_large_parameters_async(conn, params)
|
|
520
|
+
with conn.cursor() as cursor:
|
|
521
|
+
await cursor.execute(sql, coerced_params)
|
|
522
|
+
current_app_state = (
|
|
523
|
+
await self._apply_app_state_delta_async(conn, cursor, app_name, app_state_delta)
|
|
524
|
+
if app_state_delta
|
|
525
|
+
else await self._select_state_async(
|
|
526
|
+
cursor,
|
|
527
|
+
f"SELECT state FROM {self._app_state_table} WHERE app_name = :app_name",
|
|
528
|
+
{"app_name": app_name},
|
|
529
|
+
)
|
|
530
|
+
)
|
|
531
|
+
current_user_state = (
|
|
532
|
+
await self._apply_user_state_delta_async(conn, cursor, app_name, user_id, user_state_delta)
|
|
533
|
+
if user_state_delta
|
|
534
|
+
else await self._select_state_async(
|
|
535
|
+
cursor,
|
|
536
|
+
f"SELECT state FROM {self._user_state_table} WHERE app_name = :app_name AND user_id = :user_id",
|
|
537
|
+
{"app_name": app_name, "user_id": user_id},
|
|
538
|
+
)
|
|
539
|
+
)
|
|
540
|
+
await conn.commit()
|
|
541
|
+
except BaseException:
|
|
542
|
+
await _rollback_async(conn)
|
|
543
|
+
raise
|
|
544
|
+
|
|
545
|
+
with adk_primary_read_scope():
|
|
546
|
+
result = await self.get_session(app_name, user_id, session_id)
|
|
547
|
+
if result is None:
|
|
548
|
+
msg = "Failed to fetch created session"
|
|
549
|
+
raise RuntimeError(msg)
|
|
550
|
+
result["state"] = merge_scoped_state(result["state"], current_app_state, current_user_state)
|
|
551
|
+
return result
|
|
552
|
+
|
|
553
|
+
async def get_session(
|
|
554
|
+
self, app_name: str, user_id: str, session_id: str, *, renew_for: "int | timedelta | None" = None
|
|
555
|
+
) -> "StoredSession | None":
|
|
556
|
+
"""Get session by ID.
|
|
557
|
+
|
|
558
|
+
Args:
|
|
559
|
+
app_name: Application name.
|
|
560
|
+
user_id: User identifier.
|
|
561
|
+
session_id: Session identifier.
|
|
562
|
+
renew_for: If positive, touch update_time while reading.
|
|
563
|
+
|
|
564
|
+
Returns:
|
|
565
|
+
Session record or None if not found.
|
|
566
|
+
|
|
567
|
+
Notes:
|
|
568
|
+
Oracle returns datetime objects for TIMESTAMP columns.
|
|
569
|
+
State is deserialized using version-appropriate format.
|
|
570
|
+
"""
|
|
571
|
+
|
|
572
|
+
try:
|
|
573
|
+
async with self._provide_replica_connection(primary=renew_for is not None) as conn:
|
|
574
|
+
with conn.cursor() as cursor:
|
|
575
|
+
if self._is_renewal_requested(renew_for):
|
|
576
|
+
await cursor.execute(
|
|
577
|
+
f"UPDATE {self._session_table} SET update_time = SYSTIMESTAMP WHERE app_name = :app_name AND user_id = :user_id AND id = :id",
|
|
578
|
+
{"app_name": app_name, "user_id": user_id, "id": session_id},
|
|
579
|
+
)
|
|
580
|
+
await conn.commit()
|
|
581
|
+
|
|
582
|
+
await cursor.execute(
|
|
583
|
+
f"""
|
|
584
|
+
SELECT id, app_name, user_id, state, create_time, update_time
|
|
585
|
+
FROM {self._session_table}
|
|
586
|
+
WHERE app_name = :app_name AND user_id = :user_id AND id = :id
|
|
587
|
+
""",
|
|
588
|
+
{"app_name": app_name, "user_id": user_id, "id": session_id},
|
|
589
|
+
)
|
|
590
|
+
row = await cursor.fetchone()
|
|
591
|
+
|
|
592
|
+
if row is None:
|
|
593
|
+
return None
|
|
594
|
+
|
|
595
|
+
session_id_val, app_name, user_id, state_data, create_time, update_time = row
|
|
596
|
+
|
|
597
|
+
state = await self._deserialize_state(state_data)
|
|
598
|
+
|
|
599
|
+
return StoredSession(
|
|
600
|
+
id=session_id_val,
|
|
601
|
+
app_name=app_name,
|
|
602
|
+
user_id=user_id,
|
|
603
|
+
state=state,
|
|
604
|
+
create_time=create_time,
|
|
605
|
+
update_time=update_time,
|
|
606
|
+
)
|
|
607
|
+
except OracleDatabaseError as e:
|
|
608
|
+
error_obj = e.args[0] if e.args else None
|
|
609
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
610
|
+
return None
|
|
611
|
+
raise
|
|
612
|
+
|
|
613
|
+
async def update_session_state(self, app_name: str, user_id: str, session_id: str, state: "dict[str, Any]") -> None:
|
|
614
|
+
"""Update session state.
|
|
615
|
+
|
|
616
|
+
Args:
|
|
617
|
+
app_name: Application name.
|
|
618
|
+
user_id: User identifier.
|
|
619
|
+
session_id: Session identifier.
|
|
620
|
+
state: New state dictionary (replaces existing state).
|
|
621
|
+
|
|
622
|
+
Notes:
|
|
623
|
+
This replaces the entire state dictionary.
|
|
624
|
+
Updates update_time to current timestamp.
|
|
625
|
+
State is serialized using version-appropriate format.
|
|
626
|
+
"""
|
|
627
|
+
state_data = await self._serialize_state(state)
|
|
628
|
+
|
|
629
|
+
sql = f"""
|
|
630
|
+
UPDATE {self._session_table}
|
|
631
|
+
SET state = :state, update_time = SYSTIMESTAMP
|
|
632
|
+
WHERE app_name = :app_name AND user_id = :user_id AND id = :id
|
|
633
|
+
"""
|
|
634
|
+
|
|
635
|
+
async with self._config.provide_connection() as conn:
|
|
636
|
+
coerced_params = await coerce_large_parameters_async(
|
|
637
|
+
conn, {"state": state_data, "app_name": app_name, "user_id": user_id, "id": session_id}
|
|
638
|
+
)
|
|
639
|
+
with conn.cursor() as cursor:
|
|
640
|
+
await cursor.execute(sql, coerced_params)
|
|
641
|
+
await conn.commit()
|
|
642
|
+
|
|
643
|
+
async def list_sessions(
|
|
644
|
+
self,
|
|
645
|
+
app_name: str,
|
|
646
|
+
user_id: "str | None" = None,
|
|
647
|
+
*,
|
|
648
|
+
order_by: "SessionOrderBy" = "update_time",
|
|
649
|
+
descending: bool = True,
|
|
650
|
+
limit: "int | None" = None,
|
|
651
|
+
offset: "int | None" = None,
|
|
652
|
+
) -> "list[StoredSession]":
|
|
653
|
+
"""List sessions for an app, optionally filtered by user.
|
|
654
|
+
|
|
655
|
+
Args:
|
|
656
|
+
app_name: Application name.
|
|
657
|
+
user_id: User identifier. If None, lists all sessions for the app.
|
|
658
|
+
order_by: Timestamp column to sort on.
|
|
659
|
+
descending: Sort direction for the timestamp column and the id tie-break.
|
|
660
|
+
limit: Maximum number of sessions to return.
|
|
661
|
+
offset: Number of leading rows to skip.
|
|
662
|
+
|
|
663
|
+
Returns:
|
|
664
|
+
List of session records.
|
|
665
|
+
|
|
666
|
+
Notes:
|
|
667
|
+
Uses composite index on (app_name, user_id) when user_id is provided.
|
|
668
|
+
State is deserialized using version-appropriate format.
|
|
669
|
+
"""
|
|
670
|
+
column, direction, page_limit, page_offset = normalize_session_list_options(order_by, descending, limit, offset)
|
|
671
|
+
if page_limit == 0:
|
|
672
|
+
return []
|
|
673
|
+
|
|
674
|
+
sql, params = _session_list_query(
|
|
675
|
+
self._session_table, app_name, user_id, column, direction, page_limit, page_offset
|
|
676
|
+
)
|
|
677
|
+
|
|
678
|
+
try:
|
|
679
|
+
async with self._provide_replica_connection(primary=False) as conn:
|
|
680
|
+
with conn.cursor() as cursor:
|
|
681
|
+
await cursor.execute(sql, params)
|
|
682
|
+
rows = await cursor.fetchall()
|
|
683
|
+
|
|
684
|
+
results = []
|
|
685
|
+
for row in rows:
|
|
686
|
+
state = await self._deserialize_state(row[3])
|
|
687
|
+
|
|
688
|
+
results.append(
|
|
689
|
+
StoredSession(
|
|
690
|
+
id=row[0],
|
|
691
|
+
app_name=row[1],
|
|
692
|
+
user_id=row[2],
|
|
693
|
+
state=state,
|
|
694
|
+
create_time=row[4],
|
|
695
|
+
update_time=row[5],
|
|
696
|
+
)
|
|
697
|
+
)
|
|
698
|
+
return results
|
|
699
|
+
except OracleDatabaseError as e:
|
|
700
|
+
error_obj = e.args[0] if e.args else None
|
|
701
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
702
|
+
return []
|
|
703
|
+
raise
|
|
704
|
+
|
|
705
|
+
async def delete_session(self, app_name: str, user_id: str, session_id: str) -> None:
|
|
706
|
+
"""Delete session and all associated events.
|
|
707
|
+
|
|
708
|
+
Args:
|
|
709
|
+
app_name: Application name.
|
|
710
|
+
user_id: User identifier.
|
|
711
|
+
session_id: Session identifier.
|
|
712
|
+
"""
|
|
713
|
+
events_sql = (
|
|
714
|
+
f"DELETE FROM {self._events_table} WHERE app_name = :app_name AND user_id = :user_id AND session_id = :id"
|
|
715
|
+
)
|
|
716
|
+
session_sql = (
|
|
717
|
+
f"DELETE FROM {self._session_table} WHERE app_name = :app_name AND user_id = :user_id AND id = :id"
|
|
718
|
+
)
|
|
719
|
+
params = {"app_name": app_name, "user_id": user_id, "id": session_id}
|
|
720
|
+
|
|
721
|
+
async with self._config.provide_connection() as conn:
|
|
722
|
+
try:
|
|
723
|
+
with conn.cursor() as cursor:
|
|
724
|
+
await cursor.execute(events_sql, params)
|
|
725
|
+
await cursor.execute(session_sql, params)
|
|
726
|
+
await conn.commit()
|
|
727
|
+
except BaseException:
|
|
728
|
+
await _rollback_async(conn)
|
|
729
|
+
raise
|
|
730
|
+
|
|
731
|
+
async def append_event(self, event_record: StoredEvent) -> None:
|
|
732
|
+
"""Append an event to a session after verifying session ownership.
|
|
733
|
+
|
|
734
|
+
Args:
|
|
735
|
+
event_record: Event record.
|
|
736
|
+
"""
|
|
737
|
+
app_name = event_record["app_name"]
|
|
738
|
+
user_id = event_record["user_id"]
|
|
739
|
+
session_id = event_record["session_id"]
|
|
740
|
+
ownership_sql = (
|
|
741
|
+
f"SELECT 1 FROM {self._session_table} "
|
|
742
|
+
"WHERE id = :id AND app_name = :app_name AND user_id = :user_id "
|
|
743
|
+
"FETCH FIRST 1 ROWS ONLY"
|
|
744
|
+
)
|
|
745
|
+
sql = f"""
|
|
746
|
+
INSERT INTO {self._events_table} (
|
|
747
|
+
id, app_name, user_id, session_id, invocation_id, timestamp, event_data
|
|
748
|
+
) VALUES (
|
|
749
|
+
:id, :app_name, :user_id, :session_id, :invocation_id, :timestamp, :event_data
|
|
750
|
+
)
|
|
751
|
+
"""
|
|
752
|
+
|
|
753
|
+
async with self._config.provide_connection() as conn:
|
|
754
|
+
try:
|
|
755
|
+
with conn.cursor() as cursor:
|
|
756
|
+
await cursor.execute(ownership_sql, {"id": session_id, "app_name": app_name, "user_id": user_id})
|
|
757
|
+
if await cursor.fetchone() is None:
|
|
758
|
+
_raise_session_not_found_for_user(session_id, app_name, user_id)
|
|
759
|
+
coerced_params = await coerce_large_parameters_async(
|
|
760
|
+
conn,
|
|
761
|
+
{
|
|
762
|
+
"id": event_record["id"],
|
|
763
|
+
"app_name": app_name,
|
|
764
|
+
"user_id": user_id,
|
|
765
|
+
"session_id": session_id,
|
|
766
|
+
"invocation_id": event_record["invocation_id"],
|
|
767
|
+
"timestamp": event_record["timestamp"],
|
|
768
|
+
"event_data": await self._serialize_event_data(event_record["event_data"]),
|
|
769
|
+
},
|
|
770
|
+
)
|
|
771
|
+
await cursor.execute(sql, coerced_params)
|
|
772
|
+
await conn.commit()
|
|
773
|
+
except BaseException:
|
|
774
|
+
await _rollback_async(conn)
|
|
775
|
+
raise
|
|
776
|
+
|
|
777
|
+
async def append_event_and_update_state(
|
|
778
|
+
self,
|
|
779
|
+
event_record: StoredEvent,
|
|
780
|
+
app_name: str,
|
|
781
|
+
user_id: str,
|
|
782
|
+
session_id: str,
|
|
783
|
+
state: "dict[str, Any]",
|
|
784
|
+
*,
|
|
785
|
+
app_state_delta: "dict[str, Any] | None" = None,
|
|
786
|
+
user_state_delta: "dict[str, Any] | None" = None,
|
|
787
|
+
) -> StoredSession:
|
|
788
|
+
"""Atomically append an event and update session + scoped state.
|
|
789
|
+
|
|
790
|
+
All writes are executed within a single transaction with ``FOR UPDATE``
|
|
791
|
+
row locks on updated scoped-state rows so they succeed or fail together.
|
|
792
|
+
"""
|
|
793
|
+
_validate_event_belongs_to_session(event_record, app_name, user_id, session_id)
|
|
794
|
+
insert_sql = f"""
|
|
795
|
+
INSERT INTO {self._events_table} (
|
|
796
|
+
id, app_name, user_id, session_id, invocation_id, timestamp, event_data
|
|
797
|
+
) VALUES (
|
|
798
|
+
:id, :app_name, :user_id, :session_id, :invocation_id, :timestamp, :event_data
|
|
799
|
+
)
|
|
800
|
+
"""
|
|
801
|
+
|
|
802
|
+
session_state = dict(state)
|
|
803
|
+
state_data = await self._serialize_state(session_state)
|
|
804
|
+
update_sql = f"""
|
|
805
|
+
UPDATE {self._session_table}
|
|
806
|
+
SET state = :state, update_time = SYSTIMESTAMP
|
|
807
|
+
WHERE app_name = :app_name AND user_id = :user_id AND id = :id
|
|
808
|
+
"""
|
|
809
|
+
|
|
810
|
+
select_sql = f"""
|
|
811
|
+
SELECT id, app_name, user_id, state, create_time, update_time
|
|
812
|
+
FROM {self._session_table}
|
|
813
|
+
WHERE app_name = :app_name AND user_id = :user_id AND id = :id
|
|
814
|
+
"""
|
|
815
|
+
|
|
816
|
+
merged_app_state: dict[str, Any] | None = None
|
|
817
|
+
merged_user_state: dict[str, Any] | None = None
|
|
818
|
+
async with self._config.provide_connection() as conn:
|
|
819
|
+
with conn.cursor() as cursor:
|
|
820
|
+
try:
|
|
821
|
+
update_params = await coerce_large_parameters_async(
|
|
822
|
+
conn, {"state": state_data, "app_name": app_name, "user_id": user_id, "id": session_id}
|
|
823
|
+
)
|
|
824
|
+
await cursor.execute(update_sql, update_params)
|
|
825
|
+
await cursor.execute(select_sql, {"app_name": app_name, "user_id": user_id, "id": session_id})
|
|
826
|
+
row = await cursor.fetchone()
|
|
827
|
+
if row is None:
|
|
828
|
+
_raise_session_not_found(session_id)
|
|
829
|
+
insert_params = await coerce_large_parameters_async(
|
|
830
|
+
conn,
|
|
831
|
+
{
|
|
832
|
+
"id": event_record["id"],
|
|
833
|
+
"app_name": event_record["app_name"],
|
|
834
|
+
"user_id": event_record["user_id"],
|
|
835
|
+
"session_id": event_record["session_id"],
|
|
836
|
+
"invocation_id": event_record["invocation_id"],
|
|
837
|
+
"timestamp": event_record["timestamp"],
|
|
838
|
+
"event_data": await self._serialize_event_data(event_record["event_data"]),
|
|
839
|
+
},
|
|
840
|
+
)
|
|
841
|
+
await cursor.execute(insert_sql, insert_params)
|
|
842
|
+
if app_state_delta:
|
|
843
|
+
merged_app_state = await self._apply_app_state_delta_async(
|
|
844
|
+
conn, cursor, app_name, app_state_delta
|
|
845
|
+
)
|
|
846
|
+
if user_state_delta:
|
|
847
|
+
merged_user_state = await self._apply_user_state_delta_async(
|
|
848
|
+
conn, cursor, app_name, user_id, user_state_delta
|
|
849
|
+
)
|
|
850
|
+
await conn.commit()
|
|
851
|
+
except Exception:
|
|
852
|
+
await conn.rollback()
|
|
853
|
+
raise
|
|
854
|
+
|
|
855
|
+
session_id_val, row_app_name, row_user_id, state_data_row, create_time, update_time = row
|
|
856
|
+
stored_session_state = await self._deserialize_state(state_data_row)
|
|
857
|
+
return StoredSession(
|
|
858
|
+
id=session_id_val,
|
|
859
|
+
app_name=row_app_name,
|
|
860
|
+
user_id=row_user_id,
|
|
861
|
+
state=merge_scoped_state(stored_session_state, merged_app_state, merged_user_state),
|
|
862
|
+
create_time=create_time,
|
|
863
|
+
update_time=update_time,
|
|
864
|
+
)
|
|
865
|
+
|
|
866
|
+
async def get_events(
|
|
867
|
+
self,
|
|
868
|
+
app_name: str,
|
|
869
|
+
user_id: str,
|
|
870
|
+
session_id: str,
|
|
871
|
+
after_timestamp: "datetime | None" = None,
|
|
872
|
+
limit: "int | None" = None,
|
|
873
|
+
) -> "list[StoredEvent]":
|
|
874
|
+
"""Get events for a session.
|
|
875
|
+
|
|
876
|
+
Args:
|
|
877
|
+
app_name: Application name.
|
|
878
|
+
user_id: User identifier.
|
|
879
|
+
session_id: Session identifier.
|
|
880
|
+
after_timestamp: Only return events after this time.
|
|
881
|
+
limit: Maximum number of events to return.
|
|
882
|
+
|
|
883
|
+
Returns:
|
|
884
|
+
List of event records ordered by timestamp ASC.
|
|
885
|
+
"""
|
|
886
|
+
if limit is not None and (isinstance(limit, bool) or limit < 0):
|
|
887
|
+
msg = "limit must be non-negative"
|
|
888
|
+
raise ValueError(msg)
|
|
889
|
+
if limit == 0:
|
|
890
|
+
return []
|
|
891
|
+
|
|
892
|
+
where_clauses = ["s.app_name = :app_name", "s.user_id = :user_id", "s.id = :session_id"]
|
|
893
|
+
params: dict[str, Any] = {"app_name": app_name, "user_id": user_id, "session_id": session_id}
|
|
894
|
+
|
|
895
|
+
if after_timestamp is not None:
|
|
896
|
+
where_clauses.append("e.timestamp > :after_timestamp")
|
|
897
|
+
params["after_timestamp"] = after_timestamp
|
|
898
|
+
|
|
899
|
+
where_clause = " AND ".join(where_clauses)
|
|
900
|
+
if limit is not None:
|
|
901
|
+
order_and_limit = f"ORDER BY e.timestamp DESC, e.id DESC FETCH FIRST {int(limit)} ROWS ONLY"
|
|
902
|
+
else:
|
|
903
|
+
order_and_limit = "ORDER BY e.timestamp ASC, e.id ASC"
|
|
904
|
+
|
|
905
|
+
sql = f"""
|
|
906
|
+
SELECT e.id, e.session_id, e.invocation_id, e.timestamp, e.event_data, s.app_name, s.user_id
|
|
907
|
+
FROM {self._events_table} e
|
|
908
|
+
JOIN {self._session_table} s ON e.session_id = s.id AND e.app_name = s.app_name AND e.user_id = s.user_id
|
|
909
|
+
WHERE {where_clause}
|
|
910
|
+
{order_and_limit}
|
|
911
|
+
"""
|
|
912
|
+
|
|
913
|
+
try:
|
|
914
|
+
async with self._provide_replica_connection(primary=False) as conn:
|
|
915
|
+
with conn.cursor() as cursor:
|
|
916
|
+
await cursor.execute(sql, params)
|
|
917
|
+
rows = await cursor.fetchall()
|
|
918
|
+
if limit is not None:
|
|
919
|
+
rows = list(reversed(rows))
|
|
920
|
+
|
|
921
|
+
return [
|
|
922
|
+
StoredEvent(
|
|
923
|
+
id=row[0],
|
|
924
|
+
session_id=row[1],
|
|
925
|
+
invocation_id=_oracle_text_value(row[2]),
|
|
926
|
+
timestamp=row[3],
|
|
927
|
+
event_data=await self._deserialize_json_field(row[4]) or {},
|
|
928
|
+
app_name=row[5],
|
|
929
|
+
user_id=row[6],
|
|
930
|
+
)
|
|
931
|
+
for row in rows
|
|
932
|
+
]
|
|
933
|
+
except OracleDatabaseError as e:
|
|
934
|
+
error_obj = e.args[0] if e.args else None
|
|
935
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
936
|
+
return []
|
|
937
|
+
raise
|
|
938
|
+
|
|
939
|
+
async def delete_expired_events(self, before: "datetime", app_name: "str | None" = None) -> int:
|
|
940
|
+
sql = f"DELETE FROM {self._events_table} WHERE timestamp < :before"
|
|
941
|
+
params: dict[str, Any] = {"before": before}
|
|
942
|
+
if app_name is not None:
|
|
943
|
+
sql += " AND app_name = :app_name"
|
|
944
|
+
params["app_name"] = app_name
|
|
945
|
+
|
|
946
|
+
try:
|
|
947
|
+
async with self._config.provide_connection() as conn:
|
|
948
|
+
with conn.cursor() as cursor:
|
|
949
|
+
await cursor.execute(sql, params)
|
|
950
|
+
await conn.commit()
|
|
951
|
+
return cursor.rowcount if cursor.rowcount and cursor.rowcount > 0 else 0
|
|
952
|
+
except OracleDatabaseError as e:
|
|
953
|
+
error_obj = e.args[0] if e.args else None
|
|
954
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
955
|
+
return 0
|
|
956
|
+
raise
|
|
957
|
+
|
|
958
|
+
async def delete_idle_sessions(self, updated_before: "datetime", app_name: "str | None" = None) -> int:
|
|
959
|
+
sql = f"DELETE FROM {self._session_table} WHERE update_time < :updated_before"
|
|
960
|
+
params: dict[str, Any] = {"updated_before": updated_before}
|
|
961
|
+
if app_name is not None:
|
|
962
|
+
sql += " AND app_name = :app_name"
|
|
963
|
+
params["app_name"] = app_name
|
|
964
|
+
|
|
965
|
+
try:
|
|
966
|
+
async with self._config.provide_connection() as conn:
|
|
967
|
+
with conn.cursor() as cursor:
|
|
968
|
+
await cursor.execute(sql, params)
|
|
969
|
+
await conn.commit()
|
|
970
|
+
return cursor.rowcount if cursor.rowcount and cursor.rowcount > 0 else 0
|
|
971
|
+
except OracleDatabaseError as e:
|
|
972
|
+
error_obj = e.args[0] if e.args else None
|
|
973
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
974
|
+
return 0
|
|
975
|
+
raise
|
|
976
|
+
|
|
977
|
+
async def delete_idle_user_states(self, updated_before: "datetime", app_name: "str | None" = None) -> int:
|
|
978
|
+
sql = f"DELETE FROM {self._user_state_table} WHERE update_time < :updated_before"
|
|
979
|
+
params: dict[str, Any] = {"updated_before": updated_before}
|
|
980
|
+
if app_name is not None:
|
|
981
|
+
sql += " AND app_name = :app_name"
|
|
982
|
+
params["app_name"] = app_name
|
|
983
|
+
|
|
984
|
+
try:
|
|
985
|
+
async with self._config.provide_connection() as conn:
|
|
986
|
+
with conn.cursor() as cursor:
|
|
987
|
+
await cursor.execute(sql, params)
|
|
988
|
+
await conn.commit()
|
|
989
|
+
return cursor.rowcount if cursor.rowcount and cursor.rowcount > 0 else 0
|
|
990
|
+
except OracleDatabaseError as e:
|
|
991
|
+
error_obj = e.args[0] if e.args else None
|
|
992
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
993
|
+
return 0
|
|
994
|
+
raise
|
|
995
|
+
|
|
996
|
+
async def get_app_state(self, app_name: str) -> "dict[str, Any] | None":
|
|
997
|
+
"""Return app-scoped state for an application."""
|
|
998
|
+
sql = f"SELECT state FROM {self._app_state_table} WHERE app_name = :app_name"
|
|
999
|
+
|
|
1000
|
+
try:
|
|
1001
|
+
async with self._config.provide_connection() as conn:
|
|
1002
|
+
with conn.cursor() as cursor:
|
|
1003
|
+
await cursor.execute(sql, {"app_name": app_name})
|
|
1004
|
+
row = await cursor.fetchone()
|
|
1005
|
+
return await self._deserialize_state(row[0]) if row is not None else None
|
|
1006
|
+
except OracleDatabaseError as e:
|
|
1007
|
+
error_obj = e.args[0] if e.args else None
|
|
1008
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
1009
|
+
return None
|
|
1010
|
+
raise
|
|
1011
|
+
|
|
1012
|
+
async def get_user_state(self, app_name: str, user_id: str) -> "dict[str, Any] | None":
|
|
1013
|
+
"""Return user-scoped state for an application user."""
|
|
1014
|
+
sql = f"""
|
|
1015
|
+
SELECT state
|
|
1016
|
+
FROM {self._user_state_table}
|
|
1017
|
+
WHERE app_name = :app_name AND user_id = :user_id
|
|
1018
|
+
"""
|
|
1019
|
+
|
|
1020
|
+
try:
|
|
1021
|
+
async with self._config.provide_connection() as conn:
|
|
1022
|
+
with conn.cursor() as cursor:
|
|
1023
|
+
await cursor.execute(sql, {"app_name": app_name, "user_id": user_id})
|
|
1024
|
+
row = await cursor.fetchone()
|
|
1025
|
+
return await self._deserialize_state(row[0]) if row is not None else None
|
|
1026
|
+
except OracleDatabaseError as e:
|
|
1027
|
+
error_obj = e.args[0] if e.args else None
|
|
1028
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
1029
|
+
return None
|
|
1030
|
+
raise
|
|
1031
|
+
|
|
1032
|
+
async def upsert_app_state(self, app_name: str, state: "dict[str, Any]") -> None:
|
|
1033
|
+
"""Insert or replace app-scoped state for an application."""
|
|
1034
|
+
sql = f"""
|
|
1035
|
+
MERGE INTO {self._app_state_table} target
|
|
1036
|
+
USING (SELECT :app_name AS app_name FROM DUAL) source
|
|
1037
|
+
ON (target.app_name = source.app_name)
|
|
1038
|
+
WHEN MATCHED THEN
|
|
1039
|
+
UPDATE SET target.state = :state, target.update_time = SYSTIMESTAMP
|
|
1040
|
+
WHEN NOT MATCHED THEN
|
|
1041
|
+
INSERT (app_name, state, update_time)
|
|
1042
|
+
VALUES (source.app_name, :state, SYSTIMESTAMP)
|
|
1043
|
+
"""
|
|
1044
|
+
|
|
1045
|
+
async with self._config.provide_connection() as conn:
|
|
1046
|
+
coerced_params = await coerce_large_parameters_async(
|
|
1047
|
+
conn, {"app_name": app_name, "state": await self._serialize_state(state)}
|
|
1048
|
+
)
|
|
1049
|
+
with conn.cursor() as cursor:
|
|
1050
|
+
await cursor.execute(sql, coerced_params)
|
|
1051
|
+
await conn.commit()
|
|
1052
|
+
|
|
1053
|
+
async def upsert_user_state(self, app_name: str, user_id: str, state: "dict[str, Any]") -> None:
|
|
1054
|
+
"""Insert or replace user-scoped state for an application user."""
|
|
1055
|
+
sql = f"""
|
|
1056
|
+
MERGE INTO {self._user_state_table} target
|
|
1057
|
+
USING (SELECT :app_name AS app_name, :user_id AS user_id FROM DUAL) source
|
|
1058
|
+
ON (target.app_name = source.app_name AND target.user_id = source.user_id)
|
|
1059
|
+
WHEN MATCHED THEN
|
|
1060
|
+
UPDATE SET target.state = :state, target.update_time = SYSTIMESTAMP
|
|
1061
|
+
WHEN NOT MATCHED THEN
|
|
1062
|
+
INSERT (app_name, user_id, state, update_time)
|
|
1063
|
+
VALUES (source.app_name, source.user_id, :state, SYSTIMESTAMP)
|
|
1064
|
+
"""
|
|
1065
|
+
|
|
1066
|
+
async with self._config.provide_connection() as conn:
|
|
1067
|
+
coerced_params = await coerce_large_parameters_async(
|
|
1068
|
+
conn, {"app_name": app_name, "user_id": user_id, "state": await self._serialize_state(state)}
|
|
1069
|
+
)
|
|
1070
|
+
with conn.cursor() as cursor:
|
|
1071
|
+
await cursor.execute(sql, coerced_params)
|
|
1072
|
+
await conn.commit()
|
|
1073
|
+
|
|
1074
|
+
async def get_metadata(self, key: str) -> "str | None":
|
|
1075
|
+
"""Return a value from the ADK internal metadata table."""
|
|
1076
|
+
sql = f"SELECT value FROM {self._metadata_table} WHERE key = :key"
|
|
1077
|
+
|
|
1078
|
+
try:
|
|
1079
|
+
async with self._config.provide_connection() as conn:
|
|
1080
|
+
with conn.cursor() as cursor:
|
|
1081
|
+
await cursor.execute(sql, {"key": key})
|
|
1082
|
+
row = await cursor.fetchone()
|
|
1083
|
+
return str(row[0]) if row is not None else None
|
|
1084
|
+
except OracleDatabaseError as e:
|
|
1085
|
+
error_obj = e.args[0] if e.args else None
|
|
1086
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
1087
|
+
return None
|
|
1088
|
+
raise
|
|
1089
|
+
|
|
1090
|
+
async def set_metadata(self, key: str, value: str) -> None:
|
|
1091
|
+
"""Set a value in the ADK internal metadata table."""
|
|
1092
|
+
sql = f"""
|
|
1093
|
+
MERGE INTO {self._metadata_table} target
|
|
1094
|
+
USING (SELECT :key AS key, :value AS value FROM DUAL) source
|
|
1095
|
+
ON (target.key = source.key)
|
|
1096
|
+
WHEN MATCHED THEN
|
|
1097
|
+
UPDATE SET target.value = source.value
|
|
1098
|
+
WHEN NOT MATCHED THEN
|
|
1099
|
+
INSERT (key, value)
|
|
1100
|
+
VALUES (source.key, source.value)
|
|
1101
|
+
"""
|
|
1102
|
+
|
|
1103
|
+
async with self._config.provide_connection() as conn:
|
|
1104
|
+
with conn.cursor() as cursor:
|
|
1105
|
+
await cursor.execute(sql, {"key": key, "value": value})
|
|
1106
|
+
await conn.commit()
|
|
1107
|
+
|
|
1108
|
+
async def prepare_schema_async(self, driver: Any) -> None:
|
|
1109
|
+
"""Prepare adapter-specific schema decisions with an asynchronous driver."""
|
|
1110
|
+
await _prepare_oracle_schema_async(self._config, driver)
|
|
1111
|
+
|
|
1112
|
+
async def reconcile_companion_objects_async(self, driver: Any) -> None:
|
|
1113
|
+
"""Create missing Oracle session and event companion indexes via catalog comparison.
|
|
1114
|
+
|
|
1115
|
+
Args:
|
|
1116
|
+
driver: Asynchronous Oracle driver executing schema reconciliation.
|
|
1117
|
+
"""
|
|
1118
|
+
specs = _oracle_session_index_specs(self._session_table) + _oracle_event_index_specs(self._events_table)
|
|
1119
|
+
rows = await driver.data_dictionary.get_indexes(driver)
|
|
1120
|
+
for statement in _oracle_missing_index_statements(specs, rows):
|
|
1121
|
+
await driver.execute(statement)
|
|
1122
|
+
|
|
1123
|
+
async def _select_state_async(self, cursor: Any, sql: str, params: "dict[str, Any]") -> "dict[str, Any] | None":
|
|
1124
|
+
try:
|
|
1125
|
+
await cursor.execute(sql, params)
|
|
1126
|
+
row = await cursor.fetchone()
|
|
1127
|
+
except OracleDatabaseError as exc:
|
|
1128
|
+
error_obj = exc.args[0] if exc.args else None
|
|
1129
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
1130
|
+
return None
|
|
1131
|
+
raise
|
|
1132
|
+
return await self._deserialize_state(row[0]) if row is not None else None
|
|
1133
|
+
|
|
1134
|
+
async def _apply_app_state_delta_async(
|
|
1135
|
+
self, conn: Any, cursor: Any, app_name: str, delta: "dict[str, Any]"
|
|
1136
|
+
) -> "dict[str, Any]":
|
|
1137
|
+
return await self._apply_state_delta_async(
|
|
1138
|
+
conn,
|
|
1139
|
+
cursor,
|
|
1140
|
+
select_for_update_sql=f"SELECT state FROM {self._app_state_table} WHERE app_name = :app_name FOR UPDATE",
|
|
1141
|
+
insert_sql=f"INSERT INTO {self._app_state_table} (app_name, state, update_time) VALUES (:app_name, :state, SYSTIMESTAMP)",
|
|
1142
|
+
update_sql=f"UPDATE {self._app_state_table} SET state = :state, update_time = SYSTIMESTAMP WHERE app_name = :app_name",
|
|
1143
|
+
key_params={"app_name": app_name},
|
|
1144
|
+
delta=delta,
|
|
1145
|
+
)
|
|
1146
|
+
|
|
1147
|
+
async def _apply_user_state_delta_async(
|
|
1148
|
+
self, conn: Any, cursor: Any, app_name: str, user_id: str, delta: "dict[str, Any]"
|
|
1149
|
+
) -> "dict[str, Any]":
|
|
1150
|
+
return await self._apply_state_delta_async(
|
|
1151
|
+
conn,
|
|
1152
|
+
cursor,
|
|
1153
|
+
select_for_update_sql=(
|
|
1154
|
+
f"SELECT state FROM {self._user_state_table} WHERE app_name = :app_name AND user_id = :user_id FOR UPDATE"
|
|
1155
|
+
),
|
|
1156
|
+
insert_sql=(
|
|
1157
|
+
f"INSERT INTO {self._user_state_table} (app_name, user_id, state, update_time) "
|
|
1158
|
+
"VALUES (:app_name, :user_id, :state, SYSTIMESTAMP)"
|
|
1159
|
+
),
|
|
1160
|
+
update_sql=(
|
|
1161
|
+
f"UPDATE {self._user_state_table} SET state = :state, update_time = SYSTIMESTAMP "
|
|
1162
|
+
"WHERE app_name = :app_name AND user_id = :user_id"
|
|
1163
|
+
),
|
|
1164
|
+
key_params={"app_name": app_name, "user_id": user_id},
|
|
1165
|
+
delta=delta,
|
|
1166
|
+
)
|
|
1167
|
+
|
|
1168
|
+
async def _apply_state_delta_async(
|
|
1169
|
+
self,
|
|
1170
|
+
conn: Any,
|
|
1171
|
+
cursor: Any,
|
|
1172
|
+
*,
|
|
1173
|
+
select_for_update_sql: str,
|
|
1174
|
+
insert_sql: str,
|
|
1175
|
+
update_sql: str,
|
|
1176
|
+
key_params: "dict[str, Any]",
|
|
1177
|
+
delta: "dict[str, Any]",
|
|
1178
|
+
) -> "dict[str, Any]":
|
|
1179
|
+
for _ in range(_MAX_STATE_MERGE_ATTEMPTS):
|
|
1180
|
+
await cursor.execute(select_for_update_sql, key_params)
|
|
1181
|
+
row = await cursor.fetchone()
|
|
1182
|
+
if row is None:
|
|
1183
|
+
merged = dict(delta)
|
|
1184
|
+
insert_params = await coerce_large_parameters_async(
|
|
1185
|
+
conn, {**key_params, "state": await self._serialize_state(merged)}
|
|
1186
|
+
)
|
|
1187
|
+
try:
|
|
1188
|
+
await cursor.execute(insert_sql, insert_params)
|
|
1189
|
+
except OracleDatabaseError as exc:
|
|
1190
|
+
if not _is_unique_violation(exc):
|
|
1191
|
+
raise
|
|
1192
|
+
continue
|
|
1193
|
+
else:
|
|
1194
|
+
return merged
|
|
1195
|
+
existing = await self._deserialize_state(row[0])
|
|
1196
|
+
merged = {**existing, **delta}
|
|
1197
|
+
update_params = await coerce_large_parameters_async(
|
|
1198
|
+
conn, {**key_params, "state": await self._serialize_state(merged)}
|
|
1199
|
+
)
|
|
1200
|
+
await cursor.execute(update_sql, update_params)
|
|
1201
|
+
return merged
|
|
1202
|
+
msg = f"Failed to lock and merge Oracle scoped state after {_MAX_STATE_MERGE_ATTEMPTS} attempts."
|
|
1203
|
+
raise SerializationConflictError(msg)
|
|
1204
|
+
|
|
1205
|
+
async def _sessions_table_ddl(self) -> str:
|
|
1206
|
+
"""Get Oracle CREATE TABLE SQL for sessions table.
|
|
1207
|
+
|
|
1208
|
+
Auto-detects optimal JSON storage type based on Oracle version.
|
|
1209
|
+
Result is cached to minimize database queries.
|
|
1210
|
+
"""
|
|
1211
|
+
storage_type = await self._detect_json_storage_type()
|
|
1212
|
+
return self._sessions_table_ddl_for_type(storage_type)
|
|
1213
|
+
|
|
1214
|
+
async def _events_table_ddl(self) -> str:
|
|
1215
|
+
"""Get Oracle CREATE TABLE SQL for events table.
|
|
1216
|
+
|
|
1217
|
+
Auto-detects optimal JSON storage type based on Oracle version.
|
|
1218
|
+
Result is cached to minimize database queries.
|
|
1219
|
+
"""
|
|
1220
|
+
storage_type = await self._detect_json_storage_type()
|
|
1221
|
+
return self._events_table_ddl_for_type(storage_type)
|
|
1222
|
+
|
|
1223
|
+
async def _app_states_table_ddl(self) -> str:
|
|
1224
|
+
"""Get Oracle CREATE TABLE SQL for app-scoped state."""
|
|
1225
|
+
storage_type = await self._detect_json_storage_type()
|
|
1226
|
+
return self._app_states_table_ddl_for_type(storage_type)
|
|
1227
|
+
|
|
1228
|
+
async def _user_states_table_ddl(self) -> str:
|
|
1229
|
+
"""Get Oracle CREATE TABLE SQL for user-scoped state."""
|
|
1230
|
+
storage_type = await self._detect_json_storage_type()
|
|
1231
|
+
return self._user_states_table_ddl_for_type(storage_type)
|
|
1232
|
+
|
|
1233
|
+
async def _metadata_table_ddl(self) -> str:
|
|
1234
|
+
"""Get Oracle CREATE TABLE SQL for ADK internal metadata."""
|
|
1235
|
+
return _ADK_METADATA_TABLE_DDL_TEMPLATE.format(self._metadata_table)
|
|
1236
|
+
|
|
1237
|
+
async def _detect_json_storage_type(self) -> JSONStorageType:
|
|
1238
|
+
"""Resolve the JSON storage type from the pool-scoped Oracle version.
|
|
1239
|
+
|
|
1240
|
+
- Oracle 21c+ with compatible >= 20: native JSON type
|
|
1241
|
+
- Oracle 12c+: BLOB with IS JSON constraint
|
|
1242
|
+
- Oracle 11g and earlier: plain BLOB
|
|
1243
|
+
"""
|
|
1244
|
+
return storage_type_from_version(await self._get_version_info())
|
|
1245
|
+
|
|
1246
|
+
async def _get_version_info(self) -> "OracleVersionInfo | None":
|
|
1247
|
+
"""Return the pool-scoped Oracle version through the data dictionary."""
|
|
1248
|
+
return await _get_oracle_version_info_async(self._config)
|
|
1249
|
+
|
|
1250
|
+
async def _serialize_state(self, state: "dict[str, Any]") -> "str | bytes":
|
|
1251
|
+
"""Serialize state dictionary to appropriate format based on storage type.
|
|
1252
|
+
|
|
1253
|
+
Args:
|
|
1254
|
+
state: State dictionary to serialize.
|
|
1255
|
+
|
|
1256
|
+
Returns:
|
|
1257
|
+
JSON string for JSON_NATIVE, bytes for BLOB types.
|
|
1258
|
+
"""
|
|
1259
|
+
storage_type = await self._detect_json_storage_type()
|
|
1260
|
+
|
|
1261
|
+
if storage_type == JSONStorageType.JSON_NATIVE:
|
|
1262
|
+
return to_json(state)
|
|
1263
|
+
|
|
1264
|
+
return to_json(state, as_bytes=True)
|
|
1265
|
+
|
|
1266
|
+
async def _deserialize_state(self, data: Any) -> "dict[str, Any]":
|
|
1267
|
+
"""Deserialize state data from database format.
|
|
1268
|
+
|
|
1269
|
+
Args:
|
|
1270
|
+
data: Data from database (may be LOB, str, bytes, or dict).
|
|
1271
|
+
|
|
1272
|
+
Returns:
|
|
1273
|
+
Deserialized state dictionary.
|
|
1274
|
+
|
|
1275
|
+
Notes:
|
|
1276
|
+
Handles LOB reading if data has read() method.
|
|
1277
|
+
Oracle JSON type may return dict directly.
|
|
1278
|
+
"""
|
|
1279
|
+
if is_async_readable(data):
|
|
1280
|
+
data = await data.read()
|
|
1281
|
+
elif is_readable(data):
|
|
1282
|
+
data = data.read()
|
|
1283
|
+
|
|
1284
|
+
if isinstance(data, dict):
|
|
1285
|
+
return cast("dict[str, Any]", _coerce_decimal_values(data))
|
|
1286
|
+
|
|
1287
|
+
if isinstance(data, bytes):
|
|
1288
|
+
return cast("dict[str, Any]", from_json(data))
|
|
1289
|
+
|
|
1290
|
+
if isinstance(data, str):
|
|
1291
|
+
return cast("dict[str, Any]", from_json(data))
|
|
1292
|
+
|
|
1293
|
+
return cast("dict[str, Any]", from_json(str(data)))
|
|
1294
|
+
|
|
1295
|
+
async def _deserialize_json_field(self, data: Any) -> "dict[str, Any] | None":
|
|
1296
|
+
"""Deserialize JSON payloads from Oracle JSON/BLOB/LOB values."""
|
|
1297
|
+
if data is None:
|
|
1298
|
+
return None
|
|
1299
|
+
return await self._deserialize_state(data)
|
|
1300
|
+
|
|
1301
|
+
async def _serialize_event_data(self, event_data: Any) -> "str | bytes":
|
|
1302
|
+
"""Serialize event_data to the configured Oracle JSON storage format."""
|
|
1303
|
+
storage_type = await self._detect_json_storage_type()
|
|
1304
|
+
event_data = _normalize_event_data_for_storage(event_data)
|
|
1305
|
+
if storage_type == JSONStorageType.JSON_NATIVE:
|
|
1306
|
+
return to_json(event_data)
|
|
1307
|
+
return to_json(event_data, as_bytes=True)
|
|
1308
|
+
|
|
1309
|
+
async def _read_event_data(self, data: Any) -> str:
|
|
1310
|
+
"""Read event_data from database, handling LOB types.
|
|
1311
|
+
|
|
1312
|
+
Args:
|
|
1313
|
+
data: Data from database (may be LOB, str, or dict).
|
|
1314
|
+
|
|
1315
|
+
Returns:
|
|
1316
|
+
JSON string.
|
|
1317
|
+
"""
|
|
1318
|
+
if is_async_readable(data):
|
|
1319
|
+
data = await data.read()
|
|
1320
|
+
elif is_readable(data):
|
|
1321
|
+
data = data.read()
|
|
1322
|
+
|
|
1323
|
+
if isinstance(data, dict):
|
|
1324
|
+
return to_json(data)
|
|
1325
|
+
|
|
1326
|
+
if isinstance(data, bytes):
|
|
1327
|
+
return data.decode("utf-8")
|
|
1328
|
+
|
|
1329
|
+
return str(data)
|
|
1330
|
+
|
|
1331
|
+
def _sessions_table_ddl_for_type(self, storage_type: JSONStorageType) -> str:
|
|
1332
|
+
"""Get Oracle CREATE TABLE SQL for sessions with specified storage type.
|
|
1333
|
+
|
|
1334
|
+
Args:
|
|
1335
|
+
storage_type: JSON storage type to use.
|
|
1336
|
+
|
|
1337
|
+
Returns:
|
|
1338
|
+
SQL statement to create adk_session table.
|
|
1339
|
+
"""
|
|
1340
|
+
if storage_type == JSONStorageType.JSON_NATIVE:
|
|
1341
|
+
state_column = "state JSON NOT NULL"
|
|
1342
|
+
elif storage_type == JSONStorageType.BLOB_JSON:
|
|
1343
|
+
state_column = "state BLOB CHECK (state IS JSON) NOT NULL"
|
|
1344
|
+
else:
|
|
1345
|
+
state_column = "state BLOB NOT NULL"
|
|
1346
|
+
|
|
1347
|
+
owner_id_column_sql = (
|
|
1348
|
+
_ADK_SESSIONS_TABLE_DDL_FOR_TYPE_TEMPLATE.format(self._owner_id_column_ddl.replace("'", "''"))
|
|
1349
|
+
if self._owner_id_column_ddl
|
|
1350
|
+
else ""
|
|
1351
|
+
)
|
|
1352
|
+
table_clauses = _adk_table_feature_clause(
|
|
1353
|
+
self._config,
|
|
1354
|
+
"session",
|
|
1355
|
+
in_memory=self._in_memory,
|
|
1356
|
+
hash_partition_key="id",
|
|
1357
|
+
range_partition_key="create_time",
|
|
1358
|
+
)
|
|
1359
|
+
|
|
1360
|
+
return _ADK_SESSIONS_TABLE_DDL_FOR_TYPE_TEMPLATE_2.format(
|
|
1361
|
+
self._session_table,
|
|
1362
|
+
state_column,
|
|
1363
|
+
owner_id_column_sql,
|
|
1364
|
+
table_clauses,
|
|
1365
|
+
self._session_table,
|
|
1366
|
+
self._session_table,
|
|
1367
|
+
self._session_table,
|
|
1368
|
+
self._session_table,
|
|
1369
|
+
)
|
|
1370
|
+
|
|
1371
|
+
def _events_table_ddl_for_type(self, storage_type: JSONStorageType) -> str:
|
|
1372
|
+
"""Get Oracle CREATE TABLE SQL for events with specified storage type.
|
|
1373
|
+
|
|
1374
|
+
The events table stores the full ADK Event in ``event_data`` and
|
|
1375
|
+
keeps scalar event fields indexed for efficient scoped reads.
|
|
1376
|
+
|
|
1377
|
+
Args:
|
|
1378
|
+
storage_type: JSON storage type to use.
|
|
1379
|
+
|
|
1380
|
+
Returns:
|
|
1381
|
+
SQL statement to create adk_event table.
|
|
1382
|
+
"""
|
|
1383
|
+
event_data_col = _event_data_column_ddl(storage_type)
|
|
1384
|
+
table_clauses = _adk_table_feature_clause(
|
|
1385
|
+
self._config,
|
|
1386
|
+
"events",
|
|
1387
|
+
in_memory=self._in_memory,
|
|
1388
|
+
hash_partition_key="session_id",
|
|
1389
|
+
range_partition_key="timestamp",
|
|
1390
|
+
)
|
|
1391
|
+
|
|
1392
|
+
return _ADK_EVENTS_TABLE_DDL_FOR_TYPE_TEMPLATE.format(
|
|
1393
|
+
self._events_table,
|
|
1394
|
+
event_data_col,
|
|
1395
|
+
self._events_table,
|
|
1396
|
+
self._session_table,
|
|
1397
|
+
table_clauses,
|
|
1398
|
+
self._events_table,
|
|
1399
|
+
self._events_table,
|
|
1400
|
+
self._events_table,
|
|
1401
|
+
self._events_table,
|
|
1402
|
+
self._events_table,
|
|
1403
|
+
self._events_table,
|
|
1404
|
+
self._events_table,
|
|
1405
|
+
self._events_table,
|
|
1406
|
+
)
|
|
1407
|
+
|
|
1408
|
+
def _app_states_table_ddl_for_type(self, storage_type: JSONStorageType) -> str:
|
|
1409
|
+
"""Get Oracle CREATE TABLE SQL for app-scoped state with specified storage type."""
|
|
1410
|
+
state_column = _json_column_ddl("state", storage_type)
|
|
1411
|
+
table_clauses = _adk_table_feature_clause(
|
|
1412
|
+
self._config,
|
|
1413
|
+
"app_state",
|
|
1414
|
+
in_memory=self._in_memory,
|
|
1415
|
+
hash_partition_key="app_name",
|
|
1416
|
+
range_partition_key="update_time",
|
|
1417
|
+
)
|
|
1418
|
+
|
|
1419
|
+
return _ADK_APP_STATES_TABLE_DDL_FOR_TYPE_TEMPLATE.format(self._app_state_table, state_column, table_clauses)
|
|
1420
|
+
|
|
1421
|
+
def _user_states_table_ddl_for_type(self, storage_type: JSONStorageType) -> str:
|
|
1422
|
+
"""Get Oracle CREATE TABLE SQL for user-scoped state with specified storage type."""
|
|
1423
|
+
state_column = _json_column_ddl("state", storage_type)
|
|
1424
|
+
table_clauses = _adk_table_feature_clause(
|
|
1425
|
+
self._config,
|
|
1426
|
+
"user_state",
|
|
1427
|
+
in_memory=self._in_memory,
|
|
1428
|
+
hash_partition_key="user_id",
|
|
1429
|
+
range_partition_key="update_time",
|
|
1430
|
+
)
|
|
1431
|
+
|
|
1432
|
+
return _ADK_USER_STATES_TABLE_DDL_FOR_TYPE_TEMPLATE.format(self._user_state_table, state_column, table_clauses)
|
|
1433
|
+
|
|
1434
|
+
def _drop_app_states_table_sql(self) -> str:
|
|
1435
|
+
return f"""
|
|
1436
|
+
BEGIN
|
|
1437
|
+
EXECUTE IMMEDIATE 'DROP TABLE {self._app_state_table}';
|
|
1438
|
+
EXCEPTION
|
|
1439
|
+
WHEN OTHERS THEN
|
|
1440
|
+
IF SQLCODE != -942 THEN
|
|
1441
|
+
RAISE;
|
|
1442
|
+
END IF;
|
|
1443
|
+
END;
|
|
1444
|
+
"""
|
|
1445
|
+
|
|
1446
|
+
def _drop_user_states_table_sql(self) -> str:
|
|
1447
|
+
return f"""
|
|
1448
|
+
BEGIN
|
|
1449
|
+
EXECUTE IMMEDIATE 'DROP TABLE {self._user_state_table}';
|
|
1450
|
+
EXCEPTION
|
|
1451
|
+
WHEN OTHERS THEN
|
|
1452
|
+
IF SQLCODE != -942 THEN
|
|
1453
|
+
RAISE;
|
|
1454
|
+
END IF;
|
|
1455
|
+
END;
|
|
1456
|
+
"""
|
|
1457
|
+
|
|
1458
|
+
def _drop_metadata_table_sql(self) -> str:
|
|
1459
|
+
return f"""
|
|
1460
|
+
BEGIN
|
|
1461
|
+
EXECUTE IMMEDIATE 'DROP TABLE {self._metadata_table}';
|
|
1462
|
+
EXCEPTION
|
|
1463
|
+
WHEN OTHERS THEN
|
|
1464
|
+
IF SQLCODE != -942 THEN
|
|
1465
|
+
RAISE;
|
|
1466
|
+
END IF;
|
|
1467
|
+
END;
|
|
1468
|
+
"""
|
|
1469
|
+
|
|
1470
|
+
def _drop_tables_sql(self) -> "list[str]":
|
|
1471
|
+
"""Get Oracle DROP TABLE SQL statements.
|
|
1472
|
+
|
|
1473
|
+
Returns:
|
|
1474
|
+
List of SQL statements to drop tables and indexes.
|
|
1475
|
+
|
|
1476
|
+
Notes:
|
|
1477
|
+
Order matters: drop events table (child) before sessions (parent).
|
|
1478
|
+
Oracle automatically drops indexes when dropping tables.
|
|
1479
|
+
"""
|
|
1480
|
+
return [
|
|
1481
|
+
self._drop_metadata_table_sql(),
|
|
1482
|
+
self._drop_user_states_table_sql(),
|
|
1483
|
+
self._drop_app_states_table_sql(),
|
|
1484
|
+
f"""
|
|
1485
|
+
BEGIN
|
|
1486
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._events_table}_session';
|
|
1487
|
+
EXCEPTION
|
|
1488
|
+
WHEN OTHERS THEN
|
|
1489
|
+
IF SQLCODE != -1418 THEN
|
|
1490
|
+
RAISE;
|
|
1491
|
+
END IF;
|
|
1492
|
+
END;
|
|
1493
|
+
""",
|
|
1494
|
+
f"""
|
|
1495
|
+
BEGIN
|
|
1496
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._session_table}_update_time';
|
|
1497
|
+
EXCEPTION
|
|
1498
|
+
WHEN OTHERS THEN
|
|
1499
|
+
IF SQLCODE != -1418 THEN
|
|
1500
|
+
RAISE;
|
|
1501
|
+
END IF;
|
|
1502
|
+
END;
|
|
1503
|
+
""",
|
|
1504
|
+
f"""
|
|
1505
|
+
BEGIN
|
|
1506
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._session_table}_app_user';
|
|
1507
|
+
EXCEPTION
|
|
1508
|
+
WHEN OTHERS THEN
|
|
1509
|
+
IF SQLCODE != -1418 THEN
|
|
1510
|
+
RAISE;
|
|
1511
|
+
END IF;
|
|
1512
|
+
END;
|
|
1513
|
+
""",
|
|
1514
|
+
f"""
|
|
1515
|
+
BEGIN
|
|
1516
|
+
EXECUTE IMMEDIATE 'DROP TABLE {self._events_table}';
|
|
1517
|
+
EXCEPTION
|
|
1518
|
+
WHEN OTHERS THEN
|
|
1519
|
+
IF SQLCODE != -942 THEN
|
|
1520
|
+
RAISE;
|
|
1521
|
+
END IF;
|
|
1522
|
+
END;
|
|
1523
|
+
""",
|
|
1524
|
+
f"""
|
|
1525
|
+
BEGIN
|
|
1526
|
+
EXECUTE IMMEDIATE 'DROP TABLE {self._session_table}';
|
|
1527
|
+
EXCEPTION
|
|
1528
|
+
WHEN OTHERS THEN
|
|
1529
|
+
IF SQLCODE != -942 THEN
|
|
1530
|
+
RAISE;
|
|
1531
|
+
END IF;
|
|
1532
|
+
END;
|
|
1533
|
+
""",
|
|
1534
|
+
]
|
|
1535
|
+
|
|
1536
|
+
|
|
1537
|
+
class OracleSyncADKStore(BaseSyncADKStore["OracleSyncConfig"]):
|
|
1538
|
+
"""Oracle synchronous ADK store using oracledb sync driver.
|
|
1539
|
+
|
|
1540
|
+
Implements session and event storage for Google Agent Development Kit
|
|
1541
|
+
using Oracle Database via the python-oracledb synchronous driver. Provides:
|
|
1542
|
+
- Session state management with version-specific JSON storage
|
|
1543
|
+
- Full-fidelity event storage via ``event_data`` column
|
|
1544
|
+
- Atomic ``create_event_and_update_state`` for durable session mutations
|
|
1545
|
+
- TIMESTAMP WITH TIME ZONE for timezone-aware timestamps
|
|
1546
|
+
- Foreign key constraints with cascade delete
|
|
1547
|
+
- Efficient upserts using MERGE statement
|
|
1548
|
+
|
|
1549
|
+
Args:
|
|
1550
|
+
config: OracleSyncConfig with extension_config["adk"] settings.
|
|
1551
|
+
|
|
1552
|
+
Notes:
|
|
1553
|
+
- JSON storage type detected by capability (native JSON, BLOB IS JSON, or plain LOB)
|
|
1554
|
+
- event_data stored as JSON (21c+) or BLOB (older versions)
|
|
1555
|
+
- TIMESTAMP WITH TIME ZONE for timezone-aware timestamps
|
|
1556
|
+
- Named parameters using :param_name
|
|
1557
|
+
- State merging handled at application level
|
|
1558
|
+
- owner_id_column supports NUMBER, VARCHAR2, RAW for Oracle FK types
|
|
1559
|
+
- Configuration is read from config.extension_config["adk"]
|
|
1560
|
+
"""
|
|
1561
|
+
|
|
1562
|
+
__slots__ = ("_in_memory",)
|
|
1563
|
+
|
|
1564
|
+
def __init__(self, config: "OracleSyncConfig", *, read_config: "OracleSyncConfig | None" = None) -> None:
|
|
1565
|
+
"""Initialize Oracle synchronous ADK store.
|
|
1566
|
+
|
|
1567
|
+
Args:
|
|
1568
|
+
read_config: Optional compatible reader for pure reads.
|
|
1569
|
+
config: OracleSyncConfig instance.
|
|
1570
|
+
|
|
1571
|
+
Notes:
|
|
1572
|
+
Configuration is read from config.extension_config["adk"]:
|
|
1573
|
+
- session_table: Sessions table name (default: "adk_session")
|
|
1574
|
+
- events_table: Events table name (default: "adk_event")
|
|
1575
|
+
- owner_id_column: Optional owner FK column DDL (default: None)
|
|
1576
|
+
- in_memory: Enable INMEMORY PRIORITY HIGH clause (default: False)
|
|
1577
|
+
"""
|
|
1578
|
+
super().__init__(config, read_config=read_config)
|
|
1579
|
+
_configure_oracle_adk_session_tables(self, config)
|
|
1580
|
+
|
|
1581
|
+
adk_config = _adk_config(config)
|
|
1582
|
+
self._in_memory: bool = bool(adk_config.get("in_memory", False))
|
|
1583
|
+
|
|
1584
|
+
def create_tables(self) -> None:
|
|
1585
|
+
"""Create the ADK tables that the data dictionary reports as missing.
|
|
1586
|
+
|
|
1587
|
+
Notes:
|
|
1588
|
+
Detects Oracle version to determine optimal JSON storage type.
|
|
1589
|
+
Consults ``data_dictionary.get_tables`` so existing tables are left
|
|
1590
|
+
untouched instead of relying on an ORA-955 swallow.
|
|
1591
|
+
"""
|
|
1592
|
+
if not self.create_schema_enabled:
|
|
1593
|
+
self.reconcile_schema()
|
|
1594
|
+
return
|
|
1595
|
+
|
|
1596
|
+
storage_type = self._detect_json_storage_type()
|
|
1597
|
+
logger.info("Creating ADK tables with storage type: %s", storage_type)
|
|
1598
|
+
|
|
1599
|
+
with self._config.provide_session() as driver:
|
|
1600
|
+
existing = _existing_table_names(driver.data_dictionary.get_tables(driver))
|
|
1601
|
+
if _bare_table_name(self._session_table) not in existing:
|
|
1602
|
+
driver.execute_script(SQL(self._sessions_table_ddl_for_type(storage_type)))
|
|
1603
|
+
if _bare_table_name(self._events_table) not in existing:
|
|
1604
|
+
driver.execute_script(SQL(self._events_table_ddl_for_type(storage_type)))
|
|
1605
|
+
if _bare_table_name(self._app_state_table) not in existing:
|
|
1606
|
+
driver.execute_script(SQL(self._app_states_table_ddl_for_type(storage_type)))
|
|
1607
|
+
if _bare_table_name(self._user_state_table) not in existing:
|
|
1608
|
+
driver.execute_script(SQL(self._user_states_table_ddl_for_type(storage_type)))
|
|
1609
|
+
if _bare_table_name(self._metadata_table) not in existing:
|
|
1610
|
+
driver.execute_script(SQL(self._metadata_table_ddl()))
|
|
1611
|
+
driver.commit()
|
|
1612
|
+
|
|
1613
|
+
def create_session(
|
|
1614
|
+
self,
|
|
1615
|
+
session_id: str,
|
|
1616
|
+
app_name: str,
|
|
1617
|
+
user_id: str,
|
|
1618
|
+
state: "dict[str, Any]",
|
|
1619
|
+
owner_id: "Any | None" = None,
|
|
1620
|
+
*,
|
|
1621
|
+
app_state_delta: "dict[str, Any] | None" = None,
|
|
1622
|
+
user_state_delta: "dict[str, Any] | None" = None,
|
|
1623
|
+
) -> StoredSession:
|
|
1624
|
+
"""Create a new session and optionally merge scoped state deltas.
|
|
1625
|
+
|
|
1626
|
+
Args:
|
|
1627
|
+
session_id: Unique session identifier.
|
|
1628
|
+
app_name: Application name.
|
|
1629
|
+
user_id: User identifier.
|
|
1630
|
+
state: Initial session-scoped state.
|
|
1631
|
+
owner_id: Optional owner ID value for owner_id_column (if configured).
|
|
1632
|
+
app_state_delta: Optional app-scoped state delta to merge atomically.
|
|
1633
|
+
user_state_delta: Optional user-scoped state delta to merge atomically.
|
|
1634
|
+
|
|
1635
|
+
Returns:
|
|
1636
|
+
Created session record with merged scoped state.
|
|
1637
|
+
"""
|
|
1638
|
+
session_state = dict(state)
|
|
1639
|
+
state_data = self._serialize_state(session_state)
|
|
1640
|
+
|
|
1641
|
+
if self._owner_id_column_name:
|
|
1642
|
+
sql = f"""
|
|
1643
|
+
INSERT INTO {self._session_table} (id, app_name, user_id, state, create_time, update_time, {self._owner_id_column_name})
|
|
1644
|
+
VALUES (:id, :app_name, :user_id, :state, SYSTIMESTAMP, SYSTIMESTAMP, :owner_id)
|
|
1645
|
+
"""
|
|
1646
|
+
params = {
|
|
1647
|
+
"id": session_id,
|
|
1648
|
+
"app_name": app_name,
|
|
1649
|
+
"user_id": user_id,
|
|
1650
|
+
"state": state_data,
|
|
1651
|
+
"owner_id": owner_id,
|
|
1652
|
+
}
|
|
1653
|
+
else:
|
|
1654
|
+
sql = f"""
|
|
1655
|
+
INSERT INTO {self._session_table} (id, app_name, user_id, state, create_time, update_time)
|
|
1656
|
+
VALUES (:id, :app_name, :user_id, :state, SYSTIMESTAMP, SYSTIMESTAMP)
|
|
1657
|
+
"""
|
|
1658
|
+
params = {"id": session_id, "app_name": app_name, "user_id": user_id, "state": state_data}
|
|
1659
|
+
|
|
1660
|
+
with self._config.provide_connection() as conn:
|
|
1661
|
+
try:
|
|
1662
|
+
coerced_params = coerce_large_parameters_sync(conn, params)
|
|
1663
|
+
with conn.cursor() as cursor:
|
|
1664
|
+
cursor.execute(sql, coerced_params)
|
|
1665
|
+
current_app_state = (
|
|
1666
|
+
self._apply_app_state_delta_sync(conn, cursor, app_name, app_state_delta)
|
|
1667
|
+
if app_state_delta
|
|
1668
|
+
else self._select_state_sync(
|
|
1669
|
+
cursor,
|
|
1670
|
+
f"SELECT state FROM {self._app_state_table} WHERE app_name = :app_name",
|
|
1671
|
+
{"app_name": app_name},
|
|
1672
|
+
)
|
|
1673
|
+
)
|
|
1674
|
+
current_user_state = (
|
|
1675
|
+
self._apply_user_state_delta_sync(conn, cursor, app_name, user_id, user_state_delta)
|
|
1676
|
+
if user_state_delta
|
|
1677
|
+
else self._select_state_sync(
|
|
1678
|
+
cursor,
|
|
1679
|
+
f"SELECT state FROM {self._user_state_table} WHERE app_name = :app_name AND user_id = :user_id",
|
|
1680
|
+
{"app_name": app_name, "user_id": user_id},
|
|
1681
|
+
)
|
|
1682
|
+
)
|
|
1683
|
+
conn.commit()
|
|
1684
|
+
except BaseException:
|
|
1685
|
+
_rollback_sync(conn)
|
|
1686
|
+
raise
|
|
1687
|
+
|
|
1688
|
+
with adk_primary_read_scope():
|
|
1689
|
+
result = self.get_session(app_name, user_id, session_id)
|
|
1690
|
+
if result is None:
|
|
1691
|
+
msg = "Failed to fetch created session"
|
|
1692
|
+
raise RuntimeError(msg)
|
|
1693
|
+
result["state"] = merge_scoped_state(result["state"], current_app_state, current_user_state)
|
|
1694
|
+
return result
|
|
1695
|
+
|
|
1696
|
+
def get_session(
|
|
1697
|
+
self, app_name: str, user_id: str, session_id: str, *, renew_for: "int | timedelta | None" = None
|
|
1698
|
+
) -> "StoredSession | None":
|
|
1699
|
+
"""Get session by ID.
|
|
1700
|
+
|
|
1701
|
+
Args:
|
|
1702
|
+
app_name: Application name.
|
|
1703
|
+
user_id: User identifier.
|
|
1704
|
+
session_id: Session identifier.
|
|
1705
|
+
renew_for: If positive, touch update_time while reading.
|
|
1706
|
+
|
|
1707
|
+
Returns:
|
|
1708
|
+
Session record or None if not found.
|
|
1709
|
+
|
|
1710
|
+
Notes:
|
|
1711
|
+
Oracle returns datetime objects for TIMESTAMP columns.
|
|
1712
|
+
State is deserialized using version-appropriate format.
|
|
1713
|
+
"""
|
|
1714
|
+
|
|
1715
|
+
sql = f"""
|
|
1716
|
+
SELECT id, app_name, user_id, state, create_time, update_time
|
|
1717
|
+
FROM {self._session_table}
|
|
1718
|
+
WHERE app_name = :app_name AND user_id = :user_id AND id = :id
|
|
1719
|
+
"""
|
|
1720
|
+
|
|
1721
|
+
should_renew = self._is_renewal_requested(renew_for)
|
|
1722
|
+
try:
|
|
1723
|
+
with self._provide_replica_connection(primary=renew_for is not None) as conn, conn.cursor() as cursor:
|
|
1724
|
+
if should_renew:
|
|
1725
|
+
cursor.execute(
|
|
1726
|
+
f"UPDATE {self._session_table} SET update_time = SYSTIMESTAMP WHERE app_name = :app_name AND user_id = :user_id AND id = :id",
|
|
1727
|
+
{"app_name": app_name, "user_id": user_id, "id": session_id},
|
|
1728
|
+
)
|
|
1729
|
+
conn.commit()
|
|
1730
|
+
|
|
1731
|
+
cursor.execute(sql, {"app_name": app_name, "user_id": user_id, "id": session_id})
|
|
1732
|
+
row = cursor.fetchone()
|
|
1733
|
+
|
|
1734
|
+
if row is None:
|
|
1735
|
+
return None
|
|
1736
|
+
|
|
1737
|
+
session_id_val, app_name, user_id, state_data, create_time, update_time = row
|
|
1738
|
+
|
|
1739
|
+
state = self._deserialize_state(state_data)
|
|
1740
|
+
|
|
1741
|
+
return StoredSession(
|
|
1742
|
+
id=session_id_val,
|
|
1743
|
+
app_name=app_name,
|
|
1744
|
+
user_id=user_id,
|
|
1745
|
+
state=state,
|
|
1746
|
+
create_time=create_time,
|
|
1747
|
+
update_time=update_time,
|
|
1748
|
+
)
|
|
1749
|
+
except OracleDatabaseError as e:
|
|
1750
|
+
error_obj = e.args[0] if e.args else None
|
|
1751
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
1752
|
+
return None
|
|
1753
|
+
raise
|
|
1754
|
+
|
|
1755
|
+
def update_session_state(self, app_name: str, user_id: str, session_id: str, state: "dict[str, Any]") -> None:
|
|
1756
|
+
"""Update session state.
|
|
1757
|
+
|
|
1758
|
+
Args:
|
|
1759
|
+
app_name: Application name.
|
|
1760
|
+
user_id: User identifier.
|
|
1761
|
+
session_id: Session identifier.
|
|
1762
|
+
state: New state dictionary (replaces existing state).
|
|
1763
|
+
|
|
1764
|
+
Notes:
|
|
1765
|
+
This replaces the entire state dictionary.
|
|
1766
|
+
Updates update_time to current timestamp.
|
|
1767
|
+
State is serialized using version-appropriate format.
|
|
1768
|
+
"""
|
|
1769
|
+
state_data = self._serialize_state(state)
|
|
1770
|
+
|
|
1771
|
+
sql = f"""
|
|
1772
|
+
UPDATE {self._session_table}
|
|
1773
|
+
SET state = :state, update_time = SYSTIMESTAMP
|
|
1774
|
+
WHERE app_name = :app_name AND user_id = :user_id AND id = :id
|
|
1775
|
+
"""
|
|
1776
|
+
|
|
1777
|
+
with self._config.provide_connection() as conn, conn.cursor() as cursor:
|
|
1778
|
+
params = coerce_large_parameters_sync(
|
|
1779
|
+
conn, {"state": state_data, "app_name": app_name, "user_id": user_id, "id": session_id}
|
|
1780
|
+
)
|
|
1781
|
+
cursor.execute(sql, params)
|
|
1782
|
+
conn.commit()
|
|
1783
|
+
|
|
1784
|
+
def list_sessions(
|
|
1785
|
+
self,
|
|
1786
|
+
app_name: str,
|
|
1787
|
+
user_id: "str | None" = None,
|
|
1788
|
+
*,
|
|
1789
|
+
order_by: "SessionOrderBy" = "update_time",
|
|
1790
|
+
descending: bool = True,
|
|
1791
|
+
limit: "int | None" = None,
|
|
1792
|
+
offset: "int | None" = None,
|
|
1793
|
+
) -> "list[StoredSession]":
|
|
1794
|
+
"""List sessions for an app, optionally filtered by user.
|
|
1795
|
+
|
|
1796
|
+
Args:
|
|
1797
|
+
app_name: Application name.
|
|
1798
|
+
user_id: User identifier. If None, lists all sessions for the app.
|
|
1799
|
+
order_by: Timestamp column to sort on.
|
|
1800
|
+
descending: Sort direction for the timestamp column and the id tie-break.
|
|
1801
|
+
limit: Maximum number of sessions to return.
|
|
1802
|
+
offset: Number of leading rows to skip.
|
|
1803
|
+
|
|
1804
|
+
Returns:
|
|
1805
|
+
List of session records.
|
|
1806
|
+
|
|
1807
|
+
Notes:
|
|
1808
|
+
Uses composite index on (app_name, user_id) when user_id is provided.
|
|
1809
|
+
State is deserialized using version-appropriate format.
|
|
1810
|
+
"""
|
|
1811
|
+
column, direction, page_limit, page_offset = normalize_session_list_options(order_by, descending, limit, offset)
|
|
1812
|
+
if page_limit == 0:
|
|
1813
|
+
return []
|
|
1814
|
+
|
|
1815
|
+
sql, params = _session_list_query(
|
|
1816
|
+
self._session_table, app_name, user_id, column, direction, page_limit, page_offset
|
|
1817
|
+
)
|
|
1818
|
+
|
|
1819
|
+
try:
|
|
1820
|
+
with self._provide_replica_connection(primary=False) as conn, conn.cursor() as cursor:
|
|
1821
|
+
cursor.execute(sql, params)
|
|
1822
|
+
rows = cursor.fetchall()
|
|
1823
|
+
|
|
1824
|
+
results = []
|
|
1825
|
+
for row in rows:
|
|
1826
|
+
state = self._deserialize_state(row[3])
|
|
1827
|
+
|
|
1828
|
+
results.append(
|
|
1829
|
+
StoredSession(
|
|
1830
|
+
id=row[0],
|
|
1831
|
+
app_name=row[1],
|
|
1832
|
+
user_id=row[2],
|
|
1833
|
+
state=state,
|
|
1834
|
+
create_time=row[4],
|
|
1835
|
+
update_time=row[5],
|
|
1836
|
+
)
|
|
1837
|
+
)
|
|
1838
|
+
return results
|
|
1839
|
+
except OracleDatabaseError as e:
|
|
1840
|
+
error_obj = e.args[0] if e.args else None
|
|
1841
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
1842
|
+
return []
|
|
1843
|
+
raise
|
|
1844
|
+
|
|
1845
|
+
def delete_session(self, app_name: str, user_id: str, session_id: str) -> None:
|
|
1846
|
+
"""Delete session and all associated events.
|
|
1847
|
+
|
|
1848
|
+
Args:
|
|
1849
|
+
app_name: Application name.
|
|
1850
|
+
user_id: User identifier.
|
|
1851
|
+
session_id: Session identifier.
|
|
1852
|
+
"""
|
|
1853
|
+
events_sql = (
|
|
1854
|
+
f"DELETE FROM {self._events_table} WHERE app_name = :app_name AND user_id = :user_id AND session_id = :id"
|
|
1855
|
+
)
|
|
1856
|
+
session_sql = (
|
|
1857
|
+
f"DELETE FROM {self._session_table} WHERE app_name = :app_name AND user_id = :user_id AND id = :id"
|
|
1858
|
+
)
|
|
1859
|
+
params = {"app_name": app_name, "user_id": user_id, "id": session_id}
|
|
1860
|
+
|
|
1861
|
+
with self._config.provide_connection() as conn:
|
|
1862
|
+
try:
|
|
1863
|
+
with conn.cursor() as cursor:
|
|
1864
|
+
cursor.execute(events_sql, params)
|
|
1865
|
+
cursor.execute(session_sql, params)
|
|
1866
|
+
conn.commit()
|
|
1867
|
+
except BaseException:
|
|
1868
|
+
_rollback_sync(conn)
|
|
1869
|
+
raise
|
|
1870
|
+
|
|
1871
|
+
def append_event(self, event_record: StoredEvent) -> None:
|
|
1872
|
+
"""Append an event to a session after verifying session ownership."""
|
|
1873
|
+
app_name = event_record["app_name"]
|
|
1874
|
+
user_id = event_record["user_id"]
|
|
1875
|
+
session_id = event_record["session_id"]
|
|
1876
|
+
ownership_sql = (
|
|
1877
|
+
f"SELECT 1 FROM {self._session_table} "
|
|
1878
|
+
"WHERE id = :id AND app_name = :app_name AND user_id = :user_id "
|
|
1879
|
+
"FETCH FIRST 1 ROWS ONLY"
|
|
1880
|
+
)
|
|
1881
|
+
sql = f"""
|
|
1882
|
+
INSERT INTO {self._events_table} (
|
|
1883
|
+
id, app_name, user_id, session_id, invocation_id, timestamp, event_data
|
|
1884
|
+
) VALUES (
|
|
1885
|
+
:id, :app_name, :user_id, :session_id, :invocation_id, :timestamp, :event_data
|
|
1886
|
+
)
|
|
1887
|
+
"""
|
|
1888
|
+
|
|
1889
|
+
with self._config.provide_connection() as conn:
|
|
1890
|
+
try:
|
|
1891
|
+
with conn.cursor() as cursor:
|
|
1892
|
+
cursor.execute(ownership_sql, {"id": session_id, "app_name": app_name, "user_id": user_id})
|
|
1893
|
+
if cursor.fetchone() is None:
|
|
1894
|
+
_raise_session_not_found_for_user(session_id, app_name, user_id)
|
|
1895
|
+
params = coerce_large_parameters_sync(
|
|
1896
|
+
conn,
|
|
1897
|
+
{
|
|
1898
|
+
"id": event_record["id"],
|
|
1899
|
+
"app_name": app_name,
|
|
1900
|
+
"user_id": user_id,
|
|
1901
|
+
"session_id": session_id,
|
|
1902
|
+
"invocation_id": event_record["invocation_id"],
|
|
1903
|
+
"timestamp": event_record["timestamp"],
|
|
1904
|
+
"event_data": self._serialize_event_data(event_record["event_data"]),
|
|
1905
|
+
},
|
|
1906
|
+
)
|
|
1907
|
+
cursor.execute(sql, params)
|
|
1908
|
+
conn.commit()
|
|
1909
|
+
except BaseException:
|
|
1910
|
+
_rollback_sync(conn)
|
|
1911
|
+
raise
|
|
1912
|
+
|
|
1913
|
+
def append_event_and_update_state(
|
|
1914
|
+
self,
|
|
1915
|
+
event_record: StoredEvent,
|
|
1916
|
+
app_name: str,
|
|
1917
|
+
user_id: str,
|
|
1918
|
+
session_id: str,
|
|
1919
|
+
state: "dict[str, Any]",
|
|
1920
|
+
*,
|
|
1921
|
+
app_state_delta: "dict[str, Any] | None" = None,
|
|
1922
|
+
user_state_delta: "dict[str, Any] | None" = None,
|
|
1923
|
+
) -> StoredSession:
|
|
1924
|
+
"""Atomically append an event and update session + scoped state."""
|
|
1925
|
+
_validate_event_belongs_to_session(event_record, app_name, user_id, session_id)
|
|
1926
|
+
insert_sql = f"""
|
|
1927
|
+
INSERT INTO {self._events_table} (
|
|
1928
|
+
id, app_name, user_id, session_id, invocation_id, timestamp, event_data
|
|
1929
|
+
) VALUES (
|
|
1930
|
+
:id, :app_name, :user_id, :session_id, :invocation_id, :timestamp, :event_data
|
|
1931
|
+
)
|
|
1932
|
+
"""
|
|
1933
|
+
|
|
1934
|
+
session_state = dict(state)
|
|
1935
|
+
state_data = self._serialize_state(session_state)
|
|
1936
|
+
update_sql = f"""
|
|
1937
|
+
UPDATE {self._session_table}
|
|
1938
|
+
SET state = :state, update_time = SYSTIMESTAMP
|
|
1939
|
+
WHERE app_name = :app_name AND user_id = :user_id AND id = :id
|
|
1940
|
+
"""
|
|
1941
|
+
|
|
1942
|
+
select_sql = f"""
|
|
1943
|
+
SELECT id, app_name, user_id, state, create_time, update_time
|
|
1944
|
+
FROM {self._session_table}
|
|
1945
|
+
WHERE app_name = :app_name AND user_id = :user_id AND id = :id
|
|
1946
|
+
"""
|
|
1947
|
+
|
|
1948
|
+
merged_app_state: dict[str, Any] | None = None
|
|
1949
|
+
merged_user_state: dict[str, Any] | None = None
|
|
1950
|
+
with self._config.provide_connection() as conn, conn.cursor() as cursor:
|
|
1951
|
+
try:
|
|
1952
|
+
update_params = coerce_large_parameters_sync(
|
|
1953
|
+
conn, {"state": state_data, "app_name": app_name, "user_id": user_id, "id": session_id}
|
|
1954
|
+
)
|
|
1955
|
+
cursor.execute(update_sql, update_params)
|
|
1956
|
+
cursor.execute(select_sql, {"app_name": app_name, "user_id": user_id, "id": session_id})
|
|
1957
|
+
row = cursor.fetchone()
|
|
1958
|
+
if row is None:
|
|
1959
|
+
_raise_session_not_found(session_id)
|
|
1960
|
+
insert_params = coerce_large_parameters_sync(
|
|
1961
|
+
conn,
|
|
1962
|
+
{
|
|
1963
|
+
"id": event_record["id"],
|
|
1964
|
+
"app_name": event_record["app_name"],
|
|
1965
|
+
"user_id": event_record["user_id"],
|
|
1966
|
+
"session_id": event_record["session_id"],
|
|
1967
|
+
"invocation_id": event_record["invocation_id"],
|
|
1968
|
+
"timestamp": event_record["timestamp"],
|
|
1969
|
+
"event_data": self._serialize_event_data(event_record["event_data"]),
|
|
1970
|
+
},
|
|
1971
|
+
)
|
|
1972
|
+
cursor.execute(insert_sql, insert_params)
|
|
1973
|
+
if app_state_delta:
|
|
1974
|
+
merged_app_state = self._apply_app_state_delta_sync(conn, cursor, app_name, app_state_delta)
|
|
1975
|
+
if user_state_delta:
|
|
1976
|
+
merged_user_state = self._apply_user_state_delta_sync(
|
|
1977
|
+
conn, cursor, app_name, user_id, user_state_delta
|
|
1978
|
+
)
|
|
1979
|
+
conn.commit()
|
|
1980
|
+
except Exception:
|
|
1981
|
+
conn.rollback()
|
|
1982
|
+
raise
|
|
1983
|
+
|
|
1984
|
+
session_id_val, row_app_name, row_user_id, state_data_row, create_time, update_time = row
|
|
1985
|
+
stored_session_state = self._deserialize_state(state_data_row)
|
|
1986
|
+
return StoredSession(
|
|
1987
|
+
id=session_id_val,
|
|
1988
|
+
app_name=row_app_name,
|
|
1989
|
+
user_id=row_user_id,
|
|
1990
|
+
state=merge_scoped_state(stored_session_state, merged_app_state, merged_user_state),
|
|
1991
|
+
create_time=create_time,
|
|
1992
|
+
update_time=update_time,
|
|
1993
|
+
)
|
|
1994
|
+
|
|
1995
|
+
def get_events(
|
|
1996
|
+
self,
|
|
1997
|
+
app_name: str,
|
|
1998
|
+
user_id: str,
|
|
1999
|
+
session_id: str,
|
|
2000
|
+
after_timestamp: "datetime | None" = None,
|
|
2001
|
+
limit: "int | None" = None,
|
|
2002
|
+
) -> "list[StoredEvent]":
|
|
2003
|
+
"""List events for a session ordered by timestamp.
|
|
2004
|
+
|
|
2005
|
+
Args:
|
|
2006
|
+
app_name: Application name.
|
|
2007
|
+
user_id: User identifier.
|
|
2008
|
+
session_id: Session identifier.
|
|
2009
|
+
after_timestamp: Only return events after this time.
|
|
2010
|
+
limit: Maximum number of events to return.
|
|
2011
|
+
|
|
2012
|
+
Returns:
|
|
2013
|
+
List of event records ordered by timestamp ASC.
|
|
2014
|
+
"""
|
|
2015
|
+
if limit is not None and (isinstance(limit, bool) or limit < 0):
|
|
2016
|
+
msg = "limit must be non-negative"
|
|
2017
|
+
raise ValueError(msg)
|
|
2018
|
+
if limit == 0:
|
|
2019
|
+
return []
|
|
2020
|
+
|
|
2021
|
+
where_clauses = ["s.app_name = :app_name", "s.user_id = :user_id", "s.id = :session_id"]
|
|
2022
|
+
params: dict[str, Any] = {"app_name": app_name, "user_id": user_id, "session_id": session_id}
|
|
2023
|
+
|
|
2024
|
+
if after_timestamp is not None:
|
|
2025
|
+
where_clauses.append("e.timestamp > :after_timestamp")
|
|
2026
|
+
params["after_timestamp"] = after_timestamp
|
|
2027
|
+
|
|
2028
|
+
where_clause = " AND ".join(where_clauses)
|
|
2029
|
+
if limit is not None:
|
|
2030
|
+
order_and_limit = f"ORDER BY e.timestamp DESC, e.id DESC FETCH FIRST {int(limit)} ROWS ONLY"
|
|
2031
|
+
else:
|
|
2032
|
+
order_and_limit = "ORDER BY e.timestamp ASC, e.id ASC"
|
|
2033
|
+
sql = f"""
|
|
2034
|
+
SELECT e.id, e.session_id, e.invocation_id, e.timestamp, e.event_data, s.app_name, s.user_id
|
|
2035
|
+
FROM {self._events_table} e
|
|
2036
|
+
JOIN {self._session_table} s ON e.session_id = s.id AND e.app_name = s.app_name AND e.user_id = s.user_id
|
|
2037
|
+
WHERE {where_clause}
|
|
2038
|
+
{order_and_limit}
|
|
2039
|
+
"""
|
|
2040
|
+
|
|
2041
|
+
try:
|
|
2042
|
+
with self._provide_replica_connection(primary=False) as conn, conn.cursor() as cursor:
|
|
2043
|
+
cursor.execute(sql, params)
|
|
2044
|
+
rows = cursor.fetchall()
|
|
2045
|
+
if limit is not None:
|
|
2046
|
+
rows = list(reversed(rows))
|
|
2047
|
+
|
|
2048
|
+
return [
|
|
2049
|
+
StoredEvent(
|
|
2050
|
+
id=row[0],
|
|
2051
|
+
session_id=row[1],
|
|
2052
|
+
invocation_id=_oracle_text_value(row[2]),
|
|
2053
|
+
timestamp=row[3],
|
|
2054
|
+
event_data=self._deserialize_json_field(row[4]) or {},
|
|
2055
|
+
app_name=row[5],
|
|
2056
|
+
user_id=row[6],
|
|
2057
|
+
)
|
|
2058
|
+
for row in rows
|
|
2059
|
+
]
|
|
2060
|
+
except OracleDatabaseError as e:
|
|
2061
|
+
error_obj = e.args[0] if e.args else None
|
|
2062
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
2063
|
+
return []
|
|
2064
|
+
raise
|
|
2065
|
+
|
|
2066
|
+
def delete_expired_events(self, before: "datetime", app_name: "str | None" = None) -> int:
|
|
2067
|
+
"""Delete events older than the given timestamp, optionally scoped to one application."""
|
|
2068
|
+
sql = f"DELETE FROM {self._events_table} WHERE timestamp < :before"
|
|
2069
|
+
params: dict[str, Any] = {"before": before}
|
|
2070
|
+
if app_name is not None:
|
|
2071
|
+
sql += " AND app_name = :app_name"
|
|
2072
|
+
params["app_name"] = app_name
|
|
2073
|
+
|
|
2074
|
+
try:
|
|
2075
|
+
with self._config.provide_connection() as conn, conn.cursor() as cursor:
|
|
2076
|
+
cursor.execute(sql, params)
|
|
2077
|
+
conn.commit()
|
|
2078
|
+
return cursor.rowcount if cursor.rowcount and cursor.rowcount > 0 else 0
|
|
2079
|
+
except OracleDatabaseError as e:
|
|
2080
|
+
error_obj = e.args[0] if e.args else None
|
|
2081
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
2082
|
+
return 0
|
|
2083
|
+
raise
|
|
2084
|
+
|
|
2085
|
+
def delete_idle_sessions(self, updated_before: "datetime", app_name: "str | None" = None) -> int:
|
|
2086
|
+
"""Delete sessions whose update_time predates the given threshold, optionally scoped to one application."""
|
|
2087
|
+
sql = f"DELETE FROM {self._session_table} WHERE update_time < :updated_before"
|
|
2088
|
+
params: dict[str, Any] = {"updated_before": updated_before}
|
|
2089
|
+
if app_name is not None:
|
|
2090
|
+
sql += " AND app_name = :app_name"
|
|
2091
|
+
params["app_name"] = app_name
|
|
2092
|
+
|
|
2093
|
+
try:
|
|
2094
|
+
with self._config.provide_connection() as conn, conn.cursor() as cursor:
|
|
2095
|
+
cursor.execute(sql, params)
|
|
2096
|
+
conn.commit()
|
|
2097
|
+
return cursor.rowcount if cursor.rowcount and cursor.rowcount > 0 else 0
|
|
2098
|
+
except OracleDatabaseError as e:
|
|
2099
|
+
error_obj = e.args[0] if e.args else None
|
|
2100
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
2101
|
+
return 0
|
|
2102
|
+
raise
|
|
2103
|
+
|
|
2104
|
+
def delete_idle_user_states(self, updated_before: "datetime", app_name: "str | None" = None) -> int:
|
|
2105
|
+
"""Delete user-scoped state rows whose update_time predates the given threshold, optionally scoped to one application."""
|
|
2106
|
+
sql = f"DELETE FROM {self._user_state_table} WHERE update_time < :updated_before"
|
|
2107
|
+
params: dict[str, Any] = {"updated_before": updated_before}
|
|
2108
|
+
if app_name is not None:
|
|
2109
|
+
sql += " AND app_name = :app_name"
|
|
2110
|
+
params["app_name"] = app_name
|
|
2111
|
+
|
|
2112
|
+
try:
|
|
2113
|
+
with self._config.provide_connection() as conn, conn.cursor() as cursor:
|
|
2114
|
+
cursor.execute(sql, params)
|
|
2115
|
+
conn.commit()
|
|
2116
|
+
return cursor.rowcount if cursor.rowcount and cursor.rowcount > 0 else 0
|
|
2117
|
+
except OracleDatabaseError as e:
|
|
2118
|
+
error_obj = e.args[0] if e.args else None
|
|
2119
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
2120
|
+
return 0
|
|
2121
|
+
raise
|
|
2122
|
+
|
|
2123
|
+
def get_app_state(self, app_name: str) -> "dict[str, Any] | None":
|
|
2124
|
+
"""Return app-scoped state for an application."""
|
|
2125
|
+
sql = f"SELECT state FROM {self._app_state_table} WHERE app_name = :app_name"
|
|
2126
|
+
|
|
2127
|
+
try:
|
|
2128
|
+
with self._config.provide_connection() as conn, conn.cursor() as cursor:
|
|
2129
|
+
cursor.execute(sql, {"app_name": app_name})
|
|
2130
|
+
row = cursor.fetchone()
|
|
2131
|
+
return self._deserialize_state(row[0]) if row is not None else None
|
|
2132
|
+
except OracleDatabaseError as e:
|
|
2133
|
+
error_obj = e.args[0] if e.args else None
|
|
2134
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
2135
|
+
return None
|
|
2136
|
+
raise
|
|
2137
|
+
|
|
2138
|
+
def get_user_state(self, app_name: str, user_id: str) -> "dict[str, Any] | None":
|
|
2139
|
+
"""Return user-scoped state for an application user."""
|
|
2140
|
+
sql = f"""
|
|
2141
|
+
SELECT state
|
|
2142
|
+
FROM {self._user_state_table}
|
|
2143
|
+
WHERE app_name = :app_name AND user_id = :user_id
|
|
2144
|
+
"""
|
|
2145
|
+
|
|
2146
|
+
try:
|
|
2147
|
+
with self._config.provide_connection() as conn, conn.cursor() as cursor:
|
|
2148
|
+
cursor.execute(sql, {"app_name": app_name, "user_id": user_id})
|
|
2149
|
+
row = cursor.fetchone()
|
|
2150
|
+
return self._deserialize_state(row[0]) if row is not None else None
|
|
2151
|
+
except OracleDatabaseError as e:
|
|
2152
|
+
error_obj = e.args[0] if e.args else None
|
|
2153
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
2154
|
+
return None
|
|
2155
|
+
raise
|
|
2156
|
+
|
|
2157
|
+
def upsert_app_state(self, app_name: str, state: "dict[str, Any]") -> None:
|
|
2158
|
+
"""Insert or replace app-scoped state for an application."""
|
|
2159
|
+
sql = f"""
|
|
2160
|
+
MERGE INTO {self._app_state_table} target
|
|
2161
|
+
USING (SELECT :app_name AS app_name FROM DUAL) source
|
|
2162
|
+
ON (target.app_name = source.app_name)
|
|
2163
|
+
WHEN MATCHED THEN
|
|
2164
|
+
UPDATE SET target.state = :state, target.update_time = SYSTIMESTAMP
|
|
2165
|
+
WHEN NOT MATCHED THEN
|
|
2166
|
+
INSERT (app_name, state, update_time)
|
|
2167
|
+
VALUES (source.app_name, :state, SYSTIMESTAMP)
|
|
2168
|
+
"""
|
|
2169
|
+
|
|
2170
|
+
with self._config.provide_connection() as conn, conn.cursor() as cursor:
|
|
2171
|
+
params = coerce_large_parameters_sync(conn, {"app_name": app_name, "state": self._serialize_state(state)})
|
|
2172
|
+
cursor.execute(sql, params)
|
|
2173
|
+
conn.commit()
|
|
2174
|
+
|
|
2175
|
+
def upsert_user_state(self, app_name: str, user_id: str, state: "dict[str, Any]") -> None:
|
|
2176
|
+
"""Insert or replace user-scoped state for an application user."""
|
|
2177
|
+
sql = f"""
|
|
2178
|
+
MERGE INTO {self._user_state_table} target
|
|
2179
|
+
USING (SELECT :app_name AS app_name, :user_id AS user_id FROM DUAL) source
|
|
2180
|
+
ON (target.app_name = source.app_name AND target.user_id = source.user_id)
|
|
2181
|
+
WHEN MATCHED THEN
|
|
2182
|
+
UPDATE SET target.state = :state, target.update_time = SYSTIMESTAMP
|
|
2183
|
+
WHEN NOT MATCHED THEN
|
|
2184
|
+
INSERT (app_name, user_id, state, update_time)
|
|
2185
|
+
VALUES (source.app_name, source.user_id, :state, SYSTIMESTAMP)
|
|
2186
|
+
"""
|
|
2187
|
+
|
|
2188
|
+
with self._config.provide_connection() as conn, conn.cursor() as cursor:
|
|
2189
|
+
params = coerce_large_parameters_sync(
|
|
2190
|
+
conn, {"app_name": app_name, "user_id": user_id, "state": self._serialize_state(state)}
|
|
2191
|
+
)
|
|
2192
|
+
cursor.execute(sql, params)
|
|
2193
|
+
conn.commit()
|
|
2194
|
+
|
|
2195
|
+
def get_metadata(self, key: str) -> "str | None":
|
|
2196
|
+
"""Return a value from the ADK internal metadata table."""
|
|
2197
|
+
sql = f"SELECT value FROM {self._metadata_table} WHERE key = :key"
|
|
2198
|
+
|
|
2199
|
+
try:
|
|
2200
|
+
with self._config.provide_connection() as conn, conn.cursor() as cursor:
|
|
2201
|
+
cursor.execute(sql, {"key": key})
|
|
2202
|
+
row = cursor.fetchone()
|
|
2203
|
+
return str(row[0]) if row is not None else None
|
|
2204
|
+
except OracleDatabaseError as e:
|
|
2205
|
+
error_obj = e.args[0] if e.args else None
|
|
2206
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
2207
|
+
return None
|
|
2208
|
+
raise
|
|
2209
|
+
|
|
2210
|
+
def set_metadata(self, key: str, value: str) -> None:
|
|
2211
|
+
"""Set a value in the ADK internal metadata table."""
|
|
2212
|
+
sql = f"""
|
|
2213
|
+
MERGE INTO {self._metadata_table} target
|
|
2214
|
+
USING (SELECT :key AS key, :value AS value FROM DUAL) source
|
|
2215
|
+
ON (target.key = source.key)
|
|
2216
|
+
WHEN MATCHED THEN
|
|
2217
|
+
UPDATE SET target.value = source.value
|
|
2218
|
+
WHEN NOT MATCHED THEN
|
|
2219
|
+
INSERT (key, value)
|
|
2220
|
+
VALUES (source.key, source.value)
|
|
2221
|
+
"""
|
|
2222
|
+
|
|
2223
|
+
with self._config.provide_connection() as conn, conn.cursor() as cursor:
|
|
2224
|
+
cursor.execute(sql, {"key": key, "value": value})
|
|
2225
|
+
conn.commit()
|
|
2226
|
+
|
|
2227
|
+
def prepare_schema_sync(self, driver: Any) -> None:
|
|
2228
|
+
"""Prepare adapter-specific schema decisions with a synchronous driver."""
|
|
2229
|
+
_prepare_oracle_schema_sync(self._config, driver)
|
|
2230
|
+
|
|
2231
|
+
def reconcile_companion_objects_sync(self, driver: Any) -> None:
|
|
2232
|
+
"""Create missing Oracle session and event companion indexes via catalog comparison.
|
|
2233
|
+
|
|
2234
|
+
Args:
|
|
2235
|
+
driver: Synchronous Oracle driver executing schema reconciliation.
|
|
2236
|
+
"""
|
|
2237
|
+
specs = _oracle_session_index_specs(self._session_table) + _oracle_event_index_specs(self._events_table)
|
|
2238
|
+
rows = driver.data_dictionary.get_indexes(driver)
|
|
2239
|
+
for statement in _oracle_missing_index_statements(specs, rows):
|
|
2240
|
+
driver.execute(statement)
|
|
2241
|
+
|
|
2242
|
+
def _select_state_sync(self, cursor: Any, sql: str, params: "dict[str, Any]") -> "dict[str, Any] | None":
|
|
2243
|
+
try:
|
|
2244
|
+
cursor.execute(sql, params)
|
|
2245
|
+
row = cursor.fetchone()
|
|
2246
|
+
except OracleDatabaseError as exc:
|
|
2247
|
+
error_obj = exc.args[0] if exc.args else None
|
|
2248
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
2249
|
+
return None
|
|
2250
|
+
raise
|
|
2251
|
+
return self._deserialize_state(row[0]) if row is not None else None
|
|
2252
|
+
|
|
2253
|
+
def _apply_app_state_delta_sync(
|
|
2254
|
+
self, conn: Any, cursor: Any, app_name: str, delta: "dict[str, Any]"
|
|
2255
|
+
) -> "dict[str, Any]":
|
|
2256
|
+
return self._apply_state_delta_sync(
|
|
2257
|
+
conn,
|
|
2258
|
+
cursor,
|
|
2259
|
+
select_for_update_sql=f"SELECT state FROM {self._app_state_table} WHERE app_name = :app_name FOR UPDATE",
|
|
2260
|
+
insert_sql=f"INSERT INTO {self._app_state_table} (app_name, state, update_time) VALUES (:app_name, :state, SYSTIMESTAMP)",
|
|
2261
|
+
update_sql=f"UPDATE {self._app_state_table} SET state = :state, update_time = SYSTIMESTAMP WHERE app_name = :app_name",
|
|
2262
|
+
key_params={"app_name": app_name},
|
|
2263
|
+
delta=delta,
|
|
2264
|
+
)
|
|
2265
|
+
|
|
2266
|
+
def _apply_user_state_delta_sync(
|
|
2267
|
+
self, conn: Any, cursor: Any, app_name: str, user_id: str, delta: "dict[str, Any]"
|
|
2268
|
+
) -> "dict[str, Any]":
|
|
2269
|
+
return self._apply_state_delta_sync(
|
|
2270
|
+
conn,
|
|
2271
|
+
cursor,
|
|
2272
|
+
select_for_update_sql=(
|
|
2273
|
+
f"SELECT state FROM {self._user_state_table} WHERE app_name = :app_name AND user_id = :user_id FOR UPDATE"
|
|
2274
|
+
),
|
|
2275
|
+
insert_sql=(
|
|
2276
|
+
f"INSERT INTO {self._user_state_table} (app_name, user_id, state, update_time) "
|
|
2277
|
+
"VALUES (:app_name, :user_id, :state, SYSTIMESTAMP)"
|
|
2278
|
+
),
|
|
2279
|
+
update_sql=(
|
|
2280
|
+
f"UPDATE {self._user_state_table} SET state = :state, update_time = SYSTIMESTAMP "
|
|
2281
|
+
"WHERE app_name = :app_name AND user_id = :user_id"
|
|
2282
|
+
),
|
|
2283
|
+
key_params={"app_name": app_name, "user_id": user_id},
|
|
2284
|
+
delta=delta,
|
|
2285
|
+
)
|
|
2286
|
+
|
|
2287
|
+
def _apply_state_delta_sync(
|
|
2288
|
+
self,
|
|
2289
|
+
conn: Any,
|
|
2290
|
+
cursor: Any,
|
|
2291
|
+
*,
|
|
2292
|
+
select_for_update_sql: str,
|
|
2293
|
+
insert_sql: str,
|
|
2294
|
+
update_sql: str,
|
|
2295
|
+
key_params: "dict[str, Any]",
|
|
2296
|
+
delta: "dict[str, Any]",
|
|
2297
|
+
) -> "dict[str, Any]":
|
|
2298
|
+
for _ in range(_MAX_STATE_MERGE_ATTEMPTS):
|
|
2299
|
+
cursor.execute(select_for_update_sql, key_params)
|
|
2300
|
+
row = cursor.fetchone()
|
|
2301
|
+
if row is None:
|
|
2302
|
+
merged = dict(delta)
|
|
2303
|
+
insert_params = coerce_large_parameters_sync(
|
|
2304
|
+
conn, {**key_params, "state": self._serialize_state(merged)}
|
|
2305
|
+
)
|
|
2306
|
+
try:
|
|
2307
|
+
cursor.execute(insert_sql, insert_params)
|
|
2308
|
+
except OracleDatabaseError as exc:
|
|
2309
|
+
if not _is_unique_violation(exc):
|
|
2310
|
+
raise
|
|
2311
|
+
continue
|
|
2312
|
+
else:
|
|
2313
|
+
return merged
|
|
2314
|
+
existing = self._deserialize_state(row[0])
|
|
2315
|
+
merged = {**existing, **delta}
|
|
2316
|
+
update_params = coerce_large_parameters_sync(conn, {**key_params, "state": self._serialize_state(merged)})
|
|
2317
|
+
cursor.execute(update_sql, update_params)
|
|
2318
|
+
return merged
|
|
2319
|
+
msg = f"Failed to lock and merge Oracle scoped state after {_MAX_STATE_MERGE_ATTEMPTS} attempts."
|
|
2320
|
+
raise SerializationConflictError(msg)
|
|
2321
|
+
|
|
2322
|
+
def _sessions_table_ddl(self) -> str:
|
|
2323
|
+
"""Get Oracle CREATE TABLE SQL for sessions table.
|
|
2324
|
+
|
|
2325
|
+
Auto-detects optimal JSON storage type based on Oracle version.
|
|
2326
|
+
Result is cached to minimize database queries.
|
|
2327
|
+
"""
|
|
2328
|
+
storage_type = self._detect_json_storage_type()
|
|
2329
|
+
return self._sessions_table_ddl_for_type(storage_type)
|
|
2330
|
+
|
|
2331
|
+
def _events_table_ddl(self) -> str:
|
|
2332
|
+
"""Get Oracle CREATE TABLE SQL for events table.
|
|
2333
|
+
|
|
2334
|
+
Auto-detects optimal JSON storage type based on Oracle version.
|
|
2335
|
+
Result is cached to minimize database queries.
|
|
2336
|
+
"""
|
|
2337
|
+
storage_type = self._detect_json_storage_type()
|
|
2338
|
+
return self._events_table_ddl_for_type(storage_type)
|
|
2339
|
+
|
|
2340
|
+
def _app_states_table_ddl(self) -> str:
|
|
2341
|
+
"""Get Oracle CREATE TABLE SQL for app-scoped state."""
|
|
2342
|
+
storage_type = self._detect_json_storage_type()
|
|
2343
|
+
return self._app_states_table_ddl_for_type(storage_type)
|
|
2344
|
+
|
|
2345
|
+
def _user_states_table_ddl(self) -> str:
|
|
2346
|
+
"""Get Oracle CREATE TABLE SQL for user-scoped state."""
|
|
2347
|
+
storage_type = self._detect_json_storage_type()
|
|
2348
|
+
return self._user_states_table_ddl_for_type(storage_type)
|
|
2349
|
+
|
|
2350
|
+
def _metadata_table_ddl(self) -> str:
|
|
2351
|
+
"""Get Oracle CREATE TABLE SQL for ADK internal metadata."""
|
|
2352
|
+
return _ADK_METADATA_TABLE_DDL_TEMPLATE.format(self._metadata_table)
|
|
2353
|
+
|
|
2354
|
+
def _detect_json_storage_type(self) -> JSONStorageType:
|
|
2355
|
+
"""Resolve the JSON storage type from the pool-scoped Oracle version.
|
|
2356
|
+
|
|
2357
|
+
- Oracle 21c+ with compatible >= 20: native JSON type
|
|
2358
|
+
- Oracle 12c+: BLOB with IS JSON constraint
|
|
2359
|
+
- Oracle 11g and earlier: plain BLOB
|
|
2360
|
+
"""
|
|
2361
|
+
return storage_type_from_version(self._get_version_info())
|
|
2362
|
+
|
|
2363
|
+
def _get_version_info(self) -> "OracleVersionInfo | None":
|
|
2364
|
+
"""Return the pool-scoped Oracle version through the data dictionary."""
|
|
2365
|
+
return _get_oracle_version_info_sync(self._config)
|
|
2366
|
+
|
|
2367
|
+
def _serialize_state(self, state: "dict[str, Any]") -> "str | bytes":
|
|
2368
|
+
"""Serialize state dictionary to appropriate format based on storage type.
|
|
2369
|
+
|
|
2370
|
+
Args:
|
|
2371
|
+
state: State dictionary to serialize.
|
|
2372
|
+
|
|
2373
|
+
Returns:
|
|
2374
|
+
JSON string for JSON_NATIVE, bytes for BLOB types.
|
|
2375
|
+
"""
|
|
2376
|
+
storage_type = self._detect_json_storage_type()
|
|
2377
|
+
|
|
2378
|
+
if storage_type == JSONStorageType.JSON_NATIVE:
|
|
2379
|
+
return to_json(state)
|
|
2380
|
+
|
|
2381
|
+
return to_json(state, as_bytes=True)
|
|
2382
|
+
|
|
2383
|
+
def _deserialize_state(self, data: Any) -> "dict[str, Any]":
|
|
2384
|
+
"""Deserialize state data from database format.
|
|
2385
|
+
|
|
2386
|
+
Args:
|
|
2387
|
+
data: Data from database (may be LOB, str, bytes, or dict).
|
|
2388
|
+
|
|
2389
|
+
Returns:
|
|
2390
|
+
Deserialized state dictionary.
|
|
2391
|
+
|
|
2392
|
+
Notes:
|
|
2393
|
+
Handles LOB reading if data has read() method.
|
|
2394
|
+
Oracle JSON type may return dict directly.
|
|
2395
|
+
"""
|
|
2396
|
+
if is_readable(data):
|
|
2397
|
+
data = data.read()
|
|
2398
|
+
|
|
2399
|
+
if isinstance(data, dict):
|
|
2400
|
+
return cast("dict[str, Any]", _coerce_decimal_values(data))
|
|
2401
|
+
|
|
2402
|
+
if isinstance(data, bytes):
|
|
2403
|
+
return cast("dict[str, Any]", from_json(data))
|
|
2404
|
+
|
|
2405
|
+
if isinstance(data, str):
|
|
2406
|
+
return cast("dict[str, Any]", from_json(data))
|
|
2407
|
+
|
|
2408
|
+
return cast("dict[str, Any]", from_json(str(data)))
|
|
2409
|
+
|
|
2410
|
+
def _deserialize_json_field(self, data: Any) -> "dict[str, Any] | None":
|
|
2411
|
+
"""Deserialize JSON payloads from Oracle JSON/BLOB/LOB values."""
|
|
2412
|
+
if data is None:
|
|
2413
|
+
return None
|
|
2414
|
+
return self._deserialize_state(data)
|
|
2415
|
+
|
|
2416
|
+
def _serialize_event_data(self, event_data: Any) -> "str | bytes":
|
|
2417
|
+
"""Serialize event_data to the configured Oracle JSON storage format."""
|
|
2418
|
+
storage_type = self._detect_json_storage_type()
|
|
2419
|
+
event_data = _normalize_event_data_for_storage(event_data)
|
|
2420
|
+
if storage_type == JSONStorageType.JSON_NATIVE:
|
|
2421
|
+
return to_json(event_data)
|
|
2422
|
+
return to_json(event_data, as_bytes=True)
|
|
2423
|
+
|
|
2424
|
+
def _read_event_data(self, data: Any) -> str:
|
|
2425
|
+
"""Read event_data from database, handling LOB types.
|
|
2426
|
+
|
|
2427
|
+
Args:
|
|
2428
|
+
data: Data from database (may be LOB, str, or dict).
|
|
2429
|
+
|
|
2430
|
+
Returns:
|
|
2431
|
+
JSON string.
|
|
2432
|
+
"""
|
|
2433
|
+
if is_readable(data):
|
|
2434
|
+
data = data.read()
|
|
2435
|
+
|
|
2436
|
+
if isinstance(data, dict):
|
|
2437
|
+
return to_json(data)
|
|
2438
|
+
|
|
2439
|
+
if isinstance(data, bytes):
|
|
2440
|
+
return data.decode("utf-8")
|
|
2441
|
+
|
|
2442
|
+
return str(data)
|
|
2443
|
+
|
|
2444
|
+
def _sessions_table_ddl_for_type(self, storage_type: JSONStorageType) -> str:
|
|
2445
|
+
"""Get Oracle CREATE TABLE SQL for sessions with specified storage type.
|
|
2446
|
+
|
|
2447
|
+
Args:
|
|
2448
|
+
storage_type: JSON storage type to use.
|
|
2449
|
+
|
|
2450
|
+
Returns:
|
|
2451
|
+
SQL statement to create adk_session table.
|
|
2452
|
+
"""
|
|
2453
|
+
if storage_type == JSONStorageType.JSON_NATIVE:
|
|
2454
|
+
state_column = "state JSON NOT NULL"
|
|
2455
|
+
elif storage_type == JSONStorageType.BLOB_JSON:
|
|
2456
|
+
state_column = "state BLOB CHECK (state IS JSON) NOT NULL"
|
|
2457
|
+
else:
|
|
2458
|
+
state_column = "state BLOB NOT NULL"
|
|
2459
|
+
|
|
2460
|
+
escaped_owner_id_ddl = self._owner_id_column_ddl.replace("'", "''") if self._owner_id_column_ddl else ""
|
|
2461
|
+
owner_id_column_sql = (
|
|
2462
|
+
_ADK_SESSIONS_TABLE_DDL_FOR_TYPE_TEMPLATE.format(escaped_owner_id_ddl) if escaped_owner_id_ddl else ""
|
|
2463
|
+
)
|
|
2464
|
+
table_clauses = _adk_table_feature_clause(
|
|
2465
|
+
self._config,
|
|
2466
|
+
"session",
|
|
2467
|
+
in_memory=self._in_memory,
|
|
2468
|
+
hash_partition_key="id",
|
|
2469
|
+
range_partition_key="create_time",
|
|
2470
|
+
)
|
|
2471
|
+
|
|
2472
|
+
return _ADK_SESSIONS_TABLE_DDL_FOR_TYPE_TEMPLATE_2.format(
|
|
2473
|
+
self._session_table,
|
|
2474
|
+
state_column,
|
|
2475
|
+
owner_id_column_sql,
|
|
2476
|
+
table_clauses,
|
|
2477
|
+
self._session_table,
|
|
2478
|
+
self._session_table,
|
|
2479
|
+
self._session_table,
|
|
2480
|
+
self._session_table,
|
|
2481
|
+
)
|
|
2482
|
+
|
|
2483
|
+
def _events_table_ddl_for_type(self, storage_type: JSONStorageType) -> str:
|
|
2484
|
+
"""Get Oracle CREATE TABLE SQL for events with specified storage type.
|
|
2485
|
+
|
|
2486
|
+
The events table stores the full ADK Event in ``event_data`` and
|
|
2487
|
+
keeps scalar event fields indexed for efficient scoped reads.
|
|
2488
|
+
|
|
2489
|
+
Args:
|
|
2490
|
+
storage_type: JSON storage type to use.
|
|
2491
|
+
|
|
2492
|
+
Returns:
|
|
2493
|
+
SQL statement to create adk_event table.
|
|
2494
|
+
"""
|
|
2495
|
+
event_data_col = _event_data_column_ddl(storage_type)
|
|
2496
|
+
table_clauses = _adk_table_feature_clause(
|
|
2497
|
+
self._config,
|
|
2498
|
+
"events",
|
|
2499
|
+
in_memory=self._in_memory,
|
|
2500
|
+
hash_partition_key="session_id",
|
|
2501
|
+
range_partition_key="timestamp",
|
|
2502
|
+
)
|
|
2503
|
+
|
|
2504
|
+
return _ADK_EVENTS_TABLE_DDL_FOR_TYPE_TEMPLATE.format(
|
|
2505
|
+
self._events_table,
|
|
2506
|
+
event_data_col,
|
|
2507
|
+
self._events_table,
|
|
2508
|
+
self._session_table,
|
|
2509
|
+
table_clauses,
|
|
2510
|
+
self._events_table,
|
|
2511
|
+
self._events_table,
|
|
2512
|
+
self._events_table,
|
|
2513
|
+
self._events_table,
|
|
2514
|
+
self._events_table,
|
|
2515
|
+
self._events_table,
|
|
2516
|
+
self._events_table,
|
|
2517
|
+
self._events_table,
|
|
2518
|
+
)
|
|
2519
|
+
|
|
2520
|
+
def _app_states_table_ddl_for_type(self, storage_type: JSONStorageType) -> str:
|
|
2521
|
+
"""Get Oracle CREATE TABLE SQL for app-scoped state with specified storage type."""
|
|
2522
|
+
state_column = _json_column_ddl("state", storage_type)
|
|
2523
|
+
table_clauses = _adk_table_feature_clause(
|
|
2524
|
+
self._config,
|
|
2525
|
+
"app_state",
|
|
2526
|
+
in_memory=self._in_memory,
|
|
2527
|
+
hash_partition_key="app_name",
|
|
2528
|
+
range_partition_key="update_time",
|
|
2529
|
+
)
|
|
2530
|
+
|
|
2531
|
+
return _ADK_APP_STATES_TABLE_DDL_FOR_TYPE_TEMPLATE.format(self._app_state_table, state_column, table_clauses)
|
|
2532
|
+
|
|
2533
|
+
def _user_states_table_ddl_for_type(self, storage_type: JSONStorageType) -> str:
|
|
2534
|
+
"""Get Oracle CREATE TABLE SQL for user-scoped state with specified storage type."""
|
|
2535
|
+
state_column = _json_column_ddl("state", storage_type)
|
|
2536
|
+
table_clauses = _adk_table_feature_clause(
|
|
2537
|
+
self._config,
|
|
2538
|
+
"user_state",
|
|
2539
|
+
in_memory=self._in_memory,
|
|
2540
|
+
hash_partition_key="user_id",
|
|
2541
|
+
range_partition_key="update_time",
|
|
2542
|
+
)
|
|
2543
|
+
|
|
2544
|
+
return _ADK_USER_STATES_TABLE_DDL_FOR_TYPE_TEMPLATE.format(self._user_state_table, state_column, table_clauses)
|
|
2545
|
+
|
|
2546
|
+
def _drop_app_states_table_sql(self) -> str:
|
|
2547
|
+
return f"""
|
|
2548
|
+
BEGIN
|
|
2549
|
+
EXECUTE IMMEDIATE 'DROP TABLE {self._app_state_table}';
|
|
2550
|
+
EXCEPTION
|
|
2551
|
+
WHEN OTHERS THEN
|
|
2552
|
+
IF SQLCODE != -942 THEN
|
|
2553
|
+
RAISE;
|
|
2554
|
+
END IF;
|
|
2555
|
+
END;
|
|
2556
|
+
"""
|
|
2557
|
+
|
|
2558
|
+
def _drop_user_states_table_sql(self) -> str:
|
|
2559
|
+
return f"""
|
|
2560
|
+
BEGIN
|
|
2561
|
+
EXECUTE IMMEDIATE 'DROP TABLE {self._user_state_table}';
|
|
2562
|
+
EXCEPTION
|
|
2563
|
+
WHEN OTHERS THEN
|
|
2564
|
+
IF SQLCODE != -942 THEN
|
|
2565
|
+
RAISE;
|
|
2566
|
+
END IF;
|
|
2567
|
+
END;
|
|
2568
|
+
"""
|
|
2569
|
+
|
|
2570
|
+
def _drop_metadata_table_sql(self) -> str:
|
|
2571
|
+
return f"""
|
|
2572
|
+
BEGIN
|
|
2573
|
+
EXECUTE IMMEDIATE 'DROP TABLE {self._metadata_table}';
|
|
2574
|
+
EXCEPTION
|
|
2575
|
+
WHEN OTHERS THEN
|
|
2576
|
+
IF SQLCODE != -942 THEN
|
|
2577
|
+
RAISE;
|
|
2578
|
+
END IF;
|
|
2579
|
+
END;
|
|
2580
|
+
"""
|
|
2581
|
+
|
|
2582
|
+
def _drop_tables_sql(self) -> "list[str]":
|
|
2583
|
+
"""Get Oracle DROP TABLE SQL statements.
|
|
2584
|
+
|
|
2585
|
+
Returns:
|
|
2586
|
+
List of SQL statements to drop tables and indexes.
|
|
2587
|
+
|
|
2588
|
+
Notes:
|
|
2589
|
+
Order matters: drop events table (child) before sessions (parent).
|
|
2590
|
+
Oracle automatically drops indexes when dropping tables.
|
|
2591
|
+
"""
|
|
2592
|
+
return [
|
|
2593
|
+
self._drop_metadata_table_sql(),
|
|
2594
|
+
self._drop_user_states_table_sql(),
|
|
2595
|
+
self._drop_app_states_table_sql(),
|
|
2596
|
+
f"""
|
|
2597
|
+
BEGIN
|
|
2598
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._events_table}_session';
|
|
2599
|
+
EXCEPTION
|
|
2600
|
+
WHEN OTHERS THEN
|
|
2601
|
+
IF SQLCODE != -1418 THEN
|
|
2602
|
+
RAISE;
|
|
2603
|
+
END IF;
|
|
2604
|
+
END;
|
|
2605
|
+
""",
|
|
2606
|
+
f"""
|
|
2607
|
+
BEGIN
|
|
2608
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._session_table}_update_time';
|
|
2609
|
+
EXCEPTION
|
|
2610
|
+
WHEN OTHERS THEN
|
|
2611
|
+
IF SQLCODE != -1418 THEN
|
|
2612
|
+
RAISE;
|
|
2613
|
+
END IF;
|
|
2614
|
+
END;
|
|
2615
|
+
""",
|
|
2616
|
+
f"""
|
|
2617
|
+
BEGIN
|
|
2618
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._session_table}_app_user';
|
|
2619
|
+
EXCEPTION
|
|
2620
|
+
WHEN OTHERS THEN
|
|
2621
|
+
IF SQLCODE != -1418 THEN
|
|
2622
|
+
RAISE;
|
|
2623
|
+
END IF;
|
|
2624
|
+
END;
|
|
2625
|
+
""",
|
|
2626
|
+
f"""
|
|
2627
|
+
BEGIN
|
|
2628
|
+
EXECUTE IMMEDIATE 'DROP TABLE {self._events_table}';
|
|
2629
|
+
EXCEPTION
|
|
2630
|
+
WHEN OTHERS THEN
|
|
2631
|
+
IF SQLCODE != -942 THEN
|
|
2632
|
+
RAISE;
|
|
2633
|
+
END IF;
|
|
2634
|
+
END;
|
|
2635
|
+
""",
|
|
2636
|
+
f"""
|
|
2637
|
+
BEGIN
|
|
2638
|
+
EXECUTE IMMEDIATE 'DROP TABLE {self._session_table}';
|
|
2639
|
+
EXCEPTION
|
|
2640
|
+
WHEN OTHERS THEN
|
|
2641
|
+
IF SQLCODE != -942 THEN
|
|
2642
|
+
RAISE;
|
|
2643
|
+
END IF;
|
|
2644
|
+
END;
|
|
2645
|
+
""",
|
|
2646
|
+
]
|
|
2647
|
+
|
|
2648
|
+
|
|
2649
|
+
class OracleAsyncADKMemoryStore(BaseAsyncADKMemoryStore["OracleAsyncConfig"]):
|
|
2650
|
+
"""Oracle ADK memory store using async oracledb driver."""
|
|
2651
|
+
|
|
2652
|
+
__slots__ = ("_approximate", "_fts_operator", "_in_memory")
|
|
2653
|
+
|
|
2654
|
+
def __init__(self, config: "OracleAsyncConfig", *, read_config: "OracleAsyncConfig | None" = None) -> None:
|
|
2655
|
+
super().__init__(config, read_config=read_config)
|
|
2656
|
+
validate_oracle_identifier(self._memory_table, "table name")
|
|
2657
|
+
adk_config = _adk_config(config)
|
|
2658
|
+
self._in_memory: bool = bool(adk_config.get("in_memory", False))
|
|
2659
|
+
self._fts_operator: str = str(adk_config.get("fts_operator", "ACCUM"))
|
|
2660
|
+
self._approximate: bool = bool(adk_config.get("approximate", True))
|
|
2661
|
+
|
|
2662
|
+
@property
|
|
2663
|
+
def fts_operator(self) -> str:
|
|
2664
|
+
"""Return the default Oracle Text operator for hybrid search."""
|
|
2665
|
+
return self._fts_operator
|
|
2666
|
+
|
|
2667
|
+
@fts_operator.setter
|
|
2668
|
+
def fts_operator(self, value: str) -> None:
|
|
2669
|
+
self._fts_operator = value
|
|
2670
|
+
|
|
2671
|
+
@property
|
|
2672
|
+
def approximate(self) -> bool:
|
|
2673
|
+
"""Return whether approximate vector search is enabled."""
|
|
2674
|
+
return self._approximate
|
|
2675
|
+
|
|
2676
|
+
@approximate.setter
|
|
2677
|
+
def approximate(self, value: bool) -> None:
|
|
2678
|
+
self._approximate = bool(value)
|
|
2679
|
+
|
|
2680
|
+
async def create_tables(self) -> None:
|
|
2681
|
+
if not self.create_schema_enabled:
|
|
2682
|
+
await self.reconcile_schema()
|
|
2683
|
+
return
|
|
2684
|
+
|
|
2685
|
+
if not self._enabled:
|
|
2686
|
+
return
|
|
2687
|
+
|
|
2688
|
+
async with self._config.provide_session() as driver:
|
|
2689
|
+
existing = _existing_table_names(await driver.data_dictionary.get_tables(driver))
|
|
2690
|
+
if _bare_table_name(self._memory_table) not in existing:
|
|
2691
|
+
await driver.execute_script(await self._memory_table_ddl())
|
|
2692
|
+
|
|
2693
|
+
async def insert_memory_entries(self, entries: "list[StoredMemory]", owner_id: "object | None" = None) -> int:
|
|
2694
|
+
if not self._enabled:
|
|
2695
|
+
msg = "Memory store is disabled"
|
|
2696
|
+
raise RuntimeError(msg)
|
|
2697
|
+
|
|
2698
|
+
if not entries:
|
|
2699
|
+
return 0
|
|
2700
|
+
|
|
2701
|
+
version = await self._get_version_info()
|
|
2702
|
+
supports_vectors = _memory_supports_vectors(version)
|
|
2703
|
+
if any(entry.get("embedding") is not None for entry in entries):
|
|
2704
|
+
_require_memory_vectors(version)
|
|
2705
|
+
embedding_column = ", embedding" if supports_vectors else ""
|
|
2706
|
+
embedding_param = ", :embedding" if supports_vectors else ""
|
|
2707
|
+
owner_column = f", {self._owner_id_column_name}" if self._owner_id_column_name else ""
|
|
2708
|
+
owner_param = ", :owner_id" if self._owner_id_column_name else ""
|
|
2709
|
+
sql = f"""
|
|
2710
|
+
INSERT INTO {self._memory_table} (
|
|
2711
|
+
id, session_id, app_name, user_id, scope, event_id, author{owner_column},
|
|
2712
|
+
timestamp{embedding_column}, content_json, content_text, metadata_json, inserted_at
|
|
2713
|
+
) VALUES (
|
|
2714
|
+
:id, :session_id, :app_name, :user_id, :scope, :event_id, :author{owner_param},
|
|
2715
|
+
:timestamp{embedding_param}, :content_json, :content_text, :metadata_json, :inserted_at
|
|
2716
|
+
)
|
|
2717
|
+
"""
|
|
2718
|
+
|
|
2719
|
+
batch_params: list[dict[str, Any]] = []
|
|
2720
|
+
for entry in entries:
|
|
2721
|
+
content_json = await self._serialize_json_field(entry["content_json"])
|
|
2722
|
+
metadata_json = await self._serialize_json_field(entry["metadata_json"])
|
|
2723
|
+
embedding_val = entry.get("embedding")
|
|
2724
|
+
bound_embedding = array.array("f", embedding_val) if embedding_val is not None else None
|
|
2725
|
+
p: dict[str, Any] = {
|
|
2726
|
+
"id": entry["id"],
|
|
2727
|
+
"session_id": entry["session_id"],
|
|
2728
|
+
"app_name": entry["app_name"],
|
|
2729
|
+
"user_id": entry["user_id"],
|
|
2730
|
+
"scope": entry.get("scope", "user"),
|
|
2731
|
+
"event_id": entry["event_id"],
|
|
2732
|
+
"author": entry["author"],
|
|
2733
|
+
"timestamp": entry["timestamp"],
|
|
2734
|
+
"content_json": content_json,
|
|
2735
|
+
"content_text": entry["content_text"],
|
|
2736
|
+
"metadata_json": metadata_json,
|
|
2737
|
+
"inserted_at": entry["inserted_at"],
|
|
2738
|
+
}
|
|
2739
|
+
if supports_vectors:
|
|
2740
|
+
p["embedding"] = bound_embedding
|
|
2741
|
+
if self._owner_id_column_name:
|
|
2742
|
+
p["owner_id"] = owner_id
|
|
2743
|
+
batch_params.append(p)
|
|
2744
|
+
|
|
2745
|
+
async with self._config.provide_connection() as conn:
|
|
2746
|
+
try:
|
|
2747
|
+
coerced_batch = [await coerce_large_parameters_async(conn, p) for p in batch_params]
|
|
2748
|
+
with conn.cursor() as cursor:
|
|
2749
|
+
await cursor.executemany(sql, coerced_batch, batcherrors=True)
|
|
2750
|
+
inserted_count = _memory_insert_count(len(entries), cursor.getbatcherrors())
|
|
2751
|
+
await conn.commit()
|
|
2752
|
+
return inserted_count
|
|
2753
|
+
except BaseException:
|
|
2754
|
+
await _rollback_async(conn)
|
|
2755
|
+
raise
|
|
2756
|
+
|
|
2757
|
+
async def search_entries(
|
|
2758
|
+
self,
|
|
2759
|
+
query: str,
|
|
2760
|
+
app_name: str,
|
|
2761
|
+
user_id: str,
|
|
2762
|
+
limit: "int | None" = None,
|
|
2763
|
+
scope_filter: Literal["all", "user", "app"] = "all",
|
|
2764
|
+
embedding: "Sequence[float] | None" = None,
|
|
2765
|
+
) -> "list[StoredMemory]":
|
|
2766
|
+
if not self._enabled:
|
|
2767
|
+
msg = "Memory store is disabled"
|
|
2768
|
+
raise RuntimeError(msg)
|
|
2769
|
+
|
|
2770
|
+
effective_limit = limit if limit is not None else self._max_results
|
|
2771
|
+
if embedding is not None:
|
|
2772
|
+
_require_memory_vectors(await self._get_version_info())
|
|
2773
|
+
|
|
2774
|
+
try:
|
|
2775
|
+
if embedding is not None and self._use_fts:
|
|
2776
|
+
return await self._search_entries_hybrid_rrf(
|
|
2777
|
+
query,
|
|
2778
|
+
embedding,
|
|
2779
|
+
app_name,
|
|
2780
|
+
user_id,
|
|
2781
|
+
effective_limit,
|
|
2782
|
+
scope_filter,
|
|
2783
|
+
fts_operator=self._fts_operator,
|
|
2784
|
+
approximate=self._approximate,
|
|
2785
|
+
read=True,
|
|
2786
|
+
)
|
|
2787
|
+
if self._use_fts:
|
|
2788
|
+
return await self._search_entries_fts(
|
|
2789
|
+
query, app_name, user_id, effective_limit, scope_filter, fts_operator=self._fts_operator, read=True
|
|
2790
|
+
)
|
|
2791
|
+
return await self._search_entries_simple(query, app_name, user_id, effective_limit, scope_filter, read=True)
|
|
2792
|
+
except OracleDatabaseError as exc:
|
|
2793
|
+
error_obj = exc.args[0] if exc.args else None
|
|
2794
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
2795
|
+
return []
|
|
2796
|
+
raise
|
|
2797
|
+
|
|
2798
|
+
async def delete_entries_by_session(self, session_id: str, app_name: str, user_id: str) -> int:
|
|
2799
|
+
clauses = ["session_id = :session_id"]
|
|
2800
|
+
params: dict[str, Any] = {"session_id": session_id}
|
|
2801
|
+
clauses.append("app_name = :app_name")
|
|
2802
|
+
params["app_name"] = app_name
|
|
2803
|
+
clauses.append("user_id = :user_id")
|
|
2804
|
+
params["user_id"] = user_id
|
|
2805
|
+
where_sql = " AND ".join(clauses)
|
|
2806
|
+
sql = f"DELETE FROM {self._memory_table} WHERE {where_sql}"
|
|
2807
|
+
async with self._config.provide_connection() as conn:
|
|
2808
|
+
with conn.cursor() as cursor:
|
|
2809
|
+
await cursor.execute(sql, params)
|
|
2810
|
+
await conn.commit()
|
|
2811
|
+
return cursor.rowcount if cursor.rowcount and cursor.rowcount > 0 else 0
|
|
2812
|
+
|
|
2813
|
+
async def delete_entries_older_than(
|
|
2814
|
+
self, days: int, app_name: "str | None" = None, scope: "str | None" = None
|
|
2815
|
+
) -> int:
|
|
2816
|
+
clauses = ["inserted_at < SYSTIMESTAMP - NUMTODSINTERVAL(:days, 'DAY')"]
|
|
2817
|
+
params: dict[str, Any] = {"days": days}
|
|
2818
|
+
if app_name is not None:
|
|
2819
|
+
clauses.append("app_name = :app_name")
|
|
2820
|
+
params["app_name"] = app_name
|
|
2821
|
+
if scope is not None:
|
|
2822
|
+
clauses.append("scope = :scope")
|
|
2823
|
+
params["scope"] = scope
|
|
2824
|
+
|
|
2825
|
+
where_sql = " AND ".join(clauses)
|
|
2826
|
+
sql = f"DELETE FROM {self._memory_table} WHERE {where_sql}"
|
|
2827
|
+
async with self._config.provide_connection() as conn:
|
|
2828
|
+
with conn.cursor() as cursor:
|
|
2829
|
+
await cursor.execute(sql, params)
|
|
2830
|
+
await conn.commit()
|
|
2831
|
+
return cursor.rowcount if cursor.rowcount and cursor.rowcount > 0 else 0
|
|
2832
|
+
|
|
2833
|
+
async def prepare_schema_async(self, driver: Any) -> None:
|
|
2834
|
+
"""Prepare adapter-specific schema decisions with an asynchronous driver."""
|
|
2835
|
+
await _prepare_oracle_schema_async(self._config, driver)
|
|
2836
|
+
|
|
2837
|
+
async def reconcile_companion_objects_async(self, driver: Any) -> None:
|
|
2838
|
+
"""Create missing Oracle memory companion indexes via catalog comparison.
|
|
2839
|
+
|
|
2840
|
+
Args:
|
|
2841
|
+
driver: Asynchronous Oracle driver executing schema reconciliation.
|
|
2842
|
+
"""
|
|
2843
|
+
if not self._enabled:
|
|
2844
|
+
return
|
|
2845
|
+
specs = _oracle_memory_index_specs(self._memory_table, use_fts=self._use_fts)
|
|
2846
|
+
rows = await driver.data_dictionary.get_indexes(driver)
|
|
2847
|
+
for statement in _oracle_missing_index_statements(specs, rows):
|
|
2848
|
+
await driver.execute(statement)
|
|
2849
|
+
|
|
2850
|
+
async def _detect_json_storage_type(self) -> "JSONStorageType":
|
|
2851
|
+
return storage_type_from_version(await self._get_version_info())
|
|
2852
|
+
|
|
2853
|
+
async def _get_version_info(self) -> "OracleVersionInfo | None":
|
|
2854
|
+
return await _get_oracle_version_info_async(self._config)
|
|
2855
|
+
|
|
2856
|
+
async def _serialize_json_field(self, value: Any) -> "str | bytes | None":
|
|
2857
|
+
if value is None:
|
|
2858
|
+
return None
|
|
2859
|
+
|
|
2860
|
+
storage_type = await self._detect_json_storage_type()
|
|
2861
|
+
if storage_type == JSONStorageType.JSON_NATIVE:
|
|
2862
|
+
return to_json(value)
|
|
2863
|
+
return to_json(value, as_bytes=True)
|
|
2864
|
+
|
|
2865
|
+
async def _deserialize_json_field(self, data: Any) -> "dict[str, Any] | None":
|
|
2866
|
+
if data is None:
|
|
2867
|
+
return None
|
|
2868
|
+
|
|
2869
|
+
if is_async_readable(data) or is_readable(data):
|
|
2870
|
+
data = await _read_lob_async(data)
|
|
2871
|
+
|
|
2872
|
+
return _extract_json_value(data)
|
|
2873
|
+
|
|
2874
|
+
async def _memory_table_ddl(self) -> str:
|
|
2875
|
+
version = await self._get_version_info()
|
|
2876
|
+
return self._memory_table_ddl_for_type(
|
|
2877
|
+
storage_type_from_version(version), supports_vectors=_memory_supports_vectors(version)
|
|
2878
|
+
)
|
|
2879
|
+
|
|
2880
|
+
def _memory_table_ddl_for_type(self, storage_type: "JSONStorageType", *, supports_vectors: bool = False) -> str:
|
|
2881
|
+
if storage_type == JSONStorageType.JSON_NATIVE:
|
|
2882
|
+
json_columns = """
|
|
2883
|
+
content_json JSON,
|
|
2884
|
+
metadata_json JSON
|
|
2885
|
+
"""
|
|
2886
|
+
elif storage_type == JSONStorageType.BLOB_JSON:
|
|
2887
|
+
json_columns = """
|
|
2888
|
+
content_json BLOB CHECK (content_json IS JSON),
|
|
2889
|
+
metadata_json BLOB CHECK (metadata_json IS JSON)
|
|
2890
|
+
"""
|
|
2891
|
+
else:
|
|
2892
|
+
json_columns = """
|
|
2893
|
+
content_json BLOB,
|
|
2894
|
+
metadata_json BLOB
|
|
2895
|
+
"""
|
|
2896
|
+
|
|
2897
|
+
escaped_owner_id_ddl = self._owner_id_column_ddl.replace("'", "''") if self._owner_id_column_ddl else ""
|
|
2898
|
+
owner_id_line = (
|
|
2899
|
+
_ADK_MEMORY_TABLE_DDL_FOR_TYPE_TEMPLATE.format(escaped_owner_id_ddl) if escaped_owner_id_ddl else ""
|
|
2900
|
+
)
|
|
2901
|
+
table_clauses = _adk_table_feature_clause(
|
|
2902
|
+
self._config,
|
|
2903
|
+
"memory",
|
|
2904
|
+
in_memory=self._in_memory,
|
|
2905
|
+
hash_partition_key="id",
|
|
2906
|
+
range_partition_key="inserted_at",
|
|
2907
|
+
)
|
|
2908
|
+
|
|
2909
|
+
fts_index = ""
|
|
2910
|
+
if self._use_fts:
|
|
2911
|
+
fts_index = _ADK_MEMORY_TABLE_DDL_FOR_TYPE_TEMPLATE_2.format(self._memory_table, self._memory_table)
|
|
2912
|
+
|
|
2913
|
+
return _ADK_MEMORY_TABLE_DDL_FOR_TYPE_TEMPLATE_3.format(
|
|
2914
|
+
self._memory_table,
|
|
2915
|
+
owner_id_line,
|
|
2916
|
+
json_columns,
|
|
2917
|
+
table_clauses,
|
|
2918
|
+
self._memory_table,
|
|
2919
|
+
self._memory_table,
|
|
2920
|
+
self._memory_table,
|
|
2921
|
+
self._memory_table,
|
|
2922
|
+
self._memory_table,
|
|
2923
|
+
self._memory_table,
|
|
2924
|
+
fts_index,
|
|
2925
|
+
f"embedding VECTOR({self._vector_dimensions}, FLOAT32),\n " if supports_vectors else "",
|
|
2926
|
+
)
|
|
2927
|
+
|
|
2928
|
+
def _drop_memory_table_sql(self) -> "list[str]":
|
|
2929
|
+
statements = [
|
|
2930
|
+
f"""
|
|
2931
|
+
BEGIN
|
|
2932
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._memory_table}_session';
|
|
2933
|
+
EXCEPTION
|
|
2934
|
+
WHEN OTHERS THEN
|
|
2935
|
+
IF SQLCODE != -1418 THEN
|
|
2936
|
+
RAISE;
|
|
2937
|
+
END IF;
|
|
2938
|
+
END;
|
|
2939
|
+
""",
|
|
2940
|
+
f"""
|
|
2941
|
+
BEGIN
|
|
2942
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._memory_table}_app_scope_user_time';
|
|
2943
|
+
EXCEPTION
|
|
2944
|
+
WHEN OTHERS THEN
|
|
2945
|
+
IF SQLCODE != -1418 THEN
|
|
2946
|
+
RAISE;
|
|
2947
|
+
END IF;
|
|
2948
|
+
END;
|
|
2949
|
+
""",
|
|
2950
|
+
f"""
|
|
2951
|
+
BEGIN
|
|
2952
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._memory_table}_scope';
|
|
2953
|
+
EXCEPTION
|
|
2954
|
+
WHEN OTHERS THEN
|
|
2955
|
+
IF SQLCODE != -1418 THEN
|
|
2956
|
+
RAISE;
|
|
2957
|
+
END IF;
|
|
2958
|
+
END;
|
|
2959
|
+
""",
|
|
2960
|
+
]
|
|
2961
|
+
if self._use_fts:
|
|
2962
|
+
statements.append(
|
|
2963
|
+
f"""
|
|
2964
|
+
BEGIN
|
|
2965
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._memory_table}_fts';
|
|
2966
|
+
EXCEPTION
|
|
2967
|
+
WHEN OTHERS THEN
|
|
2968
|
+
IF SQLCODE != -1418 THEN
|
|
2969
|
+
RAISE;
|
|
2970
|
+
END IF;
|
|
2971
|
+
END;
|
|
2972
|
+
"""
|
|
2973
|
+
)
|
|
2974
|
+
statements.append(
|
|
2975
|
+
f"""
|
|
2976
|
+
BEGIN
|
|
2977
|
+
EXECUTE IMMEDIATE 'DROP TABLE {self._memory_table}';
|
|
2978
|
+
EXCEPTION
|
|
2979
|
+
WHEN OTHERS THEN
|
|
2980
|
+
IF SQLCODE != -942 THEN
|
|
2981
|
+
RAISE;
|
|
2982
|
+
END IF;
|
|
2983
|
+
END;
|
|
2984
|
+
"""
|
|
2985
|
+
)
|
|
2986
|
+
return statements
|
|
2987
|
+
|
|
2988
|
+
async def _execute_insert_entry(self, cursor: Any, sql: str, params: "dict[str, Any]") -> bool:
|
|
2989
|
+
"""Execute an insert and skip duplicate key errors."""
|
|
2990
|
+
try:
|
|
2991
|
+
await cursor.execute(sql, params)
|
|
2992
|
+
except OracleDatabaseError as exc:
|
|
2993
|
+
error_obj = exc.args[0] if exc.args else None
|
|
2994
|
+
if error_obj and error_obj.code == ORACLE_DUPLICATE_KEY_ERROR:
|
|
2995
|
+
return False
|
|
2996
|
+
raise
|
|
2997
|
+
return True
|
|
2998
|
+
|
|
2999
|
+
async def _search_entries_fts(
|
|
3000
|
+
self,
|
|
3001
|
+
query: str,
|
|
3002
|
+
app_name: str,
|
|
3003
|
+
user_id: str,
|
|
3004
|
+
limit: int,
|
|
3005
|
+
scope_filter: Literal["all", "user", "app"] = "all",
|
|
3006
|
+
*,
|
|
3007
|
+
fts_operator: str = "ACCUM",
|
|
3008
|
+
read: bool = False,
|
|
3009
|
+
) -> "list[StoredMemory]":
|
|
3010
|
+
sanitized_query = sanitize_oracle_text_query(query, operator=fts_operator)
|
|
3011
|
+
if not sanitized_query:
|
|
3012
|
+
return []
|
|
3013
|
+
where_scope, scope_params = _build_oracle_scope_where(app_name, user_id, scope_filter)
|
|
3014
|
+
embedding_column = (
|
|
3015
|
+
"embedding" if _memory_supports_vectors(await self._get_version_info()) else "NULL AS embedding"
|
|
3016
|
+
)
|
|
3017
|
+
sql = f"""
|
|
3018
|
+
SELECT id, session_id, app_name, user_id, scope, event_id, author,
|
|
3019
|
+
timestamp, content_json, content_text, metadata_json, inserted_at, embedding
|
|
3020
|
+
FROM (
|
|
3021
|
+
SELECT id, session_id, app_name, user_id, scope, event_id, author,
|
|
3022
|
+
timestamp, content_json, content_text, metadata_json, inserted_at, {embedding_column},
|
|
3023
|
+
SCORE(1) AS score
|
|
3024
|
+
FROM {self._memory_table}
|
|
3025
|
+
WHERE {where_scope}
|
|
3026
|
+
AND CONTAINS(content_text, :query, 1) > 0
|
|
3027
|
+
ORDER BY score DESC, timestamp DESC
|
|
3028
|
+
)
|
|
3029
|
+
WHERE ROWNUM <= :limit
|
|
3030
|
+
"""
|
|
3031
|
+
params = {**scope_params, "query": sanitized_query, "limit": limit}
|
|
3032
|
+
async with self._provide_replica_connection(primary=not read) as conn:
|
|
3033
|
+
with conn.cursor() as cursor:
|
|
3034
|
+
await cursor.execute(sql, params)
|
|
3035
|
+
rows = await cursor.fetchall()
|
|
3036
|
+
return await self._rows_to_records(rows)
|
|
3037
|
+
|
|
3038
|
+
async def _search_entries_hybrid_rrf(
|
|
3039
|
+
self,
|
|
3040
|
+
query: str,
|
|
3041
|
+
embedding: "Sequence[float] | Any",
|
|
3042
|
+
app_name: str,
|
|
3043
|
+
user_id: str,
|
|
3044
|
+
limit: int,
|
|
3045
|
+
scope_filter: Literal["all", "user", "app"] = "all",
|
|
3046
|
+
*,
|
|
3047
|
+
vector_column: str = "embedding",
|
|
3048
|
+
rrf_k: int = 60,
|
|
3049
|
+
metric: str = "COSINE",
|
|
3050
|
+
fts_operator: str = "ACCUM",
|
|
3051
|
+
approximate: bool = True,
|
|
3052
|
+
read: bool = False,
|
|
3053
|
+
) -> "list[StoredMemory]":
|
|
3054
|
+
sanitized_query = sanitize_oracle_text_query(query, operator=fts_operator)
|
|
3055
|
+
where_scope, scope_params = _build_oracle_scope_where(app_name, user_id, scope_filter)
|
|
3056
|
+
select_cols = (
|
|
3057
|
+
"id",
|
|
3058
|
+
"session_id",
|
|
3059
|
+
"app_name",
|
|
3060
|
+
"user_id",
|
|
3061
|
+
"scope",
|
|
3062
|
+
"event_id",
|
|
3063
|
+
"author",
|
|
3064
|
+
"timestamp",
|
|
3065
|
+
"content_json",
|
|
3066
|
+
"content_text",
|
|
3067
|
+
"metadata_json",
|
|
3068
|
+
"inserted_at",
|
|
3069
|
+
"embedding",
|
|
3070
|
+
)
|
|
3071
|
+
if not sanitized_query:
|
|
3072
|
+
sql = build_oracle_hnsw_subquery(
|
|
3073
|
+
table=self._memory_table,
|
|
3074
|
+
vector_column=vector_column,
|
|
3075
|
+
select_columns=select_cols,
|
|
3076
|
+
vector_param="query_vector",
|
|
3077
|
+
top_k_param="top_k",
|
|
3078
|
+
metric=metric,
|
|
3079
|
+
approximate=approximate,
|
|
3080
|
+
where_clause=where_scope,
|
|
3081
|
+
)
|
|
3082
|
+
params: dict[str, Any] = {**scope_params, "query_vector": embedding, "top_k": limit}
|
|
3083
|
+
else:
|
|
3084
|
+
sql = build_oracle_hybrid_rrf_sql(
|
|
3085
|
+
table=self._memory_table,
|
|
3086
|
+
key_column="id",
|
|
3087
|
+
vector_column=vector_column,
|
|
3088
|
+
text_column="content_text",
|
|
3089
|
+
select_columns=select_cols,
|
|
3090
|
+
vector_param="query_vector",
|
|
3091
|
+
text_param="text_query",
|
|
3092
|
+
top_k_param="top_k",
|
|
3093
|
+
rrf_k_param="rrf_k",
|
|
3094
|
+
metric=metric,
|
|
3095
|
+
approximate=approximate,
|
|
3096
|
+
where_clause=where_scope,
|
|
3097
|
+
)
|
|
3098
|
+
params = {
|
|
3099
|
+
**scope_params,
|
|
3100
|
+
"query_vector": embedding,
|
|
3101
|
+
"text_query": sanitized_query,
|
|
3102
|
+
"top_k": limit,
|
|
3103
|
+
"rrf_k": rrf_k,
|
|
3104
|
+
}
|
|
3105
|
+
async with self._provide_replica_connection(primary=not read) as conn:
|
|
3106
|
+
with conn.cursor() as cursor:
|
|
3107
|
+
await cursor.execute(sql, params)
|
|
3108
|
+
rows = await cursor.fetchall()
|
|
3109
|
+
return await self._rows_to_records(rows)
|
|
3110
|
+
|
|
3111
|
+
async def _search_entries_simple(
|
|
3112
|
+
self,
|
|
3113
|
+
query: str,
|
|
3114
|
+
app_name: str,
|
|
3115
|
+
user_id: str,
|
|
3116
|
+
limit: int,
|
|
3117
|
+
scope_filter: Literal["all", "user", "app"] = "all",
|
|
3118
|
+
*,
|
|
3119
|
+
read: bool = False,
|
|
3120
|
+
) -> "list[StoredMemory]":
|
|
3121
|
+
where_scope, scope_params = _build_oracle_scope_where(app_name, user_id, scope_filter)
|
|
3122
|
+
embedding_column = (
|
|
3123
|
+
"embedding" if _memory_supports_vectors(await self._get_version_info()) else "NULL AS embedding"
|
|
3124
|
+
)
|
|
3125
|
+
sql = f"""
|
|
3126
|
+
SELECT id, session_id, app_name, user_id, scope, event_id, author,
|
|
3127
|
+
timestamp, content_json, content_text, metadata_json, inserted_at, embedding
|
|
3128
|
+
FROM (
|
|
3129
|
+
SELECT id, session_id, app_name, user_id, scope, event_id, author,
|
|
3130
|
+
timestamp, content_json, content_text, metadata_json, inserted_at, {embedding_column}
|
|
3131
|
+
FROM {self._memory_table}
|
|
3132
|
+
WHERE {where_scope}
|
|
3133
|
+
AND LOWER(content_text) LIKE :pattern
|
|
3134
|
+
ORDER BY timestamp DESC
|
|
3135
|
+
)
|
|
3136
|
+
WHERE ROWNUM <= :limit
|
|
3137
|
+
"""
|
|
3138
|
+
pattern = f"%{query.lower()}%"
|
|
3139
|
+
params = {**scope_params, "pattern": pattern, "limit": limit}
|
|
3140
|
+
async with self._provide_replica_connection(primary=not read) as conn:
|
|
3141
|
+
with conn.cursor() as cursor:
|
|
3142
|
+
await cursor.execute(sql, params)
|
|
3143
|
+
rows = await cursor.fetchall()
|
|
3144
|
+
return await self._rows_to_records(rows)
|
|
3145
|
+
|
|
3146
|
+
async def _rows_to_records(self, rows: "list[Any]") -> "list[StoredMemory]":
|
|
3147
|
+
records: list[StoredMemory] = []
|
|
3148
|
+
for row in rows:
|
|
3149
|
+
content_json = await self._deserialize_json_field(row[8]) if row[8] is not None else {}
|
|
3150
|
+
metadata_json = await self._deserialize_json_field(row[10])
|
|
3151
|
+
content_text = row[9]
|
|
3152
|
+
if is_async_readable(content_text) or is_readable(content_text):
|
|
3153
|
+
content_text = await _read_lob_async(content_text)
|
|
3154
|
+
embedding_val = row[_EMBEDDING_COLUMN_INDEX] if len(row) > _EMBEDDING_COLUMN_INDEX else None
|
|
3155
|
+
embedding = [float(x) for x in embedding_val] if embedding_val is not None else None
|
|
3156
|
+
records.append({
|
|
3157
|
+
"id": row[0],
|
|
3158
|
+
"session_id": row[1],
|
|
3159
|
+
"app_name": row[2],
|
|
3160
|
+
"user_id": row[3],
|
|
3161
|
+
"scope": row[4],
|
|
3162
|
+
"event_id": row[5],
|
|
3163
|
+
"author": row[6],
|
|
3164
|
+
"timestamp": row[7],
|
|
3165
|
+
"content_json": cast("dict[str, Any]", content_json),
|
|
3166
|
+
"content_text": str(content_text),
|
|
3167
|
+
"metadata_json": metadata_json,
|
|
3168
|
+
"inserted_at": row[11],
|
|
3169
|
+
"embedding": embedding,
|
|
3170
|
+
})
|
|
3171
|
+
return records
|
|
3172
|
+
|
|
3173
|
+
|
|
3174
|
+
class OracleSyncADKMemoryStore(BaseSyncADKMemoryStore["OracleSyncConfig"]):
|
|
3175
|
+
"""Oracle ADK memory store using sync oracledb driver."""
|
|
3176
|
+
|
|
3177
|
+
__slots__ = ("_approximate", "_fts_operator", "_in_memory")
|
|
3178
|
+
|
|
3179
|
+
def __init__(self, config: "OracleSyncConfig", *, read_config: "OracleSyncConfig | None" = None) -> None:
|
|
3180
|
+
super().__init__(config, read_config=read_config)
|
|
3181
|
+
validate_oracle_identifier(self._memory_table, "table name")
|
|
3182
|
+
adk_config = _adk_config(config)
|
|
3183
|
+
self._in_memory = bool(adk_config.get("in_memory", False))
|
|
3184
|
+
self._fts_operator: str = str(adk_config.get("fts_operator", "ACCUM"))
|
|
3185
|
+
self._approximate: bool = bool(adk_config.get("approximate", True))
|
|
3186
|
+
|
|
3187
|
+
@property
|
|
3188
|
+
def fts_operator(self) -> str:
|
|
3189
|
+
"""Return the default Oracle Text operator for hybrid search."""
|
|
3190
|
+
return self._fts_operator
|
|
3191
|
+
|
|
3192
|
+
@fts_operator.setter
|
|
3193
|
+
def fts_operator(self, value: str) -> None:
|
|
3194
|
+
self._fts_operator = value
|
|
3195
|
+
|
|
3196
|
+
@property
|
|
3197
|
+
def approximate(self) -> bool:
|
|
3198
|
+
"""Return whether approximate vector search is enabled."""
|
|
3199
|
+
return self._approximate
|
|
3200
|
+
|
|
3201
|
+
@approximate.setter
|
|
3202
|
+
def approximate(self, value: bool) -> None:
|
|
3203
|
+
self._approximate = bool(value)
|
|
3204
|
+
|
|
3205
|
+
def create_tables(self) -> None:
|
|
3206
|
+
"""Create the memory table when the data dictionary reports it missing."""
|
|
3207
|
+
if not self.create_schema_enabled:
|
|
3208
|
+
self.reconcile_schema()
|
|
3209
|
+
return
|
|
3210
|
+
|
|
3211
|
+
if not self._enabled:
|
|
3212
|
+
return
|
|
3213
|
+
|
|
3214
|
+
with self._config.provide_session() as driver:
|
|
3215
|
+
existing = _existing_table_names(driver.data_dictionary.get_tables(driver))
|
|
3216
|
+
if _bare_table_name(self._memory_table) not in existing:
|
|
3217
|
+
driver.execute_script(self._memory_table_ddl())
|
|
3218
|
+
|
|
3219
|
+
def insert_memory_entries(self, entries: "list[StoredMemory]", owner_id: "object | None" = None) -> int:
|
|
3220
|
+
"""Bulk insert memory entries with deduplication."""
|
|
3221
|
+
if not self._enabled:
|
|
3222
|
+
msg = "Memory store is disabled"
|
|
3223
|
+
raise RuntimeError(msg)
|
|
3224
|
+
|
|
3225
|
+
if not entries:
|
|
3226
|
+
return 0
|
|
3227
|
+
|
|
3228
|
+
version = self._get_version_info()
|
|
3229
|
+
supports_vectors = _memory_supports_vectors(version)
|
|
3230
|
+
if any(entry.get("embedding") is not None for entry in entries):
|
|
3231
|
+
_require_memory_vectors(version)
|
|
3232
|
+
embedding_column = ", embedding" if supports_vectors else ""
|
|
3233
|
+
embedding_param = ", :embedding" if supports_vectors else ""
|
|
3234
|
+
owner_column = f", {self._owner_id_column_name}" if self._owner_id_column_name else ""
|
|
3235
|
+
owner_param = ", :owner_id" if self._owner_id_column_name else ""
|
|
3236
|
+
sql = f"""
|
|
3237
|
+
INSERT INTO {self._memory_table} (
|
|
3238
|
+
id, session_id, app_name, user_id, scope, event_id, author{owner_column},
|
|
3239
|
+
timestamp{embedding_column}, content_json, content_text, metadata_json, inserted_at
|
|
3240
|
+
) VALUES (
|
|
3241
|
+
:id, :session_id, :app_name, :user_id, :scope, :event_id, :author{owner_param},
|
|
3242
|
+
:timestamp{embedding_param}, :content_json, :content_text, :metadata_json, :inserted_at
|
|
3243
|
+
)
|
|
3244
|
+
"""
|
|
3245
|
+
|
|
3246
|
+
batch_params: list[dict[str, Any]] = []
|
|
3247
|
+
for entry in entries:
|
|
3248
|
+
content_json = self._serialize_json_field(entry["content_json"])
|
|
3249
|
+
metadata_json = self._serialize_json_field(entry["metadata_json"])
|
|
3250
|
+
embedding_val = entry.get("embedding")
|
|
3251
|
+
bound_embedding = array.array("f", embedding_val) if embedding_val is not None else None
|
|
3252
|
+
p: dict[str, Any] = {
|
|
3253
|
+
"id": entry["id"],
|
|
3254
|
+
"session_id": entry["session_id"],
|
|
3255
|
+
"app_name": entry["app_name"],
|
|
3256
|
+
"user_id": entry["user_id"],
|
|
3257
|
+
"scope": entry.get("scope", "user"),
|
|
3258
|
+
"event_id": entry["event_id"],
|
|
3259
|
+
"author": entry["author"],
|
|
3260
|
+
"timestamp": entry["timestamp"],
|
|
3261
|
+
"content_json": content_json,
|
|
3262
|
+
"content_text": entry["content_text"],
|
|
3263
|
+
"metadata_json": metadata_json,
|
|
3264
|
+
"inserted_at": entry["inserted_at"],
|
|
3265
|
+
}
|
|
3266
|
+
if supports_vectors:
|
|
3267
|
+
p["embedding"] = bound_embedding
|
|
3268
|
+
if self._owner_id_column_name:
|
|
3269
|
+
p["owner_id"] = owner_id
|
|
3270
|
+
batch_params.append(p)
|
|
3271
|
+
|
|
3272
|
+
with self._config.provide_connection() as conn:
|
|
3273
|
+
try:
|
|
3274
|
+
coerced_batch = [coerce_large_parameters_sync(conn, p) for p in batch_params]
|
|
3275
|
+
with conn.cursor() as cursor:
|
|
3276
|
+
cursor.executemany(sql, coerced_batch, batcherrors=True)
|
|
3277
|
+
inserted_count = _memory_insert_count(len(entries), cursor.getbatcherrors())
|
|
3278
|
+
conn.commit()
|
|
3279
|
+
return inserted_count
|
|
3280
|
+
except BaseException:
|
|
3281
|
+
_rollback_sync(conn)
|
|
3282
|
+
raise
|
|
3283
|
+
|
|
3284
|
+
def search_entries(
|
|
3285
|
+
self,
|
|
3286
|
+
query: str,
|
|
3287
|
+
app_name: str,
|
|
3288
|
+
user_id: str,
|
|
3289
|
+
limit: "int | None" = None,
|
|
3290
|
+
scope_filter: Literal["all", "user", "app"] = "all",
|
|
3291
|
+
embedding: "Sequence[float] | None" = None,
|
|
3292
|
+
) -> "list[StoredMemory]":
|
|
3293
|
+
"""Search memory entries by text query."""
|
|
3294
|
+
if not self._enabled:
|
|
3295
|
+
msg = "Memory store is disabled"
|
|
3296
|
+
raise RuntimeError(msg)
|
|
3297
|
+
|
|
3298
|
+
effective_limit = limit if limit is not None else self._max_results
|
|
3299
|
+
if embedding is not None:
|
|
3300
|
+
_require_memory_vectors(self._get_version_info())
|
|
3301
|
+
|
|
3302
|
+
try:
|
|
3303
|
+
if embedding is not None and self._use_fts:
|
|
3304
|
+
return self._search_entries_hybrid_rrf(
|
|
3305
|
+
query,
|
|
3306
|
+
embedding,
|
|
3307
|
+
app_name,
|
|
3308
|
+
user_id,
|
|
3309
|
+
effective_limit,
|
|
3310
|
+
scope_filter,
|
|
3311
|
+
fts_operator=self._fts_operator,
|
|
3312
|
+
approximate=self._approximate,
|
|
3313
|
+
read=True,
|
|
3314
|
+
)
|
|
3315
|
+
if self._use_fts:
|
|
3316
|
+
return self._search_entries_fts(
|
|
3317
|
+
query, app_name, user_id, effective_limit, scope_filter, fts_operator=self._fts_operator, read=True
|
|
3318
|
+
)
|
|
3319
|
+
return self._search_entries_simple(query, app_name, user_id, effective_limit, scope_filter, read=True)
|
|
3320
|
+
except OracleDatabaseError as exc:
|
|
3321
|
+
error_obj = exc.args[0] if exc.args else None
|
|
3322
|
+
if error_obj and error_obj.code == ORACLE_TABLE_NOT_FOUND_ERROR:
|
|
3323
|
+
return []
|
|
3324
|
+
raise
|
|
3325
|
+
|
|
3326
|
+
def delete_entries_by_session(self, session_id: str, app_name: str, user_id: str) -> int:
|
|
3327
|
+
"""Delete memory entries for a specific session, owned by an app and user."""
|
|
3328
|
+
clauses = ["session_id = :session_id"]
|
|
3329
|
+
params: dict[str, Any] = {"session_id": session_id}
|
|
3330
|
+
clauses.append("app_name = :app_name")
|
|
3331
|
+
params["app_name"] = app_name
|
|
3332
|
+
clauses.append("user_id = :user_id")
|
|
3333
|
+
params["user_id"] = user_id
|
|
3334
|
+
where_sql = " AND ".join(clauses)
|
|
3335
|
+
sql = f"DELETE FROM {self._memory_table} WHERE {where_sql}"
|
|
3336
|
+
with self._config.provide_connection() as conn, conn.cursor() as cursor:
|
|
3337
|
+
cursor.execute(sql, params)
|
|
3338
|
+
conn.commit()
|
|
3339
|
+
return cursor.rowcount if cursor.rowcount and cursor.rowcount > 0 else 0
|
|
3340
|
+
|
|
3341
|
+
def delete_entries_older_than(self, days: int, app_name: "str | None" = None, scope: "str | None" = None) -> int:
|
|
3342
|
+
"""Delete memory entries older than specified days."""
|
|
3343
|
+
clauses = ["inserted_at < SYSTIMESTAMP - NUMTODSINTERVAL(:days, 'DAY')"]
|
|
3344
|
+
params: dict[str, Any] = {"days": days}
|
|
3345
|
+
if app_name is not None:
|
|
3346
|
+
clauses.append("app_name = :app_name")
|
|
3347
|
+
params["app_name"] = app_name
|
|
3348
|
+
if scope is not None:
|
|
3349
|
+
clauses.append("scope = :scope")
|
|
3350
|
+
params["scope"] = scope
|
|
3351
|
+
|
|
3352
|
+
where_sql = " AND ".join(clauses)
|
|
3353
|
+
sql = f"DELETE FROM {self._memory_table} WHERE {where_sql}"
|
|
3354
|
+
with self._config.provide_connection() as conn, conn.cursor() as cursor:
|
|
3355
|
+
cursor.execute(sql, params)
|
|
3356
|
+
conn.commit()
|
|
3357
|
+
return cursor.rowcount if cursor.rowcount and cursor.rowcount > 0 else 0
|
|
3358
|
+
|
|
3359
|
+
def prepare_schema_sync(self, driver: Any) -> None:
|
|
3360
|
+
"""Prepare adapter-specific schema decisions with a synchronous driver."""
|
|
3361
|
+
_prepare_oracle_schema_sync(self._config, driver)
|
|
3362
|
+
|
|
3363
|
+
def reconcile_companion_objects_sync(self, driver: Any) -> None:
|
|
3364
|
+
"""Create missing Oracle memory companion indexes via catalog comparison.
|
|
3365
|
+
|
|
3366
|
+
Args:
|
|
3367
|
+
driver: Synchronous Oracle driver executing schema reconciliation.
|
|
3368
|
+
"""
|
|
3369
|
+
if not self._enabled:
|
|
3370
|
+
return
|
|
3371
|
+
specs = _oracle_memory_index_specs(self._memory_table, use_fts=self._use_fts)
|
|
3372
|
+
rows = driver.data_dictionary.get_indexes(driver)
|
|
3373
|
+
for statement in _oracle_missing_index_statements(specs, rows):
|
|
3374
|
+
driver.execute(statement)
|
|
3375
|
+
|
|
3376
|
+
def _detect_json_storage_type(self) -> "JSONStorageType":
|
|
3377
|
+
return storage_type_from_version(self._get_version_info())
|
|
3378
|
+
|
|
3379
|
+
def _get_version_info(self) -> "OracleVersionInfo | None":
|
|
3380
|
+
return _get_oracle_version_info_sync(self._config)
|
|
3381
|
+
|
|
3382
|
+
def _serialize_json_field(self, value: Any) -> "str | bytes | None":
|
|
3383
|
+
if value is None:
|
|
3384
|
+
return None
|
|
3385
|
+
|
|
3386
|
+
storage_type = self._detect_json_storage_type()
|
|
3387
|
+
if storage_type == JSONStorageType.JSON_NATIVE:
|
|
3388
|
+
return to_json(value)
|
|
3389
|
+
return to_json(value, as_bytes=True)
|
|
3390
|
+
|
|
3391
|
+
def _deserialize_json_field(self, data: Any) -> "dict[str, Any] | None":
|
|
3392
|
+
if data is None:
|
|
3393
|
+
return None
|
|
3394
|
+
|
|
3395
|
+
if is_readable(data):
|
|
3396
|
+
data = _read_lob_sync(data)
|
|
3397
|
+
|
|
3398
|
+
return _extract_json_value(data)
|
|
3399
|
+
|
|
3400
|
+
def _memory_table_ddl(self) -> str:
|
|
3401
|
+
version = self._get_version_info()
|
|
3402
|
+
return self._memory_table_ddl_for_type(
|
|
3403
|
+
storage_type_from_version(version), supports_vectors=_memory_supports_vectors(version)
|
|
3404
|
+
)
|
|
3405
|
+
|
|
3406
|
+
def _memory_table_ddl_for_type(self, storage_type: "JSONStorageType", *, supports_vectors: bool = False) -> str:
|
|
3407
|
+
if storage_type == JSONStorageType.JSON_NATIVE:
|
|
3408
|
+
json_columns = """
|
|
3409
|
+
content_json JSON,
|
|
3410
|
+
metadata_json JSON
|
|
3411
|
+
"""
|
|
3412
|
+
elif storage_type == JSONStorageType.BLOB_JSON:
|
|
3413
|
+
json_columns = """
|
|
3414
|
+
content_json BLOB CHECK (content_json IS JSON),
|
|
3415
|
+
metadata_json BLOB CHECK (metadata_json IS JSON)
|
|
3416
|
+
"""
|
|
3417
|
+
else:
|
|
3418
|
+
json_columns = """
|
|
3419
|
+
content_json BLOB,
|
|
3420
|
+
metadata_json BLOB
|
|
3421
|
+
"""
|
|
3422
|
+
|
|
3423
|
+
escaped_owner_id_ddl = self._owner_id_column_ddl.replace("'", "''") if self._owner_id_column_ddl else ""
|
|
3424
|
+
owner_id_line = (
|
|
3425
|
+
_ADK_MEMORY_TABLE_DDL_FOR_TYPE_TEMPLATE.format(escaped_owner_id_ddl) if escaped_owner_id_ddl else ""
|
|
3426
|
+
)
|
|
3427
|
+
table_clauses = _adk_table_feature_clause(
|
|
3428
|
+
self._config,
|
|
3429
|
+
"memory",
|
|
3430
|
+
in_memory=self._in_memory,
|
|
3431
|
+
hash_partition_key="id",
|
|
3432
|
+
range_partition_key="inserted_at",
|
|
3433
|
+
)
|
|
3434
|
+
|
|
3435
|
+
fts_index = ""
|
|
3436
|
+
if self._use_fts:
|
|
3437
|
+
fts_index = _ADK_MEMORY_TABLE_DDL_FOR_TYPE_TEMPLATE_2.format(self._memory_table, self._memory_table)
|
|
3438
|
+
|
|
3439
|
+
return _ADK_MEMORY_TABLE_DDL_FOR_TYPE_TEMPLATE_3.format(
|
|
3440
|
+
self._memory_table,
|
|
3441
|
+
owner_id_line,
|
|
3442
|
+
json_columns,
|
|
3443
|
+
table_clauses,
|
|
3444
|
+
self._memory_table,
|
|
3445
|
+
self._memory_table,
|
|
3446
|
+
self._memory_table,
|
|
3447
|
+
self._memory_table,
|
|
3448
|
+
self._memory_table,
|
|
3449
|
+
self._memory_table,
|
|
3450
|
+
fts_index,
|
|
3451
|
+
f"embedding VECTOR({self._vector_dimensions}, FLOAT32),\n " if supports_vectors else "",
|
|
3452
|
+
)
|
|
3453
|
+
|
|
3454
|
+
def _drop_memory_table_sql(self) -> "list[str]":
|
|
3455
|
+
statements = [
|
|
3456
|
+
f"""
|
|
3457
|
+
BEGIN
|
|
3458
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._memory_table}_session';
|
|
3459
|
+
EXCEPTION
|
|
3460
|
+
WHEN OTHERS THEN
|
|
3461
|
+
IF SQLCODE != -1418 THEN
|
|
3462
|
+
RAISE;
|
|
3463
|
+
END IF;
|
|
3464
|
+
END;
|
|
3465
|
+
""",
|
|
3466
|
+
f"""
|
|
3467
|
+
BEGIN
|
|
3468
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._memory_table}_app_scope_user_time';
|
|
3469
|
+
EXCEPTION
|
|
3470
|
+
WHEN OTHERS THEN
|
|
3471
|
+
IF SQLCODE != -1418 THEN
|
|
3472
|
+
RAISE;
|
|
3473
|
+
END IF;
|
|
3474
|
+
END;
|
|
3475
|
+
""",
|
|
3476
|
+
f"""
|
|
3477
|
+
BEGIN
|
|
3478
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._memory_table}_scope';
|
|
3479
|
+
EXCEPTION
|
|
3480
|
+
WHEN OTHERS THEN
|
|
3481
|
+
IF SQLCODE != -1418 THEN
|
|
3482
|
+
RAISE;
|
|
3483
|
+
END IF;
|
|
3484
|
+
END;
|
|
3485
|
+
""",
|
|
3486
|
+
]
|
|
3487
|
+
if self._use_fts:
|
|
3488
|
+
statements.append(
|
|
3489
|
+
f"""
|
|
3490
|
+
BEGIN
|
|
3491
|
+
EXECUTE IMMEDIATE 'DROP INDEX idx_{self._memory_table}_fts';
|
|
3492
|
+
EXCEPTION
|
|
3493
|
+
WHEN OTHERS THEN
|
|
3494
|
+
IF SQLCODE != -1418 THEN
|
|
3495
|
+
RAISE;
|
|
3496
|
+
END IF;
|
|
3497
|
+
END;
|
|
3498
|
+
"""
|
|
3499
|
+
)
|
|
3500
|
+
statements.append(
|
|
3501
|
+
f"""
|
|
3502
|
+
BEGIN
|
|
3503
|
+
EXECUTE IMMEDIATE 'DROP TABLE {self._memory_table}';
|
|
3504
|
+
EXCEPTION
|
|
3505
|
+
WHEN OTHERS THEN
|
|
3506
|
+
IF SQLCODE != -942 THEN
|
|
3507
|
+
RAISE;
|
|
3508
|
+
END IF;
|
|
3509
|
+
END;
|
|
3510
|
+
"""
|
|
3511
|
+
)
|
|
3512
|
+
return statements
|
|
3513
|
+
|
|
3514
|
+
def _execute_insert_entry(self, cursor: Any, sql: str, params: "dict[str, Any]") -> bool:
|
|
3515
|
+
"""Execute an insert and skip duplicate key errors."""
|
|
3516
|
+
try:
|
|
3517
|
+
cursor.execute(sql, params)
|
|
3518
|
+
except OracleDatabaseError as exc:
|
|
3519
|
+
error_obj = exc.args[0] if exc.args else None
|
|
3520
|
+
if error_obj and error_obj.code == ORACLE_DUPLICATE_KEY_ERROR:
|
|
3521
|
+
return False
|
|
3522
|
+
raise
|
|
3523
|
+
return True
|
|
3524
|
+
|
|
3525
|
+
def _search_entries_fts(
|
|
3526
|
+
self,
|
|
3527
|
+
query: str,
|
|
3528
|
+
app_name: str,
|
|
3529
|
+
user_id: str,
|
|
3530
|
+
limit: int,
|
|
3531
|
+
scope_filter: Literal["all", "user", "app"] = "all",
|
|
3532
|
+
*,
|
|
3533
|
+
fts_operator: str = "ACCUM",
|
|
3534
|
+
read: bool = False,
|
|
3535
|
+
) -> "list[StoredMemory]":
|
|
3536
|
+
sanitized_query = sanitize_oracle_text_query(query, operator=fts_operator)
|
|
3537
|
+
if not sanitized_query:
|
|
3538
|
+
return []
|
|
3539
|
+
where_scope, scope_params = _build_oracle_scope_where(app_name, user_id, scope_filter)
|
|
3540
|
+
embedding_column = "embedding" if _memory_supports_vectors(self._get_version_info()) else "NULL AS embedding"
|
|
3541
|
+
sql = f"""
|
|
3542
|
+
SELECT id, session_id, app_name, user_id, scope, event_id, author,
|
|
3543
|
+
timestamp, content_json, content_text, metadata_json, inserted_at, embedding
|
|
3544
|
+
FROM (
|
|
3545
|
+
SELECT id, session_id, app_name, user_id, scope, event_id, author,
|
|
3546
|
+
timestamp, content_json, content_text, metadata_json, inserted_at, {embedding_column},
|
|
3547
|
+
SCORE(1) AS score
|
|
3548
|
+
FROM {self._memory_table}
|
|
3549
|
+
WHERE {where_scope}
|
|
3550
|
+
AND CONTAINS(content_text, :query, 1) > 0
|
|
3551
|
+
ORDER BY score DESC, timestamp DESC
|
|
3552
|
+
)
|
|
3553
|
+
WHERE ROWNUM <= :limit
|
|
3554
|
+
"""
|
|
3555
|
+
params = {**scope_params, "query": sanitized_query, "limit": limit}
|
|
3556
|
+
with self._provide_replica_connection(primary=not read) as conn, conn.cursor() as cursor:
|
|
3557
|
+
cursor.execute(sql, params)
|
|
3558
|
+
rows = cursor.fetchall()
|
|
3559
|
+
return self._rows_to_records(rows)
|
|
3560
|
+
|
|
3561
|
+
def _search_entries_hybrid_rrf(
|
|
3562
|
+
self,
|
|
3563
|
+
query: str,
|
|
3564
|
+
embedding: "Sequence[float] | Any",
|
|
3565
|
+
app_name: str,
|
|
3566
|
+
user_id: str,
|
|
3567
|
+
limit: int,
|
|
3568
|
+
scope_filter: Literal["all", "user", "app"] = "all",
|
|
3569
|
+
*,
|
|
3570
|
+
vector_column: str = "embedding",
|
|
3571
|
+
rrf_k: int = 60,
|
|
3572
|
+
metric: str = "COSINE",
|
|
3573
|
+
fts_operator: str = "ACCUM",
|
|
3574
|
+
approximate: bool = True,
|
|
3575
|
+
read: bool = False,
|
|
3576
|
+
) -> "list[StoredMemory]":
|
|
3577
|
+
sanitized_query = sanitize_oracle_text_query(query, operator=fts_operator)
|
|
3578
|
+
where_scope, scope_params = _build_oracle_scope_where(app_name, user_id, scope_filter)
|
|
3579
|
+
select_cols = (
|
|
3580
|
+
"id",
|
|
3581
|
+
"session_id",
|
|
3582
|
+
"app_name",
|
|
3583
|
+
"user_id",
|
|
3584
|
+
"scope",
|
|
3585
|
+
"event_id",
|
|
3586
|
+
"author",
|
|
3587
|
+
"timestamp",
|
|
3588
|
+
"content_json",
|
|
3589
|
+
"content_text",
|
|
3590
|
+
"metadata_json",
|
|
3591
|
+
"inserted_at",
|
|
3592
|
+
"embedding",
|
|
3593
|
+
)
|
|
3594
|
+
if not sanitized_query:
|
|
3595
|
+
sql = build_oracle_hnsw_subquery(
|
|
3596
|
+
table=self._memory_table,
|
|
3597
|
+
vector_column=vector_column,
|
|
3598
|
+
select_columns=select_cols,
|
|
3599
|
+
vector_param="query_vector",
|
|
3600
|
+
top_k_param="top_k",
|
|
3601
|
+
metric=metric,
|
|
3602
|
+
approximate=approximate,
|
|
3603
|
+
where_clause=where_scope,
|
|
3604
|
+
)
|
|
3605
|
+
params: dict[str, Any] = {**scope_params, "query_vector": embedding, "top_k": limit}
|
|
3606
|
+
else:
|
|
3607
|
+
sql = build_oracle_hybrid_rrf_sql(
|
|
3608
|
+
table=self._memory_table,
|
|
3609
|
+
key_column="id",
|
|
3610
|
+
vector_column=vector_column,
|
|
3611
|
+
text_column="content_text",
|
|
3612
|
+
select_columns=select_cols,
|
|
3613
|
+
vector_param="query_vector",
|
|
3614
|
+
text_param="text_query",
|
|
3615
|
+
top_k_param="top_k",
|
|
3616
|
+
rrf_k_param="rrf_k",
|
|
3617
|
+
metric=metric,
|
|
3618
|
+
approximate=approximate,
|
|
3619
|
+
where_clause=where_scope,
|
|
3620
|
+
)
|
|
3621
|
+
params = {
|
|
3622
|
+
**scope_params,
|
|
3623
|
+
"query_vector": embedding,
|
|
3624
|
+
"text_query": sanitized_query,
|
|
3625
|
+
"top_k": limit,
|
|
3626
|
+
"rrf_k": rrf_k,
|
|
3627
|
+
}
|
|
3628
|
+
with self._provide_replica_connection(primary=not read) as conn, conn.cursor() as cursor:
|
|
3629
|
+
cursor.execute(sql, params)
|
|
3630
|
+
rows = cursor.fetchall()
|
|
3631
|
+
return self._rows_to_records(rows)
|
|
3632
|
+
|
|
3633
|
+
def _search_entries_simple(
|
|
3634
|
+
self,
|
|
3635
|
+
query: str,
|
|
3636
|
+
app_name: str,
|
|
3637
|
+
user_id: str,
|
|
3638
|
+
limit: int,
|
|
3639
|
+
scope_filter: Literal["all", "user", "app"] = "all",
|
|
3640
|
+
*,
|
|
3641
|
+
read: bool = False,
|
|
3642
|
+
) -> "list[StoredMemory]":
|
|
3643
|
+
where_scope, scope_params = _build_oracle_scope_where(app_name, user_id, scope_filter)
|
|
3644
|
+
embedding_column = "embedding" if _memory_supports_vectors(self._get_version_info()) else "NULL AS embedding"
|
|
3645
|
+
sql = f"""
|
|
3646
|
+
SELECT id, session_id, app_name, user_id, scope, event_id, author,
|
|
3647
|
+
timestamp, content_json, content_text, metadata_json, inserted_at, embedding
|
|
3648
|
+
FROM (
|
|
3649
|
+
SELECT id, session_id, app_name, user_id, scope, event_id, author,
|
|
3650
|
+
timestamp, content_json, content_text, metadata_json, inserted_at, {embedding_column}
|
|
3651
|
+
FROM {self._memory_table}
|
|
3652
|
+
WHERE {where_scope}
|
|
3653
|
+
AND LOWER(content_text) LIKE :pattern
|
|
3654
|
+
ORDER BY timestamp DESC
|
|
3655
|
+
)
|
|
3656
|
+
WHERE ROWNUM <= :limit
|
|
3657
|
+
"""
|
|
3658
|
+
pattern = f"%{query.lower()}%"
|
|
3659
|
+
params = {**scope_params, "pattern": pattern, "limit": limit}
|
|
3660
|
+
with self._provide_replica_connection(primary=not read) as conn, conn.cursor() as cursor:
|
|
3661
|
+
cursor.execute(sql, params)
|
|
3662
|
+
rows = cursor.fetchall()
|
|
3663
|
+
return self._rows_to_records(rows)
|
|
3664
|
+
|
|
3665
|
+
def _rows_to_records(self, rows: "list[Any]") -> "list[StoredMemory]":
|
|
3666
|
+
records: list[StoredMemory] = []
|
|
3667
|
+
for row in rows:
|
|
3668
|
+
content_json = self._deserialize_json_field(row[8]) if row[8] is not None else {}
|
|
3669
|
+
metadata_json = self._deserialize_json_field(row[10])
|
|
3670
|
+
content_text = row[9]
|
|
3671
|
+
if is_readable(content_text):
|
|
3672
|
+
content_text = _read_lob_sync(content_text)
|
|
3673
|
+
embedding_val = row[_EMBEDDING_COLUMN_INDEX] if len(row) > _EMBEDDING_COLUMN_INDEX else None
|
|
3674
|
+
embedding = [float(x) for x in embedding_val] if embedding_val is not None else None
|
|
3675
|
+
records.append({
|
|
3676
|
+
"id": row[0],
|
|
3677
|
+
"session_id": row[1],
|
|
3678
|
+
"app_name": row[2],
|
|
3679
|
+
"user_id": row[3],
|
|
3680
|
+
"scope": row[4],
|
|
3681
|
+
"event_id": row[5],
|
|
3682
|
+
"author": row[6],
|
|
3683
|
+
"timestamp": row[7],
|
|
3684
|
+
"content_json": cast("dict[str, Any]", content_json),
|
|
3685
|
+
"content_text": str(content_text),
|
|
3686
|
+
"metadata_json": metadata_json,
|
|
3687
|
+
"inserted_at": row[11],
|
|
3688
|
+
"embedding": embedding,
|
|
3689
|
+
})
|
|
3690
|
+
return records
|
|
3691
|
+
|
|
3692
|
+
|
|
3693
|
+
def _rollback_sync(conn: Any) -> None:
|
|
3694
|
+
with contextlib.suppress(Exception):
|
|
3695
|
+
conn.rollback()
|
|
3696
|
+
|
|
3697
|
+
|
|
3698
|
+
async def _rollback_async(conn: Any) -> None:
|
|
3699
|
+
with contextlib.suppress(Exception):
|
|
3700
|
+
await conn.rollback()
|
|
3701
|
+
|
|
3702
|
+
|
|
3703
|
+
def _memory_supports_vectors(version: "OracleVersionInfo | None") -> bool:
|
|
3704
|
+
if version is None or (version.major, version.minor, version.patch) < (23, 4, 0):
|
|
3705
|
+
return False
|
|
3706
|
+
compatible = parse_oracle_version_components(version.compatible) if version.compatible else None
|
|
3707
|
+
return compatible is None or compatible >= (23, 4, 0)
|
|
3708
|
+
|
|
3709
|
+
|
|
3710
|
+
def _require_memory_vectors(version: "OracleVersionInfo | None") -> None:
|
|
3711
|
+
if not _memory_supports_vectors(version):
|
|
3712
|
+
msg = "Oracle memory embeddings require Oracle Database and COMPATIBLE version 23.4 or later."
|
|
3713
|
+
raise ImproperConfigurationError(msg)
|
|
3714
|
+
|
|
3715
|
+
|
|
3716
|
+
def _memory_insert_count(entry_count: int, batch_errors: "list[Any]") -> int:
|
|
3717
|
+
for error in batch_errors:
|
|
3718
|
+
if error.code != ORACLE_DUPLICATE_KEY_ERROR:
|
|
3719
|
+
raise OracleDatabaseError(error)
|
|
3720
|
+
return entry_count - len(batch_errors)
|
|
3721
|
+
|
|
3722
|
+
|
|
3723
|
+
async def _prepare_oracle_schema_async(config: "OracleAsyncConfig", driver: Any) -> None:
|
|
3724
|
+
"""Resolve and cache the pool-scoped Oracle version on an asynchronous driver."""
|
|
3725
|
+
cache = config._oracle_version_cache
|
|
3726
|
+
if cache.resolved:
|
|
3727
|
+
return
|
|
3728
|
+
if getattr(driver, "_oracle_version_cache", None) is None:
|
|
3729
|
+
with contextlib.suppress(AttributeError):
|
|
3730
|
+
driver._oracle_version_cache = cache
|
|
3731
|
+
version_info = await driver.data_dictionary.get_version(driver)
|
|
3732
|
+
cache.resolved = True
|
|
3733
|
+
cache.version = version_info
|
|
3734
|
+
if version_info is None:
|
|
3735
|
+
logger.warning("Could not detect Oracle version, defaulting to BLOB_JSON storage")
|
|
3736
|
+
|
|
3737
|
+
|
|
3738
|
+
def _prepare_oracle_schema_sync(config: "OracleSyncConfig", driver: Any) -> None:
|
|
3739
|
+
"""Resolve and cache the pool-scoped Oracle version on a synchronous driver."""
|
|
3740
|
+
cache = config._oracle_version_cache
|
|
3741
|
+
if cache.resolved:
|
|
3742
|
+
return
|
|
3743
|
+
if getattr(driver, "_oracle_version_cache", None) is None:
|
|
3744
|
+
with contextlib.suppress(AttributeError):
|
|
3745
|
+
driver._oracle_version_cache = cache
|
|
3746
|
+
version_info = driver.data_dictionary.get_version(driver)
|
|
3747
|
+
cache.resolved = True
|
|
3748
|
+
cache.version = version_info
|
|
3749
|
+
if version_info is None:
|
|
3750
|
+
logger.warning("Could not detect Oracle version, defaulting to BLOB_JSON storage")
|
|
3751
|
+
|
|
3752
|
+
|
|
3753
|
+
async def _get_oracle_version_info_async(config: "OracleAsyncConfig") -> "OracleVersionInfo | None":
|
|
3754
|
+
"""Return the pool-scoped Oracle version through an asynchronous session."""
|
|
3755
|
+
cache = config._oracle_version_cache
|
|
3756
|
+
if cache.resolved:
|
|
3757
|
+
return cache.version
|
|
3758
|
+
async with config.provide_session() as driver:
|
|
3759
|
+
version_info = await driver.data_dictionary.get_version(driver)
|
|
3760
|
+
if version_info is None:
|
|
3761
|
+
logger.warning("Could not detect Oracle version, defaulting to BLOB_JSON storage")
|
|
3762
|
+
return version_info
|
|
3763
|
+
|
|
3764
|
+
|
|
3765
|
+
def _get_oracle_version_info_sync(config: "OracleSyncConfig") -> "OracleVersionInfo | None":
|
|
3766
|
+
"""Return the pool-scoped Oracle version through a synchronous session."""
|
|
3767
|
+
cache = config._oracle_version_cache
|
|
3768
|
+
if cache.resolved:
|
|
3769
|
+
return cache.version
|
|
3770
|
+
with config.provide_session() as driver:
|
|
3771
|
+
version_info = driver.data_dictionary.get_version(driver)
|
|
3772
|
+
if version_info is None:
|
|
3773
|
+
logger.warning("Could not detect Oracle version, defaulting to BLOB_JSON storage")
|
|
3774
|
+
return version_info
|
|
3775
|
+
|
|
3776
|
+
|
|
3777
|
+
def _validate_event_belongs_to_session(event_record: StoredEvent, app_name: str, user_id: str, session_id: str) -> None:
|
|
3778
|
+
record_scope = (event_record["app_name"], event_record["user_id"], event_record["session_id"])
|
|
3779
|
+
expected_scope = (app_name, user_id, session_id)
|
|
3780
|
+
if record_scope != expected_scope:
|
|
3781
|
+
msg = f"event_record scope {record_scope!r} does not match {expected_scope!r}"
|
|
3782
|
+
raise ValueError(msg)
|
|
3783
|
+
|
|
3784
|
+
|
|
3785
|
+
def _configure_oracle_adk_session_tables(store: Any, config: Any) -> None:
|
|
3786
|
+
"""Apply Oracle clean-break ADK table names independent of shared-base drift."""
|
|
3787
|
+
|
|
3788
|
+
adk_config = _adk_config(config)
|
|
3789
|
+
table_names = {
|
|
3790
|
+
"_session_table": str(adk_config.get("session_table") or ORACLE_DEFAULT_SESSION_TABLE),
|
|
3791
|
+
"_events_table": str(adk_config.get("events_table") or ORACLE_DEFAULT_EVENTS_TABLE),
|
|
3792
|
+
"_app_state_table": str(adk_config.get("app_state_table") or ORACLE_DEFAULT_APP_STATE_TABLE),
|
|
3793
|
+
"_user_state_table": str(adk_config.get("user_state_table") or ORACLE_DEFAULT_USER_STATE_TABLE),
|
|
3794
|
+
"_metadata_table": str(adk_config.get("metadata_table") or ORACLE_DEFAULT_METADATA_TABLE),
|
|
3795
|
+
}
|
|
3796
|
+
for attribute_name, table_name in table_names.items():
|
|
3797
|
+
validate_oracle_identifier(table_name, "table name")
|
|
3798
|
+
setattr(store, attribute_name, table_name)
|
|
3799
|
+
|
|
3800
|
+
|
|
3801
|
+
def _normalize_event_data_for_storage(event_data: Any) -> Any:
|
|
3802
|
+
"""Return event data without Google ADK-invalid durable ``actions: null``."""
|
|
3803
|
+
|
|
3804
|
+
if isinstance(event_data, dict) and event_data.get("actions") is None:
|
|
3805
|
+
normalized = dict(event_data)
|
|
3806
|
+
normalized.pop("actions", None)
|
|
3807
|
+
return normalized
|
|
3808
|
+
return event_data
|
|
3809
|
+
|
|
3810
|
+
|
|
3811
|
+
def _raise_session_not_found(session_id: str) -> NoReturn:
|
|
3812
|
+
msg = f"Session {session_id} not found during append_event_and_update_state."
|
|
3813
|
+
raise ValueError(msg)
|
|
3814
|
+
|
|
3815
|
+
|
|
3816
|
+
def _is_unique_violation(exc: BaseException) -> bool:
|
|
3817
|
+
if isinstance(exc, OracleDatabaseError):
|
|
3818
|
+
error_obj = exc.args[0] if exc.args else None
|
|
3819
|
+
if error_obj is not None and getattr(error_obj, "code", None) == ORACLE_DUPLICATE_KEY_ERROR:
|
|
3820
|
+
return True
|
|
3821
|
+
return "ORA-00001" in str(exc)
|
|
3822
|
+
|
|
3823
|
+
|
|
3824
|
+
def _raise_session_not_found_for_user(session_id: str, app_name: str, user_id: str) -> NoReturn:
|
|
3825
|
+
msg = f"Session {session_id} not found for app={app_name!r}, user={user_id!r}"
|
|
3826
|
+
raise ValueError(msg)
|
|
3827
|
+
|
|
3828
|
+
|
|
3829
|
+
_normalize_decimals = build_nested_decimal_normalizer(mode="float")
|
|
3830
|
+
|
|
3831
|
+
|
|
3832
|
+
def _coerce_decimal_values(value: Any) -> Any:
|
|
3833
|
+
return _normalize_decimals(value)
|
|
3834
|
+
|
|
3835
|
+
|
|
3836
|
+
def _oracle_text_value(value: Any) -> str:
|
|
3837
|
+
"""Normalize Oracle VARCHAR2 values back to Python strings.
|
|
3838
|
+
|
|
3839
|
+
Oracle stores empty strings as ``NULL``. The ADK event contract allows
|
|
3840
|
+
empty strings for fields like ``invocation_id``, so reads coerce ``NULL``
|
|
3841
|
+
back to ``""``.
|
|
3842
|
+
"""
|
|
3843
|
+
return "" if value is None else str(value)
|
|
3844
|
+
|
|
3845
|
+
|
|
3846
|
+
def _extract_json_value(data: Any) -> "dict[str, Any]":
|
|
3847
|
+
"""Extract and coerce a dictionary from a database JSON payload."""
|
|
3848
|
+
if isinstance(data, dict):
|
|
3849
|
+
return cast("dict[str, Any]", coerce_decimal_values(data))
|
|
3850
|
+
if isinstance(data, bytes):
|
|
3851
|
+
return cast("dict[str, Any]", from_json(data))
|
|
3852
|
+
if isinstance(data, str):
|
|
3853
|
+
return cast("dict[str, Any]", from_json(data))
|
|
3854
|
+
return cast("dict[str, Any]", from_json(str(data)))
|
|
3855
|
+
|
|
3856
|
+
|
|
3857
|
+
def _event_data_column_ddl(storage_type: JSONStorageType) -> str:
|
|
3858
|
+
"""Return the DDL fragment for the event_data column."""
|
|
3859
|
+
if storage_type == JSONStorageType.JSON_NATIVE:
|
|
3860
|
+
return "event_data JSON NOT NULL"
|
|
3861
|
+
if storage_type == JSONStorageType.BLOB_JSON:
|
|
3862
|
+
return "event_data BLOB CHECK (event_data IS JSON) NOT NULL"
|
|
3863
|
+
return "event_data BLOB NOT NULL"
|
|
3864
|
+
|
|
3865
|
+
|
|
3866
|
+
def _json_column_ddl(column_name: str, storage_type: JSONStorageType) -> str:
|
|
3867
|
+
"""Return an Oracle JSON column DDL fragment for the configured storage type."""
|
|
3868
|
+
if storage_type == JSONStorageType.JSON_NATIVE:
|
|
3869
|
+
return _ADK_JSON_COLUMN_DDL_TEMPLATE.format(column_name)
|
|
3870
|
+
if storage_type == JSONStorageType.BLOB_JSON:
|
|
3871
|
+
return _ADK_JSON_COLUMN_DDL_TEMPLATE_2.format(column_name, column_name)
|
|
3872
|
+
return _ADK_JSON_COLUMN_DDL_TEMPLATE_3.format(column_name)
|
|
3873
|
+
|
|
3874
|
+
|
|
3875
|
+
def _bare_table_name(name: str) -> str:
|
|
3876
|
+
"""Return the case-folded, schema-stripped table name for membership checks."""
|
|
3877
|
+
return name.rsplit(".", 1)[-1].casefold()
|
|
3878
|
+
|
|
3879
|
+
|
|
3880
|
+
def _existing_table_names(rows: "list[Any]") -> "set[str]":
|
|
3881
|
+
"""Collapse ``data_dictionary.get_tables`` rows into a case-folded name set."""
|
|
3882
|
+
return {str(row.get("table_name", "")).rsplit(".", 1)[-1].casefold() for row in rows}
|
|
3883
|
+
|
|
3884
|
+
|
|
3885
|
+
def _adk_config(config: Any) -> OracleADKConfig:
|
|
3886
|
+
extension_config = getattr(config, "extension_config", {})
|
|
3887
|
+
if not isinstance(extension_config, dict):
|
|
3888
|
+
return {}
|
|
3889
|
+
adk_config = extension_config.get("adk", {})
|
|
3890
|
+
if isinstance(adk_config, dict):
|
|
3891
|
+
return cast("OracleADKConfig", adk_config)
|
|
3892
|
+
return {}
|
|
3893
|
+
|
|
3894
|
+
|
|
3895
|
+
def _adk_table_feature_clause(
|
|
3896
|
+
config: Any, table_kind: str, *, in_memory: bool, hash_partition_key: str, range_partition_key: str
|
|
3897
|
+
) -> str:
|
|
3898
|
+
report = oracle_table_feature_report(
|
|
3899
|
+
config,
|
|
3900
|
+
"adk",
|
|
3901
|
+
_adk_config(config),
|
|
3902
|
+
table_kind,
|
|
3903
|
+
in_memory=in_memory,
|
|
3904
|
+
hash_partition_key=hash_partition_key,
|
|
3905
|
+
range_partition_key=range_partition_key,
|
|
3906
|
+
)
|
|
3907
|
+
return report["plsql_clause"]
|
|
3908
|
+
|
|
3909
|
+
|
|
3910
|
+
async def _read_lob_async(data: Any) -> Any:
|
|
3911
|
+
return await read_oracle_lob_async(data)
|
|
3912
|
+
|
|
3913
|
+
|
|
3914
|
+
def _read_lob_sync(data: Any) -> Any:
|
|
3915
|
+
return read_oracle_lob_sync(data)
|
|
3916
|
+
|
|
3917
|
+
|
|
3918
|
+
def _build_oracle_scope_where(
|
|
3919
|
+
app_name: str, user_id: str, scope_filter: Literal["all", "user", "app"]
|
|
3920
|
+
) -> tuple[str, dict[str, Any]]:
|
|
3921
|
+
if scope_filter == "all":
|
|
3922
|
+
return "app_name = :app_name AND ((scope = 'user' AND user_id = :user_id) OR scope = 'app')", {
|
|
3923
|
+
"app_name": app_name,
|
|
3924
|
+
"user_id": user_id,
|
|
3925
|
+
}
|
|
3926
|
+
if scope_filter == "user":
|
|
3927
|
+
return "app_name = :app_name AND scope = 'user' AND user_id = :user_id", {
|
|
3928
|
+
"app_name": app_name,
|
|
3929
|
+
"user_id": user_id,
|
|
3930
|
+
}
|
|
3931
|
+
return "app_name = :app_name AND scope = 'app'", {"app_name": app_name}
|
|
3932
|
+
|
|
3933
|
+
|
|
3934
|
+
def _session_list_query(
|
|
3935
|
+
session_table: str,
|
|
3936
|
+
app_name: str,
|
|
3937
|
+
user_id: "str | None",
|
|
3938
|
+
column: str,
|
|
3939
|
+
direction: str,
|
|
3940
|
+
limit: "int | None",
|
|
3941
|
+
offset: int,
|
|
3942
|
+
) -> "tuple[str, dict[str, Any]]":
|
|
3943
|
+
"""Return the bounded session-list query and its named binds."""
|
|
3944
|
+
params: dict[str, Any] = {"app_name": app_name}
|
|
3945
|
+
where_clause = "app_name = :app_name"
|
|
3946
|
+
if user_id is not None:
|
|
3947
|
+
params["user_id"] = user_id
|
|
3948
|
+
where_clause = f"{where_clause} AND user_id = :user_id"
|
|
3949
|
+
|
|
3950
|
+
page_clause = ""
|
|
3951
|
+
if limit is not None:
|
|
3952
|
+
params["page_limit"] = limit
|
|
3953
|
+
params["page_offset"] = offset
|
|
3954
|
+
page_clause = "\n OFFSET :page_offset ROWS FETCH NEXT :page_limit ROWS ONLY"
|
|
3955
|
+
|
|
3956
|
+
sql = f"""
|
|
3957
|
+
SELECT id, app_name, user_id, state, create_time, update_time
|
|
3958
|
+
FROM {session_table}
|
|
3959
|
+
WHERE {where_clause}
|
|
3960
|
+
ORDER BY {column} {direction}, id {direction}{page_clause}
|
|
3961
|
+
"""
|
|
3962
|
+
return sql, params
|
|
3963
|
+
|
|
3964
|
+
|
|
3965
|
+
def _oracle_session_index_specs(table: str) -> list[tuple[str, str, str]]:
|
|
3966
|
+
"""Return companion index specifications for the Oracle ADK sessions table.
|
|
3967
|
+
|
|
3968
|
+
Args:
|
|
3969
|
+
table: Unqualified Oracle sessions table name.
|
|
3970
|
+
|
|
3971
|
+
Returns:
|
|
3972
|
+
Ordered ``(index_name, table_name, definition)`` tuples.
|
|
3973
|
+
"""
|
|
3974
|
+
return [
|
|
3975
|
+
(f"idx_{table}_app_user", table, "(app_name, user_id)"),
|
|
3976
|
+
(f"idx_{table}_update_time", table, "(update_time DESC)"),
|
|
3977
|
+
]
|
|
3978
|
+
|
|
3979
|
+
|
|
3980
|
+
def _oracle_event_index_specs(table: str) -> list[tuple[str, str, str]]:
|
|
3981
|
+
"""Return companion index specifications for the Oracle ADK events table.
|
|
3982
|
+
|
|
3983
|
+
Args:
|
|
3984
|
+
table: Unqualified Oracle events table name.
|
|
3985
|
+
|
|
3986
|
+
Returns:
|
|
3987
|
+
Ordered ``(index_name, table_name, definition)`` tuples.
|
|
3988
|
+
"""
|
|
3989
|
+
return [
|
|
3990
|
+
(f"idx_{table}_session", table, "(session_id, timestamp ASC)"),
|
|
3991
|
+
(f"idx_{table}_invocation", table, "(invocation_id)"),
|
|
3992
|
+
(f"idx_{table}_timestamp", table, "(timestamp ASC)"),
|
|
3993
|
+
(f"idx_{table}_app_timestamp", table, "(app_name, timestamp ASC)"),
|
|
3994
|
+
]
|
|
3995
|
+
|
|
3996
|
+
|
|
3997
|
+
def _oracle_memory_index_specs(table: str, *, use_fts: bool) -> list[tuple[str, str, str]]:
|
|
3998
|
+
"""Return companion index specifications for the Oracle ADK memory table.
|
|
3999
|
+
|
|
4000
|
+
Args:
|
|
4001
|
+
table: Unqualified Oracle memory table name.
|
|
4002
|
+
use_fts: Whether Oracle Text ``CTXSYS.CONTEXT`` FTS indexing is enabled.
|
|
4003
|
+
|
|
4004
|
+
Returns:
|
|
4005
|
+
Ordered ``(index_name, table_name, definition)`` tuples.
|
|
4006
|
+
"""
|
|
4007
|
+
specs: list[tuple[str, str, str]] = [
|
|
4008
|
+
(f"idx_{table}_app_scope_user_time", table, "(app_name, scope, user_id, timestamp DESC)"),
|
|
4009
|
+
(f"idx_{table}_scope", table, "(app_name, scope)"),
|
|
4010
|
+
(f"idx_{table}_session", table, "(session_id)"),
|
|
4011
|
+
]
|
|
4012
|
+
if use_fts:
|
|
4013
|
+
specs.append((f"idx_{table}_fts", table, "(content_text) INDEXTYPE IS CTXSYS.CONTEXT"))
|
|
4014
|
+
return specs
|
|
4015
|
+
|
|
4016
|
+
|
|
4017
|
+
def _oracle_missing_index_statements(specs: list[tuple[str, str, str]], rows: list[Any]) -> list[str]:
|
|
4018
|
+
"""Filter Oracle companion index specs against schema-scoped catalog metadata.
|
|
4019
|
+
|
|
4020
|
+
Args:
|
|
4021
|
+
specs: Ordered ``(index_name, table_name, definition)`` tuples.
|
|
4022
|
+
rows: Rows returned by ``data_dictionary.get_indexes(driver)``.
|
|
4023
|
+
|
|
4024
|
+
Returns:
|
|
4025
|
+
Bare ``CREATE INDEX`` statements for missing ``(table_name, index_name)`` pairs.
|
|
4026
|
+
"""
|
|
4027
|
+
return [
|
|
4028
|
+
f"CREATE INDEX {index_name} ON {table}{definition}"
|
|
4029
|
+
for index_name, table, definition in specs
|
|
4030
|
+
if not metadata_has_index(rows, table, index_name)
|
|
4031
|
+
]
|