|
1 | | -from typing import Dict, Any, Optional |
2 | | -from dataclasses import dataclass |
| 1 | +""" |
| 2 | +Request models for the SEA (Statement Execution API) backend. |
| 3 | +
|
| 4 | +These models define the structures used in SEA API requests. |
| 5 | +""" |
| 6 | + |
| 7 | +from typing import Dict, List, Any, Optional, Union |
| 8 | +from dataclasses import dataclass, field |
| 9 | + |
| 10 | + |
| 11 | +@dataclass |
| 12 | +class StatementParameter: |
| 13 | + """Representation of a parameter for a SQL statement.""" |
| 14 | + |
| 15 | + name: str |
| 16 | + value: Optional[str] = None |
| 17 | + type: Optional[str] = None |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class ExecuteStatementRequest: |
| 22 | + """Representation of a request to execute a SQL statement.""" |
| 23 | + |
| 24 | + session_id: str |
| 25 | + statement: str |
| 26 | + warehouse_id: str |
| 27 | + disposition: str = "EXTERNAL_LINKS" |
| 28 | + format: str = "JSON_ARRAY" |
| 29 | + result_compression: Optional[str] = None |
| 30 | + parameters: Optional[List[StatementParameter]] = None |
| 31 | + wait_timeout: str = "10s" |
| 32 | + on_wait_timeout: str = "CONTINUE" |
| 33 | + row_limit: Optional[int] = None |
| 34 | + |
| 35 | + def to_dict(self) -> Dict[str, Any]: |
| 36 | + """Convert the request to a dictionary for JSON serialization.""" |
| 37 | + result: Dict[str, Any] = { |
| 38 | + "warehouse_id": self.warehouse_id, |
| 39 | + "session_id": self.session_id, |
| 40 | + "statement": self.statement, |
| 41 | + "disposition": self.disposition, |
| 42 | + "format": self.format, |
| 43 | + "wait_timeout": self.wait_timeout, |
| 44 | + "on_wait_timeout": self.on_wait_timeout, |
| 45 | + } |
| 46 | + |
| 47 | + if self.row_limit is not None and self.row_limit > 0: |
| 48 | + result["row_limit"] = self.row_limit |
| 49 | + |
| 50 | + if self.result_compression: |
| 51 | + result["result_compression"] = self.result_compression |
| 52 | + |
| 53 | + if self.parameters: |
| 54 | + result["parameters"] = [ |
| 55 | + { |
| 56 | + "name": param.name, |
| 57 | + **({"value": param.value} if param.value is not None else {}), |
| 58 | + **({"type": param.type} if param.type is not None else {}), |
| 59 | + } |
| 60 | + for param in self.parameters |
| 61 | + ] |
| 62 | + |
| 63 | + return result |
| 64 | + |
| 65 | + |
| 66 | +@dataclass |
| 67 | +class GetStatementRequest: |
| 68 | + """Representation of a request to get information about a statement.""" |
| 69 | + |
| 70 | + statement_id: str |
| 71 | + |
| 72 | + def to_dict(self) -> Dict[str, Any]: |
| 73 | + """Convert the request to a dictionary for JSON serialization.""" |
| 74 | + return {"statement_id": self.statement_id} |
| 75 | + |
| 76 | + |
| 77 | +@dataclass |
| 78 | +class CancelStatementRequest: |
| 79 | + """Representation of a request to cancel a statement.""" |
| 80 | + |
| 81 | + statement_id: str |
| 82 | + |
| 83 | + def to_dict(self) -> Dict[str, Any]: |
| 84 | + """Convert the request to a dictionary for JSON serialization.""" |
| 85 | + return {"statement_id": self.statement_id} |
| 86 | + |
| 87 | + |
| 88 | +@dataclass |
| 89 | +class CloseStatementRequest: |
| 90 | + """Representation of a request to close a statement.""" |
| 91 | + |
| 92 | + statement_id: str |
| 93 | + |
| 94 | + def to_dict(self) -> Dict[str, Any]: |
| 95 | + """Convert the request to a dictionary for JSON serialization.""" |
| 96 | + return {"statement_id": self.statement_id} |
3 | 97 |
|
4 | 98 |
|
5 | 99 | @dataclass |
|
0 commit comments