Fix: Postgresql compatibility

This commit is contained in:
Laurent Destailleur 2010-05-07 21:02:48 +00:00
parent 7c496dcffe
commit 65e86e9c29
10 changed files with 156 additions and 44 deletions

View File

@ -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)

View File

@ -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";

View File

@ -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;
}

View File

@ -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);
}
}
}

View File

@ -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);

View File

@ -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);

View File

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com>
* Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2010 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2010 Regis Houssin <regis@dolibarr.fr>
* Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
*
@ -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)

View File

@ -94,7 +94,7 @@ function print_actions_filter($form,$canedit,$status,$year,$month,$day,$showbort
print '<td nowrap="nowrap">';
print $langs->trans("Project").' &nbsp; ';
print '</td><td nowrap="nowrap">';
select_projects($socid,$pid,'projectid');
select_projects($socid?$socid:-1,$pid,'projectid');
print '</td></tr>';
}

View File

@ -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

View File

@ -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);