/// <summary>
    /// Run the code example.
    /// </summary>
    /// <param name="user">The DFP user object running the code example.</param>
    public override void Run(DfpUser user) {
      // Get the InventoryService.
      PublisherQueryLanguageService pqlService =
          (PublisherQueryLanguageService) user.GetService(
              DfpService.v201306.PublisherQueryLanguageService);

      int pageSize = 500;
      // Create statement to select all targetable cities.
      String selectStatement = "SELECT * FROM City WHERE targetable = true LIMIT " + pageSize;
      int offset = 0;
      int resultSetSize = 0;
      List<Row> allRows = new List<Row>();
      ResultSet resultSet;

      try {
        do {
          // For criteria that do not have a "targetable" property, that predicate
          // may be left off, i.e. just "SELECT * FROM Browser_Groups LIMIT 500"
          StatementBuilder statementBuilder =
              new StatementBuilder(selectStatement + " OFFSET " + offset);

          // Get all cities.
          resultSet = pqlService.select(statementBuilder.ToStatement());

          // Collect all cities from each page.
          allRows.AddRange(resultSet.rows);

          // Display results.
          Console.WriteLine(PqlUtilities.ResultSetToString(resultSet));

          offset += pageSize;
          resultSetSize = resultSet.rows == null ? 0 : resultSet.rows.Length;
        } while (resultSetSize == pageSize);

        Console.WriteLine("Number of results found: " + allRows.Count);

        // Optionally, save all rows to a CSV.
        // Get a string array representation of the data rows.
        resultSet.rows = allRows.ToArray();
        List<String[]> rows = PqlUtilities.ResultSetToStringArrayList(resultSet);

        // Write the contents to a csv file.
        CsvFile file = new CsvFile();
        file.Headers.AddRange(rows[0]);
        file.Records.AddRange(rows.GetRange(1, rows.Count - 1).ToArray());
        file.Write("cities.csv");
      } catch (Exception ex) {
        Console.WriteLine("Failed to get all cities. Exception says \"{0}\"", ex.Message);
      }
    }
    public void TestAllStatementBuilderPartialFunctions() {
      StatementBuilder statementBuilder = new StatementBuilder()
          .Select("Name, Id")
          .From("Geo_Target")
          .Where("Targetable = :targetable")
          .OrderBy("Id DESC")
          .Limit(StatementBuilder.SUGGESTED_PAGE_LIMIT)
          .Offset(0)
          .AddValue("targetable", true)
          .IncreaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT)
          .RemoveLimitAndOffset();
      Assert.AreEqual(null, statementBuilder.GetOffset());

      Statement expectedStatement = new Statement();
      expectedStatement.query = "SELECT Name, Id FROM Geo_Target"
          + " WHERE Targetable = :targetable ORDER BY Id DESC";
      String_ValueMapEntry targetableEntry = new String_ValueMapEntry();
      targetableEntry.key = "targetable";
      BooleanValue targetableValue = new BooleanValue();
      targetableValue.value = true;
      targetableEntry.value = targetableValue;
      expectedStatement.values = new String_ValueMapEntry[] {targetableEntry};
      Assert.True(StatementsAreEqual(expectedStatement, statementBuilder.ToStatement()));
    }
    public void TestStatementBuilderBasicStatement() {
      StatementBuilder statementBuilder = new StatementBuilder()
          .Where("ID = 1");

      Statement expectedStatement = new Statement();
      expectedStatement.query = "WHERE ID = 1";
      expectedStatement.values = new String_ValueMapEntry[0];

      Assert.True(StatementsAreEqual(expectedStatement,
                                     statementBuilder.ToStatement()));
    }
    public void TestStatementBuilderIncludesKeywordPrefix() {
      StatementBuilder statementBuilder = new StatementBuilder()
          .Where("WHEREWITHALL = 1");

      Statement expectedStatement = new Statement();
      expectedStatement.query = "WHERE WHEREWITHALL = 1";
      expectedStatement.values = new String_ValueMapEntry[0];

      Assert.True(StatementsAreEqual(expectedStatement,
        statementBuilder.ToStatement()));
    }
    public void TestStatementBuilderBlankStatement() {
      StatementBuilder statementBuilder = new StatementBuilder();

      Statement expectedStatement = new Statement();
      expectedStatement.query = "";
      expectedStatement.values = new String_ValueMapEntry[0];

      Assert.True(StatementsAreEqual(expectedStatement,
                                     statementBuilder.ToStatement()));
    }