Skip to content

Commit bc6f471

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 picked from commit c7aa95b)
1 parent 1dad781 commit bc6f471

File tree

5 files changed

+31
-48
lines changed

5 files changed

+31
-48
lines changed

driver/catalogue.c

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

418418
DBGH(stmt, "tables catalog SQL [%zu]:`" LWPDL "`.", pos, (int)pos, pbuf);
419419

420-
ret = EsSQLFreeStmt(stmt, ESODBC_SQL_CLOSE);
421-
assert(SQL_SUCCEEDED(ret)); /* can't return error */
422-
ret = attach_sql(stmt, pbuf, pos);
423-
if (SQL_SUCCEEDED(ret)) {
424-
ret = EsSQLExecute(stmt);
425-
}
420+
ret = EsSQLExecDirectW(stmt, pbuf, (SQLINTEGER)pos);
426421
end:
427422
free(pbuf);
428423
return ret;
@@ -573,12 +568,7 @@ SQLRETURN EsSQLColumnsW
573568
goto end;
574569
}
575570

576-
ret = EsSQLFreeStmt(stmt, ESODBC_SQL_CLOSE);
577-
assert(SQL_SUCCEEDED(ret)); /* can't return error */
578-
ret = attach_sql(stmt, pbuf, cnt);
579-
if (SQL_SUCCEEDED(ret)) {
580-
ret = EsSQLExecute(stmt);
581-
}
571+
ret = EsSQLExecDirectW(stmt, pbuf, (SQLINTEGER)cnt);
582572
end:
583573
free(pbuf);
584574
return ret;

driver/connect.c

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

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

