cvs: php4 /pear/DB mssql.php

From: Date: Wed, 15 Aug 2001 18:48:06 +0000
Subject: cvs: php4 /pear/DB mssql.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-533@lists.php.net to get a copy of this message
cox Wed Aug 15 14:48:06 2001 EDT Modified files: /php4/pear/DB mssql.php Log: - added sequences (emulated), contributed by: Esben Maaløe <acebone@f2s.com> - added extension error handling: errorCode, mssqRaiseError, errorcode_map,... untested changes, please test them or provide a SQL Server :-) Index: php4/pear/DB/mssql.php diff -u php4/pear/DB/mssql.php:1.24 php4/pear/DB/mssql.php:1.25 --- php4/pear/DB/mssql.php:1.24 Tue Jul 31 19:19:13 2001 +++ php4/pear/DB/mssql.php Wed Aug 15 14:48:05 2001 @@ -16,7 +16,7 @@ // | Authors: Sterling Hughes <sterling@php.net> | // +----------------------------------------------------------------------+ // -// $Id: mssql.php,v 1.24 2001/07/31 23:19:13 ssb Exp $ +// $Id: mssql.php,v 1.25 2001/08/15 18:48:05 cox Exp $ // // Database independent query interface definition for PHP's Microsoft SQL Server // extension. @@ -43,6 +43,10 @@ 'pconnect' => true, 'transactions' => true ); + // XXX Add here error codes ie: 'S100E' => DB_ERROR_SYNTAX + $this->errorcode_map = array( + + ); } function connect($dsninfo, $persistent = false) @@ -59,10 +63,15 @@ } else { $conn = @$connect_function($dbhost); } + if (!$conn) { + return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null, + null, mssql_get_last_message()); + } if ($dsninfo['database']) { - @mssql_select_db($dsninfo['database'], $conn); - } else { - return $this->raiseError(mssql_get_last_message()); + if (!@mssql_select_db($dsninfo['database'], $conn)) { + return $this->raiseError(DB_ERROR_NODBSELECTED, null, null, + null, mssql_get_last_message()); + } } $this->connection = $conn; return DB_OK; @@ -84,14 +93,14 @@ if ($this->transaction_opcount == 0) { $result = @mssql_query('BEGIN TRAN', $this->connection); if (!$result) { - return $this->raiseError(mssql_get_last_message()); + return $this->mssqlRaiseError(); } } $this->transaction_opcount++; } $result = @mssql_query($query, $this->connection); if (!$result) { - return $this->raiseError(mssql_get_last_message()); + return $this->mssqlRaiseError(); } // Determine which queries that should return data, and which // should return an error code only. @@ -149,7 +158,7 @@ { $cols = @mssql_num_fields($result); if (!$cols) { - return $this->raiseError(mssql_get_last_message()); + return $this->mssqlRaiseError(); } return $cols; } @@ -158,7 +167,7 @@ { $rows = @mssql_num_rows($result); if (!$rows) { - return $this->raiseError(mssql_get_last_message()); + return $this->mssqlRaiseError(); } return $rows; } @@ -186,7 +195,7 @@ $result = @mssql_query('COMMIT TRAN', $this->connection); $this->transaction_opcount = 0; if (!$result) { - return $this->raiseError(mssql_get_last_message()); + return $this->mssqlRaiseError(); } } return DB_OK; @@ -204,7 +213,7 @@ $result = @mssql_query('ROLLBACK TRAN', $this->connection); $this->transaction_opcount = 0; if (!$result) { - return $this->raiseError(mssql_get_last_message()); + return $this->mssqlRaiseError(); } } return DB_OK; @@ -224,7 +233,7 @@ if (DB::isManip($this->last_query)) { $res = @mssql_query('select @@rowcount', $this->connection); if (!$res) { - return $this->raiseError(mssql_get_last_message()); + return $this->mssqlRaiseError(); } $ar = @mssql_fetch_row($res); if (!$ar) { @@ -232,13 +241,96 @@ } else { @mssql_free_result($res); $result = $ar[0]; - } + } } else { $result = 0; } return $result; } + // {{{ nextId() + + /** + * Get the next value in a sequence. We emulate sequences + * for MSSQL. Will create the sequence if it does not exist. + * + * @access public + * + * @param $seq_name the name of the sequence + * + * @param $ondemand whether to create the sequence table on demand + * (default is true) + * + * @return a sequence integer, or a DB error + */ + function nextId($seq_name, $ondemand = true) + { + $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); + $repeat = 0; + do { + $result = $this->query("INSERT INTO ${sqn}_seq (vapor) VALUES (0)"); + if ($ondemand && DB::isError($result) && + ($result->getCode() == DB_ERROR || $result->getCode() == DB_ERROR_NOSUCHTABLE)) + { + $repeat = 1; + $result = $this->createSequence($seq_name); + if (DB::isError($result)) { + return $result; + } + } else { + $result = $this->query("SELECT @@IDENTITY FROM ${sqn}_seq"); + $repeat = 0; + } + } while ($repeat); + if (DB::isError($result)) { + return $result; + } + $result = $result->fetchRow(); + return $result[0]; + } + // }}} + // {{{ createSequence() + + function createSequence($seq_name) + { + $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); + return $this->query("CREATE TABLE ${sqn}_seq". + '([id] [int] IDENTITY (1, 1) NOT NULL ,' . + '[vapor] [int] NULL)'); + } + // }}} + // {{{ dropSequence() + + function dropSequence($seq_name) + { + $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); + return $this->query("DROP TABLE ${sqn}_seq"); + } + // }}} + + function errorCode() + { + $this->pushErrorHandling(PEAR_ERROR_RETURN); + $error_code = $this->getOne('select @@ERROR as ErrorCode'); + $this->popErrorHandling(); + // XXX Debug + if (!isset($this->errorcode_map[$error_code])) { + trigger_error("Error code: '$error_code' is not defined, please add it to \$this->errorcode_map", E_USER_WARNING); + } + return $error_code; + } + + function mssqlRaiseError($code = null) + { + if ($code !== null) { + $code = $this->errorCode(); + if (DB::isError($code)) { + return $this->raiseError($code); + } + } + return $this->raiseError($code, null, null, null, + mssql_get_last_message()); + } } -?> +?> \ No newline at end of file

« previous php.pear.cvs (#533) next »