11import json
22import logging
3+ import textwrap
34import threading
45import uuid
56from dataclasses import dataclass
1819 ExecutionState ,
1920 ResultsFormat ,
2021 DataCompression ,
22+ GeometryRepresentation ,
2123)
2224from wherobots .db .cursor import Cursor
2325from wherobots .db .errors import NotSupportedError , OperationalError
2426
25- _DEFAULT_RESULTS_FORMAT = ResultsFormat .ARROW
26- _DEFAULT_DATA_COMPRESSION = DataCompression .BROTLI
27-
2827
2928@dataclass
3029class Query :
@@ -53,9 +52,16 @@ def __init__(
5352 self ,
5453 ws : websockets .sync .client .ClientConnection ,
5554 read_timeout : float = DEFAULT_READ_TIMEOUT_SECONDS ,
55+ results_format : ResultsFormat | None = None ,
56+ data_compression : DataCompression | None = None ,
57+ geometry_representation : GeometryRepresentation | None = None ,
5658 ):
5759 self .__ws = ws
5860 self .__read_timeout = read_timeout
61+ self .__results_format = results_format
62+ self .__data_compression = data_compression
63+ self .__geometry_representation = geometry_representation
64+
5965 self .__queries : dict [str , Query ] = {}
6066 self .__thread = threading .Thread (
6167 target = self .__main_loop , daemon = True , name = "wherobots-connection"
@@ -155,9 +161,7 @@ def __listen(self):
155161 query .handler (reader .read_pandas ())
156162 else :
157163 query .handler (
158- OperationalError (
159- f"Unsupported results format { result_format } "
160- )
164+ OperationalError (f"Unsupported results format { result_format } " )
161165 )
162166 elif kind == EventKind .ERROR :
163167 query .state = ExecutionState .FAILED
@@ -167,7 +171,9 @@ def __listen(self):
167171 logging .warning ("Received unknown %s event!" , kind )
168172
169173 def __send (self , message : dict [str , Any ]) -> None :
170- self .__ws .send (json .dumps (message ))
174+ request = json .dumps (message )
175+ logging .debug ("Request: %s" , request )
176+ self .__ws .send (request )
171177
172178 def __recv (self ) -> dict [str , Any ]:
173179 frame = self .__ws .recv (timeout = self .__read_timeout )
@@ -194,6 +200,10 @@ def __execute_sql(self, sql: str, handler: Callable[[Any], None]) -> str:
194200 state = ExecutionState .EXECUTION_REQUESTED ,
195201 handler = handler ,
196202 )
203+
204+ logging .info (
205+ "Executing SQL query %s: %s" , execution_id , textwrap .shorten (sql , width = 60 )
206+ )
197207 self .__send (request )
198208 return execution_id
199209
@@ -205,9 +215,14 @@ def __request_results(self, execution_id: str) -> None:
205215 request = {
206216 "kind" : RequestKind .RETRIEVE_RESULTS .value ,
207217 "execution_id" : execution_id ,
208- "format" : _DEFAULT_RESULTS_FORMAT .value ,
209- "compression" : _DEFAULT_DATA_COMPRESSION .value ,
210218 }
219+ if self .__results_format :
220+ request ["format" ] = self .__results_format .value
221+ if self .__data_compression :
222+ request ["compression" ] = self .__data_compression .value
223+ if self .__geometry_representation :
224+ request ["geometry" ] = self .__geometry_representation .value
225+
211226 query .state = ExecutionState .RESULTS_REQUESTED
212227 logging .info ("Requesting results from %s ..." , execution_id )
213228 self .__send (request )
0 commit comments