Update of /cvsroot/phpweather/phpweather/db
In directory usw-pr-cvs1:/tmp/cvs-serv22705

Added Files:
        db_common.php db_dba.php db_mysql.php db_null.php db_pgsql.php 
Log Message:
These files have been updated, so that they work with the new
features, like get_countries() etc.


--- NEW FILE ---
<?php
require_once(PHPWEATHER_BASE_DIR . '/base_object.php');

/**
 * Common class for all the database-types.
 *
 * It contains some properties most database-types need, like $is_connected, $link_id 
and $result_id.
 *
 * @author   Martin Geisler <[EMAIL PROTECTED]>
 * @version  $Id: db_common.php,v 1.1 2001/06/27 11:16:21 gimpster Exp $
 */
class db_common extends base_object {

  /**
   * Maintains the status of the database-connection.
   *
   * @var     boolean
   * @access  public
   */
  var $is_connected;

  /**
   * contains the link_id used when querying.
   *
   * @var     integer
   * @access  private
   */
  var $link_id;
  
  /**
   * contains the result_id used when fetching rows from a result-set.
   *
   * @var     integer
   * @access  private
   */
  var $result_id;

  /**
   * Initializes the database-object.
   *
   * $is_connected, $link_id and $result_id is set to false, to indicate that we're 
not connected.
   * 
   * @param  array  the initial properties of the object
   * @see    $is_connected, $link_id, $result_id
   */
  function db_common($input) {
    /* We start by calling the parent constructor. */
    $this->base_object($input);
    
    /* We're not connected at first, so we set these variables to
       indicate that. */
    $this->is_connected = false;
    $this->link_id      = false;
    $this->result_id    = false;
  }

}

?>

--- NEW FILE ---
<?php
require_once(PHPWEATHER_BASE_DIR . '/db/db_common.php');

/**
 * This class is the 'dba' database-type. This type of database is a wrapper itself, 
so you have to pass a handler to it as the index 'db_handler', when you create it.
 *
 * It implements all the methods necessary to insert, update and retrive METARs using 
a Berkeley DB style database. This is a file-based database, so you have to make sure 
that you have write access to the directory where you place the database.
 *
 * @author   Martin Geisler <[EMAIL PROTECTED]>
 * @version  $Id: db_dba.php,v 1.1 2001/06/27 11:16:21 gimpster Exp $
 */
class db_dba extends db_common {

  /**
   * A link ID to the stations database.
   *
   * We have to maintain a different link ID for each database used.
   *
   * @var     integer
   * @access  private
   */
  var $link_stations_id;

  /**
   * A link ID to the countries database.
   *
   * We have to maintain a different link ID for each database used.
   * 
   * @var    integer
   * @access private
   */
  var $link_countries_id;

  /**
   * Constructor
   *
   * @param  array  the initial properties of the object
   */
  function db_dba($input) {
    /* We just call the parent constructor. */
    $this->db_common($input);
  }

  /**
   * Gets the type of the database.
   *
   * @return  string  The type of the database, 'mysql' in this case.
   * @access  public
   */
  function get_type() {
    return 'dba';
  }

  /**
   * Establishes a connection to the database. It is assumed, that the database is 
already created.
   *
   * If there has already been made a connection to the database, this function just 
returns true, and nothing will be changed. This means that it is safe to call this 
instead of testing $is_connected. If $properties['db_pconnect'] is true, then a 
persistent connection will be established. $db_hostname is used as the path for the 
database.
   *
   * @return  boolean  Returns true, if a connection were established, false otherwise.
   * @access  public
   * @see     disconnect()
   */
  function connect() {
    if ($this->is_connected) {
      return true;
    }
    if (!$this->properties['db_pconnect']) {
      $this->is_connected = $this->link_id =
        dba_open(PHPWEATHER_BASE_DIR . '/db/files/' .
                 $this->properties['db_metars'],
                 'w',
                 $this->properties['db_handler']);
      $this->link_stations_id = 
        dba_open(PHPWEATHER_BASE_DIR . '/db/files/' .
                 $this->properties['db_stations'],
                 'r',
                 $this->properties['db_handler']);
      $this->link_countries_id = 
        dba_open(PHPWEATHER_BASE_DIR . '/db/files/' .
                 $this->properties['db_countries'],
                 'r',
                 $this->properties['db_handler']);
    } else {
      $this->is_connected = $this->link_id =
        dba_popen(PHPWEATHER_BASE_DIR . '/db/files/' .
                  $this->properties['db_metars'],
                  'w',
                  $this->properties['db_handler']);
      $this->link_stations_id = 
        dba_popen(PHPWEATHER_BASE_DIR . '/db/files/' .
                  $this->properties['db_stations'],
                  'r',
                  $this->properties['db_handler']);
      $this->link_countries_id = 
        dba_popen(PHPWEATHER_BASE_DIR . '/db/files/' .
                  $this->properties['db_countries'],
                  'r',
                  $this->properties['db_handler']);
    }
    return $this->is_connected;
  }
  
