Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 3634

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>PDO-&#62;sqliteCreateAggregate()</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="SQLite Functions (PDO_SQLITE)"
HREF="ref.pdo-sqlite.html"><LINK
REL="PREVIOUS"
TITLE="PDO_SQLITE DSN"
HREF="ref.pdo-sqlite.connection.html"><LINK
REL="NEXT"
TITLE="PDO->sqliteCreateFunction()"
HREF="function.pdo-sqlitecreatefunction.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="refentry"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP 手册</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="ref.pdo-sqlite.connection.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.pdo-sqlitecreatefunction.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.PDO-sqliteCreateAggregate"
></A
>PDO-&#62;sqliteCreateAggregate()</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN218336"
></A
><P
>    (no version information, might be only in CVS)</P
>PDO-&#62;sqliteCreateAggregate()&nbsp;--&nbsp;
   Registers an aggregating User Defined Function for use in SQL statements
  </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN218339"
></A
><H2
>说明</H2
>class <B
CLASS="classname"
>PDO</B
> { <BR
></BR
>bool <B
CLASS="methodname"
>sqliteCreateAggregate</B
> ( string function_name, callback step_func, callback finalize_func [, int num_args] )<BR
></BR
>}<DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>警告</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>本函数是<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>实验性</I
></SPAN
>的。本函数的行为,包括函数名称以及其它任何关于本函数的文档可能会在没有通知的情况下随
PHP 以后的发布而改变。使用本函数风险自担。</P
></TD
></TR
></TABLE
></DIV
><P
>&#13;   This method is similar to <A
HREF="function.pdo-sqlitecreatefunction.html"
>PDO-&#62;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
>&#13;   The key difference between this method and <A
HREF="function.pdo-sqlitecreatefunction.html"
>PDO-&#62;sqliteCreateFunction()</A
> is that two functions are
   required to manage the aggregate. 
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN218366"
></A
><H2
>参数</H2
><P
>&#13;   <P
></P
><DIV
CLASS="variablelist"
><DL
><DT
><CODE
CLASS="parameter"
>function_name</CODE
></DT
><DD
><P
>&#13;       The name of the function used in SQL statements.
      </P
></DD
><DT
><CODE
CLASS="parameter"
>step_func</CODE
></DT
><DD
><P
>&#13;       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
>&#13;       This function need to be defined as:
       <B
CLASS="methodname"
><TT
CLASS="replaceable"
><I
>step</I
></TT
></B
> ( mixed context, int rownumber, mixed value1 [, mixed value2 [, mixed ..]] )<BR
></BR
>
      </P
><P
>&#13;       <CODE
CLASS="varname"
>context</CODE
> will be <TT
CLASS="constant"
><B
>NULL</B
></TT
> 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
>&#13;       <CODE
CLASS="varname"
>rownumber</CODE
> will hold the current row number.
      </P
></DD
><DT
><CODE
CLASS="parameter"
>finalize_func</CODE
></DT
><DD
><P
>&#13;       Callback function to aggregate the "stepped" 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.html#language.types.intro"
>scalar type</A
>).
      </P
><P
>&#13;       This function need to be defined as:
       <B
CLASS="methodname"
><TT
CLASS="replaceable"
><I
>fini</I
></TT
></B
> ( mixed context, int rownumber )<BR
></BR
>
      </P
><P
>&#13;       <CODE
CLASS="varname"
>context</CODE
> will hold the return value from the very
       last call to the step function.
      </P
><P
>&#13;       <CODE
CLASS="varname"
>rownumber</CODE
> will hold the number of rows over which
       the aggregate was performed.
      </P
><P
>&#13;       The return value of this function will be used as the return value for
       the aggregate.
      </P
></DD
><DT
><CODE
CLASS="parameter"
>num_args</CODE
></DT
><DD
><P
>&#13;       Hint to the SQLite parser if the callback function accepts a
       predetermined number of arguments.
      </P
