MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file sqlite.php

Documentation is available at sqlite.php

  1. <?php
  2. // vim: set et ts=4 sw=4 fdm=marker:
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox,                 |
  7. // | Stig. S. Bakken, Lukas Smith                                         |
  8. // | All rights reserved.                                                 |
  9. // +----------------------------------------------------------------------+
  10. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
  11. // | API as well as database abstraction for PHP applications.            |
  12. // | This LICENSE is in the BSD license style.                            |
  13. // |                                                                      |
  14. // | Redistribution and use in source and binary forms, with or without   |
  15. // | modification, are permitted provided that the following conditions   |
  16. // | are met:                                                             |
  17. // |                                                                      |
  18. // | Redistributions of source code must retain the above copyright       |
  19. // | notice, this list of conditions and the following disclaimer.        |
  20. // |                                                                      |
  21. // | Redistributions in binary form must reproduce the above copyright    |
  22. // | notice, this list of conditions and the following disclaimer in the  |
  23. // | documentation and/or other materials provided with the distribution. |
  24. // |                                                                      |
  25. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  26. // | Lukas Smith nor the names of his contributors may be used to endorse |
  27. // | or promote products derived from this software without specific prior|
  28. // | written permission.                                                  |
  29. // |                                                                      |
  30. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  31. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  32. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  33. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  34. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  35. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  36. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  37. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  38. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  39. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  40. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  41. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  42. // +----------------------------------------------------------------------+
  43. // | Author: Lukas Smith <smith@backendmedia.com>                         |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: sqlite.php,v 1.6 2004/03/29 09:20:38 quipo Exp $
  47. //
  48.  
  49. require_once 'MDB2/Driver/Datatype/Common.php';
  50.  
  51. /**
  52.  * MDB2 SQLite driver
  53.  *
  54.  * @package MDB2
  55.  * @category Database
  56.  * @author  Lukas Smith <smith@backendmedia.com>
  57.  */
  58. {
  59.     // }}}
  60.     // {{{ constructor
  61.  
  62.     /**
  63.      * Constructor
  64.      */
  65.     function MDB2_Driver_Datatype_sqlite($db_index)
  66.     {
  67.         $this->MDB2_Driver_Datatype_Common($db_index);
  68.     }
  69.  
  70.     // }}}
  71.     // {{{ convertResult()
  72.  
  73.     /**
  74.      * convert a value to a RDBMS indepdenant MDB2 type
  75.      *
  76.      * @param mixed  $value   value to be converted
  77.      * @param int    $type    constant that specifies which type to convert to
  78.      * @return mixed converted value
  79.      * @access public
  80.      */
  81.     function convertResult($value$type)
  82.     {
  83.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  84.         switch ($type{
  85.             case MDB2_TYPE_DECIMAL:
  86.                 return sprintf('%.'.$db->options['decimal_places'].'f'doubleval($value)/pow(10.0$db->options['decimal_places']));
  87.             default:
  88.                 return $this->_baseConvertResult($value$type);
  89.         }
  90.     }
  91.  
  92.     // }}}
  93.     // {{{ getIntegerDeclaration()
  94.  
  95.     /**
  96.      * Obtain DBMS specific SQL code portion needed to declare an integer type
  97.      * field to be used in statements like CREATE TABLE.
  98.      *
  99.      * @param string  $name   name the field to be declared.
  100.      * @param string  $field  associative array with the name of the properties
  101.      *                         of the field being declared as array indexes.
  102.      *                         Currently, the types of supported field
  103.      *                         properties are as follows:
  104.      *
  105.      *                        unsigned
  106.      *                         Boolean flag that indicates whether the field
  107.      *                         should be declared as unsigned integer if
  108.      *                         possible.
  109.      *
  110.      *                        default
  111.      *                         Integer value to be used as default for this
  112.      *                         field.
  113.      *
  114.      *                        notnull
  115.      *                         Boolean flag that indicates whether this field is
  116.      *                         constrained to not be set to null.
  117.      * @return string  DBMS specific SQL code portion that should be used to
  118.      *                  declare the specified field.
  119.      * @access public
  120.      */
  121.     function getIntegerDeclaration($name$field)
  122.     {
  123.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  124.         $unsigned = isset($field['unsigned']' UNSIGNED' '';
  125.         $default = isset($field['default']' DEFAULT '.
  126.             $this->quoteInteger($field['default']'';
  127.         $notnull = isset($field['notnull']' NOT NULL' '';
  128.         return $name.' INT'.$unsigned.$default.$notnull;
  129.        ;
  130.     }
  131.  
  132.     // }}}
  133.     // {{{ getCLOBDeclaration()
  134.  
  135.     /**
  136.      * Obtain DBMS specific SQL code portion needed to declare an character
  137.      * large object type field to be used in statements like CREATE TABLE.
  138.      *
  139.      * @param string  $name   name the field to be declared.
  140.      * @param string  $field  associative array with the name of the
  141.      *                         properties of the field being declared as array
  142.      *                         indexes. Currently, the types of supported field
  143.      *                         properties are as follows:
  144.      *
  145.      *                        length
  146.      *                         Integer value that determines the maximum length
  147.      *                         of the large object field. If this argument is
  148.      *                         missing the field should be declared to have the
  149.      *                         longest length allowed by the DBMS.
  150.      *
  151.      *                        notnull
  152.      *                         Boolean flag that indicates whether this field
  153.      *                         is constrained to not be set to null.
  154.      * @return string  DBMS specific SQL code portion that should be used to
  155.      *                  declare the specified field.
  156.      * @access public
  157.      */
  158.     function getCLOBDeclaration($name$field)
  159.     {
  160.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  161.         if (isset($field['length'])) {
  162.             $length $field['length'];
  163.             if ($length <= 255{
  164.                 $type 'TINYTEXT';
  165.             else {
  166.                 if ($length <= 65535{
  167.                     $type 'TEXT';
  168.                 else {
  169.                     if ($length <= 16777215{
  170.                         $type 'MEDIUMTEXT';
  171.                     else {
  172.                         $type 'LONGTEXT';
  173.                     }
  174.                 }
  175.             }
  176.         else {
  177.             $type 'LONGTEXT';
  178.         }
  179.         $notnull = isset($field['notnull']' NOT NULL' '';
  180.         return $name.' '.$type.$notnull;
  181.     }
  182.  
  183.     // }}}
  184.     // {{{ getBLOBDeclaration()
  185.  
  186.     /**
  187.      * Obtain DBMS specific SQL code portion needed to declare an binary large
  188.      * object type field to be used in statements like CREATE TABLE.
  189.      *
  190.      * @param string  $name   name the field to be declared.
  191.      * @param string  $field  associative array with the name of the properties
  192.      *                         of the field being declared as array indexes.
  193.      *                         Currently, the types of supported field
  194.      *                         properties are as follows:
  195.      *
  196.      *                        length
  197.      *                         Integer value that determines the maximum length
  198.      *                         of the large object field. If this argument is
  199.      *                         missing the field should be declared to have the
  200.      *                         longest length allowed by the DBMS.
  201.      *
  202.      *                        notnull
  203.      *                         Boolean flag that indicates whether this field is
  204.      *                         constrained to not be set to null.
  205.      * @return string  DBMS specific SQL code portion that should be used to
  206.      *                  declare the specified field.
  207.      * @access public
  208.      */
  209.     function getBLOBDeclaration($name$field)
  210.     {
  211.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  212.         if (isset($field['length'])) {
  213.             $length $field['length'];
  214.             if ($length <= 255{
  215.                 $type 'TINYBLOB';
  216.             else {
  217.                 if ($length <= 65535{
  218.                     $type 'BLOB';
  219.                 else {
  220.                     if ($length <= 16777215{
  221.                         $type 'MEDIUMBLOB';
  222.                     else {
  223.                         $type 'LONGBLOB';
  224.                     }
  225.                 }
  226.             }
  227.         }
  228.         else {
  229.             $type 'LONGBLOB';
  230.         }
  231.         $notnull = isset($field['notnull']' NOT NULL' '';
  232.         return $name.' '.$type.$notnull;
  233.     }
  234.  
  235.     // }}}
  236.     // {{{ getDateDeclaration()
  237.  
  238.     /**
  239.      * Obtain DBMS specific SQL code portion needed to declare an date type
  240.      * field to be used in statements like CREATE TABLE.
  241.      *
  242.      * @param string  $name   name the field to be declared.
  243.      * @param string  $field  associative array with the name of the properties
  244.      *                         of the field being declared as array indexes.
  245.      *                         Currently, the types of supported field properties
  246.      *                         are as follows:
  247.      *
  248.      *                        default
  249.      *                         Date value to be used as default for this field.
  250.      *
  251.      *                        notnull
  252.      *                         Boolean flag that indicates whether this field is
  253.      *                         constrained to not be set to null.
  254.      * @return string  DBMS specific SQL code portion that should be used to
  255.      *                  declare the specified field.
  256.      * @access public
  257.      */
  258.     function getDateDeclaration($name$field)
  259.     {
  260.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  261.         $default = isset($field['default']' DEFAULT '.
  262.             $this->quoteDate($field['default']'';
  263.         $notnull = isset($field['notnull']' NOT NULL' '';
  264.         return $name.' DATE'.$default.$notnull;
  265.     }
  266.  
  267.     // }}}
  268.     // {{{ getTimestampDeclaration()
  269.  
  270.     /**
  271.      * Obtain DBMS specific SQL code portion needed to declare an timestamp
  272.      * type field to be used in statements like CREATE TABLE.
  273.      *
  274.      * @param string  $name   name the field to be declared.
  275.      * @param string  $field  associative array with the name of the properties
  276.      *                         of the field being declared as array indexes.
  277.      *                         Currently, the types of supported field
  278.      *                         properties are as follows:
  279.      *
  280.      *                        default
  281.      *                         Time stamp value to be used as default for this
  282.      *                         field.
  283.      *
  284.      *                        notnull
  285.      *                         Boolean flag that indicates whether this field is
  286.      *                         constrained to not be set to null.
  287.      * @return string  DBMS specific SQL code portion that should be used to
  288.      *                  declare the specified field.
  289.      * @access public
  290.      */
  291.     function getTimestampDeclaration($name$field)
  292.     {
  293.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  294.         $default = isset($field['default']' DEFAULT '.
  295.             $this->quoteTimestamp($field['default']'';
  296.         $notnull = isset($field['notnull']' NOT NULL' '';
  297.         return $name.' DATETIME'.$default.$notnull;
  298.     }
  299.  
  300.     // }}}
  301.     // {{{ getTimeDeclaration()
  302.  
  303.     /**
  304.      * Obtain DBMS specific SQL code portion needed to declare an time type
  305.      * field to be used in statements like CREATE TABLE.
  306.      *
  307.      * @param string  $name   name the field to be declared.
  308.      * @param string  $field  associative array with the name of the properties
  309.      *                         of the field being declared as array indexes.
  310.      *                         Currently, the types of supported field
  311.      *                         properties are as follows:
  312.      *
  313.      *                        default
  314.      *                         Time value to be used as default for this field.
  315.      *
  316.      *                        notnull
  317.      *                         Boolean flag that indicates whether this field is
  318.      *                         constrained to not be set to null.
  319.      * @return string  DBMS specific SQL code portion that should be used to
  320.      *                  declare the specified field.
  321.      * @access public
  322.      */
  323.     function getTimeDeclaration($name$field)
  324.     {
  325.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  326.         $default = isset($field['default']' DEFAULT '.
  327.             $this->quoteTime($field['default']'';
  328.         $notnull = isset($field['notnull']' NOT NULL' '';
  329.         return $name.' TIME'.$default.$notnull;
  330.     }
  331.  
  332.     // }}}
  333.     // {{{ getFloatDeclaration()
  334.  
  335.     /**
  336.      * Obtain DBMS specific SQL code portion needed to declare an float type
  337.      * field to be used in statements like CREATE TABLE.
  338.      *
  339.      * @param string  $name   name the field to be declared.
  340.      * @param string  $field  associative array with the name of the properties
  341.      *                         of the field being declared as array indexes.
  342.      *                         Currently, the types of supported field
  343.      *                         properties are as follows:
  344.      *
  345.      *                        default
  346.      *                         Integer value to be used as default for this
  347.      *                         field.
  348.      *
  349.      *                        notnull
  350.      *                         Boolean flag that indicates whether this field is
  351.      *                         constrained to not be set to null.
  352.      * @return string  DBMS specific SQL code portion that should be used to
  353.      *                  declare the specified field.
  354.      * @access public
  355.      */
  356.     function getFloatDeclaration($name$field)
  357.     {
  358.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  359.         $type 'DOUBLE'.($db->options['fixed_float''('.($db->options['fixed_float']+2).','.$db->options['fixed_float'].')' '');
  360.         $default = isset($field['default']' DEFAULT '.
  361.             $this->quoteFloat($field['default']'';
  362.         $notnull = isset($field['notnull']' NOT NULL' '';
  363.         return $name.' '.$type.$default.$notnull;
  364.     }
  365.  
  366.     // }}}
  367.     // {{{ getDecimalDeclaration()
  368.  
  369.     /**
  370.      * Obtain DBMS specific SQL code portion needed to declare an decimal type
  371.      * field to be used in statements like CREATE TABLE.
  372.      *
  373.      * @param string  $name   name the field to be declared.
  374.      * @param string  $field  associative array with the name of the properties
  375.      *                         of the field being declared as array indexes.
  376.      *                         Currently, the types of supported field
  377.      *                         properties are as follows:
  378.      *
  379.      *                        default
  380.      *                         Integer value to be used as default for this
  381.      *                         field.
  382.      *
  383.      *                        notnull
  384.      *                         Boolean flag that indicates whether this field is
  385.      *                         constrained to not be set to null.
  386.      * @return string  DBMS specific SQL code portion that should be used to
  387.      *                  declare the specified field.
  388.      * @access public
  389.      */
  390.     function getDecimalDeclaration($name$field)
  391.     {
  392.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  393.         $type 'BIGINT';
  394.         $default = isset($field['default']' DEFAULT '.
  395.             $this->quoteDecimal($field['default']'';
  396.         $notnull = isset($field['notnull']' NOT NULL' '';
  397.         return $name.' '.$type.$default.$notnull;
  398.     }
  399.  
  400.     // }}}
  401.     // {{{ quoteCLOB()
  402.  
  403.     /**
  404.      * Convert a text value into a DBMS specific format that is suitable to
  405.      * compose query statements.
  406.      *
  407.      * @param           $clob 
  408.      * @return string  text string that represents the given argument value in
  409.      *                  a DBMS specific format.
  410.      * @access public
  411.      */
  412.     function quoteCLOB($clob)
  413.     {
  414.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  415.         if ($clob === null{
  416.             return 'NULL';
  417.         }
  418.         $value "'";
  419.         $data = null;
  420.         while (!$this->endOfLOB($clob)) {
  421.             $result $this->readLOB($clob$data$db->options['lob_buffer_length']);
  422.             if (MDB2::isError($result)) {
  423.                 return $result;
  424.             }
  425.             $value .= $db->escape($data);
  426.         }
  427.         $value .= "'";
  428.         return $value;
  429.     }
  430.  
  431.     // }}}
  432.     // {{{ quoteBLOB()
  433.  
  434.     /**
  435.      * Convert a text value into a DBMS specific format that is suitable to
  436.      * compose query statements.
  437.      *
  438.      * @param           $blob 
  439.      * @return string  text string that represents the given argument value in
  440.      *                  a DBMS specific format.
  441.      * @access public
  442.      */
  443.     function quoteBLOB($blob)
  444.     {
  445.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  446.         if ($blob === null{
  447.             return 'NULL';
  448.         }
  449.         $value "'";
  450.         $data = null;
  451.         while (!$this->endOfLOB($blob)) {
  452.         $result $this->readLOB($blob$data$db->options['lob_buffer_length']);
  453.             if (MDB2::isError($result)) {
  454.                 return $result;
  455.             }
  456.             $value .= $db->escape($data);
  457.         }
  458.         $value .= "'";
  459.         return $value;
  460.     }
  461.  
  462.     // }}}
  463.     // {{{ quoteFloat()
  464.  
  465.     /**
  466.      * Convert a text value into a DBMS specific format that is suitable to
  467.      * compose query statements.
  468.      *
  469.      * @param string  $value text string value that is intended to be converted.
  470.      * @return string  text string that represents the given argument value in
  471.      *                  a DBMS specific format.
  472.      * @access public
  473.      */
  474.     function quoteFloat($value)
  475.     {
  476.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  477.         return ($value === null'NULL' : (float)$value;
  478.     }
  479.  
  480.     // }}}
  481.     // {{{ quoteDecimal()
  482.  
  483.     /**
  484.      * Convert a text value into a DBMS specific format that is suitable to
  485.      * compose query statements.
  486.      *
  487.      * @param string  $value text string value that is intended to be converted.
  488.      * @return string  text string that represents the given argument value in
  489.      *                  a DBMS specific format.
  490.      * @access public
  491.      */
  492.     function quoteDecimal($value)
  493.     {
  494.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  495.         return (($value === null'NULL' strval(round(doubleval($value)*pow(10.0$db->options['decimal_places']))));
  496.     }
  497. }
  498.  
  499. ?>

Documentation generated on Mon, 11 Mar 2019 10:15:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.