par hika - 2005-07-03 11:25:26
crée le 2005-07-03 11:25:26

db.inc.php


<?php
/*
  Wrapper SQL functions
  Copyright (c) 2002,2003 Gilbert Cao (hika)
  All rights reserved.

  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, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

  /* WRAPPER SQL */
  function DAY($expr) {
    if (defined("USE_MYSQL"))
      return "DAYOFMONTH(".$expr.")";
    else if (defined("USE_PGSQL"))
      return "DATE_PART('day', ".$expr.")";
  }
  
  function MONTH($expr) {
    if (defined("USE_MYSQL"))
      return "MONTH(".$expr.")";
    else if (defined("USE_PGSQL"))
      return "DATE_PART('month', ".$expr.")";
  }
  
  function YEAR($expr) {
    if (defined("USE_MYSQL"))
      return "YEAR(".$expr.")";
    else if (defined("USE_PGSQL"))
      return "DATE_PART('year', ".$expr.")";
  }
  
  function SYSDATE() {
    if (defined("USE_MYSQL"))
      return "sysdate()";
    else if (defined("USE_PGSQL"))
      return "now()";
  }
  
  function UNIX_TIMESTAMP($expr) {
    if (defined("USE_MYSQL"))
      return "UNIX_TIMESTAMP(".$expr.")";
    else if (defined("USE_PGSQL"))
      return "EXTRACT(EPOCH FROM ".$expr.")";
  }
  # Since PHP 5.3.X, native PHP function date_add() exists
  function __DATE_ADD($expr, $value, $interval) {
    if (defined("USE_MYSQL"))
      return "DATE_ADD(".$expr.", INTERVAL ".$value." ".strtoupper($interval).")";
    else if (defined("USE_PGSQL"))
      return $expr." + interval '".$value." ".strtolower($interval)."'";
  }
  
  # Since PHP 5.3.X, native PHP function date_diff() exists
  function __DATE_DIFF($interval, $expr1, $expr2) {
    if (defined("USE_MYSQL")) {
      switch($interval) {
      case "d":
        return "TO_DAYS(".$expr1.") - TO_DAYS(".$expr2.")";
      }
    }
    else if (defined("USE_PGSQL")) {
      switch($interval) {
      case "d":
        return "DATE_PART('day', ".$expr1." - ".$expr2.")";
      }
    }
  }  
  
  function LIMIT($offset, $number) {
    if (defined("USE_MYSQL"))
      return "LIMIT ".$offset.", ".$number;
    else if (defined("USE_PGSQL"))
      return "LIMIT ".$number." OFFSET ".$offset;
  }
  
  function TO_BOOL($value) {
    if (defined("USE_MYSQL"))
      return $value;
    else if (defined("USE_PGSQL"))
      return ($value ? "TRUE" : "FALSE" );
  }

  function BINARY($bin) {
    if (defined("USE_MYSQL"))
      return "'".addslashes($bin)."'";
    else if (defined("USE_PGSQL")) {
      $search = array(chr(92), chr(0), chr(39));
      $replace = array("\\\\134", "\\\\000", "\\\\047");
      return "'".str_replace($search, $replace, $bin)."'";
    }
  }
  
  function SHOW_BINARY($bin) {
    if (defined("USE_MYSQL"))
      echo $bin;
    else if (defined("USE_PGSQL"))
      echo stripcslashes($bin);
  }
  
  function STRING_SQL($value) {
    if (defined("USE_MYSQL")) {
      if (is_null($value))
        return "NULL";
      return "'".addslashes($value)."'";
    }
    else if (defined("USE_PGSQL")) {
      if (is_null($value))
        return "NULL";
      return "'".addslashes($value)."'";
    }
  }
  
  function DATE_SQL($day, $month, $year, $hour = 0, $minute = 0, $second = 0) {
    if (defined("USE_MYSQL"))
      return "'".$year."-".$month."-".$day." ".$hour.":".$minute.":".$second."'";
    else if (defined("USE_PGSQL"))
      return "'".$year."-".$month."-".$day." ".$hour.":".$minute.":".$second."'";
  }
  
  function SAFE_DATE_SQL($value) {
    if (ereg("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})\ ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})\$", $value, $array_result)) {
      if ((checkdate($array_result[2], $array_result[3], $array_result[1]))
      && ($array_result[4] < 24)
      && ($array_result[5] < 60)
      && ($array_result[6] < 60)) {
        return "'".$array_result[1]."-".$array_result[2]."-".$array_result[3]." ".
               $array_result[4].":".$array_result[5].":".$array_result[6]."'";
      }
    }
    return "NULL";
  }

  function NUM_SQL($value, $decimal = 0) {
    if (defined("USE_MYSQL")) {
      if (is_null($value))
        return "NULL";
      return str_replace(",", ".", round($value, $decimal));
    }
    else if (defined("USE_PGSQL")) {
      if (is_null($value))
        return "NULL";
      return str_replace(",", ".", round($value, $decimal));
    }
  }
  
  if (defined("USE_MYSQL")) {
    include_once("mysql.php");
  }
  else if (defined("USE_PGSQL")) {
    include_once("pgsql.php"); 
  }
?>

par hika - 2005-07-03 11:25:26
crée le 2005-07-03 11:25:26

FreeBSD Mall Smarty Template Engine Page générée le Samedi 26 Avril 2025 14:38:25 Powered by FreeBSD
POWERED BY FreeBSD, Apache, PHP, PostgreSQL, Code & design by Hika
BSD Daemon Copyright 1988 by Marshall Kirk McKusick. All Rights Reserved.