Skip to content
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
37 changes: 24 additions & 13 deletions odbc/src/odbc_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "odbc_compat.h"

#include <optional>

namespace {
namespace Odbc = NYdb::NOdbc;
using Odbc::TConnection;
Expand All @@ -21,12 +23,16 @@ namespace {
return Odbc::CallOdbc<Mode, Handle>(handle, std::forward<Fn>(fn));
}

template<class Char>
template<class Char, bool PreserveNull = false>
struct TLazyText {
Char* Value;
SQLINTEGER Length;
std::string Resolve() const {
return Odbc::GetString(Value, Length);
auto Resolve() const {
if constexpr (PreserveNull) {
return Value ? std::optional{Odbc::GetString(Value, Length)} : std::nullopt;
} else {
return Odbc::GetString(Value, Length);
}
}
};

Expand All @@ -44,6 +50,11 @@ namespace {
return {value, length};
}

template<class Char>
TLazyText<Char, true> OptionalText(Char* value, SQLINTEGER length) {
return {value, length};
}

template<class Handle, auto Method, class... Args>
SQLRETURN Forward(SQLHANDLE handle, Args&&... args) {
return Call<Odbc::ECallMode::Ordinary, Handle>(handle, [&](Handle* value) {
Expand Down Expand Up @@ -287,15 +298,15 @@ ODBC_FORWARD(SQLColumns, TStatement, TStatement::Columns,
(SQLHSTMT statementHandle, SQLCHAR* catalogName, SQLSMALLINT nameLength1,
SQLCHAR* schemaName, SQLSMALLINT nameLength2, SQLCHAR* tableName,
SQLSMALLINT nameLength3, SQLCHAR* columnName, SQLSMALLINT nameLength4),
(statementHandle, Text(catalogName, nameLength1), Text(schemaName, nameLength2),
Text(tableName, nameLength3), Text(columnName, nameLength4)))
(statementHandle, OptionalText(catalogName, nameLength1), OptionalText(schemaName, nameLength2),
OptionalText(tableName, nameLength3), OptionalText(columnName, nameLength4)))

ODBC_FORWARD(SQLTables, TStatement, TStatement::Tables,
(SQLHSTMT statementHandle, SQLCHAR* catalogName, SQLSMALLINT nameLength1,
SQLCHAR* schemaName, SQLSMALLINT nameLength2, SQLCHAR* tableName,
SQLSMALLINT nameLength3, SQLCHAR* tableType, SQLSMALLINT nameLength4),
(statementHandle, Text(catalogName, nameLength1), Text(schemaName, nameLength2),
Text(tableName, nameLength3), Text(tableType, nameLength4)))
(statementHandle, OptionalText(catalogName, nameLength1), OptionalText(schemaName, nameLength2),
OptionalText(tableName, nameLength3), OptionalText(tableType, nameLength4)))

ODBC_FORWARD(SQLCloseCursor, TStatement, TStatement::Close,
(SQLHSTMT statementHandle), (statementHandle, false))
Expand Down Expand Up @@ -371,15 +382,15 @@ ODBC_FORWARD(SQLStatistics, TStatement, TStatement::Statistics,
(SQLHSTMT statementHandle, SQLCHAR* catalogName, SQLSMALLINT nameLength1,
SQLCHAR* schemaName, SQLSMALLINT nameLength2, SQLCHAR* tableName,
SQLSMALLINT nameLength3, SQLUSMALLINT unique, SQLUSMALLINT reserved),
(statementHandle, Text(catalogName, nameLength1), Text(schemaName, nameLength2),
Text(tableName, nameLength3), unique, reserved))
(statementHandle, OptionalText(catalogName, nameLength1), OptionalText(schemaName, nameLength2),
OptionalText(tableName, nameLength3), unique, reserved))

ODBC_FORWARD(SQLSpecialColumns, TStatement, TStatement::SpecialColumns,
(SQLHSTMT statementHandle, SQLUSMALLINT identifierType, SQLCHAR* catalogName,
SQLSMALLINT nameLength1, SQLCHAR* schemaName, SQLSMALLINT nameLength2,
SQLCHAR* tableName, SQLSMALLINT nameLength3, SQLUSMALLINT scope, SQLUSMALLINT),
(statementHandle, Text(catalogName, nameLength1), Text(schemaName, nameLength2),
Text(tableName, nameLength3), identifierType, scope))
(statementHandle, OptionalText(catalogName, nameLength1), OptionalText(schemaName, nameLength2),
OptionalText(tableName, nameLength3), identifierType, scope))

ODBC_FORWARD(SQLColAttribute, TStatement, Odbc::NMetadata::ColAttribute,
(SQLHSTMT statementHandle, SQLUSMALLINT columnNumber, SQLUSMALLINT fieldIdentifier,
Expand Down Expand Up @@ -424,8 +435,8 @@ ODBC_FORWARD(SQLPrimaryKeys, TStatement, TStatement::PrimaryKeys,
(SQLHSTMT statementHandle, SQLCHAR* catalogName, SQLSMALLINT nameLength1,
SQLCHAR* schemaName, SQLSMALLINT nameLength2,
SQLCHAR* tableName, SQLSMALLINT nameLength3),
(statementHandle, Text(catalogName, nameLength1), Text(schemaName, nameLength2),
Text(tableName, nameLength3)))
(statementHandle, OptionalText(catalogName, nameLength1), OptionalText(schemaName, nameLength2),
OptionalText(tableName, nameLength3)))

ODBC_FORWARD(SQLForeignKeys, TStatement, TStatement::ForeignKeys,
(SQLHSTMT statementHandle, SQLCHAR* pkCatalogName, SQLSMALLINT nameLength1,
Expand Down
46 changes: 24 additions & 22 deletions odbc/src/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
#include <optional>
#include <vector>
#include <string>
#include <string_view>


namespace NYdb::NOdbc {

using TMetadataArgument = std::optional<std::string>;

class TStatement : public TErrorManager {
public:
TStatement(TConnection* conn);
Expand All @@ -39,30 +42,25 @@ class TStatement : public TErrorManager {
SQLRETURN BindCol(SQLUSMALLINT columnNumber, SQLSMALLINT targetType, SQLPOINTER targetValue, SQLLEN bufferLength, SQLLEN* strLenOrInd);
SQLRETURN BindParameter(SQLUSMALLINT paramNumber, SQLSMALLINT inputOutputType, SQLSMALLINT valueType, SQLSMALLINT parameterType, SQLULEN columnSize, SQLSMALLINT decimalDigits, SQLPOINTER parameterValuePtr, SQLLEN bufferLength, SQLLEN* strLenOrIndPtr);

SQLRETURN Columns(const std::string& catalogName,
const std::string& schemaName,
const std::string& tableName,
const std::string& columnName);
SQLRETURN Columns(const TMetadataArgument& catalogName, const TMetadataArgument& schemaName,
const TMetadataArgument& tableName, const TMetadataArgument& columnName);

SQLRETURN Tables(const std::string& catalogName,
const std::string& schemaName,
const std::string& tableName,
const std::string& tableType);
SQLRETURN Tables(const TMetadataArgument& catalogName, const TMetadataArgument& schemaName,
const TMetadataArgument& tableName, const TMetadataArgument& tableType);

SQLRETURN GetTypeInfo(SQLSMALLINT dataType);
SQLRETURN Statistics(const std::string& catalogName,
const std::string& schemaName,
const std::string& tableName,
SQLRETURN Statistics(const TMetadataArgument& catalogName,
const TMetadataArgument& schemaName,
const TMetadataArgument& tableName,
SQLUSMALLINT unique,
SQLUSMALLINT accuracy);
SQLRETURN SpecialColumns(const std::string& catalogName,
const std::string& schemaName,
const std::string& tableName,
SQLRETURN SpecialColumns(const TMetadataArgument& catalogName,
const TMetadataArgument& schemaName,
const TMetadataArgument& tableName,
SQLUSMALLINT identifierType,
SQLUSMALLINT scope);
SQLRETURN PrimaryKeys(const std::string& catalogName,
const std::string& schemaName,
const std::string& tableName);
SQLRETURN PrimaryKeys(const TMetadataArgument& catalogName, const TMetadataArgument& schemaName,
const TMetadataArgument& tableName);
SQLRETURN ForeignKeys(const std::string& pkCatalogName,
const std::string& pkSchemaName,
const std::string& pkTableName,
Expand Down Expand Up @@ -165,19 +163,23 @@ class TStatement : public TErrorManager {
std::optional<TDescriptorAttribute> ResolveDescriptorAttribute(SQLINTEGER attr);

SQLUSMALLINT FindNextNeedDataParam() const;
std::string GetTraversalRoot(const std::string& pattern) const;
std::string GetMetadataTableName(const std::string& path) const;
bool MetadataNamespaceMatches(const std::string& catalog, const std::string& schema) const;
bool MetadataNamespaceMatches(const TMetadataArgument& catalog,
const TMetadataArgument& schema,
bool catalogPatternsAllowed,
bool schemaPatternsAllowed) const;

NQuery::TExecuteQueryResult ExecuteQuery(
NQuery::TSession& session,
const NYdb::TParams& params,
SQLULEN paramSet);

NYdb::NRetry::TRetryOperationSettings MakeAutocommitRetrySettings();
std::vector<NScheme::TSchemeEntry> GetPatternEntries(const std::string& pattern);
SQLRETURN VisitEntry(const std::string& path, const std::string& pattern, std::vector<NScheme::TSchemeEntry>& resultEntries);
bool IsPatternMatch(const std::string& path, const std::string& pattern);
std::vector<NScheme::TSchemeEntry> GetMetadataEntries(const TMetadataArgument& tableName,
bool patternsAllowed);
void VisitEntry(const std::string& path, const std::string& tableName,
bool patternsAllowed, std::string_view literalPrefix, bool hasWildcard,
std::vector<NScheme::TSchemeEntry>& resultEntries);
std::optional<std::string> GetTableType(NScheme::ESchemeEntryType type);
};

Expand Down
Loading
Loading