reference/pdo_sqlite/PDO/sqliteCreateAggregate.xml
9e0f03ac354d797d1d16c0fcc1663e5e170f2727
...
...
@@ -56,21 +56,45 @@
56
56
<type>mixed</type><methodname><replaceable>step</replaceable></methodname>
57
57
<methodparam><type>mixed</type><parameter>context</parameter></methodparam>
58
58
<methodparam><type>int</type><parameter>rownumber</parameter></methodparam>
59
-
<methodparam><type>mixed</type><parameter>value1</parameter></methodparam>
60
-
<methodparam choice="opt"><type>mixed</type><parameter>value2</parameter></methodparam>
61
-
<methodparam choice="opt"><type>mixed</type><parameter>..</parameter></methodparam>
59
+
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
60
+
<methodparam rep="repeat"><type>mixed</type><parameter>values</parameter></methodparam>
62
61
</methodsynopsis>
63
-
</para>
64
-
<para>
65
-
<varname>context</varname> will be &null; for the first row; on
66
-
subsequent rows it will have the value that was previously returned
67
-
from the step function; you should use this to maintain the aggregate
68
-
state.
69
-
</para>
70
-
<para>
71
-
<varname>rownumber</varname> will hold the current row number.
72
-
</para>
73
-
<para>
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>
74
98
The return value of this function will be used as the
75
99
<parameter>context</parameter> argument in the next call of the step or
76
100
finalize functions.
...
...
@@ -84,7 +108,7 @@
84
108
Callback function to aggregate the "stepped" data from each row.
85
109
Once all the rows have been processed, this function will be called
86
110
and it should then take the data from the aggregation context and
87
-
return the result. Callback functions should return a type understood
111
+
return the result. This callback function should return a type understood
88
112
by SQLite (i.e. <link
89
113
linkend="language.types.intro">scalar type</link>).
90
114
</para>
...
...
@@ -93,18 +117,26 @@
93
117
<methodsynopsis>
94
118
<type>mixed</type><methodname><replaceable>fini</replaceable></methodname>
95
119
<methodparam><type>mixed</type><parameter>context</parameter></methodparam>
96
-
<methodparam><type>int</type><parameter>rownumber</parameter></methodparam>
120
+
<methodparam><type>int</type><parameter>rowcount</parameter></methodparam>
97
121
</methodsynopsis>
98
-
</para>
99
-
<para>
100
-
<varname>context</varname> will hold the return value from the very
101
-
last call to the step function.
102
-
</para>
103
-
<para>
104
-
<varname>rownumber</varname> will hold the number of rows over which
105
-
the aggregate was performed.
106
-
</para>
107
-
<para>
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>
108
140
The return value of this function will be used as the return value for
109
141
the aggregate.
110
142
</para>
...
...
@@ -158,7 +190,7 @@ foreach ($data as $str) {
158
190
}
159
191
$insert = null;
160
192

161
-
function max_len_step(&$context, $rownumber, $string)
193
+
function max_len_step($context, $rownumber, $string)
162
194
{
163
195
if (strlen($string) > $context) {
164
196
$context = strlen($string);
...
...
@@ -166,9 +198,9 @@ function max_len_step(&$context, $rownumber, $string)
166
198
return $context;
167
199
}
168
200

169
-
function max_len_finalize(&$context, $rownumber)
201
+
function max_len_finalize($context, $rowcount)
170
202
{
171
-
return $context;
203
+
return $context === null ? 0 : $context;
172
204
}
173
205

174
206
$db->sqliteCreateAggregate('max_len', 'max_len_step', 'max_len_finalize');
...
...
@@ -184,18 +216,18 @@ var_dump($db->query('SELECT max_len(a) from strings')->fetchAll());
184
216
In this example, we are creating an aggregating function that will
185
217
calculate the length of the longest string in one of the columns of the
186
218
table. For each row, the <literal>max_len_step</literal> function is
187
-
called and passed a <parameter>context</parameter> parameter. The context
219
+
called and passed a <literal>$context</literal> parameter. The context
188
220
parameter is just like any other PHP variable and be set to hold an array
189
221
or even an object value. In this example, we are simply using it to hold
190
222
the maximum length we have seen so far; if the
191
-
<parameter>string</parameter> has a length longer than the current
223
+
<literal>$string</literal> has a length longer than the current
192
224
maximum, we update the context to hold this new maximum length.
193
225
</para>
194
226
<para>
195
227
After all of the rows have been processed, SQLite calls the
196
228
<literal>max_len_finalize</literal> function to determine the aggregate
197
229
result. Here, we could perform some kind of calculation based on the
198
-
data found in the <parameter>context</parameter>. In our simple example
230
+
data found in the <literal>$context</literal>. In our simple example
199
231
though, we have been calculating the result as the query progressed, so we
200
232
simply need to return the context value.
201
233
</para>
...
...
@@ -215,13 +247,6 @@ var_dump($db->query('SELECT max_len(a) from strings')->fetchAll());
215
247
native SQL functions.
216
248
</para>
217
249
</tip>
218
-
<note>
219
-
<para>
220
-
This method is not available with the SQLite2 driver.
221
-
Use the old style sqlite API for that instead.
222
-
</para>
223
-
</note>
224
-

225
250
</refsect1>
226
251

227
252

228
253