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

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