2 /***************************************************************************
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
11 * Copyright (C) 2009 Jonas Häggqvist
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
25 * Simple DB class using sqlite and a bunch of assumptions.
30 public function __construct($file) {
32 $this->dh
= @sqlite_open
($file, 0666, $err);
33 if ($this->dh
=== false) {
37 $res = $this->query("SELECT COUNT(*) as count FROM sqlite_master WHERE type='table'");
38 if ($res->next('count') === "0") {
39 $checkwps_table = <<<END
40 CREATE TABLE checkwps (
62 emailverification TEXT,
69 name TEXT PRIMARY KEY,
73 $target_table = <<<END
74 CREATE TABLE targets (
75 shortname TEXT PRIMARY KEY,
91 $this->query($target_table);
92 $this->query($checkwps_table);
93 $this->query($theme_table);
94 $this->query($admin_table);
95 $this->query($log_table);
100 public function query($sql) {
101 $res = @sqlite_query
(
107 if ($res === false) {
108 $this->error($err, $sql);
111 return new result($res, $this->dh
);
115 private function error($err, $sql = "") {
117 * Sometimes the error is empty, in which case the explanation can be
121 $code = sqlite_last_error($this->dh
);
122 $err = sprintf("%s (%d)",
123 sqlite_error_string($code),
127 $msg = sprintf("<b>DB Error:</b> %s", $err);
129 $msg .= sprintf("<br />\n<b>SQL:</b> %s", $sql);
131 /* xxx: We'd probably want to log this rather than output it */
135 public static function quote($input) {
136 return sqlite_escape_string($input);
141 * Simple OO wrapper around the regular sqlite functions. Newer PHP versions
142 * have something like this, but use this to avoid depending on that.
148 public function __construct($rh, &$dh) {
153 public function numrows() {
154 return sqlite_num_rows($this->rh
);
157 public function next($field = false) {
158 $row = sqlite_fetch_array($this->rh
);
159 if ($field !== false && isset($row[$field])) {
167 public function insertid() {
168 return sqlite_last_insert_rowid($this->dh
);
171 public function rowsaffected() {
172 return sqlite_changes($this->dh
);