Skip to content

Commit 1dad781

Browse files
committed
Introduce CBOR format support for REST payloads (#169)
* Squashed 'libs/tinycbor/' content from commit d2dd95c git-subtree-dir: libs/tinycbor git-subtree-split: d2dd95cb8841d88d5a801e3ef9c328fd6200e7bd * add tinycbor library to the project With this commit CMake will add the tinycbor files to the sources to be compiled into the driver. CMake will exclude two files: - open_memstream.c that won't be compiled by MSVC (WITHOUT_OPEN_MEMSTREAM compilation flag doesn't exclude its source); - cborparser.c that will be copied (and patched) under the building folder. The patching of the later file above adds one function that allows copy-free extraction of text/byte strings. * introduce CBOR format support for REST payloads This commit adds basic support for CBOR encapsulation, as an alternative to JSON. The format to use is connection-specific and configured in the connection string. The introduction stops short of supporting encoding/decoding of the parameters and result sets values: it will allow building a non-parameterized query and decoding the response object (either with a result-set or with an error), but without unpacking the values in the result set of the latter. Server version querying is also carried over CBOR. All communication is done either over JSON or CBOR, although the driver will carry on if it receives a JSON response to a CBOR request (or the other way around). However, driver-generated "fake" responses to catalog queries are always JSON-formatted (makes maintenance of text responses easier). The commit also enhances the support of the Elasticsearch-formatted errors (both JSON and CBOR) in that the "error" parameter will be parsed if of a map type (generally an ES/SQL error) or passed on as is, if of a string type. The previous behavior was to abandon parsing if "error" wasn't a map and present the entire error answer to the user; this wouldn't work well with a CBOR object now. * fix a copy&paste define error - the define is not yet used, though. * add the legal files for tinycbor as 3rd party lib. - add to the repo the files necessary to generate the legal notices and reports. * addressing PR review comments - reducing code duplication on srv. version checking; - fixing a couple of comment typos; - assigning const strings to named vars. (cherry picked from commit 992f0e0)
1 parent 42eed3a commit 1dad781

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+13316
-536
lines changed

CMakeLists.txt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,33 @@ add_custom_target(curlclean
283283
WORKING_DIRECTORY "${LIBCURL_PATH_SRC}/winbuild"
284284
)
285285

