提交 cdf64f19 编写于 作者: T Tao Liu

[TD-314] modify python connector to adapt the taos API change

上级 14333f31
......@@ -142,12 +142,13 @@ class CTaosInterface(object):
libtaos.taos_fetch_fields.restype = ctypes.POINTER(TaosField)
libtaos.taos_init.restype = None
libtaos.taos_connect.restype = ctypes.c_void_p
libtaos.taos_use_result.restype = ctypes.c_void_p
#libtaos.taos_use_result.restype = ctypes.c_void_p
libtaos.taos_fetch_row.restype = ctypes.POINTER(ctypes.c_void_p)
libtaos.taos_errstr.restype = ctypes.c_char_p
libtaos.taos_subscribe.restype = ctypes.c_void_p
libtaos.taos_consume.restype = ctypes.c_void_p
libtaos.taos_fetch_lengths.restype = ctypes.c_void_p
libtaos.taos_free_result.restype = None
def __init__(self, config=None):
'''
......@@ -249,12 +250,12 @@ class CTaosInterface(object):
raise AttributeError("sql is expected as a string")
# finally:
# CTaosInterface.libtaos.close(connection)
@staticmethod
def affectedRows(connection):
def affectedRows(result):
"""The affected rows after runing query
"""
return CTaosInterface.libtaos.taos_affected_rows(connection)
return CTaosInterface.libtaos.taos_affected_rows(result)
@staticmethod
def subscribe(connection, restart, topic, sql, interval):
......@@ -292,18 +293,17 @@ class CTaosInterface(object):
CTaosInterface.libtaos.taos_unsubscribe(sub, 1 if keepProgress else 0)
@staticmethod
def useResult(connection):
def useResult(result):
'''Use result after calling self.query
'''
result = ctypes.c_void_p(CTaosInterface.libtaos.taos_use_result(connection))
fields = []
pfields = CTaosInterface.fetchFields(result)
for i in range(CTaosInterface.fieldsCount(connection)):
for i in range(CTaosInterface.fieldsCount(result)):
fields.append({'name': pfields[i].name.decode('utf-8'),
'bytes': pfields[i].bytes,
'type': ord(pfields[i].type)})
return result, fields
return fields
@staticmethod
def fetchBlock(result, fields):
......@@ -337,8 +337,8 @@ class CTaosInterface(object):
result.value = None
@staticmethod
def fieldsCount(connection):
return CTaosInterface.libtaos.taos_field_count(connection)
def fieldsCount(result):
return CTaosInterface.libtaos.taos_field_count(result)
@staticmethod
def fetchFields(result):
......@@ -386,29 +386,30 @@ class CTaosInterface(object):
# return (ctypes.cast(data, ctypes.c_char_p).value).rstrip('\x00')
@staticmethod
def errno(connection):
def errno(result):
"""Return the error number.
"""
return CTaosInterface.libtaos.taos_errno(connection)
return CTaosInterface.libtaos.taos_errno(result)
@staticmethod
def errStr(connection):
def errStr(result):
"""Return the error styring
"""
return CTaosInterface.libtaos.taos_errstr(connection).decode('utf-8')
return CTaosInterface.libtaos.taos_errstr(result).decode('utf-8')
if __name__ == '__main__':
cinter = CTaosInterface()
conn = cinter.connect()
result = cinter.query(conn, 'show databases')
print('Query return value: {}'.format(cinter.query(conn, 'show databases')))
print('Affected rows: {}'.format(cinter.affectedRows(conn)))
print('Query Affected rows: {}'.format(cinter.affectedRows(result)))
result, des = CTaosInterface.useResult(conn)
fields = CTaosInterface.useResult(result)
data, num_of_rows = CTaosInterface.fetchBlock(result, des)
data, num_of_rows = CTaosInterface.fetchBlock(result, fields)
print(data)
cinter.freeresult(result)
cinter.close(conn)
\ No newline at end of file
......@@ -78,9 +78,7 @@ class TDengineConnection(object):
def clear_result_set(self):
"""Clear unused result set on this connection.
"""
result = self._chandle.useResult(self._conn)[0]
if result:
self._chandle.freeResult(result)
pass
if __name__ == "__main__":
conn = TDengineConnection(host='192.168.1.107')
......
......@@ -122,26 +122,26 @@ class TDengineCursor(object):
# querySeqNum += 1
# localSeqNum = querySeqNum # avoid raice condition
# print(" >> Exec Query ({}): {}".format(localSeqNum, str(stmt)))
res = CTaosInterface.query(self._connection._conn, stmt)
self._result = CTaosInterface.query(self._connection._conn, stmt)
# print(" << Query ({}) Exec Done".format(localSeqNum))
if (self._logfile):
with open(self._logfile, "a") as logfile:
logfile.write("%s;\n" % operation)
if res == 0:
if CTaosInterface.fieldsCount(self._connection._conn) == 0:
if self._result is not None:
if CTaosInterface.fieldsCount(self._result) == 0:
self._affected_rows += CTaosInterface.affectedRows(
self._connection._conn)
return CTaosInterface.affectedRows(self._connection._conn)
self._result )
return CTaosInterface.affectedRows(self._result )
else:
self._result, self._fields = CTaosInterface.useResult(
self._connection._conn)
self._fields = CTaosInterface.useResult(
self._result)
return self._handle_result()
else:
raise ProgrammingError(
CTaosInterface.errStr(
self._connection._conn))
self._result ))
def executemany(self, operation, seq_of_parameters):
"""Prepare a database operation (query or command) and then execute it against all parameter sequences or mappings found in the sequence seq_of_parameters.
......
......@@ -708,13 +708,14 @@ void *readTable(void *sarg) {
sprintf(command, "select %s from %s%d where ts>= %" PRId64, aggreFunc[j], tb_prefix, i, sTime);
double t = getCurrentTime();
/*
if (taos_query(taos, command) != 0) {
fprintf(stderr, "Failed to query\n");
taos_close(taos);
exit(EXIT_FAILURE);
}
TAOS_RES *result = taos_use_result(taos);
*/
TAOS_RES *result = taos_query(taos, command) ;
if (result == NULL) {
fprintf(stderr, "Failed to retreive results:%s\n", taos_errstr(taos));
taos_close(taos);
......@@ -779,13 +780,13 @@ void *readMetric(void *sarg) {
fprintf(fp, "%s\n", command);
double t = getCurrentTime();
if (taos_query(taos, command) != 0) {
fprintf(stderr, "Failed to query\n");
taos_close(taos);
exit(EXIT_FAILURE);
}
// if (taos_query(taos, command) != 0) {
// fprintf(stderr, "Failed to query\n");
// taos_close(taos);
// exit(EXIT_FAILURE);
// }
TAOS_RES *result = taos_use_result(taos);
TAOS_RES *result = taos_query(taos,command);
if (result == NULL) {
fprintf(stderr, "Failed to retreive results:%s\n", taos_errstr(taos));
taos_close(taos);
......
......@@ -372,12 +372,13 @@ int taosGetTableRecordInfo(char *table, STableRecordInfo *pTableRecordInfo) {
memset(pTableRecordInfo, 0, sizeof(STableRecordInfo));
sprintf(command, "show tables like %s", table);
/*
if (taos_query(taos, command) != 0) {
fprintf(stderr, "failed to run command %s\n", command);
return -1;
}
result = taos_use_result(taos);
*/
result = taos_query(taos, command) ;
if (result == NULL) {
fprintf(stderr, "failed to use result\n");
return -1;
......@@ -400,12 +401,12 @@ int taosGetTableRecordInfo(char *table, STableRecordInfo *pTableRecordInfo) {
if (isSet) return 0;
sprintf(command, "show stables like %s", table);
if (taos_query(taos, command) != 0) {
/* if (taos_query(taos, command) != 0) {
fprintf(stderr, "failed to run command %s\n", command);
return -1;
}
result = taos_use_result(taos);
*/
result = taos_query(taos, command);
if (result == NULL) {
fprintf(stderr, "failed to use result\n");
return -1;
......@@ -467,12 +468,12 @@ int taosDumpOut(SDumpArguments *arguments) {
taosDumpCharset(fp);
sprintf(command, "show databases");
if (taos_query(taos, command) != 0) {
/*if (taos_query(taos, command) != 0) {
fprintf(stderr, "failed to run command: %s, reason: %s\n", command, taos_errstr(taos));
goto _exit_failure;
}
}*/
result = taos_use_result(taos);
result = taos_query(taos, command);
if (result == NULL) {
fprintf(stderr, "failed to use result\n");
goto _exit_failure;
......@@ -551,7 +552,7 @@ int taosDumpOut(SDumpArguments *arguments) {
taosDumpCreateDbClause(dbInfos[0], arguments->with_property, fp);
sprintf(command, "use %s", dbInfos[0]->name);
if (taos_query(taos, command) != 0) {
if (taos_query(taos, command) == NULL ) {
fprintf(stderr, "invalid database %s\n", dbInfos[0]->name);
goto _exit_failure;
}
......@@ -612,7 +613,7 @@ int taosDumpDb(SDbInfo *dbInfo, SDumpArguments *arguments, FILE *fp) {
taosDumpCreateDbClause(dbInfo, arguments->with_property, fp);
sprintf(command, "use %s", dbInfo->name);
if (taos_query(taos, command) != 0) {
if (taos_query(taos, command) == NULL) {
fprintf(stderr, "invalid database %s\n", dbInfo->name);
return -1;
}
......@@ -620,12 +621,12 @@ int taosDumpDb(SDbInfo *dbInfo, SDumpArguments *arguments, FILE *fp) {
fprintf(fp, "USE %s\n\n", dbInfo->name);
sprintf(command, "show tables");
if (taos_query(taos, command) != 0) {
/* if (taos_query(taos, command) != 0) {
fprintf(stderr, "failed to run command %s\n", command);
return -1;
}
}*/
result = taos_use_result(taos);
result = taos_query(taos, command);
if (result == NULL) {
fprintf(stderr, "failed to use result\n");
return -1;
......@@ -725,12 +726,12 @@ void taosDumpCreateMTableClause(STableDef *tableDes, char *metric, int numOfCols
TAOS_ROW row = NULL;
sprintf(command, "select %s from %s limit 1", tableDes->cols[counter].field, tableDes->name);
if (taos_query(taos, command) != 0) {
/*if (taos_query(taos, command) != 0) {
fprintf(stderr, "failed to run command %s\n", command);
return;
}
}*/
result = taos_use_result(taos);
result = taos_query(taos, command);
if (result == NULL) {
fprintf(stderr, "failed to use result\n");
return;
......@@ -806,12 +807,12 @@ int taosGetTableDes(char *table, STableDef *tableDes) {
int count = 0;
sprintf(command, "describe %s", table);
if (taos_query(taos, command) != 0) {
/*if (taos_query(taos, command) != 0) {
fprintf(stderr, "failed to run command %s\n", command);
return -1;
}
}*/
result = taos_use_result(taos);
result = taos_query(taos, command);
if (result == NULL) {
fprintf(stderr, "failed to use result\n");
return -1;
......@@ -889,12 +890,12 @@ int32_t taosDumpMetric(char *metric, SDumpArguments *arguments, FILE *fp) {
strcpy(tableRecord.metric, metric);
sprintf(command, "select tbname from %s", metric);
if (taos_query(taos, command) != 0) {
/*if (taos_query(taos, command) != 0) {
fprintf(stderr, "failed to run command %s\n", command);
return -1;
}
}*/
result = taos_use_result(taos);
result = taos_query(taos, command);
if (result == NULL) {
fprintf(stderr, "failed to use result\n");
return -1;
......@@ -942,12 +943,12 @@ int taosDumpTableData(FILE *fp, char *tbname, SDumpArguments *arguments) {
sprintf(command, "select * from %s where _c0 >= %" PRId64 " and _c0 <= %" PRId64 " order by _c0 asc", tbname, arguments->start_time,
arguments->end_time);
if (taos_query(taos, command) != 0) {
/*if (taos_query(taos, command) != 0) {
fprintf(stderr, "failed to run command %s, reason: %s\n", command, taos_errstr(taos));
return -1;
}
}*/
result = taos_use_result(taos);
result = taos_query(taos, command);
if (result == NULL) {
fprintf(stderr, "failed to use result\n");
return -1;
......@@ -1194,7 +1195,7 @@ int taosDumpIn(SDumpArguments *arguments) {
tcommand = command;
}
taosReplaceCtrlChar(tcommand);
if (taos_query(taos, tcommand) != 0)
if (taos_query(taos, tcommand) == NULL)
fprintf(stderr, "linenu: %" PRId64 " failed to run command %s reason:%s \ncontinue...\n", linenu, command,
taos_errstr(taos));
......@@ -1242,7 +1243,7 @@ int taosDumpIn(SDumpArguments *arguments) {
tcommand = command;
}
taosReplaceCtrlChar(tcommand);
if (taos_query(taos, tcommand) != 0)
if (taos_query(taos, tcommand) == NULL)
fprintf(stderr, "linenu:%" PRId64 " failed to run command %s reason: %s \ncontinue...\n", linenu, command,
taos_errstr(taos));
}
......@@ -1265,7 +1266,7 @@ int taosDumpIn(SDumpArguments *arguments) {
tcommand = command;
}
taosReplaceCtrlChar(lcommand);
if (taos_query(taos, tcommand) != 0)
if (taos_query(taos, tcommand) == NULL)
fprintf(stderr, "linenu:%" PRId64 " failed to run command %s reason:%s \ncontinue...\n", linenu, command,
taos_errstr(taos));
}
......
......@@ -26,13 +26,13 @@
void taosMsleep(int mseconds);
static int32_t doQuery(TAOS* taos, const char* sql) {
int32_t code = taos_query(taos, sql);
if (code != 0) {
TAOS_RES* res = taos_query(taos, sql);
if (res == NULL) {
printf("failed to execute query, reason:%s\n", taos_errstr(taos));
return -1;
}
TAOS_RES* res = taos_use_result(taos);
//TAOS_RES* res = taos_use_result(taos);
TAOS_ROW row = NULL;
char buf[512] = {0};
......@@ -167,15 +167,8 @@ int main(int argc, char *argv[]) {
// query the records
sprintf(qstr, "SELECT * FROM m1");
if (taos_query(taos, qstr) != 0) {
printf("failed to select, reason:%s\n", taos_errstr(taos));
exit(1);
}
result = taos_use_result(taos);
result = taos_query(taos, qstr);
if (result == NULL) {
printf("failed to get result, reason:%s\n", taos_errstr(taos));
exit(1);
......
......@@ -122,9 +122,9 @@ void* taos_execute(void *param) {
int64_t timestamp = 1530374400000L;
sprintf(sql, "insert into db.t%d values(%ld, %d, %d, %d)", pThread->index, timestamp, 0, 0, 0);
int code = taos_query(taos, sql);
if (code != 0) printf("error code:%d, sql:%s\n", code, sql);
int affectrows = taos_affected_rows(taos);
void *result = taos_query(taos, sql);
if (result == NULL) printf("error , sql:%s\n", sql);
int affectrows = taos_affected_rows(result);
if (affectrows != 1) printf("affect rows:%d, sql:%s\n", affectrows, sql);
timestamp -= 1000;
......@@ -133,9 +133,9 @@ void* taos_execute(void *param) {
for (int i = 1; i < rowNum; ++i) {
sprintf(sql, "import into db.t%d values(%ld, %d, %d, %d)", pThread->index, timestamp, i, i, i);
code = taos_query(taos, sql);
if (code != 0) printf("error code:%d, sql:%s\n", code, sql);
int affectrows = taos_affected_rows(taos);
void * result = taos_query(taos, sql);
if (result == NULL) printf("error , sql:%s\n", sql);
int affectrows = taos_affected_rows(result);
if (affectrows != 1) printf("affect rows:%d, sql:%s\n", affectrows, sql);
total_affect_rows += affectrows;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册