diff --git a/include/ClickHouse_Server.h b/include/ClickHouse_Server.h index ad3fb41187..b0a5fcc873 100644 --- a/include/ClickHouse_Server.h +++ b/include/ClickHouse_Server.h @@ -12,6 +12,7 @@ class ClickHouse_Session { public: SQLite3DB *sessdb; bool transfer_started; + bool schema_initialized; uint8_t sid; ClickHouse_Session(); bool init(); diff --git a/lib/ClickHouse_Server.cpp b/lib/ClickHouse_Server.cpp index e2f2b84f87..0e5a661715 100644 --- a/lib/ClickHouse_Server.cpp +++ b/lib/ClickHouse_Server.cpp @@ -1036,6 +1036,22 @@ void ClickHouse_Server_session_handler(MySQL_Session *sess, void *_pa, PtrSize_t try { clickhouse_thread___mysql_sess = sess; if (clickhouse_sess->connected == true) { + if (clickhouse_sess->schema_initialized == false) { + if (sess && sess->client_myds) { + MySQL_Data_Stream *ds = sess->client_myds; + if (ds->myconn && ds->myconn->userinfo && ds->myconn->userinfo->schemaname) { + char *sn = ds->myconn->userinfo->schemaname; + char *use_query = NULL; + use_query = (char *)malloc(strlen(sn)+8); + sprintf(use_query,"USE %s", sn); + clickhouse::Query myq(use_query); + clickhouse_sess->client->Execute(myq); + free(use_query); + } + } + clickhouse_sess->schema_initialized = true; + } + if (expected_resultset) { clickhouse_sess->client->Select(query, [](const Block& block) { ClickHouse_to_MySQL(block); } ); @@ -1105,6 +1121,7 @@ ClickHouse_Session::ClickHouse_Session() { sessdb = new SQLite3DB(); sessdb->open((char *)"file:mem_sqlitedb_clickhouse?mode=memory&cache=shared", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX); transfer_started = false; + schema_initialized = false; } bool ClickHouse_Session::init() {