></DD
></DL
></DIV
>
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN218430"
></A
><H2
>返回值</H2
><P
>&#13;   如果成功则返回 <TT
CLASS="constant"
><B
>TRUE</B
></TT
>,失败则返回 <TT
CLASS="constant"
><B
>FALSE</B
></TT
>。
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN218435"
></A
><H2
>范例</H2
><P
>&#13;   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN218438"
></A
><P
><B
>例 1. max_length aggregation function example</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$data </font><font color="#007700">= array(<br />&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'one'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'two'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'three'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'four'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'five'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'six'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'seven'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'eight'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'nine'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'ten'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;);<br /></font><font color="#0000BB">$db </font><font color="#007700">= new </font><font color="#0000BB">PDO</font><font color="#007700">(</font><font color="#DD0000">'sqlite::memory:'</font><font color="#007700">);<br /></font><font color="#0000BB">$db</font><font color="#007700">-&gt;</font><font color="#0000BB">exec</font><font color="#007700">(</font><font color="#DD0000">"CREATE TABLE strings(a)"</font><font color="#007700">);<br /></font><font color="#0000BB">$insert </font><font color="#007700">= </font><font color="#0000BB">$db</font><font color="#007700">-&gt;</font><font color="#0000BB">prepare</font><font color="#007700">(</font><font color="#DD0000">'INSERT INTO strings VALUES (?)'</font><font color="#007700">);<br />foreach (</font><font color="#0000BB">$data </font><font color="#007700">as </font><font color="#0000BB">$str</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$insert</font><font color="#007700">-&gt;</font><font color="#0000BB">execute</font><font color="#007700">(array(</font><font color="#0000BB">$str</font><font color="#007700">));<br />}<br /></font><font color="#0000BB">$insert </font><font color="#007700">= </font><font color="#0000BB">null</font><font color="#007700">;<br /><br />function </font><font color="#0000BB">max_len_step</font><font color="#007700">(&amp;</font><font color="#0000BB">$context</font><font color="#007700">, </font><font color="#0000BB">$rownumber</font><font color="#007700">, </font><font color="#0000BB">$string</font><font color="#007700">) <br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if (</font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#0000BB">$string</font><font color="#007700">) &gt; </font><font color="#0000BB">$context</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$context </font><font color="#007700">= </font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#0000BB">$string</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />function </font><font color="#0000BB">max_len_finalize</font><font color="#007700">(&amp;</font><font color="#0000BB">$context</font><font color="#007700">, </font><font color="#0000BB">$rownumber</font><font color="#007700">) <br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">$context</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">$db</font><font color="#007700">-&gt;</font><font color="#0000BB">sqliteCreateAggregate</font><font color="#007700">(</font><font color="#DD0000">'max_len'</font><font color="#007700">, </font><font color="#DD0000">'max_len_step'</font><font color="#007700">, </font><font color="#DD0000">'max_len_finalize'</font><font color="#007700">);<br /><br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$db</font><font color="#007700">-&gt;</font><font color="#0000BB">query</font><font color="#007700">(</font><font color="#DD0000">'SELECT max_len(a) from strings'</font><font color="#007700">)-&gt;</font><font color="#0000BB">fetchAll</font><font color="#007700">());<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
  </P
><P
>&#13;   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 <TT
CLASS="literal"
>max_len_step</TT
> function is
   called and passed a <CODE
CLASS="parameter"
>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="parameter"
>string</CODE
> has a length longer than the current
   maximum, we update the context to hold this new maximum length.
  </P
><P
>&#13;   After all of the rows have been processed, SQLite calls the
   <TT
CLASS="literal"
>max_len_finalize</TT
> function to determine the aggregate
   result.  Here, we could perform some kind of calculation based on the
   data found in the <CODE
CLASS="parameter"
>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"
><BLOCKQUOTE
CLASS="tip"
><P
><B
>提示: </B
>
    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
></BLOCKQUOTE
></DIV
><DIV
CLASS="tip"
><BLOCKQUOTE
CLASS="tip"
><P
><B
>提示: </B
>
    You can use <A
HREF="function.pdo-sqlitecreatefunction.html"
>PDO-&#62;sqliteCreateFunction()</A
> and
    <A
HREF="function.pdo-sqlitecreateaggregate.html"
>PDO-&#62;sqliteCreateAggregate()</A
> to override SQLite
    native SQL functions.
   </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
    This method is not available with the SQLite2 driver.
    Use the old style sqlite API for that instead.
   </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN218456"
></A
><H2
>参见</H2
><P
>&#13;   <P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
><A
HREF="function.pdo-sqlitecreatefunction.html"
>PDO-&#62;sqliteCreateFunction()</A
></TD
></TR
><TR
><TD
><A
HREF="function.sqlite-create-function.html"
><B
CLASS="function"
>sqlite_create_function()</B
></A
></TD
></TR
><TR
><TD
><A
HREF="function.sqlite-create-aggregate.html"
><B
CLASS="function"
>sqlite_create_aggregate()</B
></A
></TD
></TR
></TBODY
></TABLE
><P
></P
>
  </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="ref.pdo-sqlite.connection.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>起始页</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.pdo-sqlitecreatefunction.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>PDO_SQLITE DSN</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.pdo-sqlite.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>PDO-&#62;sqliteCreateFunction()</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>