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

Documentation generated on Mon, 11 Mar 2019 15:12:00 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.