# DB.class.php

Go to the documentation of this file.

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