  /**
   * Disconnects from the database.
   *
   * If we're already disconnected from the database, this function will just return 
true.
   *
   * @return  boolean  Since dba_close() doesn't return any value, this function 
always returns true.
   * @access  public
   * @see     connect()
   */
  function disconnect() {
    if ($this->is_connected) {
      dba_close($this->link_id);
      $this->is_connected = false;
    }
    return true;
  }

  /**
   * Inserts a METAR into the database.
   *
   * @param   string   The ICAO of the station.
   * @param   string   The raw METAR.
   * @param   integer  A standard UNIX timestamp.
   * @access  public
   * @see update_metar()
   */
  function insert_metar($station, $metar, $timestamp) {
    dba_insert($station, $metar . ':' . $timestamp, $this->link_id);
  }

  /**
   * Updates an existing METAR in the database.
   *
   * @param   string   The ICAO of the station.
   * @param   string   The raw METAR.
   * @param   integer  A standard UNIX timestamp.
   * @access  public
   * @see insert_metar()
   */
  function update_metar($station, $metar, $timestamp) {
    dba_replace($station, $metar . ':' . $timestamp, $this->link_id);
  }

  /**
   * Gets a METAR form the database.
   *
   * @param   string  The ICAO of the station.
   * @return  array   The raw METAR and UNIX timestamp as an array, in that order.
   * @access  public
   */
  function get_metar($station) {
    if (dba_exists($station, $this->link_id)) {
      $row = dba_fetch($station, $this->link_id);
      $this->debug("Returning this row from the DBA database:<br><code>$row</code>");
      return explode(':', $row);
    } else {
      return false;
    }
  }

  /**
   * Translates an ICAO into a station name
   *
   * The boring ICAO (e.g. EKYT) is translated into something like 'Aalborg, Denmark'.
   *
   * @param   string  The ICAO one want's to translate.
   * @return  string  The full name of the station, including country.
   * @access  public
   */
  function lookup_icao($icao) {
    if (dba_exists($icao, $this->link_stations_id)) {
      list($name, $country) = explode(':',
                                      dba_fetch($icao, $this->link_stations_id));
      return "$name, $country";
    } else {
      return $icao;
    }
  }

  /**
   * Creates the necessary files.
   *
   * @return  bool  Returns true is the files were created, false otherwise.
   * @access  private
   */
  function create_tables() {
    /* The following code is taken from connect(). The difference is,
       that all the databases are created and truncated by this code. */
    if (!$this->properties['db_pconnect']) {
      $this->is_connected = $this->link_id =
        dba_open(PHPWEATHER_BASE_DIR . '/db/files/' .
                 $this->properties['db_metars'],
                 'n',
                 $this->properties['db_handler']);
      $this->link_stations_id = 
        dba_open(PHPWEATHER_BASE_DIR . '/db/files/' .
                 $this->properties['db_stations'],
                 'n',
                 $this->properties['db_handler']);
      $this->link_countries_id = 
        dba_open(PHPWEATHER_BASE_DIR . '/db/files/' .
                 $this->properties['db_countries'],
                 'n',
                 $this->properties['db_handler']);
    } else {
      $this->is_connected = $this->link_id =
        dba_popen(PHPWEATHER_BASE_DIR . '/db/files/' .
                  $this->properties['db_metars'],
                  'n',
                  $this->properties['db_handler']);
      $this->link_stations_id = 
        dba_popen(PHPWEATHER_BASE_DIR . '/db/files/' .
                  $this->properties['db_stations'],
                  'n',
                  $this->properties['db_handler']);
      $this->link_countries_id = 
        dba_popen(PHPWEATHER_BASE_DIR . '/db/files/' .
                  $this->properties['db_countries'],
                  'n',
                  $this->properties['db_handler']);
    }
    return $this->is_connected;
  }

