male zmiany w konfiguracji, bazie danych, pare nowych funkcji w helpers, refaktoryzac...
[watermeloncms.git] / cms / wtrmln / libs / db.php
blobb96f612c9a3e7def3fb0ce34d9707761b8625aa2
1 <?php if(!defined('WTRMLN_IS')) die;
2 /********************************************************************
4 Watermelon CMS
6 Copyright 2008 Radosław Pietruszewski
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 version 2 as published by the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 ********************************************************************/
24 * Lib DB
25 * wersja 1.9.5
27 * Komunikacja z bazą danych
31 class DB
34 * public static int $queriesCounter
36 * licznik zapytań (liczba wykonanych zapytań)
38 public static $queriesCounter = 0;
41 * public static array $errorList
43 * lista (log) błędów. Przydatne w debbugowaniu
45 * $errorList = array(string $error[, string $error[, ...]])
46 * $error = treść błędu
49 public static $errorList = array();
52 * private static mysql_link $link
54 * resource bazy danych (zwracany przez mysql_connect)
57 private static $link;
60 * private static DB $instance
62 * Zawiera instancję tej klasy
64 * DEPRECATED - Niedługo to coś zostanie wywalone z kodu. Póki co tylko
65 * zakomentowane. (Akcja wywalanie singletona)
68 //private static $instance = NULL;
71 * private static string $prefix
73 * prefiks nazw tabel (np. tabela 'users' wraz z prefiksem 'wcms_' to 'wcms_users')
76 private static $prefix;
79 * public void connect(string $host, string $user, string $pass, string $name, string $prefix)
81 * Łączy z bazą danych
84 public function connect($host, $user, $pass, $name, $prefix)
86 self::$link = @mysql_connect($host, $user, $pass);
88 self::$prefix = $prefix;
90 if(!self::$link)
92 panic('Lib DB: 0');
95 $dbselect = @mysql_select_db($name);
97 if(!$dbselect)
99 panic('Lib DB: 1');
104 * public DBresult query(string $query)
106 * Zapytanie do bazy danych
108 * Zwraca FALSE w przypadku porażki
112 public static function query($query)
114 // jeśli jakaś nazwa tabeli w "panie kopniętym" zaczyna się od
115 // podwójnego podkreślnika, zamienia na prefix
117 $query = str_replace('`__', '`' . self::$prefix, $query);
119 // poniżej eksperymentalny kod. Może zostanie, może nie :P
121 $numargs = func_num_args();
122 $arg_list = func_get_args();
123 for($i = 1; $i < $numargs; $i++)
125 $query = str_replace('%' . $i, $arg_list[$i], $query);
128 // inkrementujemy licznik zapytań
130 self::$queriesCounter++;
132 $queryResult = mysql_query($query);
134 if($queryResult)
136 return new DBresult($queryResult);
138 else
140 self::$errorList[] = mysql_error();
141 return FALSE;
146 * public string[] errorList()
148 * Zwraca zawartość listy błędów
152 public static function errorList()
154 return self::$errorList;
158 * public string lastError()
160 * Zwraca ostatni napotkany błąd
164 public static function lastError()
166 return end(self::$errorList);
170 * public int queries()
172 * Zwraca liczbę wykonanych zapytań
176 public static function queries()
178 return self::$queriesCounter;
182 * public static DB Instance()
184 * Singleton... (zwraca instancję tej klasy)
186 * DEPRECATED - Niedługo to coś zostanie wywalone z kodu. Póki co tylko
187 * zakomentowane. (Akcja wywalanie singletona)
190 public static function Instance()
192 if(!self::$instance instanceof self)
193 self::$instance = new self;
194 return self::$instance;
199 ############
200 ############
201 ############
203 class DBresult
205 public $res; // resource zwrócony przez DB::query
208 * public void DBresult(resource $res)
210 * Ustawia $this->res
213 public function DBresult($res)
215 $this->res = $res;
219 * public int num_rows()
221 * Zwraca ilość znalezionych wyników
225 public function num_rows()
227 return mysql_num_rows($this->res);
231 * public object to_obj()
233 * Zwraca dane w postaci obiektu
237 public function to_obj()
239 return mysql_fetch_object($this->res);
243 * public array to_array()
245 * Zwraca dane w postaci tablicy
249 public function to_array()
251 return mysql_fetch_array($this->res);