function queryRecordSet( $recordSetStructureId, QueryTransactionInformation $transactionInformation, Attribute $keyAttribute, TableColumnsToAttributesMapping $tableColumnsToAttributeMapping, Table $table, array $restrictions, array $orderBy = array(), $count = - 1, $offset = 0 ) {
	$dbr = wfGetDB( DB_SLAVE );
	
	$selectFields =  $tableColumnsToAttributeMapping->getSelectColumns();
	$attributes = $tableColumnsToAttributeMapping->getAttributes();

	if ( $table->isVersioned ) {
		$allAttributes = array_merge( $attributes, $transactionInformation->versioningAttributes() );
	} else {
		$allAttributes = $attributes;
	}
	
	$query = getTransactedSQL( $transactionInformation, $selectFields, $table, $restrictions, $orderBy, $count, $offset );
	$queryResult = $dbr->query( $query );
	
	if ( !is_null( $recordSetStructureId ) ) {
		$structure = new Structure( $recordSetStructureId, $allAttributes );
	} else {
		$structure = new Structure( $allAttributes );
	}

	$recordSet = new ArrayRecordSet( $structure, new Structure( $keyAttribute ) );

	while ( $row = $dbr->fetchRow( $queryResult ) ) {
		$record = new ArrayRecord( $structure );
		$columnIndex = 0;

		for ( $i = 0; $i < $tableColumnsToAttributeMapping->getCount(); $i++ ) {
			$mapping = $tableColumnsToAttributeMapping->getMapping( $i );
			$attribute = $mapping->getAttribute();
			$tableColumns = $mapping->getTableColumns();
			
			if ( count( $tableColumns ) == 1 ) {
				$value = $row[$columnIndex];
			} else {
				$value = getRecordFromRow( $row, $columnIndex, $attribute->type );
			}
			
			$record->setAttributeValue( $attribute, $value );
			$columnIndex += count( $tableColumns );
		}
			
		$transactionInformation->setVersioningAttributes( $record, $row );
		$recordSet->add( $record );
	}
		
	return $recordSet;
}
function splitRecordSet( $recordSet, $groupAttribute ) {
	$result = array();
	$structure = $recordSet->getStructure();
	$key = $recordSet->getKey();
	
	for ( $i = 0; $i < $recordSet->getRecordCount(); $i++ ) {
		$record = $recordSet->getRecord( $i );
		$groupAttributeValue = $record->getAttributeValue( $groupAttribute );
		@$groupRecordSet = $result[$groupAttributeValue]; # FIXME - check existence in array

		if ( $groupRecordSet == null ) {
			$groupRecordSet = new ArrayRecordSet( $structure, $key );
			$result[$groupAttributeValue] = $groupRecordSet;
		}
		
		$groupRecordSet->add( $record );
	}
	
	return $result;
}
function getUpdatedTranslatedTextRecordSet( $transactionId ) {

	$o = OmegaWikiAttributes::getInstance();

	$dc = wdGetDataSetContext();
	$dbr = wfGetDB( DB_SLAVE );
	$queryResult = $dbr->query(
		"SELECT value_id, object_id, attribute_mid, translated_content_id, language_id, text_text, " .
			getOperationSelectColumn( "{$dc}_translated_content", $transactionId ) . ', ' .
			getIsLatestSelectColumn( "{$dc}_translated_content", array( 'translated_content_id', 'language_id' ), $transactionId ) .
		" FROM {$dc}_translated_content_attribute_values, {$dc}_translated_content, {$dc}_text " .
		" WHERE {$dc}_translated_content_attribute_values.value_tcid={$dc}_translated_content.translated_content_id " .
		" AND {$dc}_translated_content.text_id={$dc}_text.text_id " .
		" AND " . getInTransactionRestriction( "{$dc}_translated_content", $transactionId ) .
		" AND " . getAtTransactionRestriction( "{$dc}_translated_content_attribute_values", $transactionId )
	);
		
	$recordSet = new ArrayRecordSet( $o->updatedTranslatedTextStructure, new Structure( $o->valueId, $o->language ) );
	
	while ( $row = $dbr->fetchObject( $queryResult ) ) {
		$record = new ArrayRecord( $o->updatedTranslatedTextStructure );
		$record->valueId = $row->value_id;
		$record->objectId = $row->object_id;
		$record->attribute = getDefinedMeaningReferenceRecord( $row->attribute_mid );
		$record->translatedContentId = $row->translated_content_id;
		$record->language = $row->language_id;
		$record->text = $row->text_text;
		$record->operation = $row->operation;
		$record->isLatest = $row->is_latest;
		$record->rollBackTranslatedContent = simpleRecord( $o->rollBackTranslatedContentStructure, array( $row->is_latest, $row->operation, getTranslatedContentHistory( $row->translated_content_id, $row->language_id, $row->is_latest ) ) );
		$recordSet->add( $record );
	}
	
	return $recordSet;
}
 function getExternalIdentifiersSearchResultAsRecordSet($queryResult)
 {
     $dbr = wfGetDB(DB_SLAVE);
     $externalIdentifierMatchStructure = new Structure($this->externalIdentifierAttribute, $this->collectionAttribute, $this->collectionMemberAttribute);
     $recordSet = new ArrayRecordSet($externalIdentifierMatchStructure, new Structure($this->externalIdentifierAttribute));
     while ($row = $dbr->fetchObject($queryResult)) {
         $record = new ArrayRecord($this->externalIdentifierMatchStructure);
         $record->setAttributeValue($this->externalIdentifierAttribute, $row->external_identifier);
         $record->setAttributeValue($this->collectionAttribute, $row->collection_mid);
         $record->setAttributeValue($this->collectionMemberAttribute, $row->member_mid);
         $recordSet->add($record);
     }
     expandDefinedMeaningReferencesInRecordSet($recordSet, array($this->collectionAttribute, $this->collectionMemberAttribute));
     return $recordSet;
 }
function filterAttributeValues( RecordSet $sourceRecordSet, Attribute $attributeAttribute, array &$attributeIds ) {
	$result = new ArrayRecordSet( $sourceRecordSet->getStructure(), $sourceRecordSet->getKey() );
	$i = 0;
	
	while ( $i < $sourceRecordSet->getRecordCount() ) {
		$record = $sourceRecordSet->getRecord( $i );
		
		if ( in_array( $record->getAttributeValue( $attributeAttribute )->definedMeaningId, $attributeIds ) ) {
			$result->add( $record );
			$sourceRecordSet->remove( $i );
		}
		else
			$i++;
	}
	
	return $result;
}