  /**
   * Inserts the stations into the database.
   *
   * It is assumed that create_tables() has been called previously (and that it 
returned true).
   *
   * @param   array  This three-dimensional array starts with a list of contry-codes. 
For each country-code the ICAOs and corresponding locations in that particular country 
are listed as key => value pairs.
   * @param   array  An associative array with country-codes as the keys and the names 
of the countries as the values.
   * @return  bool
   * @access  private
   */
  function insert_stations($data, $countries) {
    while(list($cc, $country) = each($countries)) {
      while(list($icao, $location) = each($data[$cc])) {
        /* We insert all the stations in a given country into the
           database. */
        dba_insert($icao, "$location:$country", $this->link_stations_id);
        $icaos[] = $icao; /* We collect the ICAOs for later. */
      }
      /* Now that we've collected all the ICAOs in the country, lets
         insert the country with it's data into the database. The name
         of the country is seperated from the list of ICAOs by a
         single semi-colon (;). The ICAOs are seperated by a normal
         colon (:). */
      dba_insert($cc,
                 $country . ';' . implode(':', $icaos),
                 $this->link_countries_id);
      unset($icaos);  /* We can now forget about the ICAOs. */
    }
    return true;
  }

  /**
   * Returns a list of available countries.
   *
   * @return  array  An associative array with the country-codes as
   *                 the keys and the names of the countries as the
   *                 values.
   * @access  public
   */
  function get_countries() {
    if (!$this->connect()) {
      return false;
    }

    $cc = dba_firstkey($this->link_countries_id); /* We need the first key. */
    while ($data = dba_fetch($cc, $this->link_countries_id)) {
      list($country) = explode(';', $data);
      /* The above statement extracts the name of the country. It's
         seperated from the ICAOs by a semi-colon (;) */
      $countries[$cc] = $country;
      $cc = dba_nextkey($this->link_countries_id);
    }

    asort($countries); // Let's sort the countries.

    return $countries;
  }

  /**
   * Returns an array of stations.
   *
   * @param   string  The country-code.
   * @param   string  This parameter is passed by reference. The name
   *                  of the country that corresponds to the
   *                  country-code is stored here.
   * @return  array   An associative array with the ICAO as the key
   *                  and the name of the station as the values. The
   *                  name of the country is not added to the name of
   *                  the station.
   * @access  public
   */
  function get_icaos($cc, &$country) {

    if (!$this->connect()) {
      return false;
    }
    
    /* The name of the country is seperated from the list of ICAOs by
       a single semi-colon (;) */
    list($country, $icaos) = explode(';', dba_fetch($cc, $this->link_countries_id));

    /* The ICAOs are seperated by a normal colon (:) */
    $icaos = explode(':', $icaos);
    while (list(, $icao) = each($icaos)) {
      list($location) = explode(':', dba_fetch($icao, $this->link_stations_id));
      $locations[$icao] = $location;
    }
    return $locations;
  }

}

?>

--- NEW FILE ---
<?php
require_once(PHPWEATHER_BASE_DIR . '/db/db_common.php');

/**
 * This class is the 'mysql' database-type.
 *
 * It implements all the methods necessary to insert, update and retrive METARs using 
a MySQL database. You'll need access to a MySQL database to be able to use this object.
 *
 * @author   Martin Geisler <[EMAIL PROTECTED]>
 * @version  $Id: db_mysql.php,v 1.1 2001/06/27 11:16:21 gimpster Exp $
 */
