cvs: peardoc /en/package/database mdb2.xml /en/package/database/mdb2 datatypes.xml intro-quote.xml
| From: | Lorenzo Alberton | Date: | Sat, 02 Dec 2006 15:38:38 +0000 |
| Subject: | cvs: peardoc /en/package/database mdb2.xml /en/package/database/mdb2 datatypes.xml intro-quote.xml | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-8974@lists.php.net to get a copy of this message | ||
quipo Sat Dec 2 15:38:38 2006 UTC
Added files:
/peardoc/en/package/database/mdb2 datatypes.xml
Modified files:
/peardoc/en/package/database mdb2.xml
/peardoc/en/package/database/mdb2 intro-quote.xml
Log:
converthed some of the documentation we had in html format, and added some new documentation on
quoting by Paul Scott.
please test!
http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/mdb2.xml?r1=1.12&r2=1.13&diff_format=u Index: peardoc/en/package/database/mdb2.xml diff -u peardoc/en/package/database/mdb2.xml:1.12 peardoc/en/package/database/mdb2.xml:1.13 --- peardoc/en/package/database/mdb2.xml:1.12 Mon Nov 27 21:28:49 2006 +++ peardoc/en/package/database/mdb2.xml Sat Dec 2 15:38:37 2006 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> -<!-- $Revision: 1.12 $ --> +<!-- $Revision: 1.13 $ --> <sect1 id="package.database.mdb2"> <title>MDB2</title> <para> @@ -17,6 +17,7 @@ &package.database.mdb2.intro-auto; &package.database.mdb2.intro-portability; &package.database.mdb2.intro-sequences; + &package.database.mdb2.datatypes; &package.database.mdb2.faq; </sect1> <!-- Keep this comment at the end of the file http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/mdb2/intro-quote.xml?r1=1.2&r2=1.3&diff_format=u Index: peardoc/en/package/database/mdb2/intro-quote.xml diff -u peardoc/en/package/database/mdb2/intro-quote.xml:1.2 peardoc/en/package/database/mdb2/intro-quote.xml:1.3 --- peardoc/en/package/database/mdb2/intro-quote.xml:1.2 Tue Nov 28 10:47:51 2006 +++ peardoc/en/package/database/mdb2/intro-quote.xml Sat Dec 2 15:38:38 2006 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1" ?> -<!-- $Revision: 1.2 $ --> +<!-- $Revision: 1.3 $ --> <refentry id="package.database.mdb2.intro-quote"> <refnamediv> @@ -17,7 +17,6 @@ whether or not to quote the value, and whether or not to escape the wildcards in the value. If you don't provide the datatype, it will be guessed from the value. </para> - <example> <title>Doing a query with quoted values</title> <programlisting role="php"> @@ -48,7 +47,33 @@ </programlisting> </example> <para> - If you use <link linkend="package.database.mdb2.intro-execute">prepared statements</link>, + With the third parameter of the <function>quote</function> you can specify + whether or not the above fields should be individually quoted: + </para> + <example> + <title>Individually choose the values to be quoted</title> + <programlisting role="php"> + <![CDATA[ +<?php +$query = 'INSERT INTO sometable (textfield1, boolfield2, datefield3) VALUES (' + .$mdb2->quote($val1, "text", true).', ' + .$mdb2->quote($val2, "boolean", false).', ' + .$mdb2->quote($val3, "date", true).')'; +?> + ]]> + </programlisting> + </example> + <para> + The above example will quote the fields and the resulting SQL will look as such: + <programlisting> + INSERT INTO sometable FIELDS (textfield1, boolfield2, datefield3) VALUES ('blah', 1, '2006-02-21') + </programlisting> + where the values defined were the values inserted accordingly. You will + notice that the "boolfield2" is unquoted as we specified &false; in the + <function>quote</function> method. + </para> + <para> + <emphasis>NB:</emphasis> If you use <link linkend="package.database.mdb2.intro-execute">prepared statements</link>, then quoting will be done automatically, you don't need to do it yourself. </para> </refsect1> @@ -63,7 +88,64 @@ problems than they solve. Anyway, it may be necessary when you have a reserved word as a field name (in this case, we suggest you to change it, if you can). </para> + <para> + Portability is broken by using the following characters inside delimited identifiers: + <itemizedlist> + <listitem> + <para>backtick (<literal>`</literal>) -- due to MySQL</para> + </listitem> + <listitem> + <para>double quote (") -- due to Oracle</para> + </listitem> + <listitem> + <para>brackets ([ or ]) -- due to Access</para> + </listitem> + </itemizedlist> + </para> + <para> + Delimited identifiers are known to generally work correctly under the following drivers: + <itemizedlist> + <listitem><para>mssql</para></listitem> + <listitem><para>mysql</para></listitem> + <listitem><para>mysqli</para></listitem> + <listitem><para>oci8</para></listitem> + <listitem><para>pgsql</para></listitem> + <listitem><para>sqlite</para></listitem> + </itemizedlist> + Firebird/InterBase doesn't seem to be able to use delimited identifiers + via PHP 4. They work fine under PHP 5. + </para> + <para> + + </para> + </refsect1> + + <refsect1 id="package.database.mdb2.intro-quote.quoting-options"> + <title>Quoting options</title> + <para> + Within the <classname>MDB2</classname> API there are a number of options to + set the quoting options, one of which simply quotes the identifiers within + the abstraction, the other quotes the field values on insert/update etc. + when using the prepared statements methods. + </para> + <para> + When using the <parameter>quoteIdentifiers</parameter> option, all of the + field identifiers will be automatically quoted in the resulting SQL statements: + <programlisting role="php"> +$mdb2->setOption('quote_identifiers', true); + </programlisting> + will result in a SQL statement that all the field names are quoted with the + backtick '<literal>`</literal>' operator (in MySQL). + <programlisting> +SELECT * FROM `sometable` WHERE `id` = '123'; + </programlisting> +as opposed to: + <programlisting> +SELECT * FROM sometable WHERE id='123'; + </programlisting> + </para> </refsect1> + <refsect1 id="package.database.mdb2.intro-quote.escape"> <title>Escape</title> @@ -77,6 +159,7 @@ <function>escapePattern</function> method. </para> </refsect1> + </refentry> <!-- Keep this comment at the end of the file http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/mdb2/datatypes.xml?view=markup&rev=1.1 Index: peardoc/en/package/database/mdb2/datatypes.xml +++ peardoc/en/package/database/mdb2/datatypes.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.database.mdb2.datatypes"> <refnamediv> <refname><classname>MDB2</classname> Datatypes</refname> <refpurpose>An overview of the MDB2 datatypes</refpurpose> </refnamediv> <refsect1> <title>Introduction</title> <para> All DBMS provide multiple choice of data types for the information that can be stored in their database table fields. However, the set of data types made available varies from DBMS to DBMS. </para> <para> To simplify the interface with the DBMS supported by <classname>MDB2</classname>, it was defined a base set of data types that applications may access independently of the underlying DBMS. </para> <para> The <classname>MDB2</classname> applications programming interface takes care of mapping data types when managing database options. It is also able to convert that is sent to and received from the underlying DBMS using the respective driver. </para> <para> The following data type examples should be used with MDB2's <function>createTable</function> method. The example array at the end of the data types section may be used with <function>createTable</function> to create a portable table on the DBMS of choice (please refer to the main MDB2 documentation to find out what DBMS back ends are properly supported). It should also be noted that the following examples do not cover the creation and maintenance of indices, this chapter is only concerned with data types and the proper usage thereof. </para> </refsect1> <refsect1> <title>"Global" table type modifiers</title> <para> Within the MDB2 API there are a few modifiers that have been designed to aid in optimal table design. These are: <itemizedlist> <listitem> <para>The <parameter>NOTNULL</parameter> identifier</para> </listitem> <listitem> <para>The <parameter>LENGTH</parameter> identifier</para> </listitem> <listitem> <para>The <parameter>DEFAULT</parameter> identifier</para> </listitem> <listitem> <para><parameter>UNSIGNED</parameter> for some field definitions, although not all DBMS's support this modifier for integer field types. </para> </listitem> </itemizedlist> Building upon the above, we can say that the modifiers alter the field definition to create more specific field types for specific usage scenarios. The <parameter>notnull</parameter> modifier will be used in the following way to set the default DBMS NOT NULL Flag on the field to true or false, depending on the DBMS's definition of the field value: In PostgreSQL the "NOT NULL" definition will be set to "NOT NULL", whilst in MySQL (for example) the "NULL" option will be set to "NO". In order to define a "NOT NULL" field type, we simply add an extra parameter to our definition array (See the examples in the following section) <programlisting role="php"> <![CDATA[ 'sometime' = array( 'type' = 'time', 'default' = '12:34:05', 'notnull' = true, ), ]]> </programlisting> Using the above example, we can also explore the <parameter>default</parameter> field operator. Default is set in the same way as the <parameter>notnull</parameter> operator to set a default value for the field. This value may be set in any character set that the DBMS supports for text fields, and any other valid data for the field's data type. In the above example, we have specified a valid time for the "Time" data type, '12:34:05'. Remember that when setting default dates and times, as well as datetimes, you should research and stay within the epoch of your chosen DBMS, otherwise you will encounter difficult to diagnose errors! </para> <para> <example> <title>Example of the <parameter>length</parameter> modifier</title> <programlisting role="php"> <![CDATA[ 'sometext' = array( 'type' = 'text', 'length' = 12, ), ]]> </programlisting> </example> </para> <para> The above example will create a character varying field of length 12 characters in the database table. If the length definition is left out, MDB2 will create a length of the maximum allowable length for the data type specified, which may create a problem with some field types and indexing. Best practice is to define lengths for all or most of your fields. </para> </refsect1> <refsect1> <title>Text data type</title> <para> The text data type is available with two options for the length: one that is explicitly length limited and another of undefined length that should be as large as the database allows. </para> <para> The length limited option is the most recommended for efficiency reasons. The undefined length option allows very large fields but may prevent the use of indexes and may not allow sorting on fields of its type. </para> <para> The fields of this type should be able to handle 8 bit characters. Drivers take care of DBMS specific escaping of characters of special meaning with the values of the strings to be converted to this type. </para> <para> <example> <title>Example of text data type with <parameter>length</parameter> option</title> <programlisting role="php"> <![CDATA[ <?php 'sometext' = array( 'type' = 'text', 'length' = 12, ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Boolean data type</title> <para> The boolean data type represents only two values that can be either 1 or 0. Do not assume that these data types are stored as integers because some DBMS drivers may implement this type with single character text fields for a matter of efficiency. Ternary logic is possible by using null as the third possible value that may be assigned to fields of this type. </para> <para> <example> <title>Example of boolean data type</title> <programlisting role="php"> <![CDATA[ <?php 'someboolean' = array( 'type' = 'boolean', ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Integer data type</title> <para> The integer data type may store integer values as large as each DBMS may handle. Fields of this type may be created optionally as unsigned integers but not all DBMS support it. Therefore, such option may be ignored. Truly portable applications should not rely on the availability of this option. </para> <para> <example> <title>Example of integer data type</title> <programlisting role="php"> <![CDATA[ <?php 'someint' = array( 'type' = 'integer', 'length' = 10, 'unsigned' = true, ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Decimal data type</title> <para> The decimal data type may store decimal numbers accurately with a fixed number of decimal places. This data type is suitable for representing accurate values like currency amounts. </para> <para> Some DBMS drivers may emulate the decimal data type using integers. Such drivers need to know in advance how many decimal places that should be used to perform eventual scale conversion when storing and retrieving values from a database. Despite this, applications may use arithmetic expressions and functions with the values stored on decimal type fields as long as any constant values that are used in the expressions are also converted with the respective MDB2 conversion functions. </para> <para> The number of places that are used to the left and the right of the decimal point is pre-determined and fixed for all decimal values stored in the same database. By default, MDB2 uses 2 places to the right of the decimal point, but this may be changed when setting the database connection. The number of places available to the right of the decimal point depend on the DBMS. </para> <para> It is not recommended to change the number places used to represent decimal values in database after it is installed. MDB2 does not keep track of changes in the number of decimal places. The number of decimal places can be set using the <function>setOption</function> method. </para> <para> <example> <title>Example of decimal data type</title> <programlisting role="php"> <![CDATA[ <?php 'somedecimal' = array( 'type' = 'decimal', ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Float data type</title> <para> The float data type may store floating point decimal numbers. This data type is suitable for representing numbers within a large scale range that do not require high accuracy. The scale and the precision limits of the values that may be stored in a database depends on the DBMS that it is used. </para> <para> <example> <title>Example of float data type</title> <programlisting role="php"> <![CDATA[ <?php 'somefloat' = array( 'type' = 'float', ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Date data type</title> <para> The date data type may represent dates with year, month and day. DBMS independent representation of dates is accomplished by using text strings formatted according to the <emphasis>IS0-8601</emphasis> standard. </para> <para> The format defined by the <emphasis>ISO-8601</emphasis> standard for dates is <literal>YYYY-MM-DD</literal> where <literal>YYYY<literal> is the number of the year (Gregorian calendar), <literal>MM</literal> is the number of the month from 01 to 12 and <literal>DD</literal> is the number of the day from 01 to 31. Months or days numbered below 10 should be padded on the left with 0. </para> <para> Some DBMS have native support for date formats, but for others the DBMS driver may have to represent them as integers or text values. In any case, it is always possible to make comparisons between date values as well sort query results by fields of this type. </para> <para> <example> <title>Example of date data type</title> <programlisting role="php"> <![CDATA[ <?php 'somedate' = array( 'type' = 'date', ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Time data type</title> <para> The time data type may represent the time of a given moment of the day. DBMS independent representation of the time of the day is also accomplished by using text strings formatted according to the <emphasis>ISO-8601</emphasis> standard. </para> <para> The format defined by the <emphasis>ISO-8601</emphasis> standard for the time of the day is <literal>HH:MI:SS</literal> where <literal>HH</literal> is the number of hour the day from 00 to 23 and <literal>MI</literal> and <literal>SS</literal> are respectively the number of the minute and of the second from 00 to 59. Hours, minutes and seconds numbered below 10 should be padded on the left with 0. </para> <para> Some DBMS have native support for time of the day formats, but for others the DBMS driver may have to represent them as integers or text values. In any case, it is always possible to make comparisons between time values as well sort query results by fields of this type. </para> <para> <example> <title>Example of time data type</title> <programlisting role="php"> <![CDATA[ <?php 'sometime' = array( 'type' = 'time', 'default' = '12:34:05', 'notnull' = true, ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Timestamp data type</title> <para> The timestamp data type is a mere combination of the date and the time of the day data types. The representation of values of the time stamp type is accomplished by joining the date and time string values in a single string joined by a space. Therefore, the format template is <literal>YYYY-MM-DD HH:MI:SS</literal>. The represented values obey the same rules and ranges described for the date and time data types </para> <para> <example> <title>Example of timestamp data type</title> <programlisting role="php"> <![CDATA[ <?php 'sometimestamp' = array( 'type' = 'timestamp', ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Large object (file) data types</title> <para> The large object data types are meant to store data of undefined length that may be too large to store in text fields, like data that is usually stored in files. </para> <para> <classname>MDB2</classname> supports two types of large object fields: Character Large OBjects (<emphasis>CLOBs</emphasis>) and Binary Large OBjects (<emphasis>BLOBs</emphasis>). <emphasis>CLOB</emphasis> fields are meant to store only data made of printable ASCII characters. <emphasis>BLOB</emphasis> fields are meant to store all types of data. </para> <para> Large object fields are usually not meant to be used as parameters of query search clause (<literal>WHERE</literal>) unless the underlying DBMS supports a feature usually known as <emphasis>"full text search"</emphasis> </para> <para> <example> <title>Example of large object data types</title> <programlisting role="php"> <![CDATA[ <?php 'someblob' = array( 'type' = 'blob', ), // or 'someclob' = array( 'type' = 'clob', ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Putting it all together</title> <para> <example> <title>Example of field definition</title> <programlisting role="php"> <![CDATA[ <?php $fields = array( 'id' => array( 'type' => 'text', 'length' => 32, 'unsigned' => true, ), 'someint' => array( 'type' => 'integer', 'length' => 10, 'unsigned' => true, ), 'sometext' => array( 'type' => 'text', 'length' => 12, ), 'somedate' => array( 'type' => 'date', ), 'sometimestamp' => array( 'type' => 'timestamp', ), 'someboolean' => array( 'type' => 'boolean', ), 'somedecimal' => array( 'type' => 'decimal', ), 'somefloat' => array( 'type' => 'float', ), 'sometime' => array( 'type' => 'time', 'default' => '12:34:05', 'notnull' => true, ), 'somedate' => array( 'type' => 'date', ), 'someclob' => array( 'type' => 'clob', ), 'someblob' => array( 'type' => 'blob', ), ); ?> ]]> </programlisting> </example> </para> <para> The above example will create a database table as such: <table> <title>PostgreSQL</title> <tgroup cols="5"> <thead> <row> <entry>Column</entry> <entry>Type</entry> <entry>Not Null</entry> <entry>Default</entry> <entry>comment</entry> </row> </thead> <tbody> <row> <entry>id</entry> <entry>character varying(32)</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>somename</entry> <entry>character varying(12)</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>somedate</entry> <entry>date</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>sometimestamp</entry> <entry>timestamp without time zone</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>someboolean</entry> <entry>boolean</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>somedecimal</entry> <entry>numeric(18,2)</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>somefloat</entry> <entry>double precision</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>sometime</entry> <entry>time without time zone</entry> <entry>NOT NULL</entry> <entry>'12:34:05'::time without time zone</entry> <entry></entry> </row> <row> <entry>someclob</entry> <entry>text</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>someblob</entry> <entry>bytea</entry> <entry></entry> <entry></entry> <entry></entry> </row> </tbody> </tgroup> </table> </para> <para> <table> <title>MySQL</title> <tgroup cols="7"> <thead> <row> <entry>Field</entry> <entry>Type</entry> <entry>Collation</entry> <entry>Attributes</entry> <entry>Null</entry> <entry>Default</entry> <entry>comment</entry> </row> </thead> <tbody> <row> <entry>id</entry> <entry>varchar(32)</entry> <entry></entry> <entry>unsigned</entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>somename</entry> <entry>varchar(12)</entry> <entry>latin1_swedish_ci</entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>somedate</entry> <entry>date</entry> <entry></entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>sometimestamp</entry> <entry>timestamp without time zone</entry> <entry></entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>someboolean</entry> <entry>tinyint(1)</entry> <entry></entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>somedecimal</entry> <entry>decimal(18,2)</entry> <entry></entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>somefloat</entry> <entry>double</entry> <entry></entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>sometime</entry> <entry>time</entry> <entry></entry> <entry></entry> <entry>NO</entry> <entry>12:34:05</entry> <entry></entry> </row> <row> <entry>someclob</entry> <entry>longtext</entry> <entry>latin1_swedish_ci</entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>someblob</entry> <entry>longblob</entry> <entry></entry> <entry>binary</entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> </tbody> </tgroup> </table> </para> </refsect1> <refsect1> <title>Custom Data Types and Further reading</title> <para> Custom data types can be defined. This will be explored soon. </para> <para> Further reading should be done at the following URL's: <ulink url="http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Datatype_Common.html#methodgetTypeDeclaration">methodgetTypeDeclaration</ulink>, <ulink url="http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Datatype_Common.html#methodgetDeclaration">methodgetDeclaration</ulink>. </para> </refsect1> </refentry> <!-- 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 sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" 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 -->
http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/mdb2.xml?r1=1.12&r2=1.13&diff_format=u Index: peardoc/en/package/database/mdb2.xml diff -u peardoc/en/package/database/mdb2.xml:1.12 peardoc/en/package/database/mdb2.xml:1.13 --- peardoc/en/package/database/mdb2.xml:1.12 Mon Nov 27 21:28:49 2006 +++ peardoc/en/package/database/mdb2.xml Sat Dec 2 15:38:37 2006 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> -<!-- $Revision: 1.12 $ --> +<!-- $Revision: 1.13 $ --> <sect1 id="package.database.mdb2"> <title>MDB2</title> <para> @@ -17,6 +17,7 @@ &package.database.mdb2.intro-auto; &package.database.mdb2.intro-portability; &package.database.mdb2.intro-sequences; + &package.database.mdb2.datatypes; &package.database.mdb2.faq; </sect1> <!-- Keep this comment at the end of the file http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/mdb2/intro-quote.xml?r1=1.2&r2=1.3&diff_format=u Index: peardoc/en/package/database/mdb2/intro-quote.xml diff -u peardoc/en/package/database/mdb2/intro-quote.xml:1.2 peardoc/en/package/database/mdb2/intro-quote.xml:1.3 --- peardoc/en/package/database/mdb2/intro-quote.xml:1.2 Tue Nov 28 10:47:51 2006 +++ peardoc/en/package/database/mdb2/intro-quote.xml Sat Dec 2 15:38:38 2006 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1" ?> -<!-- $Revision: 1.2 $ --> +<!-- $Revision: 1.3 $ --> <refentry id="package.database.mdb2.intro-quote"> <refnamediv> @@ -17,7 +17,6 @@ whether or not to quote the value, and whether or not to escape the wildcards in the value. If you don't provide the datatype, it will be guessed from the value. </para> - <example> <title>Doing a query with quoted values</title> <programlisting role="php"> @@ -48,7 +47,33 @@ </programlisting> </example> <para> - If you use <link linkend="package.database.mdb2.intro-execute">prepared statements</link>, + With the third parameter of the <function>quote</function> you can specify + whether or not the above fields should be individually quoted: + </para> + <example> + <title>Individually choose the values to be quoted</title> + <programlisting role="php"> + <![CDATA[ +<?php +$query = 'INSERT INTO sometable (textfield1, boolfield2, datefield3) VALUES (' + .$mdb2->quote($val1, "text", true).', ' + .$mdb2->quote($val2, "boolean", false).', ' + .$mdb2->quote($val3, "date", true).')'; +?> + ]]> + </programlisting> + </example> + <para> + The above example will quote the fields and the resulting SQL will look as such: + <programlisting> + INSERT INTO sometable FIELDS (textfield1, boolfield2, datefield3) VALUES ('blah', 1, '2006-02-21') + </programlisting> + where the values defined were the values inserted accordingly. You will + notice that the "boolfield2" is unquoted as we specified &false; in the + <function>quote</function> method. + </para> + <para> + <emphasis>NB:</emphasis> If you use <link linkend="package.database.mdb2.intro-execute">prepared statements</link>, then quoting will be done automatically, you don't need to do it yourself. </para> </refsect1> @@ -63,7 +88,64 @@ problems than they solve. Anyway, it may be necessary when you have a reserved word as a field name (in this case, we suggest you to change it, if you can). </para> + <para> + Portability is broken by using the following characters inside delimited identifiers: + <itemizedlist> + <listitem> + <para>backtick (<literal>`</literal>) -- due to MySQL</para> + </listitem> + <listitem> + <para>double quote (") -- due to Oracle</para> + </listitem> + <listitem> + <para>brackets ([ or ]) -- due to Access</para> + </listitem> + </itemizedlist> + </para> + <para> + Delimited identifiers are known to generally work correctly under the following drivers: + <itemizedlist> + <listitem><para>mssql</para></listitem> + <listitem><para>mysql</para></listitem> + <listitem><para>mysqli</para></listitem> + <listitem><para>oci8</para></listitem> + <listitem><para>pgsql</para></listitem> + <listitem><para>sqlite</para></listitem> + </itemizedlist> + Firebird/InterBase doesn't seem to be able to use delimited identifiers + via PHP 4. They work fine under PHP 5. + </para> + <para> + + </para> + </refsect1> + + <refsect1 id="package.database.mdb2.intro-quote.quoting-options"> + <title>Quoting options</title> + <para> + Within the <classname>MDB2</classname> API there are a number of options to + set the quoting options, one of which simply quotes the identifiers within + the abstraction, the other quotes the field values on insert/update etc. + when using the prepared statements methods. + </para> + <para> + When using the <parameter>quoteIdentifiers</parameter> option, all of the + field identifiers will be automatically quoted in the resulting SQL statements: + <programlisting role="php"> +$mdb2->setOption('quote_identifiers', true); + </programlisting> + will result in a SQL statement that all the field names are quoted with the + backtick '<literal>`</literal>' operator (in MySQL). + <programlisting> +SELECT * FROM `sometable` WHERE `id` = '123'; + </programlisting> +as opposed to: + <programlisting> +SELECT * FROM sometable WHERE id='123'; + </programlisting> + </para> </refsect1> + <refsect1 id="package.database.mdb2.intro-quote.escape"> <title>Escape</title> @@ -77,6 +159,7 @@ <function>escapePattern</function> method. </para> </refsect1> + </refentry> <!-- Keep this comment at the end of the file http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/mdb2/datatypes.xml?view=markup&rev=1.1 Index: peardoc/en/package/database/mdb2/datatypes.xml +++ peardoc/en/package/database/mdb2/datatypes.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.database.mdb2.datatypes"> <refnamediv> <refname><classname>MDB2</classname> Datatypes</refname> <refpurpose>An overview of the MDB2 datatypes</refpurpose> </refnamediv> <refsect1> <title>Introduction</title> <para> All DBMS provide multiple choice of data types for the information that can be stored in their database table fields. However, the set of data types made available varies from DBMS to DBMS. </para> <para> To simplify the interface with the DBMS supported by <classname>MDB2</classname>, it was defined a base set of data types that applications may access independently of the underlying DBMS. </para> <para> The <classname>MDB2</classname> applications programming interface takes care of mapping data types when managing database options. It is also able to convert that is sent to and received from the underlying DBMS using the respective driver. </para> <para> The following data type examples should be used with MDB2's <function>createTable</function> method. The example array at the end of the data types section may be used with <function>createTable</function> to create a portable table on the DBMS of choice (please refer to the main MDB2 documentation to find out what DBMS back ends are properly supported). It should also be noted that the following examples do not cover the creation and maintenance of indices, this chapter is only concerned with data types and the proper usage thereof. </para> </refsect1> <refsect1> <title>"Global" table type modifiers</title> <para> Within the MDB2 API there are a few modifiers that have been designed to aid in optimal table design. These are: <itemizedlist> <listitem> <para>The <parameter>NOTNULL</parameter> identifier</para> </listitem> <listitem> <para>The <parameter>LENGTH</parameter> identifier</para> </listitem> <listitem> <para>The <parameter>DEFAULT</parameter> identifier</para> </listitem> <listitem> <para><parameter>UNSIGNED</parameter> for some field definitions, although not all DBMS's support this modifier for integer field types. </para> </listitem> </itemizedlist> Building upon the above, we can say that the modifiers alter the field definition to create more specific field types for specific usage scenarios. The <parameter>notnull</parameter> modifier will be used in the following way to set the default DBMS NOT NULL Flag on the field to true or false, depending on the DBMS's definition of the field value: In PostgreSQL the "NOT NULL" definition will be set to "NOT NULL", whilst in MySQL (for example) the "NULL" option will be set to "NO". In order to define a "NOT NULL" field type, we simply add an extra parameter to our definition array (See the examples in the following section) <programlisting role="php"> <![CDATA[ 'sometime' = array( 'type' = 'time', 'default' = '12:34:05', 'notnull' = true, ), ]]> </programlisting> Using the above example, we can also explore the <parameter>default</parameter> field operator. Default is set in the same way as the <parameter>notnull</parameter> operator to set a default value for the field. This value may be set in any character set that the DBMS supports for text fields, and any other valid data for the field's data type. In the above example, we have specified a valid time for the "Time" data type, '12:34:05'. Remember that when setting default dates and times, as well as datetimes, you should research and stay within the epoch of your chosen DBMS, otherwise you will encounter difficult to diagnose errors! </para> <para> <example> <title>Example of the <parameter>length</parameter> modifier</title> <programlisting role="php"> <![CDATA[ 'sometext' = array( 'type' = 'text', 'length' = 12, ), ]]> </programlisting> </example> </para> <para> The above example will create a character varying field of length 12 characters in the database table. If the length definition is left out, MDB2 will create a length of the maximum allowable length for the data type specified, which may create a problem with some field types and indexing. Best practice is to define lengths for all or most of your fields. </para> </refsect1> <refsect1> <title>Text data type</title> <para> The text data type is available with two options for the length: one that is explicitly length limited and another of undefined length that should be as large as the database allows. </para> <para> The length limited option is the most recommended for efficiency reasons. The undefined length option allows very large fields but may prevent the use of indexes and may not allow sorting on fields of its type. </para> <para> The fields of this type should be able to handle 8 bit characters. Drivers take care of DBMS specific escaping of characters of special meaning with the values of the strings to be converted to this type. </para> <para> <example> <title>Example of text data type with <parameter>length</parameter> option</title> <programlisting role="php"> <![CDATA[ <?php 'sometext' = array( 'type' = 'text', 'length' = 12, ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Boolean data type</title> <para> The boolean data type represents only two values that can be either 1 or 0. Do not assume that these data types are stored as integers because some DBMS drivers may implement this type with single character text fields for a matter of efficiency. Ternary logic is possible by using null as the third possible value that may be assigned to fields of this type. </para> <para> <example> <title>Example of boolean data type</title> <programlisting role="php"> <![CDATA[ <?php 'someboolean' = array( 'type' = 'boolean', ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Integer data type</title> <para> The integer data type may store integer values as large as each DBMS may handle. Fields of this type may be created optionally as unsigned integers but not all DBMS support it. Therefore, such option may be ignored. Truly portable applications should not rely on the availability of this option. </para> <para> <example> <title>Example of integer data type</title> <programlisting role="php"> <![CDATA[ <?php 'someint' = array( 'type' = 'integer', 'length' = 10, 'unsigned' = true, ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Decimal data type</title> <para> The decimal data type may store decimal numbers accurately with a fixed number of decimal places. This data type is suitable for representing accurate values like currency amounts. </para> <para> Some DBMS drivers may emulate the decimal data type using integers. Such drivers need to know in advance how many decimal places that should be used to perform eventual scale conversion when storing and retrieving values from a database. Despite this, applications may use arithmetic expressions and functions with the values stored on decimal type fields as long as any constant values that are used in the expressions are also converted with the respective MDB2 conversion functions. </para> <para> The number of places that are used to the left and the right of the decimal point is pre-determined and fixed for all decimal values stored in the same database. By default, MDB2 uses 2 places to the right of the decimal point, but this may be changed when setting the database connection. The number of places available to the right of the decimal point depend on the DBMS. </para> <para> It is not recommended to change the number places used to represent decimal values in database after it is installed. MDB2 does not keep track of changes in the number of decimal places. The number of decimal places can be set using the <function>setOption</function> method. </para> <para> <example> <title>Example of decimal data type</title> <programlisting role="php"> <![CDATA[ <?php 'somedecimal' = array( 'type' = 'decimal', ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Float data type</title> <para> The float data type may store floating point decimal numbers. This data type is suitable for representing numbers within a large scale range that do not require high accuracy. The scale and the precision limits of the values that may be stored in a database depends on the DBMS that it is used. </para> <para> <example> <title>Example of float data type</title> <programlisting role="php"> <![CDATA[ <?php 'somefloat' = array( 'type' = 'float', ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Date data type</title> <para> The date data type may represent dates with year, month and day. DBMS independent representation of dates is accomplished by using text strings formatted according to the <emphasis>IS0-8601</emphasis> standard. </para> <para> The format defined by the <emphasis>ISO-8601</emphasis> standard for dates is <literal>YYYY-MM-DD</literal> where <literal>YYYY<literal> is the number of the year (Gregorian calendar), <literal>MM</literal> is the number of the month from 01 to 12 and <literal>DD</literal> is the number of the day from 01 to 31. Months or days numbered below 10 should be padded on the left with 0. </para> <para> Some DBMS have native support for date formats, but for others the DBMS driver may have to represent them as integers or text values. In any case, it is always possible to make comparisons between date values as well sort query results by fields of this type. </para> <para> <example> <title>Example of date data type</title> <programlisting role="php"> <![CDATA[ <?php 'somedate' = array( 'type' = 'date', ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Time data type</title> <para> The time data type may represent the time of a given moment of the day. DBMS independent representation of the time of the day is also accomplished by using text strings formatted according to the <emphasis>ISO-8601</emphasis> standard. </para> <para> The format defined by the <emphasis>ISO-8601</emphasis> standard for the time of the day is <literal>HH:MI:SS</literal> where <literal>HH</literal> is the number of hour the day from 00 to 23 and <literal>MI</literal> and <literal>SS</literal> are respectively the number of the minute and of the second from 00 to 59. Hours, minutes and seconds numbered below 10 should be padded on the left with 0. </para> <para> Some DBMS have native support for time of the day formats, but for others the DBMS driver may have to represent them as integers or text values. In any case, it is always possible to make comparisons between time values as well sort query results by fields of this type. </para> <para> <example> <title>Example of time data type</title> <programlisting role="php"> <![CDATA[ <?php 'sometime' = array( 'type' = 'time', 'default' = '12:34:05', 'notnull' = true, ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Timestamp data type</title> <para> The timestamp data type is a mere combination of the date and the time of the day data types. The representation of values of the time stamp type is accomplished by joining the date and time string values in a single string joined by a space. Therefore, the format template is <literal>YYYY-MM-DD HH:MI:SS</literal>. The represented values obey the same rules and ranges described for the date and time data types </para> <para> <example> <title>Example of timestamp data type</title> <programlisting role="php"> <![CDATA[ <?php 'sometimestamp' = array( 'type' = 'timestamp', ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Large object (file) data types</title> <para> The large object data types are meant to store data of undefined length that may be too large to store in text fields, like data that is usually stored in files. </para> <para> <classname>MDB2</classname> supports two types of large object fields: Character Large OBjects (<emphasis>CLOBs</emphasis>) and Binary Large OBjects (<emphasis>BLOBs</emphasis>). <emphasis>CLOB</emphasis> fields are meant to store only data made of printable ASCII characters. <emphasis>BLOB</emphasis> fields are meant to store all types of data. </para> <para> Large object fields are usually not meant to be used as parameters of query search clause (<literal>WHERE</literal>) unless the underlying DBMS supports a feature usually known as <emphasis>"full text search"</emphasis> </para> <para> <example> <title>Example of large object data types</title> <programlisting role="php"> <![CDATA[ <?php 'someblob' = array( 'type' = 'blob', ), // or 'someclob' = array( 'type' = 'clob', ), ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1> <title>Putting it all together</title> <para> <example> <title>Example of field definition</title> <programlisting role="php"> <![CDATA[ <?php $fields = array( 'id' => array( 'type' => 'text', 'length' => 32, 'unsigned' => true, ), 'someint' => array( 'type' => 'integer', 'length' => 10, 'unsigned' => true, ), 'sometext' => array( 'type' => 'text', 'length' => 12, ), 'somedate' => array( 'type' => 'date', ), 'sometimestamp' => array( 'type' => 'timestamp', ), 'someboolean' => array( 'type' => 'boolean', ), 'somedecimal' => array( 'type' => 'decimal', ), 'somefloat' => array( 'type' => 'float', ), 'sometime' => array( 'type' => 'time', 'default' => '12:34:05', 'notnull' => true, ), 'somedate' => array( 'type' => 'date', ), 'someclob' => array( 'type' => 'clob', ), 'someblob' => array( 'type' => 'blob', ), ); ?> ]]> </programlisting> </example> </para> <para> The above example will create a database table as such: <table> <title>PostgreSQL</title> <tgroup cols="5"> <thead> <row> <entry>Column</entry> <entry>Type</entry> <entry>Not Null</entry> <entry>Default</entry> <entry>comment</entry> </row> </thead> <tbody> <row> <entry>id</entry> <entry>character varying(32)</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>somename</entry> <entry>character varying(12)</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>somedate</entry> <entry>date</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>sometimestamp</entry> <entry>timestamp without time zone</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>someboolean</entry> <entry>boolean</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>somedecimal</entry> <entry>numeric(18,2)</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>somefloat</entry> <entry>double precision</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>sometime</entry> <entry>time without time zone</entry> <entry>NOT NULL</entry> <entry>'12:34:05'::time without time zone</entry> <entry></entry> </row> <row> <entry>someclob</entry> <entry>text</entry> <entry></entry> <entry></entry> <entry></entry> </row> <row> <entry>someblob</entry> <entry>bytea</entry> <entry></entry> <entry></entry> <entry></entry> </row> </tbody> </tgroup> </table> </para> <para> <table> <title>MySQL</title> <tgroup cols="7"> <thead> <row> <entry>Field</entry> <entry>Type</entry> <entry>Collation</entry> <entry>Attributes</entry> <entry>Null</entry> <entry>Default</entry> <entry>comment</entry> </row> </thead> <tbody> <row> <entry>id</entry> <entry>varchar(32)</entry> <entry></entry> <entry>unsigned</entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>somename</entry> <entry>varchar(12)</entry> <entry>latin1_swedish_ci</entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>somedate</entry> <entry>date</entry> <entry></entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>sometimestamp</entry> <entry>timestamp without time zone</entry> <entry></entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>someboolean</entry> <entry>tinyint(1)</entry> <entry></entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>somedecimal</entry> <entry>decimal(18,2)</entry> <entry></entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>somefloat</entry> <entry>double</entry> <entry></entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>sometime</entry> <entry>time</entry> <entry></entry> <entry></entry> <entry>NO</entry> <entry>12:34:05</entry> <entry></entry> </row> <row> <entry>someclob</entry> <entry>longtext</entry> <entry>latin1_swedish_ci</entry> <entry></entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> <row> <entry>someblob</entry> <entry>longblob</entry> <entry></entry> <entry>binary</entry> <entry>YES</entry> <entry></entry> <entry></entry> </row> </tbody> </tgroup> </table> </para> </refsect1> <refsect1> <title>Custom Data Types and Further reading</title> <para> Custom data types can be defined. This will be explored soon. </para> <para> Further reading should be done at the following URL's: <ulink url="http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Datatype_Common.html#methodgetTypeDeclaration">methodgetTypeDeclaration</ulink>, <ulink url="http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Datatype_Common.html#methodgetDeclaration">methodgetDeclaration</ulink>. </para> </refsect1> </refentry> <!-- 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 sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" 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 -->