<?php

  /*

      recordset.php

      Contains some database access functions for ease of use.

      ----

      THIS FILE HAS DATABASE SPECIFIC FUNCTIONS, therefore, if a
      database migration occurs, this page must be updated.

      ----

      Copyright (C) 2003  Central Manchester CLC
      Copyright (C) 2003  David Thorne (mmmbena1@hotmail.com)

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation version 2.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  */

  require_once "system/opendb.php";

  // Create a recordset and populate it with the returned rows from the $sql command
  function db_recordset($sql) {
    $result = pg_exec($sql) or die("<b>Fatal Error: SQL Query Failed.</b><br><pre>$sql</pre><br>This could be because there is no valid connection, or because the query is incorrect.");
    $recordset = array();
    for ($i=0;$i<pg_numrows($result);$i++) {
      $recordset[] = pg_fetch_array($result,$i,PGSQL_ASSOC);
    }
    return $recordset;
  }

  // Send an $sql command to the database, and if an INSERT then return the inserted ID field.
  function db_send($sql) {
    return pg_exec($sql) or die("<b>Fatal Error: SQL Query Failed.</b><br><pre>$sql</pre><br>This could be because there is no valid connection, or because the query is incorrect.");
  }

  // Filter a recordset for all $fields with a value of $value.
  function filter_records($recordset,$field,$value) {
    $return_array = array();

    // Reset Recordset and contruct the filtered recordset
    reset($recordset);
    foreach ($recordset as $key => $val)
      if ($val[$field] == $value) $return_array[] = $val;

    return $return_array;
  }

  function compact_records($recordset,$field) {
    $return_array = array();

    foreach ($recordset as $record) {
      $return_array[] = $record[$field];
    }

    return $return_array;
  }

  // Merges several recordsets into one, and removes the first occurrences of
  // records with duplicate fileds (as passed to the function)
  // First argument is the field to check on, all other arguments
  // are the arrays to be merged
  function merge_recordsets() {
    $arrays=func_get_args();
    $field = array_shift($arrays);

    $returnarray = array();
    foreach ($arrays as $array) {
        // Don't bother if the array is empty
        if (count($array) > 0) {
            // Remove any existing elements from the original array
            foreach ($array as $element) {
                foreach ($returnarray as $retindex=>$retelement) {
                    if ($retelement[$field] == $element[$field]) {
                        unset($returnarray[$retindex]);
                    }
                }
            }
        }
        $returnarray = array_merge($returnarray, $array);
    }
    return($returnarray);
  }

?>