286+
#
287+
# add tinycbor to the project
288+
#
289+
set(TINYCBOR_PATH_SRC ${CMAKE_SOURCE_DIR}/libs/tinycbor CACHE PATH
290+
"Lib tinycbor source path")
291+
aux_source_directory(${TINYCBOR_PATH_SRC}/src DRV_SRC)
292+
list(FILTER DRV_SRC EXCLUDE REGEX .*open_memstream.c$) # Win-unsupported
293+
list(FILTER DRV_SRC EXCLUDE REGEX .*cborparser.c$) # to be patched
294+
file(COPY ${TINYCBOR_PATH_SRC}/src/cborparser.c DESTINATION ${CMAKE_BINARY_DIR})
295+
# tinycbor doesn't expose (yet? #125) the text/binary string pointer, since the
296+
# string can span multiple stream chunks. However, in our case the CBOR object
297+
# is available entirely, so access to it can safely be had; this saves a
298+
# superfluous allocation/copy. FIXME -- lib PR, proper exposure.
299+
file(APPEND ${CMAKE_BINARY_DIR}/cborparser.c
300+
"
301+
CborError cbor_value_get_string_chunk(CborValue *it,
302+
const void **bufferptr, size_t *len)
303+
{
304+
CborError err = get_string_chunk(it, bufferptr, len);
305+
return err != CborNoError ? err : preparse_next_value(it);
306+
}")
307+
aux_source_directory(${CMAKE_BINARY_DIR} DRV_SRC)
308+
set(TINYCBOR_INC ${TINYCBOR_PATH_SRC}/src)
309+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /DWITHOUT_OPEN_MEMSTREAM")
310+
# limit how deep the parser will recurse (current need: 3)
311+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /DCBOR_PARSER_MAX_RECURSIONS=16")
312+
286313
#
287314
# Patch installer's and editor's AssemblyInfos with the version being built
288315
#
@@ -339,7 +366,8 @@ add_library(${DRV_NAME} SHARED ${DRV_SRC} ${CMAKE_BINARY_DIR}/${DRV_NAME}.def
339366
target_compile_definitions(${DRV_NAME} PRIVATE "DRIVER_BUILD")
340367
add_dependencies(${DRV_NAME} dsneditor)
341368
include_directories(${ODBC_INC} ${DRV_SRC_DIR} ${LIBCURL_INC_PATH}
342-
${UJSON4C_INC} ${CTIMESTAMP_PATH_SRC} ${DSNEDITOR_INC_PATH})
369+
${UJSON4C_INC} ${CTIMESTAMP_PATH_SRC} ${TINYCBOR_INC}
370+
${DSNEDITOR_INC_PATH})
343371
target_link_libraries(${DRV_NAME} odbccp32 legacy_stdio_definitions
344372
${DSNBND_LIB_BIN_DIR_BASE}-$<CONFIG>/esdsnbnd${BARCH}${CMAKE_IMPORT_LIBRARY_SUFFIX}
345373
libcurl ${LIBCURL_WIN_LIBS})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name,version,revision,url,license,copyright
2+
tinycbor,,d2dd95c,https://github.com/intel/tinycbor.git,MIT,Copyright (c) 2017 Intel Corporation
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Intel Corporation
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

devtools/3rd_party/licenses/tinycbor-NOTICE.txt

Whitespace-only changes.

driver/catalogue.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@
4242
" CATALOG " ESODBC_STRING_DELIM WPFWP_LDESC ESODBC_STRING_DELIM
4343

4444

45-
static SQLRETURN fake_answer(SQLHSTMT hstmt, const char *src, size_t cnt)
45+
static SQLRETURN fake_answer(SQLHSTMT hstmt, cstr_st *answer)
4646
{
47-
char *dup;
47+
cstr_st fake = *answer;
4848

49-
if (! (dup = strdup(src))) {
50-
ERRNH(hstmt, "OOM with %zu.", cnt);
49+
if (! (fake.str = strdup(answer->str))) {
50+
ERRNH(hstmt, "OOM with %zu.", fake.cnt);
5151
RET_HDIAGS(hstmt, SQL_STATE_HY001);
5252
}
53-
return attach_answer(STMH(hstmt), dup, cnt);
53+
return attach_answer(STMH(hstmt), &fake, /*is JSON*/TRUE);
5454

5555
}
5656

@@ -86,10 +86,10 @@ SQLRETURN EsSQLStatisticsW(
8686
"\"rows\":[]" \
8787
"}"
8888
/*INDENT-ON*/
89+
cstr_st statistics = CSTR_INIT(STATISTICS_EMPTY);
8990

9091
INFOH(hstmt, "no statistics available.");
91-
return fake_answer(hstmt, STATISTICS_EMPTY,
92-
sizeof(STATISTICS_EMPTY) - /*\0*/1);
92+
return fake_answer(hstmt, &statistics);
9393

9494
# undef STATISTICS_EMPTY
9595
}
@@ -614,11 +614,11 @@ SQLRETURN EsSQLSpecialColumnsW
614614
"\"rows\":[]" \
615615
"}"
616616
/*INDENT-ON*/
617+
cstr_st special_cols = CSTR_INIT(SPECIAL_COLUMNS_EMPTY);
617618

618619

619620
INFOH(hstmt, "no special columns available.");
620-
return fake_answer(hstmt, SPECIAL_COLUMNS_EMPTY,
621-
sizeof(SPECIAL_COLUMNS_EMPTY) - /*\0*/1);
621+
return fake_answer(hstmt, &special_cols);
622622

623623
# undef SPECIAL_COLUMNS_EMPTY
624624
}
@@ -661,10 +661,10 @@ SQLRETURN EsSQLForeignKeysW(
661661
"\"rows\":[]" \
662662
"}"
663663
/*INDENT-ON*/
664+
cstr_st foreign_keys = CSTR_INIT(FOREIGN_KEYS_EMPTY);
664665

665666
INFOH(hstmt, "no foreign keys supported.");
666-
return fake_answer(hstmt, FOREIGN_KEYS_EMPTY,
667-
sizeof(FOREIGN_KEYS_EMPTY) - /*\0*/1);
667+
return fake_answer(hstmt, &foreign_keys);
668668

669669
# undef FOREIGN_KEYS_EMPTY
670670
}
@@ -692,10 +692,10 @@ SQLRETURN EsSQLPrimaryKeysW(
692692
"\"rows\":[]" \
693693
"}"
694694
/*INDENT-ON*/
695+
cstr_st prim_keys = CSTR_INIT(PRIMARY_KEYS_EMPTY);
695696

696697
INFOH(hstmt, "no primary keys supported.");
697-
return fake_answer(hstmt, PRIMARY_KEYS_EMPTY,
698-
sizeof(PRIMARY_KEYS_EMPTY) - /*\0*/1);
698+
return fake_answer(hstmt, &prim_keys);
699699

700700
# undef PRIMARY_KEYS_EMPTY
701701
}

0 commit comments

Comments
 (0)