2014-09-05 07:44:07 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 08:37:21 +00:00
|
|
|
<!-- $Revision$ -->
|
2012-01-12 16:47:02 +00:00
|
|
|
<refentry xml:id="pdo.sqlitecreatefunction" xmlns="http://docbook.org/ns/docbook">
|
2005-09-09 16:50:02 +00:00
|
|
|
<refnamediv>
|
2009-02-12 11:55:33 +00:00
|
|
|
<refname>PDO::sqliteCreateFunction</refname>
|
2005-09-09 16:50:02 +00:00
|
|
|
<refpurpose>
|
|
|
|
Registers a User Defined Function for use in SQL statements
|
|
|
|
</refpurpose>
|
|
|
|
</refnamediv>
|
2005-09-09 03:25:37 +00:00
|
|
|
|
2005-09-09 16:50:02 +00:00
|
|
|
<refsect1 role="description">
|
|
|
|
&reftitle.description;
|
2012-01-13 14:20:43 +00:00
|
|
|
<methodsynopsis>
|
2013-06-18 05:36:30 +00:00
|
|
|
<modifier>public</modifier> <type>bool</type><methodname>PDO::sqliteCreateFunction</methodname>
|
2012-01-13 14:20:43 +00:00
|
|
|
<methodparam><type>string</type><parameter>function_name</parameter></methodparam>
|
2012-03-12 05:55:59 +00:00
|
|
|
<methodparam><type>callable</type><parameter>callback</parameter></methodparam>
|
2017-03-17 14:17:07 +00:00
|
|
|
<methodparam choice="opt"><type>int</type><parameter>num_args</parameter><initializer>-1</initializer></methodparam>
|
2017-03-17 14:07:36 +00:00
|
|
|
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
|
2012-01-13 14:20:43 +00:00
|
|
|
</methodsynopsis>
|
2005-09-09 16:50:02 +00:00
|
|
|
&warn.experimental.func;
|
2005-09-09 03:25:37 +00:00
|
|
|
|
|
|
|
<para>
|
2007-02-02 00:26:54 +00:00
|
|
|
This method allows you to register a PHP function with SQLite as an
|
|
|
|
<acronym>UDF</acronym> (User Defined Function), so that it can be called
|
|
|
|
from within your SQL statements.
|
2005-09-09 03:25:37 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The UDF can be used in any SQL statement that can call functions, such as
|
|
|
|
SELECT and UPDATE statements and also in triggers.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="parameters">
|
|
|
|
&reftitle.parameters;
|
|
|
|
<para>
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>function_name</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name of the function used in SQL statements.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>callback</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Callback function to handle the defined SQL function.
|
|
|
|
</para>
|
|
|
|
<note>
|
|
|
|
<simpara>
|
|
|
|
Callback functions should return a type understood by SQLite (i.e.
|
|
|
|
<link linkend="language.types.intro">scalar type</link>).
|
|
|
|
</simpara>
|
|
|
|
</note>
|
2018-09-22 10:40:40 +00:00
|
|
|
<para>
|
|
|
|
This function need to be defined as:
|
|
|
|
<methodsynopsis>
|
|
|
|
<type>mixed</type><methodname><replaceable>callback</replaceable></methodname>
|
2020-11-02 11:17:26 +00:00
|
|
|
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
|
|
|
|
<methodparam rep="repeat"><type>mixed</type><parameter>values</parameter></methodparam>
|
2018-09-22 10:40:40 +00:00
|
|
|
</methodsynopsis>
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
2020-11-02 11:17:26 +00:00
|
|
|
<term><parameter>value</parameter></term>
|
2018-09-22 10:40:40 +00:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The first argument passed to the SQL function.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
2020-11-02 11:17:26 +00:00
|
|
|
<term><parameter>values</parameter></term>
|
2018-09-22 10:40:40 +00:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Further arguments passed to the SQL function.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
2005-09-09 03:25:37 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>num_args</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2017-03-17 14:17:07 +00:00
|
|
|
The number of arguments that the SQL function takes. If
|
|
|
|
this parameter is <literal>-1</literal>, then the SQL function may take
|
|
|
|
any number of arguments.
|
2005-09-09 03:25:37 +00:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2017-03-17 14:07:36 +00:00
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>flags</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
A bitwise conjunction of flags. Currently, only
|
|
|
|
<constant>PDO::SQLITE_DETERMINISTIC</constant> is supported, which specifies
|
|
|
|
that the function always returns the same result given the same inputs
|
|
|
|
within a single SQL statement.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2005-09-09 03:25:37 +00:00
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
2007-02-26 01:40:44 +00:00
|
|
|
<refsect1 role="returnvalues">
|
|
|
|
&reftitle.returnvalues;
|
|
|
|
<para>
|
|
|
|
&return.success;
|
2007-02-26 11:25:37 +00:00
|
|
|
</para>
|
2007-02-26 01:40:44 +00:00
|
|
|
</refsect1>
|
|
|
|
|
2017-03-17 14:07:36 +00:00
|
|
|
<refsect1 role="changelog">
|
|
|
|
&reftitle.changelog;
|
|
|
|
<informaltable>
|
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
|
|
|
<entry>&Version;</entry>
|
|
|
|
<entry>&Description;</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
<row>
|
2018-04-27 15:08:50 +00:00
|
|
|
<entry>7.1.4</entry>
|
2017-03-17 14:07:36 +00:00
|
|
|
<entry>
|
2018-04-27 15:08:50 +00:00
|
|
|
The <parameter>flags</parameter> parameter has been added.
|
2017-03-17 14:07:36 +00:00
|
|
|
</entry>
|
|
|
|
</row>
|
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</informaltable>
|
|
|
|
</refsect1>
|
|
|
|
|
2005-09-09 03:25:37 +00:00
|
|
|
<refsect1 role="examples">
|
|
|
|
&reftitle.examples;
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>PDO::sqliteCreateFunction</function> example</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
function md5_and_reverse($string)
|
|
|
|
{
|
|
|
|
return strrev(md5($string));
|
|
|
|
}
|
|
|
|
|
|
|
|
$db = new PDO('sqlite:sqlitedb');
|
|
|
|
$db->sqliteCreateFunction('md5rev', 'md5_and_reverse', 1);
|
|
|
|
$rows = $db->query('SELECT md5rev(filename) FROM files')->fetchAll();
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
In this example, we have a function that calculates the md5 sum of a
|
|
|
|
string, and then reverses it. When the SQL statement executes, it
|
|
|
|
returns the value of the filename transformed by our function. The data
|
2018-09-22 10:40:40 +00:00
|
|
|
returned in <literal>$rows</literal> contains the processed result.
|
2005-09-09 03:25:37 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The beauty of this technique is that you do not need to process the
|
2009-02-16 15:30:18 +00:00
|
|
|
result using a &foreach; loop after you have queried for the data.
|
2005-09-09 03:25:37 +00:00
|
|
|
</para>
|
|
|
|
<!-- not for PDO it doesn't, at least not yet
|
|
|
|
<para>
|
|
|
|
PHP registers a special function named <literal>php</literal> when the
|
|
|
|
database is first opened. The php function can be used to call any PHP
|
|
|
|
function without having to register it first.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>Example of using the PHP function</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
$rows = $db->query("SELECT php('md5', filename) from files")->fetchAll();
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
<para>
|
|
|
|
This example will call the <function>md5</function> on each
|
|
|
|
<literal>filename</literal> column in the database and return the result
|
|
|
|
into <parameter>$rows</parameter>
|
|
|
|
</para>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
-->
|
|
|
|
<tip>
|
|
|
|
<para>
|
2012-01-12 16:47:02 +00:00
|
|
|
You can use <xref linkend="pdo.sqlitecreatefunction" /> and
|
|
|
|
<xref linkend="pdo.sqlitecreateaggregate" /> to override SQLite
|
2007-02-02 00:26:54 +00:00
|
|
|
native SQL functions.
|
2005-09-09 03:25:37 +00:00
|
|
|
</para>
|
|
|
|
</tip>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="seealso">
|
|
|
|
&reftitle.seealso;
|
|
|
|
<para>
|
|
|
|
<simplelist>
|
2012-01-12 16:47:02 +00:00
|
|
|
<member><xref linkend="pdo.sqlitecreateaggregate" /></member>
|
2005-09-10 16:45:53 +00:00
|
|
|
<member><function>sqlite_create_function</function></member>
|
|
|
|
<member><function>sqlite_create_aggregate</function></member>
|
2005-09-09 03:25:37 +00:00
|
|
|
</simplelist>
|
|
|
|
</para>
|
2005-09-09 04:10:15 +00:00
|
|
|
</refsect1>
|
2005-09-09 03:25:37 +00:00
|
|
|
|
2005-09-09 16:50:02 +00:00
|
|
|
</refentry>
|
2005-09-09 03:25:37 +00:00
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:t
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
sgml-parent-document:nil
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2005-09-09 03:25:37 +00:00
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
|
|
vim: et tw=78 syn=sgml
|
|
|
|
vi: ts=1 sw=1
|
|
|
|
-->
|