Source for file MssqlConnector.php

Documentation is available at MssqlConnector.php

  1. <?php
  2.  
  3. ///////////////////////////////////////////////////////////////////////////////
  4. /**
  5.  * IConnector implementation for the PHP MSSQL database extension.
  6.  *
  7.  * System requirements:
  8.  * <ul>
  9.  * <li>PHP 5</li>
  10.  * <li>The {@link PHP_MANUAL#book.mssql Microsoft SQL Server database extension}</li>
  11.  * </ul>
  12.  *
  13.  * This library is free software: you can redistribute it and/or modify
  14.  * it under the terms of the GNU Lesser General Public License as published by
  15.  * the Free Software Foundation, either version 3 of the License, or
  16.  * (at your option) any later version.
  17.  * The Connector library is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20.  * {@link http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License}
  21.  * for more details.
  22.  *
  23.  * @author Per Egil Roksvaag
  24.  * @copyright 2009 Per Egil Roksvaag
  25.  * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  26.  * @package connector
  27.  * @version 2.0.0
  28.  */
  29.  
  30. ///////////////////////////////////////////////////////////////////////////////
  31. /**
  32.  * Includes the parent class.
  33.  */
  34.  
  35. require_once("BaseConnector.php");
  36.  
  37. ///////////////////////////////////////////////////////////////////////////////
  38. /**
  39.  * IConnector implementation for the PHP MSSQL database extension.
  40.  * @package connector
  41.  */
  42.  
  43. class MssqlConnector extends BaseConnector implements IConnector
  44. {
  45.     ///////////////////////////////////////////////////////////////////////////
  46.     /**
  47.      * Open a connection to a MSSQL Server 2000 or higher and set global options.
  48.      *
  49.      * @param array $connection An associated array of connection settings, like host and user name.
  50.      * @param array $options An associated array of global options for the resulting instance.
  51.      * @return MssqlConnector 
  52.      */
  53.  
  54.     public function MssqlConnector($connection$options array())
  55.     {
  56.         if($this->open($connection))
  57.         {
  58.             unset($this->options[self::PARAM_PREFIX]);
  59.             unset($this->options[self::PARAM_QUERIES]);
  60.             parent::BaseConnector($options);
  61.         }
  62.         else if($this->lookup("throwException"$options))
  63.         {
  64.             throw new Exception("MssqlConnector error: Connection failed.");
  65.         }
  66.         else if($this->lookup(self::LOG_ERROR$options))
  67.         {
  68.             $log "MssqlConnector error: Connection failed in ";
  69.             error_log($log.$_SERVER["SCRIPT_FILENAME"]);
  70.         }
  71.     }
  72.  
  73.     ///////////////////////////////////////////////////////////////////////////
  74.     /**
  75.      * Open a database connection.
  76.      * @param array $connection An associated array of connection settings, like host and user name.
  77.      * @return bool <var>true</var>, if a database connection was successfully opened, <var>false</var> otherwise.
  78.      */
  79.  
  80.     protected function open($connection)
  81.     {
  82.         $hostname $this->lookup(self::CONN_HOSTNAME$connection);
  83.         $database $this->lookup(self::CONN_DATABASE$connection);
  84.         $username $this->lookup(self::CONN_USERNAME$connection);
  85.         $password $this->lookup(self::CONN_PASSWORD$connection);
  86.  
  87.         $link $this->lookup(self::CONN_POOL$connection=== false;
  88.         $port $this->lookup(self::CONN_PORT$connection);
  89.         $port && $hostname.= ",".$port;
  90.  
  91.         $this->conn = $this->lookup(self::CONN_PERSISTENT$connection)
  92.             ? mssql_pconnect($hostname$username$password$link)
  93.             : mssql_connect($hostname$username$password$link);
  94.  
  95.         if($this->conn)
  96.         {
  97.              $database && mssql_select_db($database$this->conn);
  98.         }
  99.         return is_resource($this->conn);
  100.     }
  101.  
  102.     ///////////////////////////////////////////////////////////////////////////
  103.     /**
  104.      * Send a SQL SELECT query to the database and get the query result.
  105.      *
  106.      * @see IConnector::select().
  107.      * @param string $query A SQL query to execute on a database.
  108.      * @param array $param An associated array of values to be used in the $query.
  109.      * @param array $map An array of type definitions for the <var>$param</var> values.
  110.      * @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
  111.      * @throws Exception when $param doesn't match the type definition $map.
  112.      * @return array The query result as a table (array of associated arrays).
  113.      */
  114.  
  115.     public function select($query$param array()$map array()$options array())
  116.     {
  117.         $param TypeValidator::check($param$map);
  118.         $query $this->bind($query$param$stack$options);
  119.         $query $this->build($query$options);
  120.         $hash $this->getHash($query$stack$options);
  121.  
  122.         if($this->lookup(self::LOG_DEBUG$options))
  123.         {
  124.             $log "MssqlConnector debug: Select query in ";
  125.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  126.         }
  127.         if($this->getCache($hash$table$options))
  128.         {
  129.             return $table;
  130.         }
  131.         if($stmt mssql_query($query$this->conn))
  132.         {
  133.             do $table[$this->fetch($stmt$options);
  134.             while(mssql_next_result($stmt));
  135.             mssql_free_result($stmt);
  136.  
  137.             if(count($table== 1$table current($table);
  138.             $this->setCache($hash$table$options);
  139.             return $table;
  140.         }
  141.         if($this->lookup(self::LOG_ERROR$options))
  142.         {
  143.             $log "MssqlConnector error: Select query in ";
  144.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  145.             error_log("MssqlConnector error: ".mssql_get_last_message());
  146.         }
  147.         return false;
  148.     }
  149.  
  150.     ///////////////////////////////////////////////////////////////////////////
  151.     /**
  152.      * Send a SQL INSERT query to the database and get the IDENTITY ID
  153.      * generated from the last INSERT operation (if any).
  154.      *
  155.      * @see IConnector::insert().
  156.      * @param string $query A SQL query to execute on a database.
  157.      * @param array $param An associated array of values to be used in the $query.
  158.      * @param array $map An array of type definitions for the <var>$param</var> values.
  159.      * @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
  160.      * @throws Exception when $param doesn't match the type definition $map.
  161.      * @return int The IDENTITY ID of the last inserted row.
  162.      */
  163.  
  164.     public function insert($query$param array()$map array()$options array())
  165.     {
  166.         $param TypeValidator::check($param$map);
  167.         $query $this->bind($query$param$stack$options);
  168.         $query rtrim($query";").";".LB."SELECT SCOPE_IDENTITY();";
  169.  
  170.         if($this->lookup(self::LOG_DEBUG$options))
  171.         {
  172.             $log "MssqlConnector debug: Insert query in ";
  173.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  174.         }
  175.         if($stmt mssql_query($query$this->conn))
  176.         {
  177.             do $row mssql_fetch_array($stmtMSSQL_NUM);
  178.             while(mssql_next_result($stmt));
  179.             mssql_free_result($stmt);
  180.  
  181.             $key is_array($rowcurrent($rownull;
  182.             return is_numeric($key? (int)$key $key;
  183.          }
  184.         else if($this->lookup(self::LOG_ERROR$options))
  185.         {
  186.             $log "MssqlConnector error: Insert query in ";
  187.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  188.             error_log("MssqlConnector error: ".mssql_get_last_message());
  189.         }
  190.         return false;
  191.     }
  192.  
  193.     ///////////////////////////////////////////////////////////////////////////
  194.     /**
  195.      * Send a SQL UPDATE query to the database and get the number of rows
  196.      * updates by the query.
  197.      *
  198.      * @see IConnector::update().
  199.      * @param string $query A SQL query to execute on a database.
  200.      * @param array $param An associated array of values to be used in the $query.
  201.      * @param array $map An array of type definitions for the <var>$param</var> values.
  202.      * @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
  203.      * @throws Exception when $param doesn't match the type definition $map.
  204.      * @return int The number of rows updates by the query.
  205.      */
  206.  
  207.     public function update($query$param array()$map array()$options array())
  208.     {
  209.         $param TypeValidator::check($param$map);
  210.         $query $this->bind($query$param$stack$options);
  211.  
  212.         if($this->lookup(self::LOG_DEBUG$options))
  213.         {
  214.             $log "MssqlConnector debug: Update query in ";
  215.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  216.         }
  217.         if(mssql_query($query$this->conn))
  218.         {
  219.             return mssql_rows_affected($this->conn);
  220.          }
  221.         else if($this->lookup(self::LOG_ERROR$options))
  222.         {
  223.             $log "MssqlConnector error: Update query in ";
  224.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  225.             error_log("MssqlConnector error: ".mssql_get_last_message());
  226.         }
  227.         return false;
  228.     }
  229.  
  230.     ///////////////////////////////////////////////////////////////////////////
  231.     /**
  232.      * Send a SQL DELETE query to the database and get the number of rows
  233.      * deleted by the query.
  234.      *
  235.      * @see IConnector::delete().
  236.      * @param string $query A SQL query to execute on a database.
  237.      * @param array $param An associated array of values to be used in the $query.
  238.      * @param array $map An array of type definitions for the <var>$param</var> values.
  239.      * @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
  240.      * @throws Exception when $param doesn't match the type definition $map.
  241.      * @return int The number of rows deleted by the query.
  242.      */
  243.  
  244.     public function delete($query$param array()$map array()$options array())
  245.     {
  246.         $param TypeValidator::check($param$map);
  247.         $query $this->bind($query$param$stack$options);
  248.  
  249.         if($this->lookup(self::LOG_DEBUG$options))
  250.         {
  251.             $log "MssqlConnector debug: Delete query in ";
  252.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  253.         }
  254.         if(mssql_query($query$this->conn))
  255.         {
  256.             return mssql_rows_affected($this->conn);
  257.          }
  258.         else if($this->lookup(self::LOG_ERROR$options))
  259.         {
  260.             $log "MssqlConnector error: Delete query in ";
  261.             error_log($log.$_SERVER["SCRIPT_FILENAME"].LB.$query);
  262.             error_log("MssqlConnector error: ".mssql_get_last_message());
  263.         }
  264.         return false;
  265.     }
  266.  
  267.     ///////////////////////////////////////////////////////////////////////////
  268.     /**
  269.      * Add a TOP x statement to a SQL SELECT query when the
  270.      * {@link IConnector::RESULT_LENGTH} is set.
  271.      *
  272.      * @param string $query A SQL query to execute on a database.
  273.      * @param array $options An associated array of options.
  274.      * @return string A modified SQL query.
  275.      */
  276.  
  277.     protected function build($query$options array())
  278.     {
  279.         $offset $this->lookup(self::RESULT_OFFSET$options0);
  280.         $length $this->lookup(self::RESULT_LENGTH$options);
  281.         
  282.         if(is_int($length))
  283.         {
  284.             $number $offset $length;
  285.             $pattern "/\A(SELECT)([\s]+(ALL|DISTINCT))?\b/i";
  286.             $query preg_replace($pattern"$0 TOP {$number}"$query);
  287.         }
  288.         return $query;
  289.     }
  290.  
  291.     ///////////////////////////////////////////////////////////////////////////
  292.     /**
  293.      * Fetch multiple rows of a query result.
  294.      *
  295.      * If the {@link IConnector::RESULT_LENGTH} or {@link IConnector::RESULT_OFFSET}
  296.      * options are set, some rows are omitted from the beginning and/or the end
  297.      * of the query result.
  298.      * If the {@link IConnector::RESULT_KEY_FIELD} option is set, the
  299.      * resulting table is an <b>associated</b> array of rows.
  300.      *
  301.      * @param resource $stmt A statement resource corresponding to an executed statement.
  302.      * @param array $options An associated array of options.
  303.      * @return array The query result as a table (array of associated arrays).
  304.      */
  305.  
  306.     protected function fetch($stmt$options array())
  307.     {
  308.         $key $this->lookup(self::RESULT_KEY_FIELD$options);
  309.         $offset $this->lookup(self::RESULT_OFFSET$options0);
  310.         $length $this->lookup(self::RESULT_LENGTH$options);
  311.         $number is_null($lengthPHP_INT_MAX $length $offset;
  312.         $table array();
  313.         $index 0;
  314.  
  315.         while($row mssql_fetch_array($stmtMSSQL_ASSOC))
  316.         {
  317.             if($number <= $indexbreak;
  318.             if($offset $index++continue;
  319.             $row $this->strDecode($row$options);
  320.             $key $table[$row[$key]] $row $table[$row;
  321.         }
  322.         return $table;
  323.     }
  324.  
  325.     ///////////////////////////////////////////////////////////////////////////
  326. }
  327.  
  328. ?>

Documentation generated on Wed, 03 Jun 2009 12:41:52 +0200 by phpDocumentor 1.4.1