Fix: pgsql compatibility

This commit is contained in:
eldy 2011-09-11 01:26:10 +02:00
parent 9f5219ee77
commit cf9f8f2352

View File

@ -57,6 +57,7 @@ class Categorie
/** /**
* Constructor * Constructor
*
* @param DB acces base de donnees * @param DB acces base de donnees
* @param id id de la categorie * @param id id de la categorie
*/ */
@ -70,6 +71,7 @@ class Categorie
/** /**
* Load category into memory from database * Load category into memory from database
* @param id id of category * @param id id of category
*/ */
function fetch($id) function fetch($id)
@ -79,7 +81,7 @@ class Categorie
$sql.= " WHERE rowid = ".$id; $sql.= " WHERE rowid = ".$id;
dol_syslog("Categorie::fetch sql=".$sql); dol_syslog("Categorie::fetch sql=".$sql);
$resql = $this->db->query ($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{ {
$res = $this->db->fetch_array($resql); $res = $this->db->fetch_array($resql);
@ -95,16 +97,16 @@ class Categorie
} }
else else
{ {
dol_print_error ($this->db); dol_print_error($this->db);
return -1; return -1;
} }
$sql = "SELECT fk_categorie_mere"; $sql = "SELECT fk_categorie_mere";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie_association"; $sql.= " FROM ".MAIN_DB_PREFIX."categorie_association";
$sql.= " WHERE fk_categorie_fille = '".$id."'"; $sql.= " WHERE fk_categorie_fille = ".$id;
dol_syslog("Categorie::fetch sql=".$sql); dol_syslog("Categorie::fetch sql=".$sql);
$resql = $this->db->query ($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{ {
$res = $this->db->fetch_array($resql); $res = $this->db->fetch_array($resql);
@ -114,13 +116,14 @@ class Categorie
} }
else else
{ {
dol_print_error ($this->db); dol_print_error($this->db);
return -1; return -1;
} }
} }
/** /**
* Add category into database * Add category into database
*
* @return int -1 : erreur SQL * @return int -1 : erreur SQL
* -2 : nouvel ID inconnu * -2 : nouvel ID inconnu
* -3 : categorie invalide * -3 : categorie invalide
@ -159,10 +162,10 @@ class Categorie
//$sql.= ",".$this->parentId; //$sql.= ",".$this->parentId;
$sql.= ")"; $sql.= ")";
$res = $this->db->query ($sql); $res = $this->db->query($sql);
if ($res) if ($res)
{ {
$id = $this->db->last_insert_id (MAIN_DB_PREFIX."categorie"); $id = $this->db->last_insert_id(MAIN_DB_PREFIX."categorie");
if ($id > 0) if ($id > 0)
{ {
@ -192,13 +195,14 @@ class Categorie
} }
else else
{ {
dol_print_error ($this->db); dol_print_error($this->db);
return -1; return -1;
} }
} }
/** /**
* Update category * Update category
*
* @return int 1 : OK * @return int 1 : OK
* -1 : SQL error * -1 : SQL error
* -2 : invalid category * -2 : invalid category
@ -217,12 +221,12 @@ class Categorie
$this->error=$langs->trans("ImpossibleUpdateCat"); $this->error=$langs->trans("ImpossibleUpdateCat");
$this->error.=" : ".$langs->trans("CategoryExistsAtSameLevel"); $this->error.=" : ".$langs->trans("CategoryExistsAtSameLevel");
return -1; return -1;
} }
$this->db->begin(); $this->db->begin();
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'categorie_association'; $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'categorie_association';
$sql .= ' WHERE fk_categorie_fille = "'.$this->id.'"'; $sql.= ' WHERE fk_categorie_fille = '.$this->id;
dol_syslog("Categorie::update sql=".$sql); dol_syslog("Categorie::update sql=".$sql);
if (! $this->db->query($sql)) if (! $this->db->query($sql))
@ -235,7 +239,7 @@ class Categorie
if($this->id_mere !="" && $this->id_mere!=$this->id) if($this->id_mere !="" && $this->id_mere!=$this->id)
{ {
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'categorie_association(fk_categorie_mere,fk_categorie_fille)'; $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'categorie_association(fk_categorie_mere,fk_categorie_fille)';
$sql .= ' VALUES ("'.$this->id_mere.'","'.$this->id.'")'; $sql.= ' VALUES ('.$this->id_mere.', '.$this->id.')';
dol_syslog("Categorie::update sql=".$sql); dol_syslog("Categorie::update sql=".$sql);
if (! $this->db->query($sql)) if (! $this->db->query($sql))
@ -284,6 +288,7 @@ class Categorie
/** /**
* Delete a category from database * Delete a category from database
*
* @param user Object user that ask to delete * @param user Object user that ask to delete
*/ */
function delete($user) function delete($user)
@ -544,21 +549,21 @@ class Categorie
$sql = "SELECT fk_categorie_fille FROM ".MAIN_DB_PREFIX."categorie_association "; $sql = "SELECT fk_categorie_fille FROM ".MAIN_DB_PREFIX."categorie_association ";
$sql .= "WHERE fk_categorie_mere = ".$this->id; $sql .= "WHERE fk_categorie_mere = ".$this->id;
$res = $this->db->query ($sql); $res = $this->db->query($sql);
if ($res) if ($res)
{ {
$cats = array (); $cats = array ();
while ($rec = $this->db->fetch_array ($res)) while ($rec = $this->db->fetch_array($res))
{ {
$cat = new Categorie ($this->db, $rec['fk_categorie_fille']); $cat = new Categorie($this->db, $rec['fk_categorie_fille']);
$cats[] = $cat; $cats[] = $cat;
} }
return $cats; return $cats;
} }
else else
{ {
dol_print_error ($this->db); dol_print_error($this->db);
return -1; return -1;
} }
} }
@ -567,13 +572,13 @@ class Categorie
/** /**
* retourne la description d'une categorie * retourne la description d'une categorie
*/ */
function get_desc ($cate) function get_desc($cate)
{ {
$sql = "SELECT description FROM ".MAIN_DB_PREFIX."categorie "; $sql = "SELECT description FROM ".MAIN_DB_PREFIX."categorie ";
$sql .= "WHERE rowid = '".$cate."'"; $sql .= "WHERE rowid = ".$cate;
$res = $this->db->query ($sql); $res = $this->db->query($sql);
$n = $this->db->fetch_array ($res); $n = $this->db->fetch_array($res);
return ($n[0]); return ($n[0]);
} }
@ -581,12 +586,12 @@ class Categorie
/** /**
* La categorie $fille est-elle une fille de cette categorie ? * La categorie $fille est-elle une fille de cette categorie ?
*/ */
function is_fille ($fille) function is_fille($fille)
{ {
$sql = "SELECT count(fk_categorie_fille) FROM ".MAIN_DB_PREFIX."categorie_association "; $sql = "SELECT count(fk_categorie_fille) FROM ".MAIN_DB_PREFIX."categorie_association ";
$sql .= "WHERE fk_categorie_mere = ".$this->id." AND fk_categorie_fille = ".$fille->id; $sql .= "WHERE fk_categorie_mere = ".$this->id." AND fk_categorie_fille = ".$fille->id;
$res = $this->db->query ($sql); $res = $this->db->query($sql);
$n = $this->db->fetch_array($res); $n = $this->db->fetch_array($res);
@ -627,7 +632,7 @@ class Categorie
} }
else else
{ {
dol_print_error ($this->db); dol_print_error($this->db);
return -1; return -1;
} }
@ -660,7 +665,7 @@ class Categorie
} }
else else
{ {
dol_print_error ($this->db); dol_print_error($this->db);
return -1; return -1;
} }
@ -778,20 +783,20 @@ class Categorie
{ {
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie"; $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
$res = $this->db->query ($sql); $res = $this->db->query($sql);
if ($res) if ($res)
{ {
$cats = array (); $cats = array ();
while ($record = $this->db->fetch_array ($res)) while ($record = $this->db->fetch_array($res))
{ {
$cat = new Categorie ($this->db, $record['rowid']); $cat = new Categorie($this->db, $record['rowid']);
$cats[$record['rowid']] = $cat; $cats[$record['rowid']] = $cat;
} }
return $cats; return $cats;
} }
else else
{ {
dol_print_error ($this->db); dol_print_error($this->db);
return -1; return -1;
} }
} }
@ -804,7 +809,7 @@ class Categorie
{ {
$sql = "SELECT count(rowid)"; $sql = "SELECT count(rowid)";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie"; $sql.= " FROM ".MAIN_DB_PREFIX."categorie";
$res = $this->db->query ($sql); $res = $this->db->query($sql);
if ($res) if ($res)
{ {
$res = $this->db->fetch_array($res); $res = $this->db->fetch_array($res);
@ -866,7 +871,7 @@ class Categorie
} }
else else
{ {
dol_print_error ($this->db); dol_print_error($this->db);
return -1; return -1;
} }
} }
@ -874,22 +879,22 @@ class Categorie
/** /**
* \brief Retourne les categories de premier niveau (qui ne sont pas filles) * \brief Retourne les categories de premier niveau (qui ne sont pas filles)
*/ */
function get_main_categories () function get_main_categories()
{ {
$allcats = $this->get_all_categories (); $allcats = $this->get_all_categories();
$maincats = array (); $maincats = array ();
$filles = array (); $filles = array ();
$sql = "SELECT fk_categorie_fille FROM ".MAIN_DB_PREFIX."categorie_association"; $sql = "SELECT fk_categorie_fille FROM ".MAIN_DB_PREFIX."categorie_association";
$res = $this->db->query ($sql); $res = $this->db->query($sql);
while ($res = $this->db->fetch_array ($res)) while ($res = $this->db->fetch_array($res))
{ {
$filles[] = $res['fk_categorie_fille']; $filles[] = $res['fk_categorie_fille'];
} }
foreach ($allcats as $cat) foreach ($allcats as $cat)
{ {
if (!in_array ($cat->id, $filles)) if (! in_array($cat->id, $filles))
{ {
$maincats[] = $cat; $maincats[] = $cat;
} }
@ -943,7 +948,7 @@ class Categorie
{ {
if(sizeof($way)<$primary_way["taille"] || $primary_way["taille"]<0) if(sizeof($way)<$primary_way["taille"] || $primary_way["taille"]<0)
{ {
$primary_way["taille"] = sizeOf($way); $primary_way["taille"] = count($way);
$primary_way["chemin"] = $way; $primary_way["chemin"] = $way;
} }
} }
@ -978,26 +983,26 @@ class Categorie
/** /**
* Retourne un tableau contenant la liste des categories meres * Retourne un tableau contenant la liste des categories meres
*/ */
function get_meres () function get_meres()
{ {
$meres = array (); $meres = array();
$sql = "SELECT fk_categorie_mere FROM ".MAIN_DB_PREFIX."categorie_association "; $sql = "SELECT fk_categorie_mere FROM ".MAIN_DB_PREFIX."categorie_association ";
$sql .= "WHERE fk_categorie_fille = ".$this->id; $sql .= "WHERE fk_categorie_fille = ".$this->id;
$res = $this->db->query ($sql); $res = $this->db->query($sql);
if ($res) if ($res)
{ {
while ($cat = $this->db->fetch_array ($res)) while ($cat = $this->db->fetch_array($res))
{ {
$meres[] = new Categorie ($this->db, $cat['fk_categorie_mere']); $meres[] = new Categorie($this->db, $cat['fk_categorie_mere']);
} }
return $meres; return $meres;
} }
else else
{ {
dol_print_error ($this->db); dol_print_error($this->db);
return -1; return -1;
} }
} }
@ -1010,9 +1015,9 @@ class Categorie
{ {
$ways = array (); $ways = array ();
foreach ($this->get_meres () as $mere) foreach ($this->get_meres() as $mere)
{ {
foreach ($mere->get_all_ways () as $way) foreach ($mere->get_all_ways() as $way)
{ {
$w = $way; $w = $way;
$w[] = $this; $w[] = $this;
@ -1021,7 +1026,7 @@ class Categorie
} }
} }
if (sizeof ($ways) == 0) if (count($ways) == 0)
$ways[0][0] = $this; $ways[0][0] = $this;
return $ways; return $ways;
@ -1046,21 +1051,21 @@ class Categorie
$sql = "SELECT ct.fk_categorie"; $sql = "SELECT ct.fk_categorie";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie_".$type." as ct"; $sql.= " FROM ".MAIN_DB_PREFIX."categorie_".$type." as ct";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie as c ON ct.fk_categorie = c.rowid"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie as c ON ct.fk_categorie = c.rowid";
$sql.= " WHERE ct.fk_".$table." = ".$id." AND c.type = ".$typeid; $sql.= " WHERE ct.fk_".$table." = ".$id." AND c.type = ".$typeid;
$res = $this->db->query ($sql); $res = $this->db->query($sql);
if ($res) if ($res)
{ {
while ($cat = $this->db->fetch_array ($res)) while ($cat = $this->db->fetch_array($res))
{ {
$cats[] = new Categorie ($this->db, $cat['fk_categorie']); $cats[] = new Categorie($this->db, $cat['fk_categorie']);
} }
return $cats; return $cats;
} }
else else
{ {
dol_print_error ($this->db); dol_print_error($this->db);
return -1; return -1;
} }
} }
@ -1081,7 +1086,7 @@ class Categorie
{ {
if (! $exact) if (! $exact)
{ {
$nom = '%'.str_replace ('*', '%', $nom).'%'; $nom = '%'.str_replace('*', '%', $nom).'%';
} }
$sql.= "AND label LIKE '".$nom."'"; $sql.= "AND label LIKE '".$nom."'";
} }
@ -1090,12 +1095,12 @@ class Categorie
$sql.="AND rowid = '".$id."'"; $sql.="AND rowid = '".$id."'";
} }
$res = $this->db->query ($sql); $res = $this->db->query($sql);
if ($res) if ($res)
{ {
while ($id = $this->db->fetch_array ($res)) while ($id = $this->db->fetch_array($res))
{ {
$cats[] = new Categorie ($this->db, $id['rowid']); $cats[] = new Categorie($this->db, $id['rowid']);
} }
return $cats; return $cats;