2009-12-09 19:45:44 +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 John Bellardo
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2000 John Bellardo <bellardo@users.sourceforge.net>
|
|
|
|
* and all contributors signed below.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "firebird.h"
|
|
|
|
#include "../jrd/jrd.h"
|
|
|
|
#include "../jrd/req.h"
|
|
|
|
#include "../jrd/cmp_proto.h"
|
|
|
|
#include "../jrd/evl_proto.h"
|
|
|
|
#include "../jrd/mov_proto.h"
|
|
|
|
|
|
|
|
#include "RecordSource.h"
|
|
|
|
|
|
|
|
using namespace Firebird;
|
|
|
|
using namespace Jrd;
|
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
// Data access: first N rows filter
|
|
|
|
// --------------------------------
|
|
|
|
|
2010-11-21 04:47:29 +01:00
|
|
|
FirstRowsStream::FirstRowsStream(CompilerScratch* csb, RecordSource* next, ValueExprNode* value)
|
2009-12-09 19:45:44 +01:00
|
|
|
: m_next(next), m_value(value)
|
|
|
|
{
|
|
|
|
fb_assert(m_next && m_value);
|
|
|
|
|
|
|
|
m_impure = CMP_impure(csb, sizeof(Impure));
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void FirstRowsStream::open(thread_db* tdbb) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
jrd_req* const request = tdbb->getRequest();
|
2010-04-05 23:20:08 +02:00
|
|
|
Impure* const impure = request->getImpure<Impure>(m_impure);
|
2009-12-09 19:45:44 +01:00
|
|
|
|
2009-12-14 15:29:20 +01:00
|
|
|
impure->irsb_flags = 0;
|
2009-12-09 19:45:44 +01:00
|
|
|
|
2010-11-21 04:47:29 +01:00
|
|
|
const dsc* desc = EVL_expr(tdbb, request, m_value);
|
2009-12-09 19:45:44 +01:00
|
|
|
const SINT64 value = (desc && !(request->req_flags & req_null)) ? MOV_get_int64(desc, 0) : 0;
|
|
|
|
|
|
|
|
if (value < 0)
|
|
|
|
status_exception::raise(Arg::Gds(isc_bad_limit_param));
|
|
|
|
|
2009-12-14 15:29:20 +01:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
impure->irsb_flags = irsb_open;
|
|
|
|
impure->irsb_count = value;
|
|
|
|
m_next->open(tdbb);
|
|
|
|
}
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void FirstRowsStream::close(thread_db* tdbb) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
jrd_req* const request = tdbb->getRequest();
|
|
|
|
|
|
|
|
invalidateRecords(request);
|
|
|
|
|
2010-04-05 23:20:08 +02:00
|
|
|
Impure* const impure = request->getImpure<Impure>(m_impure);
|
2009-12-09 19:45:44 +01:00
|
|
|
|
|
|
|
if (impure->irsb_flags & irsb_open)
|
|
|
|
{
|
|
|
|
impure->irsb_flags &= ~irsb_open;
|
|
|
|
|
|
|
|
m_next->close(tdbb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool FirstRowsStream::getRecord(thread_db* tdbb) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2012-12-17 18:33:45 +01:00
|
|
|
if (--tdbb->tdbb_quantum < 0)
|
|
|
|
JRD_reschedule(tdbb, 0, true);
|
|
|
|
|
2009-12-09 19:45:44 +01:00
|
|
|
jrd_req* const request = tdbb->getRequest();
|
2010-04-05 23:20:08 +02:00
|
|
|
Impure* const impure = request->getImpure<Impure>(m_impure);
|
2009-12-09 19:45:44 +01:00
|
|
|
|
|
|
|
if (!(impure->irsb_flags & irsb_open))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (impure->irsb_count <= 0)
|
|
|
|
{
|
|
|
|
invalidateRecords(request);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
impure->irsb_count--;
|
|
|
|
|
2009-12-10 13:45:16 +01:00
|
|
|
return m_next->getRecord(tdbb);
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool FirstRowsStream::refetchRecord(thread_db* tdbb) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
return m_next->refetchRecord(tdbb);
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool FirstRowsStream::lockRecord(thread_db* tdbb) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
return m_next->lockRecord(tdbb);
|
|
|
|
}
|
|
|
|
|
2011-02-02 12:31:04 +01:00
|
|
|
void FirstRowsStream::print(thread_db* tdbb, string& plan, bool detailed, unsigned level) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2011-02-02 12:31:04 +01:00
|
|
|
if (detailed)
|
|
|
|
plan += printIndent(++level) + "First N Records";
|
2009-12-09 19:45:44 +01:00
|
|
|
|
2011-02-02 12:31:04 +01:00
|
|
|
m_next->print(tdbb, plan, detailed, level);
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FirstRowsStream::markRecursive()
|
|
|
|
{
|
|
|
|
m_next->markRecursive();
|
|
|
|
}
|
|
|
|
|
2011-12-14 07:45:24 +01:00
|
|
|
void FirstRowsStream::findUsedStreams(StreamList& streams, bool expandAll) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
2011-12-14 07:45:24 +01:00
|
|
|
m_next->findUsedStreams(streams, expandAll);
|
2009-12-09 19:45:44 +01:00
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void FirstRowsStream::invalidateRecords(jrd_req* request) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
m_next->invalidateRecords(request);
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void FirstRowsStream::nullRecords(thread_db* tdbb) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
m_next->nullRecords(tdbb);
|
|
|
|
}
|