<?/*
Copyright (C) 2003 Media Net Link, Inc. Danville, CA 94526 USA

This file is part of AthenaRMS.

AthenaRMS 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.

AthenaRMS 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
*/?>
<?php
  require_once 'DB.php';
  require_once 'athena.conf';

  global $dbi;
  global $db_config;
  global $ADODB_SESSION_DRIVER;
  global $ADODB_SESSION_CONNECT;
  global $ADODB_SESSION_USER;
  global $ADODB_SESSION_PWD;
  global $ADODB_SESSION_DB;
  global $ADODB_SESSION_USE_LOBS;

  ## call wherever you'd use || in pgsql/oracle or CONCAT() in myqsl
  ## takes any number of arguments
  function sql_concat() {
    global $db_config;
    $args = func_get_args();

    if ($db_config['driver'] == 'mysql')
      return('CONCAT(' . join(', ', $args) . ')');
    else
      return join(' || ', $args);
  }

  ## call wherever you'd use to_date in pgsql to put a date into
  ## a timestamp string.  all dates should now be in format:
  ## YYYY-MM-DD HH:MM:SS
  function to_date($str = '?') {
    global $db_config;

    switch ($db_config['driver']) {
      case 'pgsql':
	return $str;
      case 'mysql':
	return $str;
      case 'oci8':
        return "to_date($str,'YYYY-MM-DD HH24:MI:SS')";
    }
  }

  ## call this whereever you'd use to_char in pgsql or date_format
  ## in myqsl.  use mysql date formatting syntax, as documented at:
  ## http://www.mysql.com/doc/en/Date_and_time_functions.html#IDX1309
  function date_format($str = '?', $fmt = '%Y/%m/%d %H:%i:%S') {
    global $db_config;

    switch ($db_config['driver']) {
      case 'pgsql':
      case 'oci8':
	$re = array(
	  '%%'	=> '__PERCENT__',

	  '%M'	=> 'Month',
	  '%W'	=> 'Day',
	  '%Y'	=> 'YYYY',
	  '%y'	=> 'YY',
	  '%a'	=> 'Dy',
	  '%d'	=> 'DD',
	  '%e'	=> 'DD',    # day without leading 0; not supported in pg?
	  '%m'	=> 'MM',
	  '%c'	=> 'MM',    # month without leading 0; not supported in pg?
	  '%b'	=> 'Mon',
	  '%j'	=> 'DDD',
	  '%H'	=> 'HH24',
	  '%k'	=> 'HH24',  # hour(24) without leading 0; ...
	  '%h'	=> 'HH12',
	  '%I'	=> 'HH12',
	  '%l'	=> 'HH12',  # hour(12) without leading 0; ...
	  '%i'	=> 'MI',
	  '%r'	=> 'HH12:MI:SS AM',
	  '%T'	=> 'HH24:MI:SS',
	  '%S'	=> 'SS',
	  '%s'	=> 'SS',
	  '%p'	=> 'am',
	  #'%w'	=> 'D',	    # day of week; mysql = 0-6, pgsql = 1-7

	  '__PERCENT__' => '%'
	);
	return("to_char($str, '" . 
	  str_replace(array_keys($re), array_values($re), $fmt) . "')");
      case 'mysql':
	return "date_format($str, '$fmt')";
    }
  }


  ## only make one of these!
  if (!$dbi) {

    ## construct db connection string
    $str = $db_config['driver']     . '://' .
	   $db_config['user'] 	    . ':'   .
	   $db_config['password'];

    if ($db_config['driver'] == 'oci8') {
      $str .= '@' . $db_config['dbname'];
      $ADODB_SESSION_USE_LOBS = 'CLOB';
    } else {
      if ($db_config['host']) {
	  $str .= '@' . $db_config['host'];
	  if (array_key_exists('port', $db_config)) 
	    $str .= ':' . $db_config['port'];
      }

      $str .= '/' . $db_config['dbname'];
    }

    $dbi = DB::connect($str, array ( 
	'persistent' => true, 'optimize' => 'portability' ));

    if (!$dbi) die("DBI connect failed");

    if (DB::isError($dbi)) die($dbi->getMessage() . "\n" . $dbi->getUserInfo());

    if ($db_config['driver'] == 'pgsql')
      $ADODB_SESSION_DRIVER   = 'postgres7';
    else $ADODB_SESSION_DRIVER = $db_config['driver'];

    if (array_key_exists('host', $db_config)) {
      $ADODB_SESSION_CONNECT  = $db_config['host'];

      if (array_key_exists('port', $db_config)) 
	$ADODB_SESSION_CONNECT .= ':' . $db_config['port'];
    }

    $ADODB_SESSION_USER     = $db_config['user'];
    $ADODB_SESSION_PWD      = $db_config['password'];
    $ADODB_SESSION_DB       = $db_config['dbname'];
  }
?>
