<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.sqlite3.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'sqlite3.createaggregate.php',
    1 => 'SQLite3::createAggregate',
    2 => 'Registers a PHP function for use as an SQL aggregate function',
  ),
  'up' => 
  array (
    0 => 'class.sqlite3.php',
    1 => 'SQLite3',
  ),
  'prev' => 
  array (
    0 => 'sqlite3.construct.php',
    1 => 'SQLite3::__construct',
  ),
  'next' => 
  array (
    0 => 'sqlite3.createcollation.php',
    1 => 'SQLite3::createCollation',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/sqlite3/sqlite3/createaggregate.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="sqlite3.createaggregate" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SQLite3::createAggregate</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">SQLite3::createAggregate</span> &mdash; <span class="dc-title">Registers a PHP function for use as an SQL aggregate function</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-sqlite3.createaggregate-description">
  <h3 class="title">Descrizione</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>SQLite3::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">$stepCallback</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">$finalCallback</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">$argCount</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="para rdfs-comment">
   Registers a PHP function or user-defined function for use as an SQL
   aggregate function for use within SQL statements.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-sqlite3.createaggregate-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">name</code></dt>
     <dd>
      <p class="para">
       Name of the SQL aggregate to be created or redefined.
      </p>
     </dd>
    
    
     <dt><code class="parameter">stepCallback</code></dt>
     <dd>
      <p class="para">
       Callback function called for each row of the result set. Your PHP
       function should accumulate the result and store it in the aggregation
       context.
      </p>
      <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>
          <p class="para">
           <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.
          </p>
         </dd>
        
        
         <dt><code class="parameter">rownumber</code></dt>
         <dd>
          <p class="para">
           The current row number.
          </p>
         </dd>
        
        
         <dt><code class="parameter">value</code></dt>
         <dd>
          <p class="para">
           The first argument passed to the aggregate.
          </p>
         </dd>
        
        
         <dt><code class="parameter">values</code></dt>
         <dd>
          <p class="para">
           Further arguments passed to the aggregate.
          </p>
         </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">finalCallback</code></dt>
     <dd>
      <p class="para">
       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>).
      </p>
      <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">$rownumber</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">rownumber</code></dt>
         <dd>
          <p class="para">
           Always <code class="literal">0</code>.
          </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">argCount</code></dt>
     <dd>
      <p class="para">
       The number of arguments that the SQL aggregate takes. If
       this parameter is negative, then the SQL aggregate may take
       any number of arguments.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-sqlite3.createaggregate-returnvalues">
  <h3 class="title">Valori restituiti</h3>
  <p class="para">
   Returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> upon successful creation of the aggregate,  o <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-sqlite3.createaggregate-examples">
  <h3 class="title">Esempi</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 max_length aggregation function example</strong></p>
    <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">= array(<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">SQLite3</span><span style="color: #007700">(</span><span style="color: #DD0000">':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">bindValue</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </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">();<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">$rownumber</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">$rownumber</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">querySingle</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT max_len(a) from strings'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Il precedente esempio visualizzerà:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">int(5)</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   In this example, we are creating an aggregating function 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 array
   or even an object value.  In this example, we are simply using it to hold
   the maximum length we have seen so far; if the
   <code class="literal">$string</code> has a length longer than the current
   maximum, we update the context to hold this new maximum length.
  </p>
  <p class="para">
   After all of the rows have been processed, SQLite calls the
   <code class="literal">max_len_finalize</code> function to determine the aggregate
   result.  Here, we could perform some kind of calculation based on the
   data found in the <code class="literal">$context</code>.  In our simple example
   though, we have been calculating the result as the query progressed, so we
   simply need to return the context value.
  </p>
  <div class="tip"><strong class="tip">Suggerimento</strong>
   <p class="para">
    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 class="tip"><strong class="tip">Suggerimento</strong>
   <p class="para">
    You can use <span class="methodname"><strong>SQLite3::createAggregate()</strong></span> to override SQLite
    native SQL functions.
   </p>
  </div>
 </div>


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