Skip to content

Commit

Permalink
Fix: pgsql compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Mar 9, 2011
1 parent 737eb57 commit f0a15ff
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
19 changes: 12 additions & 7 deletions htdocs/comm/propal/class/propalestats.class.php
Expand Up @@ -88,13 +88,14 @@ function getNbByMonth($year)
if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(p.datep,'%Y') = ".$year;
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm DESC";
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');

return $this->_getNbByMonth($year, $sql);
}

/**
* Renvoie le nombre de propale par ann�e
* Renvoie le nombre de propale par annee
*
*/
function getNbByYear()
Expand All @@ -105,12 +106,13 @@ function getNbByYear()
$sql.= " FROM ".$this->from;
if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE ".$this->where;
$sql.= " GROUP BY dm DESC";
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');

return $this->_getNbByYear($sql);
}
/**
* Renvoie le nombre de propale par mois pour une ann�e donn�e
* Renvoie le nombre de propale par mois pour une annee donnee
*
*/
function getAmountByMonth($year)
Expand All @@ -122,7 +124,8 @@ function getAmountByMonth($year)
if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(p.datep,'%Y') = ".$year;
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm DESC";
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');

return $this->_getAmountByMonth($year, $sql);
}
Expand All @@ -139,7 +142,8 @@ function getAverageByMonth($year)
if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(p.datep,'%Y') = ".$year;
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm DESC";
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');

return $this->_getAverageByMonth($year, $sql);
}
Expand All @@ -157,7 +161,8 @@ function getAllByYear()
$sql.= " FROM ".$this->from;
if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE ".$this->where;
$sql.= " GROUP BY year DESC";
$sql.= " GROUP BY year";
$sql.= $this->db->order('year','DESC');

return $this->_getAllByYear($sql);
}
Expand Down
21 changes: 13 additions & 8 deletions htdocs/commande/class/commandestats.class.php
Expand Up @@ -100,13 +100,14 @@ function getNbByMonth($year)
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(c.date_commande,'%Y') = ".$year;
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm DESC";
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');

return $this->_getNbByMonth($year, $sql);
}

/**
* Renvoie le nombre de commande par ann�e
* Renvoie le nombre de commande par annee
*
*/
function getNbByYear()
Expand All @@ -118,13 +119,14 @@ function getNbByYear()
$sql.= " FROM ".$this->from;
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE ".$this->where;
$sql.= " GROUP BY dm DESC";
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');

return $this->_getNbByYear($sql);
}

/**
* Renvoie le nombre de commande par mois pour une ann�e donn�e
* Renvoie le nombre de commande par mois pour une annee donnee
*
*/
function getAmountByMonth($year)
Expand All @@ -137,13 +139,14 @@ function getAmountByMonth($year)
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(c.date_commande,'%Y') = ".$year;
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm DESC";
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');

return $this->_getAmountByMonth($year, $sql);
}

/**
* Renvoie le nombre de commande par mois pour une ann�e donn�e
* Renvoie le nombre de commande par mois pour une annee donnee
*
*/
function getAverageByMonth($year)
Expand All @@ -156,7 +159,8 @@ function getAverageByMonth($year)
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(c.date_commande,'%Y') = ".$year;
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm DESC";
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');

return $this->_getAverageByMonth($year, $sql);
}
Expand All @@ -174,7 +178,8 @@ function getAllByYear()
$sql.= " FROM ".$this->from;
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE ".$this->where;
$sql.= " GROUP BY year DESC";
$sql.= " GROUP BY year";
$sql.= $this->db->order('year','DESC');

return $this->_getAllByYear($sql);
}
Expand Down
12 changes: 8 additions & 4 deletions htdocs/compta/deplacement/class/deplacementstats.class.php
Expand Up @@ -94,7 +94,8 @@ function getNbByMonth($year)
$sql.= " FROM ".$this->from;
$sql.= " WHERE YEAR(dated) = ".$year;
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm DESC";
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');

