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 versions 4 and 5                                                 |
  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@pooteeweet.org>                           |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: sqlite.php,v 1.95 2006/01/22 18:30:16 lsmith Exp $
  47. //
  48.  
  49. /**
  50.  * MDB2 SQLite driver
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author  Lukas Smith <smith@pooteeweet.org>
  55.  */
  56. class MDB2_Driver_sqlite extends MDB2_Driver_Common
  57. {
  58.     // {{{ properties
  59.     var $escape_quotes = "'";
  60.  
  61.     var $_lasterror '';
  62.  
  63.     // }}}
  64.     // {{{ constructor
  65.  
  66.     /**
  67.      * Constructor
  68.      */
  69.     function __construct()
  70.     {
  71.         parent::__construct();
  72.  
  73.         $this->phptype 'sqlite';
  74.         $this->dbsyntax 'sqlite';
  75.  
  76.         $this->supported['sequences''emulated';
  77.         $this->supported['indexes'= true;
  78.         $this->supported['affected_rows'= true;
  79.         $this->supported['summary_functions'= true;
  80.         $this->supported['order_by_text'= true;
  81.         $this->supported['current_id''emulated';
  82.         $this->supported['limit_queries'= true;
  83.         $this->supported['LOBs'= true;
  84.         $this->supported['replace'= true;
  85.         $this->supported['transactions'= true;
  86.         $this->supported['sub_selects'= true;
  87.         $this->supported['auto_increment'= true;
  88.         $this->supported['primary_key'=  false; // not implemented
  89.  
  90.         $this->options['base_transaction_name''___php_MDB2_sqlite_auto_commit_off';
  91.         $this->options['fixed_float'= 0;
  92.         $this->options['database_path''';
  93.         $this->options['database_extension''';
  94.     }
  95.  
  96.     // }}}
  97.     // {{{ errorInfo()
  98.  
  99.     /**
  100.      * This method is used to collect information about an error
  101.      *
  102.      * @param integer $error 
  103.      * @return array 
  104.      * @access public
  105.      */
  106.     function errorInfo($error = null)
  107.     {
  108.         $native_code = null;
  109.         if ($this->connection{
  110.             $native_code @sqlite_last_error($this->connection);
  111.         }
  112.         $native_msg $this->_lasterror
  113.             ? html_entity_decode($this->_lasterror@sqlite_error_string($native_code);
  114.  
  115.         if (is_null($error)) {
  116.             static $error_regexps;
  117.             if (empty($error_regexps)) {
  118.                 $error_regexps = array(
  119.                     '/^no such table:/' => MDB2_ERROR_NOSUCHTABLE,
  120.                     '/^no such index:/' => MDB2_ERROR_NOT_FOUND,
  121.                     '/^(table|index) .* already exists$/' => MDB2_ERROR_ALREADY_EXISTS,
  122.                     '/PRIMARY KEY must be unique/i' => MDB2_ERROR_CONSTRAINT,
  123.                     '/is not unique/' => MDB2_ERROR_CONSTRAINT,
  124.                     '/columns .* are not unique/i' => MDB2_ERROR_CONSTRAINT,
  125.                     '/uniqueness constraint failed/' => MDB2_ERROR_CONSTRAINT,
  126.                     '/may not be NULL/' => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  127.                     '/^no such column:/' => MDB2_ERROR_NOSUCHFIELD,
  128.                     '/column not present in both tables/i' => MDB2_ERROR_NOSUCHFIELD,
  129.                     '/^near ".*": syntax error$/' => MDB2_ERROR_SYNTAX,
  130.                     '/[0-9]+ values for [0-9]+ columns/i' => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  131.                  );
  132.             }
  133.             foreach ($error_regexps as $regexp => $code{
  134.                 if (preg_match($regexp$native_msg)) {
  135.                     $error $code;
  136.                     break;
  137.                 }
  138.             }
  139.         }
  140.         return array($error$native_code$native_msg);
  141.     }
  142.  
  143.     // }}}
  144.     // {{{ escape()
  145.  
  146.     /**
  147.      * Quotes a string so it can be safely used in a query. It will quote
  148.      * the text so it can safely be used within a query.
  149.      *
  150.      * @param string $text the input string to quote
  151.      * @return string quoted string
  152.      * @access public
  153.      */
  154.     function escape($text)
  155.     {
  156.         return @sqlite_escape_string($text);
  157.     }
  158.  
  159.     // }}}
  160.     // {{{ beginTransaction()
  161.  
  162.     /**
  163.      * Start a transaction.
  164.      *
  165.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  166.      * @access public
  167.      */
  168.     function beginTransaction()
  169.     {
  170.         $this->debug('starting transaction''beginTransaction');
  171.         if ($this->in_transaction{
  172.             return MDB2_OK;  //nothing to do
  173.         }
  174.         if (!$this->destructor_registered && $this->opened_persistent{
  175.             $this->destructor_registered = true;
  176.             register_shutdown_function('MDB2_closeOpenTransactions');
  177.         }
  178.         $query 'BEGIN TRANSACTION '.$this->options['base_transaction_name'];
  179.         $result $this->_doQuery($querytrue);
  180.         if (PEAR::isError($result)) {
  181.             return $result;
  182.         }
  183.         $this->in_transaction = true;
  184.         return MDB2_OK;
  185.     }
  186.  
  187.     // }}}
  188.     // {{{ commit()
  189.  
  190.     /**
  191.      * Commit the database changes done during a transaction that is in
  192.      * progress.
  193.      *
  194.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  195.      * @access public
  196.      */
  197.     function commit()
  198.     {
  199.         $this->debug('commit transaction''commit');
  200.         if (!$this->in_transaction{
  201.             return $this->raiseError(MDB2_ERRORnullnull,
  202.                 'commit: transaction changes are being auto committed');
  203.         }
  204.         $query 'COMMIT TRANSACTION '.$this->options['base_transaction_name'];
  205.         $result $this->_doQuery($querytrue);
  206.         if (PEAR::isError($result)) {
  207.             return $result;
  208.         }
  209.         $this->in_transaction = false;
  210.         return MDB2_OK;
  211.     }
  212.  
  213.     // }}}
  214.     // {{{ rollback()
  215.  
  216.     /**
  217.      * Cancel any database changes done during a transaction that is in
  218.      * progress.
  219.      *
  220.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  221.      * @access public
  222.      */
  223.     function rollback()
  224.     {
  225.         $this->debug('rolling back transaction''rollback');
  226.         if (!$this->in_transaction{
  227.             return $this->raiseError(MDB2_ERRORnullnull,
  228.                 'rollback: transactions can not be rolled back when changes are auto committed');
  229.         }
  230.         $query 'ROLLBACK TRANSACTION '.$this->options['base_transaction_name'];
  231.         $result $this->_doQuery($querytrue);
  232.         if (PEAR::isError($result)) {
  233.             return $result;
  234.         }
  235.         $this->in_transaction = false;
  236.         return MDB2_OK;
  237.     }
  238.  
  239.     // }}}
  240.     // {{{ getDatabaseFile()
  241.  
  242.     /**
  243.      * Builds the string with path+dbname+extension
  244.      *
  245.      * @return string full database path+file
  246.      * @access protected
  247.      */
  248.     function _getDatabaseFile($database_name)
  249.     {
  250.         if ($database_name == ''{
  251.             return $database_name;
  252.         }
  253.         return $this->options['database_path'].$database_name.$this->options['database_extension'];
  254.     }
  255.  
  256.     // }}}
  257.     // {{{ connect()
  258.  
  259.     /**
  260.      * Connect to the database
  261.      *
  262.      * @return true on success, MDB2 Error Object on failure
  263.      ***/
  264.     function connect()
  265.     {
  266.         $database_file $this->_getDatabaseFile($this->database_name);
  267.         if (is_resource($this->connection)) {
  268.             if (count(array_diff($this->connected_dsn$this->dsn)) == 0
  269.                 && $this->connected_database_name == $database_file
  270.                 && $this->opened_persistent == $this->options['persistent']
  271.             {
  272.                 return MDB2_OK;
  273.             }
  274.             $this->disconnect(false);
  275.         }
  276.  
  277.         if (!PEAR::loadExtension($this->phptype)) {
  278.             return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  279.                 'connect: extension '.$this->phptype.' is not compiled into PHP');
  280.         }
  281.  
  282.         if (!empty($this->database_name)) {
  283.             if (!file_exists($database_file)) {
  284.                 if (!touch($database_file)) {
  285.                     return $this->raiseError(MDB2_ERROR_NOT_FOUND);
  286.                 }
  287.                 if (!isset($this->dsn['mode'])
  288.                     || !is_numeric($this->dsn['mode'])
  289.                 {
  290.                     $mode = 0644;
  291.                 else {
  292.                     $mode octdec($this->dsn['mode']);
  293.                 }
  294.                 if (!chmod($database_file$mode)) {
  295.                     return $this->raiseError(MDB2_ERROR_NOT_FOUND);
  296.                 }
  297.                 if (!file_exists($database_file)) {
  298.                     return $this->raiseError(MDB2_ERROR_NOT_FOUND);
  299.                 }
  300.             }
  301.             if (!is_file($database_file)) {
  302.                 return $this->raiseError(MDB2_ERROR_INVALID);
  303.             }
  304.             if (!is_readable($database_file)) {
  305.                 return $this->raiseError(MDB2_ERROR_ACCESS_VIOLATION);
  306.             }
  307.  
  308.             $connect_function ($this->options['persistent''sqlite_popen' 'sqlite_open');
  309.             $php_errormsg '';
  310.             @ini_set('track_errors'true);
  311.             $connection @$connect_function($database_file);
  312.             @ini_restore('track_errors');
  313.             $this->_lasterror $php_errormsg;
  314.             if (!$connection{
  315.                 return $this->raiseError(MDB2_ERROR_CONNECT_FAILED);
  316.             }
  317.             $this->connection $connection;
  318.             $this->connected_dsn $this->dsn;
  319.             $this->connected_database_name $database_file;
  320.             $this->opened_persistent $this->getoption('persistent');
  321.             $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  322.         }
  323.         return MDB2_OK;
  324.     }
  325.  
  326.     // }}}
  327.     // {{{ disconnect()
  328.  
  329.     /**
  330.      * Log out and disconnect from the database.
  331.      *
  332.      * @return mixed true on success, false if not connected and error
  333.      *                 object on error
  334.      * @access public
  335.      */
  336.     function disconnect($force = true)
  337.     {
  338.         if (is_resource($this->connection)) {
  339.             if ($this->in_transaction{
  340.                 $this->rollback();
  341.             }
  342.             if (!$this->opened_persistent || $force{
  343.                 @sqlite_close($this->connection);
  344.             }
  345.             $this->connection = 0;
  346.             $this->in_transaction = false;
  347.         }
  348.         return MDB2_OK;
  349.     }
  350.  
  351.     // }}}
  352.     // {{{ _doQuery()
  353.  
  354.     /**
  355.      * Execute a query
  356.      * @param string $query  query
  357.      * @param boolean $is_manip  if the query is a manipulation query
  358.      * @param resource $connection 
  359.      * @param string $database_name 
  360.      * @return result or error object
  361.      * @access protected
  362.      */
  363.     function _doQuery($query$is_manip = false$connection = null$database_name = null)
  364.     {
  365.         $this->last_query $query;
  366.         $this->debug($query'query');
  367.         if ($this->options['disable_query']{
  368.             if ($is__manip{
  369.                 return 0;
  370.             }
  371.             return null;
  372.         }
  373.  
  374.         if (is_null($connection)) {
  375.             $connection $this->getConnection();
  376.             if (PEAR::isError($connection)) {
  377.                 return $connection;
  378.             }
  379.         }
  380.  
  381.         $function $this->options['result_buffering']
  382.             ? 'sqlite_query' 'sqlite_unbuffered_query';
  383.         $php_errormsg '';
  384.         @ini_set('track_errors'true);
  385.         $result @$function($query.';'$connection);
  386.         @ini_restore('track_errors');
  387.         $this->_lasterror $php_errormsg;
  388.  
  389.         if (!$result{
  390.             return $this->raiseError();
  391.         }
  392.  
  393.         return $result;
  394.     }
  395.  
  396.     // }}}
  397.     // {{{ _affectedRows()
  398.  
  399.     /**
  400.      * returns the number of rows affected
  401.      *
  402.      * @param resource $result 
  403.      * @param resource $connection 
  404.      * @return mixed MDB2 Error Object or the number of rows affected
  405.      * @access private
  406.      */
  407.     function _affectedRows($connection$result = null)
  408.     {
  409.         if (is_null($connection)) {
  410.             $connection $this->getConnection();
  411.             if (PEAR::isError($connection)) {
  412.                 return $connection;
  413.             }
  414.         }
  415.         return @sqlite_changes($connection);
  416.     }
  417.  
  418.     // }}}
  419.     // {{{ _modifyQuery()
  420.  
  421.     /**
  422.      * Changes a query string for various DBMS specific reasons
  423.      *
  424.      * @param string $query  query to modify
  425.      * @return the new (modified) query
  426.      * @access protected
  427.      */
  428.     function _modifyQuery($query$is_manip$limit$offset)
  429.     {
  430.         if ($this->options['portability'MDB2_PORTABILITY_DELETE_COUNT{
  431.             if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i'$query)) {
  432.                 $query preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
  433.                                       'DELETE FROM \1 WHERE 1=1'$query);
  434.             }
  435.         }
  436.         if ($limit > 0
  437.             && !preg_match('/LIMIT\s*\d(\s*(,|OFFSET)\s*\d+)?/i'$query)
  438.         {
  439.             $query rtrim($query);
  440.             if (substr($query-1== ';'{
  441.                 $query substr($query0-1);
  442.             }
  443.             if ($is_manip{
  444.                 $query.= " LIMIT $limit";
  445.             else {
  446.                 $query.= " LIMIT $offset,$limit";
  447.             }
  448.         }
  449.         return $query;
  450.     }
  451.  
  452.     // }}}
  453.     // {{{ getServerVersion()
  454.  
  455.     /**
  456.      * return version information about the server
  457.      *
  458.      * @param string     $native  determines if the raw version string should be returned
  459.      * @return mixed array/string with version information or MDB2 error object
  460.      * @access public
  461.      */
  462.     function getServerVersion($native = false)
  463.     {
  464.         if (!function_exists('sqlite_libversion')) {
  465.             return parent::getServerVersion($native);
  466.         }
  467.         $server_info = sqlite_libversion();
  468.         if (!$native{
  469.             $tmp explode('.'$server_info3);
  470.             $server_info = array(
  471.                 'major' => isset($tmp[0]$tmp[0: null,
  472.                 'minor' => isset($tmp[1]$tmp[1: null,
  473.                 'patch' => isset($tmp[2]$tmp[2: null,
  474.                 'extra' => null,
  475.                 'native' => $server_info,
  476.             );
  477.         }
  478.         return $server_info;
  479.     }
  480.  
  481.     // }}}
  482.     // {{{ replace()
  483.  
  484.     /**
  485.      * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
  486.      * query, except that if there is already a row in the table with the same
  487.      * key field values, the REPLACE query just updates its values instead of
  488.      * inserting a new row.
  489.      *
  490.      * The REPLACE type of query does not make part of the SQL standards. Since
  491.      * practically only SQLite implements it natively, this type of query is
  492.      * emulated through this method for other DBMS using standard types of
  493.      * queries inside a transaction to assure the atomicity of the operation.
  494.      *
  495.      * @access public
  496.      *
  497.      * @param string $table name of the table on which the REPLACE query will
  498.      *   be executed.
  499.      * @param array $fields associative array that describes the fields and the
  500.      *   values that will be inserted or updated in the specified table. The
  501.      *   indexes of the array are the names of all the fields of the table. The
  502.      *   values of the array are also associative arrays that describe the
  503.      *   values and other properties of the table fields.
  504.      *
  505.      *   Here follows a list of field properties that need to be specified:
  506.      *
  507.      *     value:
  508.      *           Value to be assigned to the specified field. This value may be
  509.      *           of specified in database independent type format as this
  510.      *           function can perform the necessary datatype conversions.
  511.      *
  512.      *     Default:
  513.      *           this property is required unless the Null property
  514.      *           is set to 1.
  515.      *
  516.      *     type
  517.      *           Name of the type of the field. Currently, all types Metabase
  518.      *           are supported except for clob and blob.
  519.      *
  520.      *     Default: no type conversion
  521.      *
  522.      *     null
  523.      *           Boolean property that indicates that the value for this field
  524.      *           should be set to null.
  525.      *
  526.      *           The default value for fields missing in INSERT queries may be
  527.      *           specified the definition of a table. Often, the default value
  528.      *           is already null, but since the REPLACE may be emulated using
  529.      *           an UPDATE query, make sure that all fields of the table are
  530.      *           listed in this function argument array.
  531.      *
  532.      *     Default: 0
  533.      *
  534.      *     key
  535.      *           Boolean property that indicates that this field should be
  536.      *           handled as a primary key or at least as part of the compound
  537.      *           unique index of the table that will determine the row that will
  538.      *           updated if it exists or inserted a new row otherwise.
  539.      *
  540.      *           This function will fail if no key field is specified or if the
  541.      *           value of a key field is set to null because fields that are
  542.      *           part of unique index they may not be null.
  543.      *
  544.      *     Default: 0
  545.      *
  546.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  547.      */
  548.     function replace($table$fields)
  549.     {
  550.         $count count($fields);
  551.         $query $values '';
  552.         $keys $colnum = 0;
  553.         for (reset($fields)$colnum $countnext($fields)$colnum++{
  554.             $name key($fields);
  555.             if ($colnum > 0{
  556.                 $query .= ',';
  557.                 $values.= ',';
  558.             }
  559.             $query.= $name;
  560.             if (isset($fields[$name]['null']&& $fields[$name]['null']{
  561.                 $value 'NULL';
  562.             else {
  563.                 $value $this->quote($fields[$name]['value']$fields[$name]['type']);
  564.             }
  565.             $values.= $value;
  566.             if (isset($fields[$name]['key']&& $fields[$name]['key']{
  567.                 if ($value === 'NULL'{
  568.                     return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  569.                         'replace: key value '.$name.' may not be NULL');
  570.                 }
  571.                 $keys++;
  572.             }
  573.         }
  574.         if ($keys == 0{
  575.             return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  576.                 'replace: not specified which fields are keys');
  577.         }
  578.  
  579.         $connection $this->getConnection();
  580.         if (PEAR::isError($connection)) {
  581.             return $connection;
  582.         }
  583.  
  584.         $query = "REPLACE INTO $table ($query) VALUES ($values)";
  585.         $this->last_query $query;
  586.         $this->debug($query'query');
  587.         $result $this->_doQuery($querytrue$connection);
  588.         if (PEAR::isError($result)) {
  589.             return $result;
  590.         }
  591.         return $this->_affectedRows($connection$result);
  592.     }
  593.  
  594.     // }}}
  595.     // {{{ nextID()
  596.  
  597.     /**
  598.      * returns the next free id of a sequence
  599.      *
  600.      * @param string $seq_name name of the sequence
  601.      * @param boolean $ondemand when true the seqence is
  602.      *                           automatic created, if it
  603.      *                           not exists
  604.      *
  605.      * @return mixed MDB2 Error Object or id
  606.      * @access public
  607.      */
  608.     function nextID($seq_name$ondemand = true)
  609.     {
  610.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  611.         $seqcol_name $this->options['seqcol_name'];
  612.         $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)";
  613.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  614.         $result $this->_doQuery($querytrue);
  615.         $this->popExpect();
  616.         if (PEAR::isError($result)) {
  617.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  618.                 $this->loadModule('Manager'nulltrue);
  619.                 // Since we are creating the sequence on demand
  620.                 // we know the first id = 1 so initialize the
  621.                 // sequence at 2
  622.                 $result $this->manager->createSequence($seq_name2);
  623.                 if (PEAR::isError($result)) {
  624.                     return $this->raiseError(MDB2_ERRORnullnull,
  625.                         'nextID: on demand sequence '.$seq_name.' could not be created');
  626.                 else {
  627.                     // First ID of a newly created sequence is 1
  628.                     return 1;
  629.                 }
  630.             }
  631.             return $result;
  632.         }
  633.         $value $this->lastInsertID();
  634.         if (is_numeric($value)) {
  635.             $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
  636.             $result $this->_doQuery($querytrue);
  637.             if (PEAR::isError($result)) {
  638.                 $this->warnings['nextID: could not delete previous sequence table values from '.$seq_name;
  639.             }
  640.         }
  641.         return $value;
  642.     }
  643.  
  644.     // }}}
  645.     // {{{ lastInsertID()
  646.  
  647.     /**
  648.      * returns the autoincrement ID if supported or $id
  649.      *
  650.      * @param mixed $id value as returned by getBeforeId()
  651.      * @param string $table name of the table into which a new row was inserted
  652.      * @return mixed MDB2 Error Object or id
  653.      * @access public
  654.      */
  655.     function lastInsertID($table = null$field = null)
  656.     {
  657.         $connection $this->getConnection();
  658.         if (PEAR::isError($connection)) {
  659.             return $connection;
  660.         }
  661.         $value @sqlite_last_insert_rowid($connection);
  662.         if (!$value{
  663.             return $this->raiseError();
  664.         }
  665.         return $value;
  666.     }
  667.  
  668.     // }}}
  669.     // {{{ currID()
  670.  
  671.     /**
  672.      * returns the current id of a sequence
  673.      *
  674.      * @param string $seq_name name of the sequence
  675.      * @return mixed MDB2 Error Object or id
  676.      * @access public
  677.      */
  678.     function currID($seq_name)
  679.     {
  680.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  681.         $seqcol_name $this->quoteIdentifier($this->options['seqcol_name']true);
  682.         $query = "SELECT MAX($seqcol_name) FROM $sequence_name";
  683.         return $this->queryOne($query'integer');
  684.     }
  685. }
  686.  
  687. class MDB2_Result_sqlite extends MDB2_Result_Common
  688. {
  689.     // }}}
  690.     // {{{ fetchRow()
  691.  
  692.     /**
  693.      * Fetch a row and insert the data into an existing array.
  694.      *
  695.      * @param int       $fetchmode  how the array data should be indexed
  696.      * @param int    $rownum    number of the row where the data can be found
  697.      * @return int data array on success, a MDB2 error on failure
  698.      * @access public
  699.      */
  700.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  701.     {
  702.         if (!is_null($rownum)) {
  703.             $seek $this->seek($rownum);
  704.             if (PEAR::isError($seek)) {
  705.                 return $seek;
  706.             }
  707.         }
  708.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  709.             $fetchmode $this->db->fetchmode;
  710.         }
  711.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  712.             $row @sqlite_fetch_array($this->resultSQLITE_ASSOC);
  713.             if (is_array($row)
  714.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  715.             {
  716.                 $row array_change_key_case($row$this->db->options['field_case']);
  717.             }
  718.         else {
  719.            $row @sqlite_fetch_array($this->resultSQLITE_NUM);
  720.         }
  721.         if (!$row{
  722.             if (is_null($this->result)) {
  723.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  724.                     'fetchRow: resultset has already been freed');
  725.                 return $err;
  726.             }
  727.             $null = null;
  728.             return $null;
  729.         }
  730.         if (($mode ($this->db->options['portability'MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES)
  731.             + ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL))
  732.         {
  733.             $this->db->_fixResultArrayValues($row$mode);
  734.         }
  735.         if (!empty($this->values)) {
  736.             $this->_assignBindColumns($row);
  737.         }
  738.         if (!empty($this->types)) {
  739.             $row $this->db->datatype->convertResultRow($this->types$row);
  740.         }
  741.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  742.             $object_class $this->db->options['fetch_class'];
  743.             if ($object_class == 'stdClass'{
  744.                 $row = (object) $row;
  745.             else {
  746.                 $row &new $object_class($row);
  747.             }
  748.         }
  749.         ++$this->rownum;
  750.         return $row;
  751.     }
  752.  
  753.     // }}}
  754.     // {{{ _getColumnNames()
  755.  
  756.     /**
  757.      * Retrieve the names of columns returned by the DBMS in a query result.
  758.      *
  759.      * @return mixed                an associative array variable
  760.      *                               that will hold the names of columns. The
  761.      *                               indexes of the array are the column names
  762.      *                               mapped to lower case and the values are the
  763.      *                               respective numbers of the columns starting
  764.      *                               from 0. Some DBMS may not return any
  765.      *                               columns when the result set does not
  766.      *                               contain any rows.
  767.      *
  768.      *                               a MDB2 error on failure
  769.      * @access private
  770.      */
  771.     function _getColumnNames()
  772.     {
  773.         $columns = array();
  774.         $numcols $this->numCols();
  775.         if (PEAR::isError($numcols)) {
  776.             return $numcols;
  777.         }
  778.         for ($column = 0; $column $numcols$column++{
  779.             $column_name @sqlite_field_name($this->result$column);
  780.             $columns[$column_name$column;
  781.         }
  782.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  783.             $columns array_change_key_case($columns$this->db->options['field_case']);
  784.         }
  785.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES{
  786.             $this->db->_fixResultArrayValues($columnsMDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES);
  787.         }
  788.         return $columns;
  789.     }
  790.  
  791.     // }}}
  792.     // {{{ numCols()
  793.  
  794.     /**
  795.      * Count the number of columns returned by the DBMS in a query result.
  796.      *
  797.      * @access public
  798.      * @return mixed integer value with the number of columns, a MDB2 error
  799.      *                        on failure
  800.      */
  801.     function numCols()
  802.     {
  803.         $cols @sqlite_num_fields($this->result);
  804.         if (is_null($cols)) {
  805.             if (is_null($this->result)) {
  806.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  807.                     'numCols: resultset has already been freed');
  808.             }
  809.             return $this->db->raiseError();
  810.         }
  811.         return $cols;
  812.     }
  813. }
  814.  
  815. {
  816.     // {{{ seek()
  817.  
  818.     /**
  819.      * seek to a specific row in a result set
  820.      *
  821.      * @param int    $rownum    number of the row where the data can be found
  822.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  823.      * @access public
  824.      */
  825.     function seek($rownum = 0)
  826.     {
  827.         if (!@sqlite_seek($this->result$rownum)) {
  828.             if (is_null($this->result)) {
  829.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  830.                     'seek: resultset has already been freed');
  831.             }
  832.             return $this->db->raiseError(MDB2_ERROR_INVALIDnullnull,
  833.                 'seek: tried to seek to an invalid row number ('.$rownum.')');
  834.         }
  835.         $this->rownum $rownum - 1;
  836.         return MDB2_OK;
  837.     }
  838.  
  839.     // }}}
  840.     // {{{ valid()
  841.  
  842.     /**
  843.      * check if the end of the result set has been reached
  844.      *
  845.      * @return mixed true or false on sucess, a MDB2 error on failure
  846.      * @access public
  847.      */
  848.     function valid()
  849.     {
  850.         $numrows $this->numRows();
  851.         if (PEAR::isError($numrows)) {
  852.             return $numrows;
  853.         }
  854.         return $this->rownum ($numrows - 1);
  855.     }
  856.  
  857.     // }}}
  858.     // {{{ numRows()
  859.  
  860.     /**
  861.      * returns the number of rows in a result object
  862.      *
  863.      * @return mixed MDB2 Error Object or the number of rows
  864.      * @access public
  865.      */
  866.     function numRows()
  867.     {
  868.         $rows @sqlite_num_rows($this->result);
  869.         if (is_null($rows)) {
  870.             if (is_null($this->result)) {
  871.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  872.                     'numRows: resultset has already been freed');
  873.             }
  874.             return $this->db->raiseError();
  875.         }
  876.         return $rows;
  877.     }
  878. }
  879.  
  880. class MDB2_Statement_sqlite extends MDB2_Statement_Common
  881. {
  882.  
  883. }
  884.  
  885. ?>

Documentation generated on Mon, 11 Mar 2019 14:36:12 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.