Skip to content

Fix: allow (re)setting the current catalog #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions driver/catalogue.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,36 @@ SQLRETURN EsSQLStatisticsW(
# undef STATISTICS_EMPTY
}

BOOL TEST_API set_current_catalog(esodbc_dbc_st *dbc, wstr_st *catalog)
{
if (dbc->catalog.cnt) {
DBGH(dbc, "catalog already set to `" LWPDL "`.", LWSTR(&dbc->catalog));
if (! EQ_WSTR(&dbc->catalog, catalog)) {
/* this should never happen, as cluster's name is not updateable
* on the fly. */
ERRH(dbc, "overwriting previously set catalog value!");
free(dbc->catalog.str);
dbc->catalog.str = NULL;
dbc->catalog.cnt = 0;
} else {
return FALSE;
}
}
if (! catalog->cnt) {
WARNH(dbc, "attempting to set catalog name to empty value.");
return FALSE;
}
if (! (dbc->catalog.str = malloc((catalog->cnt + 1) * sizeof(SQLWCHAR)))) {
ERRNH(dbc, "OOM for %zu wchars.", catalog->cnt + 1);
return FALSE;
}
wmemcpy(dbc->catalog.str, catalog->str, catalog->cnt);
dbc->catalog.str[catalog->cnt] = L'\0';
dbc->catalog.cnt = catalog->cnt;
INFOH(dbc, "current catalog name: `" LWPDL "`.", LWSTR(&dbc->catalog));

return TRUE;
}

/* writes into 'dest', of size 'room', the current requested attr. of 'dbc'.
* returns negative on error, or the char count written otherwise */
Expand Down Expand Up @@ -175,6 +205,9 @@ SQLSMALLINT fetch_server_attr(esodbc_dbc_st *dbc, SQLINTEGER attr_id,
/* 0-term room left out when binding */
buff[attr_val.cnt] = L'\0'; /* write_wstr() expects the 0-term */
}
if (attr_id == SQL_ATTR_CURRENT_CATALOG) {
set_current_catalog(dbc, &attr_val);
}
}
DBGH(dbc, "attribute %ld value: `" LWPDL "`.", attr_id, LWSTR(&attr_val));

Expand Down
1 change: 1 addition & 0 deletions driver/catalogue.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

SQLSMALLINT fetch_server_attr(esodbc_dbc_st *dbc, SQLINTEGER attr_id,
SQLWCHAR *dest, SQLSMALLINT room);
BOOL TEST_API set_current_catalog(esodbc_dbc_st *dbc, wstr_st *catalog);


SQLRETURN EsSQLStatisticsW(
Expand Down
42 changes: 39 additions & 3 deletions driver/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,13 @@ void cleanup_dbc(esodbc_dbc_st *dbc)
dbc->srv_ver.string.str = NULL;
dbc->srv_ver.string.cnt = 0;
}
if (dbc->catalog.str) {
free(dbc->catalog.str);
dbc->catalog.str = NULL;
dbc->catalog.cnt = 0;
} else {
assert(dbc->catalog.cnt == 0);
}

assert(dbc->abuff == NULL);
cleanup_curl(dbc);
Expand Down Expand Up @@ -3082,7 +3089,37 @@ SQLRETURN EsSQLDisconnect(SQLHDBC ConnectionHandle)
return SQL_SUCCESS;
}


/* ES/SQL doesn't support catalogs (yet). This function checks that a
* previously retrieved (and cached) catalog value is the same with what the
* app currently tries to set it to.
* Ideally, the app provided value would be cached here too (as per the spec:
* "SQL_ATTR_CURRENT_CATALOG can be set before or after connecting"), in case
* there's no connection "established" yet and checked at "establishment"
* time. But there's no client reported yet setting a catalog value before
* connecting. */
static SQLRETURN check_catalog_name(esodbc_dbc_st *dbc, SQLWCHAR *name,
SQLINTEGER len)
{
wstr_st catalog;
catalog.str = name;
if (len < 0) {
catalog.cnt = wcslen(name);
} else {
catalog.cnt = (size_t)len;
}
if (! EQ_WSTR(&dbc->catalog, &catalog)) {
if (! dbc->catalog.cnt) {
/* this will happen if the app tries to set a value that it
* discovered over a different connection.
* TODO on a first reported issue. */
WARNH(dbc, "connection's current catalog not yet set!");
}
ERRH(dbc, "setting catalog name not supported.");
RET_HDIAGS(dbc, SQL_STATE_HYC00);
}
WARNH(dbc, "ignoring attempt to set the current catalog.");
return SQL_SUCCESS;
}

