Beispiel #1
0
/*#
   @function set Dictionary
   @brief Stores a value in a dictionary
   @param key The key to be found.
   @param value The key to be set.
   @return True if the value was overwritten, false if it has been inserted anew.
   
   @note This method bypassess setIndex__ override in blessed (POOP) dictionaries.

   @see oob
*/
FALCON_FUNC  mth_dictSet( ::Falcon::VMachine *vm )
{
   Item *i_dict, *i_key, *i_value;
   
   if( vm->self().isMethodic() )
   {
      i_dict = &vm->self();
      i_key = vm->param(0);
      i_value = vm->param(1);
   }
   else {
      i_dict = vm->param(0);
      i_key = vm->param(1);
      i_value = vm->param(2);
   }
   
   if( i_dict == 0  || ! i_dict->isDict() || i_key == 0 || i_value == 0 ) 
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
            .origin( e_orig_runtime )
            .extra( vm->self().isMethodic() ? "X,X" : "D,X,X" ) );
   }

   CoreDict *dict = i_dict->asDict();
   Item *value = dict->find( *i_key );
   if ( value == 0 )
   {
      vm->regA().setBoolean( false );
      dict->put( *i_key, *i_value );
   }
   else {
      vm->regA().setBoolean( true );
      *value = *i_value;
   }
}
Beispiel #2
0
/*#
   @method get Dictionary
   @brief Retreives a value associated with the given key
   @param key The key to be found.
   @return The value associated with a key, or an out-of-band nil if not found.

   Return the value associated with the key, if present, or one of the
   values if more than one key matching the given one is present. If
   not present, the value returned will be nil. Notice that nil may be also
   returned if the value associated with a given key is exactly nil. In
   case the key cannot be found, the returned value will be marked as OOB.
   
   @note This method bypassess getIndex__ override in blessed (POOP) dictionaries.

   @see oob
*/
FALCON_FUNC  mth_dictGet( ::Falcon::VMachine *vm )
{
   Item *i_dict, *i_key;
   
   if( vm->self().isMethodic() )
   {
      i_dict = &vm->self();
      i_key = vm->param(0);
   }
   else {
      i_dict = vm->param(0);
      i_key = vm->param(1);
   }
   
   if( i_dict == 0  || ! i_dict->isDict() || i_key == 0 ) 
   {
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
            .origin( e_orig_runtime )
            .extra( vm->self().isMethodic() ? "X" : "D,X" ) );
   }

   CoreDict *dict = i_dict->asDict();
   Item *value = dict->find( *i_key );
   if ( value == 0 )
   {
      vm->retnil();
      vm->regA().setOob();
   }
   else
      vm->retval( *value );
}
Beispiel #3
0
static void internal_record_fetch( VMachine* vm, DBIRecordset* dbr, Item& target )
{
   int count = dbr->getColumnCount();

   if( target.isArray() )
   {
      CoreArray* aret = target.asArray();
      aret->resize( count );
      for ( int i = 0; i < count; i++ )
      {
         dbr->getColumnValue( i, aret->items()[i] );
      }
      vm->retval( aret );
   }
   else if( target.isDict() )
   {
      CoreDict* dret = target.asDict();
      for ( int i = 0; i < count; i++ )
      {
         String fieldName;
         dbr->getColumnName( i, fieldName );
         Item* value = dret->find( Item(&fieldName) );
         if( value == 0 )
         {
            Item v;
            dbr->getColumnValue( i, v );
            CoreString* key = new CoreString(fieldName);
            key->bufferize();
            dret->put( key, v );
         }
         else
         {
            dbr->getColumnValue( i, *value );
         }
      }
      vm->retval( dret );
   }
   /*
   else
   {
      CoreTable* tbl = dyncast<CoreTable*>(target.asObject()->getFalconData());
      ItemArray iaCols( count );

      if( tbl->order() == CoreTable::noitem )
      {
         String* fieldName = new String[count];
         for( int i = 0; i < count; ++ i )
         {
            dbr->getColumnName( i, fieldName[i] );
            iaCols.append( fieldName );
         }

         if( ! tbl->setHeader( iaCols ) )
         {
            delete[] fieldName;
            throw new DBIError( ErrorParam( FALCON_DBI_ERROR_FETCH, __LINE__ )
                  .extra("Incompatible table columns" ) );
         }

         delete[] fieldName;
      }
      else
      {
         if( tbl->order() != (unsigned) count )
         {
            throw new DBIError( ErrorParam( FALCON_DBI_ERROR_FETCH, __LINE__ )
                              .extra("Incompatible table columns" ) );
         }
      }

      // put in the values
      do {
         CoreArray* row = new CoreArray();
         row->resize( count );

         for( int i = 0; i < count; ++ i )
         {
            dbr->getColumnValue( i, row->at( i ) );
         }
         tbl->insertRow( row );
      }
      while( dbr->fetchRow() );

      vm->retval( target );
   }
   */
}