CVS: phpweather/db pw_db_mysql.php,1.7,1.8
Brought to you by:
iridium
From: Etienne T. <eti...@us...> - 2003-09-30 19:48:07
|
Update of /cvsroot/phpweather/phpweather/db In directory sc8-pr-cvs1:/tmp/cvs-serv12758/db Modified Files: pw_db_mysql.php Log Message: Added archiving of METAR and TAF reports. The functions insert_metar, update_metar, insert_taf, update_taf have been modified to include the time of the report in the archive DB. Modified only pw_db_mysql to reflect these changes. The DBs pw_metars_arch and pw_tafs_arch must be created. You must set properties archive_metars and archive_tafs to true for the archive to happen transparently at each insert or update. Index: pw_db_mysql.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/db/pw_db_mysql.php,v retrieving revision 1.7 retrieving revision 1.8 diff -u -3 -r1.7 -r1.8 --- pw_db_mysql.php 8 Sep 2003 04:20:44 -0000 1.7 +++ pw_db_mysql.php 30 Sep 2003 19:48:00 -0000 1.8 @@ -183,14 +183,14 @@ * @access public * @see update_metar() */ - function insert_metar($icao, $metar, $timestamp) { + function insert_metar($icao, $metar, $timestamp, $time) { $this->query(sprintf('INSERT INTO %s SET icao = "%s", ' . - 'metar = "%s", timestamp = FROM_UNIXTIME(%d)', - $this->properties['db_metars'], $icao, - addslashes($metar), intval($timestamp))); + 'metar = "%s", timestamp = FROM_UNIXTIME(%d)', + $this->properties['db_metars'], $icao, + addslashes($metar), intval($timestamp))); + $this->insert_metar_arch($icao, $metar, $time); } - /** * Updates an existing METAR in the database. * @@ -200,14 +200,42 @@ * @access public * @see insert_metar() */ - function update_metar($icao, $metar, $timestamp) { + function update_metar($icao, $metar, $timestamp, $time) { $this->query(sprintf('UPDATE %s' . ' SET metar = "%s", timestamp = FROM_UNIXTIME(%d)' . ' WHERE icao = "%s"', $this->properties['db_metars'], addslashes($metar), intval($timestamp), $icao)); + $this->insert_metar_arch($icao, $metar, $time); } + /** + * Inserts an archive METAR into the database. + * + * @param string The ICAO of the station. + * @param string The raw METAR. + * @param integer The time of the report. + * @access public + */ + function insert_metar_arch($icao, $metar, $time) { + if(isset($this->properties['archive_metars']) && + $this->properties['archive_metars']==true) { + $this->query(sprintf('SHOW TABLES LIKE "%s"', + $this->properties['db_metars_arch'])); + if ($this->num_rows()==1) { + $this->query(sprintf('DELETE FROM %s WHERE icao = "%s" AND ' . + 'time = "%s"' , + $this->properties['db_metars_arch'], + $icao,$time)); + $this->query(sprintf('INSERT IGNORE INTO %s SET icao = "%s", ' . + 'time = "%s", ' . + 'metar = "%s"', + $this->properties['db_metars_arch'], $icao, + $time, + addslashes($metar))); + } + } + } /** * Gets a METAR form the database. @@ -232,11 +260,12 @@ * @access public * @see update_taf() */ - function insert_taf($icao, $taf, $timestamp) { + function insert_taf($icao, $taf, $timestamp, $time) { $this->query(sprintf('INSERT INTO %s SET icao = "%s", ' . 'taf = "%s", timestamp = FROM_UNIXTIME(%d)', $this->properties['db_tafs'], $icao, addslashes($taf), intval($timestamp))); + $this->insert_taf_arch($icao, $taf, $time); } /** @@ -254,8 +283,37 @@ ' WHERE icao = "%s"', $this->properties['db_tafs'], addslashes($taf), intval($timestamp), $icao)); + $this->insert_taf_arch($icao, $taf, $time); } + /** + * Inserts an archive TAF into the database. + * + * @param string The ICAO of the station. + * @param string The raw TAF. + * @param integer The time of the report. + * @access public + */ + function insert_taf_arch($icao, $taf, $time) { + if(isset($this->properties['archive_tafs']) && + $this->properties['archive_tafs']==true) { + $this->query(sprintf('SHOW TABLES LIKE "%s"', + $this->properties['db_tafs_arch'])); + if ($this->num_rows()==1) { + $this->query(sprintf('DELETE FROM %s WHERE icao = "%s" AND ' . + 'time = "%s"' , + $this->properties['db_tafs_arch'], + $icao,$time)); + $this->query(sprintf('INSERT IGNORE INTO %s SET icao = "%s", ' . + 'time = "%s", ' . + 'taf = "%s"', + $this->properties['db_tafs_arch'], $icao, + $time, + addslashes($taf))); + } + } + } + /** * Gets a TAF form the database. * @@ -292,7 +350,7 @@ PRIMARY KEY (icao), UNIQUE icao (icao))'); - /* First we make a table for the TAFs */ + /* Then we make a table for the TAFs */ $this->query('DROP TABLE IF EXISTS ' . $this->properties['db_tafs']); $this->query('CREATE TABLE ' . $this->properties['db_tafs'] . '( icao char(4) NOT NULL, @@ -300,6 +358,20 @@ timestamp timestamp(14), PRIMARY KEY (icao), UNIQUE icao (icao))'); + + /* We make the archival databases */ + $this->query('DROP TABLE IF EXISTS ' . $this->properties['db_metars_arch']); + $this->query('CREATE TABLE ' . $this->properties['db_metars_arch'] . '( + icao varchar(4) NOT NULL default '', + time timestamp(14) NOT NULL, + metar varchar(255) NOT NULL default '', + PRIMARY KEY (icao,time)'); + $this->query('DROP TABLE IF EXISTS ' . $this->properties['db_tafs_arch']); + $this->query('CREATE TABLE ' . $this->properties['db_tafs_arch'] . '( + icao varchar(4) NOT NULL default '', + time timestamp(14) NOT NULL, + taf varchar(255) NOT NULL default '', + PRIMARY KEY (icao,time)'); /* Then we make a table for the stations. */ $this->query('DROP TABLE IF EXISTS ' . $this->properties['db_stations']); |