From 963c5ac25b551b6999c4d0eec0c1b9566bca2413 Mon Sep 17 00:00:00 2001 From: "cris.pei" Date: Fri, 11 Oct 2024 20:47:27 +0800 Subject: [PATCH 1/2] fix: migrate the get-meta-api-call to the prepare method --- taos/connection.py | 7 ++++--- taos/statement2.py | 33 +++++++++++++++++---------------- tests/test_stmt2.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/taos/connection.py b/taos/connection.py index 06c6ccf4..78a65b22 100644 --- a/taos/connection.py +++ b/taos/connection.py @@ -126,11 +126,12 @@ def statement2(self, sql=None, option=None): return None if option is not None: option = option.get_impl() - stmt = taos_stmt2_init(self._conn, option) + _stmt2 = taos_stmt2_init(self._conn, option) + stmt2 = TaosStmt2(_stmt2, decode_binary=self.decode_binary) if sql is not None: - taos_stmt2_prepare(stmt, sql) + stmt2.prepare(sql) - return TaosStmt2(stmt, decode_binary=self.decode_binary) + return stmt2 def load_table_info(self, tables): # type: (str) -> None diff --git a/taos/statement2.py b/taos/statement2.py index c0632fec..dbb19c07 100644 --- a/taos/statement2.py +++ b/taos/statement2.py @@ -229,6 +229,7 @@ def __init__(self, stmt2, decode_binary=True): self.fields = None self.tag_fields = None self.types = None + self._is_insert = None self.valid_field_types = [TAOS_FIELD_COL, TAOS_FIELD_TAG, TAOS_FIELD_QUERY, TAOS_FIELD_TBNAME] def prepare(self, sql): @@ -244,8 +245,16 @@ def prepare(self, sql): if len(sql) == 0: raise StatementError("sql is empty.") + self.fields = None + self.tag_fields = None + self._is_insert = None + taos_stmt2_prepare(self._stmt2, sql) + # obtain schema if insert + if self.is_insert(): + if obtainSchema(self) is False: + raise StatementError(f"obtain schema failed. tbnames={tbnames}") def bind_param(self, tbnames, tags, datas): if self._stmt2 is None: @@ -254,22 +263,11 @@ def bind_param(self, tbnames, tags, datas): log.debug(f"bind_param tbnames = {tbnames} \n") log.debug(f"bind_param tags = {tags} \n") log.debug(f"bind_param datasTbs = {datas} \n") - - # obtain schema if insert - if self.is_insert(): - if tbnames is not None: - bindv = createBindV(self, tbnames, None, None) - if bindv == None: - raise StatementError("create stmt2 bindV failed.") - taos_stmt2_bind_param(self._stmt2, bindv.get_address(), -1) - if obtainSchema(self) is False: - raise StatementError(f"obtain schema failed. tbnames={tbnames}") - - # check consistent - if checkConsistent(tbnames, tags, datas) == False: - raise StatementError("check consistent failed.") - + # check consistent + if checkConsistent(tbnames, tags, datas) == False: + raise StatementError("check consistent failed.") + # bindV bindv = createBindV(self, tbnames, tags, datas) if bindv == None: @@ -319,7 +317,10 @@ def is_insert(self): if self._stmt2 is None: raise StatementError("stmt2 object is null.") - return taos_stmt2_is_insert(self._stmt2) + if self._is_insert is None: + self._is_insert = taos_stmt2_is_insert(self._stmt2) + + return self._is_insert def get_fields(self, field_type): if self._stmt2 is None: diff --git a/tests/test_stmt2.py b/tests/test_stmt2.py index a5c3742a..004744ed 100644 --- a/tests/test_stmt2.py +++ b/tests/test_stmt2.py @@ -183,6 +183,31 @@ def insert_bind_param_normal_tables(conn, stmt2, dbname): # check correct checkResultCorrects(conn, dbname, None, ["ntb2"], [None], datas) +def insert_bind_param_with_table(conn, stmt2, dbname, stbname, ctb): + + tbnames = None + tags = [ + ["grade2", 1] + ] + + # prepare data + datas = [ + # table 1 + [ + # student + [1601481600000,1601481600004,"2024-09-19 10:00:00", "2024-09-19 10:00:01.123", datetime(2024,9,20,10,11,12,456)], + ["Mary", "Tom", "Jack", "Jane", "alex" ], + [0, 1, 1, 0, 1 ], + [98, 80, 60, 100, 99 ] + ] + ] + + stmt2.bind_param(tbnames, tags, datas) + stmt2.execute() + + # check correct + checkResultCorrects(conn, dbname, stbname, [ctb], tags, datas) + # insert with single table (performance is lower) def insert_bind_param_with_tables(conn, stmt2, dbname, stbname): @@ -377,6 +402,13 @@ def test_stmt2_insert(conn): try: prepare(conn, dbname, stbname) + + ctb = 'ctb' + stmt2 = conn.statement2(f"insert into {dbname}.{ctb} using {dbname}.{stbname} tags (?,?) values(?,?,?,?)") + insert_bind_param_with_table(conn, stmt2, dbname, stbname, ctb) + print("insert normal table ........................... ok\n") + stmt2.close() + # prepare stmt2 = conn.statement2(f"insert into ? using {dbname}.{stbname} tags(?,?) values(?,?,?,?)") print("insert prepare sql ............................ ok\n") From 2145c594e479f9e02113d50d0d5c7b81a5e6bea8 Mon Sep 17 00:00:00 2001 From: "cris.pei" Date: Sat, 12 Oct 2024 14:19:25 +0800 Subject: [PATCH 2/2] fix: when get-meta-api-call failed, error message optimization --- taos/statement2.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/taos/statement2.py b/taos/statement2.py index dbb19c07..42c73466 100644 --- a/taos/statement2.py +++ b/taos/statement2.py @@ -111,10 +111,14 @@ def obtainSchema(statement2): #statement2.fields = [FieldType.C_TIMESTAMP, FieldType.C_BINARY, FieldType.C_BOOL, FieldType.C_INT] #return len(statement2.fields) > 0 - count, statement2.fields = statement2.get_fields(TAOS_FIELD_COL) - count, statement2.tag_fields = statement2.get_fields(TAOS_FIELD_TAG) - log.debug(f"obtain schema tag fields = {statement2.tag_fields}") - log.debug(f"obtain schema fields = {statement2.fields}") + try: + count, statement2.fields = statement2.get_fields(TAOS_FIELD_COL) + count, statement2.tag_fields = statement2.get_fields(TAOS_FIELD_TAG) + log.debug(f"obtain schema tag fields = {statement2.tag_fields}") + log.debug(f"obtain schema fields = {statement2.fields}") + except Exception as err: + log.debug(f"obtain schema tag/col fields failed, reason: {repr(err)}") + return False return len(statement2.fields) > 0 @@ -254,7 +258,7 @@ def prepare(self, sql): # obtain schema if insert if self.is_insert(): if obtainSchema(self) is False: - raise StatementError(f"obtain schema failed. tbnames={tbnames}") + raise StatementError(f"obtain schema failed. sql={sql}") def bind_param(self, tbnames, tags, datas): if self._stmt2 is None: