Title: Execute statement and execute · Issue #17 · stackql/stackqljs · GitHub
Open Graph Title: Execute statement and execute · Issue #17 · stackql/stackqljs
X Title: Execute statement and execute · Issue #17 · stackql/stackqljs
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 ...
Open Graph 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 muta...
X 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...
Opengraph URL: https://github.com/stackql/stackqljs/issues/17
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Execute statement and execute","articleBody":"```\n\tdef executeStmt(self, query):\n\t\t\"\"\"Executes a query using the StackQL instance and returns the output as a string. \n\t\t\tThis is intended for operations which do not return a result set, for example a mutation \n\t\t\toperation such as an `INSERT` or a `DELETE` or life cycle method such as an `EXEC` operation\n\t\t\tor a `REGISTRY PULL` operation.\n\n\t\tThis method determines the mode of operation (server_mode or local execution) based \n\t\ton the `server_mode` attribute of the instance. If `server_mode` is True, it runs the query \n\t\tagainst the server. Otherwise, it executes the query using a subprocess.\n\n\t\t:param query: The StackQL query string to be executed.\n\t\t:type query: str, list of dict objects, or Pandas DataFrame\n\n\t\t:return: The output result of the query in string format. If in `server_mode`, it \n\t\t\t\treturns a JSON string representation of the result. \n\t\t:rtype: dict, Pandas DataFrame or str (for `csv` output)\n\n\t\tExample:\n\t\t\t\u003e\u003e\u003e from pystackql import StackQL\n\t\t\t\u003e\u003e\u003e stackql = StackQL()\n\t\t\t\u003e\u003e\u003e stackql_query = \"REGISTRY PULL okta\"\n\t\t\t\u003e\u003e\u003e result = stackql.executeStmt(stackql_query)\n\t\t\t\u003e\u003e\u003e result\n\t\t\"\"\"\n\t\tif self.server_mode:\n\t\t\t# Use server mode\n\t\t\tresult = self._run_server_query(query, True)\n\t\t\tif self.output == 'pandas':\n\t\t\t\treturn pd.DataFrame(result)\n\t\t\telif self.output == 'csv':\n\t\t\t\t# return the string representation of the result\n\t\t\t\treturn result[0]['message']\n\t\t\telse:\n\t\t\t\treturn result\n\t\telse:\n\t\t\tresult_msg = self._run_query(query)\n\t\t\tif self.output == 'pandas':\n\t\t\t\treturn pd.DataFrame({'message': [result_msg]})\n\t\t\telif self.output == 'csv':\n\t\t\t\treturn result_msg\n\t\t\telse:\n\t\t\t\treturn [{'message': result_msg}]\t\t\t\n\t\n\tdef execute(self, query):\n\t\t\"\"\"Executes a query using the StackQL instance and returns the output \n\t\tin the format specified by the `output` attribute.\n\n\t\tDepending on the `server_mode` and `output` attribute of the instance, \n\t\tthis method either runs the query against the StackQL server or executes \n\t\tit locally using a subprocess, returning the data in a dictionary, Pandas \n\t\tDataFrame, or CSV format.\n\n\t\t:param query: The StackQL query string to be executed.\n\t\t:type query: str\n\n\t\t:return: The output result of the query. Depending on the `output` attribute, \n\t\t\t\tthe result can be a dictionary, a Pandas DataFrame, or a raw CSV string.\n\t\t\t\tCSV output is currently not supported in `server_mode`.\n\t\t:rtype: dict, pd.DataFrame, or str\n\n\t\tExample:\n\t\t\t\u003e\u003e\u003e from pystackql import StackQL\n\t\t\t\u003e\u003e\u003e stackql = StackQL()\n\t\t\t\u003e\u003e\u003e stackql_query = \\\"\\\"\\\"SELECT SPLIT_PART(machineType, '/', -1) as machine_type, \n\t\t\t... status, COUNT(*) as num_instances\n\t\t\t... FROM google.compute.instances \n\t\t\t... WHERE project = 'stackql-demo' \n\t\t\t... AND zone = 'australia-southeast1-a'\n\t\t\t... GROUP BY machine_type, status\n\t\t\t... HAVING COUNT(*) \u003e 2\\\"\\\"\\\"\n\t\t\t\u003e\u003e\u003e result = stackql.execute(stackql_query)\n\t\t\"\"\"\n\t\tif self.server_mode:\n\t\t\t# Use server mode\n\t\t\tresult = self._run_server_query(query)\n\t\t\t\n\t\t\tif self.output == 'pandas':\n\t\t\t\tjson_str = json.dumps(result)\n\t\t\t\treturn pd.read_json(StringIO(json_str))\n\t\t\telif self.output == 'csv':\n\t\t\t\traise ValueError(\"CSV output is not supported in server_mode.\")\n\t\t\telse: # Assume 'dict' output\n\t\t\t\treturn result\n\t\t\t\n\t\telse:\n\t\t\t# Local mode handling (existing logic)\n\t\t\toutput = self._run_query(query)\n\t\t\tif self.output == 'csv':\n\t\t\t\treturn output\n\t\t\telif self.output == 'pandas':\n\t\t\t\ttry:\n\t\t\t\t\treturn pd.read_json(StringIO(output))\n\t\t\t\texcept ValueError:\n\t\t\t\t\treturn pd.DataFrame([{\"error\": \"Invalid JSON output: {}\".format(output.strip())}])\n\t\t\telse: # Assume 'dict' output\n\t\t\t\ttry:\n\t\t\t\t\treturn json.loads(output)\n\t\t\t\texcept ValueError:\n\t\t\t\t\treturn [{\"error\": \"Invalid JSON output: {}\".format(output.strip())}]\n```","author":{"url":"https://github.com/yunchengyang515","@type":"Person","name":"yunchengyang515"},"datePublished":"2023-12-09T04:15:01.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/17/stackqljs/issues/17"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0b56d3af-d16c-26e2-78b9-0a90ad47b6e6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CE56:3E5F9F:F1C411:1502362:6A4DF1F5 |
| html-safe-nonce | 4b759f2198b8844cabdd719adbfa0a7582d01de756c0b2dc4b2e98b2b88a1ff6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRTU2OjNFNUY5RjpGMUM0MTE6MTUwMjM2Mjo2QTRERjFGNSIsInZpc2l0b3JfaWQiOiI3ODAyNDMzMjYxODE3MTY0Mjc3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | dc1164f2fab288523c1db8088390ef8ee3740523c08f1745e3b55dca6192ab97 |
| hovercard-subject-tag | issue:2033599887 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/stackql/stackqljs/17/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fa89f730dff4ee9b21c64efa57d706d407a5da68b0018e7d18969b8aa9b0417c/stackql/stackqljs/issues/17 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fa89f730dff4ee9b21c64efa57d706d407a5da68b0018e7d18969b8aa9b0417c/stackql/stackqljs/issues/17 |
| og:image:alt | 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 muta... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yunchengyang515 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5818716c93c6a2925b815402541a32814e43a7b1261c322b0c2df75224289566 |
| turbo-cache-control | no-preview |
| go-import | github.com/stackql/stackqljs git https://github.com/stackql/stackqljs.git |
| octolytics-dimension-user_id | 95105302 |
| octolytics-dimension-user_login | stackql |
| octolytics-dimension-repository_id | 711717177 |
| octolytics-dimension-repository_nwo | stackql/stackqljs |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 711717177 |
| octolytics-dimension-repository_network_root_nwo | stackql/stackqljs |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f4bb89367ca678f057d79b1abc45d6675b1bd5b2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width