CombineResultSets() public static method

Combines the first and second result sets, if and only if, the columns of both result sets match.
public static CombineResultSets ( ResultSet $first, ResultSet $second )
$first ResultSet
$second ResultSet
Ejemplo n.º 1
0
/**
 * Fetches a match table from a PQL statement and writes it to a file.
 */
function fetchMatchTable($statementBuilder, $pqlService, $fileName)
{
    $resultSet = null;
    $combinedResultSet = null;
    do {
        $resultSet = $pqlService->select($statementBuilder->ToStatement());
        // Combine result sets with previous ones.
        $combinedResultSet = !isset($combinedResultSet) ? $resultSet : Pql::CombineResultSets($combinedResultSet, $resultSet);
        $statementBuilder->IncreaseOffsetBy(StatementBuilder::SUGGESTED_PAGE_LIMIT);
    } while (isset($resultSet->rows) && count($resultSet->rows) > 0);
    // Change to your file location.
    $filePath = sprintf("%s/%s-%s.csv", sys_get_temp_dir(), $fileName, uniqid());
    $fp = fopen($filePath, 'w');
    // Write the result set to a CSV.
    fputcsv($fp, Pql::GetColumnLabels($combinedResultSet));
    foreach ($combinedResultSet->rows as $row) {
        fputcsv($fp, Pql::GetRowStringValues($row));
    }
    fclose($fp);
    return $filePath;
}
    // Log SOAP XML request and response.
    $user->LogDefaults();
    // Get the PublisherQueryLanguageService.
    $pqlService = $user->GetService('PublisherQueryLanguageService', 'v201508');
    // Create statement to select all line items.
    $statementBuilder = new StatementBuilder();
    $statementBuilder->Select('Id, BrowserName, MajorVersion, MinorVersion')->From('Browser')->OrderBy('BrowserName ASC')->Limit(StatementBuilder::SUGGESTED_PAGE_LIMIT);
    // Default for result sets.
    $resultSet = null;
    $combinedResultSet = null;
    $i = 0;
    do {
        // Get all browsers.
        $resultSet = $pqlService->select($statementBuilder->ToStatement());
        // Combine result sets with previous ones.
        $combinedResultSet = !isset($combinedResultSet) ? $resultSet : Pql::CombineResultSets($combinedResultSet, $resultSet);
        printf("%d) %d browsers beginning at offset %d were found.\n", $i++, isset($resultSet->rows) ? count($resultSet->rows) : 0, $statementBuilder->GetOffset());
        $statementBuilder->IncreaseOffsetBy(StatementBuilder::SUGGESTED_PAGE_LIMIT);
    } while (isset($resultSet->rows) && count($resultSet->rows) > 0);
    // Change to your file location.
    $filePath = sprintf("%s/Browsers-%s.csv", sys_get_temp_dir(), uniqid());
    $fp = fopen($filePath, 'w');
    // Write the result set to a CSV.
    fputcsv($fp, Pql::GetColumnLabels($combinedResultSet));
    foreach ($combinedResultSet->rows as $row) {
        fputcsv($fp, Pql::GetRowStringValues($row));
    }
    fclose($fp);
    printf("Browsers saved to %s\n", $filePath);
} catch (OAuth2Exception $e) {
    ExampleUtils::CheckForOAuth2Errors($e);
    $user = new DfpUser();
    // Log SOAP XML request and response.
    $user->LogDefaults();
    $pqlService = $user->GetService('PublisherQueryLanguageService', 'v201608');
    // Create statement to select all programmatic buyers.
    $statementBuilder = new StatementBuilder();
    $statementBuilder->Select('BuyerAccountId, Name')->From('Programmatic_Buyer')->OrderBy('BuyerAccountId ASC')->Limit(StatementBuilder::SUGGESTED_PAGE_LIMIT);
    // Retrieve a small amount of rows at a time, paging through until all rows
    // have been retrieved.
    $resultSet = null;
    $combinedResultSet = null;
    $i = 0;
    do {
        $resultSet = $pqlService->select($statementBuilder->ToStatement());
        // Combine result sets with previous ones.
        $combinedResultSet = $combinedResultSet !== null ? $resultSet : Pql::CombineResultSets($combinedResultSet, $resultSet);
        printf("%d) %d programmatic buyers beginning at offset %d were found.\n", $i++, $resultSet->rows !== null ? count($resultSet->rows) : 0, $statementBuilder->GetOffset());
        $statementBuilder->IncreaseOffsetBy(StatementBuilder::SUGGESTED_PAGE_LIMIT);
    } while ($resultSet->rows !== null && count($resultSet->rows) > 0);
    // Change to another file location if desired.
    $filePath = sprintf("%s/programmatic-buyers-%s.csv", sys_get_temp_dir(), uniqid());
    $fp = fopen($filePath, 'w');
    // Write the result set to a CSV.
    fputcsv($fp, Pql::GetColumnLabels($combinedResultSet));
    foreach ($combinedResultSet->rows as $row) {
        fputcsv($fp, Pql::GetRowStringValues($row));
    }
    fclose($fp);
    printf("Programmatic buyers saved to %s\n", $filePath);
} catch (OAuth2Exception $e) {
    ExampleUtils::CheckForOAuth2Errors($e);
 /**
  * @covers Pql::CombineResultSets
  * @expectedException InvalidArgumentException
  */
 public function testCombineResultSetWithBadColumnsThrowsException()
 {
     $row1 = new Row(array($this->textValue1, $this->booleanValue1, $this->numberValue1));
     $row2 = new Row(array($this->textValue2, $this->booleanValue2, $this->numberValue2));
     $row3 = new Row(array($this->textValue3, $this->booleanValue3));
     $resultSet1 = new ResultSet(array($this->column1, $this->column2, $this->column3), array($row1, $row2));
     $resultSet2 = new ResultSet(array($this->column1, $this->column2), array($row3));
     Pql::CombineResultSets($resultSet1, $resultSet2);
 }