Enhance support of default value in extrafields

This commit is contained in:
Laurent Destailleur
2017-09-02 15:12:21 +02:00
parent 9923c87fd8
commit 3beee569a6
5 changed files with 64 additions and 25 deletions

View File

@@ -1088,8 +1088,26 @@ class DoliDBPgsql extends DoliDB
$sql.="(".$field_desc['value'].")";
}
// TODO May not work with pgsql. May need to run a second request. If it works, just remove the comment
if ($field_desc['null'] == 'not null' || $field_desc['null'] == 'NOT NULL') $sql.=" NOT NULL";
if ($field_desc['null'] == 'not null' || $field_desc['null'] == 'NOT NULL')
{
// We will try to change format of column to NOT NULL. To be sure the ALTER works, we try to update fields that are NULL
if ($field_desc['type'] == 'varchar' || $field_desc['type'] == 'text')
{
$sqlbis="UPDATE ".$table." SET ".$field_name." = '".$this->escape($field_desc['default'] ? $field_desc['default'] : '')."' WHERE ".$field_name." IS NULL";
$this->query($sqlbis);
}
elseif ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int')
{
$sqlbis="UPDATE ".$table." SET ".$field_name." = ".((int) $this->escape($field_desc['default'] ? $field_desc['default'] : 0))." WHERE ".$field_name." IS NULL";
$this->query($sqlbis);
}
}
if ($field_desc['default'])
{
if ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') $sql.=" DEFAULT ".$this->escape($field_desc['default']);
elseif ($field_desc['type'] == 'text') $sql.=" DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields
}
dol_syslog($sql,LOG_DEBUG);
if (! $this->query($sql))