$res=$this->_getNbByMonth($year, $sql);
//var_dump($res);print '<br>';
Expand All @@ -113,7 +114,8 @@ function getAmountByMonth($year)
$sql.= " FROM ".$this->from;
$sql.= " WHERE date_format(dated,'%Y') = ".$year;
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm DESC";
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');

$res=$this->_getAmountByMonth($year, $sql);
//var_dump($res);print '<br>';
Expand All @@ -131,7 +133,8 @@ function getAverageByMonth($year)
$sql.= " FROM ".$this->from;
$sql.= " WHERE date_format(dated,'%Y') = ".$year;
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm DESC";
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');

return $this->_getAverageByMonth($year, $sql);
}
Expand All @@ -145,7 +148,8 @@ function getAllByYear()
$sql = "SELECT date_format(dated,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
$sql.= " FROM ".$this->from;
$sql.= " WHERE ".$this->where;
$sql.= " GROUP BY year DESC";
$sql.= " GROUP BY year";
$sql.= $this->db->order('year','DESC');

return $this->_getAllByYear($sql);
}
Expand Down
10 changes: 5 additions & 5 deletions htdocs/compta/facture/class/facturestats.class.php
Expand Up @@ -91,7 +91,7 @@ function getNbByYear()
$sql.= " FROM ".$this->from;
$sql.= " WHERE ".$this->where;
$sql.= " GROUP BY dm";
$sql.= " ORDER BY dm DESC";
$sql.= $this->db->order('dm','DESC');

return $this->_getNbByYear($sql);
}
Expand All @@ -109,7 +109,7 @@ function getNbByMonth($year)
$sql.= " WHERE datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm";
$sql.= " ORDER BY dm DESC";
$sql.= $this->db->order('dm','DESC');

$res=$this->_getNbByMonth($year, $sql);
//var_dump($res);print '<br>';
Expand All @@ -129,7 +129,7 @@ function getAmountByMonth($year)
$sql.= " WHERE date_format(datef,'%Y') = ".$year;
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm";
$sql.= " ORDER BY dm DESC";
$sql.= $this->db->order('dm','DESC');

$res=$this->_getAmountByMonth($year, $sql);
//var_dump($res);print '<br>';
Expand All @@ -148,7 +148,7 @@ function getAverageByMonth($year)
$sql.= " WHERE datef BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
$sql.= " AND ".$this->where;
$sql.= " GROUP BY dm";
$sql.= " ORDER BY dm DESC";
$sql.= $this->db->order('dm','DESC');

return $this->_getAverageByMonth($year, $sql);
}
Expand All @@ -163,7 +163,7 @@ function getAllByYear()
$sql.= " FROM ".$this->from;
$sql.= " WHERE ".$this->where;
$sql.= " GROUP BY year";
$sql.= " ORDER BY year DESC";
$sql.= $this->db->order('year','DESC');

