2 from abc
import abstractmethod
3 from typing
import Dict, Any, Optional
4 from nebula3.data.ResultSet
import ResultSet
5 from nebula3.common.ttypes
import ErrorCode, Value, NList, Date, Time, DateTime
9 def __init__(self, stmt: str, param: Any, code: ErrorCode, msg: str):
11 self.
paramparam = param
17 f
"ExecuteError. err_code: {self.code}, err_msg: {self.msg}.\n"
18 + f
"Statement: \n{self.stmt}\n"
19 + f
"Parameter: \n{self.param}"
25 def execute_parameter(
26 self, stmt: str, params: Optional[Dict[str, Any]]
31 def execute_json_with_parameter(
32 self, stmt: str, params: Optional[Dict[str, Any]]
36 def execute(self, stmt: str) -> ResultSet:
39 def execute_json(self, stmt: str) -> bytes:
45 params: Optional[Dict[str, Any]] =
None,
47 """**Recommended** Execute a statement with parameters in Python type instead of thrift type."""
53 if not result.is_succeeded():
54 raise ExecuteError(stmt, params, result.error_code(), result.error_msg())
59 def _build_byte_param(params: dict) -> dict:
61 for k, v
in params.items():
62 if isinstance(v, Value):
64 elif str(type(v)).startswith(
"nebula3.common.ttypes"):
67 byte_params[k] = _cast_value(v)
71 def _cast_value(value: Any) -> Value:
73 Cast the value to nebula Value type
74 ref: https://github.com/vesoft-inc/nebula/blob/master/src/common/datatypes/Value.cpp
75 :param value: the value to be casted
76 :return: the casted value
78 casted_value = Value()
79 if isinstance(value, bool):
80 casted_value.set_bVal(value)
81 elif isinstance(value, int):
82 casted_value.set_iVal(value)
83 elif isinstance(value, str):
84 casted_value.set_sVal(value)
85 elif isinstance(value, float):
86 casted_value.set_fVal(value)
87 elif isinstance(value, datetime.date):
88 date_value = Date(year=value.year, month=value.month, day=value.day)
89 casted_value.set_dVal(date_value)
90 elif isinstance(value, datetime.time):
95 microsec=value.microsecond,
97 casted_value.set_tVal(time_value)
98 elif isinstance(value, datetime.datetime):
99 datetime_value = DateTime(
106 microsec=value.microsecond,
108 casted_value.set_dtVal(datetime_value)
110 elif isinstance(value, list):
113 byte_list.append(_cast_value(item))
114 casted_value.set_lVal(NList(values=byte_list))
115 elif isinstance(value, dict):
117 raise TypeError(
"Unsupported type: dict")
119 raise TypeError(f
"Unsupported type: {type(value)}")
bytes execute_json_with_parameter(self, str stmt, Optional[Dict[str, Any]] params)
def execute_py(self, str stmt, Optional[Dict[str, Any]] params=None)
ResultSet execute_parameter(self, str stmt, Optional[Dict[str, Any]] params)