source: src/webservice/htdocs/SiWeather/Webservice/DatabaseHandler.php@ 4b09d68

Last change on this file since 4b09d68 was d2faced, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Introduced database schema version
    • Added creationDate field to the stations table
  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[92c9c7c]1<?php
2/*
3 Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 MA 02110-1301 USA.
19*/
20
[8c356c1]21namespace SiWeather\Webservice;
[92c9c7c]22
23/**
24 * @brief Manages the database connection for SiWeather webservice
25 * @note This class needs to load the SpatialLite SQLite extension.
26 * Therefore the sqlite3.extension_dir in php.ini must be set
27 * to a path, where this extension could be found!
28 */
29class DatabaseHandler {
30 function __construct()
31 {
[8c356c1]32 $this->_config = new WebserviceConfig();
33
[92c9c7c]34 $this->_lastErrorMessage = "";
35 $this->_database = null;
[8c356c1]36 if(!$this->_config->dataDirectory()){
[a0f5395]37 $this->setLastErrorMessage("Could not connect to the database: No data directory set!");
[92c9c7c]38 $this->databaseFile = null;
39 }
40 else{
[8c356c1]41 $this->_databaseFile = $this->_config->dataDirectory() ."/siweather.sqlite";
[92c9c7c]42 }
43 }
44
45 function __destruct()
46 {
47 if(!is_null($this->_database)){
48 $this->close();
49 }
50 }
51
52 public function databaseFile()
53 {
54 return $this->_databaseFile;
55 }
56
57 public function lastErrorMessage()
58 {
59 return $this->_lastErrorMessage;
60 }
61
[a0f5395]62 public function lastDatabaseErrorMessage(){
63 if(is_null($this->_database))
64 return false;
65 return $this->_database->lastErrorMsg();
66 }
67
68 public function lastInsertRowId(){
69 if(is_null($this->_database))
70 return -1;
71 return $this->_database->lastInsertRowID();
72 }
73
[d2faced]74 /**
75 * @brief Connects to the database and does some checks.
76 *
77 * @note This function should only called by the setup with @p ignoreVersion = true!
78 * Otherwise the database version is not checked! Data loss may be the consequence!
79 */
80 public function connect($ignoreVersion = false)
[92c9c7c]81 {
82 if(!is_null($this->_database)){
83 return true;
84 }
[b38cc03]85 if(is_null($this->_databaseFile) || empty($this->_databaseFile)){
[a0f5395]86 $this->setLastErrorMessage("Could not connect to the database: No database file given");
[92c9c7c]87 return false;
88 }
89 try {
[b38cc03]90 $db = new \SQLite3($this->_databaseFile, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
[92c9c7c]91 }
[b38cc03]92 catch(\Exception $exception){
[a0f5395]93 $this->setLastErrorMessage("Could not connect to the database: "+ $exception->getMessage());
[92c9c7c]94 return false;
95 }
[b38cc03]96 // loading SpatiaLite as an extension
[8c356c1]97 $extension = $this->_config->getConfig("spatialiteExtension");
98 if(empty($extension)){
[a0f5395]99 $this->setLastErrorMessage("No spatialiteExtension set!");
[8c356c1]100 return false;
101 }
102 if(!$db->loadExtension($extension)){
[a0f5395]103 $this->setLastErrorMessage("Could not load SpatialLite extension: ". $db->lastErrorMsg());
[92c9c7c]104 return false;
105 }
106 $this->_database = $db;
[d2faced]107
108 // check schema version
109 if(!$this->checkVersions() && !$ignoreVersion)
110 return false;
111
[92c9c7c]112 return true;
113 }
114
115 public function close()
116 {
117 if(is_null($this->_database)){
118 return true;
119 }
120 if($this->_database->close()){
121 $this->_database = null;
122 return true;
123 }
[a0f5395]124 $this->setLastErrorMessage("Failed to close the database: "+ $this->_database->lastErrorMsg());
[92c9c7c]125 return false;
126 }
[8c356c1]127
[d2faced]128 public function databaseTableExists($table){
129 if(!$this->_database || !is_object($this->_database)){
130 return false;
131 }
132 $statement = $this->prepare("SELECT COUNT(*) FROM sqlite_master WHERE type=\"table\" AND name=:name");
133 if(!$statement){
134 $this->setLastErrorMessage(\Translator::swGettext("Could not prepare query to check if table %s exists", $table));
135 return false;
136 }
137 $statement->bindValue(":name", $table);
138 $result = $statement->execute();
139 $row = $result->fetchArray();
140 if(!$row || !is_array($row) || count($row) < 1 || $row[0] != 1)
141 return false;
142 return true;
143 }
144
145 public function checkVersions(){
146 if(is_null($this->_database)){
147 $this->setLastErrorMessage("No database available to check versions");
148 return false;
149 }
150 $table = "schema";
151 if(!$this->databaseTableExists($table)){
152 $this->setLastErrorMessage("Database table with schema versions does not exists!");
153 return false;
154 }
155
156 // check schema version
157 $neededVersion = $this->_config->getConfig("databaseVersionCore");
158 $currentVersion = $this->exec("SELECT version FROM ". $table ." WHERE name='core'");
159 if(empty($neededVersion) || !is_numeric($neededVersion) || !$currentVersion || $currentVersion != $neededVersion){
160 $this->setLastErrorMessage("Found wrong database core version. Expected ". $neededVersion ." got ". $currentVersion);
161 return false;
162 }
163 return true;
164 }
165
[8c356c1]166 public function exec($query)
167 {
[b38cc03]168 if(is_null($this->_database)){
169 return false;
170 }
[8c356c1]171 if(!$this->connect()){
172 return false;
173 }
[a0f5395]174 $result = $this->_database->exec($query);
175 if(!$result)
176 $this->setLastErrorMessage("Could not execute databae query \"". $query ."\": ". $this->_database->lastErrorMsg());
177 return $result;
[8c356c1]178 }
179
180 public function prepare($query)
181 {
[b38cc03]182 if(is_null($this->_database)){
183 return false;
184 }
[8c356c1]185 if(!$this->connect()){
186 return false;
187 }
[a0f5395]188 $result = $this->_database->prepare($query);
189 if(!$result){
190 error_log("logging");
191 $this->setLastErrorMessage("Could not prepare database query \"". $query ."\": ". $this->_database->lastErrorMsg());
192 }
193 return $result;
[8c356c1]194 }
195
196 public function versionSpatialite(){
197 if(is_null($this->_database)){
[d55ec21]198 $this->setLastErrorMessage("No database connection");
[8c356c1]199 return 0;
200 }
201 if(!$this->connect()){
202 return 0;
203 }
204 $result = $this->_database->query("SELECT spatialite_version()");
205 while($row = $result->fetchArray()){
206 return $row[0];
207 }
208 return 0;
209 }
210
211 public function versionSqlite(){
212 if(is_null($this->_database)){
[d55ec21]213 $this->setLastErrorMessage("No database connection");
[8c356c1]214 return 0;
215 }
216 if(!$this->connect()){
217 return 0;
218 }
219 $result = $this->_database->query("SELECT sqlite_version()");
220 while($row = $result->fetchArray()){
221 return $row[0];
222 }
223 return 0;
224 }
[a0f5395]225
226 private function setLastErrorMessage($message){
227 if(!empty($message))
228 error_log($message);
229 $this->_lastErrorMessage = $message;
230 }
[8c356c1]231
232 private $_database;
233 private $_databaseFile;
234 private $_lastErrorMessage;
235 private $_config;
[92c9c7c]236}
237
[8c356c1]238?>
Note: See TracBrowser for help on using the repository browser.