source: src/webservice/htdocs/inc/DatabaseHandler.php@ 8f38d1e

Last change on this file since 8f38d1e was 92c9c7c, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Improved htaccess rewrite rule (but not working right now)
    • started to implement database connection
    • Improved host url handling
    • Improved external handling

Signed-off-by: Joachim Langenbach <joachim.langenbach@…>

  • Property mode set to 100644
File size: 3.0 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
21require_once("config/config.php");
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 private $_database;
31 private $_databaseFile;
32 private $_lastErrorMessage;
33
34 function __construct()
35 {
36 $this->_lastErrorMessage = "";
37 $this->_database = null;
38 if(!getConfig("dataDirectory")){
39 $this->_lastErrorMessage = "No data directory set!";
40 error_log("Could not connect to the database: "+ $this->lastErrorMessage());
41 $this->databaseFile = null;
42 }
43 else{
44 $this->_databaseFile = getConfig("dataDirectory") ."/siweather.sqlite";
45 }
46 }
47
48 function __destruct()
49 {
50 if(!is_null($this->_database)){
51 $this->close();
52 }
53 }
54
55 public function databaseFile()
56 {
57 return $this->_databaseFile;
58 }
59
60 public function lastErrorMessage()
61 {
62 return $this->_lastErrorMessage;
63 }
64
65 public function connect()
66 {
67 if(!is_null($this->_database)){
68 echo "database is not null";
69 return true;
70 }
71 if(is_null($this->_databaseFile)){
72 $this->_lastErrorMessage = "No database file given";
73 error_log("Could not connect to the database: "+ $this->lastErrorMessage());
74 return false;
75 }
76 try {
77 $db = new SQLite3($this->_databaseFile);
78 }
79 catch(PDOException $exception){
80 $this->_lastErrorMessage = $exception->getMessage();
81 error_log("Could not connect to the database: "+ $this->lastErrorMessage());
82 return false;
83 }
84 # loading SpatiaLite as an extension
85 if(!$db->loadExtension("libspatialite.so.7")){
86 $this->_lastErrorMessage = "Could not load SpatialLite extension: ". $db->lastErrorMsg();
87 error_log($this->lastErrorMessage());
88 return false;
89 }
90 $this->_database = $db;
91 return true;
92 }
93
94 public function close()
95 {
96 if(is_null($this->_database)){
97 return true;
98 }
99 if($this->_database->close()){
100 $this->_database = null;
101 return true;
102 }
103 $this->_lastErrorMessage = $this->_database->lastErrorMsg();
104 error_log("Failed to close the database: "+ $this->lastErrorMessage());
105 return false;
106 }
107}
108
109?>
Note: See TracBrowser for help on using the repository browser.