diff --git a/odbc/CMakeLists.txt b/odbc/CMakeLists.txt index 123cae44c3..219ad2b282 100644 --- a/odbc/CMakeLists.txt +++ b/odbc/CMakeLists.txt @@ -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 diff --git a/odbc/src/connection.cpp b/odbc/src/connection.cpp index 04023ba877..3832bbc7d3 100644 --- a/odbc/src/connection.cpp +++ b/odbc/src/connection.cpp @@ -1,7 +1,7 @@ #include "connection.h" #include "statement.h" -#include "utils/escape.h" +#include "utils/param_rewrite.h" #include #include @@ -122,37 +122,6 @@ NQuery::TTransaction& TConnection::GetOrCreateTx(TStatement& statement) { return *Tx_; } -std::optional TConnection::GetClient() { - if (!Ydb_) { - return std::nullopt; - } - return Ydb_->QueryClient; -} - -std::optional TConnection::GetTableClient() { - if (!Ydb_) { - return std::nullopt; - } - return Ydb_->TableClient; -} - -std::optional 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()) { diff --git a/odbc/src/connection.h b/odbc/src/connection.h index 32f86c8c07..6cd1b6ee37 100644 --- a/odbc/src/connection.h +++ b/odbc/src/connection.h @@ -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 LockTransaction() { return std::unique_lock(TransactionMutex_); } - SQLRETURN Execute(TStatement& statement); void EndTranFromEnvironment(SQLSMALLINT completionType); - std::optional 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 GetTableClient(); - std::optional GetSchemeClient(); + NTable::TTableClient* GetTableClient() { return Ydb_ ? &Ydb_->TableClient : nullptr; } + NScheme::TSchemeClient* GetSchemeClient() { return Ydb_ ? &Ydb_->SchemeClient : nullptr; } SQLRETURN SetAutocommit(bool value); bool GetAutocommit() const; diff --git a/odbc/src/descriptor.cpp b/odbc/src/descriptor.cpp index bdc5a76b8b..53d2aa5ac0 100644 --- a/odbc/src/descriptor.cpp +++ b/odbc/src/descriptor.cpp @@ -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 SQLRETURN WriteScalar(SQLPOINTER value, T scalar) { *static_cast(value) = scalar; @@ -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(*this); - generation = GetGeneration(); +bool TDescriptor::SnapshotUnlocked(TDescriptorSnapshot& state, bool appParam) const { + const bool shapeChanged = !HasSameParameterShape(state, appParam); + static_cast(state) = static_cast(*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() { @@ -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); } @@ -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( - 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(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( - 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(IsCharacter(record->Type))); + return WriteScalar(value, static_cast(IsCharacterType(record->Type))); case SQL_DESC_FIXED_PREC_SCALE: return WriteScalar(value, static_cast( - record->Type == SQL_DECIMAL || record->Type == SQL_NUMERIC)); + HasFixedPrecisionScale(record->Type))); case SQL_DESC_SEARCHABLE: return WriteScalar(value, static_cast(SQL_SEARCHABLE)); case SQL_DESC_UNNAMED: @@ -231,8 +237,8 @@ SQLRETURN TDescriptor::GetDescRec(SQLSMALLINT recNumber, SQLCHAR* name, SQLSMALL nameLength = static_cast(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); @@ -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; } diff --git a/odbc/src/descriptor.h b/odbc/src/descriptor.h index 96e5269a39..243f4f08f3 100644 --- a/odbc/src/descriptor.h +++ b/odbc/src/descriptor.h @@ -65,18 +65,27 @@ class TDescriptorState { THeader Header_; std::vector 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, @@ -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, @@ -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 Generation_ = 1; diff --git a/odbc/src/environment.cpp b/odbc/src/environment.cpp index d85f8b8410..0f130c4749 100644 --- a/odbc/src/environment.cpp +++ b/odbc/src/environment.cpp @@ -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) { diff --git a/odbc/src/environment.h b/odbc/src/environment.h index 8cb6a192e7..c68765d10e 100644 --- a/odbc/src/environment.h +++ b/odbc/src/environment.h @@ -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); diff --git a/odbc/src/metadata.cpp b/odbc/src/metadata.cpp index 29ff92fd23..8528595085 100644 --- a/odbc/src/metadata.cpp +++ b/odbc/src/metadata.cpp @@ -302,12 +302,10 @@ SQLRETURN NMetadata::ColAttribute(TStatement* statement, SQLUSMALLINT columnNumb return Diag::WriteString( statement, "", characterAttributePtr, bufferLength, stringLengthAttributePtr); - case SQL_DESC_TYPE_NAME: { - const TSqlTypeSpec* spec = FindSqlTypeSpec(column.SqlType); + case SQL_DESC_TYPE_NAME: return Diag::WriteString( - 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); @@ -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: diff --git a/odbc/src/statement.cpp b/odbc/src/statement.cpp index e5c63e3630..0875873bb0 100644 --- a/odbc/src/statement.cpp +++ b/odbc/src/statement.cpp @@ -4,7 +4,6 @@ #include "utils/attr.h" #include "utils/diag.h" #include "utils/param_rewrite.h" -#include "utils/status_util.h" #include #include @@ -102,9 +101,9 @@ void TStatement::SetAppDescriptor(SQLINTEGER attr, TDescriptor* descriptor) { if (current != descriptor) { current = descriptor; if (attr == SQL_ATTR_APP_ROW_DESC) { - RowGeneration_ = 0; + RowBindings_.Reset(); } else { - ParamGeneration_ = 0; + ParamBindings_.Reset(); AtExecValues_.clear(); InvalidatePreparedColumnMeta(); } @@ -112,65 +111,51 @@ void TStatement::SetAppDescriptor(SQLINTEGER attr, TDescriptor* descriptor) { } void TStatement::RefreshRowBindings() { - if (GetAppRowDesc().GetGeneration() != RowGeneration_) { - GetAppRowDesc().Snapshot(RowBindings_, RowGeneration_); - } - if (ImpRowDesc_.GetGeneration() != ImpRowGeneration_) { - ImpRowDesc_.SnapshotHeader(ImpRowBindings_, ImpRowGeneration_); - } + GetAppRowDesc().Snapshot(RowBindings_); + ImpRowDesc_.SnapshotHeader(ImpRowBindings_); } void TStatement::RefreshParamBindings() { - const auto paramSchema = ParamBindings_.GetSchemaGeneration(); - const auto impSchema = ImpParamBindings_.GetSchemaGeneration(); - if (GetAppParamDesc().GetGeneration() != ParamGeneration_) { - GetAppParamDesc().Snapshot(ParamBindings_, ParamGeneration_); - } - if (ImpParamDesc_.GetGeneration() != ImpParamGeneration_) { - ImpParamDesc_.Snapshot(ImpParamBindings_, ImpParamGeneration_); - } - if (paramSchema != ParamBindings_.GetSchemaGeneration() - || impSchema != ImpParamBindings_.GetSchemaGeneration()) { + const bool appChanged = GetAppParamDesc().Snapshot(ParamBindings_, true); + const bool impChanged = ImpParamDesc_.Snapshot(ImpParamBindings_); + if (appChanged || impChanged) { InvalidatePreparedColumnMeta(); } } SQLRETURN TStatement::Prepare(const std::string& statementText) { - IsPrepared_ = false; - ParamCount_ = 0; - PreparedQuery_.clear(); + Query_.reset(); RowCount_ = -1; PreparedColumnMeta_.reset(); ClearResults(); - PreparedQuery_ = GetConnection().PrepareSql(statementText); - ParamCount_ = CountOdbcParams(PreparedQuery_); + TPreparedQuery query(GetConnection().PrepareSql(statementText)); ImpParamDesc_.WithLock([&](TDescriptor& desc) { - while (desc.GetRecordCount() > ParamCount_) { + while (desc.GetRecordCount() > query.ParameterCount()) { desc.RemoveRecord(desc.GetRecordCount()); } - for (SQLUSMALLINT i = 1; i <= ParamCount_; ++i) { + for (SQLUSMALLINT i = 1; i <= query.ParameterCount(); ++i) { desc.RemoveInferredRecord(i); desc.Record(i).Nullable = SQL_NULLABLE_UNKNOWN; desc.Record(i).WideTemporalType.reset(); } }); - IsPrepared_ = true; + Query_.emplace(std::move(query)); return SQL_SUCCESS; } SQLRETURN TStatement::Execute() { - if (!IsPrepared_ || PreparedQuery_.empty()) { + if (!Query_ || Query_->Text().empty()) { throw TOdbcException("HY007", 0, "No prepared statement"); } RefreshParamBindings(); - for (SQLUSMALLINT number = 1; number <= ParamCount_; ++number) { + for (SQLUSMALLINT number = 1; number <= ParamCount(); ++number) { const auto* record = ImpParamBindings_.FindRecord(number); if (record && !record->WideTemporalType && (record->Type == SQL_TYPE_DATE || record->Type == SQL_TYPE_TIMESTAMP)) { DescribeParam(number, nullptr, nullptr, nullptr, nullptr); } } - const SQLULEN paramsetSize = ParamCount_ > 0 ? ParamBindings_.GetArraySize() : 1; + const SQLULEN paramsetSize = ParamCount() > 0 ? ParamBindings_.GetArraySize() : 1; ClearResults(); RowCount_ = -1; if (auto* processed = ImpParamBindings_.GetRowsProcessedPtr()) { @@ -180,7 +165,7 @@ SQLRETURN TStatement::Execute() { std::fill_n(statuses, paramsetSize, SQL_PARAM_UNUSED); } if (paramsetSize > 1 - && !StartsWithSqlStatement(PreparedQuery_, {"SELECT", "INSERT", "UPDATE", "DELETE", "UPSERT", "REPLACE"})) { + && !Query_->StartsWith({"SELECT", "INSERT", "UPDATE", "DELETE", "UPSERT", "REPLACE"})) { return AddError("HYC00", 0, "Parameter arrays are supported only for SELECT and data-modification statements"); } for (SQLULEN paramSet = 0; paramSet < paramsetSize; ++paramSet) { @@ -195,10 +180,11 @@ SQLRETURN TStatement::Execute() { } InAtExec_ = false; NeedDataParam_ = 0; - return GetConnection().Execute(*this); + return ExecuteInternal(); } SQLRETURN TStatement::ExecuteInternal() { + std::unique_lock transaction; bool completed = false; Y_DEFER { if (!completed) { @@ -210,10 +196,13 @@ SQLRETURN TStatement::ExecuteInternal() { } } }; + if (!GetConnection().GetAutocommit()) { + transaction = GetConnection().LockTransaction(); + } RowCount_ = 0; bool hasSuccessfulParamSet = false; bool rowCountUsable = true; - const SQLULEN paramsetSize = ParamCount_ > 0 ? ParamBindings_.GetArraySize() : 1; + const SQLULEN paramsetSize = ParamCount() > 0 ? ParamBindings_.GetArraySize() : 1; SQLUSMALLINT* const operations = ParamBindings_.GetArrayStatusPtr(); SQLUSMALLINT* const statuses = ImpParamBindings_.GetArrayStatusPtr(); SQLULEN* const processed = ImpParamBindings_.GetRowsProcessedPtr(); @@ -290,22 +279,38 @@ SQLRETURN TStatement::ExecuteParamSet( if (!client) { throw TOdbcException("HY000", 0, "No client connection"); } + const auto activeParams = GetBoundParams(paramSet); NYdb::TParams params = NYdb::TParamsBuilder().Build(); - const SQLRETURN buildRc = BuildParams(params, paramSet); + const SQLRETURN buildRc = BuildParams(params, paramSet, activeParams); if (buildRc != SQL_SUCCESS) { return buildRc; } + const auto rewritten = Query_->Bind(activeParams, Attributes_.GetNoScanMode() != SQL_NOSCAN_ON); + if (!rewritten.Success) { + throw TOdbcException(rewritten.SqlState, 0, rewritten.Message); + } + const auto txSettings = GetConnection().MakeTxSettings(); + const bool noTx = txSettings.GetMode() == NQuery::TTxSettings::TS_SNAPSHOT_RW + || Query_->StartsWith({"CREATE", "DROP", "ALTER", "GRANT", "REVOKE"}); + const auto control = GetConnection().GetAutocommit() + ? (noTx ? NQuery::TTxControl::NoTx() : NQuery::TTxControl::BeginTx(txSettings).CommitTx()) + : NQuery::TTxControl::Tx(GetConnection().GetOrCreateTx(*this)).CommitTx(false); + TExecutionInput input{GetConnection().WrapQueryForCurrentCatalog(rewritten.Sql), std::move(params), {}, control}; + input.Settings.StatsMode(NQuery::EStatsMode::Basic).CollectAffectedRows(true); + if (const auto timeout = Attributes_.GetQueryTimeoutSec()) { + input.Settings.ClientTimeout(TDuration::Seconds(timeout)); + } if (GetConnection().GetAutocommit()) { const NYdb::NRetry::TRetryOperationSettings retrySettings = MakeAutocommitRetrySettings(); const NYdb::TStatus execStatus = client->RetryQuerySync( - [this, ¶ms, &affectedRows, paramSet](NQuery::TSession session) -> NYdb::TStatus { + [this, &input, &affectedRows](NQuery::TSession session) -> NYdb::TStatus { CheckCancellation(); - NQuery::TExecuteQueryResult result = ExecuteQuery(session, params, paramSet); + NQuery::TExecuteQueryResult result = ExecuteQuery(session, input); CheckExecutionStatus(result); if (!result.IsSuccess()) { - return StatusFrom(result); + return NYdb::TStatus(result); } affectedRows = ExtractAffectedRows(result); if (!IsCancelRequested()) { @@ -317,9 +322,9 @@ SQLRETURN TStatement::ExecuteParamSet( NStatusHelpers::ThrowOnError(execStatus); } else { - auto session = GetConnection().GetOrCreateTx(*this).GetSession(); + auto session = GetConnection().GetTx()->GetSession(); CheckCancellation(); - NQuery::TExecuteQueryResult result = ExecuteQuery(session, params, paramSet); + NQuery::TExecuteQueryResult result = ExecuteQuery(session, input); NStatusHelpers::ThrowOnError(result); affectedRows = ExtractAffectedRows(result); if (!IsCancelRequested()) { @@ -350,10 +355,10 @@ SQLRETURN TStatement::MoreResults() { SQLUSMALLINT TStatement::FindNextNeedDataParam(SQLULEN paramSet) const { if (const auto* operations = ParamBindings_.GetArrayStatusPtr(); - ParamCount_ > 0 && operations && operations[paramSet] != SQL_PARAM_PROCEED) { + ParamCount() > 0 && operations && operations[paramSet] != SQL_PARAM_PROCEED) { return 0; } - for (SQLUSMALLINT i = 1; i <= ParamCount_; ++i) { + for (SQLUSMALLINT i = 1; i <= ParamCount(); ++i) { const auto* record = ParamBindings_.FindRecord(i); if (record && ImpParamBindings_.FindRecord(i) && ParamBindings_.ResolveBinding(*record, paramSet).IsDataAtExec() && (AtExecValues_.size() <= i || !AtExecValues_[i].Complete)) { @@ -375,53 +380,10 @@ NYdb::NRetry::TRetryOperationSettings TStatement::MakeAutocommitRetrySettings() } NQuery::TExecuteQueryResult TStatement::ExecuteQuery( - NQuery::TSession& session, - const NYdb::TParams& params, - SQLULEN paramSet) -{ - const std::vector activeParams = GetBoundParams(paramSet); - const TParamRewriteResult rewritten = RewriteOdbcSql( - PreparedQuery_, activeParams, Attributes_.GetNoScanMode() != SQL_NOSCAN_ON); - if (!rewritten.Success) { - throw TOdbcException(rewritten.SqlState, 0, rewritten.Message); - } - const bool isDdl = StartsWithSqlStatement( - rewritten.Sql, {"CREATE", "DROP", "ALTER", "GRANT", "REVOKE"}); - const std::string queryText = GetConnection().WrapQueryForCurrentCatalog(rewritten.Sql); - NQuery::TExecuteQuerySettings execSettings; - execSettings.StatsMode(NQuery::EStatsMode::Basic).CollectAffectedRows(true); - const SQLUINTEGER queryTimeoutSec = Attributes_.GetQueryTimeoutSec(); - if (queryTimeoutSec > 0) { - execSettings.ClientTimeout(TDuration::Seconds(queryTimeoutSec)); - } - const auto txSettings = GetConnection().MakeTxSettings(); - if (GetConnection().GetAutocommit()) { - // TS_SNAPSHOT_RW doesn't support explicit BeginTx() - we use NoTx() instead - // DDL must use NoTx() per YDB documentation - const bool isSnapshotRw = (txSettings.GetMode() == NQuery::TTxSettings::TS_SNAPSHOT_RW); - - if (isSnapshotRw || isDdl) { - return session.ExecuteQuery( - queryText, - NQuery::TTxControl::NoTx(), - params, - execSettings).ExtractValueSync(); - } - return session.ExecuteQuery( - queryText, - NQuery::TTxControl::BeginTx(txSettings).CommitTx(), - params, - execSettings).ExtractValueSync(); - } - return session.ExecuteQuery( - queryText, - NQuery::TTxControl::Tx(*GetConnection().GetTx()).CommitTx(false), - params, - execSettings).ExtractValueSync(); + NQuery::TSession& session, const TExecutionInput& input) { + return session.ExecuteQuery(input.Sql, input.TxControl, input.Params, input.Settings).ExtractValueSync(); } - - SQLRETURN TStatement::Fetch() { return FetchScroll(SQL_FETCH_NEXT, 0); } @@ -647,12 +609,18 @@ SQLRETURN TStatement::BindParameter(SQLUSMALLINT paramNumber, if (parameterType == SQL_UNKNOWN_TYPE) { throw TOdbcException("HY004", 0, "Invalid SQL data type"); } - GetAppParamDesc().WithLock(ImpParamDesc_, [&](TDescriptor& appDesc, TDescriptor& impDesc) { + const bool shapeChanged = GetAppParamDesc().WithLock(ImpParamDesc_, [&](TDescriptor& appDesc, TDescriptor& impDesc) { + const auto* oldApp = appDesc.FindRecord(static_cast(paramNumber)); + const auto* oldImp = impDesc.FindRecord(static_cast(paramNumber)); if (!parameterValuePtr && !strLenOrIndPtr) { + const bool changed = oldApp || oldImp; appDesc.RemoveRecord(static_cast(paramNumber)); impDesc.RemoveRecord(static_cast(paramNumber)); - return; + return changed; } + const bool changed = !oldApp || oldApp->Type != valueType + || !oldImp || oldImp->Type != parameterType + || oldImp->Length != static_cast(columnSize) || oldImp->Scale != decimalDigits; TDescRecord& app = appDesc.Record(static_cast(paramNumber)); app.Type = valueType; app.Length = bufferLength; @@ -671,6 +639,7 @@ SQLRETURN TStatement::BindParameter(SQLUSMALLINT paramNumber, imp.Scale = decimalDigits; imp.Nullable = SQL_NULLABLE; imp.ParameterType = inputOutputType; + return changed; }); if (!parameterValuePtr && !strLenOrIndPtr) { if (AtExecValues_.size() > paramNumber) { @@ -683,13 +652,15 @@ SQLRETURN TStatement::BindParameter(SQLUSMALLINT paramNumber, AtExecValues_[paramNumber] = {}; } - InvalidatePreparedColumnMeta(); + if (shapeChanged) { + InvalidatePreparedColumnMeta(); + } return SQL_SUCCESS; } std::vector TStatement::GetBoundParams(SQLULEN paramSet) const { std::vector params; - for (SQLUSMALLINT number = 1; number <= ParamCount_; ++number) { + for (SQLUSMALLINT number = 1; number <= ParamCount(); ++number) { const TDescRecord* app = ParamBindings_.FindRecord(number); const TDescRecord* imp = ImpParamBindings_.FindRecord(number); if (!app || !imp) { @@ -716,7 +687,8 @@ std::vector TStatement::GetBoundParams(SQLULEN paramSet) const { return params; } -SQLRETURN TStatement::BuildParams(NYdb::TParams& out, SQLULEN paramSet) { +SQLRETURN TStatement::BuildParams(NYdb::TParams& out, SQLULEN paramSet, + const std::vector& params) { ClearErrors(); NYdb::TParamsBuilder paramsBuilder; const auto conversionError = [&](const TBoundParam& param) { @@ -729,34 +701,22 @@ SQLRETURN TStatement::BuildParams(NYdb::TParams& out, SQLULEN paramSet) { + " (C type " + std::to_string(static_cast(param.ValueType)) + ", SQL type " + std::to_string(static_cast(param.ParameterType)) + ")"); }; - for (const TBoundParam& param : GetBoundParams(paramSet)) { - const std::string paramName = "$p" + std::to_string(param.ParamNumber); + for (TBoundParam param : params) { + SQLLEN indicator = 0; if (param.AtExec) { if (AtExecValues_.size() <= param.ParamNumber || !AtExecValues_[param.ParamNumber].Complete) { return AddError("HY000", 0, "Missing data-at-execution parameter value"); } - TAtExecValue& value = AtExecValues_[param.ParamNumber]; - SQLLEN indicator = value.Indicator == SQL_NULL_DATA - ? SQL_NULL_DATA - : static_cast(value.Data.size()); - TBoundParam tmp = param; - tmp.ParameterValuePtr = value.Data.data(); - tmp.StrLenOrIndPtr = &indicator; - const bool optional = BoundParamIsNull(tmp) - || GetDeclaredParamOptionality(PreparedQuery_, param.ParamNumber).value_or(false); - const SQLRETURN convRc = ConvertParam( - tmp, paramsBuilder.AddParam(paramName), optional); - if (convRc != SQL_SUCCESS) { - return conversionError(param); - } - continue; + auto& value = AtExecValues_[param.ParamNumber]; + indicator = value.Indicator == SQL_NULL_DATA ? SQL_NULL_DATA : static_cast(value.Data.size()); + param.ParameterValuePtr = value.Data.data(); + param.StrLenOrIndPtr = &indicator; } const bool optional = BoundParamIsNull(param) - || GetDeclaredParamOptionality(PreparedQuery_, param.ParamNumber).value_or(false); - const SQLRETURN convRc = ConvertParam( - param, paramsBuilder.AddParam(paramName), optional); - if (convRc != SQL_SUCCESS) { + || Query_->DeclaredOptionality(param.ParamNumber).value_or(false); + if (ConvertParam(param, paramsBuilder.AddParam("$p" + std::to_string(param.ParamNumber)), optional) + != SQL_SUCCESS) { return conversionError(param); } } @@ -769,29 +729,27 @@ SQLRETURN TStatement::NumParams(SQLSMALLINT* paramCount) { if (!paramCount) { throw TOdbcException("HY000", 0, "Invalid parameter"); } - if (!IsPrepared_) { + if (!Query_) { throw TOdbcException("HY010", 0, "Function sequence error"); } - *paramCount = ParamCount_; + *paramCount = ParamCount(); return SQL_SUCCESS; } void TStatement::ResetForMetadata() { ClearErrors(); RowCount_ = -1; - PreparedQuery_.clear(); - IsPrepared_ = false; - ParamCount_ = 0; + Query_.reset(); PreparedColumnMeta_.reset(); ClearResults(); } SQLRETURN TStatement::DescribeParam(SQLUSMALLINT paramNumber, SQLSMALLINT* dataTypePtr, SQLULEN* paramSizePtr, SQLSMALLINT* decimalDigitsPtr, SQLSMALLINT* nullablePtr) { - if (!IsPrepared_) { + if (!Query_) { throw TOdbcException("HY010", 0, "Function sequence error"); } - if (paramNumber < 1 || paramNumber > ParamCount_) { + if (paramNumber < 1 || paramNumber > ParamCount()) { throw TOdbcException("07009", 0, "Invalid descriptor index"); } RefreshParamBindings(); @@ -799,16 +757,17 @@ SQLRETURN TStatement::DescribeParam(SQLUSMALLINT paramNumber, SQLSMALLINT* dataT const bool temporal = record && !record->WideTemporalType && (record->Type == SQL_TYPE_DATE || record->Type == SQL_TYPE_TIMESTAMP); if (!record || (record->Type == SQL_UNKNOWN_TYPE && !record->Inferred) || temporal) { + bool shapeChanged = false; if (const auto query = BuildParamMetadataQuery( - PreparedQuery_, Attributes_.GetNoScanMode() != SQL_NOSCAN_ON, paramNumber, ParamCount_)) { + Query_->Text(), Attributes_.GetNoScanMode() != SQL_NOSCAN_ON, paramNumber, ParamCount())) { auto columns = ReadQueryMetadata(*query, true); // Column nullability constrains insert targets, not predicate parameters. - if (!StartsWithSqlStatement(PreparedQuery_, {"INSERT", "UPSERT", "REPLACE"})) { + if (!Query_->StartsWith({"INSERT", "UPSERT", "REPLACE"})) { for (auto& column : columns) { column.Nullable = SQL_NULLABLE_UNKNOWN; } } - ImpParamDesc_.WithLock([&](TDescriptor& desc) { + shapeChanged = ImpParamDesc_.WithLock([&](TDescriptor& desc) { for (size_t index = 0; index < columns.size(); ++index) { const auto i = static_cast(index + 1); const auto& column = columns[index]; @@ -824,14 +783,17 @@ SQLRETURN TStatement::DescribeParam(SQLUSMALLINT paramNumber, SQLSMALLINT* dataT parameter.Nullable = column.Nullable; parameter.Inferred = true; } - desc.SnapshotUnlocked(ImpParamBindings_, ImpParamGeneration_); + return desc.SnapshotUnlocked(ImpParamBindings_); }); } else if (temporal) { - ImpParamDesc_.WithLock([&](TDescriptor& desc) { + shapeChanged = ImpParamDesc_.WithLock([&](TDescriptor& desc) { desc.Record(paramNumber).WideTemporalType = EWideTemporal::None; - desc.SnapshotUnlocked(ImpParamBindings_, ImpParamGeneration_); + return desc.SnapshotUnlocked(ImpParamBindings_); }); } + if (shapeChanged) { + InvalidatePreparedColumnMeta(); + } record = ImpParamBindings_.FindRecord(static_cast(paramNumber)); } if (!record || record->Type == SQL_UNKNOWN_TYPE) { @@ -859,6 +821,9 @@ SQLRETURN TStatement::ParamData(SQLPOINTER* valuePtr) { if (!InAtExec_) { return SQL_NO_DATA; } + if (!Query_) { + throw TOdbcException("HY010", 0, "Function sequence error"); + } RefreshParamBindings(); if (NeedDataParam_ != 0) { if (AtExecValues_.size() <= NeedDataParam_) { @@ -875,7 +840,7 @@ SQLRETURN TStatement::ParamData(SQLPOINTER* valuePtr) { } InAtExec_ = false; NeedDataParam_ = 0; - return GetConnection().Execute(*this); + return ExecuteInternal(); } SQLRETURN TStatement::PutData(SQLPOINTER data, SQLLEN strLenOrInd) { @@ -1032,8 +997,8 @@ void TStatement::ResetParams() { GetAppParamDesc().WithLock(ImpParamDesc_, [&](TDescriptor& app, TDescriptor& imp) { app.ClearRecords(); imp.ClearRecords(); - app.SnapshotUnlocked(ParamBindings_, ParamGeneration_); - imp.SnapshotUnlocked(ImpParamBindings_, ImpParamGeneration_); + app.SnapshotUnlocked(ParamBindings_, true); + imp.SnapshotUnlocked(ImpParamBindings_); }); AtExecValues_.clear(); } @@ -1069,18 +1034,17 @@ void TStatement::EnsurePreparedColumnMeta() { if (PreparedColumnMeta_) { return; } - if (!IsPrepared_) { + if (!Query_) { throw TOdbcException("HY010", 0, "Function sequence error"); } - if (HasMultipleSqlStatements(PreparedQuery_)) { + if (Query_->HasMultipleStatements()) { throw TOdbcException( "HYC00", 0, "Result metadata before execution is unavailable for multiple statements"); } - if (!StartsWithSqlStatement(PreparedQuery_, {"SELECT"})) { - if (StartsWithSqlStatement( - PreparedQuery_, + if (!Query_->StartsWith({"SELECT"})) { + if (Query_->StartsWith( {"INSERT", "UPDATE", "DELETE", "UPSERT", "REPLACE", "MERGE", "CREATE", "DROP", "ALTER", "GRANT", "REVOKE", "COMMIT", "ROLLBACK"})) { PreparedColumnMeta_.emplace(); @@ -1095,11 +1059,11 @@ void TStatement::EnsurePreparedColumnMeta() { // Use descriptor types and typed NULLs, never application parameter values. std::vector activeParams; NYdb::TParamsBuilder builder; - for (SQLUSMALLINT number = 1; number <= ParamCount_; ++number) { + for (SQLUSMALLINT number = 1; number <= ParamCount(); ++number) { const auto* imp = ImpParamBindings_.FindRecord(number); const auto* app = ParamBindings_.FindRecord(number); if (!imp || imp->Type == SQL_UNKNOWN_TYPE - || GetDeclaredParamOptionality(PreparedQuery_, number) == false) { + || Query_->DeclaredOptionality(number) == false) { throw TOdbcException("HYC00", 0, "Result metadata requires known, nullable parameter types"); } activeParams.push_back({static_cast(number), @@ -1112,7 +1076,7 @@ void TStatement::EnsurePreparedColumnMeta() { } const TParamRewriteResult rewritten = RewriteOdbcSql( - BuildResultMetadataQuery(PreparedQuery_), activeParams, + BuildResultMetadataQuery(Query_->Text()), activeParams, Attributes_.GetNoScanMode() != SQL_NOSCAN_ON); if (!rewritten.Success) { throw TOdbcException(rewritten.SqlState, 0, rewritten.Message); @@ -1156,7 +1120,7 @@ std::vector TStatement::ReadQueryMetadata( execSettings).ExtractValueSync(); CheckExecutionStatus(result); if (!result.IsSuccess()) { - return StatusFrom(result); + return NYdb::TStatus(result); } if (result.GetResultSets().size() != 1) { return NYdb::TStatus( @@ -1231,7 +1195,7 @@ void TStatement::SetResults(const NQuery::TExecuteQueryResult& result) { } auto cursor = sets.empty() ? nullptr : CreateExecCursor( sets.front(), Attributes_.CursorType == SQL_CURSOR_STATIC); - if (cursor && IsPrepared_) { + if (cursor && Query_) { PreparedColumnMeta_ = cursor->GetColumnMeta(); } if (!sets.empty()) { diff --git a/odbc/src/statement.h b/odbc/src/statement.h index 320b0b4f3d..7e64a56dd8 100644 --- a/odbc/src/statement.h +++ b/odbc/src/statement.h @@ -5,6 +5,7 @@ #include "utils/attr.h" #include "utils/bindings.h" #include "utils/cursor.h" +#include "utils/param_rewrite.h" #include @@ -138,9 +139,7 @@ class TStatement : public THandle { std::unique_ptr Cursor_; std::deque RemainingResultSets_; std::optional> PreparedColumnMeta_; - std::string PreparedQuery_; - bool IsPrepared_ = false; - SQLSMALLINT ParamCount_ = 0; + std::optional Query_; SQLLEN RowCount_ = -1; TAttributes Attributes_; @@ -151,14 +150,10 @@ class TStatement : public THandle { TDescriptor ImpParamDesc_; TDescriptor* AppRowDescPtr_ = nullptr; TDescriptor* AppParamDescPtr_ = nullptr; - TDescriptorState RowBindings_; - TDescriptorState ParamBindings_; - TDescriptorState ImpParamBindings_; - TDescriptorState ImpRowBindings_; - uint64_t RowGeneration_ = 0; - uint64_t ParamGeneration_ = 0; - uint64_t ImpParamGeneration_ = 0; - uint64_t ImpRowGeneration_ = 0; + TDescriptorSnapshot RowBindings_; + TDescriptorSnapshot ParamBindings_; + TDescriptorSnapshot ImpParamBindings_; + TDescriptorSnapshot ImpRowBindings_; std::atomic ExecutionState_ = EExecutionState::Idle; SQLUSMALLINT NeedDataParam_ = 0; bool InAtExec_ = false; @@ -166,7 +161,8 @@ class TStatement : public THandle { SQLUSMALLINT GetDataColumn_ = 0; SQLLEN GetDataOffset_ = 0; - SQLRETURN BuildParams(NYdb::TParams& out, SQLULEN paramSet); + SQLSMALLINT ParamCount() const { return Query_ ? Query_->ParameterCount() : 0; } + SQLRETURN BuildParams(NYdb::TParams& out, SQLULEN paramSet, const std::vector& params); SQLRETURN ExecuteParamSet(SQLULEN paramSet, std::optional& affectedRows); SQLRETURN ExecuteInternal(); SQLRETURN FillBoundColumns(SQLULEN row, bool useOffset); @@ -208,10 +204,15 @@ class TStatement : public THandle { bool schemaPatternsAllowed) const; protected: + struct TExecutionInput { + std::string Sql; + NYdb::TParams Params; + NQuery::TExecuteQuerySettings Settings; + NQuery::TTxControl TxControl; + }; virtual NQuery::TExecuteQueryResult ExecuteQuery( NQuery::TSession& session, - const NYdb::TParams& params, - SQLULEN paramSet); + const TExecutionInput& input); private: NYdb::NRetry::TRetryOperationSettings MakeAutocommitRetrySettings(); diff --git a/odbc/src/statement_metadata.cpp b/odbc/src/statement_metadata.cpp index b9b33b15ed..3e8679432c 100644 --- a/odbc/src/statement_metadata.cpp +++ b/odbc/src/statement_metadata.cpp @@ -204,21 +204,13 @@ bool IsSpecialValue(const TMetadataArgument& value, std::string_view special) { return value && *value == special; } -bool IsDateTimeType(SQLSMALLINT type) { - return type == SQL_TYPE_DATE || type == SQL_TYPE_TIME || type == SQL_TYPE_TIMESTAMP; -} - TOdbcScalar GetSqlDataType(SQLSMALLINT type) { - return I(IsDateTimeType(type) ? SQL_DATETIME : type); + return I(GetDateTimeCode(type) ? SQL_DATETIME : type); } TOdbcScalar GetDateTimeSub(SQLSMALLINT type) { - switch (type) { - case SQL_TYPE_DATE: return I(SQL_CODE_DATE); - case SQL_TYPE_TIME: return I(SQL_CODE_TIME); - case SQL_TYPE_TIMESTAMP: return I(SQL_CODE_TIMESTAMP); - default: return Null(); - } + const SQLSMALLINT code = GetDateTimeCode(type); + return code ? I(code) : Null(); } TOdbcScalar GetColumnSize(const TYdbTypeInfo& type) { @@ -334,7 +326,7 @@ TTable BuildTypeInfoRows(SQLSMALLINT dataType) { : spec.Type == SQL_TYPE_TIME ? TOdbcScalar("{t '") : spec.Type == SQL_TYPE_TIMESTAMP ? TOdbcScalar("{ts '") : text ? TOdbcScalar("'") : Null(); - const TOdbcScalar literalSuffix = IsDateTimeType(spec.Type) ? TOdbcScalar("'}") + const TOdbcScalar literalSuffix = GetDateTimeCode(spec.Type) ? TOdbcScalar("'}") : text ? TOdbcScalar("'") : Null(); table.push_back({ typeName, I(spec.Type), I(decimal ? 35 : static_cast(spec.ColumnSize)), diff --git a/odbc/src/utils/cursor.cpp b/odbc/src/utils/cursor.cpp index 2f32df31ad..ea0334e8d6 100644 --- a/odbc/src/utils/cursor.cpp +++ b/odbc/src/utils/cursor.cpp @@ -193,122 +193,6 @@ TCursorWindow::TCursorWindow(size_t totalRows) : TotalRows_(totalRows) {} -class TCursorWindow::TPositionResolver { -public: - struct TTarget { - EPosition Position; - size_t Start = 0; - bool OverlappedStart = false; - }; - - TPositionResolver( - const TCursorWindow& window, - size_t rowsetSize, - SQLULEN requestedRowsetSize, - size_t visibleRows) - : Window_(window) - , RowsetSize_(rowsetSize) - , RequestedRowsetSize_(static_cast(requestedRowsetSize)) - , VisibleRows_(visibleRows) - {} - - TTarget Resolve(SQLSMALLINT orientation, SQLLEN offset) const { - switch (orientation) { - case SQL_FETCH_NEXT: { - if (Window_.Position_ == EPosition::Before) { - return Rowset(0); - } - return Window_.Position_ == EPosition::After - ? Boundary(EPosition::After) - : Forward(Window_.Start_, Window_.PreviousRowsetSize_); - } - case SQL_FETCH_PRIOR: { - if (Window_.Position_ == EPosition::Before) { - return Boundary(EPosition::Before); - } - const size_t origin = Window_.Position_ == EPosition::After - ? VisibleRows_ - : Window_.Start_; - return Backward(origin, RowsetSize_, origin > 0); - } - case SQL_FETCH_FIRST: - return Rowset(0); - case SQL_FETCH_LAST: - return VisibleRows_ == 0 - ? Boundary(EPosition::After) - : Rowset(VisibleRows_ > RowsetSize_ - ? VisibleRows_ - RowsetSize_ - : 0); - case SQL_FETCH_ABSOLUTE: - return Absolute(offset); - case SQL_FETCH_RELATIVE: { - if (Window_.Position_ == EPosition::Before) { - return offset > 0 ? Absolute(offset) : Boundary(EPosition::Before); - } - if (Window_.Position_ == EPosition::After) { - return offset < 0 ? Absolute(offset) : Boundary(EPosition::After); - } - if (offset >= 0) { - return Forward(Window_.Start_, static_cast(offset)); - } - return Backward( - Window_.Start_, NegativeMagnitude(offset), Window_.Start_ > 0); - } - default: - return Boundary(Window_.Position_ == EPosition::After - ? EPosition::After - : EPosition::Before); - } - } - -private: - static TTarget Boundary(EPosition position) { - return {position}; - } - - static TTarget Rowset(size_t start, bool overlappedStart = false) { - return {EPosition::Rowset, start, overlappedStart}; - } - - TTarget Positive(uintmax_t start) const { - return start > std::numeric_limits::max() - ? Boundary(EPosition::After) - : Rowset(static_cast(start)); - } - - TTarget Forward(size_t origin, uintmax_t distance) const { - return distance > std::numeric_limits::max() - origin - ? Boundary(EPosition::After) - : Positive(static_cast(origin) + distance); - } - - TTarget Backward(size_t origin, uintmax_t distance, bool allowOverlap) const { - if (distance <= origin) { - return Rowset(origin - static_cast(distance)); - } - const uintmax_t rowsBeforeStart = distance - origin; - return allowOverlap && rowsBeforeStart < RequestedRowsetSize_ - ? Rowset(0, true) - : Boundary(EPosition::Before); - } - - TTarget Absolute(SQLLEN offset) const { - if (offset > 0) { - return Positive(static_cast(offset) - 1); - } - if (offset == 0) { - return Boundary(EPosition::Before); - } - return Backward( - VisibleRows_, NegativeMagnitude(offset), VisibleRows_ > 0); - } - - const TCursorWindow& Window_; - size_t RowsetSize_; - uintmax_t RequestedRowsetSize_; - size_t VisibleRows_; -}; - TFetchResult TCursorWindow::Fetch( SQLSMALLINT orientation, SQLLEN offset, @@ -324,24 +208,97 @@ TFetchResult TCursorWindow::Fetch( const size_t visibleRows = maxRows == 0 ? TotalRows_ : std::min(TotalRows_, ToSize(maxRows)); - const auto target = TPositionResolver(*this, rowset, rowsetSize, visibleRows) - .Resolve(orientation, offset); + EPosition position = EPosition::Rowset; + size_t start = 0; + bool overlappedStart = false; + const auto forward = [&](size_t origin, uintmax_t distance) { + if (distance > std::numeric_limits::max() - origin + || static_cast(origin) + distance > std::numeric_limits::max()) { + position = EPosition::After; + } else { + start = origin + static_cast(distance); + } + }; + const auto backward = [&](size_t origin, uintmax_t distance, bool allowOverlap) { + if (distance <= origin) { + start = origin - static_cast(distance); + } else if (allowOverlap && distance - origin < static_cast(rowsetSize)) { + overlappedStart = true; + } else { + position = EPosition::Before; + } + }; + const auto absolute = [&] { + if (offset > 0) { + forward(0, static_cast(offset) - 1); + } else if (offset == 0) { + position = EPosition::Before; + } else { + backward(visibleRows, NegativeMagnitude(offset), visibleRows > 0); + } + }; + + switch (orientation) { + case SQL_FETCH_NEXT: + if (Position_ == EPosition::After) { + position = EPosition::After; + } else if (Position_ == EPosition::Rowset) { + forward(Start_, PreviousRowsetSize_); + } + break; + case SQL_FETCH_PRIOR: + if (Position_ == EPosition::Before) { + position = EPosition::Before; + } else { + const size_t origin = Position_ == EPosition::After ? visibleRows : Start_; + backward(origin, rowset, origin > 0); + } + break; + case SQL_FETCH_FIRST: + break; + case SQL_FETCH_LAST: + if (visibleRows == 0) { + position = EPosition::After; + } else { + start = visibleRows > rowset ? visibleRows - rowset : 0; + } + break; + case SQL_FETCH_ABSOLUTE: + absolute(); + break; + case SQL_FETCH_RELATIVE: + if (Position_ != EPosition::Rowset) { + if ((Position_ == EPosition::Before && offset > 0) + || (Position_ == EPosition::After && offset < 0)) { + absolute(); + } else { + position = Position_; + } + } else if (offset >= 0) { + forward(Start_, static_cast(offset)); + } else { + backward(Start_, NegativeMagnitude(offset), Start_ > 0); + } + break; + default: + position = Position_ == EPosition::After ? EPosition::After : EPosition::Before; + } - if (target.Position != EPosition::Rowset) { - SetBoundary(target.Position); + if (position != EPosition::Rowset) { + SetBoundary(position); return {}; } - if (target.Start >= visibleRows - || target.Start > static_cast(std::numeric_limits::max())) { + if (start >= visibleRows + || start > static_cast(std::numeric_limits::max())) { SetBoundary(EPosition::After); return {}; } Position_ = EPosition::Rowset; - Start_ = target.Start; - Size_ = std::min(rowset, visibleRows - target.Start); + Start_ = start; + Size_ = std::min(rowset, visibleRows - start); PreviousRowsetSize_ = rowset; - return {static_cast(Size_), target.OverlappedStart}; + return {static_cast(Size_), overlappedStart}; } std::optional TCursorWindow::Resolve(SQLULEN row) const { diff --git a/odbc/src/utils/cursor_window.h b/odbc/src/utils/cursor_window.h index 14891cc421..f12c3ecfb3 100644 --- a/odbc/src/utils/cursor_window.h +++ b/odbc/src/utils/cursor_window.h @@ -31,8 +31,6 @@ class TCursorWindow { After, }; - class TPositionResolver; - void SetBoundary(EPosition position); size_t TotalRows_ = 0; diff --git a/odbc/src/utils/escape.cpp b/odbc/src/utils/escape.cpp index 158c0c8b04..f31adbe777 100644 --- a/odbc/src/utils/escape.cpp +++ b/odbc/src/utils/escape.cpp @@ -1,9 +1,8 @@ -#include "escape.h" +#include "param_rewrite.h" #include "sql_type_map.h" #include -#include #include #include #include @@ -40,7 +39,7 @@ struct TSqlScanner { size_t QuestionCount = 0; SQLSMALLINT MaxDollarIndex = 0; - std::bitset Indices; + std::vector Indices; void Append(std::string_view text) { if (Output_) { @@ -401,7 +400,7 @@ struct TSqlScanner { } const auto index = static_cast(QuestionCount); if (Output_) { - Indices.set(index); + Indices.push_back(index); Append("$p"); Append(std::to_string(index)); } @@ -412,7 +411,7 @@ struct TSqlScanner { if (parameters && ReadDollar(pos, end, index)) { MaxDollarIndex = std::max(MaxDollarIndex, static_cast(index)); if (Output_) { - Indices.set(index); + Indices.push_back(index); } } Append(Sql_[pos++]); @@ -421,46 +420,92 @@ struct TSqlScanner { }; -TParamRewriteResult RewriteSql( - std::string_view sql, - const std::vector& boundParams, - bool escapes) { - std::string body; - body.reserve(sql.size()); - TSqlScanner scanner{sql, &body, escapes}; - scanner.Scan(0, sql.size(), true); - if (scanner.Indices.none()) { - return {.Sql = std::move(body)}; +std::vector> ReadDeclarations(std::string_view sql) { + std::vector> result; + const TSqlScanner scanner{sql, nullptr, false}; + for (size_t pos = scanner.SkipSqlTrivia(0, sql.size()); pos < sql.size();) { + const bool declare = StartsWithKeyword(sql, pos, "DECLARE"); + if (!declare && !StartsWithKeyword(sql, pos, "PRAGMA")) break; + const size_t end = scanner.FindStatementEnd(pos, sql.size()); + if (end == std::string_view::npos) break; + size_t token = scanner.SkipSqlTrivia(pos + (declare ? 7 : 6), end); + SQLUSMALLINT number = 0; + if (declare && scanner.ReadDollar(token, end, number)) { + const std::string name = "$p" + std::to_string(number); + if (sql.substr(token, name.size()) == name) { + token = scanner.SkipSqlTrivia(token + name.size(), end); + if (StartsWithKeyword(sql, token, "AS")) { + const auto type = TrimTrailingSqlTrivia(sql.substr(token + 2, end - token - 2)); + if (result.size() <= number) result.resize(number + 1); + if (!result[number]) result[number] = !type.empty() && type.back() == '?'; + } + } + } + pos = scanner.SkipSqlTrivia(end + 1, sql.size()); } - if (scanner.QuestionCount > 0 && scanner.QuestionCount != boundParams.size()) { + return result; +} + +} // namespace + +TPreparedQuery::TPreparedQuery(std::string sql) + : Sql_(std::move(sql)) + , ParameterCount_(CountOdbcParams(Sql_)) + , StatementOffset_(Sql_.size() - GetSqlStatement(Sql_).size()) + , Declarations_(ReadDeclarations(Sql_)) { + const TSqlScanner scanner{Sql_, nullptr, false}; + const auto end = scanner.FindStatementEnd(StatementOffset_, Sql_.size()); + Multiple_ = end != std::string_view::npos + && scanner.SkipSqlTrivia(end + 1, Sql_.size()) < Sql_.size(); +} + +std::optional TPreparedQuery::DeclaredOptionality(SQLUSMALLINT number) const { + return number < Declarations_.size() ? Declarations_[number] : std::nullopt; +} + +bool TPreparedQuery::StartsWith(std::initializer_list keywords) const { + return std::ranges::any_of(keywords, [&](auto keyword) { + return StartsWithKeyword(Sql_, StatementOffset_, keyword); + }); +} + +TParamRewriteResult TPreparedQuery::Bind(const std::vector& params, bool escapes) { + auto& cached = Bodies_[escapes]; + if (!cached) { + std::string body; + body.reserve(Sql_.size()); + TSqlScanner scanner{Sql_, &body, escapes}; + scanner.Scan(0, Sql_.size(), true); + std::ranges::sort(scanner.Indices); + scanner.Indices.erase(std::unique(scanner.Indices.begin(), scanner.Indices.end()), scanner.Indices.end()); + cached.emplace(TBody{std::move(body), scanner.QuestionCount, std::move(scanner.Indices)}); + } + if (cached->QuestionCount && cached->QuestionCount != params.size()) { return {.Success = false, .SqlState = "07002", .Message = "COUNT field incorrect: unbound ? placeholders"}; } + const bool ordered = std::ranges::is_sorted(params, {}, &TBoundParam::ParamNumber); + auto bound = params.begin(); std::string declarations; - for (size_t rawIndex = 0; rawIndex < scanner.Indices.size(); ++rawIndex) { - if (!scanner.Indices.test(rawIndex)) { - continue; - } - const auto index = static_cast(rawIndex); - const std::string prefix = "DECLARE $p" + std::to_string(index) + " AS"; - if (GetDeclaredParamOptionality(sql, index).has_value()) { - continue; + for (const auto number : cached->Indices) { + if (DeclaredOptionality(number).has_value()) continue; + if (ordered) { + while (bound != params.end() && bound->ParamNumber < number) ++bound; + } else { + bound = std::ranges::find(params, number, &TBoundParam::ParamNumber); } - const auto bound = std::ranges::find(boundParams, index, &TBoundParam::ParamNumber); - if (bound == boundParams.end()) { + if (bound == params.end() || bound->ParamNumber != number) { return {.Success = false, .SqlState = "07002", .Message = "COUNT field incorrect"}; } const auto type = ResolveParamType(*bound); if (!type) { return {.Success = false, .SqlState = "07006", .Message = "Restricted data type attribute violation"}; } - declarations += prefix + " " + type->YqlType + declarations += "DECLARE $p" + std::to_string(number) + " AS " + type->YqlType + (BoundParamIsNull(*bound) ? "?;\n" : ";\n"); } - return {.Sql = declarations.empty() ? std::move(body) : AddSqlPreamble(body, declarations)}; + return {.Sql = declarations.empty() ? cached->Sql : AddSqlPreamble(cached->Sql, declarations)}; } -} // namespace - std::string AddSqlPreamble(std::string_view sql, std::string_view preamble) { // Lexer directives must precede generated statements. const TSqlScanner scanner{sql, nullptr, false}; @@ -485,40 +530,7 @@ TParamRewriteResult RewriteOdbcSql( std::string_view sql, const std::vector& boundParams, bool rewriteEscapes) { - return RewriteSql(sql, boundParams, rewriteEscapes); -} - -std::optional GetDeclaredParamOptionality( - std::string_view sql, - SQLUSMALLINT paramNumber) -{ - const std::string param = "$p" + std::to_string(paramNumber); - const TSqlScanner scanner{sql, nullptr, false}; - for (size_t pos = scanner.SkipSqlTrivia(0, sql.size()); pos < sql.size();) { - const bool isDeclare = StartsWithKeyword(sql, pos, "DECLARE"); - if (!isDeclare && !StartsWithKeyword(sql, pos, "PRAGMA")) { - break; - } - const size_t semicolon = scanner.FindStatementEnd(pos, sql.size()); - if (semicolon == std::string_view::npos) { - break; - } - size_t token = scanner.SkipSqlTrivia(pos + (isDeclare ? 7 : 6), semicolon); - if (isDeclare && semicolon - token >= param.size() - && sql.substr(token, param.size()) == param - && (token + param.size() == semicolon - || (!std::isalnum(static_cast(sql[token + param.size()])) - && sql[token + param.size()] != '_'))) { - token = scanner.SkipSqlTrivia(token + param.size(), semicolon); - if (StartsWithKeyword(sql, token, "AS")) { - const std::string_view type = TrimTrailingSqlTrivia( - sql.substr(token + 2, semicolon - token - 2)); - return !type.empty() && type.back() == '?'; - } - } - pos = scanner.SkipSqlTrivia(semicolon + 1, sql.size()); - } - return std::nullopt; + return TPreparedQuery(std::string(sql)).Bind(boundParams, rewriteEscapes); } std::string_view TrimTrailingSqlTrivia(std::string_view sql) { @@ -577,15 +589,6 @@ SQLSMALLINT CountOdbcParams(std::string_view sql) { : scanner.MaxDollarIndex; } -bool StartsWithSqlStatement( - std::string_view sql, - std::initializer_list keywords) { - sql = GetSqlStatement(sql); - return std::ranges::any_of(keywords, [&](std::string_view keyword) { - return StartsWithKeyword(sql, 0, keyword); - }); -} - std::optional BuildParamMetadataQuery( std::string_view input, bool rewriteEscapes, SQLUSMALLINT paramNumber, SQLSMALLINT paramCount) { const std::string sql = rewriteEscapes ? RewriteOdbcEscapes(std::string(input)) : std::string(input); diff --git a/odbc/src/utils/escape.h b/odbc/src/utils/escape.h deleted file mode 100644 index 0b37374b83..0000000000 --- a/odbc/src/utils/escape.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -#include "param_rewrite.h" diff --git a/odbc/src/utils/handle.h b/odbc/src/utils/handle.h index 22807617f8..b9e789804f 100644 --- a/odbc/src/utils/handle.h +++ b/odbc/src/utils/handle.h @@ -79,10 +79,16 @@ SQLRETURN CallOdbc(SQLHANDLE handlePtr, Fn&& func) { } if constexpr (Mode == ECallMode::Consuming && !std::is_same_v - && !std::is_same_v) { + && !std::is_same_v + && !std::is_same_v) { parent = owner->GetParent()->LockOperation(); } auto lock = owner->LockOperation(); + // Waiting for an active statement must not block unrelated DBC calls. + std::unique_lock statementParent; + if constexpr (Mode == ECallMode::Consuming && std::is_same_v) { + statementParent = owner->GetParent()->LockOperation(); + } if constexpr (Mode == ECallMode::Ordinary && std::is_same_v) { owner->StartExecution(); } diff --git a/odbc/src/utils/param_rewrite.h b/odbc/src/utils/param_rewrite.h index fd619e57c5..6166c3c51f 100644 --- a/odbc/src/utils/param_rewrite.h +++ b/odbc/src/utils/param_rewrite.h @@ -17,6 +17,30 @@ struct TParamRewriteResult { std::string Message; }; +class TPreparedQuery { +public: + explicit TPreparedQuery(std::string sql); + const std::string& Text() const { return Sql_; } + SQLSMALLINT ParameterCount() const { return ParameterCount_; } + std::optional DeclaredOptionality(SQLUSMALLINT number) const; + bool StartsWith(std::initializer_list keywords) const; + bool HasMultipleStatements() const { return Multiple_; } + TParamRewriteResult Bind(const std::vector& params, bool escapes); + +private: + struct TBody { + std::string Sql; + size_t QuestionCount; + std::vector Indices; + }; + std::string Sql_; + SQLSMALLINT ParameterCount_; + size_t StatementOffset_; + bool Multiple_; + std::vector> Declarations_; + std::optional Bodies_[2]; +}; + std::string RewriteOdbcEscapes(const std::string& sql); std::string AddSqlPreamble(std::string_view sql, std::string_view preamble); @@ -25,10 +49,6 @@ TParamRewriteResult RewriteOdbcSql( const std::vector& boundParams, bool rewriteEscapes); -std::optional GetDeclaredParamOptionality( - std::string_view sql, - SQLUSMALLINT paramNumber); - std::optional BuildParamMetadataQuery( std::string_view sql, bool rewriteEscapes, SQLUSMALLINT paramNumber, SQLSMALLINT paramCount); @@ -38,8 +58,4 @@ bool HasMultipleSqlStatements(std::string_view sql); SQLSMALLINT CountOdbcParams(std::string_view sql); -bool StartsWithSqlStatement( - std::string_view sql, - std::initializer_list keywords); - } // namespace NYdb::NOdbc diff --git a/odbc/src/utils/sql_type_map.cpp b/odbc/src/utils/sql_type_map.cpp index 51a2b8e758..2ec542f590 100644 --- a/odbc/src/utils/sql_type_map.cpp +++ b/odbc/src/utils/sql_type_map.cpp @@ -68,6 +68,29 @@ const TSqlTypeSpec* FindSqlTypeSpec(SQLSMALLINT sqlType) { return it == TypeSpecs.end() ? nullptr : &*it; } +std::string_view GetSqlTypeName(SQLSMALLINT sqlType, std::string_view unknownName) { + const auto* spec = FindSqlTypeSpec(sqlType); + return spec ? spec->Name : unknownName; +} + +bool IsCharacterType(SQLSMALLINT sqlType) { + return sqlType == SQL_CHAR || sqlType == SQL_VARCHAR || sqlType == SQL_LONGVARCHAR + || sqlType == SQL_WCHAR || sqlType == SQL_WVARCHAR || sqlType == SQL_WLONGVARCHAR; +} + +SQLSMALLINT GetDateTimeCode(SQLSMALLINT sqlType) { + switch (sqlType) { + 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; + } +} + +bool HasFixedPrecisionScale(SQLSMALLINT sqlType) { + return sqlType == SQL_DECIMAL || sqlType == SQL_NUMERIC; +} + SQLLEN GetDisplaySize(SQLSMALLINT sqlType, SQLLEN size, bool isUnsigned) { switch (sqlType) { case SQL_TINYINT: case SQL_SMALLINT: case SQL_INTEGER: case SQL_BIGINT: diff --git a/odbc/src/utils/sql_type_map.h b/odbc/src/utils/sql_type_map.h index ca56729e99..a4532f8746 100644 --- a/odbc/src/utils/sql_type_map.h +++ b/odbc/src/utils/sql_type_map.h @@ -88,6 +88,10 @@ struct TParamTypeSpec { std::span GetSqlTypeSpecs(); const TSqlTypeSpec* FindSqlTypeSpec(SQLSMALLINT sqlType); +std::string_view GetSqlTypeName(SQLSMALLINT sqlType, std::string_view unknownName = {}); +bool IsCharacterType(SQLSMALLINT sqlType); +SQLSMALLINT GetDateTimeCode(SQLSMALLINT sqlType); +bool HasFixedPrecisionScale(SQLSMALLINT sqlType); SQLLEN GetDisplaySize(SQLSMALLINT sqlType, SQLLEN size, bool isUnsigned = false); SQLULEN GetCTypeSize(SQLSMALLINT cType, SQLLEN variableLength); std::string MapSqlTypeToken(std::string_view sqlType); diff --git a/odbc/src/utils/status_util.cpp b/odbc/src/utils/status_util.cpp deleted file mode 100644 index 99620babd8..0000000000 --- a/odbc/src/utils/status_util.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "status_util.h" - -#include - -namespace NYdb::NOdbc { - -NYdb::TStatus StatusFrom(const NYdb::TStatus& ydbStatus) { - return NYdb::TStatus(ydbStatus.GetStatus(), NYdb::NIssue::TIssues(ydbStatus.GetIssues())); -} - -} // namespace NYdb::NOdbc diff --git a/odbc/src/utils/status_util.h b/odbc/src/utils/status_util.h deleted file mode 100644 index 43595c69ac..0000000000 --- a/odbc/src/utils/status_util.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include - -namespace NYdb::NOdbc { - -NYdb::TStatus StatusFrom(const NYdb::TStatus& ydbStatus); - -} // namespace NYdb::NOdbc diff --git a/odbc/src/utils/util.cpp b/odbc/src/utils/util.cpp index e957d7baf8..e830ab1c0d 100644 --- a/odbc/src/utils/util.cpp +++ b/odbc/src/utils/util.cpp @@ -157,12 +157,4 @@ TConnectionStringEntries ParseConnectionStringEntries(std::string_view connectio return entries; } -std::map ParseConnectionString(std::string_view connectionString) { - std::map params; - for (auto&& [key, value] : ParseConnectionStringEntries(connectionString)) { - params[std::move(key)] = std::move(value); - } - return params; -} - } // namespace NYdb::NOdbc diff --git a/odbc/src/utils/util.h b/odbc/src/utils/util.h index deacd84fb8..553a338f22 100644 --- a/odbc/src/utils/util.h +++ b/odbc/src/utils/util.h @@ -4,7 +4,6 @@ #include "odbc_compat.h" -#include #include #include #include @@ -22,6 +21,4 @@ using TConnectionStringEntries = std::vector TConnectionStringEntries ParseConnectionStringEntries(std::string_view connectionString); -std::map ParseConnectionString(std::string_view connectionString); - } // namespace NYdb::NOdbc diff --git a/odbc/tests/integration/concurrency_it.cpp b/odbc/tests/integration/concurrency_it.cpp index 8bd5c9c687..41b83d8e67 100644 --- a/odbc/tests/integration/concurrency_it.cpp +++ b/odbc/tests/integration/concurrency_it.cpp @@ -150,9 +150,9 @@ class TPausedStatement : public TStatement { protected: NYdb::NQuery::TExecuteQueryResult ExecuteQuery( - NYdb::NQuery::TSession& session, const NYdb::TParams& params, SQLULEN paramSet) override + NYdb::NQuery::TSession& session, const TExecutionInput& input) override { - auto result = TStatement::ExecuteQuery(session, params, paramSet); + auto result = TStatement::ExecuteQuery(session, input); auto expected = EState::Armed; if (State_.compare_exchange_strong(expected, EState::Paused)) { EXPECT_TRUE(result.IsSuccess()) << result.GetIssues().ToString(); @@ -510,6 +510,32 @@ TEST(Concurrency, ParentFreeWaitsForChildPublication) { EXPECT_EQ(SQLFreeHandle(SQL_HANDLE_ENV, env), SQL_SUCCESS); } +TEST(Concurrency, StatementFreeDoesNotBlockConnectionCalls) { + TConnection connection; + auto statement = std::make_unique(&connection); + connection.AddChild(statement.get()); + auto* owner = statement.get(); + SQLHSTMT handle = static_cast(statement.release()); + std::future freeing, attributes; + auto operation = owner->LockOperation(); + freeing = std::async(std::launch::async, [&] { return SQLFreeHandle(SQL_HANDLE_STMT, handle); }); + // The held statement lock keeps the consuming call inside its lifecycle gate. + EXPECT_TRUE(WaitUntil([&] { return !connection.LockExclusive(std::try_to_lock).owns_lock(); })); + EXPECT_EQ(freeing.wait_for(0s), std::future_status::timeout); + SQLUINTEGER autocommit = SQL_AUTOCOMMIT_OFF; + attributes = std::async(std::launch::async, [&] { + return SQLGetConnectAttr(static_cast(&connection), SQL_ATTR_AUTOCOMMIT, + &autocommit, sizeof(autocommit), nullptr); + }); + const auto ready = attributes.wait_for(1s); + operation.unlock(); + EXPECT_EQ(ready, std::future_status::ready); + EXPECT_EQ(attributes.get(), SQL_SUCCESS); + EXPECT_EQ(autocommit, SQL_AUTOCOMMIT_ON); + EXPECT_EQ(freeing.get(), SQL_SUCCESS); + EXPECT_TRUE(connection.GetChildren().empty()); +} + TEST(Concurrency, CancelQueuedTransactionDoesNotWaitOrAbortOtherStatements) { TConnectionHandles connection; TStatementHandle statement(connection.Dbc); diff --git a/odbc/tests/integration/statement_api_it.cpp b/odbc/tests/integration/statement_api_it.cpp index 11ccc9c5de..85d0a1d5be 100644 --- a/odbc/tests/integration/statement_api_it.cpp +++ b/odbc/tests/integration/statement_api_it.cpp @@ -917,9 +917,42 @@ TEST(StatementApi, PreparedResultMetadataBeforeExecute) { EXPECT_TRUE(SqlStatePrefix(executeError, "HY007") || SqlStatePrefix(executeError, "HY010")) << executeError; - CHECK_ODBC_OK(SQLExecDirect(stmt, + CHECK_ODBC_OK(SQLPrepare(stmt, + (SQLCHAR*)"SELECT ? AS value FROM result_metadata_before_execute_test", SQL_NTS), + stmt, SQL_HANDLE_STMT); + CHECK_ODBC_OK(SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, + 0, 0, ¶meter, 0, nullptr), stmt, SQL_HANDLE_STMT); + CHECK_ODBC_OK(SQLExecute(stmt), stmt, SQL_HANDLE_STMT); + CHECK_ODBC_OK(SQLCloseCursor(stmt), stmt, SQL_HANDLE_STMT); + SQLHSTMT dropStmt = SQL_NULL_HSTMT; + ASSERT_EQ(SQLAllocHandle(SQL_HANDLE_STMT, dbc, &dropStmt), SQL_SUCCESS); + CHECK_ODBC_OK(SQLExecDirect(dropStmt, (SQLCHAR*)"DROP TABLE result_metadata_before_execute_test", SQL_NTS), + dropStmt, SQL_HANDLE_STMT); + SQLFreeHandle(SQL_HANDLE_STMT, dropStmt); + + // Pointer/name updates keep the executed schema without querying the dropped table. + SQLINTEGER reboundParameter = 2; + SQLLEN reboundIndicator = 0; + CHECK_ODBC_OK(SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, + 0, 0, &reboundParameter, 0, &reboundIndicator), stmt, SQL_HANDLE_STMT); + CHECK_ODBC_OK(SQLGetStmtAttr(stmt, SQL_ATTR_APP_PARAM_DESC, &apd, 0, nullptr), stmt, SQL_HANDLE_STMT); + CHECK_ODBC_OK(SQLSetDescField(apd, 1, SQL_DESC_NAME, (SQLPOINTER)"renamed", SQL_NTS), + apd, SQL_HANDLE_DESC); + SQLHDESC ipd = SQL_NULL_HDESC; + CHECK_ODBC_OK(SQLGetStmtAttr(stmt, SQL_ATTR_IMP_PARAM_DESC, &ipd, 0, nullptr), + stmt, SQL_HANDLE_STMT); + CHECK_ODBC_OK(SQLSetDescField(ipd, 1, SQL_DESC_NAME, (SQLPOINTER)"renamed", SQL_NTS), + ipd, SQL_HANDLE_DESC); + CHECK_ODBC_OK(SQLNumResultCols(stmt, &columnCount), stmt, SQL_HANDLE_STMT); + EXPECT_EQ(columnCount, 1); + CHECK_ODBC_OK(SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, + 0, 0, &reboundParameter, 0, &reboundIndicator), stmt, SQL_HANDLE_STMT); + CHECK_ODBC_OK(SQLGetDescField(ird, 0, SQL_DESC_COUNT, &descriptorCount, 0, nullptr), + ird, SQL_HANDLE_DESC); + EXPECT_EQ(descriptorCount, 0); + EXPECT_EQ(SQLNumResultCols(stmt, &columnCount), SQL_ERROR); SQLFreeHandle(SQL_HANDLE_STMT, stmt); SQLDisconnect(dbc); SQLFreeHandle(SQL_HANDLE_DBC, dbc); diff --git a/odbc/tests/unit/conn_string_ut.cpp b/odbc/tests/unit/conn_string_ut.cpp index 8b406c30b6..40e945e9b1 100644 --- a/odbc/tests/unit/conn_string_ut.cpp +++ b/odbc/tests/unit/conn_string_ut.cpp @@ -2,31 +2,27 @@ #include +using NYdb::NOdbc::ParseConnectionStringEntries; +using NYdb::NOdbc::TConnectionStringEntries; + TEST(ConnString, ParsesBraceEscapedSemicolons) { - const auto params = NYdb::NOdbc::ParseConnectionString("Database={path;with;semicolons};Server=host"); - ASSERT_EQ(params.at("Database"), "path;with;semicolons"); - ASSERT_EQ(params.at("Server"), "host"); + EXPECT_EQ(ParseConnectionStringEntries("Database={path;with;semicolons};Server=host"), + (TConnectionStringEntries{{"Database", "path;with;semicolons"}, {"Server", "host"}})); } TEST(ConnString, DecodesEscapedClosingBrace) { - const auto params = NYdb::NOdbc::ParseConnectionString( - "PWD={a}}b};Database=/local;Server=host"); - ASSERT_EQ(params.at("PWD"), "a}b"); - ASSERT_EQ(params.at("Database"), "/local"); - ASSERT_EQ(params.at("Server"), "host"); + EXPECT_EQ(ParseConnectionStringEntries("PWD={a}}b};Database=/local;Server=host"), + (TConnectionStringEntries{{"PWD", "a}b"}, {"Database", "/local"}, {"Server", "host"}})); } TEST(ConnString, ParsesSimplePairs) { - const auto params = NYdb::NOdbc::ParseConnectionString("DSN=YDB;Database=/local;Server=grpc://localhost:2136"); - ASSERT_EQ(params.at("DSN"), "YDB"); - ASSERT_EQ(params.at("Database"), "/local"); - ASSERT_EQ(params.at("Server"), "grpc://localhost:2136"); + EXPECT_EQ(ParseConnectionStringEntries("DSN=YDB;Database=/local;Server=grpc://localhost:2136"), + (TConnectionStringEntries{{"DSN", "YDB"}, {"Database", "/local"}, {"Server", "grpc://localhost:2136"}})); } TEST(ConnString, TrimsWhitespace) { - const auto params = NYdb::NOdbc::ParseConnectionString(" Database = /local ; Server = host "); - ASSERT_EQ(params.at("Database"), "/local"); - ASSERT_EQ(params.at("Server"), "host"); + EXPECT_EQ(ParseConnectionStringEntries(" Database = /local ; Server = host "), + (TConnectionStringEntries{{"Database", "/local"}, {"Server", "host"}})); } TEST(OdbcString, ConvertsUtf16ToUtf8) { diff --git a/odbc/tests/unit/descriptor_ut.cpp b/odbc/tests/unit/descriptor_ut.cpp index c3754bd885..40a4091166 100644 --- a/odbc/tests/unit/descriptor_ut.cpp +++ b/odbc/tests/unit/descriptor_ut.cpp @@ -3,6 +3,7 @@ #include "statement.h" #include +#include namespace NYdb::NOdbc { namespace { @@ -75,21 +76,45 @@ TEST(OdbcStatement, NoOpUnbindPreservesDescriptorGenerations) { SQLHDESC handle = SQL_NULL_HDESC; ASSERT_EQ(statement.GetStmtAttr(SQL_ATTR_APP_ROW_DESC, &handle, 0, nullptr), SQL_SUCCESS); auto& descriptor = *CheckedHandle(handle); - TDescriptorState before, after; - uint64_t generation = 0, nextGeneration = 0; - descriptor.Snapshot(before, generation); + TDescriptorSnapshot snapshot; + ASSERT_TRUE(descriptor.Snapshot(snapshot, true)); + const auto generation = descriptor.GetGeneration(); ASSERT_EQ(statement.BindCol(2, SQL_C_LONG, nullptr, 0, nullptr), SQL_SUCCESS); - descriptor.Snapshot(after, nextGeneration); - EXPECT_EQ(nextGeneration, generation); - EXPECT_EQ(after.GetSchemaGeneration(), before.GetSchemaGeneration()); - EXPECT_EQ(after.GetRecordCount(), 1); + EXPECT_FALSE(descriptor.Snapshot(snapshot, true)); + EXPECT_EQ(descriptor.GetGeneration(), generation); + EXPECT_EQ(snapshot.GetRecordCount(), 1); + + SQLINTEGER rebound = 0; + { + auto lock = descriptor.LockOperation(); + ASSERT_EQ(descriptor.SetDescField(1, SQL_DESC_DATA_PTR, &rebound, 0), SQL_SUCCESS); + ASSERT_EQ(descriptor.SetDescField(1, SQL_DESC_NAME, (SQLPOINTER)"renamed", SQL_NTS), SQL_SUCCESS); + } + EXPECT_FALSE(descriptor.Snapshot(snapshot, true)); + EXPECT_NE(descriptor.GetGeneration(), generation); + ASSERT_NE(snapshot.FindRecord(1), nullptr); + EXPECT_EQ(snapshot.FindRecord(1)->DataPtr, &rebound); + EXPECT_EQ(snapshot.FindRecord(1)->Name, "renamed"); + + { + auto lock = descriptor.LockOperation(); + ASSERT_EQ(descriptor.SetDescField(1, SQL_DESC_CONCISE_TYPE, + reinterpret_cast(SQL_C_SHORT), 0), SQL_SUCCESS); + } + EXPECT_TRUE(descriptor.Snapshot(snapshot, true)); + { + auto lock = descriptor.LockOperation(); + ASSERT_EQ(descriptor.SetDescField(2, SQL_DESC_NAME, (SQLPOINTER)"new", SQL_NTS), SQL_SUCCESS); + } + EXPECT_TRUE(descriptor.Snapshot(snapshot, true)); + EXPECT_EQ(snapshot.GetRecordCount(), 2); + ASSERT_EQ(statement.BindCol(2, SQL_C_LONG, nullptr, 0, nullptr), SQL_SUCCESS); + EXPECT_TRUE(descriptor.Snapshot(snapshot, true)); ASSERT_EQ(statement.BindCol(1, SQL_C_LONG, nullptr, 0, nullptr), SQL_SUCCESS); - descriptor.Snapshot(after, nextGeneration); - EXPECT_NE(nextGeneration, generation); - EXPECT_NE(after.GetSchemaGeneration(), before.GetSchemaGeneration()); - EXPECT_EQ(after.GetRecordCount(), 0); + EXPECT_TRUE(descriptor.Snapshot(snapshot, true)); + EXPECT_EQ(snapshot.GetRecordCount(), 0); } TEST(OdbcStatement, CancelClearsNeedDataAndAllowsReuse) { @@ -124,6 +149,11 @@ TEST(OdbcStatement, CancelClearsNeedDataAndAllowsReuse) { ASSERT_EQ(statement.ParamData(&value), SQL_NEED_DATA); EXPECT_EQ(value, &token); EXPECT_EQ(statement.Cancel(), SQL_SUCCESS); + + ASSERT_EQ(statement.Execute(), SQL_NEED_DATA); + EXPECT_THROW(statement.Prepare("SELECT $p32768"), std::length_error); + EXPECT_THROW(statement.ParamData(&value), TOdbcException); + EXPECT_EQ(statement.Cancel(), SQL_SUCCESS); } TEST_F(TOdbcDescriptor, ExecuteUsesLatestParameterBinding) { @@ -145,6 +175,32 @@ TEST_F(TOdbcDescriptor, ExecuteUsesLatestParameterBinding) { SQLSMALLINT type = 0; ASSERT_EQ(statement.DescribeParam(1, &type, nullptr, nullptr, nullptr), SQL_SUCCESS); EXPECT_EQ(type, SQL_VARCHAR); + + SQLHDESC handle = SQL_NULL_HDESC; + ASSERT_EQ(statement.GetStmtAttr(SQL_ATTR_IMP_PARAM_DESC, &handle, 0, nullptr), SQL_SUCCESS); + auto& descriptor = *CheckedHandle(handle); + TDescriptorSnapshot snapshot; + ASSERT_TRUE(descriptor.Snapshot(snapshot)); + { + auto lock = descriptor.LockOperation(); + ASSERT_EQ(descriptor.SetDescField(1, SQL_DESC_LENGTH, + reinterpret_cast(32), 0), SQL_SUCCESS); + } + EXPECT_TRUE(descriptor.Snapshot(snapshot)); + { + auto lock = descriptor.LockOperation(); + ASSERT_EQ(descriptor.SetDescField(1, SQL_DESC_SCALE, + reinterpret_cast(3), 0), SQL_SUCCESS); + } + EXPECT_TRUE(descriptor.Snapshot(snapshot)); + ASSERT_EQ(statement.BindParameter(1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_TYPE_TIMESTAMP, + 32, 3, &second, 0, &indicator), SQL_SUCCESS); + EXPECT_TRUE(descriptor.Snapshot(snapshot)); + ASSERT_EQ(statement.DescribeParam(1, &type, nullptr, nullptr, nullptr), SQL_SUCCESS); + EXPECT_EQ(type, SQL_TYPE_TIMESTAMP); + EXPECT_TRUE(descriptor.Snapshot(snapshot)); + ASSERT_NE(snapshot.FindRecord(1), nullptr); + EXPECT_EQ(snapshot.FindRecord(1)->WideTemporalType, EWideTemporal::None); } } // namespace diff --git a/odbc/tests/unit/escape_ut.cpp b/odbc/tests/unit/escape_ut.cpp index 3806c42788..37a04ae550 100644 --- a/odbc/tests/unit/escape_ut.cpp +++ b/odbc/tests/unit/escape_ut.cpp @@ -1,4 +1,4 @@ -#include "utils/escape.h" +#include "utils/param_rewrite.h" #include diff --git a/odbc/tests/unit/param_rewrite_ut.cpp b/odbc/tests/unit/param_rewrite_ut.cpp index f0c8d3c289..5d6ac6f7d5 100644 --- a/odbc/tests/unit/param_rewrite_ut.cpp +++ b/odbc/tests/unit/param_rewrite_ut.cpp @@ -6,9 +6,8 @@ using NYdb::NOdbc::RewriteOdbcSql; using NYdb::NOdbc::CountOdbcParams; -using NYdb::NOdbc::GetDeclaredParamOptionality; using NYdb::NOdbc::HasMultipleSqlStatements; -using NYdb::NOdbc::StartsWithSqlStatement; +using NYdb::NOdbc::TPreparedQuery; using NYdb::NOdbc::TBoundParam; using NYdb::NOdbc::BuildParamMetadataQuery; @@ -45,6 +44,27 @@ TEST(OdbcParamRewrite, RewritesQuestionMarks) { "--!ansi_lexer\n/* header */ DECLARE $p1 AS Int32;\nSELECT $p1 AS \"result\""); } +TEST(OdbcParamRewrite, ReusesPreparedSyntaxAcrossBindingsAndScanModes) { + NYdb::NOdbc::TPreparedQuery query("--!ansi_lexer\nSELECT ?, {fn ABS(-1)}"); + EXPECT_EQ(query.ParameterCount(), 1); + EXPECT_TRUE(query.StartsWith({"SELECT"})); + EXPECT_FALSE(query.HasMultipleStatements()); + EXPECT_EQ(query.Bind({IntParam(1)}, true).Sql, + "--!ansi_lexer\nDECLARE $p1 AS Int32;\nSELECT $p1, ABS(-1)"); + auto moved = std::move(query); + EXPECT_EQ(moved.Bind({NullIntParam(1)}, false).Sql, + "--!ansi_lexer\nDECLARE $p1 AS Int32?;\nSELECT $p1, {fn ABS(-1)}"); + auto unsignedParam = IntParam(1); + unsignedParam.ValueType = SQL_C_UBIGINT; + EXPECT_EQ(moved.Bind({unsignedParam}, true).Sql, + "--!ansi_lexer\nDECLARE $p1 AS Uint64;\nSELECT $p1, ABS(-1)"); + EXPECT_EQ(moved.Bind({}, true).SqlState, "07002"); + NYdb::NOdbc::TPreparedQuery sparse("DECLARE $p32767 AS Int32?; SELECT $p32767, $p32767"); + EXPECT_EQ(sparse.ParameterCount(), 32767); + EXPECT_EQ(sparse.DeclaredOptionality(32767), true); + EXPECT_EQ(sparse.Bind({IntParam(32767)}, false).Sql, sparse.Text()); +} + TEST(OdbcParamRewrite, ParameterMetadataProbes) { const std::pair cases[] = { {"/* ? */ INSERT INTO `test` (`id`, str) VALUES (?, ?)", @@ -129,16 +149,11 @@ TEST(OdbcParamRewrite, SkipsLiteralAndYqlOptionalSyntax) { } TEST(OdbcParamRewrite, DetectsOptionalDeclarationsBeforeComments) { - EXPECT_EQ(GetDeclaredParamOptionality( - "DECLARE $p1 AS Int32? /* nullable */;\nSELECT $p1", 1), true); - EXPECT_EQ(GetDeclaredParamOptionality( - "DECLARE $p1 AS Int32? -- nullable\n;\nSELECT $p1", 1), true); - EXPECT_EQ(GetDeclaredParamOptionality( - "DECLARE $p1 AS Int32 /* not optional?; */;\nSELECT $p1", 1), false); - EXPECT_EQ(GetDeclaredParamOptionality( - "/* DECLARE $p1 AS Int32?; */\nSELECT $p1", 1), std::nullopt); - EXPECT_EQ(GetDeclaredParamOptionality( - "pragma TablePathPrefix = \"/local\"; declare /* p */ $p1 as Int32?; SELECT $p1", 1), + EXPECT_EQ(TPreparedQuery("DECLARE $p1 AS Int32? /* nullable */;\nSELECT $p1").DeclaredOptionality(1), true); + EXPECT_EQ(TPreparedQuery("DECLARE $p1 AS Int32? -- nullable\n;\nSELECT $p1").DeclaredOptionality(1), true); + EXPECT_EQ(TPreparedQuery("DECLARE $p1 AS Int32 /* not optional?; */;\nSELECT $p1").DeclaredOptionality(1), false); + EXPECT_EQ(TPreparedQuery("/* DECLARE $p1 AS Int32?; */\nSELECT $p1").DeclaredOptionality(1), std::nullopt); + EXPECT_EQ(TPreparedQuery("pragma TablePathPrefix = \"/local\"; declare /* p */ $p1 as Int32?; SELECT $p1").DeclaredOptionality(1), true); } @@ -194,6 +209,9 @@ TEST(OdbcParamRewrite, ParameterCountLimits) { EXPECT_EQ(CountOdbcParams("SELECT $p32767"), 32767); EXPECT_TRUE(RewriteOdbcSql("SELECT $p32767", {IntParam(32767)}, false).Success); markers += "?,"; + TPreparedQuery escaped("SELECT {fn COALESCE(" + markers + "0)}"); + EXPECT_EQ(escaped.ParameterCount(), 0); + EXPECT_THROW(escaped.Bind({}, true), std::length_error); for (const auto& sql : {markers, markers + markers, std::string("SELECT $p32768"), std::string("SELECT $p65536"), std::string("SELECT $p999999999999999999999")}) { EXPECT_THROW(CountOdbcParams(sql), std::length_error); @@ -203,22 +221,17 @@ TEST(OdbcParamRewrite, ParameterCountLimits) { } TEST(OdbcParamRewrite, ClassifiesAfterTrivia) { - EXPECT_TRUE(StartsWithSqlStatement(" -- lead\n /* block */ INSERT INTO t VALUES (1)", {"INSERT"})); - EXPECT_TRUE(StartsWithSqlStatement( - "PRAGMA TablePathPrefix = \"/local\"; DECLARE $p1 AS Int32; UPSERT INTO t VALUES ($p1)", - {"UPSERT"})); - EXPECT_TRUE(StartsWithSqlStatement( - "$rows = (SELECT 1); SELECT * FROM $rows; -- trailing", {"SELECT"})); - EXPECT_TRUE(StartsWithSqlStatement( - "DEFINE ACTION $read() AS SELECT 1; END DEFINE; SELECT 2", {"SELECT"})); - EXPECT_TRUE(StartsWithSqlStatement( - "PRAGMA config = \"quoted\\\";value\"; CREATE TABLE t (id Int32)", {"CREATE"})); - EXPECT_TRUE(StartsWithSqlStatement("DELETE FROM t; SELECT * FROM t", {"DELETE"})); - EXPECT_FALSE(StartsWithSqlStatement("DELETE FROM t; SELECT * FROM t", {"SELECT"})); + EXPECT_TRUE(TPreparedQuery(" -- lead\n /* block */ INSERT INTO t VALUES (1)").StartsWith({"INSERT"})); + EXPECT_TRUE(TPreparedQuery("PRAGMA TablePathPrefix = \"/local\"; DECLARE $p1 AS Int32; UPSERT INTO t VALUES ($p1)").StartsWith({"UPSERT"})); + EXPECT_TRUE(TPreparedQuery("$rows = (SELECT 1); SELECT * FROM $rows; -- trailing").StartsWith({"SELECT"})); + EXPECT_TRUE(TPreparedQuery("DEFINE ACTION $read() AS SELECT 1; END DEFINE; SELECT 2").StartsWith({"SELECT"})); + EXPECT_TRUE(TPreparedQuery("PRAGMA config = \"quoted\\\";value\"; CREATE TABLE t (id Int32)").StartsWith({"CREATE"})); + EXPECT_TRUE(TPreparedQuery("DELETE FROM t; SELECT * FROM t").StartsWith({"DELETE"})); + EXPECT_FALSE(TPreparedQuery("DELETE FROM t; SELECT * FROM t").StartsWith({"SELECT"})); EXPECT_TRUE(HasMultipleSqlStatements("DELETE FROM t; SELECT * FROM t")); EXPECT_TRUE(HasMultipleSqlStatements("PRAGMA x; DELETE FROM t; SELECT * FROM t")); EXPECT_FALSE(HasMultipleSqlStatements("PRAGMA x; SELECT 1; -- trailing")); EXPECT_FALSE(HasMultipleSqlStatements( "DEFINE ACTION $read() AS SELECT 1; END DEFINE; SELECT 2")); - EXPECT_FALSE(StartsWithSqlStatement(" -- lead\n SELECT 1", {"INSERT", "UPDATE"})); + EXPECT_FALSE(TPreparedQuery(" -- lead\n SELECT 1").StartsWith({"INSERT", "UPDATE"})); }