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

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