| [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] | 21 | namespace 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 | */
|
|---|
| 29 | class 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 |
|
|---|
| [92c9c7c] | 74 | public function connect()
|
|---|
| 75 | {
|
|---|
| 76 | if(!is_null($this->_database)){
|
|---|
| 77 | return true;
|
|---|
| 78 | }
|
|---|
| [b38cc03] | 79 | if(is_null($this->_databaseFile) || empty($this->_databaseFile)){
|
|---|
| [a0f5395] | 80 | $this->setLastErrorMessage("Could not connect to the database: No database file given");
|
|---|
| [92c9c7c] | 81 | return false;
|
|---|
| 82 | }
|
|---|
| 83 | try {
|
|---|
| [b38cc03] | 84 | $db = new \SQLite3($this->_databaseFile, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
|
|---|
| [92c9c7c] | 85 | }
|
|---|
| [b38cc03] | 86 | catch(\Exception $exception){
|
|---|
| [a0f5395] | 87 | $this->setLastErrorMessage("Could not connect to the database: "+ $exception->getMessage());
|
|---|
| [92c9c7c] | 88 | return false;
|
|---|
| 89 | }
|
|---|
| [b38cc03] | 90 | // loading SpatiaLite as an extension
|
|---|
| [8c356c1] | 91 | $extension = $this->_config->getConfig("spatialiteExtension");
|
|---|
| 92 | if(empty($extension)){
|
|---|
| [a0f5395] | 93 | $this->setLastErrorMessage("No spatialiteExtension set!");
|
|---|
| [8c356c1] | 94 | return false;
|
|---|
| 95 | }
|
|---|
| 96 | if(!$db->loadExtension($extension)){
|
|---|
| [a0f5395] | 97 | $this->setLastErrorMessage("Could not load SpatialLite extension: ". $db->lastErrorMsg());
|
|---|
| [92c9c7c] | 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 | }
|
|---|
| [a0f5395] | 113 | $this->setLastErrorMessage("Failed to close the database: "+ $this->_database->lastErrorMsg());
|
|---|
| [92c9c7c] | 114 | return false;
|
|---|
| 115 | }
|
|---|
| [8c356c1] | 116 |
|
|---|
| 117 | public function exec($query)
|
|---|
| 118 | {
|
|---|
| [b38cc03] | 119 | if(is_null($this->_database)){
|
|---|
| 120 | return false;
|
|---|
| 121 | }
|
|---|
| [8c356c1] | 122 | if(!$this->connect()){
|
|---|
| 123 | return false;
|
|---|
| 124 | }
|
|---|
| [a0f5395] | 125 | $result = $this->_database->exec($query);
|
|---|
| 126 | if(!$result)
|
|---|
| 127 | $this->setLastErrorMessage("Could not execute databae query \"". $query ."\": ". $this->_database->lastErrorMsg());
|
|---|
| 128 | return $result;
|
|---|
| [8c356c1] | 129 | }
|
|---|
| 130 |
|
|---|
| 131 | public function prepare($query)
|
|---|
| 132 | {
|
|---|
| [b38cc03] | 133 | if(is_null($this->_database)){
|
|---|
| 134 | return false;
|
|---|
| 135 | }
|
|---|
| [8c356c1] | 136 | if(!$this->connect()){
|
|---|
| 137 | return false;
|
|---|
| 138 | }
|
|---|
| [a0f5395] | 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;
|
|---|
| [8c356c1] | 145 | }
|
|---|
| 146 |
|
|---|
| 147 | public function versionSpatialite(){
|
|---|
| 148 | if(is_null($this->_database)){
|
|---|
| [a0f5395] | 149 | $this->setLastErrorMessage("Unknown (No database connection)");
|
|---|
| [8c356c1] | 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)){
|
|---|
| [a0f5395] | 164 | $this->setLastErrorMessage("Unknown (No database connection)");
|
|---|
| [8c356c1] | 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 | }
|
|---|
| [a0f5395] | 176 |
|
|---|
| 177 | private function setLastErrorMessage($message){
|
|---|
| 178 | if(!empty($message))
|
|---|
| 179 | error_log($message);
|
|---|
| 180 | $this->_lastErrorMessage = $message;
|
|---|
| 181 | }
|
|---|
| [8c356c1] | 182 |
|
|---|
| 183 | private $_database;
|
|---|
| 184 | private $_databaseFile;
|
|---|
| 185 | private $_lastErrorMessage;
|
|---|
| 186 | private $_config;
|
|---|
| [92c9c7c] | 187 | }
|
|---|
| 188 |
|
|---|
| [8c356c1] | 189 | ?>
|
|---|