提交 04517568 编写于 作者: F freemine

1. typo correction in CMakeLists.txt

2. fprint error message when SQLxxx failed in tcodbc
上级 75786385
...@@ -27,7 +27,7 @@ IF (TD_LINUX_64) ...@@ -27,7 +27,7 @@ IF (TD_LINUX_64)
ADD_SUBDIRECTORY(tests) ADD_SUBDIRECTORY(tests)
endif() endif()
ELSE () ELSE ()
message(WARNING "unixodbc is not installed yet, you may install it under ubuntu by typing: sudo apt install unixodbc-dev") message(WARNING "unixodbc is not installed yet, you may install it under ubuntu by typing: sudo apt install unixodbc")
ENDIF () ENDIF ()
ENDIF () ENDIF ()
#!/bin/bash
odbcinst -u -d -n TAOS &&
odbcinst -i -d -f "$(dirname "$0")/template.ini" &&
odbcinst -i -s -f "$(dirname "$0")/template.dsn" &&
echo yes
...@@ -47,18 +47,18 @@ do { \ ...@@ -47,18 +47,18 @@ do { \
#define LOCK(obj) pthread_mutex_lock(&obj->lock); #define LOCK(obj) pthread_mutex_lock(&obj->lock);
#define UNLOCK(obj) pthread_mutex_unlock(&obj->lock); #define UNLOCK(obj) pthread_mutex_unlock(&obj->lock);
#define SET_ERROR(obj, sqlstate, eno, err_fmt, ...) \ #define SET_ERROR(obj, sqlstate, eno, err_fmt, ...) \
do { \ do { \
obj->err.err_no = eno; \ obj->err.err_no = eno; \
const char* estr = tstrerror(eno); \ const char* estr = tstrerror(eno); \
if (!estr) estr = "Unknown error"; \ if (!estr) estr = "Unknown error"; \
int n = snprintf(NULL, 0, "@[%d][%x]%s: " err_fmt "", __LINE__, eno, estr, ##__VA_ARGS__); \ int n = snprintf(NULL, 0, "%s: @[%d][TSDB:%x]" err_fmt "", estr, __LINE__, eno, ##__VA_ARGS__); \
if (n<0) break; \ if (n<0) break; \
char *err_str = (char*)realloc(obj->err.err_str, n+1); \ char *err_str = (char*)realloc(obj->err.err_str, n+1); \
if (!err_str) break; \ if (!err_str) break; \
obj->err.err_str = err_str; \ obj->err.err_str = err_str; \
snprintf(obj->err.err_str, n+1, "@[%d][%x]%s: " err_fmt "", __LINE__, eno, estr, ##__VA_ARGS__); \ snprintf(obj->err.err_str, n+1, "%s: @[%d][TSDB:%x]" err_fmt "", estr, __LINE__, eno, ##__VA_ARGS__); \
snprintf((char*)obj->err.sql_state, sizeof(obj->err.sql_state), "%s", sqlstate); \ snprintf((char*)obj->err.sql_state, sizeof(obj->err.sql_state), "%s", sqlstate); \
} while (0) } while (0)
#define CLR_ERROR(obj) \ #define CLR_ERROR(obj) \
......
...@@ -37,14 +37,30 @@ static const char *pro_stmts[] = { ...@@ -37,14 +37,30 @@ static const char *pro_stmts[] = {
// "drop database db" // "drop database db"
}; };
#define CHK_RESULT(r, ht, h) \
do { \
if (r==0) break; \
SQLCHAR ss[10]; \
SQLINTEGER ne = 0; \
SQLCHAR es[4096]; \
SQLSMALLINT n = 0; \
ss[0] = '\0'; \
es[0] = '\0'; \
SQLRETURN ret = SQLGetDiagRec(ht, h, 1, ss, &ne, es, sizeof(es), &n); \
if (ret) break; \
fprintf(stderr, "%s%s\n", ss, es); \
} while (0)
static int do_statement(SQLHSTMT stmt, const char *statement) { static int do_statement(SQLHSTMT stmt, const char *statement) {
SQLRETURN r = 0; SQLRETURN r = 0;
do { do {
fprintf(stderr, "prepare [%s]\n", statement); fprintf(stderr, "prepare [%s]\n", statement);
r = SQLPrepare(stmt, (SQLCHAR*)statement, strlen(statement)); r = SQLPrepare(stmt, (SQLCHAR*)statement, strlen(statement));
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
fprintf(stderr, "execute [%s]\n", statement); fprintf(stderr, "execute [%s]\n", statement);
r = SQLExecute(stmt); r = SQLExecute(stmt);
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
fprintf(stderr, "done\n"); fprintf(stderr, "done\n");
} while (0); } while (0);
...@@ -63,53 +79,65 @@ static int do_insert(SQLHSTMT stmt, data_t data) { ...@@ -63,53 +79,65 @@ static int do_insert(SQLHSTMT stmt, data_t data) {
do { do {
fprintf(stderr, "prepare [%s]\n", statement); fprintf(stderr, "prepare [%s]\n", statement);
r = SQLPrepare(stmt, (SQLCHAR*)statement, strlen(statement)); r = SQLPrepare(stmt, (SQLCHAR*)statement, strlen(statement));
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
fprintf(stderr, "bind 1 [%s]\n", statement); fprintf(stderr, "bind 1 [%s]\n", statement);
r = SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_TIMESTAMP, ignored, ignored, &data.ts, ignored, NULL); r = SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_TIMESTAMP, ignored, ignored, &data.ts, ignored, NULL);
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
fprintf(stderr, "bind 2 [%s]\n", statement); fprintf(stderr, "bind 2 [%s]\n", statement);
r = SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_BIT, SQL_BIT, ignored, ignored, &data.b, ignored, NULL); r = SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_BIT, SQL_BIT, ignored, ignored, &data.b, ignored, NULL);
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
fprintf(stderr, "bind 3 [%s]\n", statement); fprintf(stderr, "bind 3 [%s]\n", statement);
r = SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_TINYINT, SQL_TINYINT, ignored, ignored, &data.v1, ignored, NULL); r = SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_TINYINT, SQL_TINYINT, ignored, ignored, &data.v1, ignored, NULL);
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
fprintf(stderr, "bind 4 [%s]\n", statement); fprintf(stderr, "bind 4 [%s]\n", statement);
r = SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_SHORT, SQL_SMALLINT, ignored, ignored, &data.v2, ignored, NULL); r = SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_SHORT, SQL_SMALLINT, ignored, ignored, &data.v2, ignored, NULL);
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
fprintf(stderr, "bind 5 [%s]\n", statement); fprintf(stderr, "bind 5 [%s]\n", statement);
r = SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, ignored, ignored, &data.v4, ignored, NULL); r = SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, ignored, ignored, &data.v4, ignored, NULL);
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
fprintf(stderr, "bind 6 [%s]\n", statement); fprintf(stderr, "bind 6 [%s]\n", statement);
r = SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_BIGINT, ignored, ignored, &data.v8, ignored, NULL); r = SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_SBIGINT, SQL_BIGINT, ignored, ignored, &data.v8, ignored, NULL);
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
fprintf(stderr, "bind 7 [%s]\n", statement); fprintf(stderr, "bind 7 [%s]\n", statement);
r = SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_FLOAT, ignored, ignored, &data.f4, ignored, NULL); r = SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_FLOAT, ignored, ignored, &data.f4, ignored, NULL);
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
fprintf(stderr, "bind 8 [%s]\n", statement); fprintf(stderr, "bind 8 [%s]\n", statement);
SQLLEN l8 = SQL_NULL_DATA; SQLLEN l8 = SQL_NULL_DATA;
r = SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, ignored, ignored, &data.f8, ignored, &l8); r = SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, ignored, ignored, &data.f8, ignored, &l8);
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
fprintf(stderr, "bind 9 [%s]\n", statement); fprintf(stderr, "bind 9 [%s]\n", statement);
lbin = SQL_NTS; lbin = SQL_NTS;
r = SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_VARBINARY, sizeof(data.bin)-1, ignored, &data.bin, ignored, &lbin); r = SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_VARBINARY, sizeof(data.bin)-1, ignored, &data.bin, ignored, &lbin);
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
fprintf(stderr, "bind 10 [%s]\n", statement); fprintf(stderr, "bind 10 [%s]\n", statement);
lblob = SQL_NTS; lblob = SQL_NTS;
r = SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, sizeof(data.blob)-1, ignored, &data.blob, ignored, &lblob); r = SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, sizeof(data.blob)-1, ignored, &data.blob, ignored, &lblob);
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
fprintf(stderr, "execute [%s]\n", statement); fprintf(stderr, "execute [%s]\n", statement);
r = SQLExecute(stmt); r = SQLExecute(stmt);
CHK_RESULT(r, SQL_HANDLE_STMT, stmt);
if (r) break; if (r) break;
// ts += 1; // ts += 1;
...@@ -136,11 +164,13 @@ int main(int argc, char *argv[]) { ...@@ -136,11 +164,13 @@ int main(int argc, char *argv[]) {
if (r!=SQL_SUCCESS) return 1; if (r!=SQL_SUCCESS) return 1;
do { do {
r = SQLAllocConnect(env, &conn); r = SQLAllocConnect(env, &conn);
CHK_RESULT(r, SQL_HANDLE_ENV, env);
if (r!=SQL_SUCCESS) break; if (r!=SQL_SUCCESS) break;
do { do {
r = SQLConnect(conn, (SQLCHAR*)dsn, strlen(dsn), r = SQLConnect(conn, (SQLCHAR*)dsn, strlen(dsn),
(SQLCHAR*)uid, strlen(uid), (SQLCHAR*)uid, strlen(uid),
(SQLCHAR*)pwd, strlen(pwd)); (SQLCHAR*)pwd, strlen(pwd));
CHK_RESULT(r, SQL_HANDLE_DBC, conn);
if (r!=SQL_SUCCESS) break; if (r!=SQL_SUCCESS) break;
do { do {
SQLHSTMT stmt = {0}; SQLHSTMT stmt = {0};
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册