return $this->_getAllByYear($sql);
}
Expand Down
7 changes: 7 additions & 0 deletions htdocs/install/pgsql/functions/functions.sql
Expand Up @@ -29,6 +29,13 @@ CREATE OR REPLACE FUNCTION UNIX_TIMESTAMP(TIMESTAMP WITH TIME ZONE) RETURNS BIGI
CREATE OR REPLACE FUNCTION date_format(timestamp without time zone, text) RETURNS text AS $$ DECLARE i int := 1; temp text := ''; c text; n text; res text; BEGIN WHILE i <= pg_catalog.length($2) LOOP c := SUBSTRING ($2 FROM i FOR 1); IF c = '%' AND i != pg_catalog.length($2) THEN n := SUBSTRING ($2 FROM (i + 1) FOR 1); SELECT INTO res CASE WHEN n = 'a' THEN pg_catalog.to_char($1, 'Dy') WHEN n = 'b' THEN pg_catalog.to_char($1, 'Mon') WHEN n = 'c' THEN pg_catalog.to_char($1, 'FMMM') WHEN n = 'D' THEN pg_catalog.to_char($1, 'FMDDth') WHEN n = 'd' THEN pg_catalog.to_char($1, 'DD') WHEN n = 'e' THEN pg_catalog.to_char($1, 'FMDD') WHEN n = 'f' THEN pg_catalog.to_char($1, 'US') WHEN n = 'H' THEN pg_catalog.to_char($1, 'HH24') WHEN n = 'h' THEN pg_catalog.to_char($1, 'HH12') WHEN n = 'I' THEN pg_catalog.to_char($1, 'HH12') WHEN n = 'i' THEN pg_catalog.to_char($1, 'MI') WHEN n = 'j' THEN pg_catalog.to_char($1, 'DDD') WHEN n = 'k' THEN pg_catalog.to_char($1, 'FMHH24') WHEN n = 'l' THEN pg_catalog.to_char($1, 'FMHH12') WHEN n = 'M' THEN pg_catalog.to_char($1, 'FMMonth') WHEN n = 'm' THEN pg_catalog.to_char($1, 'MM') WHEN n = 'p' THEN pg_catalog.to_char($1, 'AM') WHEN n = 'r' THEN pg_catalog.to_char($1, 'HH12:MI:SS AM') WHEN n = 'S' THEN pg_catalog.to_char($1, 'SS') WHEN n = 's' THEN pg_catalog.to_char($1, 'SS') WHEN n = 'T' THEN pg_catalog.to_char($1, 'HH24:MI:SS') WHEN n = 'U' THEN pg_catalog.to_char($1, '?') WHEN n = 'u' THEN pg_catalog.to_char($1, '?') WHEN n = 'V' THEN pg_catalog.to_char($1, '?') WHEN n = 'v' THEN pg_catalog.to_char($1, '?') WHEN n = 'W' THEN pg_catalog.to_char($1, 'FMDay') WHEN n = 'w' THEN EXTRACT(DOW FROM $1)::text WHEN n = 'X' THEN pg_catalog.to_char($1, '?') WHEN n = 'x' THEN pg_catalog.to_char($1, '?') WHEN n = 'Y' THEN pg_catalog.to_char($1, 'YYYY') WHEN n = 'y' THEN pg_catalog.to_char($1, 'YY') WHEN n = '%' THEN pg_catalog.to_char($1, '%') ELSE NULL END; temp := temp operator(pg_catalog.||) res; i := i + 2; ELSE temp = temp operator(pg_catalog.||) c; i := i + 1; END IF; END LOOP; RETURN temp; END $$ IMMUTABLE STRICT LANGUAGE PLPGSQL;


CREATE OR REPLACE FUNCTION YEAR(TIMESTAMP without TIME ZONE) RETURNS INTEGER AS $$ SELECT EXTRACT(YEAR FROM $1)::INTEGER; $$ LANGUAGE SQL IMMUTABLE;

CREATE OR REPLACE FUNCTION YEAR(TIMESTAMP WITH TIME ZONE) RETURNS INTEGER AS $$ SELECT EXTRACT(YEAR FROM $1)::INTEGER; $$ LANGUAGE SQL STABLE;

CREATE OR REPLACE FUNCTION YEAR(DATE) RETURNS INTEGER AS $$ SELECT EXTRACT(YEAR FROM $1)::INTEGER; $$ LANGUAGE SQL IMMUTABLE;


CREATE OR REPLACE FUNCTION MONTH(TIMESTAMP without TIME ZONE) RETURNS INTEGER AS $$ SELECT EXTRACT(MONTH FROM $1)::INTEGER; $$ LANGUAGE SQL IMMUTABLE;

CREATE OR REPLACE FUNCTION MONTH(TIMESTAMP WITH TIME ZONE) RETURNS INTEGER AS $$ SELECT EXTRACT(MONTH FROM $1)::INTEGER; $$ LANGUAGE SQL STABLE;
Expand Down

0 comments on commit f0a15ff

Please sign in to comment.