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 Vlad Khorsun
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Vlad Khorsun <hvlad@users.sourceforge.net>
|
|
|
|
* and all contributors signed below.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "firebird.h"
|
|
|
|
#include "../jrd/common.h"
|
|
|
|
#include "../jrd/jrd.h"
|
|
|
|
#include "../jrd/req.h"
|
|
|
|
#include "../jrd/cmp_proto.h"
|
|
|
|
#include "../jrd/exe_proto.h"
|
|
|
|
#include "../jrd/vio_proto.h"
|
|
|
|
|
|
|
|
#include "RecordSource.h"
|
|
|
|
|
|
|
|
using namespace Firebird;
|
|
|
|
using namespace Jrd;
|
|
|
|
|
|
|
|
// ----------------------------
|
|
|
|
// Data access: recursive union
|
|
|
|
// ----------------------------
|
|
|
|
|
|
|
|
RecursiveStream::RecursiveStream(CompilerScratch* csb, UCHAR stream, UCHAR mapStream,
|
|
|
|
RecordSource* root, RecordSource* inner,
|
2010-10-12 10:02:57 +02:00
|
|
|
jrd_nod* rootMap, jrd_nod* innerMap,
|
2009-12-09 19:45:44 +01:00
|
|
|
size_t streamCount, const UCHAR* innerStreams,
|
2010-01-22 09:23:36 +01:00
|
|
|
size_t saveOffset)
|
2009-12-09 19:45:44 +01:00
|
|
|
: RecordStream(csb, stream),
|
|
|
|
m_mapStream(mapStream),
|
2009-12-13 11:42:55 +01:00
|
|
|
m_root(root), m_inner(inner),
|
2009-12-09 19:45:44 +01:00
|
|
|
m_rootMap(rootMap), m_innerMap(innerMap),
|
2010-01-22 09:23:36 +01:00
|
|
|
m_innerStreams(csb->csb_pool),
|
|
|
|
m_saveOffset(saveOffset)
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
fb_assert(m_root && m_inner && m_rootMap && m_innerMap);
|
|
|
|
|
|
|
|
m_impure = CMP_impure(csb, sizeof(Impure));
|
2010-01-22 09:23:36 +01:00
|
|
|
m_saveSize = csb->csb_impure - saveOffset;
|
2009-12-09 19:45:44 +01:00
|
|
|
|
|
|
|
m_innerStreams.resize(streamCount);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < streamCount; i++)
|
|
|
|
{
|
|
|
|
m_innerStreams[i] = innerStreams[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
m_root->markRecursive();
|
|
|
|
m_inner->markRecursive();
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void RecursiveStream::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
|
|
|
|
|
|
|
impure->irsb_flags = irsb_open;
|
|
|
|
|
|
|
|
VIO_record(tdbb, &request->req_rpb[m_stream], m_format, tdbb->getDefaultPool());
|
|
|
|
VIO_record(tdbb, &request->req_rpb[m_mapStream], m_format, tdbb->getDefaultPool());
|
|
|
|
|
|
|
|
// Set up our instance data
|
|
|
|
|
|
|
|
impure->irsb_mode = ROOT;
|
|
|
|
impure->irsb_level = 1;
|
|
|
|
impure->irsb_stack = NULL;
|
|
|
|
impure->irsb_data = NULL;
|
|
|
|
|
|
|
|
// Initialize the record number of each stream in the union
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_innerStreams.getCount(); i++)
|
|
|
|
{
|
|
|
|
const UCHAR stream = m_innerStreams[i];
|
|
|
|
request->req_rpb[stream].rpb_number.setValue(BOF_NUMBER);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_root->open(tdbb);
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void RecursiveStream::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);
|
|
|
|
Impure* const saveImpure = request->getImpure<Impure>(m_saveOffset);
|
2009-12-09 19:45:44 +01:00
|
|
|
|
|
|
|
if (impure->irsb_flags & irsb_open)
|
|
|
|
{
|
|
|
|
impure->irsb_flags &= ~irsb_open;
|
|
|
|
|
|
|
|
while (impure->irsb_level > 1)
|
|
|
|
{
|
|
|
|
m_inner->close(tdbb);
|
|
|
|
|
|
|
|
delete[] impure->irsb_data;
|
|
|
|
|
|
|
|
UCHAR* const tmp = impure->irsb_stack;
|
2010-01-22 09:23:36 +01:00
|
|
|
memcpy(saveImpure, tmp, m_saveSize);
|
2009-12-09 19:45:44 +01:00
|
|
|
delete[] tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_root->close(tdbb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
bool RecursiveStream::getRecord(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);
|
|
|
|
Impure* const saveImpure = request->getImpure<Impure>(m_saveOffset);
|
2009-12-09 19:45:44 +01:00
|
|
|
|
|
|
|
if (!(impure->irsb_flags & irsb_open))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t rpbsSize = sizeof(record_param) * m_innerStreams.getCount();
|
|
|
|
Record* const record = request->req_rpb[m_stream].rpb_record;
|
|
|
|
Record* const mapRecord = request->req_rpb[m_mapStream].rpb_record;
|
|
|
|
|
2010-08-09 17:48:51 +02:00
|
|
|
const RecordSource* rsb;
|
2009-12-09 19:45:44 +01:00
|
|
|
|
|
|
|
switch (impure->irsb_mode)
|
|
|
|
{
|
|
|
|
case ROOT:
|
|
|
|
rsb = m_root;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RECURSE:
|
|
|
|
{
|
|
|
|
// Stop infinite recursion of bad queries
|
|
|
|
if (impure->irsb_level > MAX_RECURSE_LEVEL)
|
|
|
|
{
|
|
|
|
status_exception::raise(Arg::Gds(isc_req_max_clones_exceeded));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save where we are
|
2010-01-22 09:23:36 +01:00
|
|
|
UCHAR* const tmp = FB_NEW(*tdbb->getDefaultPool()) UCHAR[m_saveSize + rpbsSize];
|
|
|
|
memcpy(tmp, saveImpure, m_saveSize);
|
2009-12-09 19:45:44 +01:00
|
|
|
|
2010-01-22 09:23:36 +01:00
|
|
|
UCHAR* p = tmp + m_saveSize;
|
2009-12-09 19:45:44 +01:00
|
|
|
for (size_t i = 0; i < m_innerStreams.getCount(); i++)
|
|
|
|
{
|
|
|
|
const UCHAR stream = m_innerStreams[i];
|
|
|
|
record_param* const rpb = &request->req_rpb[stream];
|
|
|
|
memmove(p, rpb, sizeof(record_param));
|
|
|
|
p += sizeof(record_param);
|
|
|
|
|
|
|
|
// Don't overwrite record contents at next level of recursion.
|
|
|
|
// RSE_open() and company will allocate new record if needed.
|
|
|
|
rpb->rpb_record = NULL;
|
|
|
|
}
|
|
|
|
impure->irsb_stack = tmp;
|
|
|
|
|
|
|
|
impure->irsb_data = FB_NEW(*request->req_pool) UCHAR[record->rec_length];
|
|
|
|
memcpy(impure->irsb_data, record->rec_data, record->rec_length);
|
|
|
|
|
|
|
|
const Impure r = *impure;
|
2010-01-22 09:23:36 +01:00
|
|
|
memset(saveImpure, 0, m_saveSize);
|
2009-12-09 19:45:44 +01:00
|
|
|
*impure = r;
|
|
|
|
|
|
|
|
// (Re-)Open a new child stream & reset record number
|
|
|
|
m_inner->open(tdbb);
|
|
|
|
rsb = m_inner;
|
|
|
|
|
|
|
|
impure->irsb_level++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fb_assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the data -- if there is none go back one level and when
|
|
|
|
// there isn't a previous level, we're done
|
|
|
|
while (!rsb->getRecord(tdbb))
|
|
|
|
{
|
|
|
|
if (impure->irsb_level == 1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rsb->close(tdbb);
|
|
|
|
delete[] impure->irsb_data;
|
|
|
|
|
|
|
|
UCHAR* const tmp = impure->irsb_stack;
|
2010-01-22 09:23:36 +01:00
|
|
|
memcpy(saveImpure, tmp, m_saveSize);
|
2009-12-09 19:45:44 +01:00
|
|
|
|
2010-01-22 09:23:36 +01:00
|
|
|
UCHAR* p = tmp + m_saveSize;
|
2009-12-09 19:45:44 +01:00
|
|
|
for (size_t i = 0; i < m_innerStreams.getCount(); i++)
|
|
|
|
{
|
|
|
|
const UCHAR stream = m_innerStreams[i];
|
|
|
|
record_param* const rpb = &request->req_rpb[stream];
|
|
|
|
Record* const tempRecord = rpb->rpb_record;
|
|
|
|
memmove(rpb, p, sizeof(record_param));
|
|
|
|
p += sizeof(record_param);
|
|
|
|
|
|
|
|
// We just restored record of current recursion level, delete record
|
|
|
|
// from upper level. It may be optimized in the future if needed.
|
|
|
|
delete tempRecord;
|
|
|
|
}
|
|
|
|
delete[] tmp;
|
|
|
|
|
|
|
|
if (impure->irsb_level > 1)
|
|
|
|
{
|
|
|
|
rsb = m_inner;
|
|
|
|
|
|
|
|
// Reset our record data so that recursive WHERE clauses work
|
|
|
|
memcpy(record->rec_data, impure->irsb_data, record->rec_length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rsb = m_root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impure->irsb_mode = RECURSE;
|
|
|
|
|
|
|
|
// We've got a record, map it into the target record
|
2010-10-12 10:02:57 +02:00
|
|
|
const jrd_nod* const map = (rsb == m_root) ? m_rootMap : m_innerMap;
|
|
|
|
const jrd_nod* const* ptr = map->nod_arg;
|
|
|
|
for (const jrd_nod *const *end = ptr + map->nod_count; ptr < end; ptr++)
|
|
|
|
{
|
2009-12-09 19:45:44 +01:00
|
|
|
EXE_assignment(tdbb, *ptr);
|
2010-10-12 10:02:57 +02:00
|
|
|
}
|
2009-12-09 19:45:44 +01:00
|
|
|
|
|
|
|
// copy target (next level) record into main (current level) record
|
|
|
|
memcpy(record->rec_data, mapRecord->rec_data, record->rec_length);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-12 10:02:57 +02:00
|
|
|
bool RecursiveStream::refetchRecord(thread_db* tdbb) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-12 10:02:57 +02:00
|
|
|
bool RecursiveStream::lockRecord(thread_db* tdbb) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
status_exception::raise(Arg::Gds(isc_record_lock_not_supp));
|
|
|
|
return false; // compiler silencer
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void RecursiveStream::dump(thread_db* tdbb, UCharBuffer& buffer) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
buffer.add(isc_info_rsb_begin);
|
|
|
|
|
|
|
|
buffer.add(isc_info_rsb_type);
|
2009-12-13 22:16:29 +01:00
|
|
|
buffer.add(isc_info_rsb_recursive);
|
2009-12-09 19:45:44 +01:00
|
|
|
|
|
|
|
buffer.add(2);
|
|
|
|
m_root->dump(tdbb, buffer);
|
|
|
|
m_inner->dump(tdbb, buffer);
|
|
|
|
|
|
|
|
buffer.add(isc_info_rsb_end);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecursiveStream::markRecursive()
|
|
|
|
{
|
|
|
|
m_root->markRecursive();
|
|
|
|
m_inner->markRecursive();
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:37:35 +02:00
|
|
|
void RecursiveStream::invalidateRecords(jrd_req* request) const
|
2009-12-09 19:45:44 +01:00
|
|
|
{
|
|
|
|
m_root->invalidateRecords(request);
|
|
|
|
m_inner->invalidateRecords(request);
|
|
|
|
}
|