onPHP

core/DB/DB.class.php Source File

 

DB.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2007 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 General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: DB.class.php 3202 2007-04-30 19:01:43Z voxus $ */
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         // credentials
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         // actually set's encoding
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             /* IsolationLevel */ $level = null,
00112             /* AccessMode */ $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         }
00220         
00225         public function query(Query $query)
00226         {
00227             return $this->queryRaw($query->toDialectString($this->getDialect()));
00228         }
00229 
00230         public function queryNull(Query $query)
00231         {
00232             if ($query instanceof SelectQuery)
00233                 throw new WrongArgumentException(
00234                     'only non-select queries supported'
00235                 );
00236             
00237             if ($this->toQueue) {
00238                 $this->queue[] = $query->toDialectString($this->getDialect());
00239                 return true;
00240             } else
00241                 return $this->query($query);
00242         }
00244         
00245         public function isConnected()
00246         {
00247             return is_resource($this->link);
00248         }
00249         
00250         public function hasSequences()
00251         {
00252             return false;
00253         }
00254         
00255         public function hasQueue()
00256         {
00257             return true;
00258         }
00259 
00260         public function isPersistent()
00261         {
00262             return $this->persistent;
00263         }
00264         
00268         public function setPersistent($really = false)
00269         {
00270             $this->persistent = ($really === true);
00271             
00272             return $this;
00273         }
00274         
00278         public function setUsername($name)
00279         {
00280             $this->username = $name;
00281             
00282             return $this;
00283         }
00284         
00288         public function setPassword($password)
00289         {
00290             $this->password = $password;
00291             
00292             return $this;
00293         }
00294         
00298         public function setHostname($host)
00299         {
00300             $port = null;
00301             
00302             if (strpos($host, ':') !== false)
00303                 list($host, $port) = explode(':', $host, 2);
00304             
00305             $this->hostname = $host;
00306             $this->port = $port;
00307             
00308             return $this;
00309         }
00310         
00314         public function setBasename($base)
00315         {
00316             $this->basename = $base;
00317             
00318             return $this;
00319         }
00320         
00324         public function setEncoding($encoding)
00325         {
00326             $this->encoding = $encoding;
00327             
00328             return $this;
00329         }
00330     }
00331 ?>

generated by doxygen-1.5.1
for onPHP at Mon Apr 30 23:10:09 2007