From 65e86e9c291662e9b1b8661c9c2f936727805432 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 7 May 2010 21:02:48 +0000 Subject: [PATCH] Fix: Postgresql compatibility --- htdocs/comm/action/index.php | 36 +++--- htdocs/comm/action/listactions.php | 4 +- htdocs/core/class/menubase.class.php | 3 +- .../modules/DolibarrModules.class.php | 19 +-- .../install/mysql/migration/2.8.0-2.9.0.sql | 6 + htdocs/install/mysql/tables/llx_menu.key.sql | 2 +- htdocs/install/upgrade2.php | 6 +- htdocs/lib/agenda.lib.php | 2 +- htdocs/lib/databases/pgsql.lib.php | 113 +++++++++++++++++- htdocs/lib/project.lib.php | 9 +- 10 files changed, 156 insertions(+), 44 deletions(-) diff --git a/htdocs/comm/action/index.php b/htdocs/comm/action/index.php index d30eccac1e5..5e0f7c2cb0b 100644 --- a/htdocs/comm/action/index.php +++ b/htdocs/comm/action/index.php @@ -187,29 +187,29 @@ $sql.= ' AND u.entity in (0,'.$conf->entity.')'; // To limit to entity if ($pid) $sql.=" AND a.fk_project=".addslashes($pid); if ($_GET["action"] == 'show_day') { - $sql.= ' AND ('; - $sql.= ' (datep BETWEEN '.$db->idate(dol_mktime(0,0,0,$month,$_GET["day"],$year)); - $sql.= ' AND '.$db->idate(dol_mktime(23,59,59,$month,$_GET["day"],$year)).')'; - $sql.= ' OR '; - $sql.= ' (datep2 BETWEEN '.$db->idate(dol_mktime(0,0,0,$month,$_GET["day"],$year)); - $sql.= ' AND '.$db->idate(dol_mktime(23,59,59,$month,$_GET["day"],$year)).')'; - $sql.= ' OR '; - $sql.= ' (datep < '.$db->idate(dol_mktime(0,0,0,$month,$_GET["day"],$year)); - $sql.= ' AND datep2 > '.$db->idate(dol_mktime(23,59,59,$month,$_GET["day"],$year)).')'; + $sql.= " AND ("; + $sql.= " (datep BETWEEN '".$db->idate(dol_mktime(0,0,0,$month,$_GET["day"],$year))."'"; + $sql.= " AND '".$db->idate(dol_mktime(23,59,59,$month,$_GET["day"],$year))."')"; + $sql.= " OR "; + $sql.= " (datep2 BETWEEN '".$db->idate(dol_mktime(0,0,0,$month,$_GET["day"],$year))."'"; + $sql.= " AND '".$db->idate(dol_mktime(23,59,59,$month,$_GET["day"],$year))."')"; + $sql.= " OR "; + $sql.= " (datep < '".$db->idate(dol_mktime(0,0,0,$month,$_GET["day"],$year))."'"; + $sql.= " AND datep2 > '".$db->idate(dol_mktime(23,59,59,$month,$_GET["day"],$year))."')"; $sql.= ')'; } else { // To limit array - $sql.= ' AND ('; - $sql.= ' (datep BETWEEN '.$db->idate(dol_mktime(0,0,0,$month,1,$year)-(60*60*24*7)); // Start 7 days before - $sql.= ' AND '.$db->idate(dol_mktime(23,59,59,$month,28,$year)+(60*60*24*10)).')'; // End 7 days after + 3 to go from 28 to 31 - $sql.= ' OR '; - $sql.= ' (datep2 BETWEEN '.$db->idate(dol_mktime(0,0,0,$month,1,$year)-(60*60*24*7)); - $sql.= ' AND '.$db->idate(dol_mktime(23,59,59,$month,28,$year)+(60*60*24*10)).')'; - $sql.= ' OR '; - $sql.= ' (datep < '.$db->idate(dol_mktime(0,0,0,$month,1,$year)-(60*60*24*7)); - $sql.= ' AND datep2 > '.$db->idate(dol_mktime(23,59,59,$month,28,$year)+(60*60*24*10)).')'; + $sql.= " AND ("; + $sql.= " (datep BETWEEN '".$db->idate(dol_mktime(0,0,0,$month,1,$year)-(60*60*24*7))."'"; // Start 7 days before + $sql.= " AND '".$db->idate(dol_mktime(23,59,59,$month,28,$year)+(60*60*24*10))."')"; // End 7 days after + 3 to go from 28 to 31 + $sql.= " OR "; + $sql.= " (datep2 BETWEEN '".$db->idate(dol_mktime(0,0,0,$month,1,$year)-(60*60*24*7))."'"; + $sql.= " AND '".$db->idate(dol_mktime(23,59,59,$month,28,$year)+(60*60*24*10))."')"; + $sql.= " OR "; + $sql.= " (datep < '".$db->idate(dol_mktime(0,0,0,$month,1,$year)-(60*60*24*7))."'"; + $sql.= " AND datep2 > '".$db->idate(dol_mktime(23,59,59,$month,28,$year)+(60*60*24*10))."')"; $sql.= ')'; } if ($filtera > 0 || $filtert > 0 || $filterd > 0) diff --git a/htdocs/comm/action/listactions.php b/htdocs/comm/action/listactions.php index 22fcad7990c..b51fa9d8b32 100644 --- a/htdocs/comm/action/listactions.php +++ b/htdocs/comm/action/listactions.php @@ -132,8 +132,8 @@ $sql.= " ud.login as logindone, ud.rowid as useriddone,"; $sql.= " sp.name, sp.firstname"; $sql.= " FROM (".MAIN_DB_PREFIX."c_actioncomm as c,"; if (!$user->rights->societe->client->voir && !$socid) $sql.= " ".MAIN_DB_PREFIX."societe_commerciaux as sc,"; -$sql.= " ".MAIN_DB_PREFIX."actioncomm as a,"; -$sql.= " ".MAIN_DB_PREFIX.'user as u)'; +$sql.= " ".MAIN_DB_PREFIX.'user as u,'; +$sql.= " ".MAIN_DB_PREFIX."actioncomm as a)"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as sp ON a.fk_contact = sp.rowid"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as ua ON a.fk_user_author = ua.rowid"; diff --git a/htdocs/core/class/menubase.class.php b/htdocs/core/class/menubase.class.php index 316d4fe6e23..90dc93c740a 100644 --- a/htdocs/core/class/menubase.class.php +++ b/htdocs/core/class/menubase.class.php @@ -70,7 +70,7 @@ class Menubase /** - * \brief Create in database + * \brief Create menu entry into database * \param user User that create * \return int <0 si ko, >0 si ok */ @@ -141,6 +141,7 @@ class Menubase if ($resql) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."menu"); + dol_syslog("Menubase::create record added has rowid=".$this->id, LOG_DEBUG); return $this->id; } diff --git a/htdocs/includes/modules/DolibarrModules.class.php b/htdocs/includes/modules/DolibarrModules.class.php index 1fdabd12459..f1976fc57f3 100644 --- a/htdocs/includes/modules/DolibarrModules.class.php +++ b/htdocs/includes/modules/DolibarrModules.class.php @@ -881,17 +881,17 @@ class DolibarrModules { if (strlen($r_subperms) ) { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def "; - $sql .= " (id, entity, libelle, module, type, bydefault, perms, subperms)"; - $sql .= " VALUES "; - $sql .= "(".$r_id.",".$conf->entity.",'".addslashes($r_desc)."','".$r_modul."','".$r_type."',".$r_def.",'".$r_perms."','".$r_subperms."')"; + $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def"; + $sql.= " (id, entity, libelle, module, type, bydefault, perms, subperms)"; + $sql.= " VALUES "; + $sql.= "(".$r_id.",".$conf->entity.",'".addslashes($r_desc)."','".$r_modul."','".$r_type."',".$r_def.",'".$r_perms."','".$r_subperms."')"; } else { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def "; - $sql .= " (id, entity, libelle, module, type, bydefault, perms)"; - $sql .= " VALUES "; - $sql .= "(".$r_id.",".$conf->entity.",'".addslashes($r_desc)."','".$r_modul."','".$r_type."',".$r_def.",'".$r_perms."')"; + $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def"; + $sql.= " (id, entity, libelle, module, type, bydefault, perms)"; + $sql.= " VALUES "; + $sql.= "(".$r_id.",".$conf->entity.",'".addslashes($r_desc)."','".$r_modul."','".$r_type."',".$r_def.",'".$r_perms."')"; } } else @@ -903,7 +903,7 @@ class DolibarrModules } dol_syslog("DolibarrModules::insert_permissions sql=".$sql, LOG_DEBUG); - $resql=$this->db->query($sql); + $resql=$this->db->query($sql,1); if (! $resql) { if ($this->db->errno() != "DB_ERROR_RECORD_ALREADY_EXISTS") @@ -913,6 +913,7 @@ class DolibarrModules $err++; break; } + else dol_syslog("DolibarrModules::insert_permissions record already exists", LOG_INFO); } } } diff --git a/htdocs/install/mysql/migration/2.8.0-2.9.0.sql b/htdocs/install/mysql/migration/2.8.0-2.9.0.sql index d1fb2b05788..b05f724c470 100755 --- a/htdocs/install/mysql/migration/2.8.0-2.9.0.sql +++ b/htdocs/install/mysql/migration/2.8.0-2.9.0.sql @@ -193,3 +193,9 @@ UPDATE llx_product SET canvas = 'service@product' WHERE fk_product_type = 1 AND UPDATE llx_product SET canvas = 'livre@droitpret' WHERE canvas = 'livre'; UPDATE llx_product SET canvas = 'livrecontrat@droitpret' WHERE canvas = 'livrecontrat'; UPDATE llx_product SET canvas = 'livrecouverture@droitpret' WHERE canvas = 'livrecouverture'; + + +ALTER TABLE llx_menu DROP INDEX idx_menu_uk_menu; + +ALTER TABLE llx_menu ADD UNIQUE INDEX idx_menu_uk_menu (menu_handler, fk_menu, position, url, entity); + diff --git a/htdocs/install/mysql/tables/llx_menu.key.sql b/htdocs/install/mysql/tables/llx_menu.key.sql index cd360e2e68a..dc9732c0719 100644 --- a/htdocs/install/mysql/tables/llx_menu.key.sql +++ b/htdocs/install/mysql/tables/llx_menu.key.sql @@ -23,5 +23,5 @@ ALTER TABLE llx_menu ADD INDEX idx_menu_menuhandler_type (menu_handler, type); -ALTER TABLE llx_menu ADD UNIQUE INDEX idx_menu_uk_menu (menu_handler, fk_menu, url, entity); +ALTER TABLE llx_menu ADD UNIQUE INDEX idx_menu_uk_menu (menu_handler, fk_menu, position, url, entity); diff --git a/htdocs/install/upgrade2.php b/htdocs/install/upgrade2.php index a4d3d5b4d53..d5995d9348b 100644 --- a/htdocs/install/upgrade2.php +++ b/htdocs/install/upgrade2.php @@ -1,6 +1,6 @@ - * Copyright (C) 2005-2009 Laurent Destailleur + * Copyright (C) 2005-2010 Laurent Destailleur * Copyright (C) 2005-2010 Regis Houssin * Copyright (C) 2010 Juanjo Menent * @@ -3103,8 +3103,8 @@ function migrate_shipping_delivery2($db,$langs,$conf) $sqlSelect.= " AND el.targettype = 'delivery'"; $sqlSelect.= " AND e.rowid = el.fk_source AND el.sourcetype = 'shipping'"; // Add condition to know if we never migrate this record - $sqlSelect.= " AND (l.ref_customer IS NULL or l.ref_customer = '')"; - $sqlSelect.= " AND (l.date_delivery IS NULL or l.date_delivery = '')"; + $sqlSelect.= " AND (l.ref_customer IS NULL".($db->type!='pgsql'?" or l.ref_customer = ''":"").")"; + $sqlSelect.= " AND (l.date_delivery IS NULL".($db->type!='pgsql'?" or l.date_delivery = ''":"").")"; $resql = $db->query($sqlSelect); if ($resql) diff --git a/htdocs/lib/agenda.lib.php b/htdocs/lib/agenda.lib.php index 0d64880b3b0..9f35112b22f 100644 --- a/htdocs/lib/agenda.lib.php +++ b/htdocs/lib/agenda.lib.php @@ -94,7 +94,7 @@ function print_actions_filter($form,$canedit,$status,$year,$month,$day,$showbort print ''; print $langs->trans("Project").'   '; print ''; - select_projects($socid,$pid,'projectid'); + select_projects($socid?$socid:-1,$pid,'projectid'); print ''; } diff --git a/htdocs/lib/databases/pgsql.lib.php b/htdocs/lib/databases/pgsql.lib.php index 594006d41cb..9061d172a0b 100644 --- a/htdocs/lib/databases/pgsql.lib.php +++ b/htdocs/lib/databases/pgsql.lib.php @@ -163,9 +163,6 @@ class DoliDb } if ($line != "") { - # We remove end of request "AFTER fieldxxx" - $line=preg_replace('/AFTER [a-z_]+/i','',$line); - # we are inside create table statement so lets process datatypes if (preg_match('/(ISAM|innodb)/i',$line)) { # end of create table sequence $line=preg_replace('/\)[\s\t]*type=(MyISAM|innodb);/i',');',$line); @@ -218,6 +215,12 @@ class DoliDb $line=preg_replace('/unique index\s*\((\w+\s*,\s*\w+)\)/i','UNIQUE\(\\1\)',$line); } + # We remove end of requests "AFTER fieldxxx" + $line=preg_replace('/AFTER [a-z0-9_]+/i','',$line); + + # We remove start of requests "ALTER TABLE tablexxx" if this is a DROP INDEX + $line=preg_replace('/ALTER TABLE [a-z0-9_]+ DROP INDEX/i','DROP INDEX',$line); + # alter table add primary key (field1, field2 ...) -> We remove the primary key name not accepted by PostGreSQL # ALTER TABLE llx_dolibarr_modules ADD PRIMARY KEY pk_dolibarr_modules (numero, entity); if (preg_match('/ALTER\s+TABLE\s*(.*)\s*ADD\s+PRIMARY\s+KEY\s*(.*)\s*\((.*)$/i',$line,$reg)) @@ -812,8 +815,14 @@ class DoliDb */ function last_insert_id($tab,$fieldid='rowid') { - $result = pg_query($this->db,"SELECT MAX(".$fieldid.") FROM ".$tab." ;"); - $nbre = pg_num_rows($result); + //$result = pg_query($this->db,"SELECT MAX(".$fieldid.") FROM ".$tab); + $result = pg_query($this->db,"SELECT currval('".$tab."_".$fieldid."_seq')"); + if (! $result) + { + print pg_last_error($this->db); + exit; + } + //$nbre = pg_num_rows($result); $row = pg_fetch_result($result,0,0); return $row; } @@ -935,6 +944,83 @@ class DoliDb } + /** + * \brief Cree une table + * \param table Nom de la table + * \param fields Tableau associatif [nom champ][tableau des descriptions] + * \param primary_key Nom du champ qui sera la clef primaire + * \param unique_keys Tableau associatifs Nom de champs qui seront clef unique => valeur + * \param fulltext Tableau des Nom de champs qui seront indexes en fulltext + * \param key Tableau des champs cles noms => valeur + * \param type Type de la table + * \return int <0 si KO, >=0 si OK + * TODO + */ + function DDLCreateTable($table,$fields,$primary_key,$type,$unique_keys="",$fulltext_keys="",$keys="") + { + // cles recherchees dans le tableau des descriptions (fields) : type,value,attribute,null,default,extra + // ex. : $fields['rowid'] = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment'); + $sql = "create table ".$table."("; + $i=0; + foreach($fields as $field_name => $field_desc) + { + $sqlfields[$i] = $field_name." "; + $sqlfields[$i] .= $field_desc['type']; + if( preg_match("/^[^\s]/i",$field_desc['value'])) + $sqlfields[$i] .= "(".$field_desc['value'].")"; + else if( preg_match("/^[^\s]/i",$field_desc['attribute'])) + $sqlfields[$i] .= " ".$field_desc['attribute']; + else if( preg_match("/^[^\s]/i",$field_desc['default'])) + { + if(preg_match("/null/i",$field_desc['default'])) + $sqlfields[$i] .= " default ".$field_desc['default']; + else + $sqlfields[$i] .= " default '".$field_desc['default']."'"; + } + else if( preg_match("/^[^\s]/i",$field_desc['null'])) + $sqlfields[$i] .= " ".$field_desc['null']; + + else if( preg_match("/^[^\s]/i",$field_desc['extra'])) + $sqlfields[$i] .= " ".$field_desc['extra']; + $i++; + } + if($primary_key != "") + $pk = "primary key(".$primary_key.")"; + + if($unique_keys != "") + { + $i = 0; + foreach($unique_keys as $key => $value) + { + $sqluq[$i] = "UNIQUE KEY '".$key."' ('".$value."')"; + $i++; + } + } + if($keys != "") + { + $i = 0; + foreach($keys as $key => $value) + { + $sqlk[$i] = "KEY ".$key." (".$value.")"; + $i++; + } + } + $sql .= implode(',',$sqlfields); + if($primary_key != "") + $sql .= ",".$pk; + if($unique_keys != "") + $sql .= ",".implode(',',$sqluq); + if($keys != "") + $sql .= ",".implode(',',$sqlk); + $sql .=") type=".$type; + + dol_syslog($sql,LOG_DEBUG); + if(! $this -> query($sql)) + return -1; + else + return 1; + } + /** * \brief Create a user * \param dolibarr_main_db_host Ip serveur @@ -957,6 +1043,23 @@ class DoliDb return 1; } + /** + * \brief decrit une table dans une database. + * \param table Nom de la table + * \param field Optionnel : Nom du champ si l'on veut la desc d'un champ + * \return resource + */ + function DDLDescTable($table,$field="") + { + $sql ="SELECT attname FROM pg_attribute, pg_type WHERE typname = '".$table."' AND attrelid = typrelid"; + $sql.=" AND attname NOT IN ('cmin', 'cmax', 'ctid', 'oid', 'tableoid', 'xmin', 'xmax')"; + if ($field) $sql.= " AND attname = '".$field."'"; + + dol_syslog($sql,LOG_DEBUG); + $this->results = $this->query($sql); + return $this->results; + } + /** * \brief Insert a new field in table * \param table Nom de la table diff --git a/htdocs/lib/project.lib.php b/htdocs/lib/project.lib.php index 8311aab1ab0..1a818d7d5eb 100644 --- a/htdocs/lib/project.lib.php +++ b/htdocs/lib/project.lib.php @@ -159,20 +159,21 @@ function task_prepare_head($object) /** * \brief Show a combo list with projects qualified for a third party) - * \param socid Id third party + * \param socid Id third party (-1=all, 0=projects not linked to a third party, id=projects not linked or linked to third party id) * \param selected Id project preselected * \param htmlname Nom de la zone html * \return int Nbre de projet si ok, <0 si ko */ -function select_projects($socid, $selected='', $htmlname='projectid') +function select_projects($socid=-1, $selected='', $htmlname='projectid') { global $db,$user,$conf,$langs; // On recherche les projets $sql = 'SELECT p.rowid, p.ref, p.title, p.fk_soc, p.fk_statut, p.public'; $sql.= ' FROM '.MAIN_DB_PREFIX .'projet as p'; - $sql.= " WHERE (p.fk_soc=".($socid?$socid:'0')." OR p.fk_soc IS NULL)"; - $sql.= " AND p.entity = ".$conf->entity; + $sql.= " WHERE p.entity = ".$conf->entity; + if ($socid == 0) $sql.= " AND (p.fk_soc=0 OR p.fk_soc IS NULL)"; + if ($socid > 0) $sql.= " AND (p.fk_soc=".$socid." OR p.fk_soc='0' OR p.fk_soc IS NULL)"; $sql.= " ORDER BY p.title ASC"; dol_syslog("project.lib::select_projects sql=".$sql);