00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00018 abstract class DB
00019 {
00020 const FULL_TEXT_AND = 1;
00021 const FULL_TEXT_OR = 2;
00022
00023 protected $link = null;
00024
00025 protected $persistent = false;
00026
00027
00028 protected $username = null;
00029 protected $password = null;
00030 protected $hostname = null;
00031 protected $port = null;
00032 protected $basename = null;
00033 protected $encoding = null;
00034
00038 private $transaction = false;
00039
00040 private $queue = array();
00041 private $toQueue = false;
00042
00043 abstract public function connect();
00044 abstract public function disconnect();
00045
00046 abstract public function getTableInfo($table);
00047
00048 abstract public function queryRaw($queryString);
00049
00050 abstract public function queryRow(Query $query);
00051 abstract public function querySet(Query $query);
00052 abstract public function queryColumn(Query $query);
00053 abstract public function queryCount(Query $query);
00054
00055
00056 abstract public function setDbEncoding();
00057
00058 public function __destruct()
00059 {
00060 if ($this->isConnected()) {
00061 if ($this->transaction)
00062 $this->rollback();
00063
00064 if (!$this->persistent)
00065 $this->disconnect();
00066 }
00067 }
00068
00069 public static function getDialect()
00070 {
00071 throw new UnimplementedFeatureException('implement me, please');
00072 }
00073
00079 public static function spawn(
00080 $connector, $user, $pass, $host,
00081 $base = null, $persistent = false, $encoding = null
00082 )
00083 {
00084 $db = new $connector;
00085
00086 $db->
00087 setUsername($user)->
00088 setPassword($pass)->
00089 setHostname($host)->
00090 setBasename($base)->
00091 setPersistent($persistent)->
00092 setEncoding($encoding);
00093
00094 return $db;
00095 }
00096
00097 public function getLink()
00098 {
00099 return $this->link;
00100 }
00101
00110 public function begin(
00111 $level = null,
00112 $mode = null
00113 )
00114 {
00115 $begin = 'start transaction';
00116
00117 if ($level && $level instanceof IsolationLevel)
00118 $begin .= ' '.$level->toString();
00119
00120 if ($mode && $mode instanceof AccessMode)
00121 $begin .= ' '.$mode->toString();
00122
00123 if ($this->toQueue)
00124 $this->queue[] = $begin;
00125 else
00126 $this->queryRaw("{$begin};\n");
00127
00128 $this->transaction = true;
00129
00130 return $this;
00131 }
00132
00136 public function commit()
00137 {
00138 if ($this->toQueue)
00139 $this->queue[] = 'commit;';
00140 else
00141 $this->queryRaw("commit;\n");
00142
00143 $this->transaction = false;
00144
00145 return $this;
00146 }
00147
00151 public function rollback()
00152 {
00153 if ($this->toQueue)
00154 $this->queue[] = 'rollback;';
00155 else
00156 $this->queryRaw("rollback;\n");
00157
00158 $this->transaction = false;
00159
00160 return $this;
00161 }
00162
00163 public function inTransaction()
00164 {
00165 return $this->transaction;
00166 }
00168
00177 public function queueStart()
00178 {
00179 if ($this->hasQueue())
00180 $this->toQueue = true;
00181
00182 return $this;
00183 }
00184
00188 public function queueStop()
00189 {
00190 $this->toQueue = false;
00191
00192 return $this;
00193 }
00194
00198 public function queueDrop()
00199 {
00200 $this->queue = array();
00201
00202 return $this;
00203 }
00204
00208 public function queueFlush()
00209 {
00210 if ($this->queue)
00211 $this->queryRaw(
00212 implode(";\n", $this->queue)
00213 );
00214
00215 $this->toQueue = false;
00216
00217 return $this->queueDrop();
00218 }
00219
00220 public function isQueueActive()
00221 {
00222 return $this->toQueue;
00223 }
00225
00230 public function query(Query $query)
00231 {
00232 return $this->queryRaw($query->toDialectString($this->getDialect()));
00233 }
00234
00235 public function queryNull(Query $query)
00236 {
00237 if ($query instanceof SelectQuery)
00238 throw new WrongArgumentException(
00239 'only non-select queries supported'
00240 );
00241
00242 if ($this->toQueue) {
00243 $this->queue[] = $query->toDialectString($this->getDialect());
00244 return true;
00245 } else
00246 return $this->query($query);
00247 }
00249
00250 public function isConnected()
00251 {
00252 return is_resource($this->link);
00253 }
00254
00255 public function hasSequences()
00256 {
00257 return false;
00258 }
00259
00260 public function hasQueue()
00261 {
00262 return true;
00263 }
00264
00265 public function isPersistent()
00266 {
00267 return $this->persistent;
00268 }
00269
00273 public function setPersistent($really = false)
00274 {
00275 $this->persistent = ($really === true);
00276
00277 return $this;
00278 }
00279
00283 public function setUsername($name)
00284 {
00285 $this->username = $name;
00286
00287 return $this;
00288 }
00289
00293 public function setPassword($password)
00294 {
00295 $this->password = $password;
00296
00297 return $this;
00298 }
00299
00303 public function setHostname($host)
00304 {
00305 $port = null;
00306
00307 if (strpos($host, ':') !== false)
00308 list($host, $port) = explode(':', $host, 2);
00309
00310 $this->hostname = $host;
00311 $this->port = $port;
00312
00313 return $this;
00314 }
00315
00319 public function setBasename($base)
00320 {
00321 $this->basename = $base;
00322
00323 return $this;
00324 }
00325
00329 public function setEncoding($encoding)
00330 {
00331 $this->encoding = $encoding;
00332
00333 return $this;
00334 }
00335 }
00336 ?>