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

Last change on this file since 8c356c1 was 8c356c1, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Finished setup
    • Added tests for setup
    • Added translation to setup
  • Property mode set to 100644
File size: 4.1 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)){
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);
75 }
76 catch(PDOException $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(!$this->connect()){
114 return false;
115 }
116 return $this->_database->exec($query);
117 }
118
119 public function prepare($query)
120 {
121 if(!$this->connect()){
122 return false;
123 }
124 return $this->_database->prepare($query);
125 }
126
127 public function versionSpatialite(){
128 if(is_null($this->_database)){
129 $this->_lastErrorMessage("Unknown (No database connection)");
130 return 0;
131 }
132 if(!$this->connect()){
133 return 0;
134 }
135 $result = $this->_database->query("SELECT spatialite_version()");
136 while($row = $result->fetchArray()){
137 return $row[0];
138 }
139 return 0;
140 }
141
142 public function versionSqlite(){
143 if(is_null($this->_database)){
144 $this->_lastErrorMessage("Unknown (No database connection)");
145 return 0;
146 }
147 if(!$this->connect()){
148 return 0;
149 }
150 $result = $this->_database->query("SELECT sqlite_version()");
151 while($row = $result->fetchArray()){
152 return $row[0];
153 }
154 return 0;
155 }
156
157 private $_database;
158 private $_databaseFile;
159 private $_lastErrorMessage;
160 private $_config;
161}
162
163?>
Note: See TracBrowser for help on using the repository browser.