255255
/* do I need to grow the existing buffer? */
256256
if (avail < have) {
@@ -260,14 +260,14 @@ static size_t write_callback(char *ptr, size_t size, size_t nmemb,
260260
while (need < dbc->apos + have) {
261261
need *= 2;
262262
}
263-
DBGH(dbc, "libcurl: need to grow buffer for new chunk of %zd "
264-
"from %zd to %zd bytes.", have, dbc->alen, need);
263+
DBGH(dbc, "libcurl: need to grow buffer for new chunk of %zu "
264+
"from %zu to %zu bytes.", have, dbc->alen, need);
265265
if (dbc->amax && (dbc->amax < need)) { /* do I need more than max? */
266266
if (dbc->amax <= (size_t)dbc->alen) { /* am I at max already? */
267267
goto too_large;
268268
} else { /* am not: alloc max possible (if that's enough) */
269269
need = dbc->amax;
270-
WARNH(dbc, "libcurl: capped buffer to max: %zd", need);
270+
WARNH(dbc, "libcurl: capped buffer to max: %zu", need);
271271
/* re'eval what I have available... */
272272
avail = dbc->amax - dbc->apos;
273273
if (avail < have) {
@@ -283,7 +283,7 @@ static size_t write_callback(char *ptr, size_t size, size_t nmemb,
283283
* the chunk right past the indicated JSON length.) */
284284
wbuf = realloc(dbc->abuff, need + /*\0*/1);
285285
if (! wbuf) {
286-
ERRNH(dbc, "libcurl: failed to realloc to %zdB.", need);
286+
ERRNH(dbc, "libcurl: failed to realloc to %zuB.", need);
287287
return 0;
288288
}
289289
dbc->abuff = wbuf;
@@ -294,7 +294,7 @@ static size_t write_callback(char *ptr, size_t size, size_t nmemb,
294294
dbc->apos += have;
295295
/* Add the 0-term for UJSON4C (but don't count it - see above) */
296296
dbc->abuff[dbc->apos] = '\0';
297-
DBGH(dbc, "libcurl: copied %zdB: `%.*s`.", have, have, ptr);
297+
DBGH(dbc, "libcurl: copied %zuB: `%.*s`.", have, have, ptr);
298298

299299
/*
300300
* "Your callback should return the number of bytes actually taken care
@@ -309,8 +309,8 @@ static size_t write_callback(char *ptr, size_t size, size_t nmemb,
309309
return have;
310310

311311
too_large:
312-
ERRH(dbc, "libcurl: at %zd and can't grow past max %zd for new chunk of "
313-
"%zd bytes.", dbc->apos, dbc->amax, have);
312+
ERRH(dbc, "libcurl: at %zu and can't grow past max %zu for new chunk of "
313+
"%zu bytes.", dbc->apos, dbc->amax, have);
314314
return 0;
315315
}
316316

@@ -779,6 +779,7 @@ SQLRETURN curl_post(esodbc_stmt_st *stmt, int url_type,
779779
ESODBC_MUX_UNLOCK(&dbc->curl_mux);
780780
return (url_type == ESODBC_CURL_QUERY) ?
781781
attach_answer(stmt, &rsp_body, is_json) :
782+
/* ESODBC_CURL_CLOSE */
782783
close_es_answ_handler(stmt, &rsp_body, is_json);
783784
} else {
784785
ERRH(stmt, "received 200 response code with empty body.");
@@ -1176,9 +1177,8 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
11761177
ipv6 ? "[" : "", LWSTR(&attrs->server), ipv6 ? "]" : "",
11771178
LWSTR(&attrs->port));
11781179
if (cnt <= 0) {
1179-
ERRNH(dbc, "failed to print SQL URL out of server: `" LWPDL "` [%zd], "
1180-
"port: `" LWPDL "` [%zd].", LWSTR(&attrs->server),
1181-
LWSTR(&attrs->port));
1180+
ERRNH(dbc, "failed to print SQL URL out of server: `" LWPDL "`, "
1181+
"port: `" LWPDL "`.", LWSTR(&attrs->server), LWSTR(&attrs->port));
11821182
SET_HDIAG(dbc, SQL_STATE_HY000, "printing server's SQL URL failed", 0);
11831183
goto err;
11841184
} else {
@@ -1213,9 +1213,8 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
12131213
ipv6 ? "[" : "", LWSTR(&attrs->server), ipv6 ? "]" : "",
12141214
LWSTR(&attrs->port));
12151215
if (cnt <= 0) {
1216-
ERRNH(dbc, "failed to print root URL out of server: `" LWPDL "` [%zd],"
1217-
" port: `" LWPDL "` [%zd].", LWSTR(&attrs->server),
1218-
LWSTR(&attrs->port));
1216+
ERRNH(dbc, "failed to print root URL out of server: `" LWPDL "`,"
1217+
" port: `" LWPDL "`.", LWSTR(&attrs->server), LWSTR(&attrs->port));
12191218
SET_HDIAG(dbc, SQL_STATE_HY000, "printing server's URL failed", 0);
12201219
goto err;
12211220
} else {
@@ -1234,16 +1233,16 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
12341233
*/
12351234
if (attrs->uid.cnt) {
12361235
if (! wstr_to_utf8(&attrs->uid, &dbc->uid)) {
1237-
ERRH(dbc, "failed to convert username [%zd] `" LWPDL "` to UTF8.",
1236+
ERRH(dbc, "failed to convert username [%zu] `" LWPDL "` to UTF8.",
12381237
attrs->uid.cnt, LWSTR(&attrs->uid));
12391238
SET_HDIAG(dbc, SQL_STATE_HY000, "username UTF8 conversion "
12401239
"failed", 0);
12411240
goto err;
12421241
}
12431242
if (attrs->pwd.cnt) {
12441243
if (! wstr_to_utf8(&attrs->pwd, &dbc->pwd)) {
1245-
ERRH(dbc, "failed to convert password [%zd] (-not shown-) to "
1246-
"UTF8.", attrs->pwd.cnt);
1244+
ERRH(dbc, "failed to convert password [%zu] `%s` to "
1245+
"UTF8.", attrs->pwd.cnt, ESODBC_PWD_VAL_SUBST);
12471246
SET_HDIAG(dbc, SQL_STATE_HY000, "password UTF8 "
12481247
"conversion failed", 0);
12491248
goto err;
@@ -1296,7 +1295,7 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
12961295
WARNH(dbc, "no reply body limit set.");
12971296
}
12981297
}
1299-
INFOH(dbc, "max body size: %zd.", dbc->amax);
1298+
INFOH(dbc, "max body size: %zu.", dbc->amax);
13001299

13011300
/*
13021301
* set max fetch size
@@ -1323,7 +1322,7 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
13231322
dbc->fetch.slen = (char)attrs->max_fetch_size.cnt;
13241323
dbc->fetch.str = malloc(dbc->fetch.slen + /*\0*/1);
13251324
if (! dbc->fetch.str) {
1326-
ERRNH(dbc, "failed to alloc %zdB.", dbc->fetch.slen);
1325+
ERRNH(dbc, "failed to alloc %cB.", dbc->fetch.slen);
13271326
RET_HDIAGS(dbc, SQL_STATE_HY001);
13281327
}
13291328
dbc->fetch.str[dbc->fetch.slen] = 0;
@@ -1922,8 +1921,8 @@ static BOOL elastic_intervals_name2types(wstr_st *type_name,
19221921
break;
19231922
}
19241923

1925-
ERR("unrecognized Elastic type `" LWPDL "` (%zd).", LWSTR(type_name),
1926-
type_name->cnt);
1924+
ERR("unrecognized Elastic type: [%zu] `" LWPDL "`.", type_name->cnt,
1925+
LWSTR(type_name));
19271926
return FALSE;
19281927
}
19291928

@@ -2601,7 +2600,7 @@ static BOOL load_es_types(esodbc_dbc_st *dbc)
26012600
ERRH(stmt, "Elasticsearch returned no type as supported.");
26022601
goto end;
26032602
} else if (ESODBC_MAX_NO_TYPES < row_cnt) {
2604-
ERRH(stmt, "Elasticsearch returned too many types (%d vs limit %zd).",
2603+
ERRH(stmt, "Elasticsearch returned too many types (%ld vs limit %d).",
26052604
row_cnt, ESODBC_MAX_NO_TYPES);
26062605
goto end;
26072606
} else {
@@ -2617,7 +2616,7 @@ static BOOL load_es_types(esodbc_dbc_st *dbc)
26172616
/* indicate rowset size */
26182617
if (! SQL_SUCCEEDED(EsSQLSetStmtAttrW(stmt, SQL_ATTR_ROW_ARRAY_SIZE,
26192618
(SQLPOINTER)ESODBC_MAX_NO_TYPES, 0))) {
2620-
ERRH(stmt, "failed to set rowset size (%zd).",
2619+
ERRH(stmt, "failed to set rowset size (%d).",
26212620
ESODBC_MAX_NO_TYPES);
26222621
goto end;
26232622
}
@@ -2916,7 +2915,7 @@ SQLRETURN EsSQLDriverConnectW
29162915
cnt += attrs.server.cnt + /*\0*/1;
29172916
dbc->dsn.str = malloc(cnt * sizeof(SQLWCHAR)); /* alloc for both */
29182917
if (! dbc->dsn.str) {
2919-
ERRNH(dbc, "OOM for %zdB.", (orig_dsn.cnt + 1) * sizeof(SQLWCHAR));
2918+
ERRNH(dbc, "OOM for %zuB.", (orig_dsn.cnt + 1) * sizeof(SQLWCHAR));
29202919
RET_HDIAGS(dbc, SQL_STATE_HY001);
29212920
}
29222921
/* copy DSN */

driver/convert.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3300,7 +3300,8 @@ static SQLRETURN wstr_to_string(esodbc_rec_st *arec, esodbc_rec_st *irec,
33003300
wstr_to_wstr(arec, irec, data_ptr, octet_len_ptr, wstr, chars_0);
33013301

33023302
/* if truncation occured, only succeed if fractional seconds are cut out */
3303-
if (min_xfer && HDRH(stmt)->diag.state == SQL_STATE_01004) {
3303+
if (ret == SQL_SUCCESS_WITH_INFO && min_xfer &&
3304+
HDRH(stmt)->diag.state == SQL_STATE_01004) {
33043305
assert(SQL_SUCCEEDED(ret));
33053306

33063307
usize = (arec_type == SQL_C_CHAR) ? sizeof(SQLCHAR) : sizeof(SQLWCHAR);

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)