# SQLite.class.php

Go to the documentation of this file.

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