Skip to content

Commit 5ecbaa5

Browse files
committed
Fixes: correct check on truncation failure. Reinit IRD on statement closing. Logging format descriptors (#170)
* fix: only fail time*/str conversion on truncation The timestamp/time to string truncation needs to fail only if current status code (for current column) indicates that truncation occured; so status code has been added to the branching evaluation. Previously, only the current diagnostic code was used in the decision (and checked if it indicated truncation), which was incorrect since this could have been set by the conversion of a previous column in same row. * fix: re-init IRD headers on statement close On statment closing the IRD descriptor's headers should be reinitialized. The fix is for good house keeping, the defect didn't have a practical impact, since the IRD is cleared also before adding a new result set; furthermore, a new result set added to an already-used IRD would only occur if a statement handle is re-prepared, which happens very rarely (generally a client app would allocate a new handle for a new query). The commit also switches a few catalog functions to using SQLExecDirect(), instead of dupplicating its code. * fix: correct logging format specifiers Correct some format specifiers for logging, mostly s/zd/zu for size_t types. Cherry-pick merging: - truncation failure check not applicable.
1 parent 42094a3 commit 5ecbaa5

File tree

4 files changed

+29
-47
lines changed

4 files changed

+29
-47
lines changed

driver/catalogue.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,7 @@ SQLRETURN EsSQLTablesW(
409409

410410
DBGH(stmt, "tables catalog SQL [%d]:`" LWPDL "`.", pos, pos, wbuf);
411411

412-
ret = EsSQLFreeStmt(stmt, ESODBC_SQL_CLOSE);
413-
assert(SQL_SUCCEEDED(ret)); /* can't return error */
414-
ret = attach_sql(stmt, wbuf, pos);
415-
if (SQL_SUCCEEDED(ret)) {
416-
ret = EsSQLExecute(stmt);
417-
}
412+
ret = EsSQLExecDirectW(stmt, wbuf, (SQLINTEGER)pos);
418413
end:
419414
free(ptr);
420415
return ret;
@@ -545,12 +540,7 @@ SQLRETURN EsSQLColumnsW
545540
goto end;
546541
}
547542

548-
ret = EsSQLFreeStmt(stmt, ESODBC_SQL_CLOSE);
549-
assert(SQL_SUCCEEDED(ret)); /* can't return error */
550-
ret = attach_sql(stmt, wbuf, pos);
551-
if (SQL_SUCCEEDED(ret)) {
552-
ret = EsSQLExecute(stmt);
553-
}
543+
ret = EsSQLExecDirectW(stmt, wbuf, (SQLINTEGER)pos);
554544
end:
555545
free(wbuf);
556546
return ret;

driver/connect.c

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ static size_t write_callback(char *ptr, size_t size, size_t nmemb,
287287
avail = dbc->alen - dbc->apos;
288288
have = size * nmemb;
289289

290-
DBGH(dbc, "libcurl: new data chunk of size [%zd] x %zd arrived; "
291-
"available buffer: %zd/%zd.", nmemb, size, avail, dbc->alen);
290+
DBGH(dbc, "libcurl: new data chunk of size [%zu] x %zu arrived; "
291+
"available buffer: %zu/%zu.", nmemb, size, avail, dbc->alen);
292292

293293
/* do I need to grow the existing buffer? */
294294
if (avail < have) {
@@ -298,14 +298,14 @@ static size_t write_callback(char *ptr, size_t size, size_t nmemb,
298298
while (need < dbc->apos + have) {
299299
need *= 2;
300300
}
301-
DBGH(dbc, "libcurl: need to grow buffer for new chunk of %zd "
302-
"from %zd to %zd bytes.", have, dbc->alen, need);
301+
DBGH(dbc, "libcurl: need to grow buffer for new chunk of %zu "
302+
"from %zu to %zu bytes.", have, dbc->alen, need);
303303
if (dbc->amax && (dbc->amax < need)) { /* do I need more than max? */
304304
if (dbc->amax <= (size_t)dbc->alen) { /* am I at max already? */
305305
goto too_large;
306306
} else { /* am not: alloc max possible (if that's enough) */
307307
need = dbc->amax;
308-
WARNH(dbc, "libcurl: capped buffer to max: %zd", need);
308+
WARNH(dbc, "libcurl: capped buffer to max: %zu", need);
309309
/* re'eval what I have available... */
310310
avail = dbc->amax - dbc->apos;
311311
if (avail < have) {
@@ -321,7 +321,7 @@ static size_t write_callback(char *ptr, size_t size, size_t nmemb,
321321
* the chunk right past the indicated JSON length.) */
322322
wbuf = realloc(dbc->abuff, need + /*\0*/1);
323323
if (! wbuf) {
324-
ERRNH(dbc, "libcurl: failed to realloc to %zdB.", need);
324+
ERRNH(dbc, "libcurl: failed to realloc to %zuB.", need);
325325
return 0;
326326
}
327327
dbc->abuff = wbuf;
@@ -332,7 +332,7 @@ static size_t write_callback(char *ptr, size_t size, size_t nmemb,
332332
dbc->apos += have;
333333
/* Add the 0-term for UJSON4C (but don't count it - see above) */
334334
dbc->abuff[dbc->apos] = '\0';
335-
DBGH(dbc, "libcurl: copied %zdB: `%.*s`.", have, have, ptr);
335+
DBGH(dbc, "libcurl: copied %zuB: `%.*s`.", have, have, ptr);
336336

337337
/*
338338
* "Your callback should return the number of bytes actually taken care
@@ -347,8 +347,8 @@ static size_t write_callback(char *ptr, size_t size, size_t nmemb,
347347
return have;
348348

349349
too_large:
350-
ERRH(dbc, "libcurl: at %zd and can't grow past max %zd for new chunk of "
351-
"%zd bytes.", dbc->apos, dbc->amax, have);
350+
ERRH(dbc, "libcurl: at %zu and can't grow past max %zu for new chunk of "
351+
"%zu bytes.", dbc->apos, dbc->amax, have);
352352
return 0;
353353
}
354354

@@ -750,6 +750,7 @@ SQLRETURN post_json(esodbc_stmt_st *stmt, int url_type, const cstr_st *u8body)
750750
ESODBC_MUX_UNLOCK(&dbc->curl_mux);
751751
return (url_type == ESODBC_CURL_QUERY) ?
752752
attach_answer(stmt, resp.str, resp.cnt) :
753+
/* ESODBC_CURL_CLOSE */
753754
close_es_answ_handler(stmt, resp.str, resp.cnt);
754755
} else {
755756
ERRH(stmt, "received 200 response code with empty body.");
@@ -920,9 +921,8 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
920921
ipv6 ? "[" : "", LWSTR(&attrs->server), ipv6 ? "]" : "",
921922
LWSTR(&attrs->port));
922923
if (cnt <= 0) {
923-
ERRNH(dbc, "failed to print SQL URL out of server: `" LWPDL "` [%zd], "
924-
"port: `" LWPDL "` [%zd].", LWSTR(&attrs->server),
925-
LWSTR(&attrs->port));
924+
ERRNH(dbc, "failed to print SQL URL out of server: `" LWPDL "`, "
925+
"port: `" LWPDL "`.", LWSTR(&attrs->server), LWSTR(&attrs->port));
926926
SET_HDIAG(dbc, SQL_STATE_HY000, "printing server's SQL URL failed", 0);
927927
goto err;
928928
} else {
@@ -957,9 +957,8 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
957957
ipv6 ? "[" : "", LWSTR(&attrs->server), ipv6 ? "]" : "",
958958
LWSTR(&attrs->port));
959959
if (cnt <= 0) {
960-
ERRNH(dbc, "failed to print root URL out of server: `" LWPDL "` [%zd],"
961-
" port: `" LWPDL "` [%zd].", LWSTR(&attrs->server),
962-
LWSTR(&attrs->port));
960+
ERRNH(dbc, "failed to print root URL out of server: `" LWPDL "`,"
961+
" port: `" LWPDL "`.", LWSTR(&attrs->server), LWSTR(&attrs->port));
963962
SET_HDIAG(dbc, SQL_STATE_HY000, "printing server's URL failed", 0);
964963
goto err;
965964
} else {
@@ -978,16 +977,16 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
978977
*/
979978
if (attrs->uid.cnt) {
980979
if (! wstr_to_utf8(&attrs->uid, &dbc->uid)) {
981-
ERRH(dbc, "failed to convert username [%zd] `" LWPDL "` to UTF8.",
980+
ERRH(dbc, "failed to convert username [%zu] `" LWPDL "` to UTF8.",
982981
attrs->uid.cnt, LWSTR(&attrs->uid));
983982
SET_HDIAG(dbc, SQL_STATE_HY000, "username UTF8 conversion "
984983
"failed", 0);
985984
goto err;
986985
}
987986
if (attrs->pwd.cnt) {
988987
if (! wstr_to_utf8(&attrs->pwd, &dbc->pwd)) {
989-
ERRH(dbc, "failed to convert password [%zd] (-not shown-) to "
990-
"UTF8.", attrs->pwd.cnt);
988+
ERRH(dbc, "failed to convert password [%zu] `%s` to "
989+
"UTF8.", attrs->pwd.cnt, ESODBC_PWD_VAL_SUBST);
991990
SET_HDIAG(dbc, SQL_STATE_HY000, "password UTF8 "
992991
"conversion failed", 0);
993992
goto err;
@@ -1040,7 +1039,7 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
10401039
WARNH(dbc, "no reply body limit set.");
10411040
}
10421041
}
1043-
INFOH(dbc, "max body size: %zd.", dbc->amax);
1042+
INFOH(dbc, "max body size: %zu.", dbc->amax);
10441043

10451044
/*
10461045
* set max fetch size
@@ -1067,7 +1066,7 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
10671066
dbc->fetch.slen = (char)attrs->max_fetch_size.cnt;
10681067
dbc->fetch.str = malloc(dbc->fetch.slen + /*\0*/1);
10691068
if (! dbc->fetch.str) {
1070-
ERRNH(dbc, "failed to alloc %zdB.", dbc->fetch.slen);
1069+
ERRNH(dbc, "failed to alloc %cB.", dbc->fetch.slen);
10711070
RET_HDIAGS(dbc, SQL_STATE_HY001);
10721071
}
10731072
dbc->fetch.str[dbc->fetch.slen] = 0;
@@ -1524,8 +1523,8 @@ static BOOL elastic_intervals_name2types(wstr_st *type_name,
15241523
break;
15251524
}
15261525

1527-
ERR("unrecognized Elastic type `" LWPDL "` (%zd).", LWSTR(type_name),
1528-
type_name->cnt);
1526+
ERR("unrecognized Elastic type: [%zu] `" LWPDL "`.", type_name->cnt,
1527+
LWSTR(type_name));
15291528
return FALSE;
15301529
}
15311530

@@ -2170,7 +2169,7 @@ static BOOL load_es_types(esodbc_dbc_st *dbc)
21702169
ERRH(stmt, "Elasticsearch returned no type as supported.");
21712170
goto end;
21722171
} else if (ESODBC_MAX_NO_TYPES < row_cnt) {
2173-
ERRH(stmt, "Elasticsearch returned too many types (%d vs limit %zd).",
2172+
ERRH(stmt, "Elasticsearch returned too many types (%ld vs limit %d).",
21742173
row_cnt, ESODBC_MAX_NO_TYPES);
21752174
goto end;
21762175
} else {
@@ -2186,7 +2185,7 @@ static BOOL load_es_types(esodbc_dbc_st *dbc)
21862185
/* indicate rowset size */
21872186
if (! SQL_SUCCEEDED(EsSQLSetStmtAttrW(stmt, SQL_ATTR_ROW_ARRAY_SIZE,
21882187
(SQLPOINTER)ESODBC_MAX_NO_TYPES, 0))) {
2189-
ERRH(stmt, "failed to set rowset size (%zd).",
2188+
ERRH(stmt, "failed to set rowset size (%d).",
21902189
ESODBC_MAX_NO_TYPES);
21912190
goto end;
21922191
}
@@ -2481,7 +2480,7 @@ SQLRETURN EsSQLDriverConnectW
24812480
cnt += attrs.server.cnt + /*\0*/1;
24822481
dbc->dsn.str = malloc(cnt * sizeof(SQLWCHAR)); /* alloc for both */
24832482
if (! dbc->dsn.str) {
2484-
ERRNH(dbc, "OOM for %zdB.", (orig_dsn.cnt + 1) * sizeof(SQLWCHAR));
2483+
ERRNH(dbc, "OOM for %zuB.", (orig_dsn.cnt + 1) * sizeof(SQLWCHAR));
24852484
RET_HDIAGS(dbc, SQL_STATE_HY001);
24862485
}
24872486
/* copy DSN */

driver/handles.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ SQLRETURN EsSQLFreeStmt(SQLHSTMT StatementHandle, SQLUSMALLINT Option)
451451
* pending results." */
452452
case SQL_CLOSE:
453453
DBGH(stmt, "closing.");
454-
clear_desc(stmt->ird, FALSE /*keep the header values*/);
454+
clear_desc(stmt->ird, TRUE);
455455
break;
456456

457457
/* "Sets the SQL_DESC_COUNT field of the APD to 0, releasing all

driver/info.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,10 +1209,9 @@ SQLRETURN EsSQLGetTypeInfoW(SQLHSTMT StatementHandle, SQLSMALLINT DataType)
12091209
{
12101210
#define SQL_TYPES_STMT "SYS TYPES"
12111211

1212-
SQLRETURN ret;
12131212
esodbc_stmt_st *stmt = STMH(StatementHandle);
12141213
SQLWCHAR wbuff[sizeof(SQL_TYPES_STMT " 32767")];
1215-
size_t cnt;
1214+
int cnt;
12161215

12171216
DBGH(stmt, "requested type description for type %hd.", DataType);
12181217
cnt = swprintf(wbuff, sizeof(wbuff)/sizeof(*wbuff),
@@ -1222,13 +1221,7 @@ SQLRETURN EsSQLGetTypeInfoW(SQLHSTMT StatementHandle, SQLSMALLINT DataType)
12221221
RET_HDIAGS(stmt, SQL_STATE_HY000);
12231222
}
12241223

1225-
ret = EsSQLFreeStmt(stmt, ESODBC_SQL_CLOSE);
1226-
assert(SQL_SUCCEEDED(ret)); /* can't return error */
1227-
ret = attach_sql(stmt, wbuff, cnt);
1228-
if (SQL_SUCCEEDED(ret)) {
1229-
ret = EsSQLExecute(stmt);
1230-
}
1231-
return ret;
1224+
return EsSQLExecDirectW(stmt, wbuff, cnt);
12321225

12331226
# undef SQL_TYPES_STMT
12341227
}

0 commit comments

Comments
 (0)