Skip to content

Commit

Permalink
Introduce CBOR format support for REST payloads (#169)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
bpintea committed Aug 28, 2019
1 parent 42eed3a commit 1dad781
Show file tree
Hide file tree
Showing 77 changed files with 13,316 additions and 536 deletions.
30 changes: 29 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,33 @@ add_custom_target(curlclean
WORKING_DIRECTORY "${LIBCURL_PATH_SRC}/winbuild"
)

#
# add tinycbor to the project
#
set(TINYCBOR_PATH_SRC ${CMAKE_SOURCE_DIR}/libs/tinycbor CACHE PATH
"Lib tinycbor source path")
aux_source_directory(${TINYCBOR_PATH_SRC}/src DRV_SRC)
list(FILTER DRV_SRC EXCLUDE REGEX .*open_memstream.c$) # Win-unsupported
list(FILTER DRV_SRC EXCLUDE REGEX .*cborparser.c$) # to be patched
file(COPY ${TINYCBOR_PATH_SRC}/src/cborparser.c DESTINATION ${CMAKE_BINARY_DIR})
# tinycbor doesn't expose (yet? #125) the text/binary string pointer, since the
# string can span multiple stream chunks. However, in our case the CBOR object
# is available entirely, so access to it can safely be had; this saves a
# superfluous allocation/copy. FIXME -- lib PR, proper exposure.
file(APPEND ${CMAKE_BINARY_DIR}/cborparser.c
"
CborError cbor_value_get_string_chunk(CborValue *it,
const void **bufferptr, size_t *len)
{
CborError err = get_string_chunk(it, bufferptr, len);
return err != CborNoError ? err : preparse_next_value(it);
}")
aux_source_directory(${CMAKE_BINARY_DIR} DRV_SRC)
set(TINYCBOR_INC ${TINYCBOR_PATH_SRC}/src)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /DWITHOUT_OPEN_MEMSTREAM")
# limit how deep the parser will recurse (current need: 3)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /DCBOR_PARSER_MAX_RECURSIONS=16")

#
# Patch installer's and editor's AssemblyInfos with the version being built
#
Expand Down Expand Up @@ -339,7 +366,8 @@ add_library(${DRV_NAME} SHARED ${DRV_SRC} ${CMAKE_BINARY_DIR}/${DRV_NAME}.def
target_compile_definitions(${DRV_NAME} PRIVATE "DRIVER_BUILD")
add_dependencies(${DRV_NAME} dsneditor)
include_directories(${ODBC_INC} ${DRV_SRC_DIR} ${LIBCURL_INC_PATH}
${UJSON4C_INC} ${CTIMESTAMP_PATH_SRC} ${DSNEDITOR_INC_PATH})
${UJSON4C_INC} ${CTIMESTAMP_PATH_SRC} ${TINYCBOR_INC}
${DSNEDITOR_INC_PATH})
target_link_libraries(${DRV_NAME} odbccp32 legacy_stdio_definitions
${DSNBND_LIB_BIN_DIR_BASE}-$<CONFIG>/esdsnbnd${BARCH}${CMAKE_IMPORT_LIBRARY_SUFFIX}
libcurl ${LIBCURL_WIN_LIBS})
Expand Down
2 changes: 2 additions & 0 deletions devtools/3rd_party/licenses/tinycbor-INFO.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name,version,revision,url,license,copyright
tinycbor,,d2dd95c,https://github.com/intel/tinycbor.git,MIT,Copyright (c) 2017 Intel Corporation
21 changes: 21 additions & 0 deletions devtools/3rd_party/licenses/tinycbor-LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Intel Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Empty file.
26 changes: 13 additions & 13 deletions driver/catalogue.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
" CATALOG " ESODBC_STRING_DELIM WPFWP_LDESC ESODBC_STRING_DELIM


static SQLRETURN fake_answer(SQLHSTMT hstmt, const char *src, size_t cnt)
static SQLRETURN fake_answer(SQLHSTMT hstmt, cstr_st *answer)
{
char *dup;
cstr_st fake = *answer;

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

}

Expand Down Expand Up @@ -86,10 +86,10 @@ SQLRETURN EsSQLStatisticsW(
"\"rows\":[]" \
"}"
/*INDENT-ON*/
cstr_st statistics = CSTR_INIT(STATISTICS_EMPTY);

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

# undef STATISTICS_EMPTY
}
Expand Down Expand Up @@ -614,11 +614,11 @@ SQLRETURN EsSQLSpecialColumnsW
"\"rows\":[]" \
"}"
/*INDENT-ON*/
cstr_st special_cols = CSTR_INIT(SPECIAL_COLUMNS_EMPTY);


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

# undef SPECIAL_COLUMNS_EMPTY
}
Expand Down Expand Up @@ -661,10 +661,10 @@ SQLRETURN EsSQLForeignKeysW(
"\"rows\":[]" \
"}"
/*INDENT-ON*/
cstr_st foreign_keys = CSTR_INIT(FOREIGN_KEYS_EMPTY);

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

# undef FOREIGN_KEYS_EMPTY
}
Expand Down Expand Up @@ -692,10 +692,10 @@ SQLRETURN EsSQLPrimaryKeysW(
"\"rows\":[]" \
"}"
/*INDENT-ON*/
cstr_st prim_keys = CSTR_INIT(PRIMARY_KEYS_EMPTY);

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

# undef PRIMARY_KEYS_EMPTY
}
Expand Down
Loading

0 comments on commit 1dad781

Please sign in to comment.