Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > 703d980c580707c382b4e43e25965bc5 > files > 4407

php-manual-pt_BR-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
>sqlite_create_aggregate</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="Manual do PHP"
HREF="index.html"><LINK
REL="UP"
TITLE="SQLite"
HREF="ref.sqlite.html"><LINK
REL="PREVIOUS"
TITLE="sqlite_column"
HREF="function.sqlite-column.html"><LINK
REL="NEXT"
TITLE="sqlite_create_function"
HREF="function.sqlite-create-function.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"
>Manual do PHP</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="function.sqlite-column.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.sqlite-create-function.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.sqlite-create-aggregate"
></A
>sqlite_create_aggregate</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN210776"
></A
><P
>    (PHP 5)</P
>sqlite_create_aggregate&nbsp;--&nbsp;Registra uma função agregada UDF para usar em SQLs</DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN210779"
></A
><H2
>Descrição</H2
>bool <B
CLASS="methodname"
>sqlite_create_aggregate</B
> ( resource dbhandle, string function_name, mixed step_func, mixed finalize_func [, int num_args] )<BR
></BR
><P
>&#13;   <B
CLASS="function"
>sqlite_create_aggregate()</B
> é similar a
   <A
HREF="function.sqlite-create-function.html"
><B
CLASS="function"
>sqlite_create_function()</B
></A
> com a diferença que registra funções que
   podem ser usadas para calcular um resultado através de todas
   as linhas de uma query.
  </P
><P
>&#13;   A diferença chave entre esta função e 
   <A
HREF="function.sqlite-create-function.html"
><B
CLASS="function"
>sqlite_create_function()</B
></A
> é que duas funções são requeridas
   para gerenciar a agregada; <CODE
CLASS="parameter"
>step_func</CODE
> é
   executada para cada linha do conjunto de resultados. Sua função PHP
   deve acumular o resultado e guardar no contexto da agregação.
   Uma vez que todas as linhas tenham sido processadas,
   <CODE
CLASS="parameter"
>finalize_func</CODE
> será chamada e deve então
   pegar os dados do contexto de agregação e então retornar o resultado.
  </P
><P
>&#13;   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN210807"
></A
><P
><B
>Exemplo 1. Exemplo de agregação</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">$dbhandle </font><font color="#007700">= </font><font color="#0000BB">sqlite_open</font><font color="#007700">(</font><font color="#DD0000">':memory:'</font><font color="#007700">);<br /></font><font color="#0000BB">sqlite_query</font><font color="#007700">(</font><font color="#0000BB">$dbhandle</font><font color="#007700">, </font><font color="#DD0000">"CREATE TABLE strings(a)"</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">$str </font><font color="#007700">= </font><font color="#0000BB">sqlite_escape_string</font><font color="#007700">(</font><font color="#0000BB">$str</font><font color="#007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">sqlite_query</font><font color="#007700">(</font><font color="#0000BB">$dbhandle</font><font color="#007700">, </font><font color="#DD0000">"INSERT INTO strings VALUES ('$str')"</font><font color="#007700">);<br />}<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">$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">) <br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return </font><font color="#0000BB">$context</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">sqlite_create_aggregate</font><font color="#007700">(</font><font color="#0000BB">$dbhandle</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">sqlite_array_query</font><font color="#007700">(</font><font color="#0000BB">$dbhandle</font><font color="#007700">, </font><font color="#DD0000">'SELECT max_len(a) from strings'</font><font color="#007700">));<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
  </P
><P
>&#13;   Neste exemplo, nós estamos criando uma função agregada que irá calcular
   o tamanho da maior string de uma das colunas da tabela.
   Para cada linha, <TT
CLASS="literal"
>max_len_step</TT
> é executada e passado o
   parâmetro <CODE
CLASS="parameter"
>context</CODE
>. O parâmetro contexto é
   igual a qualquer outra variavel PHP e pode guardar uma matriz ou um objeto.
   Neste exemplo, nós estamos usando simplesmente para guardar o tamanho
   da maior string que nós vimos até aqui, se 
   <CODE
CLASS="parameter"
>string</CODE
> tiver um tamanho maior do que o máximo atual,
   nós atualizamos o contexto para guardar este novo tamanho máximo.
  </P
><P
>&#13;   Depois que todas as linhas tiverem sido processadas, SQLite chama a função
   <TT
CLASS="literal"
>max_len_finalize</TT
> para determinar o resultado agregado.
   Aqui você pode fazer algum tipo de calculo baseado nos dados encontrados em
   <CODE
CLASS="parameter"
>context</CODE
>. No nosso exemplo simples,
   nós já calculamos o resultado de acordo com o progresso da query, então nós
   precisamos apenas retornar o valor de contexto.
  </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>
    O exemplo acima não irá funcionar corretamente se a coluna conter dados binários. 
    De uma olhada na pagina do manual para a função <A
HREF="function.sqlite-udf-decode-binary.html"
><B
CLASS="function"
>sqlite_udf_decode_binary()</B
></A
> 
    para uma explicação do porque é assim, e um exemplo de como fazer respeitar
    a codificação binária.
   </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="tip"
><BLOCKQUOTE
CLASS="tip"
><P
><B
>Dica: </B
>
    NÃO é recomendado que você guarde uma copia dos valores no contexto
    e então processe no final, já que você pode fazer com que o SQLite use uma
    grande quantidade de memória para processar a query - pense quanta memória
    você vai necessitar se tiver um milhão de linhas, cada uma contendo uma string de 32 bytes de
    tamanho.
   </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="tip"
><BLOCKQUOTE
CLASS="tip"
><P
><B
>Dica: </B
>
    Você pode usar <A
HREF="function.sqlite-create-function.html"
><B
CLASS="function"
>sqlite_create_function()</B
></A
> e
    <B
CLASS="function"
>sqlite_create_aggregate()</B
> para sobrescrever as funções
    nativas de SQL do SQLite.
   </P
></BLOCKQUOTE
></DIV
><P
>&#13;   Veja também <A
HREF="function.sqlite-create-function.html"
><B
CLASS="function"
>sqlite_create_function()</B
></A
>,
   <A
HREF="function.sqlite-udf-encode-binary.html"
><B
CLASS="function"
>sqlite_udf_encode_binary()</B
></A
> e
   <A
HREF="function.sqlite-udf-decode-binary.html"
><B
CLASS="function"
>sqlite_udf_decode_binary()</B
></A
>.
  </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="function.sqlite-column.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Principal</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="function.sqlite-create-function.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>sqlite_column</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.sqlite.html"
ACCESSKEY="U"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>sqlite_create_function</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>