Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 10207

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Registers an aggregating User Defined Function for use in SQL statements</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ref.pdo-sqlite.connection.html">PDO_SQLITE DSN</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdo.sqlitecreatefunction.html">PDO::sqliteCreateFunction</a></div>
 <div class="up"><a href="ref.pdo-sqlite.html">SQLite (PDO)</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="pdo.sqlitecreateaggregate" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDO::sqliteCreateAggregate</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PECL pdo_sqlite &gt;= 1.0.0)</p><p class="refpurpose"><span class="refname">PDO::sqliteCreateAggregate</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.sqlitecreateaggregate-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span> <span class="methodname"><strong>PDO::sqliteCreateAggregate</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$function_name</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$step_func</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$finalize_func</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$num_args</code></span>
  ] )</div>

  <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function is
<em class="emphasis">EXPERIMENTAL</em>. The behaviour of this function, its name, and
surrounding documentation may change without notice in a future release of PHP.
This function should be used at your own risk.
</p></div>
  <p class="para">
   This method is similar to <a href="pdo.sqlitecreatefunction.html" class="xref">PDO::sqliteCreateFunction</a> except that it registers functions that can be used to calculate a
   result aggregated across all the rows of a query.
  </p>
  <p class="para">
   The key difference between this method and <a href="pdo.sqlitecreatefunction.html" class="xref">PDO::sqliteCreateFunction</a> is that two functions are
   required to manage the aggregate. 
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-pdo.sqlitecreateaggregate-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">function_name</code></em></span>
     <dd>

      <p class="para">
       The name of the function used in SQL statements.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">step_func</code></em></span>
     <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>
         ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$context</code></span>
        , <span class="methodparam"><span class="type">int</span> <code class="parameter">$rownumber</code></span>
        , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value1</code></span>
        [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value2</code></span>
        [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$..</code></span>
       ]] )</div>

      </p>
      <p class="para">
       <var class="varname"><var class="varname">context</var></var> will be <strong><code>NULL</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>
      <p class="para">
       <var class="varname"><var class="varname">rownumber</var></var> will hold the current row number.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">finalize_func</code></em></span>
     <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. Callback functions should return a type understood
       by SQLite (i.e. <a href="language.types.intro.html" 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.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$context</code></span>
        , <span class="methodparam"><span class="type">int</span> <code class="parameter">$rownumber</code></span>
        )</div>

      </p>
      <p class="para">
       <var class="varname"><var class="varname">context</var></var> will hold the return value from the very
       last call to the step function.
      </p>
      <p class="para">
       <var class="varname"><var class="varname">rownumber</var></var> will hold the number of rows over which
       the aggregate was performed.
      </p>
      <p class="para">
       The return value of this function will be used as the return value for
       the aggregate.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">num_args</code></em></span>
     <dd>

      <p class="para">
       Hint to the SQLite parser if the callback function accepts a
       predetermined number of arguments.
      </p>
     </dd>

    </dt>

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdo.sqlitecreateaggregate-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdo.sqlitecreateaggregate-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-990">
    <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&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'one'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'two'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'three'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'four'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'five'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'six'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'seven'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'eight'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'nine'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'ten'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;);<br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</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&nbsp;TABLE&nbsp;strings(a)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$insert&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;INTO&nbsp;strings&nbsp;VALUES&nbsp;(?)'</span><span style="color: #007700">);<br />foreach&nbsp;(</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /><br />function&nbsp;</span><span style="color: #0000BB">max_len_step</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$rownumber</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">)&nbsp;<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">)&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">;<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">max_len_finalize</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$rownumber</span><span style="color: #007700">)&nbsp;<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</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">sqliteCreateAggregate</span><span style="color: #007700">(</span><span style="color: #DD0000">'max_len'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'max_len_step'</span><span style="color: #007700">,&nbsp;</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&nbsp;max_len(a)&nbsp;from&nbsp;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>
  </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 <em>max_len_step</em> function is
   called and passed a <em><code class="parameter">context</code></em> 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
   <em><code class="parameter">string</code></em> 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
   <em>max_len_finalize</em> function to determine the aggregate
   result.  Here, we could perform some kind of calculation based on the
   data found in the <em><code class="parameter">context</code></em>.  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">Tip</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">Tip</strong>
   <p class="para">
    You can use <a href="pdo.sqlitecreatefunction.html" class="xref">PDO::sqliteCreateFunction</a> and
    <a href="pdo.sqlitecreateaggregate.html" class="xref">PDO::sqliteCreateAggregate</a> to override SQLite
    native SQL functions.
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    This method is not available with the SQLite2 driver.
    Use the old style sqlite API for that instead.
   </p>
  </p></blockquote>

 </div>



 <div class="refsect1 seealso" id="refsect1-pdo.sqlitecreateaggregate-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="pdo.sqlitecreatefunction.html" class="xref">PDO::sqliteCreateFunction</a></li>
    <li class="member"> <span class="function"><a href="function.sqlite-create-function.html" class="function" rel="rdfs-seeAlso">sqlite_create_function()</a> - Registers a &quot;regular&quot; User Defined Function for use in SQL statements</span></li>
    <li class="member"> <span class="function"><a href="function.sqlite-create-aggregate.html" class="function" rel="rdfs-seeAlso">sqlite_create_aggregate()</a> - Register an aggregating UDF for use in SQL statements</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ref.pdo-sqlite.connection.html">PDO_SQLITE DSN</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdo.sqlitecreatefunction.html">PDO::sqliteCreateFunction</a></div>
 <div class="up"><a href="ref.pdo-sqlite.html">SQLite (PDO)</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>