SQLite.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2005-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 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 /* $Id: SQLite.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00022     final class SQLite extends Sequenceless
00023     {
00027         public static function getDialect()
00028         {
00029             return LiteDialect::me();
00030         }
00031         
00035         public function connect()
00036         {
00037             if ($this->persistent)
00038                 $this->link = sqlite_popen($this->basename);
00039             else
00040                 $this->link = sqlite_open($this->basename);
00041             
00042             if (!$this->link)
00043                 throw new DatabaseException(
00044                     'can not open SQLite base: '
00045                     .sqlite_error_string(sqlite_last_error($this->link))
00046                 );
00047             
00048             return $this;
00049         }
00050         
00054         public function disconnect()
00055         {
00056             if ($this->isConnected())
00057                 sqlite_close($this->link);
00058             
00059             return $this;
00060         }
00061         
00062         public function isConnected()
00063         {
00064             return is_resource($this->link);
00065         }
00066         
00071         public function setDbEncoding()
00072         {
00073             throw new UnsupportedMethodException();
00074         }
00075         
00080         public function queryRaw($queryString)
00081         {
00082             try {
00083                 return sqlite_query($queryString, $this->link);
00084             } catch (BaseException $e) {
00085                 $code = sqlite_last_error($this->link);
00086                 
00087                 if ($code == 19)
00088                     $e = 'DuplicateObjectException';
00089                 else
00090                     $e = 'DatabaseException';
00091                 
00092                 throw new $e(
00093                     sqlite_error_string($code).' - '.$queryString,
00094                     $code
00095                 );
00096             }
00097         }
00098         
00103         public function queryCount(Query $query)
00104         {
00105             $this->queryNull($query);
00106             
00107             return sqlite_changes($this->link);
00108         }
00109         
00110         public function queryRow(Query $query)
00111         {
00112             $res = $this->query($query);
00113             
00114             if ($this->checkSingle($res)) {
00115                 if (!$row = sqlite_fetch_array($res, SQLITE_NUM))
00116                     return null;
00117                 
00118                 $names = $query->getFieldNames();
00119                 $width = count($names);
00120                 $assoc = array();
00121                 
00122                 for ($i = 0; $i < $width; ++$i)
00123                     $assoc[$names[$i]] = $row[$i];
00124                 
00125                 return $assoc;
00126             } else
00127                 return null;
00128         }
00129         
00130         public function queryColumn(Query $query)
00131         {
00132             $res = $this->query($query);
00133             
00134             if ($res) {
00135                 $array = array();
00136                 
00137                 while ($row = sqlite_fetch_single($res))
00138                     $array[] = $row;
00139                 
00140                 return $array;
00141             } else
00142                 return null;
00143         }
00144         
00145         public function querySet(Query $query)
00146         {
00147             $res = $this->query($query);
00148             
00149             if ($res) {
00150                 $array = array();
00151                 $names = $query->getFieldNames();
00152                 $width = count($names);
00153                 
00154                 while ($row = sqlite_fetch_array($res, SQLITE_NUM)) {
00155                     $assoc = array();
00156                     
00157                     for ($i = 0; $i < $width; ++$i)
00158                         $assoc[$names[$i]] = $row[$i];
00159                     
00160                     $array[] = $assoc;
00161                 }
00162                 
00163                 return $array;
00164             } else
00165                 return null;
00166         }
00167         
00168         public function getTableInfo($table)
00169         {
00170             throw new UnimplementedFeatureException();
00171         }
00172         
00173         protected function getInsertId()
00174         {
00175             return sqlite_last_insert_rowid($this->link);
00176         }
00177         
00178         private function checkSingle($result)
00179         {
00180             if (sqlite_num_rows($result) > 1)
00181                 throw new TooManyRowsException(
00182                     'query returned too many rows (we need only one)'
00183                 );
00184             
00185             return $result;
00186         }
00187     }
00188 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4