1414
1515from __future__ import annotations
1616
17+ import functools
18+ import textwrap
19+ import types
20+ from typing import Callable
21+
1722from google .auth .credentials import Credentials
1823
1924from . import utils
2025from ..tool_context import ToolContext
26+ from .settings import QueryResultMode
2127from .settings import SpannerToolSettings
2228
2329
@@ -49,16 +55,29 @@ def execute_sql(
4955 query not returned in the result.
5056
5157 Examples:
52- Fetch data or insights from a table:
58+ <Example>
59+ >>> execute_sql("my_project", "my_instance", "my_database",
60+ ... "SELECT COUNT(*) AS count FROM my_table")
61+ {
62+ "status": "SUCCESS",
63+ "rows": [
64+ [100]
65+ ]
66+ }
67+ </Example>
5368
54- >>> execute_sql("my_project", "my_instance", "my_database",
55- ... "SELECT COUNT(*) AS count FROM my_table")
56- {
57- "status": "SUCCESS",
58- "rows": [
59- [100]
60- ]
61- }
69+ <Example>
70+ >>> execute_sql("my_project", "my_instance", "my_database",
71+ ... "SELECT name, rating, description FROM hotels_table")
72+ {
73+ "status": "SUCCESS",
74+ "rows": [
75+ ["The Hotel", 4.1, "Modern hotel."],
76+ ["Park Inn", 4.5, "Cozy hotel."],
77+ ...
78+ ]
79+ }
80+ </Example>
6281
6382 Note:
6483 This is running with Read-Only Transaction for query that only read data.
@@ -72,3 +91,105 @@ def execute_sql(
7291 settings ,
7392 tool_context ,
7493 )
94+
95+
96+ _EXECUTE_SQL_DICT_LIST_MODE_DOCSTRING = textwrap .dedent ("""\
97+ Run a Spanner Read-Only query in the spanner database and return the result.
98+
99+ Args:
100+ project_id (str): The GCP project id in which the spanner database
101+ resides.
102+ instance_id (str): The instance id of the spanner database.
103+ database_id (str): The database id of the spanner database.
104+ query (str): The Spanner SQL query to be executed.
105+ credentials (Credentials): The credentials to use for the request.
106+ settings (SpannerToolSettings): The settings for the tool.
107+ tool_context (ToolContext): The context for the tool.
108+
109+ Returns:
110+ dict: Dictionary with the result of the query.
111+ If the result contains the key "result_is_likely_truncated" with
112+ value True, it means that there may be additional rows matching the
113+ query not returned in the result.
114+
115+ Examples:
116+ <Example>
117+ >>> execute_sql("my_project", "my_instance", "my_database",
118+ ... "SELECT COUNT(*) AS count FROM my_table")
119+ {
120+ "status": "SUCCESS",
121+ "rows": [
122+ {
123+ "count": 100
124+ }
125+ ]
126+ }
127+ </Example>
128+
129+ <Example>
130+ >>> execute_sql("my_project", "my_instance", "my_database",
131+ ... "SELECT COUNT(*) FROM my_table")
132+ {
133+ "status": "SUCCESS",
134+ "rows": [
135+ {
136+ "": 100
137+ }
138+ ]
139+ }
140+ </Example>
141+
142+ <Example>
143+ >>> execute_sql("my_project", "my_instance", "my_database",
144+ ... "SELECT name, rating, description FROM hotels_table")
145+ {
146+ "status": "SUCCESS",
147+ "rows": [
148+ {
149+ "name": "The Hotel",
150+ "rating": 4.1,
151+ "description": "Modern hotel."
152+ },
153+ {
154+ "name": "Park Inn",
155+ "rating": 4.5,
156+ "description": "Cozy hotel."
157+ },
158+ ...
159+ ]
160+ }
161+ </Example>
162+
163+ Note:
164+ This is running with Read-Only Transaction for query that only read data.
165+ """ )
166+
167+
168+ def get_execute_sql (settings : SpannerToolSettings ) -> Callable [..., dict ]:
169+ """Get the execute_sql tool customized as per the given tool settings.
170+
171+ Args:
172+ settings: Spanner tool settings indicating the behavior of the execute_sql
173+ tool.
174+
175+ Returns:
176+ callable[..., dict]: A version of the execute_sql tool respecting the tool
177+ settings.
178+ """
179+
180+ if settings and settings .query_result_mode is QueryResultMode .DICT_LIST :
181+
182+ execute_sql_wrapper = types .FunctionType (
183+ execute_sql .__code__ ,
184+ execute_sql .__globals__ ,
185+ execute_sql .__name__ ,
186+ execute_sql .__defaults__ ,
187+ execute_sql .__closure__ ,
188+ )
189+ functools .update_wrapper (execute_sql_wrapper , execute_sql )
190+ # Update with the new docstring
191+ execute_sql_wrapper .__doc__ = _EXECUTE_SQL_DICT_LIST_MODE_DOCSTRING
192+ return execute_sql_wrapper
193+
194+ # Return the default execute_sql function.
195+ return execute_sql
0 commit comments