<commit_before>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "stdafx.h"

#include "diagnostics/assert.h"
#include "diagnostics/util_macros.h"

#include "system/globalenv.h"

#include "context/dynamic_context.h"

#include "runtime/api/plan_iterator_wrapper.h"
#include "runtime/base/noarybase.h"
#include "runtime/core/var_iterators.h"
#include "runtime/util/iterator_impl.h"
#include "runtime/visitors/planiter_visitor.h"

#include "store/api/iterator.h"
#include "store/api/iterator_factory.h"
#include "store/api/item.h"
#include "store/api/item_factory.h"
#include "store/api/temp_seq.h"
#include "store/api/store.h"

#include "util/string_util.h"

namespace zorba
{

SERIALIZABLE_CLASS_VERSIONS(CtxVarIterator)

SERIALIZABLE_CLASS_VERSIONS(CtxVarDeclareIterator)

SERIALIZABLE_CLASS_VERSIONS(CtxVarAssignIterator)

SERIALIZABLE_CLASS_VERSIONS(CtxVarIsSetIterator)

SERIALIZABLE_CLASS_VERSIONS(ForVarIterator)

SERIALIZABLE_CLASS_VERSIONS(LetVarIterator)


/////////////////////////////////////////////////////////////////////////////////
//                                                                             //
//  Prolog and Block-Local Var Declaration                                     //
//                                                                             //
/////////////////////////////////////////////////////////////////////////////////

CtxVarDeclareIterator::CtxVarDeclareIterator(
    static_context* sctx,
    const QueryLoc& loc,
    std::vector<PlanIter_t>& args,
    ulong varid,
    const store::Item_t& varName,
    bool isExtern,
    bool isLocal,
    bool singleItem)
  :
  NaryBaseIterator<CtxVarDeclareIterator, PlanIteratorState>(sctx, loc, args),
  theVarId(varid),
  theVarName(varName),
  theIsExternal(isExtern),
  theIsLocal(isLocal),
  theSingleItem(singleItem)
{
}


void CtxVarDeclareIterator::serialize(::zorba::serialization::Archiver& ar)
{
  serialize_baseclass(ar,
  (NaryBaseIterator<CtxVarDeclareIterator, PlanIteratorState>*)this);

  ar & theVarId;
  ar & theVarName;
  ar & theIsExternal;
  ar & theIsLocal;
  ar & theSingleItem;
}


bool CtxVarDeclareIterator::nextImpl(store::Item_t& result, PlanState& planState) const
{
  dynamic_context* dctx = planState.theLocalDynCtx;

  PlanIteratorState* state;
  DEFAULT_STACK_INIT(PlanIteratorState, state, planState);

  if (!theIsExternal || !dctx->exists_variable(theVarId))
  {
    dctx->declare_variable(theVarId, (theIsExternal || theIsLocal));

    if (!theChildren.empty())
    {
      if (theSingleItem)
      {
        store::Item_t item;

        if (! consumeNext(item, theChildren[0], planState))
        {
          RAISE_ERROR(err::XPTY0004, loc,
          ERROR_PARAMS(ZED(VarValMustBeSingleItem_2), theVarName));
        }

        dctx->set_variable(theVarId, theVarName, loc, item);

        if (consumeNext(item, theChildren[0], planState))
        {
          RAISE_ERROR(err::XPTY0004, loc,
          ERROR_PARAMS(ZED(VarValMustBeSingleItem_2), theVarName));
        }
      }
      else
      {
        store::Iterator_t planIter = new PlanIteratorWrapper(theChildren[0], planState);

        dctx->set_variable(theVarId, theVarName, loc, planIter);
      }
    }
  }

  STACK_END(state);
}


NARY_ACCEPT(CtxVarDeclareIterator);


/////////////////////////////////////////////////////////////////////////////////
//                                                                             //
//  Prolog and Block-Local Var Initialization/Assignment                       //
//                                                                             //
/////////////////////////////////////////////////////////////////////////////////


void CtxVarAssignIterator::serialize(::zorba::serialization::Archiver& ar)
{
  serialize_baseclass(ar,
  (UnaryBaseIterator<CtxVarAssignIterator, PlanIteratorState>*)this);

  ar & theVarId;

  ar & theVarName;
  ar & theIsLocal;
  ar & theSingleItem;
}


bool CtxVarAssignIterator::nextImpl(store::Item_t& result, PlanState& planState) const
{
  store::Item_t item;

  PlanIteratorState* state;
  DEFAULT_STACK_INIT(PlanIteratorState, state, planState);

  if (theSingleItem)
  {
    if (! consumeNext(item, theChild, planState))
    {
      RAISE_ERROR(err::XPTY0004, loc,
      ERROR_PARAMS(ZED(VarValMustBeSingleItem_2), theVarName));
    }

    if (theIsLocal)
      planState.theLocalDynCtx->set_variable(theVarId, theVarName, loc, item);
    else
      planState.theGlobalDynCtx->set_variable(theVarId, theVarName, loc, item);

    if (consumeNext(item, theChild, planState))
    {
      RAISE_ERROR(err::XPTY0004, loc,
      ERROR_PARAMS(ZED(VarValMustBeSingleItem_2), theVarName));
    }
  }
  else
  {
    store::Iterator_t planIter = new PlanIteratorWrapper(theChild, planState);

    if (theIsLocal)
      planState.theLocalDynCtx->set_variable(theVarId, theVarName, loc, planIter);
    else
      planState.theGlobalDynCtx->set_variable(theVarId, theVarName, loc, planIter);
  }

  STACK_END (state);
}


UNARY_ACCEPT(CtxVarAssignIterator);


/////////////////////////////////////////////////////////////////////////////////
//                                                                             //<commit_msg>fixed output of proper variable name<commit_after>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "stdafx.h"

#include "diagnostics/assert.h"
#include "diagnostics/util_macros.h"

#include "system/globalenv.h"

#include "context/dynamic_context.h"

#include "runtime/api/plan_iterator_wrapper.h"
#include "runtime/base/noarybase.h"
#include "runtime/core/var_iterators.h"
#include "runtime/util/iterator_impl.h"
#include "runtime/visitors/planiter_visitor.h"

#include "store/api/iterator.h"
#include "store/api/iterator_factory.h"
#include "store/api/item.h"
#include "store/api/item_factory.h"
#include "store/api/temp_seq.h"
#include "store/api/store.h"

#include "util/string_util.h"

namespace zorba
{

SERIALIZABLE_CLASS_VERSIONS(CtxVarIterator)

SERIALIZABLE_CLASS_VERSIONS(CtxVarDeclareIterator)

SERIALIZABLE_CLASS_VERSIONS(CtxVarAssignIterator)

SERIALIZABLE_CLASS_VERSIONS(CtxVarIsSetIterator)

SERIALIZABLE_CLASS_VERSIONS(ForVarIterator)

SERIALIZABLE_CLASS_VERSIONS(LetVarIterator)


/////////////////////////////////////////////////////////////////////////////////
//                                                                             //
//  Prolog and Block-Local Var Declaration                                     //
//                                                                             //
/////////////////////////////////////////////////////////////////////////////////

CtxVarDeclareIterator::CtxVarDeclareIterator(
    static_context* sctx,
    const QueryLoc& loc,
    std::vector<PlanIter_t>& args,
    ulong varid,
    const store::Item_t& varName,
    bool isExtern,
    bool isLocal,
    bool singleItem)
  :
  NaryBaseIterator<CtxVarDeclareIterator, PlanIteratorState>(sctx, loc, args),
  theVarId(varid),
  theVarName(varName),
  theIsExternal(isExtern),
  theIsLocal(isLocal),
  theSingleItem(singleItem)
{
}


void CtxVarDeclareIterator::serialize(::zorba::serialization::Archiver& ar)
{
  serialize_baseclass(ar,
  (NaryBaseIterator<CtxVarDeclareIterator, PlanIteratorState>*)this);

  ar & theVarId;
  ar & theVarName;
  ar & theIsExternal;
  ar & theIsLocal;
  ar & theSingleItem;
}


bool CtxVarDeclareIterator::nextImpl(store::Item_t& result, PlanState& planState) const
{
  dynamic_context* dctx = planState.theLocalDynCtx;

  PlanIteratorState* state;
  DEFAULT_STACK_INIT(PlanIteratorState, state, planState);

  if (!theIsExternal || !dctx->exists_variable(theVarId))
  {
    dctx->declare_variable(theVarId, (theIsExternal || theIsLocal));

    if (!theChildren.empty())
    {
      if (theSingleItem)
      {
        store::Item_t item;

        if (! consumeNext(item, theChildren[0], planState))
        {
          RAISE_ERROR(err::XPTY0004, loc,
          ERROR_PARAMS(ZED(VarValMustBeSingleItem_2), theVarName->getStringValue()));
        }

        dctx->set_variable(theVarId, theVarName, loc, item);

        if (consumeNext(item, theChildren[0], planState))
        {
          RAISE_ERROR(err::XPTY0004, loc,
          ERROR_PARAMS(ZED(VarValMustBeSingleItem_2), theVarName->getStringValue()));
        }
      }
      else
      {
        store::Iterator_t planIter = new PlanIteratorWrapper(theChildren[0], planState);

        dctx->set_variable(theVarId, theVarName, loc, planIter);
      }
    }
  }

  STACK_END(state);
}


NARY_ACCEPT(CtxVarDeclareIterator);


/////////////////////////////////////////////////////////////////////////////////
//                                                                             //
//  Prolog and Block-Local Var Initialization/Assignment                       //
//                                                                             //
/////////////////////////////////////////////////////////////////////////////////


void CtxVarAssignIterator::serialize(::zorba::serialization::Archiver& ar)
{
  serialize_baseclass(ar,
  (UnaryBaseIterator<CtxVarAssignIterator, PlanIteratorState>*)this);

  ar & theVarId;

  ar & theVarName;
  ar & theIsLocal;
  ar & theSingleItem;
}


bool CtxVarAssignIterator::nextImpl(store::Item_t& result, PlanState& planState) const
{
  store::Item_t item;

  PlanIteratorState* state;
  DEFAULT_STACK_INIT(PlanIteratorState, state, planState);

  if (theSingleItem)
  {
    if (! consumeNext(item, theChild, planState))
    {
      RAISE_ERROR(err::XPTY0004, loc,
      ERROR_PARAMS(ZED(VarValMustBeSingleItem_2), theVarName->getStringValue()));
    }

    if (theIsLocal)
      planState.theLocalDynCtx->set_variable(theVarId, theVarName, loc, item);
    else
      planState.theGlobalDynCtx->set_variable(theVarId, theVarName, loc, item);

    if (consumeNext(item, theChild, planState))
    {
      RAISE_ERROR(err::XPTY0004, loc,
      ERROR_PARAMS(ZED(VarValMustBeSingleItem_2), theVarName->getStringValue()));
    }
  }
  else
  {
    store::Iterator_t planIter = new PlanIteratorWrapper(theChild, planState);

    if (theIsLocal)
      planState.theLocalDynCtx->set_variable(theVarId, theVarName, loc, planIter);
    else
      planState.theGlobalDynCtx->set_variable(theVarId, theVarName, loc, planIter);
  }

  STACK_END (state);
}


UNARY_ACCEPT(CtxVarAssignIterator);


/////////////////////////////////////////////////////////////////////////////////
//                                                                             //
{'fraction_non_alphanumeric': 0.165879625654675, 'fraction_numerical': 0.00601013136429982, 'mean_word_length': 2.2682379349046013, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6796222', 'n_tokens_mistral': 3464, 'n_tokens_neox': 3383, 'n_words': 633}