-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Milestone
Description
def executeStmt(self, query):
"""Executes a query using the StackQL instance and returns the output as a string.
This is intended for operations which do not return a result set, for example a mutation
operation such as an `INSERT` or a `DELETE` or life cycle method such as an `EXEC` operation
or a `REGISTRY PULL` operation.
This method determines the mode of operation (server_mode or local execution) based
on the `server_mode` attribute of the instance. If `server_mode` is True, it runs the query
against the server. Otherwise, it executes the query using a subprocess.
:param query: The StackQL query string to be executed.
:type query: str, list of dict objects, or Pandas DataFrame
:return: The output result of the query in string format. If in `server_mode`, it
returns a JSON string representation of the result.
:rtype: dict, Pandas DataFrame or str (for `csv` output)
Example:
>>> from pystackql import StackQL
>>> stackql = StackQL()
>>> stackql_query = "REGISTRY PULL okta"
>>> result = stackql.executeStmt(stackql_query)
>>> result
"""
if self.server_mode:
# Use server mode
result = self._run_server_query(query, True)
if self.output == 'pandas':
return pd.DataFrame(result)
elif self.output == 'csv':
# return the string representation of the result
return result[0]['message']
else:
return result
else:
result_msg = self._run_query(query)
if self.output == 'pandas':
return pd.DataFrame({'message': [result_msg]})
elif self.output == 'csv':
return result_msg
else:
return [{'message': result_msg}]
def execute(self, query):
"""Executes a query using the StackQL instance and returns the output
in the format specified by the `output` attribute.
Depending on the `server_mode` and `output` attribute of the instance,
this method either runs the query against the StackQL server or executes
it locally using a subprocess, returning the data in a dictionary, Pandas
DataFrame, or CSV format.
:param query: The StackQL query string to be executed.
:type query: str
:return: The output result of the query. Depending on the `output` attribute,
the result can be a dictionary, a Pandas DataFrame, or a raw CSV string.
CSV output is currently not supported in `server_mode`.
:rtype: dict, pd.DataFrame, or str
Example:
>>> from pystackql import StackQL
>>> stackql = StackQL()
>>> stackql_query = \"\"\"SELECT SPLIT_PART(machineType, '/', -1) as machine_type,
... status, COUNT(*) as num_instances
... FROM google.compute.instances
... WHERE project = 'stackql-demo'
... AND zone = 'australia-southeast1-a'
... GROUP BY machine_type, status
... HAVING COUNT(*) > 2\"\"\"
>>> result = stackql.execute(stackql_query)
"""
if self.server_mode:
# Use server mode
result = self._run_server_query(query)
if self.output == 'pandas':
json_str = json.dumps(result)
return pd.read_json(StringIO(json_str))
elif self.output == 'csv':
raise ValueError("CSV output is not supported in server_mode.")
else: # Assume 'dict' output
return result
else:
# Local mode handling (existing logic)
output = self._run_query(query)
if self.output == 'csv':
return output
elif self.output == 'pandas':
try:
return pd.read_json(StringIO(output))
except ValueError:
return pd.DataFrame([{"error": "Invalid JSON output: {}".format(output.strip())}])
else: # Assume 'dict' output
try:
return json.loads(output)
except ValueError:
return [{"error": "Invalid JSON output: {}".format(output.strip())}]
Metadata
Metadata
Assignees
Labels
No labels