You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by is...@apache.org on 2023/07/10 18:04:28 UTC
[ignite-3] branch main updated: IGNITE-19212 ODBC: Implement basic query execution (#2284)
This is an automated email from the ASF dual-hosted git repository.
isapego pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git
The following commit(s) were added to refs/heads/main by this push:
new 37651e2928 IGNITE-19212 ODBC: Implement basic query execution (#2284)
37651e2928 is described below
commit 37651e2928a2028ef8c5c842971f475208613040
Author: Igor Sapego <is...@apache.org>
AuthorDate: Mon Jul 10 22:04:23 2023 +0400
IGNITE-19212 ODBC: Implement basic query execution (#2284)
---
modules/platforms/cpp/ignite/client/CMakeLists.txt | 4 +
.../cpp/ignite/client/detail/cluster_connection.h | 2 +-
.../cpp/ignite/client/detail/node_connection.cpp | 18 +-
.../cpp/ignite/client/detail/node_connection.h | 2 +-
.../cpp/ignite/client/detail/sql/result_set_impl.h | 10 +-
modules/platforms/cpp/ignite/network/data_buffer.h | 3 +
modules/platforms/cpp/ignite/odbc/CMakeLists.txt | 1 +
.../cpp/ignite/odbc/config/configuration.cpp | 12 +
.../cpp/ignite/odbc/config/configuration.h | 19 +-
.../cpp/ignite/odbc/diagnostic/diagnosable.h | 28 +
.../ignite/odbc/diagnostic/diagnosable_adapter.cpp | 1 +
.../platforms/cpp/ignite/odbc/meta/column_meta.cpp | 15 +-
.../platforms/cpp/ignite/odbc/meta/column_meta.h | 11 +-
modules/platforms/cpp/ignite/odbc/query/cursor.h | 75 ++
.../platforms/cpp/ignite/odbc/query/data_query.cpp | 367 +++++++++
.../platforms/cpp/ignite/odbc/query/data_query.h | 212 +++++
.../platforms/cpp/ignite/odbc/sql_connection.cpp | 71 +-
modules/platforms/cpp/ignite/odbc/sql_connection.h | 97 +++
.../platforms/cpp/ignite/odbc/sql_statement.cpp | 12 +-
modules/platforms/cpp/ignite/odbc/utility.cpp | 3 +
.../{client/detail => protocol}/client_operation.h | 0
.../cpp/tests/client-test/compute_test.cpp | 2 +-
.../platforms/cpp/tests/odbc-test/CMakeLists.txt | 1 +
.../cpp/tests/odbc-test/api_robustness_test.cpp | 72 +-
.../cpp/tests/odbc-test/meta_queries_test.cpp | 863 +++++++++++++++++++++
.../cpp/tests/odbc-test/odbc_connection.h | 148 ++++
modules/platforms/cpp/tests/odbc-test/odbc_suite.h | 84 +-
.../cpp/tests/odbc-test/odbc_test_utils.h | 100 +++
28 files changed, 2067 insertions(+), 166 deletions(-)
diff --git a/modules/platforms/cpp/ignite/client/CMakeLists.txt b/modules/platforms/cpp/ignite/client/CMakeLists.txt
index 20c312e846..be5507c286 100644
--- a/modules/platforms/cpp/ignite/client/CMakeLists.txt
+++ b/modules/platforms/cpp/ignite/client/CMakeLists.txt
@@ -52,7 +52,11 @@ set(PUBLIC_HEADERS
compute/deployment_unit.h
detail/type_mapping_utils.h
network/cluster_node.h
+ sql/column_metadata.h
+ sql/column_origin.h
+ sql/result_set.h
sql/sql.h
+ sql/sql_statement.h
table/ignite_tuple.h
table/key_value_view.h
table/record_view.h
diff --git a/modules/platforms/cpp/ignite/client/detail/cluster_connection.h b/modules/platforms/cpp/ignite/client/detail/cluster_connection.h
index aceba4a122..c38399ef3e 100644
--- a/modules/platforms/cpp/ignite/client/detail/cluster_connection.h
+++ b/modules/platforms/cpp/ignite/client/detail/cluster_connection.h
@@ -17,7 +17,6 @@
#pragma once
-#include "ignite/client/detail/client_operation.h"
#include "ignite/client/detail/node_connection.h"
#include "ignite/client/detail/protocol_context.h"
#include "ignite/client/detail/response_handler.h"
@@ -26,6 +25,7 @@
#include "ignite/common/ignite_result.h"
#include "ignite/network/async_client_pool.h"
+#include "ignite/protocol/client_operation.h"
#include "ignite/protocol/reader.h"
#include "ignite/protocol/writer.h"
diff --git a/modules/platforms/cpp/ignite/client/detail/node_connection.cpp b/modules/platforms/cpp/ignite/client/detail/node_connection.cpp
index 1f232d8026..532ea3e124 100644
--- a/modules/platforms/cpp/ignite/client/detail/node_connection.cpp
+++ b/modules/platforms/cpp/ignite/client/detail/node_connection.cpp
@@ -80,19 +80,19 @@ bool node_connection::handshake() {
void node_connection::process_message(bytes_view msg) {
protocol::reader reader(msg);
- auto responseType = reader.read_int32();
- if (message_type(responseType) != message_type::RESPONSE) {
- m_logger->log_warning("Unsupported message type: " + std::to_string(responseType));
+ auto response_type = reader.read_int32();
+ if (message_type(response_type) != message_type::RESPONSE) {
+ m_logger->log_warning("Unsupported message type: " + std::to_string(response_type));
return;
}
- auto reqId = reader.read_int64();
+ auto req_id = reader.read_int64();
auto flags = reader.read_int32();
UNUSED_VALUE flags; // Flags are unused for now.
- auto handler = get_and_remove_handler(reqId);
+ auto handler = get_and_remove_handler(req_id);
if (!handler) {
- m_logger->log_error("Missing handler for request with id=" + std::to_string(reqId));
+ m_logger->log_error("Missing handler for request with id=" + std::to_string(req_id));
return;
}
@@ -108,9 +108,9 @@ void node_connection::process_message(bytes_view msg) {
auto pos = reader.position();
bytes_view data{msg.data() + pos, msg.size() - pos};
- auto handlingRes = handler->handle(shared_from_this(), data);
- if (handlingRes.has_error())
- m_logger->log_error("Uncaught user callback exception: " + handlingRes.error().what_str());
+ auto handling_res = handler->handle(shared_from_this(), data);
+ if (handling_res.has_error())
+ m_logger->log_error("Uncaught user callback exception: " + handling_res.error().what_str());
}
ignite_result<void> node_connection::process_handshake_rsp(bytes_view msg) {
diff --git a/modules/platforms/cpp/ignite/client/detail/node_connection.h b/modules/platforms/cpp/ignite/client/detail/node_connection.h
index 1428bc21b5..d4eb0ea418 100644
--- a/modules/platforms/cpp/ignite/client/detail/node_connection.h
+++ b/modules/platforms/cpp/ignite/client/detail/node_connection.h
@@ -17,7 +17,7 @@
#pragma once
-#include <ignite/client/detail/client_operation.h>
+#include "ignite/protocol/client_operation.h"
#include <ignite/client/detail/protocol_context.h>
#include <ignite/client/detail/response_handler.h>
#include <ignite/client/ignite_client_configuration.h>
diff --git a/modules/platforms/cpp/ignite/client/detail/sql/result_set_impl.h b/modules/platforms/cpp/ignite/client/detail/sql/result_set_impl.h
index 10973ca0da..e8c6dfd1cb 100644
--- a/modules/platforms/cpp/ignite/client/detail/sql/result_set_impl.h
+++ b/modules/platforms/cpp/ignite/client/detail/sql/result_set_impl.h
@@ -154,7 +154,7 @@ public:
*
* @return @c true if there are more pages with results and @c false otherwise.
*/
- [[nodiscard]] IGNITE_API bool has_more_pages() { return m_resource_id.has_value() && m_has_more_pages; }
+ [[nodiscard]] bool has_more_pages() { return m_resource_id.has_value() && m_has_more_pages; }
/**
* Fetch the next page of results asynchronously.
@@ -162,7 +162,7 @@ public:
*
* @param callback Callback to call on completion.
*/
- IGNITE_API void fetch_next_page_async(std::function<void(ignite_result<void>)> callback) {
+ void fetch_next_page_async(std::function<void(ignite_result<void>)> callback) {
require_result_set();
if (!m_resource_id)
@@ -213,8 +213,8 @@ private:
const msgpack_object_array &arr = obj.via.array;
- constexpr std::uint32_t minCount = 6;
- assert(arr.size >= minCount);
+ constexpr std::uint32_t min_count = 6;
+ assert(arr.size >= min_count);
auto name = protocol::unpack_object<std::string>(arr.ptr[0]);
auto nullable = protocol::unpack_object<bool>(arr.ptr[1]);
@@ -229,7 +229,7 @@ private:
return;
}
- assert(arr.size >= minCount + 3);
+ assert(arr.size >= min_count + 3);
auto origin_name =
arr.ptr[6].type == MSGPACK_OBJECT_NIL ? name : protocol::unpack_object<std::string>(arr.ptr[6]);
diff --git a/modules/platforms/cpp/ignite/network/data_buffer.h b/modules/platforms/cpp/ignite/network/data_buffer.h
index f6e3a162a7..0f32125074 100644
--- a/modules/platforms/cpp/ignite/network/data_buffer.h
+++ b/modules/platforms/cpp/ignite/network/data_buffer.h
@@ -118,6 +118,9 @@ private:
*/
class data_buffer_owning {
public:
+ // Default.
+ data_buffer_owning() = default;
+
/**
* Constructor.
*
diff --git a/modules/platforms/cpp/ignite/odbc/CMakeLists.txt b/modules/platforms/cpp/ignite/odbc/CMakeLists.txt
index 775d2a0268..e6a8eebb1f 100644
--- a/modules/platforms/cpp/ignite/odbc/CMakeLists.txt
+++ b/modules/platforms/cpp/ignite/odbc/CMakeLists.txt
@@ -34,6 +34,7 @@ set(SOURCES
diagnostic/diagnostic_record_storage.cpp
meta/column_meta.cpp
meta/table_meta.cpp
+ query/data_query.cpp
odbc.cpp
entry_points.cpp
ssl_mode.cpp
diff --git a/modules/platforms/cpp/ignite/odbc/config/configuration.cpp b/modules/platforms/cpp/ignite/odbc/config/configuration.cpp
index 98afe1eb17..1507f70acb 100644
--- a/modules/platforms/cpp/ignite/odbc/config/configuration.cpp
+++ b/modules/platforms/cpp/ignite/odbc/config/configuration.cpp
@@ -36,11 +36,21 @@ static inline const std::string port{"port"};
/** Key for address attribute. */
static inline const std::string address{"address"};
+/** Key for address attribute. */
+static inline const std::string schema{"schema"};
+
} // namespace key;
namespace ignite {
+void try_get_string_param(value_with_default<std::string> &dst, const config_map &config_params, const std::string &key) {
+ auto it = config_params.find(key);
+ if (it != config_params.end()) {
+ dst = {it->second, true};
+ }
+}
+
void configuration::from_config_map(const config_map &config_params) {
*this = configuration();
@@ -73,6 +83,8 @@ void configuration::from_config_map(const config_map &config_params) {
m_end_points = {{{host, port}}, true};
}
+
+ try_get_string_param(m_schema, config_params, key::schema);
}
} // namespace ignite
diff --git a/modules/platforms/cpp/ignite/odbc/config/configuration.h b/modules/platforms/cpp/ignite/odbc/config/configuration.h
index c8dcf3c182..f48927a173 100644
--- a/modules/platforms/cpp/ignite/odbc/config/configuration.h
+++ b/modules/platforms/cpp/ignite/odbc/config/configuration.h
@@ -48,6 +48,9 @@ public:
/** Default value for address attribute. */
static inline const std::vector<end_point> address{{host, port}};
+
+ /** Default value for Driver attribute. */
+ static inline const std::string schema{"PUBLIC"};
};
// Default.
@@ -67,10 +70,19 @@ public:
*
* @return Fetch results page size.
*/
- [[nodiscard]] value_with_default<std::int32_t> get_page_size() const {
+ [[nodiscard]] const value_with_default<std::int32_t> &get_page_size() const {
return m_page_size;
}
+ /**
+ * Get schema.
+ *
+ * @return Schema.
+ */
+ [[nodiscard]] const value_with_default<std::string> &get_schema() const {
+ return m_schema;
+ }
+
/**
* Fill from configuration params.
*
@@ -81,10 +93,13 @@ public:
private:
/** Request and response page size. */
- value_with_default<int32_t> m_page_size{default_value::page_size, false};
+ value_with_default<std::int32_t> m_page_size{default_value::page_size, false};
/** Connection end-points. */
value_with_default<std::vector<end_point>> m_end_points{default_value::address, false};
+
+ /** Schema. */
+ value_with_default<std::string> m_schema{default_value::schema, false};
};
} // namespace ignite
diff --git a/modules/platforms/cpp/ignite/odbc/diagnostic/diagnosable.h b/modules/platforms/cpp/ignite/odbc/diagnostic/diagnosable.h
index 2aed02c0e6..4b5c63bebd 100644
--- a/modules/platforms/cpp/ignite/odbc/diagnostic/diagnosable.h
+++ b/modules/platforms/cpp/ignite/odbc/diagnostic/diagnosable.h
@@ -17,7 +17,11 @@
#pragma once
+#include "ignite/common/ignite_error.h"
#include "ignite/odbc/diagnostic/diagnostic_record_storage.h"
+#include "ignite/odbc/odbc_error.h"
+
+#include <functional>
namespace ignite
{
@@ -80,6 +84,30 @@ public:
*/
virtual void add_status_record(const diagnostic_record& rec) = 0;
+ /**
+ * Catch and handle any known errors that can happen in function.
+ *
+ * @param func Code to handle.
+ * @return @c true if no error happened and false otherwise.
+ */
+ bool catch_errors(const std::function<void()>& func) {
+ try {
+ func();
+ }
+ catch (const odbc_error& err)
+ {
+ add_status_record(err);
+ return false;
+ }
+ catch (const ignite_error& err)
+ {
+ add_status_record(sql_state::SHY000_GENERAL_ERROR, err.what_str());
+ return false;
+ }
+
+ return true;
+ }
+
protected:
// Default
diagnosable() = default;
diff --git a/modules/platforms/cpp/ignite/odbc/diagnostic/diagnosable_adapter.cpp b/modules/platforms/cpp/ignite/odbc/diagnostic/diagnosable_adapter.cpp
index 708dae3dbc..c56bd0da9b 100644
--- a/modules/platforms/cpp/ignite/odbc/diagnostic/diagnosable_adapter.cpp
+++ b/modules/platforms/cpp/ignite/odbc/diagnostic/diagnosable_adapter.cpp
@@ -46,6 +46,7 @@ void diagnosable_adapter::add_status_record(const odbc_error& err)
void diagnosable_adapter::add_status_record(const diagnostic_record& rec)
{
+ LOG_MSG("Adding new record: " << rec.get_sql_state() << " " << rec.get_message_text());
m_diagnostic_records.add_status_record(rec);
}
diff --git a/modules/platforms/cpp/ignite/odbc/meta/column_meta.cpp b/modules/platforms/cpp/ignite/odbc/meta/column_meta.cpp
index fe4568a8bc..5f2f9b805e 100644
--- a/modules/platforms/cpp/ignite/odbc/meta/column_meta.cpp
+++ b/modules/platforms/cpp/ignite/odbc/meta/column_meta.cpp
@@ -269,10 +269,7 @@ bool column_meta::get_attribute(uint16_t field_id, SQLLEN& value) const
case SQL_DESC_PRECISION:
case SQL_COLUMN_PRECISION:
{
- if (m_precision == -1)
- value = ignite_type_column_size(m_data_type);
- else
- value = m_precision;
+ value = m_precision < 0 ? 0 : m_precision;
break;
}
@@ -280,15 +277,7 @@ bool column_meta::get_attribute(uint16_t field_id, SQLLEN& value) const
case SQL_DESC_SCALE:
case SQL_COLUMN_SCALE:
{
- if (m_scale == -1)
- {
- value = ignite_type_decimal_digits(m_data_type);
-
- if (value < 0)
- value = 0;
- }
- else
- value = m_scale;
+ value = m_scale < 0 ? 0 : m_scale;
break;
}
diff --git a/modules/platforms/cpp/ignite/odbc/meta/column_meta.h b/modules/platforms/cpp/ignite/odbc/meta/column_meta.h
index 8914baaab8..54206c5c53 100644
--- a/modules/platforms/cpp/ignite/odbc/meta/column_meta.h
+++ b/modules/platforms/cpp/ignite/odbc/meta/column_meta.h
@@ -82,12 +82,19 @@ public:
* @param table_name Table name.
* @param column_name Column name.
* @param data_type Data type.
+ * @param precision Precision.
+ * @param scale Scale.
+ * @param nullable Nullable.
*/
- column_meta(std::string schema_name, std::string table_name, std::string column_name, ignite_type data_type)
+ column_meta(std::string schema_name, std::string table_name, std::string column_name, ignite_type data_type,
+ std::int32_t precision, std::int32_t scale, bool nullable)
: m_schema_name(std::move(schema_name))
, m_table_name(std::move(table_name))
, m_column_name(std::move(column_name))
- , m_data_type(data_type) { }
+ , m_data_type(data_type)
+ , m_precision(precision)
+ , m_scale(scale)
+ , m_nullability(nullable ? nullability::NULLABLE : nullability::NO_NULL) { }
/**
* Read using reader.
diff --git a/modules/platforms/cpp/ignite/odbc/query/cursor.h b/modules/platforms/cpp/ignite/odbc/query/cursor.h
new file mode 100644
index 0000000000..a58615aeaf
--- /dev/null
+++ b/modules/platforms/cpp/ignite/odbc/query/cursor.h
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <memory>
+
+#include <cstdint>
+
+namespace ignite
+{
+
+class cursor
+{
+public:
+ // Delete
+ cursor(cursor&&) = delete;
+ cursor(const cursor&) = delete;
+ cursor& operator=(cursor&&) = delete;
+ cursor& operator=(const cursor&) = delete;
+
+ /**
+ * Constructor.
+ * @param query_id ID of the executed query.
+ */
+ explicit cursor(std::int64_t query_id)
+ : m_query_id(query_id) {}
+
+ /**
+ * Check if the cursor has data.
+ *
+ * @return True if the cursor has data.
+ */
+ [[nodiscard]] bool has_data() const {
+ return false;
+ }
+
+ /**
+ * Check whether cursor closed remotely.
+ *
+ * @return true, if the cursor closed remotely.
+ */
+ [[nodiscard]] bool is_closed_remotely() const {
+ return false;
+ }
+
+ /**
+ * Get query ID.
+ *
+ * @return Query ID.
+ */
+ [[nodiscard]] std::int64_t get_query_id() const {
+ return m_query_id;
+ }
+
+private:
+ /** Cursor id. */
+ std::int64_t m_query_id;
+};
+
+}
diff --git a/modules/platforms/cpp/ignite/odbc/query/data_query.cpp b/modules/platforms/cpp/ignite/odbc/query/data_query.cpp
new file mode 100644
index 0000000000..b071b34a28
--- /dev/null
+++ b/modules/platforms/cpp/ignite/odbc/query/data_query.cpp
@@ -0,0 +1,367 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ignite/odbc/log.h"
+#include "ignite/odbc/odbc_error.h"
+#include "ignite/odbc/query/data_query.h"
+#include "ignite/odbc/query/cursor.h"
+#include "ignite/tuple/binary_tuple_builder.h"
+
+#include <memory>
+#include <utility>
+
+namespace
+{
+using namespace ignite;
+
+/**
+ * Reads result set metadata.
+ *
+ * @param reader Reader.
+ * @return Result set meta columns.
+ */
+column_meta_vector read_meta(protocol::reader &reader) {
+ auto size = reader.read_array_size();
+
+ column_meta_vector columns;
+ columns.reserve(size);
+
+ reader.read_array_raw([&columns](std::uint32_t idx, const msgpack_object &obj) {
+ if (obj.type != MSGPACK_OBJECT_ARRAY)
+ throw ignite_error("Meta column expected to be serialized as array");
+
+ const msgpack_object_array &arr = obj.via.array;
+
+ constexpr std::uint32_t min_count = 6;
+ assert(arr.size >= min_count);
+
+ auto name = protocol::unpack_object<std::string>(arr.ptr[0]);
+ auto nullable = protocol::unpack_object<bool>(arr.ptr[1]);
+ auto typ = ignite_type(protocol::unpack_object<std::int32_t>(arr.ptr[2]));
+ auto scale = protocol::unpack_object<std::int32_t>(arr.ptr[3]);
+ auto precision = protocol::unpack_object<std::int32_t>(arr.ptr[4]);
+
+ bool origin_present = protocol::unpack_object<bool>(arr.ptr[5]);
+
+ if (!origin_present) {
+ columns.emplace_back("", "", std::move(name), typ, precision, scale, nullable);
+ return;
+ }
+
+ assert(arr.size >= min_count + 3);
+ auto origin_name =
+ arr.ptr[6].type == MSGPACK_OBJECT_NIL ? name : protocol::unpack_object<std::string>(arr.ptr[6]);
+
+ auto origin_schema_id = protocol::try_unpack_object<std::int32_t>(arr.ptr[7]);
+ std::string origin_schema;
+ if (origin_schema_id) {
+ if (*origin_schema_id >= std::int32_t(columns.size())) {
+ throw ignite_error("Origin schema ID is too large: " + std::to_string(*origin_schema_id)
+ + ", id=" + std::to_string(idx));
+ }
+ origin_schema = columns[*origin_schema_id].get_schema_name();
+ } else {
+ origin_schema = protocol::unpack_object<std::string>(arr.ptr[7]);
+ }
+
+ auto origin_table_id = protocol::try_unpack_object<std::int32_t>(arr.ptr[8]);
+ std::string origin_table;
+ if (origin_table_id) {
+ if (*origin_table_id >= std::int32_t(columns.size())) {
+ throw ignite_error("Origin table ID is too large: " + std::to_string(*origin_table_id)
+ + ", id=" + std::to_string(idx));
+ }
+ origin_table = columns[*origin_table_id].get_table_name();
+ } else {
+ origin_table = protocol::unpack_object<std::string>(arr.ptr[8]);
+ }
+
+ columns.emplace_back(
+ std::move(origin_schema), std::move(origin_table), std::move(name), typ, precision, scale, nullable);
+ });
+
+ return columns;
+}
+
+}
+
+namespace ignite
+{
+
+data_query::data_query(diagnosable_adapter &m_diag, sql_connection &m_connection, std::string sql,
+ const parameter_set ¶ms, std::int32_t &timeout)
+ : query(m_diag, query_type::DATA)
+ , m_connection(m_connection)
+ , m_query(std::move(sql))
+ , m_params(params)
+ , m_timeout(timeout) { }
+
+data_query::~data_query()
+{
+ internal_close();
+}
+
+sql_result data_query::execute()
+{
+ internal_close();
+
+ return make_request_execute();
+}
+
+const column_meta_vector *data_query::get_meta()
+{
+ if (!m_result_meta_available) {
+ make_request_resultset_meta();
+ if (!m_result_meta_available)
+ return nullptr;
+ }
+
+ return &m_result_meta;
+}
+
+sql_result data_query::fetch_next_row(column_binding_map &column_bindings)
+{
+ UNUSED_VALUE column_bindings;
+ // TODO: IGNITE-19213 Implement data fetching
+ m_diag.add_status_record(sql_state::SHYC00_OPTIONAL_FEATURE_NOT_IMPLEMENTED, "Data fetching is not implemented");
+ return sql_result::AI_ERROR;
+}
+
+sql_result data_query::get_column(std::uint16_t column_idx, application_data_buffer &buffer)
+{
+ UNUSED_VALUE column_idx;
+ UNUSED_VALUE buffer;
+ // TODO: IGNITE-19213 Implement data fetching
+ m_diag.add_status_record(sql_state::SHYC00_OPTIONAL_FEATURE_NOT_IMPLEMENTED, "Data fetching is not implemented");
+ return sql_result::AI_ERROR;
+}
+
+sql_result data_query::close()
+{
+ return internal_close();
+}
+
+sql_result data_query::internal_close()
+{
+ if (!m_cursor)
+ return sql_result::AI_SUCCESS;
+
+ sql_result result = sql_result::AI_SUCCESS;
+
+ if (!is_closed_remotely())
+ result = make_request_close();
+
+ if (result == sql_result::AI_SUCCESS)
+ {
+ m_cursor.reset();
+ m_rows_affected = -1;
+ }
+
+ return result;
+}
+
+bool data_query::is_data_available() const
+{
+ return m_cursor.get() && m_cursor->has_data();
+}
+
+std::int64_t data_query::affected_rows() const
+{
+ return m_rows_affected;
+}
+
+sql_result data_query::next_result_set()
+{
+ // TODO: IGNITE-19855 Multiple queries execution is not supported.
+ internal_close();
+ return sql_result::AI_NO_DATA;
+}
+
+bool data_query::is_closed_remotely() const
+{
+ return m_rows_affected < 0 || m_cursor->is_closed_remotely();
+}
+
+sql_result data_query::make_request_execute()
+{
+ auto &schema = m_connection.get_schema();
+
+ network::data_buffer_owning response;
+ auto success = m_diag.catch_errors([&]{
+ response = m_connection.sync_request(detail::client_operation::SQL_EXEC, [&](protocol::writer &writer) {
+ // TODO: IGNITE-19399 Implement transactions support.
+ writer.write_nil();
+
+ writer.write(schema);
+ writer.write(m_connection.get_configuration().get_page_size().get_value());
+ writer.write(std::int64_t(m_connection.get_timeout()) * 1000);
+ writer.write_nil(); // Session timeout (unused, session is closed by the server immediately).
+
+ // Properties are not used for now.
+ writer.write(0);
+ binary_tuple_builder prop_builder{0};
+ prop_builder.start();
+ prop_builder.layout();
+ auto prop_data = prop_builder.build();
+ writer.write_binary(prop_data);
+
+ writer.write(m_query);
+
+ auto args_num = std::int32_t(m_params.get_parameters_number());
+ if (args_num == 0) {
+ writer.write_nil();
+ return;
+ }
+
+ // TODO: IGNITE-19205 Implement column bindings reading.
+ throw odbc_error(sql_state::SHYC00_OPTIONAL_FEATURE_NOT_IMPLEMENTED, "Parameters are not yet supported");
+ });
+ });
+
+ if (!success)
+ return sql_result::AI_ERROR;
+
+ protocol::reader reader(response.get_bytes_view());
+
+ auto resource_id = reader.read_object_nullable<std::int64_t>();
+ if (resource_id) {
+ m_cursor = std::make_unique<cursor>(*resource_id);
+ }
+
+ bool has_rowset = reader.read_bool();
+ UNUSED_VALUE reader.read_bool(); // has_more_pages
+ UNUSED_VALUE reader.read_bool(); // was_applied
+ m_rows_affected = reader.read_int64();
+
+ if (has_rowset) {
+ auto columns = read_meta(reader);
+ set_resultset_meta(columns);
+ // TODO: IGNITE-19213 Implement data fetching
+ }
+
+ return sql_result::AI_SUCCESS;
+}
+
+sql_result data_query::make_request_close()
+{
+ if (!m_cursor)
+ return sql_result::AI_SUCCESS;
+
+ LOG_MSG("Closing cursor: " << m_cursor->get_query_id());
+
+ auto success = m_diag.catch_errors([&]{
+ UNUSED_VALUE m_connection.sync_request(detail::client_operation::SQL_CURSOR_CLOSE,
+ [&](protocol::writer &writer) {
+ writer.write(m_cursor->get_query_id());
+ });
+ });
+
+ return success ? sql_result::AI_SUCCESS : sql_result::AI_ERROR;
+}
+
+sql_result data_query::make_request_fetch()
+{
+ // TODO: IGNITE-19213 Implement data fetching
+ m_diag.add_status_record(sql_state::SHYC00_OPTIONAL_FEATURE_NOT_IMPLEMENTED, "Data fetching is not implemented");
+ return sql_result::AI_ERROR;
+}
+
+sql_result data_query::make_request_resultset_meta()
+{
+ // TODO: IGNITE-19854 Implement metadata fetching for the non-executed query.
+ m_diag.add_status_record(sql_state::SHYC00_OPTIONAL_FEATURE_NOT_IMPLEMENTED,
+ "Metadata for non-executed queries is not supported");
+
+ return sql_result::AI_ERROR;
+}
+
+sql_result data_query::process_conversion_result(conversion_result conv_res, std::int32_t row_idx,
+ std::int32_t column_idx)
+{
+ switch (conv_res)
+ {
+ case conversion_result::AI_SUCCESS:
+ {
+ return sql_result::AI_SUCCESS;
+ }
+
+ case conversion_result::AI_NO_DATA:
+ {
+ return sql_result::AI_NO_DATA;
+ }
+
+ case conversion_result::AI_VARLEN_DATA_TRUNCATED:
+ {
+ m_diag.add_status_record(sql_state::S01004_DATA_TRUNCATED,
+ "Buffer is too small for the column data. Truncated from the right.", row_idx, column_idx);
+
+ return sql_result::AI_SUCCESS_WITH_INFO;
+ }
+
+ case conversion_result::AI_FRACTIONAL_TRUNCATED:
+ {
+ m_diag.add_status_record(sql_state::S01S07_FRACTIONAL_TRUNCATION,
+ "Buffer is too small for the column data. Fraction truncated.", row_idx, column_idx);
+
+ return sql_result::AI_SUCCESS_WITH_INFO;
+ }
+
+ case conversion_result::AI_INDICATOR_NEEDED:
+ {
+ m_diag.add_status_record(sql_state::S22002_INDICATOR_NEEDED,
+ "Indicator is needed but not supplied for the column buffer.", row_idx, column_idx);
+
+ return sql_result::AI_SUCCESS_WITH_INFO;
+ }
+
+ case conversion_result::AI_UNSUPPORTED_CONVERSION:
+ {
+ m_diag.add_status_record(sql_state::SHYC00_OPTIONAL_FEATURE_NOT_IMPLEMENTED,
+ "Data conversion is not supported.", row_idx, column_idx);
+
+ return sql_result::AI_SUCCESS_WITH_INFO;
+ }
+
+ case conversion_result::AI_FAILURE:
+ default:
+ {
+ m_diag.add_status_record(sql_state::S01S01_ERROR_IN_ROW,
+ "Can not retrieve row column.", row_idx, column_idx);
+
+ break;
+ }
+ }
+
+ return sql_result::AI_ERROR;
+}
+
+void data_query::set_resultset_meta(const column_meta_vector& value)
+{
+ m_result_meta.assign(value.begin(), value.end());
+ m_result_meta_available = true;
+
+ for (size_t i = 0; i < m_result_meta.size(); ++i)
+ {
+ column_meta& meta = m_result_meta.at(i);
+ LOG_MSG("\n[" << i << "] SchemaName: " << meta.get_schema_name()
+ << "\n[" << i << "] TypeName: " << meta.get_table_name()
+ << "\n[" << i << "] ColumnName: " << meta.get_column_name()
+ << "\n[" << i << "] ColumnType: " << static_cast<int32_t>(meta.get_data_type()));
+ }
+}
+
+} // namespace ignite
diff --git a/modules/platforms/cpp/ignite/odbc/query/data_query.h b/modules/platforms/cpp/ignite/odbc/query/data_query.h
new file mode 100644
index 0000000000..f4459648cd
--- /dev/null
+++ b/modules/platforms/cpp/ignite/odbc/query/data_query.h
@@ -0,0 +1,212 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "ignite/odbc/app/parameter_set.h"
+#include "ignite/odbc/query/query.h"
+#include "ignite/odbc/sql_connection.h"
+
+namespace ignite
+{
+
+class cursor;
+
+/**
+ * Data query.
+ */
+class data_query : public query
+{
+public:
+ // Delete
+ data_query(data_query&&) = delete;
+ data_query(const data_query&) = delete;
+ data_query& operator=(data_query&&) = delete;
+ data_query& operator=(const data_query&) = delete;
+
+ /**
+ * Constructor.
+ *
+ * @param diag Diagnostics collector.
+ * @param connection Associated connection.
+ * @param sql SQL query string.
+ * @param params SQL params.
+ * @param timeout Timeout.
+ */
+ data_query(diagnosable_adapter &diag, sql_connection &connection, std::string sql, const parameter_set ¶ms,
+ std::int32_t &timeout);
+
+ /**
+ * Destructor.
+ */
+ ~data_query() override;
+
+ /**
+ * Execute query.
+ *
+ * @return True on success.
+ */
+ sql_result execute() override;
+
+ /**
+ * Get column metadata.
+ *
+ * @return Column metadata.
+ */
+ const column_meta_vector *get_meta() override;
+
+ /**
+ * Fetch next result row to application buffers.
+ *
+ * @param column_bindings Application buffers to put data to.
+ * @return Operation result.
+ */
+ sql_result fetch_next_row(column_binding_map &column_bindings) override;
+
+ /**
+ * Get data of the specified column in the result set.
+ *
+ * @param column_idx Column index.
+ * @param buffer Buffer to put column data to.
+ * @return Operation result.
+ */
+ sql_result get_column(std::uint16_t column_idx, application_data_buffer &buffer) override;
+
+ /**
+ * Close query.
+ *
+ * @return Result.
+ */
+ sql_result close() override;
+
+ /**
+ * Check if data is available.
+ *
+ * @return True if data is available.
+ */
+ [[nodiscard]] bool is_data_available() const override;
+
+ /**
+ * Get number of rows affected by the statement.
+ *
+ * @return Number of rows affected by the statement.
+ */
+ [[nodiscard]] std::int64_t affected_rows() const override;
+
+ /**
+ * Move to the next result set.
+ *
+ * @return Operation result.
+ */
+ sql_result next_result_set() override;
+
+ /**
+ * Get SQL query string.
+ *
+ * @return SQL query string.
+ */
+ [[nodiscard]] const std::string& get_query() const {
+ return m_query;
+ }
+
+private:
+ /**
+ * Check whether all cursors are closed remotely.
+ *
+ * @return true, if all cursors closed remotely.
+ */
+ [[nodiscard]] bool is_closed_remotely() const;
+
+ /**
+ * Make query execute request and use response to set internal
+ * state.
+ *
+ * @return Result.
+ */
+ sql_result make_request_execute();
+
+ /**
+ * Make query close request.
+ *
+ * @return Result.
+ */
+ sql_result make_request_close();
+
+ /**
+ * Make data fetch request and use response to set internal state.
+ *
+ * @return Result.
+ */
+ sql_result make_request_fetch();
+
+ /**
+ * Make result set metadata request.
+ *
+ * @return Result.
+ */
+ sql_result make_request_resultset_meta();
+
+ /**
+ * Process column conversion operation result.
+ *
+ * @param conv_res Conversion result.
+ * @param row_idx Row index.
+ * @param column_idx Column index.
+ * @return General SQL result.
+ */
+ sql_result process_conversion_result(conversion_result conv_res, std::int32_t row_idx, std::int32_t column_idx);
+
+ /**
+ * Set metadata for the result set.
+ *
+ * @param value Metadata.
+ */
+ void set_resultset_meta(const column_meta_vector &value);
+
+ /**
+ * Close query.
+ *
+ * @return Result.
+ */
+ sql_result internal_close();
+
+ /** Connection associated with the statement. */
+ sql_connection &m_connection;
+
+ /** SQL query. */
+ std::string m_query;
+
+ /** Parameter bindings. */
+ const parameter_set &m_params;
+
+ /** Result set metadata is available */
+ bool m_result_meta_available{false};
+
+ /** Result set metadata. */
+ column_meta_vector m_result_meta;
+
+ /** Number of rows affected. */
+ std::int64_t m_rows_affected{-1};
+
+ /** Cursor. */
+ std::unique_ptr<cursor> m_cursor;
+
+ /** Timeout. */
+ std::int32_t &m_timeout;
+};
+
+} // namespace ignite
diff --git a/modules/platforms/cpp/ignite/odbc/sql_connection.cpp b/modules/platforms/cpp/ignite/odbc/sql_connection.cpp
index bc78a11bf5..108ad05ceb 100644
--- a/modules/platforms/cpp/ignite/odbc/sql_connection.cpp
+++ b/modules/platforms/cpp/ignite/odbc/sql_connection.cpp
@@ -26,6 +26,7 @@
#include <ignite/common/bytes.h>
#include <ignite/network/network.h>
+#include <ignite/protocol/client_operation.h>
#include <algorithm>
#include <cstring>
@@ -225,20 +226,14 @@ bool sql_connection::send(const std::byte* data, std::size_t len, std::int32_t t
if (!m_socket)
throw odbc_error(sql_state::S08003_NOT_CONNECTED, "Connection is not established");
- auto new_len = len + PROTOCOL_HEADER_SIZE;
- std::vector<std::byte> msg(new_len);
-
- bytes::store<endian::BIG, std::int32_t>(msg.data(), std::int32_t(len));
- memcpy(msg.data() + PROTOCOL_HEADER_SIZE, data, len);
-
- operation_result res = send_all(msg.data(), msg.size(), timeout);
+ operation_result res = send_all(data, len, timeout);
if (res == operation_result::TIMEOUT)
return false;
if (res == operation_result::FAIL)
throw odbc_error(sql_state::S08S01_LINK_FAILURE, "Can not send message due to connection failure");
- TRACE_MSG("message sent: (" << msg.size() << " bytes)" << hex_dump(msg.data(), msg.size()));
+ TRACE_MSG("message sent: (" << len << " bytes)" << hex_dump(data, len));
return true;
}
@@ -328,6 +323,48 @@ sql_connection::operation_result sql_connection::receive_all(void* dst, std::siz
return operation_result::SUCCESS;
}
+void sql_connection::send_message(bytes_view req, std::int32_t timeout) {
+ ensure_connected();
+
+ bool success = send(req.data(), req.size(), timeout);
+ if (!success)
+ throw odbc_error(sql_state::SHYT00_TIMEOUT_EXPIRED, "Could not send a request due to timeout");
+}
+
+network::data_buffer_owning sql_connection::receive_message(std::int64_t id, std::int32_t timeout) {
+ ensure_connected();
+ std::vector<std::byte> res;
+
+ while (true) {
+ bool success = receive(res, timeout);
+ if (!success)
+ throw odbc_error(sql_state::SHYT00_TIMEOUT_EXPIRED, "Could not receive a response within timeout");
+
+ protocol::reader reader(res);
+ auto response_type = reader.read_int32();
+ if (detail::message_type(response_type) != detail::message_type::RESPONSE) {
+ LOG_MSG("Unsupported message type: " + std::to_string(response_type));
+ continue;
+ }
+
+ auto req_id = reader.read_int64();
+ if (req_id != id) {
+ throw odbc_error(sql_state::S08S01_LINK_FAILURE,
+ "Response with unknown ID is received: " + std::to_string(req_id));
+ }
+
+ auto flags = reader.read_int32();
+ UNUSED_VALUE flags; // Flags are unused for now.
+
+ auto err = protocol::read_error(reader);
+ if (err) {
+ throw odbc_error(sql_state::SHY000_GENERAL_ERROR, err.value().what_str());
+ }
+
+ return network::data_buffer_owning{std::move(res), reader.position()};
+ }
+}
+
const configuration&sql_connection::get_configuration() const
{
return m_config;
@@ -504,6 +541,22 @@ sql_result sql_connection::internal_set_attribute(int attr, void* value, SQLINTE
return sql_result::AI_SUCCESS;
}
+std::vector<std::byte> sql_connection::make_request(std::int64_t id, detail::client_operation op,
+ const std::function<void(protocol::writer &)> &func) {
+ std::vector<std::byte> req;
+ protocol::buffer_adapter buffer(req);
+ buffer.reserve_length_header();
+
+ protocol::writer writer(buffer);
+ writer.write(std::int32_t(op));
+ writer.write(id);
+ func(writer);
+
+ buffer.write_length_header();
+
+ return req;
+}
+
sql_result sql_connection::make_request_handshake()
{
static constexpr int8_t ODBC_CLIENT = 3;
@@ -607,7 +660,7 @@ sql_result sql_connection::make_request_handshake()
void sql_connection::ensure_connected()
{
- if (!m_socket)
+ if (m_socket)
return;
bool success = try_restore_connection();
diff --git a/modules/platforms/cpp/ignite/odbc/sql_connection.h b/modules/platforms/cpp/ignite/odbc/sql_connection.h
index b9e6159230..bb53c51fac 100644
--- a/modules/platforms/cpp/ignite/odbc/sql_connection.h
+++ b/modules/platforms/cpp/ignite/odbc/sql_connection.h
@@ -23,11 +23,14 @@
#include "ignite/odbc/protocol_version.h"
#include "ignite/odbc/odbc_error.h"
+#include "ignite/network/data_buffer.h"
#include "ignite/network/socket_client.h"
#include "ignite/network/tcp_range.h"
#include "ignite/protocol/buffer_adapter.h"
#include "ignite/protocol/writer.h"
+#include "ignite/protocol/client_operation.h"
+#include <atomic>
#include <cstdint>
#include <vector>
@@ -153,6 +156,45 @@ public:
*/
bool receive(std::vector<std::byte>& msg, std::int32_t timeout);
+ /**
+ * Synchronously send request message.
+ * Uses provided timeout.
+ *
+ * @param req Request message.
+ * @param timeout Timeout. 0 means disabled.
+ * @throw OdbcError on error.
+ */
+ void send_message(bytes_view req, std::int32_t timeout);
+
+ /**
+ * Synchronously send request message.
+ *
+ * @param req Request message.
+ * @throw OdbcError on error.
+ */
+ void send_message(bytes_view req) {
+ send_message(req, m_timeout);
+ }
+
+ /**
+ * Receive message.
+ *
+ * @param id Expected message ID.
+ * @param timeout Timeout.
+ * @return Message.
+ */
+ network::data_buffer_owning receive_message(std::int64_t id, std::int32_t timeout);
+
+ /**
+ * Receive message.
+ *
+ * @param id Expected message ID.
+ * @return Message.
+ */
+ network::data_buffer_owning receive_message(std::int64_t id) {
+ return receive_message(id, m_timeout);
+ }
+
/**
* Get configuration.
*
@@ -196,7 +238,59 @@ public:
*/
void set_attribute(int attr, void* value, SQLINTEGER value_len);
+ /**
+ * Make new request.
+ *
+ * @param id Request ID.
+ * @param op Operation.
+ * @param func Function.
+ */
+ [[nodiscard]] static std::vector<std::byte> make_request(std::int64_t id, detail::client_operation op,
+ const std::function<void(protocol::writer &)> &func);
+
+ /**
+ * Get connection schema.
+ *
+ * @return Schema.
+ */
+ const std::string& get_schema() const {
+ return m_config.get_schema().get_value();
+ }
+
+ /**
+ * Get timeout.
+ *
+ * @return Timeout.
+ */
+ std::int32_t get_timeout() const {
+ return m_timeout;
+ }
+
+ /**
+ * Make a synchronous request and get a response.
+ *
+ * @param op Operation.
+ * @param wr Payload writing function.
+ * @return Response.
+ */
+ network::data_buffer_owning sync_request(detail::client_operation op,
+ const std::function<void(protocol::writer &)> &wr) {
+ auto req_id = generate_next_req_id();
+ auto request = make_request(req_id, op, wr);
+
+ send_message(request);
+ return receive_message(req_id);
+ }
+
private:
+ /**
+ * Generate and get next request ID.
+ *
+ * @return Request ID.
+ */
+ std::int64_t generate_next_req_id() {
+ return m_req_id_gen.fetch_add(1);
+ }
/**
* Init connection socket, using configuration.
@@ -386,6 +480,9 @@ private:
/** Protocol version. */
protocol_version m_protocol_version;
+
+ /** Request ID generator. */
+ std::atomic_int64_t m_req_id_gen{0};
};
} // namespace ignite
\ No newline at end of file
diff --git a/modules/platforms/cpp/ignite/odbc/sql_statement.cpp b/modules/platforms/cpp/ignite/odbc/sql_statement.cpp
index 804cf493c8..1ee5eda4eb 100644
--- a/modules/platforms/cpp/ignite/odbc/sql_statement.cpp
+++ b/modules/platforms/cpp/ignite/odbc/sql_statement.cpp
@@ -23,6 +23,7 @@
#include "ignite/odbc/sql_connection.h"
#include "ignite/odbc/sql_statement.h"
#include "ignite/odbc/utility.h"
+#include "ignite/odbc/query/data_query.h"
namespace ignite {
@@ -578,8 +579,7 @@ sql_result sql_statement::internal_prepare_sql_query(const std::string& query)
if (m_current_query)
m_current_query->close();
- // TODO: IGNITE-19212 implement basic query execution
- m_current_query.reset();
+ m_current_query = std::make_unique<data_query>(*this, m_connection, query, m_parameters, m_timeout);
return sql_result::AI_SUCCESS;
}
@@ -951,18 +951,18 @@ sql_result sql_statement::internal_get_column_attribute(uint16_t column_idx, uin
return sql_result::AI_ERROR;
}
- const column_meta& columnMeta = meta->at(column_idx - 1);
+ const column_meta& column_meta = meta->at(column_idx - 1);
bool found = false;
if (numeric_buf)
- found = columnMeta.get_attribute(attr_id, *numeric_buf);
+ found = column_meta.get_attribute(attr_id, *numeric_buf);
if (!found)
{
std::string out;
- found = columnMeta.get_attribute(attr_id, out);
+ found = column_meta.get_attribute(attr_id, out);
size_t outSize = out.size();
@@ -1175,7 +1175,7 @@ sql_result sql_statement::update_params_meta()
assert(qry0);
assert(qry0->get_type() == query_type::DATA);
- // TODO: IGNITE-19212: Implement params metadata fetching
+ // TODO: IGNITE-19854: Implement params metadata fetching
add_status_record(sql_state::SHYC00_OPTIONAL_FEATURE_NOT_IMPLEMENTED, "Parameters metadata is not supported");
return sql_result::AI_ERROR;
diff --git a/modules/platforms/cpp/ignite/odbc/utility.cpp b/modules/platforms/cpp/ignite/odbc/utility.cpp
index feaee86499..bb51a09ee0 100644
--- a/modules/platforms/cpp/ignite/odbc/utility.cpp
+++ b/modules/platforms/cpp/ignite/odbc/utility.cpp
@@ -49,6 +49,9 @@ std::string sql_string_to_string(const unsigned char* sql_str, std::int32_t sql_
else if (sql_str_len > 0)
res.assign(sql_str_c, sql_str_len);
+ while (!res.empty() && res.back() == 0)
+ res.pop_back();
+
return res;
}
diff --git a/modules/platforms/cpp/ignite/client/detail/client_operation.h b/modules/platforms/cpp/ignite/protocol/client_operation.h
similarity index 100%
rename from modules/platforms/cpp/ignite/client/detail/client_operation.h
rename to modules/platforms/cpp/ignite/protocol/client_operation.h
diff --git a/modules/platforms/cpp/tests/client-test/compute_test.cpp b/modules/platforms/cpp/tests/client-test/compute_test.cpp
index 7aef5fcda3..69e6299889 100644
--- a/modules/platforms/cpp/tests/client-test/compute_test.cpp
+++ b/modules/platforms/cpp/tests/client-test/compute_test.cpp
@@ -248,7 +248,7 @@ TEST_F(compute_test, execute_colocated) {
auto resNodeName = m_client.get_compute().execute_colocated(TABLE_1, key, {}, NODE_NAME_JOB, {});
auto expectedNodeName = PLATFORM_TEST_NODE_RUNNER + var.second;
- EXPECT_EQ(expectedNodeName, resNodeName);
+ EXPECT_EQ(expectedNodeName, resNodeName.value().get<std::string>());
}
}
diff --git a/modules/platforms/cpp/tests/odbc-test/CMakeLists.txt b/modules/platforms/cpp/tests/odbc-test/CMakeLists.txt
index e93a944bf0..ce4b96bc1a 100644
--- a/modules/platforms/cpp/tests/odbc-test/CMakeLists.txt
+++ b/modules/platforms/cpp/tests/odbc-test/CMakeLists.txt
@@ -25,6 +25,7 @@ set(SOURCES
api_robustness_test.cpp
connection_test.cpp
main.cpp
+ meta_queries_test.cpp
)
add_executable(${TARGET} ${SOURCES})
diff --git a/modules/platforms/cpp/tests/odbc-test/api_robustness_test.cpp b/modules/platforms/cpp/tests/odbc-test/api_robustness_test.cpp
index cee3072d1d..6aea8e128f 100644
--- a/modules/platforms/cpp/tests/odbc-test/api_robustness_test.cpp
+++ b/modules/platforms/cpp/tests/odbc-test/api_robustness_test.cpp
@@ -32,7 +32,18 @@ using namespace ignite;
/**
* Test suite.
*/
-class api_robustness_test : public odbc_suite {};
+class api_robustness_test : public odbc_suite {
+public:
+ static void SetUpTestSuite() {
+ odbc_connection conn;
+ conn.odbc_connect(get_basic_connection_string());
+
+ auto table_avail = conn.wait_for_table(TABLE_NAME_ALL_COLUMNS_SQL, std::chrono::seconds(10));
+ if (!table_avail) {
+ FAIL() << "Table '" + TABLE_NAME_ALL_COLUMNS_SQL + "' is not available";
+ }
+ }
+};
std::vector<SQLSMALLINT> unsupported_c_types = {
SQL_C_INTERVAL_YEAR,
@@ -165,7 +176,7 @@ TEST_F(api_robustness_test, sql_prepare)
odbc_connect(get_basic_connection_string());
- SQLCHAR sql[] = "SELECT strField FROM TestType";
+ SQLCHAR sql[] = "select str from TBL_ALL_COLUMNS_SQL";
// Everything is ok.
SQLRETURN ret = SQLPrepare(m_statement, sql, sizeof(sql));
@@ -197,14 +208,11 @@ TEST_F(api_robustness_test, sql_exec_direct)
odbc_connect(get_basic_connection_string());
- SQLCHAR sql[] = "SELECT strField FROM TestType";
+ SQLCHAR sql[] = "select str from TBL_ALL_COLUMNS_SQL";
// Everything is ok.
- SQLRETURN ret = SQLExecDirect(m_statement, sql, sizeof(sql));
-
- UNUSED_VALUE ret;
- // TODO IGNITE-19212: Uncomment once query execution is implemented.
- //ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+ SQLRETURN ret = SQLExecDirect(m_statement, sql, sizeof(sql) - 1);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
SQLCloseCursor(m_statement);
@@ -214,7 +222,7 @@ TEST_F(api_robustness_test, sql_exec_direct)
SQLCloseCursor(m_statement);
// Value is null.
- SQLExecDirect(m_statement, 0, sizeof(sql));
+ SQLExecDirect(m_statement, 0, SQL_NTS);
SQLCloseCursor(m_statement);
@@ -360,7 +368,7 @@ TEST_F(api_robustness_test, sql_bind_parameter)
sql_type, 100, 100, &ind1, sizeof(ind1), &len1);
ASSERT_EQ(ret, SQL_ERROR);
- // TODO IGNITE-19212: Uncomment once column binding is implemented.
+ // TODO IGNITE-19205: Uncomment once column binding is implemented.
//EXPECT_EQ(get_statement_error_state(), "HY105");
}
@@ -384,12 +392,12 @@ TEST_F(api_robustness_test, sql_native_sql)
odbc_connect(get_basic_connection_string());
- SQLCHAR sql[] = "SELECT strField FROM TestType";
+ SQLCHAR sql[] = "select str from TBL_ALL_COLUMNS_SQL";
SQLCHAR buffer[ODBC_BUFFER_SIZE];
SQLINTEGER resLen = 0;
// Everything is ok.
- SQLRETURN ret = SQLNativeSql(m_conn, sql, sizeof(sql), buffer, sizeof(buffer), &resLen);
+ SQLRETURN ret = SQLNativeSql(m_conn, sql, sizeof(sql) - 1, buffer, sizeof(buffer), &resLen);
ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
@@ -397,16 +405,16 @@ TEST_F(api_robustness_test, sql_native_sql)
SQLNativeSql(m_conn, sql, 0, buffer, sizeof(buffer), &resLen);
// Buffer size is null.
- SQLNativeSql(m_conn, sql, sizeof(sql), buffer, 0, &resLen);
+ SQLNativeSql(m_conn, sql, sizeof(sql) - 1, buffer, 0, &resLen);
// Res size is null.
- SQLNativeSql(m_conn, sql, sizeof(sql), buffer, sizeof(buffer), 0);
+ SQLNativeSql(m_conn, sql, sizeof(sql) - 1, buffer, sizeof(buffer), 0);
// Value is null.
SQLNativeSql(m_conn, sql, 0, buffer, sizeof(buffer), &resLen);
// Buffer is null.
- SQLNativeSql(m_conn, sql, sizeof(sql), 0, sizeof(buffer), &resLen);
+ SQLNativeSql(m_conn, sql, sizeof(sql) - 1, 0, sizeof(buffer), &resLen);
// All nulls.
SQLNativeSql(m_conn, sql, 0, 0, 0, 0);
@@ -419,13 +427,10 @@ TEST_F(api_robustness_test, sql_col_attribute)
odbc_connect(get_basic_connection_string());
- SQLCHAR sql[] = "SELECT strField FROM TestType";
+ SQLCHAR sql[] = "select str from TBL_ALL_COLUMNS_SQL";
- SQLRETURN ret = SQLExecDirect(m_statement, sql, sizeof(sql));
-
- UNUSED_VALUE ret;
- // TODO IGNITE-19214: Uncomment once column metadata is implemented.
- //ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+ SQLRETURN ret = SQLExecDirect(m_statement, sql, sizeof(sql) - 1);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
SQLCHAR buffer[ODBC_BUFFER_SIZE];
SQLSMALLINT resLen = 0;
@@ -465,13 +470,10 @@ TEST_F(api_robustness_test, sql_describe_col)
odbc_connect(get_basic_connection_string());
- SQLCHAR sql[] = "SELECT strField FROM TestType";
-
- SQLRETURN ret = SQLExecDirect(m_statement, sql, sizeof(sql));
+ SQLCHAR sql[] = "select str from TBL_ALL_COLUMNS_SQL";
- UNUSED_VALUE ret;
- // TODO IGNITE-19214: Uncomment once column metadata is implemented.
- //ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+ SQLRETURN ret = SQLExecDirect(m_statement, sql, sizeof(sql) - 1);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
SQLCHAR columnName[ODBC_BUFFER_SIZE];
SQLSMALLINT columnNameLen = 0;
@@ -505,22 +507,16 @@ TEST_F(api_robustness_test, sql_row_count)
odbc_connect(get_basic_connection_string());
- SQLCHAR sql[] = "SELECT strField FROM TestType";
-
- SQLRETURN ret = SQLExecDirect(m_statement, sql, sizeof(sql));
+ SQLCHAR sql[] = "select str from TBL_ALL_COLUMNS_SQL";
- UNUSED_VALUE ret;
- // TODO IGNITE-19212: Uncomment once query execution is implemented.
- //ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+ SQLRETURN ret = SQLExecDirect(m_statement, sql, SQL_NTS);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
SQLLEN rows = 0;
// Everything is ok.
ret = SQLRowCount(m_statement, &rows);
-
- UNUSED_VALUE ret;
- // TODO IGNITE-19212: Uncomment once query execution is implemented.
- //ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
SQLRowCount(m_statement, 0);
}
@@ -687,7 +683,7 @@ TEST_F(api_robustness_test, sql_num_params)
odbc_connect(get_basic_connection_string());
- SQLCHAR sql[] = "SELECT strField FROM TestType";
+ SQLCHAR sql[] = "select str from TBL_ALL_COLUMNS_SQL";
// Everything is ok.
SQLRETURN ret = SQLPrepare(m_statement, sql, sizeof(sql));
diff --git a/modules/platforms/cpp/tests/odbc-test/meta_queries_test.cpp b/modules/platforms/cpp/tests/odbc-test/meta_queries_test.cpp
new file mode 100644
index 0000000000..d2bfa8c09c
--- /dev/null
+++ b/modules/platforms/cpp/tests/odbc-test/meta_queries_test.cpp
@@ -0,0 +1,863 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ignite/common/config.h"
+#include "odbc_suite.h"
+#include "odbc_connection.h"
+
+#include <gtest/gtest.h>
+
+#include <vector>
+#include <string>
+#include <algorithm>
+
+using namespace ignite;
+
+/**
+ * Checks single row result set for correct work with SQLGetData.
+ *
+ * @param stmt Statement.
+ */
+void check_single_row_result_set_with_get_data(SQLHSTMT stmt)
+{
+ SQLRETURN ret = SQLFetch(stmt);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << get_odbc_error_message(SQL_HANDLE_STMT, stmt);
+
+ char buf[1024];
+ SQLLEN buf_len = sizeof(buf);
+
+ ret = SQLGetData(stmt, 1, SQL_C_CHAR, buf, sizeof(buf), &buf_len);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << get_odbc_error_message(SQL_HANDLE_STMT, stmt);
+
+ ret = SQLFetch(stmt);
+
+ ASSERT_EQ(ret, SQL_NO_DATA);
+
+ ret = SQLGetData(stmt, 1, SQL_C_CHAR, buf, sizeof(buf), &buf_len);
+
+ ASSERT_EQ(ret, SQL_ERROR);
+ EXPECT_EQ(get_odbc_error_state(SQL_HANDLE_STMT, stmt), "24000");
+}
+
+/**
+ * Check string column.
+ *
+ * @param stmt Statement.
+ * @param colId Column ID to check.
+ * @param value Expected value.
+ */
+void check_string_column(SQLHSTMT stmt, int colId, const std::string& value)
+{
+ char buf[1024];
+ SQLLEN bufLen = sizeof(buf);
+
+ SQLRETURN ret = SQLGetData(stmt, colId, SQL_C_CHAR, buf, sizeof(buf), &bufLen);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << get_odbc_error_message(SQL_HANDLE_STMT, stmt);
+
+ if (bufLen <= 0)
+ EXPECT_TRUE(value.empty());
+ else
+ EXPECT_EQ(std::string(buf, static_cast<size_t>(bufLen)), value);
+}
+
+/**
+ * Check result set column metadata using SQLDescribeCol.
+ *
+ * @param stmt Statement.
+ * @param idx Index.
+ * @param exp_name Expected name.
+ * @param exp_data_type Expected data type.
+ * @param exp_size Expected column size.
+ * @param exp_scale Expected column scale.
+ * @param exp_nullability expected nullability.
+ */
+void check_column_meta_with_sqldescribe_col(SQLHSTMT stmt, SQLUSMALLINT idx, const std::string& exp_name,
+ SQLSMALLINT exp_data_type, SQLULEN exp_size, SQLSMALLINT exp_scale, SQLSMALLINT exp_nullability)
+{
+ std::vector<SQLCHAR> name(ODBC_BUFFER_SIZE);
+ SQLSMALLINT name_len = 0;
+ SQLSMALLINT data_type = 0;
+ SQLULEN size;
+ SQLSMALLINT scale;
+ SQLSMALLINT nullability;
+
+ SQLRETURN ret = SQLDescribeCol(
+ stmt, idx, &name[0], (SQLSMALLINT)name.size(), &name_len, &data_type, &size, &scale, &nullability);
+
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, stmt);
+
+ EXPECT_GE(name_len, 0);
+ EXPECT_LE(name_len, static_cast<SQLSMALLINT>(ODBC_BUFFER_SIZE));
+
+ std::string name_str(name.begin(), name.begin() + name_len);
+
+ EXPECT_EQ(name_str, exp_name);
+ EXPECT_EQ(data_type, exp_data_type);
+ EXPECT_EQ(size, exp_size);
+ EXPECT_EQ(scale, exp_scale);
+ EXPECT_EQ(nullability, exp_nullability);
+}
+
+/**
+ * Check result set column metadata using SQLColAttribute.
+ *
+ * @param stmt Statement.
+ * @param idx Index.
+ * @param exp_name Expected name.
+ * @param exp_data_type Expected data type.
+ * @param exp_size Expected column size.
+ * @param exp_scale Expected column scale.
+ * @param exp_nullability expected nullability.
+ */
+void check_column_meta_with_sqlcol_attribute(SQLHSTMT stmt, SQLUSMALLINT idx, const std::string& exp_name,
+ SQLLEN exp_data_type, SQLULEN exp_size, SQLLEN exp_scale, SQLLEN exp_nullability)
+{
+ std::vector<SQLCHAR> name(ODBC_BUFFER_SIZE);
+ SQLSMALLINT name_len = 0;
+ SQLLEN data_type = 0;
+ SQLLEN size;
+ SQLLEN scale;
+ SQLLEN nullability;
+
+ SQLRETURN ret = SQLColAttribute(stmt, idx, SQL_DESC_NAME, &name[0], (SQLSMALLINT)name.size(), &name_len, nullptr);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, stmt);
+
+ ret = SQLColAttribute(stmt, idx, SQL_DESC_TYPE, nullptr, 0, nullptr, &data_type);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, stmt);
+
+ ret = SQLColAttribute(stmt, idx, SQL_DESC_PRECISION, nullptr, 0, nullptr, &size);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, stmt);
+
+ ret = SQLColAttribute(stmt, idx, SQL_DESC_SCALE, nullptr, 0, nullptr, &scale);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, stmt);
+
+ ret = SQLColAttribute(stmt, idx, SQL_DESC_NULLABLE, nullptr, 0, nullptr, &nullability);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, stmt);
+
+ EXPECT_GE(name_len, 0);
+ EXPECT_LE(name_len, static_cast<SQLSMALLINT>(ODBC_BUFFER_SIZE));
+
+ std::string name_str(name.begin(), name.begin() + name_len);
+
+ EXPECT_EQ(name_str, exp_name);
+ EXPECT_EQ(data_type, exp_data_type);
+ EXPECT_EQ(size, exp_size);
+ EXPECT_EQ(scale, exp_scale);
+ EXPECT_EQ(nullability, exp_nullability);
+}
+
+/**
+ * Test suite.
+ */
+class meta_queries_test : public odbc_suite {
+public:
+ static void SetUpTestSuite() {
+ odbc_connection conn;
+ conn.odbc_connect(get_basic_connection_string());
+
+ auto table_avail = conn.wait_for_table(TABLE_NAME_ALL_COLUMNS_SQL, std::chrono::seconds(10));
+ if (!table_avail) {
+ FAIL() << "Table '" + TABLE_NAME_ALL_COLUMNS_SQL + "' is not available";
+ }
+
+ SQLRETURN ret = conn.exec_query("CREATE TABLE META_QUERIES_TEST(ID INT PRIMARY KEY, STR VARCHAR(60))");
+ if (!SQL_SUCCEEDED(ret)) {
+ FAIL() << conn.get_statement_error_message();
+ }
+ }
+
+ static void TearDownTestSuite() {
+ odbc_connection conn;
+ conn.odbc_connect(get_basic_connection_string());
+
+ conn.exec_query("DROP TABLE META_QUERIES_TEST");
+ }
+
+ void SetUp() override {
+ odbc_connect(get_basic_connection_string());
+ exec_query("DELETE FROM " + TABLE_NAME_ALL_COLUMNS_SQL);
+ odbc_clean_up();
+ }
+
+ void TearDown() override {
+ odbc_connect(get_basic_connection_string());
+ exec_query("DROP TABLE IF EXISTS test_scale_precision");
+ odbc_clean_up();
+ }
+
+ /**
+ * @param exec Function to call before tests. May be PrepareQuery or ExecQuery.
+ *
+ * 1. Connect to node using ODBC.
+ * 2. Create table with decimal and char columns with specified size and scale.
+ * 3. Execute or prepare statement.
+ * 4. Check precision and scale of every column using SQLDescribeCol.
+ */
+ template<typename F1, typename F2>
+ void check_col_precision_and_scale(F1 exec, F2 check)
+ {
+ odbc_connect(get_basic_connection_string());
+
+ SQLRETURN ret = exec_query(
+ "create table test_scale_precision("
+ " id int primary key,"
+ " dec1 decimal(3,0),"
+ " dec2 decimal(42,12),"
+ " dec3 decimal,"
+ " char1 char(3),"
+ " char2 char(42),"
+ " char3 char not null,"
+ " vchar varchar"
+ ")");
+
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ ret = SQLFreeStmt(m_statement, SQL_CLOSE);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ ret = exec_query(
+ "insert into "
+ "test_scale_precision(id, dec1, dec2, dec3, char1, char2, char3, vchar) "
+ "values (1, 12, 160.23, -1234.56789, 'TST', 'Lorem Ipsum', 'Some test value', 'Some test varchar')");
+
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ ret = SQLFreeStmt(m_statement, SQL_CLOSE);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ ret = (this->*exec)("select id, dec1, dec2, dec3, char1, char2, char3, vchar from PUBLIC.test_scale_precision");
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ SQLSMALLINT columnCount = 0;
+
+ ret = SQLNumResultCols(m_statement, &columnCount);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ EXPECT_EQ(columnCount, 8);
+
+ check(m_statement, 1, "ID", SQL_INTEGER, 10, 0, SQL_NO_NULLS);
+ check(m_statement, 2, "DEC1", SQL_DECIMAL, 3, 0, SQL_NULLABLE);
+ check(m_statement, 3, "DEC2", SQL_DECIMAL, 42, 12, SQL_NULLABLE);
+ check(m_statement, 4, "DEC3", SQL_DECIMAL, 32767, 0, SQL_NULLABLE);
+ check(m_statement, 5, "CHAR1", SQL_VARCHAR, 3, 0, SQL_NULLABLE);
+ check(m_statement, 6, "CHAR2", SQL_VARCHAR, 42, 0, SQL_NULLABLE);
+ check(m_statement, 7, "CHAR3", SQL_VARCHAR, 1, 0, SQL_NO_NULLS);
+ check(m_statement, 8, "VCHAR", SQL_VARCHAR, 65536, 0, SQL_NULLABLE);
+ }
+
+ void insert_test_string()
+ {
+ auto insert_req =
+ "INSERT INTO " + TABLE_NAME_ALL_COLUMNS_SQL + "(key, str) VALUES(42, 'Lorem ipsum')";
+
+ SQLRETURN ret = exec_query(insert_req);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << get_odbc_error_message(SQL_HANDLE_STMT, m_statement);
+
+ // Resetting parameters.
+ ret = SQLFreeStmt(m_statement, SQL_RESET_PARAMS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << get_odbc_error_message(SQL_HANDLE_STMT, m_statement);
+ }
+
+};
+
+// TODO IGNITE-19216 Implement type info fetching
+#ifdef MUTED
+TEST_F(meta_queries_test, test_get_type_info_all_types)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLRETURN ret = SQLGetTypeInfo(m_statement, SQL_ALL_TYPES);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+}
+#endif // MUTED
+
+TEST_F(meta_queries_test, test_date_type_column_attribute_curdate)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR req[] = "select {fn CURDATE()}";
+ SQLRETURN ret = SQLExecDirect(m_statement, req, SQL_NTS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ SQLLEN int_val = 0;
+
+ ret = SQLColAttribute(m_statement, 1, SQL_DESC_TYPE, nullptr, 0, nullptr, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ EXPECT_EQ(int_val, SQL_TYPE_DATE);
+}
+
+TEST_F(meta_queries_test, test_date_type_column_attribute_literal)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR req[] = "select DATE '2020-10-25'";
+ SQLExecDirect(m_statement, req, SQL_NTS);
+
+ SQLLEN int_val = 0;
+
+ SQLRETURN ret = SQLColAttribute(m_statement, 1, SQL_DESC_TYPE, nullptr, 0, nullptr, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ EXPECT_EQ(int_val, SQL_TYPE_DATE);
+}
+
+TEST_F(meta_queries_test, test_date_type_column_attribute_field)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR req[] = "select {fn CONVERT(date, DATE)} from TBL_ALL_COLUMNS_SQL";
+ SQLRETURN ret = SQLExecDirect(m_statement, req, SQL_NTS);
+ if (!SQL_SUCCEEDED(ret))
+ {
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+ return;
+ }
+
+ SQLLEN int_val = 0;
+
+ ret = SQLColAttribute(m_statement, 1, SQL_DESC_TYPE, nullptr, 0, nullptr, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ {
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+ return;
+ }
+
+ EXPECT_EQ(int_val, SQL_TYPE_DATE);
+}
+
+TEST_F(meta_queries_test, test_time_type_column_attribute_literal)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR req[] = "select TIME '12:42:13'";
+ SQLExecDirect(m_statement, req, SQL_NTS);
+
+ SQLLEN int_val = 0;
+
+ SQLRETURN ret = SQLColAttribute(m_statement, 1, SQL_DESC_TYPE, nullptr, 0, nullptr, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ EXPECT_EQ(int_val, SQL_TYPE_TIME);
+}
+
+TEST_F(meta_queries_test, test_time_type_column_attribute_field)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR req[] = "select time2 from TBL_ALL_COLUMNS_SQL";
+ SQLExecDirect(m_statement, req, SQL_NTS);
+
+ SQLLEN int_val = 0;
+
+ SQLRETURN ret = SQLColAttribute(m_statement, 1, SQL_DESC_TYPE, nullptr, 0, nullptr, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ EXPECT_EQ(int_val, SQL_TYPE_TIME);
+}
+
+TEST_F(meta_queries_test, test_col_attributes_column_length)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR req[] = "select str from META_QUERIES_TEST";
+ SQLExecDirect(m_statement, req, SQL_NTS);
+
+ SQLLEN int_val;
+ SQLCHAR str_buf[1024];
+ SQLSMALLINT str_len;
+
+ SQLRETURN ret = SQLColAttribute(m_statement, 1, SQL_COLUMN_LENGTH, str_buf, sizeof(str_buf), &str_len, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ EXPECT_EQ(int_val, 60);
+}
+
+TEST_F(meta_queries_test, test_col_attributes_column_presicion)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR req[] = "select str from META_QUERIES_TEST";
+ SQLExecDirect(m_statement, req, SQL_NTS);
+
+ SQLLEN int_val;
+ SQLCHAR str_buf[1024];
+ SQLSMALLINT str_len;
+
+ SQLRETURN ret = SQLColAttribute(m_statement, 1, SQL_COLUMN_PRECISION, str_buf, sizeof(str_buf), &str_len, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ EXPECT_EQ(int_val, 60);
+}
+
+TEST_F(meta_queries_test, test_col_attributes_column_scale)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR req[] = "select str from TBL_ALL_COLUMNS_SQL";
+ SQLExecDirect(m_statement, req, SQL_NTS);
+
+ SQLLEN int_val;
+ SQLCHAR str_buf[1024];
+ SQLSMALLINT str_len;
+
+ SQLRETURN ret = SQLColAttribute(m_statement, 1, SQL_COLUMN_SCALE, str_buf, sizeof(str_buf), &str_len, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+}
+
+// TODO: IGNITE-19854 Implement metadata fetching for the non-executed query.
+#ifdef MUTED
+TEST_F(meta_queries_test, test_col_attributes_column_length_prepare)
+{
+ odbc_connect(get_basic_connection_string());
+
+ insert_test_string();
+
+ SQLCHAR req[] = "select str from TBL_ALL_COLUMNS_SQL";
+ SQLPrepare(m_statement, req, SQL_NTS);
+
+ SQLLEN int_val;
+ SQLCHAR str_buf[1024];
+ SQLSMALLINT str_len;
+
+ SQLRETURN ret = SQLColAttribute(m_statement, 1, SQL_COLUMN_LENGTH, str_buf, sizeof(str_buf), &str_len, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ EXPECT_EQ(int_val, 60);
+
+ ret = SQLExecute(m_statement);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ ret = SQLColAttribute(m_statement, 1, SQL_COLUMN_LENGTH, str_buf, sizeof(str_buf), &str_len, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ EXPECT_EQ(int_val, 60);
+}
+
+TEST_F(meta_queries_test, test_col_attributes_column_presicion_prepare)
+{
+ odbc_connect(get_basic_connection_string());
+
+ insert_test_string();
+
+ SQLCHAR req[] = "select str from TBL_ALL_COLUMNS_SQL";
+ SQLPrepare(m_statement, req, SQL_NTS);
+
+ SQLLEN int_val;
+ SQLCHAR str_buf[1024];
+ SQLSMALLINT str_len;
+
+ SQLRETURN ret = SQLColAttribute(m_statement, 1, SQL_COLUMN_PRECISION, str_buf, sizeof(str_buf), &str_len, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ EXPECT_EQ(int_val, 60);
+
+ ret = SQLExecute(m_statement);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ ret = SQLColAttribute(m_statement, 1, SQL_COLUMN_PRECISION, str_buf, sizeof(str_buf), &str_len, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ EXPECT_EQ(int_val, 60);
+}
+
+TEST_F(meta_queries_test, test_col_attributes_column_scale_prepare)
+{
+ odbc_connect(get_basic_connection_string());
+
+ insert_test_string();
+
+ SQLCHAR req[] = "select str from TBL_ALL_COLUMNS_SQL";
+ SQLPrepare(m_statement, req, SQL_NTS);
+
+ SQLLEN int_val;
+ SQLCHAR str_buf[1024];
+ SQLSMALLINT str_len;
+
+ SQLRETURN ret = SQLColAttribute(m_statement, 1, SQL_COLUMN_SCALE, str_buf, sizeof(str_buf), &str_len, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ ret = SQLExecute(m_statement);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ ret = SQLColAttribute(m_statement, 1, SQL_COLUMN_SCALE, str_buf, sizeof(str_buf), &str_len, &int_val);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+}
+#endif //MUTED
+
+// TODO: IGNITE-19216 Implement type info query.
+#ifdef MUTED
+TEST_F(meta_queries_test, test_get_data_with_get_type_info)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLRETURN ret = SQLGetTypeInfo(m_statement, SQL_VARCHAR);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ check_single_row_result_set_with_get_data(m_statement);
+}
+#endif //MUTED
+
+// TODO: IGNITE-19214 Implement tables metadata fetching
+#ifdef MUTED
+TEST_F(meta_queries_test, test_get_data_with_tables)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR empty[] = "";
+ SQLCHAR table[] = "TestType";
+
+ SQLRETURN ret = SQLTables(m_statement, empty, SQL_NTS, empty, SQL_NTS, table, SQL_NTS, empty, SQL_NTS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ check_single_row_result_set_with_get_data(m_statement);
+}
+#endif //MUTED
+
+// TODO: IGNITE-19214 Implement table column metadata fetching
+#ifdef MUTED
+TEST_F(meta_queries_test, test_get_data_with_columns)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR empty[] = "";
+ SQLCHAR table[] = "TestType";
+ SQLCHAR column[] = "str";
+
+ SQLRETURN ret = SQLColumns(m_statement, empty, SQL_NTS, empty, SQL_NTS, table, SQL_NTS, column, SQL_NTS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ check_single_row_result_set_with_get_data(m_statement);
+}
+#endif //MUTED
+
+TEST_F(meta_queries_test, test_get_data_with_select_query)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR insert_req[] = "insert into META_QUERIES_TEST(id, str) VALUES(1, 'Lorem ipsum')";
+ SQLRETURN ret = SQLExecDirect(m_statement, insert_req, SQL_NTS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ SQLCHAR select_req[] = "select str from META_QUERIES_TEST";
+ ret = SQLExecDirect(m_statement, select_req, SQL_NTS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ // TODO: IGNITE-19213 Implement data fetching
+// check_single_row_result_set_with_get_data(m_statement);
+}
+
+TEST_F(meta_queries_test, test_insert_too_long_value_ok)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR insert_req[] =
+ "insert into META_QUERIES_TEST(id, str) "
+ "VALUES(42, '0000000000111111111122222222223333333333444444444455555555556666666666')";
+
+ SQLRETURN ret = SQLExecDirect(m_statement, insert_req, SQL_NTS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+}
+
+TEST_F(meta_queries_test, test_get_info_scroll_options)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLUINTEGER val = 0;
+ SQLRETURN ret = SQLGetInfo(m_conn, SQL_SCROLL_OPTIONS, &val, 0, nullptr);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_DBC, m_conn));
+
+ EXPECT_NE(val, 0);
+}
+
+// TODO: IGNITE-19214 Implement tables metadata fetching
+#ifdef MUTED
+TEST_F(meta_queries_test, test_ddl_tables_meta)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR empty[] = "";
+ SQLCHAR table[] = "TestTable";
+
+ SQLRETURN ret = SQLTables(m_statement, empty, SQL_NTS, empty, SQL_NTS, table, SQL_NTS, empty, SQL_NTS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ ret = SQLFetch(m_statement);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ check_string_column(m_statement, 1, "");
+ check_string_column(m_statement, 2, "\"PUBLIC\"");
+ check_string_column(m_statement, 3, "META_QUERIES_TEST");
+ check_string_column(m_statement, 4, "TABLE");
+
+ ret = SQLFetch(m_statement);
+
+ ASSERT_EQ(ret, SQL_NO_DATA);
+}
+
+TEST_F(meta_queries_test, test_ddl_tables_meta_table_type_list)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR create_table[] = "create table TestTable(id int primary key, testColumn varchar)";
+ SQLRETURN ret = SQLExecDirect(m_statement, create_table, SQL_NTS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ SQLCHAR* typeLists[] = {(SQLCHAR*)"'TABLE'", (SQLCHAR*)"TABLE,VIEW"};
+ for (auto &type_list : typeLists) {
+ SQLCHAR empty[] = "";
+ SQLCHAR table[] = "TestTable";
+
+ ret = SQLTables(m_statement, empty, SQL_NTS, empty, SQL_NTS, table, SQL_NTS, type_list, SQL_NTS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ ret = SQLFetch(m_statement);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ check_string_column(m_statement, 1, "");
+ check_string_column(m_statement, 2, "\"PUBLIC\"");
+ check_string_column(m_statement, 3, "TESTTABLE");
+ check_string_column(m_statement, 4, "TABLE");
+
+ ret = SQLFetch(m_statement);
+
+ ASSERT_EQ(ret, SQL_NO_DATA);
+ }
+}
+#endif //MUTED
+
+// TODO: IGNITE-19214 Implement table column metadata fetching
+#ifdef MUTED
+TEST_F(meta_queries_test, test_ddl_columns_meta)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR create_table[] = "create table TestTable(id int primary key, testColumn varchar)";
+ SQLRETURN ret = SQLExecDirect(m_statement, create_table, SQL_NTS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ SQLCHAR empty[] = "";
+ SQLCHAR table[] = "TestTable";
+
+ ret = SQLColumns(m_statement, empty, SQL_NTS, empty, SQL_NTS, table, SQL_NTS, empty, SQL_NTS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ ret = SQLFetch(m_statement);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ check_string_column(m_statement, 1, "");
+ check_string_column(m_statement, 2, "\"PUBLIC\"");
+ check_string_column(m_statement, 3, "TESTTABLE");
+ check_string_column(m_statement, 4, "ID");
+
+ ret = SQLFetch(m_statement);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ check_string_column(m_statement, 1, "");
+ check_string_column(m_statement, 2, "\"PUBLIC\"");
+ check_string_column(m_statement, 3, "TESTTABLE");
+ check_string_column(m_statement, 4, "TESTCOLUMN");
+
+ ret = SQLFetch(m_statement);
+
+ ASSERT_EQ(ret, SQL_NO_DATA);
+}
+
+TEST_F(meta_queries_test, test_ddl_columns_meta_escaped)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLCHAR create_table[] = "create table ESG_FOCUS(id int primary key, TEST_COLUMN varchar)";
+ SQLRETURN ret = SQLExecDirect(m_statement, create_table, SQL_NTS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ SQLCHAR empty[] = "";
+ SQLCHAR table[] = "ESG\\_FOCUS";
+
+ ret = SQLColumns(m_statement, empty, SQL_NTS, empty, SQL_NTS, table, SQL_NTS, empty, SQL_NTS);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ ret = SQLFetch(m_statement);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ check_string_column(m_statement, 1, "");
+ check_string_column(m_statement, 2, "\"PUBLIC\"");
+ check_string_column(m_statement, 3, "ESG_FOCUS");
+ check_string_column(m_statement, 4, "ID");
+
+ ret = SQLFetch(m_statement);
+
+ if (!SQL_SUCCEEDED(ret))
+ FAIL() << (get_odbc_error_message(SQL_HANDLE_STMT, m_statement));
+
+ check_string_column(m_statement, 1, "");
+ check_string_column(m_statement, 2, "\"PUBLIC\"");
+ check_string_column(m_statement, 3, "ESG_FOCUS");
+ check_string_column(m_statement, 4, "TEST_COLUMN");
+
+ ret = SQLFetch(m_statement);
+
+ ASSERT_EQ(ret, SQL_NO_DATA);
+}
+#endif //MUTED
+
+// TODO: IGNITE-19854 Implement metadata fetching for the non-executed query.
+#ifdef MUTED
+TEST_F(meta_queries_test, test_sqlnum_result_cols_after_sqlprepare)
+{
+ odbc_connect(get_basic_connection_string());
+
+ SQLRETURN ret = exec_query("create table TestSqlPrepare(id int primary key, test1 varchar, test2 long, test3 varchar)");
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ ret = SQLFreeStmt(m_statement, SQL_CLOSE);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ ret = prepare_query("select * from PUBLIC.TestSqlPrepare");
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ SQLSMALLINT columnCount = 0;
+
+ ret = SQLNumResultCols(m_statement, &columnCount);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ EXPECT_EQ(columnCount, 4);
+
+ ret = SQLExecute(m_statement);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ columnCount = 0;
+
+ ret = SQLNumResultCols(m_statement, &columnCount);
+ ODBC_FAIL_ON_ERROR(ret, SQL_HANDLE_STMT, m_statement);
+
+ EXPECT_EQ(columnCount, 4);
+}
+#endif //MUTED
+
+/**
+ * Check that SQLDescribeCol return valid scale and precision for columns of different type after Prepare.
+ */
+TEST_F(meta_queries_test, test_sqldescribe_col_precision_and_scale_after_prepare)
+{
+ // TODO: IGNITE-19854 Implement metadata fetching for the non-executed query.
+// check_col_precision_and_scale(&odbc_suite::prepare_query, &check_column_meta_with_sqldescribe_col);
+}
+
+/**
+ * Check that SQLDescribeCol return valid scale and precision for columns of different type after Execute.
+ */
+TEST_F(meta_queries_test, test_sqldescribe_col_precision_and_scale_after_exec)
+{
+ check_col_precision_and_scale(&odbc_suite::exec_query, &check_column_meta_with_sqldescribe_col);
+}
+
+/**
+ * Check that SQLColAttribute return valid scale and precision for columns of different type after Prepare.
+ */
+TEST_F(meta_queries_test, test_sqlcol_attribute_precision_and_scale_after_prepare)
+{
+ // TODO: IGNITE-19854 Implement metadata fetching for the non-executed query.
+// check_col_precision_and_scale(&odbc_suite::prepare_query, &check_column_meta_with_sqlcol_attribute);
+}
+
+/**
+ * Check that SQLColAttribute return valid scale and precision for columns of different type after Execute.
+ */
+TEST_F(meta_queries_test, test_sqlcol_attribute_precision_and_scale_after_exec)
+{
+ check_col_precision_and_scale(&odbc_suite::exec_query, &check_column_meta_with_sqlcol_attribute);
+}
diff --git a/modules/platforms/cpp/tests/odbc-test/odbc_connection.h b/modules/platforms/cpp/tests/odbc-test/odbc_connection.h
new file mode 100644
index 0000000000..4345a88235
--- /dev/null
+++ b/modules/platforms/cpp/tests/odbc-test/odbc_connection.h
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#ifdef _WIN32
+# include <windows.h>
+#endif
+
+#include "odbc_test_utils.h"
+#include "test_utils.h"
+
+#include <gtest/gtest.h>
+
+#include <memory>
+#include <string_view>
+#include <utility>
+#include <chrono>
+
+#include <sql.h>
+#include <sqlext.h>
+
+namespace ignite {
+
+/**
+ * Test suite.
+ */
+class odbc_connection {
+public:
+ /**
+ * Destructor.
+ */
+ ~odbc_connection() {
+ odbc_clean_up();
+ }
+
+ /**
+ * Prepare handles for connection.
+ */
+ void prepare_environment() {
+ ignite::prepare_environment(m_env, m_conn);
+ }
+
+ /**
+ * ODBC connect.
+ *
+ * @param connect_str Connect string.
+ */
+ void odbc_connect(std::string_view connect_str) {
+ ignite::odbc_connect(connect_str, m_env, m_conn, m_statement);
+ }
+
+ /**
+ * Disconnect.
+ */
+ void odbc_disconnect() {
+ ignite::odbc_disconnect(m_conn, m_statement);
+ }
+
+ /**
+ * Clean up handles.
+ */
+ void odbc_clean_up() {
+ ignite::odbc_clean_up(m_env, m_conn, m_statement);
+ }
+
+ /**
+ * Execute query.
+ *
+ * @param qry Query.
+ * @return Result.
+ */
+ SQLRETURN exec_query(const std::string& qry) { // NOLINT(readability-make-member-function-const)
+ auto sql = make_odbc_string(qry);
+ return SQLExecDirect(m_statement, sql.data(), static_cast<SQLINTEGER>(sql.size()));
+ }
+
+ /**
+ * Get statement error state.
+ *
+ * @return Statement error state.
+ */
+ [[nodiscard]] std::string get_statement_error_state() const {
+ return get_odbc_error_state(SQL_HANDLE_STMT, m_statement);
+ }
+
+ /**
+ * Get statement error message.
+ *
+ * @return Statement error message.
+ */
+ [[nodiscard]] std::string get_statement_error_message() const {
+ return get_odbc_error_message(SQL_HANDLE_STMT, m_statement);
+ }
+
+ /**
+ * Get connection error state.
+ *
+ * @return Connection error state.
+ */
+ [[nodiscard]] std::string get_connection_error_state() const {
+ return get_odbc_error_state(SQL_HANDLE_DBC, m_conn);
+ }
+
+ /**
+ * Wait for table to become available.
+ *
+ * @param table Table name to wait.
+ * @param timeout Timeout.
+ * @return @c true if table is available, @c false on timeout.
+ */
+ bool wait_for_table(const std::string &table, std::chrono::seconds timeout) {
+ auto start_time = std::chrono::steady_clock::now();
+ do {
+ auto res = exec_query("select * from " + table);
+ if (SQL_SUCCEEDED(res))
+ return true;
+
+ std::this_thread::sleep_for(std::chrono::seconds(1));
+ } while ((std::chrono::steady_clock::now() - start_time) < timeout);
+ return false;
+ }
+
+ /** Environment handle. */
+ SQLHENV m_env{SQL_NULL_HANDLE};
+
+ /** Connection handle. */
+ SQLHDBC m_conn{SQL_NULL_HANDLE};
+
+ /** Statement handle. */
+ SQLHSTMT m_statement{SQL_NULL_HANDLE};
+};
+
+} // namespace ignite
diff --git a/modules/platforms/cpp/tests/odbc-test/odbc_suite.h b/modules/platforms/cpp/tests/odbc-test/odbc_suite.h
index b639fb180f..03ed47be03 100644
--- a/modules/platforms/cpp/tests/odbc-test/odbc_suite.h
+++ b/modules/platforms/cpp/tests/odbc-test/odbc_suite.h
@@ -22,6 +22,7 @@
#endif
#include "ignite_runner.h"
+#include "odbc_connection.h"
#include "odbc_test_utils.h"
#include "test_utils.h"
@@ -35,16 +36,14 @@
namespace ignite {
-using namespace std::string_view_literals;
-
/**
* Test suite.
*/
-class odbc_suite : public ::testing::Test {
+class odbc_suite : public ::testing::Test, public odbc_connection {
public:
- static constexpr std::string_view TABLE_1 = "tbl1"sv;
- static constexpr std::string_view TABLE_NAME_ALL_COLUMNS = "tbl_all_columns"sv;
- static constexpr std::string_view TABLE_NAME_ALL_COLUMNS_SQL = "tbl_all_columns_sql"sv;
+ static inline const std::string TABLE_1 = "tbl1";
+ static inline const std::string TABLE_NAME_ALL_COLUMNS = "tbl_all_columns";
+ static inline const std::string TABLE_NAME_ALL_COLUMNS_SQL = "tbl_all_columns_sql";
static constexpr const char *KEY_COLUMN = "key";
static constexpr const char *VAL_COLUMN = "val";
@@ -72,79 +71,6 @@ public:
static std::string get_basic_connection_string() {
return "driver={" + DRIVER_NAME + "};address=" + get_nodes_address() + ';';
}
-
- /**
- * Prepare handles for connection.
- */
- void prepare_environment(){
- // Allocate an environment handle
- SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &m_env);
-
- EXPECT_TRUE(m_env != nullptr);
-
- // We want ODBC 3 support
- SQLSetEnvAttr(m_env, SQL_ATTR_ODBC_VERSION, reinterpret_cast<void*>(SQL_OV_ODBC3), 0);
-
- // Allocate a connection handle
- SQLAllocHandle(SQL_HANDLE_DBC, m_env, &m_conn);
-
- EXPECT_TRUE(m_conn != nullptr);
- }
-
- /**
- * ODBC connect.
- *
- * @param connect_str Connect string.
- */
- void odbc_connect(std::string_view connect_str) {
- prepare_environment();
-
- // Connect string
- std::vector<SQLCHAR> connect_str0(connect_str.begin(), connect_str.end());
-
- SQLCHAR out_str[ODBC_BUFFER_SIZE];
- SQLSMALLINT out_str_len;
-
- // Connecting to ODBC server.
- SQLRETURN ret = SQLDriverConnect(m_conn, NULL, &connect_str0[0], static_cast<SQLSMALLINT>(connect_str0.size()),
- out_str, sizeof(out_str), &out_str_len, SQL_DRIVER_COMPLETE);
-
- if (!SQL_SUCCEEDED(ret)) {
- FAIL() << get_odbc_error_message(SQL_HANDLE_DBC, m_conn);
- }
-
- // Allocate a statement handle
- SQLAllocHandle(SQL_HANDLE_STMT, m_conn, &m_statement);
-
- EXPECT_TRUE(m_statement != nullptr);
- }
-
- /**
- * Get statement error state.
- *
- * @return Statement error state.
- */
- [[nodiscard]] std::string get_statement_error_state() const {
- return get_odbc_error_state(SQL_HANDLE_STMT, m_statement);
- }
-
- /**
- * Get connection error state.
- *
- * @return Connection error state.
- */
- [[nodiscard]] std::string get_connection_error_state() const {
- return get_odbc_error_state(SQL_HANDLE_DBC, m_conn);
- }
-
- /** Environment handle. */
- SQLHENV m_env{SQL_NULL_HANDLE};
-
- /** Connection handle. */
- SQLHDBC m_conn{SQL_NULL_HANDLE};
-
- /** Statement handle. */
- SQLHSTMT m_statement{SQL_NULL_HANDLE};
};
} // namespace ignite
diff --git a/modules/platforms/cpp/tests/odbc-test/odbc_test_utils.h b/modules/platforms/cpp/tests/odbc-test/odbc_test_utils.h
index 2828f04921..ce004f996a 100644
--- a/modules/platforms/cpp/tests/odbc-test/odbc_test_utils.h
+++ b/modules/platforms/cpp/tests/odbc-test/odbc_test_utils.h
@@ -97,4 +97,104 @@ constexpr size_t ODBC_BUFFER_SIZE = 1024;
return {str.begin(), str.end()};
}
+/**
+ * Convert string to SQLCHAR vector.
+ *
+ * @param str String.
+ * @return SQLCHAR vector.
+ */
+[[nodiscard]] inline std::vector<SQLCHAR> make_odbc_string(std::string_view str) {
+ return {str.begin(), str.end()};
+}
+
+/**
+ * Prepare handles for connection.
+ *
+ * @param env Environment handle.
+ * @param conn Connection handle.
+ */
+inline void prepare_environment(SQLHENV &env, SQLHDBC &conn) {
+ // Allocate an environment handle
+ SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
+
+ EXPECT_TRUE(env != SQL_NULL_HANDLE);
+
+ // We want ODBC 3 support
+ SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, reinterpret_cast<void*>(SQL_OV_ODBC3), 0);
+
+ // Allocate a connection handle
+ SQLAllocHandle(SQL_HANDLE_DBC, env, &conn);
+
+ EXPECT_TRUE(conn != SQL_NULL_HANDLE);
+}
+
+/**
+ * ODBC connect.
+ *
+ * @param connect_str Connect string.
+ * @param env Environment handle.
+ * @param conn Connection handle.
+ * @param stmt Statement handle.
+ */
+inline void odbc_connect(std::string_view connect_str, SQLHENV &env, SQLHDBC &conn, SQLHSTMT &stmt) {
+ prepare_environment(env, conn);
+
+ // Connect string
+ auto connect_str0 = make_odbc_string(connect_str);
+
+ SQLCHAR out_str[ODBC_BUFFER_SIZE];
+ SQLSMALLINT out_str_len;
+
+ // Connecting to ODBC server.
+ SQLRETURN ret = SQLDriverConnect(conn, nullptr, &connect_str0[0],
+ static_cast<SQLSMALLINT>(connect_str0.size()), out_str, sizeof(out_str), &out_str_len, SQL_DRIVER_COMPLETE);
+
+ if (!SQL_SUCCEEDED(ret)) {
+ FAIL() << get_odbc_error_message(SQL_HANDLE_DBC, conn);
+ }
+
+ // Allocate a statement handle
+ SQLAllocHandle(SQL_HANDLE_STMT, conn, &stmt);
+
+ EXPECT_TRUE(stmt != SQL_NULL_HANDLE);
+}
+
+/**
+ * Disconnect.
+ * @param conn Connection handle.
+ * @param stmt Statement handle.
+ */
+inline void odbc_disconnect(SQLHDBC &conn, SQLHSTMT &stmt) {
+ if (stmt) {
+ // Releasing the statement handle.
+ SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+ stmt = SQL_NULL_HANDLE;
+ }
+
+ if (conn) {
+ // Disconnecting from the server.
+ SQLDisconnect(conn);
+
+ // Releasing allocated handles.
+ SQLFreeHandle(SQL_HANDLE_DBC, conn);
+ conn = SQL_NULL_HANDLE;
+ }
+}
+
+/**
+ * Clean up handles.
+ * @param env Environment handle.
+ * @param conn Connection handle.
+ * @param stmt Statement handle.
+ */
+inline void odbc_clean_up(SQLHENV &env, SQLHDBC &conn, SQLHSTMT &stmt) {
+ odbc_disconnect(conn, stmt);
+
+ if (env) {
+ // Releasing allocated handles.
+ SQLFreeHandle(SQL_HANDLE_ENV, env);
+ env = SQL_NULL_HANDLE;
+ }
+}
+
} // namespace ignite