2015-01-27 11:29:30 +01:00
|
|
|
/*
|
|
|
|
* The contents of this file are subject to the Initial
|
|
|
|
* Developer's Public License Version 1.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed AS IS,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing rights
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Dmitry Yemanov
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Dmitry Yemanov <dimitrf@firebirdsql.org>
|
|
|
|
* and all contributors signed below.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "firebird.h"
|
2021-12-21 09:02:58 +01:00
|
|
|
#include "../common/classes/ClumpletWriter.h"
|
2015-01-27 11:29:30 +01:00
|
|
|
#include "../jrd/tra_proto.h"
|
|
|
|
#include "../jrd/trace/TraceManager.h"
|
|
|
|
#include "../jrd/trace/TraceDSQLHelpers.h"
|
|
|
|
|
2021-12-21 09:02:58 +01:00
|
|
|
#include "../dsql/dsql_proto.h"
|
2015-01-27 11:29:30 +01:00
|
|
|
#include "../dsql/DsqlCursor.h"
|
|
|
|
|
|
|
|
using namespace Firebird;
|
|
|
|
using namespace Jrd;
|
|
|
|
|
2015-02-14 19:55:00 +01:00
|
|
|
static const char* const SCRATCH = "fb_cursor_";
|
|
|
|
static const ULONG PREFETCH_SIZE = 65536; // 64 KB
|
2015-01-27 11:29:30 +01:00
|
|
|
|
2022-02-07 19:52:12 +01:00
|
|
|
DsqlCursor::DsqlCursor(DsqlDmlRequest* req, ULONG flags)
|
2022-02-09 19:47:58 +01:00
|
|
|
: m_dsqlRequest(req), m_message(req->getDsqlStatement()->getReceiveMsg()),
|
2021-12-01 09:44:50 +01:00
|
|
|
m_resultSet(NULL), m_flags(flags),
|
2015-01-27 11:29:30 +01:00
|
|
|
m_space(req->getPool(), SCRATCH),
|
2021-12-01 09:44:50 +01:00
|
|
|
m_state(BOS), m_eof(false), m_position(0), m_cachedCount(0)
|
2015-01-27 11:29:30 +01:00
|
|
|
{
|
2022-02-09 19:47:58 +01:00
|
|
|
TRA_link_cursor(m_dsqlRequest->req_transaction, this);
|
2015-01-27 11:29:30 +01:00
|
|
|
}
|
|
|
|
|
2015-02-17 12:42:50 +01:00
|
|
|
DsqlCursor::~DsqlCursor()
|
|
|
|
{
|
|
|
|
if (m_resultSet)
|
|
|
|
m_resultSet->resetHandle();
|
|
|
|
}
|
|
|
|
|
2015-01-27 11:29:30 +01:00
|
|
|
jrd_tra* DsqlCursor::getTransaction() const
|
|
|
|
{
|
2022-02-09 19:47:58 +01:00
|
|
|
return m_dsqlRequest->req_transaction;
|
2015-01-27 11:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Attachment* DsqlCursor::getAttachment() const
|
|
|
|
{
|
2022-02-09 19:47:58 +01:00
|
|
|
return m_dsqlRequest->req_dbb->dbb_attachment;
|
2015-01-27 11:29:30 +01:00
|
|
|
}
|
|
|
|
|
2023-09-19 01:27:13 +02:00
|
|
|
void DsqlCursor::setInterfacePtr(JResultSet* interfacePtr) noexcept
|
2015-02-17 12:42:50 +01:00
|
|
|
{
|
|
|
|
fb_assert(!m_resultSet);
|
|
|
|
m_resultSet = interfacePtr;
|
|
|
|
}
|
|
|
|
|
2015-01-27 11:29:30 +01:00
|
|
|
void DsqlCursor::close(thread_db* tdbb, DsqlCursor* cursor)
|
|
|
|
{
|
|
|
|
if (!cursor)
|
|
|
|
return;
|
|
|
|
|
2022-02-07 19:52:12 +01:00
|
|
|
const auto attachment = cursor->getAttachment();
|
2022-02-09 19:47:58 +01:00
|
|
|
const auto dsqlRequest = cursor->m_dsqlRequest;
|
2015-01-27 11:29:30 +01:00
|
|
|
|
2022-02-09 19:47:58 +01:00
|
|
|
if (dsqlRequest->getRequest())
|
2015-01-27 11:29:30 +01:00
|
|
|
{
|
|
|
|
ThreadStatusGuard status_vector(tdbb);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Report some remaining fetches if any
|
2022-02-09 19:47:58 +01:00
|
|
|
if (dsqlRequest->req_fetch_baseline)
|
2015-01-27 11:29:30 +01:00
|
|
|
{
|
2022-02-09 19:47:58 +01:00
|
|
|
TraceDSQLFetch trace(attachment, dsqlRequest);
|
2015-02-18 16:01:17 +01:00
|
|
|
trace.fetch(true, ITracePlugin::RESULT_SUCCESS);
|
2015-01-27 11:29:30 +01:00
|
|
|
}
|
|
|
|
|
2022-02-09 19:47:58 +01:00
|
|
|
if (dsqlRequest->req_traced && TraceManager::need_dsql_free(attachment))
|
2015-01-27 11:29:30 +01:00
|
|
|
{
|
2022-02-09 19:47:58 +01:00
|
|
|
TraceSQLStatementImpl stmt(dsqlRequest, NULL);
|
2015-01-27 11:29:30 +01:00
|
|
|
TraceManager::event_dsql_free(attachment, &stmt, DSQL_close);
|
|
|
|
}
|
|
|
|
|
2022-02-09 19:47:58 +01:00
|
|
|
JRD_unwind_request(tdbb, dsqlRequest->getRequest());
|
2015-01-27 11:29:30 +01:00
|
|
|
}
|
|
|
|
catch (Firebird::Exception&)
|
|
|
|
{} // no-op
|
|
|
|
}
|
|
|
|
|
2022-02-09 19:47:58 +01:00
|
|
|
dsqlRequest->req_cursor = NULL;
|
|
|
|
TRA_unlink_cursor(dsqlRequest->req_transaction, cursor);
|
2015-01-27 11:29:30 +01:00
|
|
|
delete cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DsqlCursor::fetchNext(thread_db* tdbb, UCHAR* buffer)
|
|
|
|
{
|
|
|
|
if (!(m_flags & IStatement::CURSOR_TYPE_SCROLLABLE))
|
|
|
|
{
|
2022-02-09 19:47:58 +01:00
|
|
|
m_eof = !m_dsqlRequest->fetch(tdbb, buffer);
|
2015-01-27 11:29:30 +01:00
|
|
|
|
|
|
|
if (m_eof)
|
|
|
|
{
|
|
|
|
m_state = EOS;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_state = POSITIONED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:41:23 +01:00
|
|
|
return fetchRelative(tdbb, buffer, 1);
|
2015-01-27 11:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int DsqlCursor::fetchPrior(thread_db* tdbb, UCHAR* buffer)
|
|
|
|
{
|
|
|
|
if (!(m_flags & IStatement::CURSOR_TYPE_SCROLLABLE))
|
|
|
|
(Arg::Gds(isc_invalid_fetch_option) << Arg::Str("PRIOR")).raise();
|
|
|
|
|
2021-11-25 18:41:23 +01:00
|
|
|
return fetchRelative(tdbb, buffer, -1);
|
2015-01-27 11:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int DsqlCursor::fetchFirst(thread_db* tdbb, UCHAR* buffer)
|
|
|
|
{
|
|
|
|
if (!(m_flags & IStatement::CURSOR_TYPE_SCROLLABLE))
|
|
|
|
(Arg::Gds(isc_invalid_fetch_option) << Arg::Str("FIRST")).raise();
|
|
|
|
|
|
|
|
return fetchAbsolute(tdbb, buffer, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DsqlCursor::fetchLast(thread_db* tdbb, UCHAR* buffer)
|
|
|
|
{
|
|
|
|
if (!(m_flags & IStatement::CURSOR_TYPE_SCROLLABLE))
|
|
|
|
(Arg::Gds(isc_invalid_fetch_option) << Arg::Str("LAST")).raise();
|
|
|
|
|
|
|
|
return fetchAbsolute(tdbb, buffer, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DsqlCursor::fetchAbsolute(thread_db* tdbb, UCHAR* buffer, SLONG position)
|
|
|
|
{
|
|
|
|
if (!(m_flags & IStatement::CURSOR_TYPE_SCROLLABLE))
|
|
|
|
(Arg::Gds(isc_invalid_fetch_option) << Arg::Str("ABSOLUTE")).raise();
|
|
|
|
|
|
|
|
if (!position)
|
|
|
|
{
|
|
|
|
m_state = BOS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SINT64 offset = -1;
|
|
|
|
|
|
|
|
if (position < 0)
|
|
|
|
{
|
|
|
|
if (!m_eof)
|
|
|
|
{
|
|
|
|
cacheInput(tdbb);
|
|
|
|
fb_assert(m_eof);
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = m_cachedCount;
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:41:23 +01:00
|
|
|
if (position + offset < 0)
|
|
|
|
{
|
|
|
|
m_state = BOS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-27 11:29:30 +01:00
|
|
|
return fetchFromCache(tdbb, buffer, position + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DsqlCursor::fetchRelative(thread_db* tdbb, UCHAR* buffer, SLONG offset)
|
|
|
|
{
|
|
|
|
if (!(m_flags & IStatement::CURSOR_TYPE_SCROLLABLE))
|
|
|
|
(Arg::Gds(isc_invalid_fetch_option) << Arg::Str("RELATIVE")).raise();
|
|
|
|
|
2021-11-25 18:41:23 +01:00
|
|
|
SINT64 position = m_position + offset;
|
|
|
|
|
2015-01-27 11:29:30 +01:00
|
|
|
if (m_state == BOS)
|
|
|
|
{
|
|
|
|
if (offset <= 0)
|
|
|
|
return -1;
|
|
|
|
|
2021-11-25 18:41:23 +01:00
|
|
|
position = offset - 1;
|
2015-01-27 11:29:30 +01:00
|
|
|
}
|
|
|
|
else if (m_state == EOS)
|
|
|
|
{
|
|
|
|
if (offset >= 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
fb_assert(m_eof);
|
|
|
|
|
2021-11-25 18:41:23 +01:00
|
|
|
position = m_cachedCount + offset;
|
2015-01-27 11:29:30 +01:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:41:23 +01:00
|
|
|
if (position < 0)
|
2015-01-27 11:29:30 +01:00
|
|
|
{
|
|
|
|
m_state = BOS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:41:23 +01:00
|
|
|
return fetchFromCache(tdbb, buffer, position);
|
2015-01-27 11:29:30 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 09:02:58 +01:00
|
|
|
void DsqlCursor::getInfo(thread_db* tdbb,
|
|
|
|
unsigned int itemsLength, const unsigned char* items,
|
|
|
|
unsigned int bufferLength, unsigned char* buffer)
|
|
|
|
{
|
|
|
|
if (bufferLength < 7) // isc_info_error + 2-byte length + 4-byte error code
|
|
|
|
{
|
|
|
|
if (bufferLength)
|
|
|
|
*buffer = isc_info_truncated;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool isScrollable = (m_flags & IStatement::CURSOR_TYPE_SCROLLABLE);
|
|
|
|
|
|
|
|
ClumpletWriter response(ClumpletReader::InfoResponse, bufferLength - 1); // isc_info_end
|
|
|
|
ISC_STATUS errorCode = 0;
|
|
|
|
bool needLength = false, completed = false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ClumpletReader infoItems(ClumpletReader::InfoItems, items, itemsLength);
|
|
|
|
for (infoItems.rewind(); !errorCode && !infoItems.isEof(); infoItems.moveNext())
|
|
|
|
{
|
|
|
|
const auto tag = infoItems.getClumpTag();
|
|
|
|
|
|
|
|
switch (tag)
|
|
|
|
{
|
|
|
|
case isc_info_end:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case isc_info_length:
|
|
|
|
needLength = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IResultSet::INF_RECORD_COUNT:
|
|
|
|
if (isScrollable && !m_eof)
|
|
|
|
{
|
|
|
|
cacheInput(tdbb);
|
|
|
|
fb_assert(m_eof);
|
|
|
|
}
|
|
|
|
response.insertInt(tag, isScrollable ? m_cachedCount : -1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
errorCode = isc_infunk;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
completed = infoItems.isEof();
|
|
|
|
|
|
|
|
if (needLength && completed)
|
|
|
|
{
|
|
|
|
response.rewind();
|
|
|
|
response.insertInt(isc_info_length, response.getBufferLength() + 1); // isc_info_end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Exception&)
|
|
|
|
{
|
|
|
|
if (!response.hasOverflow())
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorCode)
|
|
|
|
{
|
|
|
|
response.clear();
|
|
|
|
response.insertInt(isc_info_error, (SLONG) errorCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
fb_assert(response.getBufferLength() <= bufferLength);
|
|
|
|
memcpy(buffer, response.getBuffer(), response.getBufferLength());
|
|
|
|
buffer += response.getBufferLength();
|
|
|
|
|
|
|
|
*buffer = completed ? isc_info_end : isc_info_truncated;
|
|
|
|
}
|
|
|
|
|
2015-01-27 11:29:30 +01:00
|
|
|
int DsqlCursor::fetchFromCache(thread_db* tdbb, UCHAR* buffer, FB_UINT64 position)
|
|
|
|
{
|
|
|
|
if (position >= m_cachedCount)
|
|
|
|
{
|
|
|
|
if (m_eof || !cacheInput(tdbb, position))
|
|
|
|
{
|
|
|
|
m_state = EOS;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fb_assert(position < m_cachedCount);
|
|
|
|
|
2022-02-09 19:47:58 +01:00
|
|
|
UCHAR* const msgBuffer = m_dsqlRequest->req_msg_buffers[m_message->msg_buffer_number];
|
2021-12-01 09:44:50 +01:00
|
|
|
|
|
|
|
const FB_UINT64 offset = position * m_message->msg_length;
|
|
|
|
const FB_UINT64 readBytes = m_space.read(offset, msgBuffer, m_message->msg_length);
|
|
|
|
fb_assert(readBytes == m_message->msg_length);
|
|
|
|
|
2022-02-09 19:47:58 +01:00
|
|
|
m_dsqlRequest->mapInOut(tdbb, true, m_message, NULL, buffer);
|
2021-12-01 09:44:50 +01:00
|
|
|
|
2015-01-27 11:29:30 +01:00
|
|
|
m_position = position;
|
|
|
|
m_state = POSITIONED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DsqlCursor::cacheInput(thread_db* tdbb, FB_UINT64 position)
|
|
|
|
{
|
|
|
|
fb_assert(!m_eof);
|
|
|
|
|
2021-12-01 09:44:50 +01:00
|
|
|
const ULONG prefetchCount = MAX(PREFETCH_SIZE / m_message->msg_length, 1);
|
2022-02-09 19:47:58 +01:00
|
|
|
const UCHAR* const msgBuffer = m_dsqlRequest->req_msg_buffers[m_message->msg_buffer_number];
|
2015-01-27 11:29:30 +01:00
|
|
|
|
|
|
|
while (position >= m_cachedCount)
|
|
|
|
{
|
2021-12-01 09:44:50 +01:00
|
|
|
for (ULONG count = 0; count < prefetchCount; count++)
|
2015-01-27 11:29:30 +01:00
|
|
|
{
|
2022-02-09 19:47:58 +01:00
|
|
|
if (!m_dsqlRequest->fetch(tdbb, NULL))
|
2015-01-27 11:29:30 +01:00
|
|
|
{
|
|
|
|
m_eof = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-12-01 09:44:50 +01:00
|
|
|
const FB_UINT64 offset = m_cachedCount * m_message->msg_length;
|
|
|
|
const FB_UINT64 writtenBytes = m_space.write(offset, msgBuffer, m_message->msg_length);
|
|
|
|
fb_assert(writtenBytes == m_message->msg_length);
|
|
|
|
m_cachedCount++;
|
2015-01-27 11:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_eof)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (position < m_cachedCount);
|
|
|
|
}
|