Spaces:
Sleeping
Sleeping
File size: 7,543 Bytes
07c3cdd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | <?php
/*
* $Id: ConnectionCommon.php,v 1.5 2005/10/17 19:03:51 dlawson_mi Exp $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://creole.phpdb.org>.
*/
/**
* Class that contains some shared/default information for connections. Classes may wish to extend this so
* as not to worry about the sleep/wakeup methods, etc.
*
* In reality this class is not very useful yet, so there's not much incentive for drivers to extend this.
*
* @author Hans Lellelid <hans@xmpl.org>
* @version $Revision: 1.5 $
* @package creole.common
*/
abstract class ConnectionCommon {
// Constants that define transaction isolation levels.
// [We don't have any code using these yet, so there's no need
// to initialize these values at this point.]
// const TRANSACTION_NONE = 0;
// const TRANSACTION_READ_UNCOMMITTED = 1;
// const TRANSACTION_READ_COMMITTED = 2;
// const TRANSACTION_REPEATABLE_READ = 3;
// const TRANSACTION_SERIALIZABLE = 4;
/**
* The depth level of current transaction.
* @var int
*/
protected $transactionOpcount = 0;
/**
* DB connection resource id.
* @var resource
*/
protected $dblink;
/**
* Array hash of connection properties.
* @var array
*/
protected $dsn;
/**
* Flags (e.g. Connection::PERSISTENT) for current connection.
* @var int
*/
protected $flags = 0;
/**
* This "magic" method is invoked upon serialize() and works in tandem with the __wakeup()
* method to ensure that your database connection is serializable.
*
* This method returns an array containing the names of any members of your class
* which need to be serialized in order to allow the class to re-connect to the database
* when it is unserialized.
*
* <p>
* Developers:
*
* Note that you cannot serialize resources (connection links) and expect them to
* be valid when you unserialize. For this reason, you must re-connect to the database in the
* __wakeup() method.
*
* It's up to your class implimentation to ensure that the necessary data is serialized.
* You probably at least need to serialize:
*
* (1) the DSN array used by connect() method
* (2) Any flags that were passed to the connection
* (3) Possibly the autocommit state
*
* @return array The class variable names that should be serialized.
* @see __wakeup()
* @see DriverManager::getConnection()
* @see DatabaseInfo::__sleep()
*/
public function __sleep()
{
return array('dsn', 'flags');
}
/**
* This "magic" method is invoked upon unserialize().
* This method will re-connects to the database using the information that was
* stored using the __sleep() method.
* @see __sleep()
*/
public function __wakeup()
{
$this->connect($this->dsn, $this->flags);
}
/**
* @see Connection::getResource()
*/
public function getResource()
{
return $this->dblink;
}
/**
* @see Connection::getDSN()
*/
public function getDSN() {
return $this->dsn;
}
/**
* @see Connection::getFlags()
*/
public function getFlags()
{
return $this->flags;
}
/**
* Creates a CallableStatement object for calling database stored procedures.
*
* @param string $sql
* @return CallableStatement
*/
public function prepareCall($sql)
{
throw new SQLException("Current driver does not support stored procedures using CallableStatement.");
}
/**
* Driver classes should override this if they support transactions.
*
* @return boolean
*/
public function supportsNestedTrans()
{
return false;
}
/**
* Begins a transaction (if supported).
*/
public function begin()
{
if ($this->transactionOpcount === 0 || $this->supportsNestedTrans()) {
$this->beginTrans();
}
$this->transactionOpcount++;
}
/**
* Commits statements in a transaction.
*/
public function commit()
{
if ($this->transactionOpcount > 0) {
if ($this->transactionOpcount == 1 || $this->supportsNestedTrans()) {
$this->commitTrans();
}
$this->transactionOpcount--;
}
}
/**
* Rollback changes in a transaction.
*/
public function rollback()
{
if ($this->transactionOpcount > 0) {
if ($this->transactionOpcount == 1 || $this->supportsNestedTrans()) {
$this->rollbackTrans();
}
$this->transactionOpcount--;
}
}
/**
* Enable/disable automatic commits.
*
* Pushes SQLWarning onto $warnings stack if the autocommit value is being changed mid-transaction. This function
* is overridden by driver classes so that they can perform the necessary begin/end transaction SQL.
*
* If auto-commit is being set to TRUE, then the current transaction will be committed immediately.
*
* @param boolean $bit New value for auto commit.
* @return void
*/
public function setAutoCommit($bit)
{
if ($this->transactionOpcount > 0) {
trigger_error("Changing autocommit in mid-transaction; committing " . $this->transactionOpcount . " uncommitted statements.", E_USER_WARNING);
}
if (!$bit) {
$this->begin();
}
else {
$this->commit();
}
}
/**
* Get auto-commit status.
*
* @return boolean
*/
public function getAutoCommit()
{
return ($this->transactionOpcount == 0);
}
/**
* Begin new transaction.
* Driver classes should override this method if they support transactions.
*/
protected function beginTrans()
{
}
/**
* Commit the current transaction.
* Driver classes should override this method if they support transactions.
*/
protected function commitTrans()
{
}
/**
* Roll back (undo) the current transaction.
* Driver classes should override this method if they support transactions.
*/
protected function rollbackTrans()
{
}
/**
* Returns false if connection is closed.
* @return boolean
*/
public function isConnected()
{
return !empty($this->dblink);
}
} |