class db_mysql extends db_common {
  
  /**
   * This constructor does nothing besides calling the parent constructor.
   *
   * @param  array  the initial properties of the object
   */
  function db_mysql($input) {
    $this->db_common($input);
  }

  /**
   * Gets the type of the database.
   *
   * @return  string  The type of the database, 'mysql' in this case.
   * @access  public
   */
  function get_type() {
    return 'mysql';
  }


  /**
   * Establishes a connection to the database.
   *
   * If there has already been made a connection to the database, this function just 
returns true, and nothing will be changed. This means that it is safe to call this 
instead of testing $is_connected. If $properties['db_pconnect'] is true, then a 
persistent connection will be established.
   *
   * @return  boolean  Returns true, if a connection were established, false otherwise.
   * @access  public
   * @see     disconnect()
   */
  function connect() {
    /* Connect to the MySQL server */
    if ($this->is_connected) {
      return true;
    }
    if (!$this->properties['db_pconnect']) {
      $this->link_id = mysql_connect($this->properties['db_hostname'],
                                     $this->properties['db_username'],
                                     $this->properties['db_password']);
    } else {
      $this->link_id = mysql_pconnect($this->properties['db_hostname'],
                                      $this->properties['db_username'],
                                      $this->properties['db_password']);
    }
    if ($this->link_id) {
      $this->is_connected = true;
      $this->select_db();
    } else {
      $this->is_connected = false;
    }
    return $this->is_connected;
  }

  
  /**
   * Disconnects from the database.
   *
   * If we're already disconnected from the database, this function will just return 
true.
   *
   * @return  boolean  If the disconnec was succesfull then it returns true, otherwise 
it returns false.
   * @access  public
   * @see     connect()
   */
  function disconnect() {
    if (!$this->is_connected || mysql_close($this->link_id)) {
      $this->is_connected = false;
      return true;
    } else {
      return false;
    }
  }


  /**
   * Selects a database.
   *
   * @return  boolean  Returns true on success, false otherwise.
   * @access  public
   */
  function select_db() {
    return mysql_select_db($this->properties['db_database'], $this->link_id);
  }


  /**
   * Executes a SQL-query.
   *
   * $result_id is updated as well
   *
   * @param   string   The SQL-statement.
   * @return  boolean  True on success, false otherwise.
   * @access  public
   */
  function query($query) {
    return ($this->result_id = mysql_query($query, $this->link_id));
  }


  /**
   * Fetches a row as an array from the database.
   *
   * @return  array   The next row from the result-set.
   * @access  public
   */
  function fetch_row() {
    $this->debug('fetch_row(): MySQL said: ' . mysql_error());
    return mysql_fetch_row($this->result_id);
  }


  /**
   * Fetches a row as an associative array from the database.
   *
   * @return  array   The next row from the result-set, as an associative array.
   * @access  public
   */
  function fetch_array() {
    return mysql_fetch_array($this->result_id);
  }


  /**
   * Returns the number of rows in the result-set.
   *
   * @return  integer  The number of rows in the current result-set.
   * @access  public
   */
  function num_rows() {
    return mysql_num_rows($this->result_id);
  }


