You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"""A class representing an instance of the StackQL query engine.
19
-
20
-
:param platform: the operating system platform (read only)
21
-
:type platform: str
22
-
23
-
:param parse_json: whether to parse the output as JSON, defaults to `False`
24
-
unless overridden by setting `output` to `csv`, `table` or `text` as a `kwarg` in the `StackQL` object constructor (read only)
25
-
:type parse_json: bool
26
-
27
-
:param params: a list of command-line parameters passed to the StackQL executable, populated by the class constructor (read only)
28
-
:type params: list
29
-
30
-
:param download_dir: the download directory for the StackQL executable - defaults to site.getuserbase() unless overridden in the `StackQL` object constructor (read only)
31
-
:type download_dir: str
32
-
33
-
:param bin_path: the full path of the StackQL executable (read only)
34
-
:type bin_path: str
35
-
36
-
:param version: the version number of the StackQL executable (read only)
37
-
:type version: str
38
-
39
-
:param package_version: the version number of the pystackql Python package (read only)
40
-
:type package_version: str
41
-
42
-
:param sha: the commit (short) sha for the installed `stackql` binary build (read only)
43
-
:type sha: str
44
-
45
-
:param auth: StackQL provider authentication object supplied using the class constructor (read only)
46
-
:type auth: dict
47
-
48
-
:param server_mode: Connect to a stackql server - defaults to `False` unless overridden in the `StackQL` object constructor (read only)
49
-
:type server_mode: bool
50
-
51
-
:param server_address: The address of the stackql server - defaults to `0.0.0.0` unless overridden in the `StackQL` object constructor (read only), only used if `server_mode` (read only)
52
-
:type auth: str
53
-
54
-
:param server_port: The port of the stackql server - defaults to `5466` unless overridden in the `StackQL` object constructor (read only), only used if `server_mode` (read only)
55
-
:type auth: int
56
-
"""
57
-
58
-
def_connect_to_server(self):
59
-
"""Establishes a connection to the server using psycopg.
60
-
61
-
Returns:
62
-
Connection object if successful, or None if an error occurred.
63
-
"""
64
-
try:
65
-
conn=psycopg2.connect(
66
-
dbname='stackql',
67
-
user='stackql',
68
-
host=self.server_address,
69
-
port=self.server_port
70
-
)
71
-
returnconn
72
-
exceptpsycopg2.OperationalErrorasoe:
73
-
print(f"OperationalError while connecting to the server: {oe}")
74
-
exceptExceptionase:
75
-
# Catching all other possible psycopg2 exceptions (and possibly other unexpected exceptions).
76
-
# You might want to log this or handle it differently in a real-world scenario.
77
-
print(f"Unexpected error while connecting to the server: {e}")
78
-
returnNone
79
-
80
-
def_run_server_query(self, query):
81
-
"""
82
-
Runs a query against the server using psycopg2.
83
-
84
-
:param query: SQL query to be executed on the server.
85
-
:type query: str
86
-
:return: List of result rows if the query fetches results; empty list if there are no results.
87
-
:rtype: list
88
-
:raises: psycopg2.ProgrammingError for issues related to the SQL query,
89
-
unless the error is "no results to fetch", in which case an empty list is returned.
90
-
"""
91
-
conn=self._connect_to_server()
92
-
try:
93
-
cur=conn.cursor(cursor_factory=RealDictCursor)
94
-
cur.execute(query)
95
-
rows=cur.fetchall()
96
-
cur.close()
97
-
returnrows
98
-
exceptpsycopg2.ProgrammingErrorase:
99
-
ifstr(e) =="no results to fetch":
100
-
return []
101
-
else:
102
-
raise
103
-
104
-
def_run_query(self, query):
105
-
"""
106
-
Internal method to execute a StackQL query using a subprocess.
107
-
108
-
The method spawns a subprocess to run the StackQL binary with the specified query and parameters.
109
-
It waits for the subprocess to complete and captures its stdout as the output. This approach ensures
110
-
that resources like pipes are properly cleaned up after the subprocess completes.
111
-
112
-
:param query: The StackQL query string to be executed.
113
-
:type query: str
114
-
115
-
:return: The output result of the query, which can either be the actual query result or an error message.
116
-
:rtype: str
117
-
118
-
Possible error messages include:
119
-
- Indications that the StackQL binary wasn't found.
120
-
- Generic error messages for other exceptions encountered during the query execution.
121
-
122
-
:raises FileNotFoundError: If the StackQL binary isn't found.
123
-
:raises Exception: For any other exceptions during the execution, providing a generic error message.
# Optionally handle other types, or raise an error.
317
-
pass
318
-
319
-
returncombined
320
-
321
-
defexecuteQueriesAsync(self, queries):
322
-
"""
323
-
Executes multiple StackQL queries asynchronously using the current StackQL instance.
324
-
325
-
This method utilizes an asyncio event loop to concurrently run a list of provided
326
-
StackQL queries. Each query is executed independently, and the combined results of
327
-
all the queries are returned as a list of JSON objects.
328
-
329
-
Note: The order of the results in the returned list may not necessarily correspond
330
-
to the order of the queries in the input list due to the asynchronous nature of execution.
331
-
332
-
:param queries: A list of StackQL query strings to be executed concurrently.
333
-
:type queries: list[str], required
334
-
335
-
:return: A list of results corresponding to each query. Each result is a JSON object.
336
-
:rtype: list[dict]
337
-
338
-
Example:
339
-
>>> queries = [
340
-
>>> "SELECT '%s' as region, instanceType, COUNT(*) as num_instances FROM aws.ec2.instances WHERE region = '%s' GROUP BY instanceType" % (region, region)
0 commit comments