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@backendmedia.com>                         |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: sqlite.php,v 1.63 2005/04/27 23:05:47 lsmith Exp $
  47. //
  48.  
  49. /**
  50.  * MDB2 SQLite driver
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author  Lukas Smith <smith@backendmedia.com>
  55.  */
  56. {
  57.     // {{{ properties
  58.     var $escape_quotes = "'";
  59.     var $database_path = '';
  60.     var $database_extension = '';
  61.  
  62.     var $_lasterror '';
  63.  
  64.     // }}}
  65.     // {{{ constructor
  66.  
  67.     /**
  68.     * Constructor
  69.     */
  70.     function __construct()
  71.     {
  72.         parent::__construct();
  73.  
  74.         $this->phptype = 'sqlite';
  75.         $this->dbsyntax = 'sqlite';
  76.  
  77.         $this->supported['sequences'= true;
  78.         $this->supported['indexes'= true;
  79.         $this->supported['affected_rows'= true;
  80.         $this->supported['summary_functions'= true;
  81.         $this->supported['order_by_text'= true;
  82.         $this->supported['current_id'= true;
  83.         $this->supported['limit_queries'= true;
  84.         $this->supported['LOBs'= true;
  85.         $this->supported['replace'= true;
  86.         $this->supported['transactions'= true;
  87.         $this->supported['sub_selects'= true;
  88.         $this->supported['auto_increment'= true;
  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.         $this->database_path = $this->options['database_path'];
  250.         $this->database_extension = $this->options['database_extension'];
  251.  
  252.         return $this->database_path.$database_name.$this->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 ($database_file{
  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->opened_persistent || $force{
  339.                 @sqlite_close($this->connection);
  340.             }
  341.             $this->connection = 0;
  342.         }
  343.         return MDB2_OK;
  344.     }
  345.  
  346.     // }}}
  347.     // {{{ _doQuery()
  348.  
  349.     /**
  350.      * Execute a query
  351.      * @param string $query  query
  352.      * @param boolean $isManip  if the query is a manipulation query
  353.      * @param resource $connection 
  354.      * @param string $database_name 
  355.      * @return result or error object
  356.      * @access protected
  357.      */
  358.     function _doQuery($query$isManip = false$connection = null$database_name = null)
  359.     {
  360.         $this->last_query = $query;
  361.         $this->debug($query'query');
  362.         if ($this->options['disable_query']{
  363.             if ($isManip{
  364.                 return MDB2_OK;
  365.             }
  366.             return null;
  367.         }
  368.  
  369.         if (is_null($connection)) {
  370.             $error $this->connect();
  371.             if (PEAR::isError($error)) {
  372.                 return $error;
  373.             }
  374.             $connection $this->connection;
  375.         }
  376.  
  377.         $function $this->options['result_buffering']
  378.             ? 'sqlite_query' 'sqlite_unbuffered_query';
  379.         $php_errormsg '';
  380.         ini_set('track_errors'true);
  381.         $result @$function($query.';'$connection);
  382.         ini_restore('track_errors');
  383.         $this->_lasterror = isset($php_errormsg$php_errormsg '';
  384.  
  385.         if (!$result{
  386.             return $this->raiseError();
  387.         }
  388.  
  389.         if ($isManip{
  390.             return @sqlite_changes($connection);
  391.         }
  392.         return $result;
  393.     }
  394.  
  395.     // }}}
  396.     // {{{ _modifyQuery()
  397.  
  398.     /**
  399.      * Changes a query string for various DBMS specific reasons
  400.      *
  401.      * @param string $query  query to modify
  402.      * @return the new (modified) query
  403.      * @access protected
  404.      */
  405.     function _modifyQuery($query$isManip$limit$offset)
  406.     {
  407.         if ($this->options['portability'MDB2_PORTABILITY_DELETE_COUNT{
  408.             if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i'$query)) {
  409.                 $query preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
  410.                                       'DELETE FROM \1 WHERE 1=1'$query);
  411.             }
  412.         }
  413.         if ($limit > 0
  414.             && !preg_match('/LIMIT\s*\d(\s*(,|OFFSET)\s*\d+)?/i'$query)
  415.         {
  416.             $query rtrim($query);
  417.             if (substr($query-1== ';'{
  418.                 $query substr($query0-1);
  419.             }
  420.             if ($isManip{
  421.                 $query .= " LIMIT $limit";
  422.             else {
  423.                 $query .= " LIMIT $offset,$limit";
  424.             }
  425.         }
  426.         return $query;
  427.     }
  428.  
  429.  
  430.     // }}}
  431.     // {{{ replace()
  432.  
  433.     /**
  434.      * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
  435.      * query, except that if there is already a row in the table with the same
  436.      * key field values, the REPLACE query just updates its values instead of
  437.      * inserting a new row.
  438.      *
  439.      * The REPLACE type of query does not make part of the SQL standards. Since
  440.      * practically only SQLite implements it natively, this type of query is
  441.      * emulated through this method for other DBMS using standard types of
  442.      * queries inside a transaction to assure the atomicity of the operation.
  443.      *
  444.      * @access public
  445.      *
  446.      * @param string $table name of the table on which the REPLACE query will
  447.      *   be executed.
  448.      * @param array $fields associative array that describes the fields and the
  449.      *   values that will be inserted or updated in the specified table. The
  450.      *   indexes of the array are the names of all the fields of the table. The
  451.      *   values of the array are also associative arrays that describe the
  452.      *   values and other properties of the table fields.
  453.      *
  454.      *   Here follows a list of field properties that need to be specified:
  455.      *
  456.      *     value:
  457.      *           Value to be assigned to the specified field. This value may be
  458.      *           of specified in database independent type format as this
  459.      *           function can perform the necessary datatype conversions.
  460.      *
  461.      *     Default:
  462.      *           this property is required unless the Null property
  463.      *           is set to 1.
  464.      *
  465.      *     type
  466.      *           Name of the type of the field. Currently, all types Metabase
  467.      *           are supported except for clob and blob.
  468.      *
  469.      *     Default: no type conversion
  470.      *
  471.      *     null
  472.      *           Boolean property that indicates that the value for this field
  473.      *           should be set to null.
  474.      *
  475.      *           The default value for fields missing in INSERT queries may be
  476.      *           specified the definition of a table. Often, the default value
  477.      *           is already null, but since the REPLACE may be emulated using
  478.      *           an UPDATE query, make sure that all fields of the table are
  479.      *           listed in this function argument array.
  480.      *
  481.      *     Default: 0
  482.      *
  483.      *     key
  484.      *           Boolean property that indicates that this field should be
  485.      *           handled as a primary key or at least as part of the compound
  486.      *           unique index of the table that will determine the row that will
  487.      *           updated if it exists or inserted a new row otherwise.
  488.      *
  489.      *           This function will fail if no key field is specified or if the
  490.      *           value of a key field is set to null because fields that are
  491.      *           part of unique index they may not be null.
  492.      *
  493.      *     Default: 0
  494.      *
  495.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  496.      */
  497.     function replace($table$fields)
  498.     {
  499.         $count count($fields);
  500.         $query $values '';
  501.         $keys $colnum = 0;
  502.         for (reset($fields)$colnum $countnext($fields)$colnum++{
  503.             $name key($fields);
  504.             if ($colnum > 0{
  505.                 $query .= ',';
  506.                 $values .= ',';
  507.             }
  508.             $query .= $name;
  509.             if (isset($fields[$name]['null']&& $fields[$name]['null']{
  510.                 $value 'NULL';
  511.             else {
  512.                 $value $this->quote($fields[$name]['value']$fields[$name]['type']);
  513.             }
  514.             $values .= $value;
  515.             if (isset($fields[$name]['key']&& $fields[$name]['key']{
  516.                 if ($value === 'NULL'{
  517.                     return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  518.                         'replace: key value '.$name.' may not be NULL');
  519.                 }
  520.                 $keys++;
  521.             }
  522.         }
  523.         if ($keys == 0{
  524.             return $this->raiseError(MDB2_ERROR_CANNOT_REPLACEnullnull,
  525.                 'replace: not specified which fields are keys');
  526.         }
  527.         $query = "REPLACE INTO $table ($query) VALUES ($values)";
  528.         $this->last_query = $query;
  529.         $this->debug($query'query');
  530.         return $this->_doQuery($querytrue);
  531.     }
  532.  
  533.     // }}}
  534.     // {{{ nextID()
  535.  
  536.     /**
  537.      * returns the next free id of a sequence
  538.      *
  539.      * @param string  $seq_name name of the sequence
  540.      * @param boolean $ondemand when true the seqence is
  541.      *                           automatic created, if it
  542.      *                           not exists
  543.      *
  544.      * @return mixed MDB2 Error Object or id
  545.      * @access public
  546.      */
  547.     function nextID($seq_name$ondemand = true)
  548.     {
  549.         $sequence_name $this->getSequenceName($seq_name);
  550.         $query = "INSERT INTO $sequence_name (".$this->options['seqcol_name'].") VALUES (NULL)";
  551.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  552.         $result $this->_doQuery($querytrue);
  553.         $this->popExpect();
  554.         if (PEAR::isError($result)) {
  555.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  556.                 $this->loadModule('Manager');
  557.                 // Since we are creating the sequence on demand
  558.                 // we know the first id = 1 so initialize the
  559.                 // sequence at 2
  560.                 $result $this->manager->createSequence($seq_name2);
  561.                 if (PEAR::isError($result)) {
  562.                     return $this->raiseError(MDB2_ERRORnullnull,
  563.                         'nextID: on demand sequence '.$seq_name.' could not be created');
  564.                 else {
  565.                     // First ID of a newly created sequence is 1
  566.                     return 1;
  567.                 }
  568.             }
  569.             return $result;
  570.         }
  571.         $value @sqlite_last_insert_rowid($this->connection);
  572.         if (is_numeric($value)
  573.             && PEAR::isError($this->_doQuery("DELETE FROM $sequence_name WHERE ".$this->options['seqcol_name']." < $value"true))
  574.         {
  575.             $this->warnings['nextID: could not delete previous sequence table values from '.$seq_name;
  576.         }
  577.         return $value;
  578.     }
  579.  
  580.     // }}}
  581.     // {{{ getAfterID()
  582.  
  583.     /**
  584.      * returns the autoincrement ID if supported or $id
  585.      *
  586.      * @param mixed $id value as returned by getBeforeId()
  587.      * @param string $table name of the table into which a new row was inserted
  588.      * @return mixed MDB2 Error Object or id
  589.      * @access public
  590.      */
  591.     function getAfterID($id$table)
  592.     {
  593.         $this->loadModule('Native');
  594.         return $this->native->getInsertID();
  595.     }
  596.  
  597.     // }}}
  598.     // {{{ currID()
  599.  
  600.     /**
  601.      * returns the current id of a sequence
  602.      *
  603.      * @param string  $seq_name name of the sequence
  604.      * @return mixed MDB2 Error Object or id
  605.      * @access public
  606.      */
  607.     function currID($seq_name)
  608.     {
  609.         $sequence_name $this->getSequenceName($seq_name);
  610.         return $this->queryOne("SELECT MAX(".$this->options['seqcol_name'].") FROM $sequence_name"'integer');
  611.     }
  612. }
  613.  
  614. class MDB2_Result_sqlite extends MDB2_Result_Common
  615. {
  616.     // }}}
  617.     // {{{ fetchRow()
  618.  
  619.     /**
  620.      * Fetch a row and insert the data into an existing array.
  621.      *
  622.      * @param int       $fetchmode  how the array data should be indexed
  623.      * @param int    $rownum    number of the row where the data can be found
  624.      * @return int data array on success, a MDB2 error on failure
  625.      * @access public
  626.      */
  627.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  628.     {
  629.         if (!is_null($rownum)) {
  630.             $seek $this->seek($rownum);
  631.             if (PEAR::isError($seek)) {
  632.                 return $seek;
  633.             }
  634.         }
  635.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  636.             $fetchmode $this->db->fetchmode;
  637.         }
  638.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  639.             $row @sqlite_fetch_array($this->resultSQLITE_ASSOC);
  640.             if (is_array($row)
  641.                 && $this->db->options['portability'MDB2_PORTABILITY_LOWERCASE
  642.             {
  643.                 $row array_change_key_case($rowCASE_LOWER);
  644.             }
  645.         else {
  646.            $row @sqlite_fetch_array($this->resultSQLITE_NUM);
  647.         }
  648.         if (!$row{
  649.             if (is_null($this->result)) {
  650.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  651.                     'fetchRow: resultset has already been freed');
  652.             }
  653.             return null;
  654.         }
  655.         if ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL{
  656.             $this->db->_convertEmptyArrayValuesToNull($row);
  657.         }
  658.         if (isset($this->values)) {
  659.             $this->_assignBindColumns($row);
  660.         }
  661.         if (isset($this->types)) {
  662.             $row $this->db->datatype->convertResultRow($this->types$row);
  663.         }
  664.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  665.             $object_class $this->db->options['fetch_class'];
  666.             if ($object_class == 'stdClass'{
  667.                 $row = (object) $row;
  668.             else {
  669.                 $row &new $object_class($row);
  670.             }
  671.         }
  672.         ++$this->rownum;
  673.         return $row;
  674.     }
  675.  
  676.     // }}}
  677.     // {{{ _getColumnNames()
  678.  
  679.     /**
  680.      * Retrieve the names of columns returned by the DBMS in a query result.
  681.      *
  682.      * @return mixed                an associative array variable
  683.      *                               that will hold the names of columns. The
  684.      *                               indexes of the array are the column names
  685.      *                               mapped to lower case and the values are the
  686.      *                               respective numbers of the columns starting
  687.      *                               from 0. Some DBMS may not return any
  688.      *                               columns when the result set does not
  689.      *                               contain any rows.
  690.      *
  691.      *                               a MDB2 error on failure
  692.      * @access private
  693.      */
  694.     function _getColumnNames()
  695.     {
  696.         $columns = array();
  697.         $numcols $this->numCols();
  698.         if (PEAR::isError($numcols)) {
  699.             return $numcols;
  700.         }
  701.         for ($column = 0; $column $numcols$column++{
  702.             $column_name @sqlite_field_name($this->result$column);
  703.             $columns[$column_name$column;
  704.         }
  705.         if ($this->db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  706.             $columns array_change_key_case($columnsCASE_LOWER);
  707.         }
  708.         return $columns;
  709.     }
  710.  
  711.     // }}}
  712.     // {{{ numCols()
  713.  
  714.     /**
  715.      * Count the number of columns returned by the DBMS in a query result.
  716.      *
  717.      * @access public
  718.      * @return mixed integer value with the number of columns, a MDB2 error
  719.      *                        on failure
  720.      */
  721.     function numCols()
  722.     {
  723.         $cols @sqlite_num_fields($this->result);
  724.         if (is_null($cols)) {
  725.             if (is_null($this->result)) {
  726.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  727.                     'numCols: resultset has already been freed');
  728.             }
  729.             return $this->db->raiseError();
  730.         }
  731.         return $cols;
  732.     }
  733. }
  734.  
  735. class MDB2_BufferedResult_sqlite extends MDB2_Result_sqlite
  736. {
  737.     // {{{ seek()
  738.  
  739.     /**
  740.     * seek to a specific row in a result set
  741.     *
  742.     * @param int    $rownum    number of the row where the data can be found
  743.     * @return mixed MDB2_OK on success, a MDB2 error on failure
  744.     * @access public
  745.     */
  746.     function seek($rownum = 0)
  747.     {
  748.         if (!@sqlite_seek($this->result$rownum)) {
  749.             if (is_null($this->result)) {
  750.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  751.                     'seek: resultset has already been freed');
  752.             }
  753.             return $this->db->raiseError(MDB2_ERROR_INVALIDnullnull,
  754.                 'seek: tried to seek to an invalid row number ('.$rownum.')');
  755.         }
  756.         $this->rownum $rownum - 1;
  757.         return MDB2_OK;
  758.     }
  759.  
  760.     // }}}
  761.     // {{{ valid()
  762.  
  763.     /**
  764.     * check if the end of the result set has been reached
  765.     *
  766.     * @return mixed true or false on sucess, a MDB2 error on failure
  767.     * @access public
  768.     */
  769.     function valid()
  770.     {
  771.         $numrows $this->numRows();
  772.         if (PEAR::isError($numrows)) {
  773.             return $numrows;
  774.         }
  775.         return $this->rownum ($numrows - 1);
  776.     }
  777.  
  778.     // }}}
  779.     // {{{ numRows()
  780.  
  781.     /**
  782.     * returns the number of rows in a result object
  783.     *
  784.     * @return mixed MDB2 Error Object or the number of rows
  785.     * @access public
  786.     */
  787.     function numRows()
  788.     {
  789.         $rows @sqlite_num_rows($this->result);
  790.         if (is_null($rows)) {
  791.             if (is_null($this->result)) {
  792.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  793.                     'numRows: resultset has already been freed');
  794.             }
  795.             return $this->raiseError();
  796.         }
  797.         return $rows;
  798.     }
  799. }
  800.  
  801. class MDB2_Statement_sqlite extends MDB2_Statement_Common
  802. {
  803.  
  804. }
  805.  
  806. ?>

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