Source for file mssql.dbconnection.php

Documentation is available at mssql.dbconnection.php

  1. <?php
  2. /**
  3.  * @package    jelix
  4.  * @subpackage db_driver
  5.  * @author     Yann Lecommandoux
  6.  * @copyright  2008 Yann Lecommandoux
  7.  * @link     http://www.jelix.org
  8.  * @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  9.  */
  10.  
  11. /**
  12.  * @experimental
  13.  */
  14. class mssqlDbConnection extends jDbConnection {
  15.  
  16.     /**
  17.      * Default constructor
  18.      * @param array $profile profile de connexion
  19.      * @return unknown_type 
  20.      */
  21.     function __construct($profile){
  22.         if(!function_exists('mssql_connect')){
  23.             throw new jException('jelix~db.error.nofunction','mssql');
  24.         }
  25.         parent::__construct($profile);
  26.     }
  27.  
  28.     /**
  29.      * begin a transaction
  30.      */
  31.     public function beginTransaction (){
  32.         $this->_doExec ('SET IMPLICIT_TRANSACTIONS OFF');
  33.         $this->_doExec ('BEGIN TRANSACTION');
  34.     }
  35.  
  36.     /**
  37.      * Commit since the last begin
  38.      */
  39.     public function commit (){
  40.         $this->_doExec ('COMMIT TRANSACTION');
  41.         $this->_doExec ('SET IMPLICIT_TRANSACTIONS ON');
  42.     }
  43.  
  44.     /**
  45.      * Rollback since the last BEGIN
  46.      */
  47.     public function rollback (){
  48.         $this->_doExec ('ROLLBACK TRANSACTION');
  49.         $this->_doExec ('SET IMPLICIT_TRANSACTIONS ON');
  50.     }
  51.  
  52.     /**
  53.      *
  54.      */
  55.     public function prepare ($query){
  56.         throw new jException('jelix~db.error.feature.unsupported'array('mssql','prepare'));
  57.     }
  58.  
  59.     public function errorInfo(){
  60.         return array'HY000'mssql_get_last_message());
  61.     }
  62.  
  63.     public function errorCode(){
  64.         return mssql_get_last_message();
  65.     }
  66.      
  67.     /**
  68.      * (non-PHPdoc)
  69.      * initialize the connection to the database
  70.      * @see lib/jelix/db/jDbConnection#_connect()
  71.      */
  72.     protected function _connect (){
  73.         $funcconnect ($this->profile['persistent']'mssql_pconnect':'mssql_connect');
  74.         if($cnx @$funcconnect ($this->profile['host']$this->profile['user']$this->profile['password'])){
  75.             /*if(isset($this->profile['force_encoding']) && $this->profile['force_encoding'] == true
  76.             && isset($this->_charsets[$GLOBALS['gJConfig']->charset])){
  77.                 mssql_query("SET ANSI_DEFAULTS ON", $cnx);
  78.             }*/
  79.             return $cnx;
  80.         }else{
  81.             throw new jException('jelix~db.error.connection',$this->profile['host']);
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * (non-PHPdoc)
  87.      *     close the connection to the database
  88.      * @see lib/jelix/db/jDbConnection#_disconnect()
  89.      */
  90.     protected function _disconnect (){
  91.         return mssql_close ($this->_connection);
  92.     }
  93.  
  94.     /**
  95.      * (non-PHPdoc)
  96.      *     execute an SQL instruction
  97.      * @see lib/jelix/db/jDbConnection#_doQuery()
  98.      */
  99.     protected function _doQuery ($query){
  100.         if(!mssql_select_db ($this->profile['database']$this->_connection)){
  101.             if(mssql_get_last_message()){
  102.                 throw new jException('jelix~db.error.database.unknown',$this->profile['database']);
  103.             else {
  104.                 throw new jException('jelix~db.error.connection.closed',$this->profile['name']);
  105.             }
  106.         }
  107.  
  108.         if ($qI mssql_query ($query$this->_connection)){
  109.             return new mssqlDbResultSet ($qI);
  110.         else{
  111.             throw new jException('jelix~db.error.query.bad',  mssql_get_last_message());
  112.         }
  113.     }
  114.      
  115.     /**
  116.      * (non-PHPdoc)
  117.      * @see lib/jelix/db/jDbConnection#_doExec()
  118.      */
  119.     protected function _doExec($query){
  120.         if(!mssql_select_db ($this->profile['database']$this->_connection))
  121.         throw new jException('jelix~db.error.database.unknown',$this->profile['database']);
  122.  
  123.         if ($qI mssql_query ($query$this->_connection)){
  124.             return mssql_rows_affected($this->_connection);
  125.         }else{
  126.             throw new jException('jelix~db.error.query.bad'mssql_get_last_message());
  127.         }
  128.     }
  129.     /**
  130.      * WARNING: it doesn't take care about offset and number.
  131.      * @notimplemented
  132.      * @see lib/jelix/db/jDbConnection#_doLimitQuery()
  133.      */
  134.     protected function _doLimitQuery ($queryString$offset$number){
  135.         $result $this->_doQuery($queryString);
  136.         return $result;
  137.     }
  138.  
  139.     /**
  140.      * (non-PHPdoc)
  141.      *     return the last inserted ID incremented in database
  142.      * @see lib/jelix/db/jDbConnection#lastInsertId()
  143.      */
  144.     public function lastInsertId($fromSequence=''){
  145.         $queryString 'SELECT @@IDENTITY AS id';
  146.         $result $this->_doQuery($queryString);
  147.         return $result;
  148.     }
  149.  
  150.     /**
  151.      * tell mssql to be implicit commit or not
  152.      * @param boolean $state the state of the autocommit value
  153.      * @return void 
  154.      */
  155.     protected function _autoCommitNotify ($state){
  156.         if ($state == ){
  157.             $this->query ('SET IMPLICIT_TRANSACTIONS ON');
  158.         else {
  159.             $this->query ('SET IMPLICIT_TRANSACTIONS OFF');
  160.         }
  161.     }
  162.  
  163.     /**
  164.      * escape special characters
  165.      * @todo support of binary strings
  166.      */
  167.     protected function _quote($text$binary){
  168.         return str_replace"'""''"$text );
  169.     }
  170. }

Documentation generated on Thu, 19 Sep 2013 00:08:38 +0200 by phpDocumentor 1.4.3