<?php
/* Database (PDO) function library module.
   Copyright 2007-2009, Future Lab. 

   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; either version 2 of the License, or
   (at your option) any later version.

   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, get a copy via the Internet at
   http://gnu.org/copyleft/gpl.html or write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
   MA 02111-1307 USA

   You can contact the author via email at rsmereka@future-lab.com

   Original version Jun/2007, Rick Smereka */

function exist($dbh, $tname, $fname, $recid)
{
   /* Test for the existance of the record 'recid' in the field 'fname'
      in the table 'tname'. The database handle is assumed to be in
      'dbh' and the database is assumed to be open. Function returns
      'true' if the record exists, 'false' otherwise. */
   $query = 'SELECT ' . $fname . ' FROM ' . $tname . ' WHERE ';
   $query .= $fname . ' = ' . "'" . $recid . "'";
   // puts("<P>query=$query</P>");

   try
      {
      if (!$stmt = $dbh->prepare($query))
         die('exist:PDO prepare failed, SQL syntax error?');

      if ($stmt->execute())
          $row = $stmt->fetch(PDO::FETCH_ASSOC);
      }
   catch(PDOException $e)
      {
      print 'exist:Error!:' . $e->getMessage() . '<br />';
      die();
      }

   if (!isset($row[$fname]) || $row[$fname] == '')
      return false;

   return true;
}

function db_get_one($dbh, $query_str, $fname)
{
   /* Obtain a single field from the database. The database handle
      is expected in 'dbh', the SQL query string is expected in
      'query_str' and the database field name is expected in
      'fname'. Function returns the field contents upon success,
      an empty string otherwise. */
   // puts("<P>db_get_one:query=$query_str</P>");
   
   try
      {
      if (!$stmt = $dbh->prepare($query_str))
         die("db_get_one:PDO prepare failed, SQL syntax?(q=$query_str");

      if ($stmt->execute())
          $row = $stmt->fetch(PDO::FETCH_ASSOC);
      }
   catch(PDOException $e)
      {
      print 'db_get_one:Error!:' . $e->getMessage() . '<br />';
      die();
      }

   if (!isset($row[$fname]) || $row[$fname] == '')
      return '';

   return $row[$fname];
}

function sql_addquotes($str, $iscommadelim = false)
{
   /* Surround a string with single quotes and check the string for 
      single quotes and add an extra single quote for each one found.
      If 'iscommadelim' is also high, append a comma and a trailing
      space to the string. Returns the modified string. */
   $ret = "'" . str_replace("'", "''", $str) . "'";

   if ($iscommadelim)
      return $ret . ', ';

   return $ret;
}

function db_error($mes, $dbname)
{
   // Display a database error.
   p("database error:$mes,dbname=$dbname");
   //die();
}

function read_config($dbh, $recid)
{
   // Read a record from the 'parms' table. Database handle is 'dbh'.
   $query = "SELECT value FROM parms WHERE name = '$recid'";

   try
      {
      if (!$stmt = $dbh->prepare($query))
         die("read_config:PDO prepare failed($recid), SQL syntax error?");

      if ($stmt->execute())
         $row = $stmt->fetch();
      }
   catch(PDOException $e)
      {
      print 'Error!:' . $e->getMessage() . '<br />';
      die();
      }

   // puts("rval={$row['value']}<BR /");
   return $row['value'];
}

function write_config($dbh, $recid, $dat)
{
   // Write data ('dat') back to a 'parms' record ('recid').
   $query = "UPDATE parms SET value = \"$dat\" WHERE name = '$recid'";
   // puts("write_config:dat=$dat<BR />");

   try
      {
      if (!$stmt = $dbh->prepare($query))
         die("write_config:PDO prepare failed($recid), SQL syntax error?");

      if (!$stmt->execute())
         die("write_config:execute write failed($recid)");
      }
   catch(PDOException $e)
      {
      print 'Error!:' . $e->getMessage() . '<br />';
      die();
      }
}
?>
