Changeset a0f5395


Ignore:
Timestamp:
Aug 31, 2015, 11:01:21 PM (11 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
master
Children:
b5ebb8d
Parents:
519df6e
Message:

Improved error loggin within DatabaseHandler

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/webservice/htdocs/SiWeather/Webservice/DatabaseHandler.php

    r519df6e ra0f5395  
    3535                $this->_database = null;
    3636                if(!$this->_config->dataDirectory()){
    37                         $this->_lastErrorMessage = "No data directory set!";
    38                         error_log("Could not connect to the database: "+ $this->lastErrorMessage());
     37                        $this->setLastErrorMessage("Could not connect to the database: No data directory set!");
    3938                        $this->databaseFile = null;
    4039                }
    … …  
    6160        }
    6261
     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
    6374        public function connect()
    6475        {
    … …  
    6778                }
    6879                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());
     80                        $this->setLastErrorMessage("Could not connect to the database: No database file given");
    7181                        return false;
    7282                }
    … …  
    7585                }
    7686                catch(\Exception $exception){
    77                         $this->_lastErrorMessage = $exception->getMessage();
    78                         error_log("Could not connect to the database: "+ $this->lastErrorMessage());
     87                        $this->setLastErrorMessage("Could not connect to the database: "+ $exception->getMessage());
    7988                        return false;
    8089                }
    … …  
    8291                $extension = $this->_config->getConfig("spatialiteExtension");
    8392                if(empty($extension)){
    84                         $this->_lastErrorMessage("No spatialiteExtension set!");
    85                         error_log($this->lastErrorMessage());
     93                        $this->setLastErrorMessage("No spatialiteExtension set!");
    8694                        return false;
    8795                }
    8896                if(!$db->loadExtension($extension)){
    89                         $this->_lastErrorMessage = "Could not load SpatialLite extension: ". $db->lastErrorMsg();
    90                         error_log($this->lastErrorMessage());
     97                        $this->setLastErrorMessage("Could not load SpatialLite extension: ". $db->lastErrorMsg());
    9198                        return false;
    9299                }
    … …  
    104111                        return true;
    105112                }
    106                 $this->_lastErrorMessage = $this->_database->lastErrorMsg();
    107                 error_log("Failed to close the database: "+ $this->lastErrorMessage());
     113                $this->setLastErrorMessage("Failed to close the database: "+ $this->_database->lastErrorMsg());
    108114                return false;
    109115        }
    … …  
    117123                        return false;
    118124                }
    119                 return $this->_database->exec($query);
     125                $result = $this->_database->exec($query);
     126                if(!$result)
     127                        $this->setLastErrorMessage("Could not execute databae query \"". $query ."\": ". $this->_database->lastErrorMsg());
     128                return $result;
    120129        }
    121130       
    … …  
    128137                        return false;
    129138                }
    130                 return $this->_database->prepare($query);
     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;
    131145        }
    132146       
    133147        public function versionSpatialite(){
    134148                if(is_null($this->_database)){
    135                         $this->_lastErrorMessage = "Unknown (No database connection)";
     149                        $this->setLastErrorMessage("Unknown (No database connection)");
    136150                        return 0;
    137151                }
    … …  
    148162        public function versionSqlite(){
    149163                if(is_null($this->_database)){
    150                         $this->_lastErrorMessage = "Unknown (No database connection)";
     164                        $this->setLastErrorMessage("Unknown (No database connection)");
    151165                        return 0;
    152166                }
    … …  
    160174                return 0;
    161175        }
     176
     177        private function setLastErrorMessage($message){
     178                if(!empty($message))
     179                        error_log($message);
     180                $this->_lastErrorMessage = $message;
     181        }
    162182       
    163183        private $_database;
Note: See TracChangeset for help on using the changeset viewer.