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

Source for file mssql.php

Documentation is available at mssql.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, Frank M. Kromann                       |
  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: Frank M. Kromann <frank@kromann.info>                        |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: mssql.php,v 1.100 2006/02/08 15:42:04 lsmith Exp $
  47. //
  48. // {{{ Class MDB2_Driver_mssql
  49. /**
  50.  * MDB2 MSSQL Server driver
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author  Frank M. Kromann <frank@kromann.info>
  55.  */
  56. class MDB2_Driver_mssql extends MDB2_Driver_Common
  57. {
  58.     // {{{ properties
  59.     var $escape_quotes = "'";
  60.  
  61.     // }}}
  62.     // {{{ constructor
  63.  
  64.     /**
  65.      * Constructor
  66.      */
  67.     function __construct()
  68.     {
  69.         parent::__construct();
  70.  
  71.         $this->phptype 'mssql';
  72.         $this->dbsyntax 'mssql';
  73.  
  74.         $this->supported['sequences''emulated';
  75.         $this->supported['indexes'= true;
  76.         $this->supported['affected_rows'= true;
  77.         $this->supported['transactions'= true;
  78.         $this->supported['summary_functions'= true;
  79.         $this->supported['order_by_text'= true;
  80.         $this->supported['current_id''emulated';
  81.         $this->supported['limit_queries''emulated';
  82.         $this->supported['LOBs'= true;
  83.         $this->supported['replace''emulated';
  84.         $this->supported['sub_selects'= true;
  85.         $this->supported['auto_increment'= true;
  86.         $this->supported['primary_key'= true;
  87.  
  88.         $this->options['database_device'= false;
  89.         $this->options['database_size'= false;
  90.     }
  91.  
  92.     // }}}
  93.     // {{{ errorInfo()
  94.  
  95.     /**
  96.      * This method is used to collect information about an error
  97.      *
  98.      * @param integer $error 
  99.      * @return array 
  100.      * @access public
  101.      */
  102.     function errorInfo($error = null)
  103.     {
  104.         $native_code = null;
  105.         if ($this->connection{
  106.             $result @mssql_query('select @@ERROR as ErrorCode'$this->connection);
  107.             if ($result{
  108.                 $native_code @mssql_result($result00);
  109.                 @mssql_free_result($result);
  110.             }
  111.         }
  112.         $native_msg @mssql_get_last_message();
  113.         if (is_null($error)) {
  114.             static $ecode_map;
  115.             if (empty($ecode_map)) {
  116.                 $ecode_map = array(
  117.                     110   => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  118.                     155   => MDB2_ERROR_NOSUCHFIELD,
  119.                     170   => MDB2_ERROR_SYNTAX,
  120.                     207   => MDB2_ERROR_NOSUCHFIELD,
  121.                     208   => MDB2_ERROR_NOSUCHTABLE,
  122.                     245   => MDB2_ERROR_INVALID_NUMBER,
  123.                     515   => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  124.                     547   => MDB2_ERROR_CONSTRAINT,
  125.                     1913  => MDB2_ERROR_ALREADY_EXISTS,
  126.                     2627  => MDB2_ERROR_CONSTRAINT,
  127.                     2714  => MDB2_ERROR_ALREADY_EXISTS,
  128.                     3701  => MDB2_ERROR_NOSUCHTABLE,
  129.                     8134  => MDB2_ERROR_DIVZERO,
  130.                 );
  131.             }
  132.             if (isset($ecode_map[$native_code])) {
  133.                 if ($native_code == 3701
  134.                     && preg_match('/Cannot drop the index/i'$native_msg)
  135.                 {
  136.                    $error = MDB2_ERROR_NOT_FOUND;
  137.                 else {
  138.                     $error $ecode_map[$native_code];
  139.                 }
  140.             }
  141.         }
  142.         return array($error$native_code$native_msg);
  143.     }
  144.  
  145.     // }}}
  146.     // {{{ quoteIdentifier()
  147.  
  148.     /**
  149.      * Quote a string so it can be safely used as a table / column name
  150.      *
  151.      * Quoting style depends on which database driver is being used.
  152.      *
  153.      * @param string $str  identifier name to be quoted
  154.      * @param bool   $check_option  check the 'quote_identifier' option
  155.      *
  156.      * @return string  quoted identifier string
  157.      *
  158.      * @access public
  159.      */
  160.     function quoteIdentifier($str$check_option = false)
  161.     {
  162.         if ($check_option && !$this->options['quote_identifier']{
  163.             return $str;
  164.         }
  165.         return '[' str_replace(']'']]'$str']';
  166.     }
  167.  
  168.     // }}}
  169.     // {{{ beginTransaction()
  170.  
  171.     /**
  172.      * Start a transaction.
  173.      *
  174.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  175.      * @access public
  176.      */
  177.     function beginTransaction()
  178.     {
  179.         $this->debug('starting transaction''beginTransaction');
  180.         if ($this->in_transaction{
  181.             return MDB2_OK;  //nothing to do
  182.         }
  183.         if (!$this->destructor_registered && $this->opened_persistent{
  184.             $this->destructor_registered = true;
  185.             register_shutdown_function('MDB2_closeOpenTransactions');
  186.         }
  187.         $result $this->_doQuery('BEGIN TRANSACTION'true);
  188.         if (PEAR::isError($result)) {
  189.             return $result;
  190.         }
  191.         $this->in_transaction = true;
  192.         return MDB2_OK;
  193.     }
  194.  
  195.     // }}}
  196.     // {{{ commit()
  197.  
  198.     /**
  199.      * Commit the database changes done during a transaction that is in
  200.      * progress.
  201.      *
  202.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  203.      * @access public
  204.      */
  205.     function commit()
  206.     {
  207.         $this->debug('commit transaction''commit');
  208.         if (!$this->in_transaction{
  209.             return $this->raiseError(MDB2_ERRORnullnull,
  210.                 'commit: transaction changes are being auto committed');
  211.         }
  212.         $result $this->_doQuery('COMMIT TRANSACTION'true);
  213.         if (PEAR::isError($result)) {
  214.             return $result;
  215.         }
  216.         $this->in_transaction = false;
  217.         return MDB2_OK;
  218.     }
  219.  
  220.     // }}}
  221.     // {{{ rollback()
  222.  
  223.     /**
  224.      * Cancel any database changes done during a transaction that is in
  225.      * progress.
  226.      *
  227.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  228.      * @access public
  229.      */
  230.     function rollback()
  231.     {
  232.         $this->debug('rolling back transaction''rollback');
  233.         if (!$this->in_transaction{
  234.             return $this->raiseError(MDB2_ERRORnullnull,
  235.                 'rollback: transactions can not be rolled back when changes are auto committed');
  236.         }
  237.         $result $this->_doQuery('ROLLBACK TRANSACTION'true);
  238.         if (PEAR::isError($result)) {
  239.             return $result;
  240.         }
  241.         $this->in_transaction = false;
  242.         return MDB2_OK;
  243.     }
  244.  
  245.     // }}}
  246.     // {{{ connect()
  247.  
  248.     /**
  249.      * Connect to the database
  250.      *
  251.      * @return true on success, MDB2 Error Object on failure
  252.      */
  253.     function connect()
  254.     {
  255.         if (is_resource($this->connection)) {
  256.             if (count(array_diff($this->connected_dsn$this->dsn)) == 0
  257.                 && $this->opened_persistent == $this->options['persistent']
  258.             {
  259.                 return MDB2_OK;
  260.             }
  261.             $this->disconnect(false);
  262.         }
  263.  
  264.         if (!PEAR::loadExtension($this->phptype)) {
  265.             return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  266.                 'connect: extension '.$this->phptype.' is not compiled into PHP');
  267.         }
  268.  
  269.         $params = array(
  270.             $this->dsn['hostspec'$this->dsn['hostspec''localhost',
  271.             $this->dsn['username'$this->dsn['username': null,
  272.             $this->dsn['password'$this->dsn['password': null,
  273.         );
  274.         if ($this->dsn['port']{
  275.             $params[0].= ((substr(PHP_OS03== 'WIN'',' ':')
  276.                         . $this->dsn['port'];
  277.         }
  278.  
  279.         $connect_function $this->options['persistent''mssql_pconnect' 'mssql_connect';
  280.  
  281.         $connection @call_user_func_array($connect_function$params);
  282.         if ($connection <= 0{
  283.             return $this->raiseError(MDB2_ERROR_CONNECT_FAILED);
  284.         }
  285.  
  286.         $this->connection $connection;
  287.         $this->connected_dsn $this->dsn;
  288.         $this->connected_database_name '';
  289.         $this->opened_persistent $this->options['persistent'];
  290.         $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  291.  
  292.        if ((bool) ini_get('mssql.datetimeconvert')) {
  293.            @ini_set('mssql.datetimeconvert''0');
  294.        }
  295.        @mssql_query('SET DATEFORMAT ymd'$connection);
  296.  
  297.         return MDB2_OK;
  298.     }
  299.  
  300.     // }}}
  301.     // {{{ disconnect()
  302.  
  303.     /**
  304.      * Log out and disconnect from the database.
  305.      *
  306.      * @return mixed true on success, false if not connected and error
  307.      *                 object on error
  308.      * @access public
  309.      */
  310.     function disconnect($force = true)
  311.     {
  312.         if (is_resource($this->connection)) {
  313.             if ($this->in_transaction{
  314.                 $this->rollback();
  315.             }
  316.             if (!$this->opened_persistent || $force{
  317.                 @mssql_close($this->connection);
  318.             }
  319.             $this->connection = 0;
  320.             $this->in_transaction = false;
  321.         }
  322.         return MDB2_OK;
  323.     }
  324.  
  325.     // }}}
  326.     // {{{ _doQuery()
  327.  
  328.     /**
  329.      * Execute a query
  330.      * @param string $query  query
  331.      * @param boolean $is_manip  if the query is a manipulation query
  332.      * @param resource $connection 
  333.      * @param string $database_name 
  334.      * @return result or error object
  335.      * @access protected
  336.      */
  337.     function _doQuery($query$is_manip = false$connection = null$database_name = null)
  338.     {
  339.         $this->last_query $query;
  340.         $this->debug($query'query'$is_manip);
  341.         if ($this->getOption('disable_query')) {
  342.             if ($is_manip{
  343.                 return 0;
  344.             }
  345.             return null;
  346.         }
  347.  
  348.         if (is_null($connection)) {
  349.             $connection $this->getConnection();
  350.             if (PEAR::isError($connection)) {
  351.                 return $connection;
  352.             }
  353.         }
  354.         if (is_null($database_name)) {
  355.             $database_name $this->database_name;
  356.         }
  357.  
  358.         if ($database_name{
  359.             if ($database_name != $this->connected_database_name{
  360.                 if (!@mssql_select_db($database_name$connection)) {
  361.                     return $this->raiseError();
  362.                 }
  363.                 $this->connected_database_name $database_name;
  364.             }
  365.         }
  366.  
  367.         $result @mssql_query($query$connection);
  368.         if (!$result{
  369.             return $this->raiseError();
  370.         }
  371.  
  372.         return $result;
  373.     }
  374.  
  375.     // }}}
  376.     // {{{ _affectedRows()
  377.  
  378.     /**
  379.      * returns the number of rows affected
  380.      *
  381.      * @param resource $result 
  382.      * @param resource $connection 
  383.      * @return mixed MDB2 Error Object or the number of rows affected
  384.      * @access private
  385.      */
  386.     function _affectedRows($connection$result = null)
  387.     {
  388.         if (is_null($connection)) {
  389.             $connection $this->getConnection();
  390.             if (PEAR::isError($connection)) {
  391.                 return $connection;
  392.             }
  393.         }
  394.         return @mssql_rows_affected($connection);
  395.     }
  396.  
  397.     // }}}
  398.     // {{{ _modifyQuery()
  399.  
  400.     /**
  401.      * Changes a query string for various DBMS specific reasons
  402.      *
  403.      * @param string $query  query to modify
  404.      * @return the new (modified) query
  405.      * @access protected
  406.      */
  407.     function _modifyQuery($query$is_manip$limit$offset)
  408.     {
  409.         if ($limit > 0{
  410.             $fetch $offset $limit;
  411.             if (!$is_manip{
  412.                 return preg_replace('/^([\s(])*SELECT(?!\s*TOP\s*\()/i',
  413.                     "\\1SELECT TOP $fetch"$query);
  414.             }
  415.         }
  416.         return $query;
  417.     }
  418.     // }}}
  419.     // {{{ _checkSequence
  420.     /**
  421.      * Checks if there's a sequence that exists.
  422.      *
  423.      * @param  string $seq_name    The sequence name to verify.
  424.      * @return bool   $tableExists The value if the table exists or not
  425.      * @access private
  426.      */
  427.     function _checkSequence($seq_name)
  428.     {
  429.         $query = "SELECT * FROM $seq_name";
  430.         $tableExists $this->_doQuery($querytrue);
  431.         if (PEAR::isError($tableExists)) {
  432.             if ($tableExists->getCode(== MDB2_ERROR_NOSUCHTABLE{
  433.                 return false;
  434.             }
  435.             //return $tableExists;
  436.             return false;
  437.         }
  438.         return true;
  439.     }
  440.  
  441.     // }}}
  442.     // {{{ nextID()
  443.  
  444.     /**
  445.      * returns the next free id of a sequence
  446.      *
  447.      * @param string $seq_name name of the sequence
  448.      * @param boolean $ondemand when true the seqence is
  449.      *                           automatic created, if it
  450.      *                           not exists
  451.      *
  452.      * @return mixed MDB2 Error Object or id
  453.      * @access public
  454.      */
  455.     function nextID($seq_name$ondemand = true)
  456.     {
  457.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  458.         $seqcol_name $this->quoteIdentifier($this->options['seqcol_name']true);
  459.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  460.         if ($this->_checkSequence($sequence_name)) {
  461.             $query = "SET IDENTITY_INSERT $sequence_name ON ".
  462.                      "INSERT INTO $sequence_name ($seqcol_name) VALUES (0)";
  463.         else {
  464.             $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (0)";
  465.         }
  466.         $result $this->_doQuery($querytrue);
  467.         $this->popExpect();
  468.         if (PEAR::isError($result)) {
  469.             if ($ondemand && !$this->_checkSequence($sequence_name)) {
  470.                 $this->loadModule('Manager'nulltrue);
  471.                 // Since we are creating the sequence on demand
  472.                 // we know the first id = 1 so initialize the
  473.                 // sequence at 2
  474.                 $result $this->manager->createSequence($seq_name2);
  475.                 if (PEAR::isError($result)) {
  476.                     return $this->raiseError(MDB2_ERRORnullnull,
  477.                         'nextID: on demand sequence '.$seq_name.' could not be created');
  478.                 else {
  479.                     // First ID of a newly created sequence is 1
  480.                     return 1;
  481.                 }
  482.             }
  483.             return $result;
  484.         }
  485.         $value $this->lastInsertID($sequence_name);
  486.         if (is_numeric($value)) {
  487.             $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
  488.             $result $this->_doQuery($querytrue);
  489.             if (PEAR::isError($result)) {
  490.                 $this->warnings['nextID: could not delete previous sequence table values from '.$seq_name;
  491.             }
  492.         }
  493.         return $value;
  494.     }
  495.     // }}}
  496.     // {{{ lastInsertID()
  497.     /**
  498.      * returns the autoincrement ID if supported or $id
  499.      *
  500.      * @param mixed $id value as returned by getBeforeId()
  501.      * @param string $table name of the table into which a new row was inserted
  502.      * @return mixed MDB2 Error Object or id
  503.      * @access public
  504.      */
  505.     function lastInsertID($table = null$field = null)
  506.     {
  507.         return $this->queryOne("SELECT @@IDENTITY FROM $table"'integer');
  508.     }
  509.     // }}}
  510. }
  511. // }}}
  512. // {{{ Class MDB2_Result_mssql
  513.  
  514. class MDB2_Result_mssql extends MDB2_Result_Common
  515. {
  516.     // {{{ _skipLimitOffset()
  517.  
  518.     /**
  519.      * Skip the first row of a result set.
  520.      *
  521.      * @param resource $result 
  522.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  523.      * @access protected
  524.      */
  525.     function _skipLimitOffset()
  526.     {
  527.         if ($this->limit{
  528.             if ($this->rownum >= $this->limit{
  529.                 return false;
  530.             }
  531.         }
  532.         if ($this->offset{
  533.             while ($this->offset_count $this->offset{
  534.                 ++$this->offset_count;
  535.                 if (!is_array(@mysql_fetch_row($this->result))) {
  536.                     $this->offset_count $this->limit;
  537.                     return false;
  538.                 }
  539.             }
  540.         }
  541.         return MDB2_OK;
  542.     }
  543.  
  544.     // }}}
  545.     // {{{ fetchRow()
  546.  
  547.     /**
  548.      * Fetch a row and insert the data into an existing array.
  549.      *
  550.      * @param int       $fetchmode  how the array data should be indexed
  551.      * @param int    $rownum    number of the row where the data can be found
  552.      * @return int data array on success, a MDB2 error on failure
  553.      * @access public
  554.      */
  555.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  556.     {
  557.         if (!$this->_skipLimitOffset()) {
  558.             $null = null;
  559.             return $null;
  560.         }
  561.         if (!is_null($rownum)) {
  562.             $seek $this->seek($rownum);
  563.             if (PEAR::isError($seek)) {
  564.                 return $seek;
  565.             }
  566.         }
  567.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  568.             $fetchmode $this->db->fetchmode;
  569.         }
  570.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  571.             $row @mssql_fetch_assoc($this->result);
  572.             if (is_array($row)
  573.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  574.             {
  575.                 $row array_change_key_case($row$this->db->options['field_case']);
  576.             }
  577.         else {
  578.             $row @mssql_fetch_row($this->result);
  579.         }
  580.         if (!$row{
  581.             if ($this->result === false{
  582.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  583.                     'fetchRow: resultset has already been freed');
  584.                 return $err;
  585.             }
  586.             $null = null;
  587.             return $null;
  588.         }
  589.         if ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL{
  590.             $this->db->_fixResultArrayValues($rowMDB2_PORTABILITY_EMPTY_TO_NULL);
  591.         }
  592.         if (!empty($this->values)) {
  593.             $this->_assignBindColumns($row);
  594.         }
  595.         if (!empty($this->types)) {
  596.             $row $this->db->datatype->convertResultRow($this->types$row);
  597.         }
  598.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  599.             $object_class $this->db->options['fetch_class'];
  600.             if ($object_class == 'stdClass'{
  601.                 $row = (object) $row;
  602.             else {
  603.                 $row &new $object_class($row);
  604.             }
  605.         }
  606.         ++$this->rownum;
  607.         return $row;
  608.     }
  609.  
  610.     // }}}
  611.     // {{{ _getColumnNames()
  612.  
  613.     /**
  614.      * Retrieve the names of columns returned by the DBMS in a query result.
  615.      *
  616.      * @param resource $result result identifier
  617.      * @return mixed                an associative array variable
  618.      *                               that will hold the names of columns. The
  619.      *                               indexes of the array are the column names
  620.      *                               mapped to lower case and the values are the
  621.      *                               respective numbers of the columns starting
  622.      *                               from 0. Some DBMS may not return any
  623.      *                               columns when the result set does not
  624.      *                               contain any rows.
  625.      *
  626.      *                               a MDB2 error on failure
  627.      * @access private
  628.      */
  629.     function _getColumnNames()
  630.     {
  631.         $columns = array();
  632.         $numcols $this->numCols();
  633.         if (PEAR::isError($numcols)) {
  634.             return $numcols;
  635.         }
  636.         for ($column = 0; $column $numcols$column++{
  637.             $column_name @mssql_field_name($this->result$column);
  638.             $columns[$column_name$column;
  639.         }
  640.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  641.             $columns array_change_key_case($columns$this->db->options['field_case']);
  642.         }
  643.         return $columns;
  644.     }
  645.  
  646.     // }}}
  647.     // {{{ numCols()
  648.  
  649.     /**
  650.      * Count the number of columns returned by the DBMS in a query result.
  651.      *
  652.      * @return mixed integer value with the number of columns, a MDB2 error
  653.      *       on failure
  654.      * @access public
  655.      */
  656.     function numCols()
  657.     {
  658.         $cols @mssql_num_fields($this->result);
  659.         if (is_null($cols)) {
  660.             if ($this->result === false{
  661.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  662.                     'numCols: resultset has already been freed');
  663.             elseif (is_null($this->result)) {
  664.                 return count($this->types);
  665.             }
  666.             return $this->db->raiseError();
  667.         }
  668.         return $cols;
  669.     }
  670.  
  671.     // }}}
  672.     // {{{ nextResult()
  673.  
  674.     /**
  675.      * Move the internal result pointer to the next available result
  676.      * Currently not supported
  677.      *
  678.      * @return true on success, false if there is no more result set or an error object on failure
  679.      * @access public
  680.      */
  681.     function nextResult()
  682.     {
  683.         if ($this->result === false{
  684.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  685.                 'nextResult: resultset has already been freed');
  686.         elseif (is_null($this->result)) {
  687.             return false;
  688.         }
  689.         return @mssql_next_result($this->result);
  690.     }
  691.  
  692.     // }}}
  693.     // {{{ free()
  694.  
  695.     /**
  696.      * Free the internal resources associated with $result.
  697.      *
  698.      * @return boolean true on success, false if $result is invalid
  699.      * @access public
  700.      */
  701.     function free()
  702.     {
  703.         $free @mssql_free_result($this->result);
  704.         if (!$free{
  705.             if (!$this->result{
  706.                 return MDB2_OK;
  707.             }
  708.             return $this->db->raiseError();
  709.         }
  710.         $this->result = false;
  711.         return MDB2_OK;
  712.     }
  713. }
  714.  
  715. {
  716.     // }}}
  717.     // {{{ seek()
  718.  
  719.     /**
  720.      * seek to a specific row in a result set
  721.      *
  722.      * @param int    $rownum    number of the row where the data can be found
  723.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  724.      * @access public
  725.      */
  726.     function seek($rownum = 0)
  727.     {
  728.         if ($this->rownum != ($rownum - 1&& !@mssql_data_seek($this->result$rownum)) {
  729.             if ($this->result === false{
  730.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  731.                     'seek: resultset has already been freed');
  732.             elseif (is_null($this->result)) {
  733.                 return MDB2_OK;
  734.             }
  735.             return $this->db->raiseError(MDB2_ERROR_INVALIDnullnull,
  736.                 'seek: tried to seek to an invalid row number ('.$rownum.')');
  737.         }
  738.         $this->rownum $rownum - 1;
  739.         return MDB2_OK;
  740.     }
  741.  
  742.     // {{{ valid()
  743.  
  744.     /**
  745.      * check if the end of the result set has been reached
  746.      *
  747.      * @return mixed true or false on sucess, a MDB2 error on failure
  748.      * @access public
  749.      */
  750.     function valid()
  751.     {
  752.         $numrows $this->numRows();
  753.         if (PEAR::isError($numrows)) {
  754.             return $numrows;
  755.         }
  756.         return $this->rownum ($numrows - 1);
  757.     }
  758.  
  759.     // }}}
  760.     // {{{ numRows()
  761.  
  762.     /**
  763.      * returns the number of rows in a result object
  764.      *
  765.      * @return mixed MDB2 Error Object or the number of rows
  766.      * @access public
  767.      */
  768.     function numRows()
  769.     {
  770.         $rows @mssql_num_rows($this->result);
  771.         if (is_null($rows)) {
  772.             if ($this->result === false{
  773.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  774.                     'numRows: resultset has already been freed');
  775.             elseif (is_null($this->result)) {
  776.                 return 0;
  777.             }
  778.             return $this->db->raiseError();
  779.         }
  780.         if ($this->limit{
  781.             $rows -= $this->limit;
  782.             if ($rows < 0{
  783.                 $rows = 0;
  784.             }
  785.         }
  786.         return $rows;
  787.     }
  788. }
  789. // }}}
  790. // {{{ MDB2_Statement_mssql
  791. class MDB2_Statement_mssql extends MDB2_Statement_Common
  792. {
  793.  
  794. }
  795. // }}}
  796.  
  797. ?>

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