home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Magazine / HomeAutomation / Apache / lib / php / DB / mssql.php < prev    next >
Encoding:
PHP Script  |  2000-07-11  |  6.1 KB  |  200 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Sterling Hughes <sterling@designmultimedia.com>             |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: mssql.php,v 1.5 2000/06/21 02:22:04 chagenbu Exp $
  21. //
  22. // Database independent query interface definition for PHP's Microsoft SQL Server
  23. // extension.
  24. //
  25.  
  26. include_once 'DB/common.php';
  27.  
  28. class DB_mssql extends DB_common {
  29.  
  30.     var $connection;
  31.     var $phptype, $dbsyntax;
  32.     var $prepare_tokens = array();
  33.     var $prepare_types = array();
  34.  
  35.     function DB_mssql() {
  36.         $this->phptype = 'mssql';
  37.         $this->dbsyntax = 'mssql';
  38.         $this->features = array(
  39.             'prepare' => false,
  40.             'pconnect' => true,
  41.             'transactions' => false
  42.         );
  43.     }
  44.  
  45.     function connect ( $dsn, $persistent=false ) {
  46.         if(is_array($dsn)) {
  47.             $dsninfo = &$dsn;
  48.         } else {
  49.             $dsninfo = DB::parseDSN($dsn);
  50.         }
  51.         if (!$dsninfo || !$dsninfo['phptype']) {
  52.             return DB_ERROR; 
  53.         }
  54.  
  55.         $user = $dsninfo['username'];
  56.         $pw = $dsninfo['password'];
  57.         $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
  58.         $connect_function = $persistent ? 'mssql_pconnect' : 'mssql_connect';
  59.         if ($dbhost && $user && $pw) {
  60.             $conn = $connect_function($dbhost, $user, $pw);
  61.         } elseif ($dbhost && $user) {
  62.             $conn = $connect_function($dbhost,$user);
  63.         } else {
  64.             $conn = $connect_function($dbhost);
  65.         }
  66.         if ($dsninfo['database']) {
  67.             @mssql_select_db($dsninfo['database'], $conn);
  68.         } else {
  69.             return DB_ERROR;
  70.         }
  71.         $this->connection = $conn;
  72.         return DB_OK;
  73.     }
  74.  
  75.     function disconnect() {
  76.         return @mssql_close($this->connection);
  77.     }
  78.  
  79.     function &query( $stmt ) {
  80.         $result = @mssql_query($stmt, $this->connection);
  81.         if (!$result) {
  82.             return DB_ERROR;
  83.         }
  84.         // Determine which queries that should return data, and which
  85.         // should return an error code only.
  86.         if (preg_match('/(SELECT|SHOW|LIST|DESCRIBE)/i', $stmt)) {
  87.             $resultObj = new DB_result($this, $result);
  88.             return $resultObj;
  89.         } else {
  90.             return DB_OK;
  91.         }
  92.     }
  93.  
  94.     function simpleQuery($stmt) {
  95.         $result = @mssql_query($stmt, $this->connection);
  96.         if (!$result) {
  97.             return DB_ERROR;
  98.         }
  99.         // Determine which queries that should return data, and which
  100.         // should return an error code only.
  101.         if ( preg_match('/(SELECT|SHOW|LIST|DESCRIBE)/i', $stmt) ) {
  102.             return $result;
  103.         } else {
  104.             return DB_OK;
  105.         }
  106.     }
  107.  
  108.     function &fetchRow($result, $getmode=DB_GETMODE_DEFAULT) {
  109.         if ($getmode & DB_GETMODE_ASSOC) {
  110.             $row = @mssql_fetch_array($result);
  111.         } else {
  112.             $row = @mssql_fetch_row($result);
  113.         }
  114.         if (!$row) {
  115.             return DB_ERROR;
  116.         }
  117.         return $row;
  118.     }
  119.  
  120.     function fetchInto($result, &$ar, $getmode=DB_GETMODE_DEFAULT) {
  121.         if ($getmode & DB_GETMODE_ASSOC) {
  122.             $ar = @mssql_fetch_array($result);
  123.         } else {
  124.             $ar = @mssql_fetch_row($result);
  125.         }
  126.         if (!$ar) {
  127.             return DB_ERROR;
  128.         }
  129.         return DB_OK;
  130.     }
  131.  
  132.     function freeResult($result) {
  133.         if (is_resource($result)) {
  134.             return @mssql_free_result($result);
  135.         }
  136.         if (!isset($this->prepare_tokens[$result])) {
  137.             return false;
  138.         }
  139.         unset($this->prepare_tokens[$result]);
  140.         unset($this->prepare_types[$result]);
  141.         return true; 
  142.     }
  143.  
  144.     function numCols($result) {
  145.         $cols = @mssql_num_fields($result);
  146.         if (!$cols) {
  147.             return DB_ERROR;
  148.         }
  149.         return $cols;
  150.     }
  151.  
  152.     function prepare($query) {
  153.         $tokens = split('[\&\?]', $query);
  154.         $token = 0;
  155.         $types = array();
  156.         for ($i = 0; $i < strlen($query); $i++) {
  157.             switch ($query[$i]) {
  158.                 case '?':
  159.                     $types[$token++] = DB_PARAM_SCALAR;
  160.                     break;
  161.                 case '&':
  162.                     $types[$token++] = DB_PARAM_OPAQUE;
  163.                     break;
  164.             }
  165.         }
  166.         $this->prepare_tokens[] = &$tokens;
  167.         end($this->prepare_tokens);
  168.         $k = key($this->prepare_tokens);
  169.         $this->prepare_types[$k] = $types;
  170.         return $k;
  171.     }
  172.  
  173.     function execute($stmt, $data = false) {
  174.         $realquery = $this->execute_emulate_query($stmt, $data);
  175.         $result = @mssql_query($realquery, $this->connection);
  176.         if (!$result) {
  177.             return DB_ERROR;
  178.         }
  179.         if ( preg_match('/(SELECT|SHOW|LIST|DESCRIBE)/i', $realquery) ) {
  180.             return $result;
  181.         } else {
  182.             return DB_OK;
  183.         }
  184.     }
  185.  
  186.     function autoCommit($onoff=false) {
  187.         return DB_ERROR_NOT_CAPABLE;
  188.     }
  189.  
  190.     function commit() {
  191.         return DB_ERROR_NOT_CAPABLE;
  192.     }
  193.  
  194.     function rollback() {
  195.         return DB_ERROR_NOT_CAPABLE;
  196.     }
  197. }
  198.  
  199. ?>
  200.