Skip to content
Closed
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
1 change: 0 additions & 1 deletion odbc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ add_library(ydb-odbc SHARED
src/utils/cursor.cpp
src/utils/types.cpp
src/utils/util.cpp
src/utils/status_util.cpp
src/utils/convert.cpp
src/utils/error_manager.cpp
src/utils/handle.cpp
Expand Down
33 changes: 1 addition & 32 deletions odbc/src/connection.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "connection.h"
#include "statement.h"

#include "utils/escape.h"
#include "utils/param_rewrite.h"

#include <ydb-cpp-sdk/client/result/result.h>
#include <ydb-cpp-sdk/client/types/status/status.h>
Expand Down Expand Up @@ -122,37 +122,6 @@ NQuery::TTransaction& TConnection::GetOrCreateTx(TStatement& statement) {
return *Tx_;
}

std::optional<NQuery::TQueryClient> TConnection::GetClient() {
if (!Ydb_) {
return std::nullopt;
}
return Ydb_->QueryClient;
}

std::optional<NTable::TTableClient> TConnection::GetTableClient() {
if (!Ydb_) {
return std::nullopt;
}
return Ydb_->TableClient;
}

std::optional<NScheme::TSchemeClient> TConnection::GetSchemeClient() {
if (!Ydb_) {
return std::nullopt;
}
return Ydb_->SchemeClient;
}

SQLRETURN TConnection::Execute(TStatement& statement) {
// The caller holds the statement operation lock and a shared lifecycle gate.
std::unique_lock transaction(TransactionMutex_, std::defer_lock);
const bool explicitTransaction = !GetAutocommit();
if (explicitTransaction) {
transaction.lock();
}
return statement.ExecuteInternal();
}

void TConnection::EndTranFromEnvironment(SQLSMALLINT completionType) {
auto check = LockShared();
if (GetAutocommit()) {
Expand Down
10 changes: 5 additions & 5 deletions odbc/src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ class TConnection : public THandle {
bool IsConnected() const { return Ydb_.has_value(); }

void CloseStatementCursors();
// Test seam for holding transaction ordering without a server-side operation.
// Serializes statements in the connection's explicit transaction.
std::unique_lock<std::mutex> LockTransaction() { return std::unique_lock(TransactionMutex_); }
SQLRETURN Execute(TStatement& statement);
void EndTranFromEnvironment(SQLSMALLINT completionType);

std::optional<NQuery::TQueryClient> GetClient();
// Borrowed clients remain valid while the caller holds the lifecycle gate.
NQuery::TQueryClient* GetClient() { return Ydb_ ? &Ydb_->QueryClient : nullptr; }
NQuery::TTransaction& GetOrCreateTx(TStatement& statement);
std::optional<NTable::TTableClient> GetTableClient();
std::optional<NScheme::TSchemeClient> GetSchemeClient();
NTable::TTableClient* GetTableClient() { return Ydb_ ? &Ydb_->TableClient : nullptr; }
NScheme::TSchemeClient* GetSchemeClient() { return Ydb_ ? &Ydb_->SchemeClient : nullptr; }

SQLRETURN SetAutocommit(bool value);
bool GetAutocommit() const;
Expand Down
86 changes: 46 additions & 40 deletions odbc/src/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,6 @@ SQLPOINTER Offset(SQLPOINTER pointer, SQLULEN offset, SQLULEN index, SQLULEN str
: nullptr;
}

bool IsCharacter(SQLSMALLINT type) {
return type == SQL_CHAR || type == SQL_VARCHAR || type == SQL_LONGVARCHAR
|| type == SQL_WCHAR || type == SQL_WVARCHAR || type == SQL_WLONGVARCHAR;
}

SQLSMALLINT DateTimeCode(SQLSMALLINT type) {
switch (type) {
case SQL_TYPE_DATE: return SQL_CODE_DATE;
case SQL_TYPE_TIME: return SQL_CODE_TIME;
case SQL_TYPE_TIMESTAMP: return SQL_CODE_TIMESTAMP;
default: return 0;
}
}

std::string TypeName(SQLSMALLINT type) {
const TSqlTypeSpec* spec = FindSqlTypeSpec(type);
return spec ? std::string(spec->Name) : type == SQL_GUID ? "GUID" : "";
}

template<typename T>
SQLRETURN WriteScalar(SQLPOINTER value, T scalar) {
*static_cast<T*>(value) = scalar;
Expand Down Expand Up @@ -66,21 +47,48 @@ TDescriptor::TDescriptor(TConnection* conn, EDescType type)
: THandle(conn)
, Type_(type) {}

void TDescriptor::Snapshot(TDescriptorState& state, uint64_t& generation) const {
bool TDescriptor::Snapshot(TDescriptorSnapshot& state, bool appParam) const {
if (GetGeneration() == state.Generation_) {
return false;
}
std::lock_guard lock(OperationMutex_);
SnapshotUnlocked(state, generation);
return SnapshotUnlocked(state, appParam);
}

void TDescriptor::SnapshotUnlocked(TDescriptorState& state, uint64_t& generation) const {
state = static_cast<const TDescriptorState&>(*this);
generation = GetGeneration();
bool TDescriptor::SnapshotUnlocked(TDescriptorSnapshot& state, bool appParam) const {
const bool shapeChanged = !HasSameParameterShape(state, appParam);
static_cast<TDescriptorState&>(state) = static_cast<const TDescriptorState&>(*this);
state.Generation_ = GetGeneration();
return shapeChanged;
}

void TDescriptor::SnapshotHeader(TDescriptorState& state, uint64_t& generation) const {
void TDescriptor::SnapshotHeader(TDescriptorSnapshot& state) const {
if (GetGeneration() == state.Generation_) {
return;
}
std::lock_guard lock(OperationMutex_);
// The implementation row descriptor is consumed only for status outputs.
state.Header_ = Header_;
generation = GetGeneration();
state.Generation_ = GetGeneration();
}

bool TDescriptorState::HasSameParameterShape(const TDescriptorState& other, bool appParam) const noexcept {
if (Records_.size() != other.Records_.size()) {
return false;
}
for (size_t i = 0; i < Records_.size(); ++i) {
const auto& record = Records_[i];
const auto& previous = other.Records_[i];
if (record.Active != previous.Active) {
return false;
}
if (record.Active && (record.Type != previous.Type
|| (!appParam && (record.Length != previous.Length || record.Scale != previous.Scale
|| record.WideTemporalType != previous.WideTemporalType)))) {
return false;
}
}
return true;
}

void TDescriptor::Clear() {
Expand Down Expand Up @@ -137,10 +145,7 @@ TResolvedBinding TDescriptorState::ResolveBinding(
};
}

void TDescriptor::Changed(bool schema) {
if (schema) {
++SchemaGeneration_;
}
void TDescriptor::Changed() {
Generation_.fetch_add(1, std::memory_order_relaxed);
}

Expand Down Expand Up @@ -182,22 +187,23 @@ SQLRETURN TDescriptor::GetDescField(SQLSMALLINT recNumber, SQLSMALLINT field, SQ
case SQL_DESC_TYPE_NAME:
case SQL_DESC_LOCAL_TYPE_NAME:
return Diag::WriteString<Diag::EStringWriteMode::Descriptor>(
this, TypeName(record->Type), value, bufferLength, lengthPtr);
this, GetSqlTypeName(record->Type, record->Type == SQL_GUID ? "GUID" : ""),
value, bufferLength, lengthPtr);
case SQL_DESC_LITERAL_PREFIX:
case SQL_DESC_LITERAL_SUFFIX:
return Diag::WriteString<Diag::EStringWriteMode::Descriptor>(this,
IsCharacter(record->Type) || DateTimeCode(record->Type) ? "'" : "",
IsCharacterType(record->Type) || GetDateTimeCode(record->Type) ? "'" : "",
value, bufferLength, lengthPtr);
case SQL_DESC_TYPE:
return WriteScalar(value, static_cast<SQLSMALLINT>(
DateTimeCode(record->Type) ? SQL_DATETIME : record->Type));
GetDateTimeCode(record->Type) ? SQL_DATETIME : record->Type));
case SQL_DESC_DATETIME_INTERVAL_CODE:
return WriteScalar(value, DateTimeCode(record->Type));
return WriteScalar(value, GetDateTimeCode(record->Type));
case SQL_DESC_CASE_SENSITIVE:
return WriteScalar(value, static_cast<SQLSMALLINT>(IsCharacter(record->Type)));
return WriteScalar(value, static_cast<SQLSMALLINT>(IsCharacterType(record->Type)));
case SQL_DESC_FIXED_PREC_SCALE:
return WriteScalar(value, static_cast<SQLSMALLINT>(
record->Type == SQL_DECIMAL || record->Type == SQL_NUMERIC));
HasFixedPrecisionScale(record->Type)));
case SQL_DESC_SEARCHABLE:
return WriteScalar(value, static_cast<SQLSMALLINT>(SQL_SEARCHABLE));
case SQL_DESC_UNNAMED:
Expand Down Expand Up @@ -231,8 +237,8 @@ SQLRETURN TDescriptor::GetDescRec(SQLSMALLINT recNumber, SQLCHAR* name, SQLSMALL
nameLength = static_cast<SQLSMALLINT>(record->Name.size());
}
WriteIf(nameLengthPtr, nameLength);
WriteIf(typePtr, DateTimeCode(record->Type) ? SQL_DATETIME : record->Type);
WriteIf(subTypePtr, DateTimeCode(record->Type));
WriteIf(typePtr, GetDateTimeCode(record->Type) ? SQL_DATETIME : record->Type);
WriteIf(subTypePtr, GetDateTimeCode(record->Type));
WriteIf(lengthPtr, record->Length);
WriteIf(precisionPtr, record->Precision);
WriteIf(scalePtr, record->Scale);
Expand Down Expand Up @@ -261,13 +267,13 @@ SQLRETURN TDescriptor::SetDescField(SQLSMALLINT recNumber, SQLSMALLINT field, SQ
return AddError("HY024", 0, "Invalid SQL_DESC_ARRAY_SIZE value");
}
Header_.ArraySize = size;
Changed(false);
Changed();
return SQL_SUCCESS;
}
default: break;
}
if (THeaderProperties::Set(field, Header_, value)) {
Changed(false);
Changed();
return SQL_SUCCESS;
}

Expand Down
22 changes: 16 additions & 6 deletions odbc/src/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,27 @@ class TDescriptorState {

THeader Header_;
std::vector<TDescRecord> Records_;
uint64_t SchemaGeneration_ = 0;

bool HasSameParameterShape(const TDescriptorState& other, bool appParam) const noexcept;

public:
SQLULEN GetArraySize() const noexcept { return Header_.ArraySize; }
SQLUSMALLINT* GetArrayStatusPtr() const noexcept { return Header_.ArrayStatusPtr; }
SQLULEN* GetRowsProcessedPtr() const noexcept { return Header_.RowsProcessedPtr; }
uint64_t GetSchemaGeneration() const noexcept { return SchemaGeneration_; }
const TDescRecord* FindRecord(SQLSMALLINT number) const noexcept;
SQLSMALLINT GetRecordCount() const noexcept;
TResolvedBinding ResolveBinding(const TDescRecord& record, SQLULEN index, bool useOffset = true) const noexcept;
};

class TDescriptorSnapshot : public TDescriptorState {
friend class TDescriptor;
public:
void Reset() noexcept { Generation_ = 0; }

private:
uint64_t Generation_ = 0;
};

class TDescriptor : public THandle, private TDescriptorState {
using THeaderProperties = TScalarProperties<
TScalarProperty<SQL_DESC_ARRAY_SIZE, &THeader::ArraySize, true, false>,
Expand All @@ -94,8 +103,9 @@ class TDescriptor : public THandle, private TDescriptorState {
// Statement access locks this descriptor; binding snapshots are statement-owned.
void Clear();
uint64_t GetGeneration() const { return Generation_.load(std::memory_order_relaxed); }
void Snapshot(TDescriptorState& state, uint64_t& generation) const;
void SnapshotHeader(TDescriptorState& state, uint64_t& generation) const;
// Returns whether the SQL parameter type shape changed; APDs contribute their C type.
bool Snapshot(TDescriptorSnapshot& state, bool appParam = false) const;
void SnapshotHeader(TDescriptorSnapshot& state) const;

// ODBC entrypoints already hold the handle operation lock.
SQLRETURN GetDescField(SQLSMALLINT recNumber, SQLSMALLINT fieldIdentifier, SQLPOINTER value,
Expand Down Expand Up @@ -135,9 +145,9 @@ class TDescriptor : public THandle, private TDescriptorState {
}
}
void ClearRecords() noexcept { Records_.clear(); Changed(); }
void SnapshotUnlocked(TDescriptorState& state, uint64_t& generation) const;
bool SnapshotUnlocked(TDescriptorSnapshot& state, bool appParam = false) const;
SQLRETURN CopyDesc(TDescriptor& target);
void Changed(bool schema = true);
void Changed();

EDescType Type_;
std::atomic<uint64_t> Generation_ = 1;
Expand Down
1 change: 0 additions & 1 deletion odbc/src/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace NYdb {
namespace NOdbc {

TEnvironment::TEnvironment() : OdbcVersion_(SQL_OV_ODBC3) {}
TEnvironment::~TEnvironment() {}

SQLRETURN TEnvironment::SetAttribute(SQLINTEGER attribute, SQLPOINTER value, SQLINTEGER stringLength) {
switch (attribute) {
Expand Down
1 change: 0 additions & 1 deletion odbc/src/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class TEnvironment : public THandle {

public:
TEnvironment();
~TEnvironment();

SQLRETURN SetAttribute(SQLINTEGER attribute, SQLPOINTER value, SQLINTEGER stringLength);
SQLRETURN GetAttribute(SQLINTEGER attribute, SQLPOINTER value, SQLINTEGER bufferLength, SQLINTEGER* stringLengthPtr);
Expand Down
14 changes: 4 additions & 10 deletions odbc/src/metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,10 @@ SQLRETURN NMetadata::ColAttribute(TStatement* statement, SQLUSMALLINT columnNumb
return Diag::WriteString<Diag::EStringWriteMode::ColumnAttribute>(
statement, "", characterAttributePtr, bufferLength,
stringLengthAttributePtr);
case SQL_DESC_TYPE_NAME: {
const TSqlTypeSpec* spec = FindSqlTypeSpec(column.SqlType);
case SQL_DESC_TYPE_NAME:
return Diag::WriteString<Diag::EStringWriteMode::ColumnAttribute>(
statement, spec ? spec->Name : "UNKNOWN", characterAttributePtr,
statement, GetSqlTypeName(column.SqlType, "UNKNOWN"), characterAttributePtr,
bufferLength, stringLengthAttributePtr);
}
case SQL_DESC_TYPE:
case SQL_DESC_CONCISE_TYPE:
return WriteAttributeNumber(statement, column.SqlType, numericAttributePtr);
Expand All @@ -333,14 +331,10 @@ SQLRETURN NMetadata::ColAttribute(TStatement* statement, SQLUSMALLINT columnNumb
return WriteAttributeNumber(statement, SQL_FALSE, numericAttributePtr);
case SQL_DESC_CASE_SENSITIVE:
return WriteAttributeNumber(statement,
column.SqlType == SQL_CHAR || column.SqlType == SQL_VARCHAR
|| column.SqlType == SQL_LONGVARCHAR || column.SqlType == SQL_WCHAR
|| column.SqlType == SQL_WVARCHAR || column.SqlType == SQL_WLONGVARCHAR,
numericAttributePtr);
IsCharacterType(column.SqlType), numericAttributePtr);
case SQL_DESC_FIXED_PREC_SCALE:
return WriteAttributeNumber(statement,
column.SqlType == SQL_DECIMAL || column.SqlType == SQL_NUMERIC,
numericAttributePtr);
HasFixedPrecisionScale(column.SqlType), numericAttributePtr);
case SQL_DESC_SEARCHABLE:
return WriteAttributeNumber(statement, SQL_PRED_SEARCHABLE, numericAttributePtr);
case SQL_DESC_UPDATABLE:
Expand Down
Loading
Loading