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

Last change on this file since b858e6f was b38cc03, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Translated more messages
    • Stripped out test externals, because they are not needed
    • Improved Setup class, to create all needed database tables
    • Smaller general improvements on the test classes
    • Extended the SetupTest extent
  • Property mode set to 100644
File size: 4.3 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->_lastErrorMessage = "No data directory set!";
38 error_log("Could not connect to the database: "+ $this->lastErrorMessage());
39 $this->databaseFile = null;
40 }
41 else{
42 $this->_databaseFile = $this->_config->dataDirectory() ."/siweather.sqlite";
43 }
44 }
45
46 function __destruct()
47 {
48 if(!is_null($this->_database)){
49 $this->close();
50 }
51 }
52
53 public function databaseFile()
54 {
55 return $this->_databaseFile;
56 }
57
58 public function lastErrorMessage()
59 {
60 return $this->_lastErrorMessage;
61 }
62
63 public function connect()
64 {
65 if(!is_null($this->_database)){
66 return true;
67 }
68 if(is_null($this->_databaseFile) || empty($this->_databaseFile)){
69 $this->_lastErrorMessage = "No database file given";
70 error_log("Could not connect to the database: "+ $this->lastErrorMessage());
71 return false;
72 }
73 try {
74 $db = new \SQLite3($this->_databaseFile, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
75 }
76 catch(\Exception $exception){
77 $this->_lastErrorMessage = $exception->getMessage();
78 error_log("Could not connect to the database: "+ $this->lastErrorMessage());
79 return false;
80 }
81 // loading SpatiaLite as an extension
82 $extension = $this->_config->getConfig("spatialiteExtension");
83 if(empty($extension)){
84 $this->_lastErrorMessage("No spatialiteExtension set!");
85 error_log($this->lastErrorMessage());
86 return false;
87 }
88 if(!$db->loadExtension($extension)){
89 $this->_lastErrorMessage = "Could not load SpatialLite extension: ". $db->lastErrorMsg();
90 error_log($this->lastErrorMessage());
91 return false;
92 }
93 $this->_database = $db;
94 return true;
95 }
96
97 public function close()
98 {
99 if(is_null($this->_database)){
100 return true;
101 }
102 if($this->_database->close()){
103 $this->_database = null;
104 return true;
105 }
106 $this->_lastErrorMessage = $this->_database->lastErrorMsg();
107 error_log("Failed to close the database: "+ $this->lastErrorMessage());
108 return false;
109 }
110
111 public function exec($query)
112 {
113 if(is_null($this->_database)){
114 return false;
115 }
116 if(!$this->connect()){
117 return false;
118 }
119 return $this->_database->exec($query);
120 }
121
122 public function prepare($query)
123 {
124 if(is_null($this->_database)){
125 return false;
126 }
127 if(!$this->connect()){
128 return false;
129 }
130 return $this->_database->prepare($query);
131 }
132
133 public function versionSpatialite(){
134 if(is_null($this->_database)){
135 $this->_lastErrorMessage = "Unknown (No database connection)";
136 return 0;
137 }
138 if(!$this->connect()){
139 return 0;
140 }
141 $result = $this->_database->query("SELECT spatialite_version()");
142 while($row = $result->fetchArray()){
143 return $row[0];
144 }
145 return 0;
146 }
147
148 public function versionSqlite(){
149 if(is_null($this->_database)){
150 $this->_lastErrorMessage = "Unknown (No database connection)";
151 return 0;
152 }
153 if(!$this->connect()){
154 return 0;
155 }
156 $result = $this->_database->query("SELECT sqlite_version()");
157 while($row = $result->fetchArray()){
158 return $row[0];
159 }
160 return 0;
161 }
162
163 private $_database;
164 private $_databaseFile;
165 private $_lastErrorMessage;
166 private $_config;
167}
168
169?>
Note: See TracBrowser for help on using the repository browser.