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

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