/*
* https://docs.microsoft.com/en-us/sql/odbc/reference/develop-app/unicode-drivers :
Expand Down Expand Up @@ -3222,8 +3259,7 @@ SQLRETURN EsSQLSetConnectAttrW(
/* string should be 0-term'd */
0 <= StringLength ? StringLength : SHRT_MAX,
(SQLWCHAR *)Value);
ERRH(dbc, "setting catalog name not supported.");
RET_HDIAGS(dbc, SQL_STATE_HYC00);
return check_catalog_name(dbc, (SQLWCHAR *)Value, StringLength);

case SQL_ATTR_TRACE:
case SQL_ATTR_TRACEFILE: /* DM-only */
Expand Down
3 changes: 2 additions & 1 deletion driver/handles.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ typedef struct struct_dbc {

wstr_st dsn; /* data source name SQLGetInfo(SQL_DATA_SOURCE_NAME) */
wstr_st server; /* ~ name; requested with SQLGetInfo(SQL_SERVER_NAME) */
wstr_st catalog; /* cached value; checked against if app setting it */
union {
wstr_st string; /* version: SQLGetInfo(SQL_DBMS_VER) */
unsigned char checking; /* first letter of DSN config option */
unsigned char checking; /* first letter of DSN config option value */
} srv_ver; /* server version */
cstr_st url; /* SQL URL (posts) */
cstr_st close_url; /* SQL close URL (posts) */
Expand Down
40 changes: 40 additions & 0 deletions test/test_driverconnect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <gtest/gtest.h>
#include "connected_dbc.h"

extern "C" {
#include "catalogue.h" /* set_current_catalog() */
}

namespace test {

class DriverConnect : public ::testing::Test, public ConnectedDBC
Expand Down Expand Up @@ -70,5 +74,41 @@ TEST_F(DriverConnect, OutputTruncated)

}

TEST_F(DriverConnect, ReinitCurrentCatalog)
{
ret = SQLDriverConnect(my_dbc, (SQLHWND)&types, (SQLWCHAR *)CONNECT_STRING,
sizeof(CONNECT_STRING) / sizeof(CONNECT_STRING[0]) - 1, NULL, 0,
&out_avail, ESODBC_SQL_DRIVER_TEST);
ASSERT_TRUE(SQL_SUCCEEDED(ret));

esodbc_dbc_st *dbc = (esodbc_dbc_st *)my_dbc;
wstr_st crr_cat = WSTR_INIT("crr_catalog");
ASSERT_TRUE(set_current_catalog(dbc, &crr_cat));
ASSERT_FALSE(set_current_catalog(dbc, &crr_cat));

wstr_st other_cat = WSTR_INIT("other_catalog");
ASSERT_TRUE(set_current_catalog(dbc, &other_cat));
}

TEST_F(DriverConnect, ResetCurrentCatalog)
{
ret = SQLDriverConnect(my_dbc, (SQLHWND)&types, (SQLWCHAR *)CONNECT_STRING,
sizeof(CONNECT_STRING) / sizeof(CONNECT_STRING[0]) - 1, NULL, 0,
&out_avail, ESODBC_SQL_DRIVER_TEST);
ASSERT_TRUE(SQL_SUCCEEDED(ret));

esodbc_dbc_st *dbc = (esodbc_dbc_st *)my_dbc;
wstr_st crr_cat = WSTR_INIT("crr_catalog");
ASSERT_TRUE(set_current_catalog(dbc, &crr_cat));
ASSERT_TRUE(SQL_SUCCEEDED(SQLSetConnectAttrW(my_dbc,
SQL_ATTR_CURRENT_CATALOG, (SQLPOINTER)crr_cat.str,
(SQLINTEGER)crr_cat.cnt)));

wstr_st other_cat = WSTR_INIT("other_catalog");
ASSERT_FALSE(SQL_SUCCEEDED(SQLSetConnectAttrW(my_dbc,
SQL_ATTR_CURRENT_CATALOG, (SQLPOINTER)other_cat.str,
(SQLINTEGER)other_cat.cnt)));
}

} // test namespace