Changeset d2faced for src/webservice/htdocs/SiWeather
- Timestamp:
- Sep 23, 2015, 7:43:40 PM (11 years ago)
- Branches:
- master
- Children:
- 363c1af
- Parents:
- f8c242e
- Location:
- src/webservice/htdocs/SiWeather/Webservice
- Files:
-
- 2 edited
-
DatabaseHandler.php (modified) (3 diffs)
-
Setup.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/webservice/htdocs/SiWeather/Webservice/DatabaseHandler.php
rf8c242e rd2faced 72 72 } 73 73 74 public function connect() 74 /** 75 * @brief Connects to the database and does some checks. 76 * 77 * @note This function should only called by the setup with @p ignoreVersion = true! 78 * Otherwise the database version is not checked! Data loss may be the consequence! 79 */ 80 public function connect($ignoreVersion = false) 75 81 { 76 82 if(!is_null($this->_database)){ … … 99 105 } 100 106 $this->_database = $db; 107 108 // check schema version 109 if(!$this->checkVersions() && !$ignoreVersion) 110 return false; 111 101 112 return true; 102 113 } … … 113 124 $this->setLastErrorMessage("Failed to close the database: "+ $this->_database->lastErrorMsg()); 114 125 return false; 126 } 127 128 public function databaseTableExists($table){ 129 if(!$this->_database || !is_object($this->_database)){ 130 return false; 131 } 132 $statement = $this->prepare("SELECT COUNT(*) FROM sqlite_master WHERE type=\"table\" AND name=:name"); 133 if(!$statement){ 134 $this->setLastErrorMessage(\Translator::swGettext("Could not prepare query to check if table %s exists", $table)); 135 return false; 136 } 137 $statement->bindValue(":name", $table); 138 $result = $statement->execute(); 139 $row = $result->fetchArray(); 140 if(!$row || !is_array($row) || count($row) < 1 || $row[0] != 1) 141 return false; 142 return true; 143 } 144 145 public function checkVersions(){ 146 if(is_null($this->_database)){ 147 $this->setLastErrorMessage("No database available to check versions"); 148 return false; 149 } 150 $table = "schema"; 151 if(!$this->databaseTableExists($table)){ 152 $this->setLastErrorMessage("Database table with schema versions does not exists!"); 153 return false; 154 } 155 156 // check schema version 157 $neededVersion = $this->_config->getConfig("databaseVersionCore"); 158 $currentVersion = $this->exec("SELECT version FROM ". $table ." WHERE name='core'"); 159 if(empty($neededVersion) || !is_numeric($neededVersion) || !$currentVersion || $currentVersion != $neededVersion){ 160 $this->setLastErrorMessage("Found wrong database core version. Expected ". $neededVersion ." got ". $currentVersion); 161 return false; 162 } 163 return true; 115 164 } 116 165 -
src/webservice/htdocs/SiWeather/Webservice/Setup.php
rf8c242e rd2faced 182 182 return false; 183 183 } 184 if(!$this->_databaseHandler->connect( )){184 if(!$this->_databaseHandler->connect(true)){ 185 185 $errorMessage = \Translator::swGettext("Could not connect to the database (%s)", $this->_databaseHandler->lastErrorMessage()); 186 186 return false; 187 } 188 // check version, if an schema already exists 189 if($this->_databaseHandler->databaseTableExists("schema")){ 190 if(!$this->_databaseHandler->checkVersions()){ 191 $errorMessage = \Translator::swGettext("Database schema versions check failed (%s)", $this->_databaseHandler->lastErrorMessage()); 192 return false; 193 } 187 194 } 188 195 189 196 // prepare Spatalite 190 if(!$this-> databaseTableExists("spatial_ref_sys")){197 if(!$this->_databaseHandler->databaseTableExists("spatial_ref_sys")){ 191 198 if(!$this->_databaseHandler->exec("BEGIN")){ 192 199 $errorMessage = _("Could not start transaction to init Spatialite"); … … 202 209 } 203 210 } 204 211 205 212 // // prepare countries table 206 213 // $table = "countries"; … … 231 238 // } 232 239 240 // prepare schema table 241 $table = "schema"; 242 if(!$this->_databaseHandler->databaseTableExists($table)){ 243 $sql = "CREATE TABLE ". $table ." ("; 244 $sql .= " id INTEGER NOT NULL PRIMARY KEY,"; 245 $sql .= " name TEXT NOT NULL,"; 246 $sql .= " version INTEGER NOT NULL"; 247 $sql .= ")"; 248 if(!$this->databaseTableCreate($table, $sql, $errorMessage)){ 249 return false; 250 } 251 $sql = "INSERT INTO ". $table ." (name, version) VALUES(:name, :version)"; 252 $sqlStatement = $this->_databaseHandler->prepare($sql); 253 $sqlStatement->clear(); 254 $sqlStatement->bindValue(":name", "core"); 255 $sqlStatement->bindValue(":version", $this->_webserviceConfig->getConfig("databaseVersionCore")); 256 if(!$sqlStatement->execute()){ 257 $errorMessage = Translation::swGettext("Could not add schema version %s to the database", "core"); 258 error_log($errorMessage); 259 return false; 260 } 261 $sqlStatement->reset(); 262 } 263 233 264 // prepare cities table 234 265 $table = "cities"; 235 if(!$this-> databaseTableExists($table)){266 if(!$this->_databaseHandler->databaseTableExists($table)){ 236 267 $sql = "CREATE TABLE ". $table ." ("; 237 268 $sql .= " id INTEGER NOT NULL PRIMARY KEY,"; … … 253 284 // prepare status table 254 285 $table = "status"; 255 if(!$this-> databaseTableExists($table)){286 if(!$this->_databaseHandler->databaseTableExists($table)){ 256 287 $sql = "CREATE TABLE ". $table ." ("; 257 288 $sql .= " id INTEGER NOT NULL PRIMARY KEY,"; … … 278 309 // prepare stations table 279 310 $table = "stations"; 280 if(!$this-> databaseTableExists($table)){311 if(!$this->_databaseHandler->databaseTableExists($table)){ 281 312 $sql = "CREATE TABLE ". $table ." ("; 282 313 $sql .= " id INTEGER NOT NULL PRIMARY KEY,"; … … 287 318 $sql .= " type TEXT NOT NULL,"; 288 319 $sql .= " typeUrl TEXT,"; 289 $sql .= " status INTEGER NOT NULL,"; 320 $sql .= " creationDate TIMESTAMP DEFAULT (strftime('%s', 'now')),"; 321 // $sql .= " creationDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,"; 322 $sql .= " status INTEGER DEFAULT 1,"; 290 323 $sql .= " FOREIGN KEY(city) REFERENCES cities(id)"; 291 324 $sql .= " FOREIGN KEY(status) REFERENCES status(id)"; … … 399 432 } 400 433 401 private function databaseTableExists($table){402 if(!$this->_databaseHandler || !is_object($this->_databaseHandler)){403 return false;404 }405 $statement = $this->_databaseHandler->prepare("SELECT COUNT(*) FROM sqlite_master WHERE type=\"table\" AND name=:name");406 if(!$statement){407 $errorMessage = \Translator::swGettext("Could not prepare query to check if table %s exists", $table);408 return false;409 }410 $statement->bindValue(":name", $table);411 $result = $statement->execute();412 $row = $result->fetchArray();413 if(!$row || !is_array($row) || count($row) < 1 || $row[0] != 1)414 return false;415 return true;416 }417 418 434 private function databaseTableCreate($table, $sqlStatement, &$errorMessage){ 419 435 if(!$this->_databaseHandler || !is_object($this->_databaseHandler)){
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/siweather/chrome/site/your_project_logo.png)