<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.pdo-sqlite.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'pdo-sqlite.createaggregate.php',
    1 => 'Pdo\\Sqlite::createAggregate',
    2 => 'Registers an aggregating user-defined function for use in SQL statements',
  ),
  'up' => 
  array (
    0 => 'class.pdo-sqlite.php',
    1 => 'Pdo\\Sqlite',
  ),
  'prev' => 
  array (
    0 => 'class.pdo-sqlite.php',
    1 => 'Pdo\\Sqlite',
  ),
  'next' => 
  array (
    0 => 'pdo-sqlite.createcollation.php',
    1 => 'Pdo\\Sqlite::createCollation',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pdo_sqlite/pdo/sqlite/createaggregate.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="pdo-sqlite.createaggregate" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Pdo\Sqlite::createAggregate</h1>
  <p class="verinfo">(PHP 8 &gt;= 8.4.0)</p><p class="refpurpose"><span class="refname">Pdo\Sqlite::createAggregate</span> &mdash; <span class="dc-title">
   Registers an aggregating user-defined function for use in SQL statements
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-pdo-sqlite.createaggregate-description">
  <h3 class="title">Descrizione</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Pdo\Sqlite::createAggregate</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$name</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$step</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$finalize</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$numArgs</code><span class="initializer"> = -1</span></span><br>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="simpara">
   This method is similar to <span class="methodname"><a href="pdo-sqlite.createfunction.php" class="methodname">Pdo\Sqlite::createFunction()</a></span>
   except that it registers functions that can be used to calculate a
   result aggregated across all the rows of a query.
  </p>
  <p class="simpara">
   The key difference between this method and
   <span class="methodname"><a href="pdo-sqlite.createfunction.php" class="methodname">Pdo\Sqlite::createFunction()</a></span>
   is that two functions are required to manage the aggregate.
  </p>
  <div class="tip"><strong class="tip">Suggerimento</strong>
   <p class="simpara">
    By using this method it is possible to override native SQL functions.
   </p>
  </div>
 </div>


 <div class="refsect1 parameters" id="refsect1-pdo-sqlite.createaggregate-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <dl>
   
    <dt><code class="parameter">name</code></dt>
    <dd>
     <span class="simpara">
      The name of the function used in SQL statements.
     </span>
    </dd>
   
   
    <dt><code class="parameter">step</code></dt>
    <dd>
     <span class="simpara">
      Callback function called for each row of the result set.
      The callback should accumulate the result and store it in the aggregation context.
     </span>
     <p class="para">
      This function need to be defined as:
      <div class="methodsynopsis dc-description">
       <span class="methodname"><span class="replaceable">step</span></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$context</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$rownumber</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">...$values</code></span><br>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

      <dl>
       
        <dt><code class="parameter">context</code></dt>
        <dd>
         <span class="simpara">
          <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> for the first row; on subsequent rows it will have the value
          that was previously returned from the step function; you should use
          this to maintain the aggregate state.
         </span>
        </dd>
       
       
        <dt><code class="parameter">rownumber</code></dt>
        <dd>
         <span class="simpara">
          The current row number.
         </span>
        </dd>
       
       
        <dt><code class="parameter">value</code></dt>
        <dd>
         <span class="simpara">
          The first argument passed to the aggregate.
         </span>
        </dd>
       
       
        <dt><code class="parameter">values</code></dt>
        <dd>
         <span class="simpara">
          Further arguments passed to the aggregate.
         </span>
        </dd>
       
      </dl>
      The return value of this function will be used as the
      <code class="parameter">context</code> argument in the next call of the step or
      finalize functions.
     </p>
    </dd>
   
   
    <dt><code class="parameter">finalize</code></dt>
    <dd>
     <span class="simpara">
      Callback function to aggregate the &quot;stepped&quot; data from each row.
      Once all the rows have been processed, this function will be called,
      and it should then take the data from the aggregation context and return the result.
      This callback function should return a type understood by SQLite
      (i.e. <a href="language.types.intro.php" class="link">scalar type</a>).
     </span>
     <p class="para">
      This function need to be defined as:
      <div class="methodsynopsis dc-description">
       <span class="methodname"><span class="replaceable">fini</span></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$context</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$rowcount</code></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

      <dl>
       
        <dt><code class="parameter">context</code></dt>
        <dd>
         <p class="para">
          Holds the return value from the very last call to the step function.
         </p>
        </dd>
       
       
        <dt><code class="parameter">rowcount</code></dt>
        <dd>
         <p class="para">
          Holds the number of rows over which the aggregate was performed.
         </p>
        </dd>
       
      </dl>
      The return value of this function will be used as the return value for
      the aggregate.
     </p>
    </dd>
   
   
    <dt><code class="parameter">numArgs</code></dt>
    <dd>
     <span class="simpara">
      Hint to the SQLite parser if the callback function accepts a
      predetermined number of arguments.
     </span>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdo-sqlite.createaggregate-returnvalues">
  <h3 class="title">Valori restituiti</h3>
  <p class="simpara">
   Restituisce <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> in caso di successo, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> in caso di fallimento.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdo-sqlite.createaggregate-examples">
  <h3 class="title">Esempi</h3>
  <div class="example" id="example-1">
   <p><strong>Example #1 <span class="methodname"><strong>Pdo\Sqlite::createAggregate()</strong></span> example</strong></p>
   <div class="example-contents"><p>
    In this example we will create a custom aggregate function named
    <code class="literal">max_length</code> that can be used in SQL queries.
   </p></div>
   <div class="example-contents"><p>
    In this example, we are creating an aggregating function,
    named <code class="literal">max_length</code>, that will calculate the length
    of the longest string in one of the columns of the table.
    For each row, the <code class="literal">max_len_step</code> function is called and
    passed a <code class="literal">$context</code> parameter.
    The context parameter is just like any other PHP variable and be set to
    hold an <span class="type"><a href="language.types.array.php" class="type array">array</a></span> or even an <span class="type"><a href="language.types.object.php" class="type object">object</a></span>.
    In this example, we are using it to hold the maximum length we have seen so far;
    if the <var class="varname">$string</var> has a length longer than the current maximum,
    we update the context to hold this new maximum length.
   </p></div>
   <div class="example-contents"><p>
    After all the rows have been processed,
    SQLite calls the <code class="literal">max_len_finalize</code> function to determine
    the aggregate result.
    It is possible to perform some kind of calculation based on the data in <code class="literal">$context</code>.
    In this basic example the result was calculated as the query progressed,
    thus so the context value can be directly returned.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$data </span><span style="color: #007700">= [<br />   </span><span style="color: #DD0000">'one'</span><span style="color: #007700">,<br />   </span><span style="color: #DD0000">'two'</span><span style="color: #007700">,<br />   </span><span style="color: #DD0000">'three'</span><span style="color: #007700">,<br />   </span><span style="color: #DD0000">'four'</span><span style="color: #007700">,<br />   </span><span style="color: #DD0000">'five'</span><span style="color: #007700">,<br />   </span><span style="color: #DD0000">'six'</span><span style="color: #007700">,<br />   </span><span style="color: #DD0000">'seven'</span><span style="color: #007700">,<br />   </span><span style="color: #DD0000">'eight'</span><span style="color: #007700">,<br />   </span><span style="color: #DD0000">'nine'</span><span style="color: #007700">,<br />   </span><span style="color: #DD0000">'ten'</span><span style="color: #007700">,<br />];<br /></span><span style="color: #0000BB">$db </span><span style="color: #007700">= new </span><span style="color: #0000BB">Pdo\Sqlite</span><span style="color: #007700">(</span><span style="color: #DD0000">'sqlite::memory:'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"CREATE TABLE strings(a)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$insert </span><span style="color: #007700">= </span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">'INSERT INTO strings VALUES (?)'</span><span style="color: #007700">);<br />foreach (</span><span style="color: #0000BB">$data </span><span style="color: #007700">as </span><span style="color: #0000BB">$str</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$insert</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(array(</span><span style="color: #0000BB">$str</span><span style="color: #007700">));<br />}<br /></span><span style="color: #0000BB">$insert </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /><br />function </span><span style="color: #0000BB">max_len_step</span><span style="color: #007700">(</span><span style="color: #0000BB">$context</span><span style="color: #007700">, </span><span style="color: #0000BB">$row_number</span><span style="color: #007700">, </span><span style="color: #0000BB">$string</span><span style="color: #007700">)<br />{<br />    if (</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">) &gt; </span><span style="color: #0000BB">$context</span><span style="color: #007700">) {<br />        </span><span style="color: #0000BB">$context </span><span style="color: #007700">= </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br />    }<br />    return </span><span style="color: #0000BB">$context</span><span style="color: #007700">;<br />}<br /><br />function </span><span style="color: #0000BB">max_len_finalize</span><span style="color: #007700">(</span><span style="color: #0000BB">$context</span><span style="color: #007700">, </span><span style="color: #0000BB">$row_count</span><span style="color: #007700">)<br />{<br />    return </span><span style="color: #0000BB">$context </span><span style="color: #007700">=== </span><span style="color: #0000BB">null </span><span style="color: #007700">? </span><span style="color: #0000BB">0 </span><span style="color: #007700">: </span><span style="color: #0000BB">$context</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createAggregate</span><span style="color: #007700">(</span><span style="color: #DD0000">'max_len'</span><span style="color: #007700">, </span><span style="color: #DD0000">'max_len_step'</span><span style="color: #007700">, </span><span style="color: #DD0000">'max_len_finalize'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT max_len(a) from strings'</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">());<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   
   <div class="tip"><strong class="tip">Suggerimento</strong>
    <p class="simpara">
     It is NOT recommended for you to store a copy of the values in the context
     and then process them at the end, as you would cause SQLite to use a lot of
     memory to process the query - just think of how much memory you would need
     if a million rows were stored in memory, each containing a string 32 bytes
     in length.
    </p>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdo-sqlite.createaggregate-seealso">
  <h3 class="title">Vedere anche:</h3>
  <ul class="simplelist">
   <li><span class="methodname"><a href="pdo-sqlite.createfunction.php" class="methodname" rel="rdfs-seeAlso">Pdo\Sqlite::createFunction()</a> - Registers a user-defined function for use in SQL statements</span></li>
   <li><span class="methodname"><a href="pdo-sqlite.createcollation.php" class="methodname" rel="rdfs-seeAlso">Pdo\Sqlite::createCollation()</a> - Registers a user-defined function for use as a collating function in SQL statements</span></li>
   <li><span class="function"><strong>sqlite_create_function()</strong></span></li>
   <li><span class="function"><strong>sqlite_create_aggregate()</strong></span></li>
  </ul>
 </div>


</div><?php manual_footer($setup); ?>