  /**
   * Inserts a METAR into the database.
   *
   * @param   string   The ICAO of the station.
   * @param   string   The raw METAR.
   * @param   integer  A standard UNIX timestamp.
   * @access  public
   * @see update_metar()
   */
  function insert_metar($icao, $metar, $timestamp) {
    $this->query("INSERT INTO metars SET icao = '$icao', metar = '$metar', timestamp = 
FROM_UNIXTIME($timestamp)");
  }


  /**
   * Updates an existing METAR in the database.
   *
   * @param   string   The ICAO of the station.
   * @param   string   The raw METAR.
   * @param   integer  A standard UNIX timestamp.
   * @access  public
   * @see insert_metar()
   */
  function update_metar($icao, $metar, $timestamp) {
    $this->query("UPDATE metars SET metar = '$metar', timestamp = 
FROM_UNIXTIME($timestamp) WHERE icao = '$icao'");
  }


  /**
   * Gets a METAR form the database.
   *
   * @param   string  The ICAO of the station.
   * @return  string  The raw METAR as an array from the database.
   * @access  public
   */
  function get_metar($icao) {
    $this->query("SELECT metar, UNIX_TIMESTAMP(timestamp) FROM metars WHERE icao = 
'$icao'");
    return $this->fetch_row();
  }


  /**
   * Creates the necessary tables in the database.
   *
   * @return  bool  Returns true if it could connect to the database, false otherwise.
   * @access  private
   */
  function create_tables() {
    if (!$this->connect()) {
      return false; // Failure!
    }

    /* First we make a table for the METARs */
    $this->query('DROP TABLE IF EXISTS ' . $this->properties['db_metars']);
    $this->query('CREATE TABLE ' . $this->properties['db_metars'] . '(
   icao varchar(4) NOT NULL,
   metar varchar(255) NOT NULL,
   timestamp timestamp(14),
   PRIMARY KEY (icao),
   UNIQUE icao (icao)
)');

    /* Then we make a table for the stations. */
    $this->query('DROP TABLE IF EXISTS ' . $this->properties['db_stations']);
    $this->query('CREATE TABLE ' . $this->properties['db_stations'] . '(
   icao varchar(4) NOT NULL,
   name varchar(255) NOT NULL,
   cc char(2) NOT NULL,
   country varchar(128) NOT NULL,
   PRIMARY KEY (icao),
   UNIQUE icao (icao),
   KEY cc (cc)
)');
   
    return true; // Succes!
    
  }

  /**
   * Translates an ICAO into a station name
   *
   * The boring ICAO (e.g. EKYT) is translated into something like 'Aalborg, Denmark'.
   *
   * @param   string  The ICAO one want's to translate.
   * @return  string  The full name of the station, including country.
   * @access  public
   */
  function lookup_icao($icao) {
    $this->query('SELECT name, country FROM ' . $this->properties['db_stations'] .
                 " WHERE icao = '$icao'");
    if ($this->num_rows()) {
      $row = $this->fetch_row();
      return "$row[0], $row[1]";
    } else {
      return $icao;
    }
  }

  /**
   * Inserts the stations into the database.
   *
   * It is assumed that create_tables() has been called previously (and that it 
returned true), so that the necessary tables are already created.
   *
   * @param   array  This three-dimensional array starts with a list of contry-codes. 
For each country-code the ICAOs and corresponding locations in that particular country 
are listed as key => value pairs.
   * @param   array  An associative array with country-codes as the keys and the names 
of the countries as the values.
   * @return  bool
   * @access  private
   */
  function insert_stations($data, $countries) {
    if (!$this->connect()) {
      return false;
    }

    while(list($cc, $country) = each($countries)) {
      /* The country names might contain dangerous characters. */
      $country = addslashes($country);
      while(list($icao, $location) = each($data[$cc])) {
        /* The station name might also be dangerous. */
        $location = addslashes($location); 
        $this->query('INSERT INTO ' . $this->properties['db_stations'] .
                       " VALUES ('$icao', '$location', '$cc', '$country')");

      }
    }
    return true;
  }


  /**
   * Returns a list of available countries.
   *
   * @return  array  An associative array with the country-codes as the keys and the 
names of the countries as the values.
   * @access  public
   */
  function get_countries() {
    if (!$this->connect()) {
      return false;
    }

    $this->query('SELECT DISTINCT cc, country FROM ' . 
$this->properties['db_stations'] . ' ORDER BY country');
    while($row = $this->fetch_row()) {
      $rows[$row[0]] = $row[1];
    }
    return $rows;
  }


  /**
   * Returns an array of stations.
   *
   * @param   string  The country-code.
   * @param   string  This parameter is passed by reference. The name of the country 
that corresponds to the country-code is stored here.
   * @return  array   An associative array with the ICAO as the key and the name of 
the station as the values. The name of the country is not added to the name of the 
station.
   * @access  public
   */
  function get_icaos($cc, &$country) {
    if (!$this->connect()) {
      return false;
    }

    $this->query('SELECT icao, name, country FROM ' . $this->properties['db_stations'] 
. " WHERE cc = '$cc' ORDER BY name");
    /* We have to do this manually the first time, so that we can set
       $country */
    list($icao, $name, $country) = $this->fetch_row();
    $rows[$icao] = $name;
    while(list($icao, $name) = $this->fetch_row()) {
      $rows[$icao] = $name;
    }
    return $rows;
  }

}

?>

--- NEW FILE ---
<?php
require_once(PHPWEATHER_BASE_DIR . '/db/db_common.php');

/**
 * This class is the 'null' database-type
 *
 * It pretends to be a database, but really it isn't :-) It just implements all the 
functions one would expect from a database, and then returns true or false or with 
empty strings and arrays as appropriate.
 *
 * @author   Martin Geisler <[EMAIL PROTECTED]>
 * @version  $Id: db_null.php,v 1.1 2001/06/27 11:16:21 gimpster Exp $
 */
class db_null extends db_common {
  
  /**
   * This constructor does nothing besides calling the parent constructor.
   *
   * @param  array  the initial properties of the object
   */
  function db_null($input) {
    $this->db_common($input);
  }

  /**
   * Gets the type of the database.
   *
   * @return  string  The type of the database, 'null' in this case.
   * @access  public
   */
  function get_type() {
    return 'null';
  }

  /**
   * Pretends to establish a connection to the database.
   *
   * Like all the other methods of this database object, this doesn't do anything 
useful.
   *
   * @return  boolean  Always returns true, so that other functions that depends on a 
connection to the database doesn't fail because of this.
   * @access  public
   */
  function connect() {
    return true;
  }


  /**
   * Pretends to insert a METAR into the database.
   *
   * @param   string   The ICAO of the station.
   * @param   string   The raw METAR.
   * @param   integer  A standard UNIX timestamp.
   * @access  public
   * @see     update_metar()
   */
  function insert_metar($station, $metar, $timestamp) {
    ;
  }

  /**
   * Pretends to update an existing METAR in the database.
   *
   * @param   string   The ICAO of the station.
   * @param   string   The raw METAR.
   * @param   integer  A standard UNIX timestamp.
   * @access  public
   * @see     insert_metar()
   */
  function update_metar($station, $metar, $timestamp) {
    ;
  }

  /**
   * Pretends to return a METAR form the database.
   *
   * @param   string  The ICAO of the station.
   * @return  string  Since we don't have a database, we just return an empty string.
   * @access  public
   */
  function get_metar($station) {
    return '';
  }

  /**
   * Translates an ICAO into a station name
   *
   * @param   string  The ICAO to translate.
   * @return  string  The translated ICAO.
   * @access  public
   */
  function lookup_icao($icao) {

    $linesize = 128;
    
    $fp = fopen(PHPWEATHER_BASE_DIR . '/db/files/stations.db', 'r');
    
    $result = '';
    $left  = 0;
    $right = filesize(PHPWEATHER_BASE_DIR . '/db/files/stations.db')/$linesize;
    
    while ($left < $right) {
      
      fseek($fp, $linesize * round(($left+$right)/2));
      
      $data = fgetcsv($fp, $linesize, ';');
      if ($data[0] > $icao) {
        $right = floor(($left+$right)/2);
      } elseif ($data[0] < $icao) {
        $left = ceil(($left+$right)/2);
      } else {
        $left = $right;
        $result = trim($data[1]);
      }
    }
    fclose($fp);
    return $result;
  }



  /**
   * Makes sure that it's possible to create the files.
   *
   * The method doesn't create any files, it just veryfies that it's we have write 
permission to PHPWEATHER_BASE_DIR/db.
   * @return  bool  True if it's possible to create files.
   * @access  private
   */
  function create_tables() {
    return is_writable(PHPWEATHER_BASE_DIR . '/db');
  }

  /**
   * Inserts the stations into the database.
   *
   * It is assumed that create_tables() has been called previously (and that it 
returned true), so that we can create the necessary files. insert_stations() populates 
PHPWEATHER_BASE_DIR/db/files/ with a file for each country. The files contain PHP 
code, so that $country and $icaos will be set, if the file is included.
   *
   * @param   array  This three-dimensional array starts with a list of contry-codes. 
For each country-code the ICAOs and corresponding locations in that particular country 
are listed as key => value pairs.
   * @param   array  An associative array with country-codes as the keys and the names 
of the countries as the values.
   * @return  bool
   * @access  private
   */
  function insert_stations($data, $countries) {
    while(list($cc, $country) = each($countries)) {
      $fp = fopen(PHPWEATHER_BASE_DIR . "/db/files/$cc.php", 'w');
      fputs($fp, "<?php\n/* File with stationnames in $countries[$cc] */\n\n");
      fputs($fp, "\$country = '" . addslashes($countries[$cc]) . "';\n\n");
      fputs($fp, "\$icaos   = array(\n");
      /* We do it ourselves the first time */
      list($icao, $location) = each($data[$cc]);
      fputs($fp, "  '$icao' => '" . addslashes($location) . "'");
      $stations[$icao] = "$location, $countries[$cc]";
      
      while(list($icao, $location) = each($data[$cc])) {
        fputs($fp, ",\n  '$icao' => '" . addslashes($location) . "'");
        $stations[$icao] = "$location, $countries[$cc]";
      }
      fputs($fp, "\n);\n\n?>\n");
      fclose($fp);
    }
    /* We write a file with all the stations. Each line is 128 bytes
       long so that it's easy to find a given station again. */
    $fp = fopen(PHPWEATHER_BASE_DIR . '/db/files/stations.db', 'w');
    if ($fp) {
      ksort($stations);
      reset($stations);
      while(list($icao, $location) = each($stations)) {
        $str = str_pad("$icao;$location", 127)."\n";
        fputs($fp, $str);
      }
      fclose($fp);
    }

    /* We also write a file with all the countries. */
    $fp = fopen(PHPWEATHER_BASE_DIR . '/db/files/countries.php', 'w');
    if ($fp) {
      fputs($fp, "<?php\n/* File with names of all countries. */\n\n\$countries = 
array(\n");
      reset($countries);
      list($cc, $country) = each($countries);
      $country = addslashes($country);
      fputs($fp, "  '$cc' => '$country'");
      while(list($cc, $country) = each($countries)) {
        $country = addslashes($country);
        fputs($fp, ",\n  '$cc' => '$country'");
      }
      fputs($fp, "\n);\n\n?>\n");
      fclose($fp);
    }
    return true;
  }

  /**
   * Returns a list of available countries.
   *
   * It uses the file PHPWEATHER_BASE_DIR/db/files/countries.php created by 
insert_stations().
   *
   * @return  array  An associative array with the country-codes as the keys and the 
names of the countries as the values.
   * @access  public
   */
  function get_countries() {
    require_once(PHPWEATHER_BASE_DIR . '/db/files/countries.php');
    return $countries;
  }

  /**
   * Returns an array of stations.
   *
   * @param   string  The country-code.
   * @param   string  This parameter is passed by reference. The name of the country 
that corresponds to the country-code is stored here.
   * @return  array   An associative array with the ICAO as the key and the name of 
the station as the values. The name of the country is not added to the name of the 
station.
   * @access  public
   */
  function get_icaos($cc, &$country) {
    include(PHPWEATHER_BASE_DIR . "/db/files/$cc.php");
    return $icaos;
  }

}

?>

--- NEW FILE ---
<?php
require_once(PHPWEATHER_BASE_DIR . '/db/db_common.php');

/**
 * This class is the 'pgsql' database-type
 *
 * It implements all the methods necessary to insert, update and retrive METARs using 
a PostgreSQL database.
 *
 * @author   Kristian Kristensen <[EMAIL PROTECTED]>
 * @version  $Id: db_pgsql.php,v 1.1 2001/06/27 11:16:21 gimpster Exp $
 */
class db_pgsql extends db_common {
  
  /**
   * This constructor does nothing besides calling the parent constructor.
   *
   * @param  array  the initial properties of the object
   */
  function db_pgsql($input = array()) {
    $this->db_common($input);
  }

  /**
   * Gets the type of the database.
   *
   * @return  string  The type of the database, 'pgsql' in this case.
   * @access  public
   */
  function get_type() {
    return 'pgsql';
  }


  /**
   * Establishes a connection to the database.
   *
   * If there has already been made a connection to the database, this function just 
returns true, and nothing will be changed. This means that it is safe to call this 
instead of testing $is_connected. If $properties['db_pconnect'] is true, then a 
persistent connection will be established.
   *
   * @return  boolean  Returns true, if a connection were established, false otherwise.
   * @access  public
   * @see     disconnect()
   */
  function connect() {
    /* Connect to the PostgreSQL server */
    if ($this->is_connected) {
      return true;
    }
    if (!$this->properties['db_pconnect']) {
      $this->link_id = pgsql_connect($this->properties['db_database']);
    } else {
      $this->link_id = pgsql_pconnect($this->properties['db_database']);
    }
    if ($this->link_id) {
      $this->is_connected = true;
      $this->select_db();
    } else {
      $this->is_connected = false;
    }
    return $this->is_connected;
  }
  
  /**
   * Disconnects from the database.
   *
   * If we're already disconnected from the database, this function will just return 
true.
   *
   * @return  boolean  If the disconnec was succesfull then it returns true, otherwise 
it returns false.
   * @access  public
   * @see     connect()
   */
  function disconnect() {
    if (!$this->is_connected || pg_close($this->link_id)) {
      $this->is_connected = false;
      return true;
    } else {
      return false;
    }
  }


  /**
   * Selects a database.
   *
   * @return  boolean  Returns true on success, false otherwise.
   * @access  public
   */
  function select_db() {
    if ($this->is_connected) {
      return true;
    } else {
      return false;
    }
  }


  /**
   * Executes a SQL-query.
   *
   * $result_id is updated as well
   *
   * @param   string   The SQL-statement.
   * @return  boolean  True on success, false otherwise.
   * @access  public
   */
  function query($query) {
    return ($this->result_id = pg_exec( $this->link_id, $query));
  }

  /**
   * Fetches a row as an array from the database.
   *
   * @return  array   The next row from the result-set.
   * @access  public
   */
  function fetch_row() {
    return pg_fetch_row($this->result_id,0);
  }

  /**
   * Fetches a row as an associative array from the database.
   *
   * @return  array   The next row from the result-set, as an associative array.
   * @access  public
   */
  function fetch_array() {
    return pg_fetch_array($this->result_id,0);
  }

  /**
   * Returns the number of rows in the result-set.
   *
   * @return  integer  The number of rows in the current result-set.
   * @access  public
   */
  function num_rows() {
    return pg_numrows($this->result_id);
  }

  /**
   * Inserts a METAR into the database.
   *
   * @param   string   The ICAO of the station.
   * @param   string   The raw METAR.
   * @param   integer  A standard UNIX timestamp.
   * @access  public
   * @see     update_metar()
   */
  function insert_metar($station, $metar, $timestamp) {
    $this->query("INSERT INTO metars SET station = '$station', metar = '$metar', 
timestamp = FROM_UNIXTIME($timestamp)");
  }


  /**
   * Updates an existing METAR in the database.
   *
   * @param   string   The ICAO of the station.
   * @param   string   The raw METAR.
   * @param   integer  A standard UNIX timestamp.
   * @access  public
   * @see     insert_metar()
   */
  function update_metar($station, $metar, $timestamp) {
    $this->query("UPDATE metars SET metar = '$metar', timestamp = '$timestamp' WHERE 
station = '$station'");
  }

  /**
   * Gets a METAR form the database.
   *
   * @param   string  The ICAO of the station.
   * @return  string  The raw METAR as an array from the database.
   * @access  public
   */
  function get_metar($station) {
    $this->query("SELECT metar, timestamp FROM metars WHERE station = '$station'");
    return $this->fetch_row();
  }

}

?>


_______________________________________________
PHPWeather-checkins mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/phpweather-checkins

Reply via email to