eZ components PHP Cross Reference Developer Tools

Source: /Database/src/handlers/mssql.php - 279 lines - 7659 bytes - Summary - Text - Print

Description: File containing the ezcDbHandlerMssql class.

   1  <?php
   2  /**
   3   * File containing the ezcDbHandlerMssql class.
   4   *
   5   * @package Database
   6   * @version 1.4.7
   7   * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
   8   * @license http://ez.no/licenses/new_bsd New BSD License
   9   */
  10  
  11  /**
  12   * MS SQL Server driver implementation.
  13   *
  14   * @see ezcDbHandler
  15   * @package Database
  16   * @version 1.4.7
  17   */
  18  class ezcDbHandlerMssql extends ezcDbHandler
  19  {
  20      /**
  21       * Contains the options that are used to set up handler.
  22       *
  23       * @var ezcDbMssqlOptions
  24       */
  25      public $options;
  26  
  27      /**
  28       * Constructs a handler object from the parameters $dbParams.
  29       *
  30       * Supported database parameters are:
  31       * - dbname|database: Database name
  32       * - host|hostspec:   Name of the host database is running on
  33       * - port:            TCP port
  34       * - user|username:   Database user name
  35       * - pass|password:   Database user password
  36       *
  37       * @param array $dbParams Database connection parameters (key=>value pairs).
  38       * @throws ezcDbMissingParameterException if the database name was not specified.
  39       */
  40      public function __construct( array $dbParams )
  41      {
  42          $database = null;
  43          $host     = null;
  44          $port     = null;
  45  
  46          foreach ( $dbParams as $key => $val )
  47          {
  48              switch ( $key )
  49              {
  50                  case 'database':
  51                  case 'dbname':
  52                      $database = $val;
  53                      break;
  54  
  55                  case 'host':
  56                  case 'hostspec':
  57                      $host = $val;
  58                      break;
  59  
  60                  case 'port':
  61                      $port = $val;
  62                      break;
  63              }
  64          }
  65  
  66          if ( !isset( $database ) )
  67          {
  68              throw new ezcDbMissingParameterException( 'database', 'dbParams' );
  69          }
  70  
  71          if ( ezcBaseFeatures::os() === 'Windows' )
  72          {
  73              $dsn = "mssql:dbname=$database";
  74          }
  75          else
  76          {
  77              // uses FreeTDS
  78              $dsn = "dblib:dbname=$database";
  79          }
  80  
  81          if ( isset( $host ) && $host )
  82          {
  83              $dsn .= ";host=$host";
  84              if ( isset( $port ) && $port )
  85              {
  86                  $dsn .= ":$port";
  87              }
  88          }
  89  
  90          parent::__construct( $dbParams, $dsn );
  91  
  92          // setup options
  93          $this->setOptions( new ezcDbMssqlOptions() );
  94      }
  95  
  96      /**
  97       * Associates an option object with this handler and changes settings for
  98       * opened connections.
  99       *
 100       * @param ezcDbMssqlOptions $options
 101       */
 102      public function setOptions( ezcDbMssqlOptions $options )
 103      {
 104          $this->options = $options;
 105          $this->setupConnection();
 106      }
 107  
 108      /**
 109       * Sets up opened connection according to options.
 110       */
 111      private function setupConnection()
 112      {
 113          $requiredMode = $this->options->quoteIdentifier;
 114          if ( $requiredMode == ezcDbMssqlOptions::QUOTES_GUESS )
 115          {
 116              $result = parent::query( "SELECT sessionproperty('QUOTED_IDENTIFIER')" );
 117              $rows = $result->fetchAll();
 118              $mode = (int)$rows[0][0];
 119              if ( $mode == 0 )
 120              {
 121                  $this->identifierQuoteChars = array( 'start' => '[', 'end' => ']' );
 122              }
 123              else
 124              {
 125                  $this->identifierQuoteChars = array( 'start' => '"', 'end' => '"' );
 126              }
 127          }
 128          else if ( $requiredMode == ezcDbMssqlOptions::QUOTES_COMPLIANT )
 129          {
 130              parent::exec( 'SET QUOTED_IDENTIFIER ON' );
 131              $this->identifierQuoteChars = array( 'start' => '"', 'end' => '"' );
 132          }
 133          else if ( $requiredMode == ezcDbMssqlOptions::QUOTES_LEGACY )
 134          {
 135              parent::exec( 'SET QUOTED_IDENTIFIER OFF' );
 136              $this->identifierQuoteChars = array( 'start' => '[', 'end' => ']' );
 137          }
 138      }
 139  
 140      /**
 141       * Returns a new ezcQueryExpression derived object with SQL Server
 142       * implementation specifics.
 143       *
 144       * @return ezcQueryExpressionMssql
 145       */
 146      public function createExpression()
 147      {
 148          return new ezcQueryExpressionMssql( $this );
 149      }
 150  
 151      /**
 152       * Returns 'mssql'.
 153       *
 154       * @return string
 155       */
 156      static public function getName()
 157      {
 158          return 'mssql';
 159      }
 160  
 161      /**
 162       * Returns a new ezcQuerySelectMssql derived object with SQL Server
 163       * implementation specifics.
 164       *
 165       * @return ezcQuerySelectMssql
 166       */
 167      public function createSelectQuery()
 168      {
 169          return new ezcQuerySelectMssql( $this );
 170      }
 171  
 172      /**
 173       * Begins a transaction.
 174       *
 175       * This method executes a begin transaction query unless a
 176       * transaction has already been started (transaction nesting level > 0 ).
 177       *
 178       * Each call to begin() must have a corresponding commit() or rollback() call.
 179       *
 180       * @see commit()
 181       * @see rollback()
 182       * @return bool
 183       */
 184      public function beginTransaction()
 185      {
 186          $retval = true;
 187          if ( $this->transactionNestingLevel == 0 )
 188          {
 189              $retval = $this->exec( "BEGIN TRANSACTION" );
 190          }
 191          // else NOP
 192  
 193          $this->transactionNestingLevel++;
 194          return $retval;
 195      }
 196  
 197      /**
 198       * Commits a transaction.
 199       *
 200       * If this this call to commit corresponds to the outermost call to
 201       * begin() and all queries within this transaction were successful,
 202       * a commit query is executed. If one of the queries
 203       * returned with an error, a rollback query is executed instead.
 204       *
 205       * This method returns true if the transaction was successful. If the
 206       * transaction failed and rollback was called, false is returned.
 207       *
 208       * @see begin()
 209       * @see rollback()
 210       * @return bool
 211       */
 212      public function commit()
 213      {
 214          if ( $this->transactionNestingLevel <= 0 )
 215          {
 216              $this->transactionNestingLevel = 0;
 217  
 218              throw new ezcDbTransactionException( "commit() called before beginTransaction()." );
 219          }
 220  
 221          $retval = true;
 222          if ( $this->transactionNestingLevel == 1 )
 223          {
 224              if ( $this->transactionErrorFlag )
 225              {
 226                  $this->exec( "ROLLBACK TRANSACTION" );
 227                  $this->transactionErrorFlag = false; // reset error flag
 228                  $retval = false;
 229              }
 230              else
 231              {
 232                  $this->exec( "COMMIT TRANSACTION" );
 233              }
 234          }
 235          // else NOP
 236  
 237          $this->transactionNestingLevel--;
 238          return $retval;
 239      }
 240  
 241      /**
 242       * Rollback a transaction.
 243       *
 244       * If this this call to rollback corresponds to the outermost call to
 245       * begin(), a rollback query is executed. If this is an inner transaction
 246       * (nesting level > 1) the error flag is set, leaving the rollback to the
 247       * outermost transaction.
 248       *
 249       * This method always returns true.
 250       *
 251       * @see begin()
 252       * @see commit()
 253       * @return bool
 254       */
 255      public function rollback()
 256      {
 257          if ( $this->transactionNestingLevel <= 0 )
 258          {
 259              $this->transactionNestingLevel = 0;
 260              throw new ezcDbTransactionException( "rollback() called without previous beginTransaction()." );
 261          }
 262  
 263          if ( $this->transactionNestingLevel == 1 )
 264          {
 265              $this->exec( "ROLLBACK TRANSACTION" );
 266              $this->transactionErrorFlag = false; // reset error flag
 267          }
 268          else
 269          {
 270              // set the error flag, so that if there is outermost commit
 271              // then ROLLBACK will be done instead of COMMIT
 272              $this->transactionErrorFlag = true;
 273          }
 274  
 275          $this->transactionNestingLevel--;
 276          return true;
 277      }
 278  }
 279  ?>

title

Description

title

Description

title

Description

title

title

Body