close Warning: AdminModule failed with TracError: Unable to instantiate component <class 'trac.ticket.admin.VersionAdminPanel'> (super(type, obj): obj (type VersionAdminPanel) is not an instance or subtype of type (Markup).)

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

Last change on this file since fb602c6 was d55ec21, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Improved testing execution of webservice related tests (they use the built in php webserver to create a test webservice instance)
  • Property mode set to 100644
File size: 4.8 KB
Line 
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
21namespace SiWeather\Webservice;
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 {
32 $this->_config = new WebserviceConfig();
33
34 $this->_lastErrorMessage = "";
35 $this->_database = null;
36 if(!$this->_config->dataDirectory()){
37 $this->setLastErrorMessage("Could not connect to the database: No data directory set!");
38 $this->databaseFile = null;
39 }
40 else{
41 $this->_databaseFile = $this->_config->dataDirectory() ."/siweather.sqlite";
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
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
74 public function connect()
75 {
76 if(!is_null($this->_database)){
77 return true;
78 }
79 if(is_null($this->_databaseFile) || empty($this->_databaseFile)){
80 $this->setLastErrorMessage("Could not connect to the database: No database file given");
81 return false;
82 }
83 try {
84 $db = new \SQLite3($this->_databaseFile, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
85 }
86 catch(\Exception $exception){
87 $this->setLastErrorMessage("Could not connect to the database: "+ $exception->getMessage());
88 return false;
89 }
90 // loading SpatiaLite as an extension
91 $extension = $this->_config->getConfig("spatialiteExtension");
92 if(empty($extension)){
93 $this->setLastErrorMessage("No spatialiteExtension set!");
94 return false;
95 }
96 if(!$db->loadExtension($extension)){
97 $this->setLastErrorMessage("Could not load SpatialLite extension: ". $db->lastErrorMsg());
98 return false;
99 }
100 $this->_database = $db;
101 return true;
102 }
103
104 public function close()
105 {
106 if(is_null($this->_database)){
107 return true;
108 }
109 if($this->_database->close()){
110 $this->_database = null;
111 return true;
112 }
113 $this->setLastErrorMessage("Failed to close the database: "+ $this->_database->lastErrorMsg());
114 return false;
115 }
116
117 public function exec($query)
118 {
119 if(is_null($this->_database)){
120 return false;
121 }
122 if(!$this->connect()){
123 return false;
124 }
125 $result = $this->_database->exec($query);
126 if(!$result)
127 $this->setLastErrorMessage("Could not execute databae query \"". $query ."\": ". $this->_database->lastErrorMsg());
128 return $result;
129 }
130
131 public function prepare($query)
132 {
133 if(is_null($this->_database)){
134 return false;
135 }
136 if(!$this->connect()){
137 return false;
138 }
139 $result = $this->_database->prepare($query);
140 if(!$result){
141 error_log("logging");
142 $this->setLastErrorMessage("Could not prepare database query \"". $query ."\": ". $this->_database->lastErrorMsg());
143 }
144 return $result;
145 }
146
147 public function versionSpatialite(){
148 if(is_null($this->_database)){
149 $this->setLastErrorMessage("No database connection");
150 return 0;
151 }
152 if(!$this->connect()){
153 return 0;
154 }
155 $result = $this->_database->query("SELECT spatialite_version()");
156 while($row = $result->fetchArray()){
157 return $row[0];
158 }
159 return 0;
160 }
161
162 public function versionSqlite(){
163 if(is_null($this->_database)){
164 $this->setLastErrorMessage("No database connection");
165 return 0;
166 }
167 if(!$this->connect()){
168 return 0;
169 }
170 $result = $this->_database->query("SELECT sqlite_version()");
171 while($row = $result->fetchArray()){
172 return $row[0];
173 }
174 return 0;
175 }
176
177 private function setLastErrorMessage($message){
178 if(!empty($message))
179 error_log($message);
180 $this->_lastErrorMessage = $message;
181 }
182
183 private $_database;
184 private $_databaseFile;
185 private $_lastErrorMessage;
186 private $_config;
187}
188
189?>
Note: See TracBrowser for help on using the repository browser.