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

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