|
4 | 4 | <refnamediv> |
5 | 5 | <refname>PDO::sqliteCreateAggregate</refname> |
6 | 6 | <refpurpose> |
7 | | - Registers an aggregating User Defined Function for use in SQL statements |
| 7 | + &Alias; <methodname>Pdo\Sqlite::createAggregate</methodname> |
8 | 8 | </refpurpose> |
9 | 9 | </refnamediv> |
10 | 10 |
|
11 | 11 | <refsect1 role="description"> |
12 | 12 | &reftitle.description; |
13 | 13 | <methodsynopsis> |
14 | 14 | <modifier>public</modifier> <type>bool</type><methodname>PDO::sqliteCreateAggregate</methodname> |
15 | | - <methodparam><type>string</type><parameter>function_name</parameter></methodparam> |
16 | | - <methodparam><type>callable</type><parameter>step_func</parameter></methodparam> |
17 | | - <methodparam><type>callable</type><parameter>finalize_func</parameter></methodparam> |
18 | | - <methodparam choice="opt"><type>int</type><parameter>num_args</parameter></methodparam> |
| 15 | + <methodparam><type>string</type><parameter>name</parameter></methodparam> |
| 16 | + <methodparam><type>callable</type><parameter>step</parameter></methodparam> |
| 17 | + <methodparam><type>callable</type><parameter>finalize</parameter></methodparam> |
| 18 | + <methodparam choice="opt"><type>int</type><parameter>numArgs</parameter><initializer>-1</initializer></methodparam> |
19 | 19 | </methodsynopsis> |
20 | | - &warn.experimental.func; |
21 | | - <para> |
22 | | - This method is similar to <xref linkend="pdo.sqlitecreatefunction" |
23 | | - /> except that it registers functions that can be used to calculate a |
24 | | - result aggregated across all the rows of a query. |
25 | | - </para> |
26 | | - <para> |
27 | | - The key difference between this method and <xref |
28 | | - linkend="pdo.sqlitecreatefunction" /> is that two functions are |
29 | | - required to manage the aggregate. |
30 | | - </para> |
| 20 | + <simpara> |
| 21 | + &info.method.alias; <methodname>Pdo\Sqlite::createAggregate</methodname>. |
| 22 | + </simpara> |
31 | 23 | </refsect1> |
32 | | - |
33 | | - <refsect1 role="parameters"> |
34 | | - &reftitle.parameters; |
35 | | - <para> |
36 | | - <variablelist> |
37 | | - <varlistentry> |
38 | | - <term><parameter>function_name</parameter></term> |
39 | | - <listitem> |
40 | | - <para> |
41 | | - The name of the function used in SQL statements. |
42 | | - </para> |
43 | | - </listitem> |
44 | | - </varlistentry> |
45 | | - <varlistentry> |
46 | | - <term><parameter>step_func</parameter></term> |
47 | | - <listitem> |
48 | | - <para> |
49 | | - Callback function called for each row of the result set. Your PHP |
50 | | - function should accumulate the result and store it in the aggregation |
51 | | - context. |
52 | | - </para> |
53 | | - <para> |
54 | | - This function need to be defined as: |
55 | | - <methodsynopsis> |
56 | | - <type>mixed</type><methodname><replaceable>step</replaceable></methodname> |
57 | | - <methodparam><type>mixed</type><parameter>context</parameter></methodparam> |
58 | | - <methodparam><type>int</type><parameter>rownumber</parameter></methodparam> |
59 | | - <methodparam><type>mixed</type><parameter>value</parameter></methodparam> |
60 | | - <methodparam rep="repeat"><type>mixed</type><parameter>values</parameter></methodparam> |
61 | | - </methodsynopsis> |
62 | | - <variablelist> |
63 | | - <varlistentry> |
64 | | - <term><parameter>context</parameter></term> |
65 | | - <listitem> |
66 | | - <para> |
67 | | - &null; for the first row; on subsequent rows it will have the value |
68 | | - that was previously returned from the step function; you should use |
69 | | - this to maintain the aggregate state. |
70 | | - </para> |
71 | | - </listitem> |
72 | | - </varlistentry> |
73 | | - <varlistentry> |
74 | | - <term><parameter>rownumber</parameter></term> |
75 | | - <listitem> |
76 | | - <para> |
77 | | - The current row number. |
78 | | - </para> |
79 | | - </listitem> |
80 | | - </varlistentry> |
81 | | - <varlistentry> |
82 | | - <term><parameter>value</parameter></term> |
83 | | - <listitem> |
84 | | - <para> |
85 | | - The first argument passed to the aggregate. |
86 | | - </para> |
87 | | - </listitem> |
88 | | - </varlistentry> |
89 | | - <varlistentry> |
90 | | - <term><parameter>values</parameter></term> |
91 | | - <listitem> |
92 | | - <para> |
93 | | - Further arguments passed to the aggregate. |
94 | | - </para> |
95 | | - </listitem> |
96 | | - </varlistentry> |
97 | | - </variablelist> |
98 | | - The return value of this function will be used as the |
99 | | - <parameter>context</parameter> argument in the next call of the step or |
100 | | - finalize functions. |
101 | | - </para> |
102 | | - </listitem> |
103 | | - </varlistentry> |
104 | | - <varlistentry> |
105 | | - <term><parameter>finalize_func</parameter></term> |
106 | | - <listitem> |
107 | | - <para> |
108 | | - Callback function to aggregate the "stepped" data from each row. |
109 | | - Once all the rows have been processed, this function will be called |
110 | | - and it should then take the data from the aggregation context and |
111 | | - return the result. This callback function should return a type understood |
112 | | - by SQLite (i.e. <link |
113 | | - linkend="language.types.intro">scalar type</link>). |
114 | | - </para> |
115 | | - <para> |
116 | | - This function need to be defined as: |
117 | | - <methodsynopsis> |
118 | | - <type>mixed</type><methodname><replaceable>fini</replaceable></methodname> |
119 | | - <methodparam><type>mixed</type><parameter>context</parameter></methodparam> |
120 | | - <methodparam><type>int</type><parameter>rowcount</parameter></methodparam> |
121 | | - </methodsynopsis> |
122 | | - <variablelist> |
123 | | - <varlistentry> |
124 | | - <term><parameter>context</parameter></term> |
125 | | - <listitem> |
126 | | - <para> |
127 | | - Holds the return value from the very last call to the step function. |
128 | | - </para> |
129 | | - </listitem> |
130 | | - </varlistentry> |
131 | | - <varlistentry> |
132 | | - <term><parameter>rowcount</parameter></term> |
133 | | - <listitem> |
134 | | - <para> |
135 | | - Holds the number of rows over which the aggregate was performed. |
136 | | - </para> |
137 | | - </listitem> |
138 | | - </varlistentry> |
139 | | - </variablelist> |
140 | | - The return value of this function will be used as the return value for |
141 | | - the aggregate. |
142 | | - </para> |
143 | | - </listitem> |
144 | | - </varlistentry> |
145 | | - <varlistentry> |
146 | | - <term><parameter>num_args</parameter></term> |
147 | | - <listitem> |
148 | | - <para> |
149 | | - Hint to the SQLite parser if the callback function accepts a |
150 | | - predetermined number of arguments. |
151 | | - </para> |
152 | | - </listitem> |
153 | | - </varlistentry> |
154 | | - </variablelist> |
155 | | - </para> |
156 | | - </refsect1> |
157 | | - |
158 | | - <refsect1 role="returnvalues"> |
159 | | - &reftitle.returnvalues; |
160 | | - <para> |
161 | | - &return.success; |
162 | | - </para> |
163 | | - </refsect1> |
164 | | - |
165 | | - <refsect1 role="examples"> |
166 | | - &reftitle.examples; |
167 | | - <para> |
168 | | - <example> |
169 | | - <title>max_length aggregation function example</title> |
170 | | - <programlisting role="php"> |
171 | | -<![CDATA[ |
172 | | -<?php |
173 | | -$data = array( |
174 | | - 'one', |
175 | | - 'two', |
176 | | - 'three', |
177 | | - 'four', |
178 | | - 'five', |
179 | | - 'six', |
180 | | - 'seven', |
181 | | - 'eight', |
182 | | - 'nine', |
183 | | - 'ten', |
184 | | - ); |
185 | | -$db = new PDO('sqlite::memory:'); |
186 | | -$db->exec("CREATE TABLE strings(a)"); |
187 | | -$insert = $db->prepare('INSERT INTO strings VALUES (?)'); |
188 | | -foreach ($data as $str) { |
189 | | - $insert->execute(array($str)); |
190 | | -} |
191 | | -$insert = null; |
192 | | -
|
193 | | -function max_len_step($context, $rownumber, $string) |
194 | | -{ |
195 | | - if (strlen($string) > $context) { |
196 | | - $context = strlen($string); |
197 | | - } |
198 | | - return $context; |
199 | | -} |
200 | | -
|
201 | | -function max_len_finalize($context, $rowcount) |
202 | | -{ |
203 | | - return $context === null ? 0 : $context; |
204 | | -} |
205 | | -
|
206 | | -$db->sqliteCreateAggregate('max_len', 'max_len_step', 'max_len_finalize'); |
207 | | -
|
208 | | -var_dump($db->query('SELECT max_len(a) from strings')->fetchAll()); |
209 | | -
|
210 | | -?> |
211 | | -]]> |
212 | | - </programlisting> |
213 | | - </example> |
214 | | - </para> |
215 | | - <para> |
216 | | - In this example, we are creating an aggregating function that will |
217 | | - calculate the length of the longest string in one of the columns of the |
218 | | - table. For each row, the <literal>max_len_step</literal> function is |
219 | | - called and passed a <literal>$context</literal> parameter. The context |
220 | | - parameter is just like any other PHP variable and be set to hold an array |
221 | | - or even an object value. In this example, we are simply using it to hold |
222 | | - the maximum length we have seen so far; if the |
223 | | - <literal>$string</literal> has a length longer than the current |
224 | | - maximum, we update the context to hold this new maximum length. |
225 | | - </para> |
226 | | - <para> |
227 | | - After all of the rows have been processed, SQLite calls the |
228 | | - <literal>max_len_finalize</literal> function to determine the aggregate |
229 | | - result. Here, we could perform some kind of calculation based on the |
230 | | - data found in the <literal>$context</literal>. In our simple example |
231 | | - though, we have been calculating the result as the query progressed, so we |
232 | | - simply need to return the context value. |
233 | | - </para> |
234 | | - <tip> |
235 | | - <para> |
236 | | - It is NOT recommended for you to store a copy of the values in the context |
237 | | - and then process them at the end, as you would cause SQLite to use a lot of |
238 | | - memory to process the query - just think of how much memory you would need |
239 | | - if a million rows were stored in memory, each containing a string 32 bytes |
240 | | - in length. |
241 | | - </para> |
242 | | - </tip> |
243 | | - <tip> |
244 | | - <para> |
245 | | - You can use <xref linkend="pdo.sqlitecreatefunction" /> and |
246 | | - <xref linkend="pdo.sqlitecreateaggregate" /> to override SQLite |
247 | | - native SQL functions. |
248 | | - </para> |
249 | | - </tip> |
250 | | - </refsect1> |
251 | | - |
252 | | - |
253 | | - <refsect1 role="seealso"> |
254 | | - &reftitle.seealso; |
255 | | - <para> |
256 | | - <simplelist> |
257 | | - <member><xref linkend="pdo.sqlitecreatefunction" /></member> |
258 | | - <member><function>sqlite_create_function</function></member> |
259 | | - <member><function>sqlite_create_aggregate</function></member> |
260 | | - </simplelist> |
261 | | - </para> |
262 | | - </refsect1> |
263 | | - |
264 | 24 | </refentry> |
265 | | - |
266 | 25 | <!-- Keep this comment at the end of the file |
267 | 26 | Local variables: |
268 | 27 | mode: sgml |
|
0 commit comments