Clean method DDLCreateTable (used by modulebuilder only) #28996

This commit is contained in:
Laurent Destailleur
2024-03-21 12:10:09 +01:00
parent ae10eedf2a
commit ce2959b022
4 changed files with 117 additions and 80 deletions

View File

@@ -825,55 +825,66 @@ class DoliDBMysqli extends DoliDB
public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null) public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null)
{ {
// phpcs:enable // phpcs:enable
// FIXME: $fulltext_keys parameter is unused // @TODO: $fulltext_keys parameter is unused
if (empty($type)) {
$type = 'InnoDB';
}
$pk = ''; $pk = '';
$sqluq = $sqlk = array(); $sqluq = $sqlk = array();
// cles recherchees dans le tableau des descriptions (fields) : type,value,attribute,null,default,extra // Keys found into the array $fields: type,value,attribute,null,default,extra
// ex. : $fields['rowid'] = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment'); // ex. : $fields['rowid'] = array(
$sql = "CREATE TABLE ".$table."("; // 'type'=>'int' or 'integer',
// 'value'=>'11',
// 'null'=>'not null',
// 'extra'=> 'auto_increment'
// );
$sql = "CREATE TABLE ".$this->sanitize($table)."(";
$i = 0; $i = 0;
$sqlfields = array(); $sqlfields = array();
foreach ($fields as $field_name => $field_desc) { foreach ($fields as $field_name => $field_desc) {
$sqlfields[$i] = $field_name." "; $sqlfields[$i] = $this->sanitize($field_name)." ";
$sqlfields[$i] .= $field_desc['type']; $sqlfields[$i] .= $this->sanitize($field_desc['type']);
if (preg_match("/^[^\s]/i", $field_desc['value'])) { if (!is_null($field_desc['value']) && $field_desc['value'] !== '') {
$sqlfields[$i] .= "(".$field_desc['value'].")"; $sqlfields[$i] .= "(".$this->sanitize($field_desc['value']).")";
} }
if (preg_match("/^[^\s]/i", $field_desc['attribute'])) { if (!is_null($field_desc['attribute']) && $field_desc['attribute'] !== '') {
$sqlfields[$i] .= " ".$field_desc['attribute']; $sqlfields[$i] .= " ".$this->sanitize($field_desc['attribute']);
} }
if (preg_match("/^[^\s]/i", $field_desc['default'])) { if (!is_null($field_desc['default']) && $field_desc['default'] !== '') {
if ((preg_match("/null/i", $field_desc['default'])) || (preg_match("/CURRENT_TIMESTAMP/i", $field_desc['default']))) { if (in_array($field_desc['type'], array('tinyint', 'smallint', 'int', 'double'))) {
$sqlfields[$i] .= " default ".$field_desc['default']; $sqlfields[$i] .= " DEFAULT ".((float) $field_desc['default']);
} elseif ($field_desc['default'] == 'null' || $field_desc['default'] == 'CURRENT_TIMESTAMP') {
$sqlfields[$i] .= " DEFAULT ".$this->sanitize($field_desc['default']);
} else { } else {
$sqlfields[$i] .= " default '".$this->escape($field_desc['default'])."'"; $sqlfields[$i] .= " DEFAULT '".$this->escape($field_desc['default'])."'";
} }
} }
if (preg_match("/^[^\s]/i", $field_desc['null'])) { if (!is_null($field_desc['null']) && $field_desc['null'] !== '') {
$sqlfields[$i] .= " ".$field_desc['null']; $sqlfields[$i] .= " ".$this->sanitize($field_desc['null'], 0, 0, 1);
} }
if (preg_match("/^[^\s]/i", $field_desc['extra'])) { if (!is_null($field_desc['extra']) && $field_desc['extra'] !== '') {
$sqlfields[$i] .= " ".$field_desc['extra']; $sqlfields[$i] .= " ".$this->sanitize($field_desc['extra']);
} }
$i++; $i++;
} }
if ($primary_key != "") { if ($primary_key != "") {
$pk = "primary key(".$primary_key.")"; $pk = "PRIMARY KEY(".$this->sanitize($primary_key).")";
} }
if (is_array($unique_keys)) { if (is_array($unique_keys)) {
$i = 0; $i = 0;
foreach ($unique_keys as $key => $value) { foreach ($unique_keys as $key => $value) {
$sqluq[$i] = "UNIQUE KEY '".$key."' ('".$this->escape($value)."')"; $sqluq[$i] = "UNIQUE KEY '".$this->sanitize($key)."' ('".$this->escape($value)."')";
$i++; $i++;
} }
} }
if (is_array($keys)) { if (is_array($keys)) {
$i = 0; $i = 0;
foreach ($keys as $key => $value) { foreach ($keys as $key => $value) {
$sqlk[$i] = "KEY ".$key." (".$value.")"; $sqlk[$i] = "KEY ".$this->sanitize($key)." (".$value.")";
$i++; $i++;
} }
} }
@@ -887,7 +898,8 @@ class DoliDBMysqli extends DoliDB
if (is_array($keys)) { if (is_array($keys)) {
$sql .= ",".implode(',', $sqlk); $sql .= ",".implode(',', $sqlk);
} }
$sql .= ") engine=".$type; $sql .= ")";
$sql .= " engine=".$this->sanitize($type);
if (!$this->query($sql)) { if (!$this->query($sql)) {
return -1; return -1;

View File

@@ -1068,51 +1068,63 @@ class DoliDBPgsql extends DoliDB
public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null) public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null)
{ {
// phpcs:enable // phpcs:enable
// FIXME: $fulltext_keys parameter is unused // @TODO: $fulltext_keys parameter is unused
$sqlfields = array(); $sqlfields = array();
$sqlk = array(); $sqlk = array();
$sqluq = array(); $sqluq = array();
// cles recherchees dans le tableau des descriptions (fields) : type,value,attribute,null,default,extra // Keys found into the array $fields: type,value,attribute,null,default,extra
// ex. : $fields['rowid'] = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment'); // ex. : $fields['rowid'] = array(
$sql = "create table ".$table."("; // 'type'=>'int' or 'integer',
// 'value'=>'11',
// 'null'=>'not null',
// 'extra'=> 'auto_increment'
// );
$sql = "CREATE TABLE ".$this->sanitize($table)."(";
$i = 0; $i = 0;
$sqlfields = array();
foreach ($fields as $field_name => $field_desc) { foreach ($fields as $field_name => $field_desc) {
$sqlfields[$i] = $field_name." "; $sqlfields[$i] = $this->sanitize($field_name)." ";
$sqlfields[$i] .= $field_desc['type']; $sqlfields[$i] .= $this->sanitize($field_desc['type']);
if (preg_match("/^[^\s]/i", $field_desc['value'])) { if (!is_null($field_desc['value']) && $field_desc['value'] !== '') {
$sqlfields[$i] .= "(".$field_desc['value'].")"; $sqlfields[$i] .= "(".$this->sanitize($field_desc['value']).")";
} elseif (preg_match("/^[^\s]/i", $field_desc['attribute'])) { }
$sqlfields[$i] .= " ".$field_desc['attribute']; if (!is_null($field_desc['attribute']) && $field_desc['attribute'] !== '') {
} elseif (preg_match("/^[^\s]/i", $field_desc['default'])) { $sqlfields[$i] .= " ".$this->sanitize($field_desc['attribute']);
if (preg_match("/null/i", $field_desc['default'])) { }
$sqlfields[$i] .= " default ".$field_desc['default']; if (!is_null($field_desc['default']) && $field_desc['default'] !== '') {
if (in_array($field_desc['type'], array('tinyint', 'smallint', 'int', 'double'))) {
$sqlfields[$i] .= " DEFAULT ".((float) $field_desc['default']);
} elseif ($field_desc['default'] == 'null' || $field_desc['default'] == 'CURRENT_TIMESTAMP') {
$sqlfields[$i] .= " DEFAULT ".$this->sanitize($field_desc['default']);
} else { } else {
$sqlfields[$i] .= " default '".$this->escape($field_desc['default'])."'"; $sqlfields[$i] .= " DEFAULT '".$this->escape($field_desc['default'])."'";
} }
} elseif (preg_match("/^[^\s]/i", $field_desc['null'])) { }
$sqlfields[$i] .= " ".$field_desc['null']; if (!is_null($field_desc['null']) && $field_desc['null'] !== '') {
} elseif (preg_match("/^[^\s]/i", $field_desc['extra'])) { $sqlfields[$i] .= " ".$this->sanitize($field_desc['null'], 0, 0, 1);
$sqlfields[$i] .= " ".$field_desc['extra']; }
if (!is_null($field_desc['extra']) && $field_desc['extra'] !== '') {
$sqlfields[$i] .= " ".$this->sanitize($field_desc['extra']);
} }
$i++; $i++;
} }
if ($primary_key != "") { if ($primary_key != "") {
$pk = "primary key(".$primary_key.")"; $pk = "PRIMARY KEY(".$this->sanitize($primary_key).")";
} }
if (is_array($unique_keys)) { if (is_array($unique_keys)) {
$i = 0; $i = 0;
foreach ($unique_keys as $key => $value) { foreach ($unique_keys as $key => $value) {
$sqluq[$i] = "UNIQUE KEY '".$key."' ('".$this->escape($value)."')"; $sqluq[$i] = "UNIQUE KEY '".$this->sanitize($key)."' ('".$this->escape($value)."')";
$i++; $i++;
} }
} }
if (is_array($keys)) { if (is_array($keys)) {
$i = 0; $i = 0;
foreach ($keys as $key => $value) { foreach ($keys as $key => $value) {
$sqlk[$i] = "KEY ".$key." (".$value.")"; $sqlk[$i] = "KEY ".$this->sanitize($key)." (".$value.")";
$i++; $i++;
} }
} }
@@ -1120,15 +1132,15 @@ class DoliDBPgsql extends DoliDB
if ($primary_key != "") { if ($primary_key != "") {
$sql .= ",".$pk; $sql .= ",".$pk;
} }
if (is_array($unique_keys)) { if ($unique_keys != "") {
$sql .= ",".implode(',', $sqluq); $sql .= ",".implode(',', $sqluq);
} }
if (is_array($keys)) { if (is_array($keys)) {
$sql .= ",".implode(',', $sqlk); $sql .= ",".implode(',', $sqlk);
} }
$sql .= ") type=".$type; $sql .= ")";
//$sql .= " engine=".$this->sanitize($type);
dol_syslog($sql, LOG_DEBUG);
if (!$this->query($sql)) { if (!$this->query($sql)) {
return -1; return -1;
} else { } else {

View File

@@ -970,51 +970,63 @@ class DoliDBSqlite3 extends DoliDB
public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null) public function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null)
{ {
// phpcs:enable // phpcs:enable
// FIXME: $fulltext_keys parameter is unused // @TODO: $fulltext_keys parameter is unused
$sqlfields = array(); $sqlfields = array();
$sqlk = array(); $sqlk = array();
$sqluq = array(); $sqluq = array();
// cles recherchees dans le tableau des descriptions (fields) : type,value,attribute,null,default,extra // Keys found into the array $fields: type,value,attribute,null,default,extra
// ex. : $fields['rowid'] = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment'); // ex. : $fields['rowid'] = array(
$sql = "create table ".$table."("; // 'type'=>'int' or 'integer',
// 'value'=>'11',
// 'null'=>'not null',
// 'extra'=> 'auto_increment'
// );
$sql = "CREATE TABLE ".$this->sanitize($table)."(";
$i = 0; $i = 0;
$sqlfields = array();
foreach ($fields as $field_name => $field_desc) { foreach ($fields as $field_name => $field_desc) {
$sqlfields[$i] = $field_name." "; $sqlfields[$i] = $this->sanitize($field_name)." ";
$sqlfields[$i] .= $field_desc['type']; $sqlfields[$i] .= $this->sanitize($field_desc['type']);
if (preg_match("/^[^\s]/i", $field_desc['value'])) { if (!is_null($field_desc['value']) && $field_desc['value'] !== '') {
$sqlfields[$i] .= "(".$field_desc['value'].")"; $sqlfields[$i] .= "(".$this->sanitize($field_desc['value']).")";
} elseif (preg_match("/^[^\s]/i", $field_desc['attribute'])) { }
$sqlfields[$i] .= " ".$field_desc['attribute']; if (!is_null($field_desc['attribute']) && $field_desc['attribute'] !== '') {
} elseif (preg_match("/^[^\s]/i", $field_desc['default'])) { $sqlfields[$i] .= " ".$this->sanitize($field_desc['attribute']);
if (preg_match("/null/i", $field_desc['default'])) { }
$sqlfields[$i] .= " default ".$field_desc['default']; if (!is_null($field_desc['default']) && $field_desc['default'] !== '') {
if (in_array($field_desc['type'], array('tinyint', 'smallint', 'int', 'double'))) {
$sqlfields[$i] .= " DEFAULT ".((float) $field_desc['default']);
} elseif ($field_desc['default'] == 'null' || $field_desc['default'] == 'CURRENT_TIMESTAMP') {
$sqlfields[$i] .= " DEFAULT ".$this->sanitize($field_desc['default']);
} else { } else {
$sqlfields[$i] .= " default '".$this->escape($field_desc['default'])."'"; $sqlfields[$i] .= " DEFAULT '".$this->escape($field_desc['default'])."'";
} }
} elseif (preg_match("/^[^\s]/i", $field_desc['null'])) { }
$sqlfields[$i] .= " ".$field_desc['null']; if (!is_null($field_desc['null']) && $field_desc['null'] !== '') {
} elseif (preg_match("/^[^\s]/i", $field_desc['extra'])) { $sqlfields[$i] .= " ".$this->sanitize($field_desc['null'], 0, 0, 1);
$sqlfields[$i] .= " ".$field_desc['extra']; }
if (!is_null($field_desc['extra']) && $field_desc['extra'] !== '') {
$sqlfields[$i] .= " ".$this->sanitize($field_desc['extra']);
} }
$i++; $i++;
} }
if ($primary_key != "") { if ($primary_key != "") {
$pk = "primary key(".$primary_key.")"; $pk = "PRIMARY KEY(".$this->sanitize($primary_key).")";
} }
if (is_array($unique_keys)) { if (is_array($unique_keys)) {
$i = 0; $i = 0;
foreach ($unique_keys as $key => $value) { foreach ($unique_keys as $key => $value) {
$sqluq[$i] = "UNIQUE KEY '".$key."' ('".$this->escape($value)."')"; $sqluq[$i] = "UNIQUE KEY '".$this->sanitize($key)."' ('".$this->escape($value)."')";
$i++; $i++;
} }
} }
if (is_array($keys)) { if (is_array($keys)) {
$i = 0; $i = 0;
foreach ($keys as $key => $value) { foreach ($keys as $key => $value) {
$sqlk[$i] = "KEY ".$key." (".$value.")"; $sqlk[$i] = "KEY ".$this->sanitize($key)." (".$value.")";
$i++; $i++;
} }
} }
@@ -1022,19 +1034,20 @@ class DoliDBSqlite3 extends DoliDB
if ($primary_key != "") { if ($primary_key != "") {
$sql .= ",".$pk; $sql .= ",".$pk;
} }
if (is_array($unique_keys)) { if ($unique_keys != "") {
$sql .= ",".implode(',', $sqluq); $sql .= ",".implode(',', $sqluq);
} }
if (is_array($keys)) { if (is_array($keys)) {
$sql .= ",".implode(',', $sqlk); $sql .= ",".implode(',', $sqlk);
} }
$sql .= ") type=".$type; $sql .= ")";
//$sql .= " engine=".$this->sanitize($type);
dol_syslog($sql, LOG_DEBUG); if (!$this->query($sql)) {
if (!$this -> query($sql)) {
return -1; return -1;
} else {
return 1;
} }
return 1;
} }
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

View File

@@ -1288,12 +1288,12 @@ function createNewDictionnary($modulename, $file, $namedic, $dictionnaires = nul
} }
$columns = array( $columns = array(
'rowid' => array('type' => 'integer(11)'), 'rowid' => array('type' => 'integer', 'value' => 11),
'code' => array('type' => 'varchar(255) NOT NULL'), 'code' => array('type' => 'varchar', 'value' => 255, 'null'=>'NOT NULL'),
'label' => array('type' => 'varchar(255) NOT NULL'), 'label' => array('type' => 'varchar', 'value' => 255, 'null'=>'NOT NULL'),
'position' => array('type' => 'integer(11) NULL'), 'position' => array('type' => 'integer', 'value' => 11, 'null'=>'NULL'),
'use_default' => array('type' => 'varchar(255) DEFAULT 1'), 'use_default' => array('type' => 'varchar', 'value' => 11, 'default'=>'1'),
'active' => array('type' => 'integer') 'active' => array('type' => 'integer', 'value' => 3)
); );
@@ -1309,13 +1309,13 @@ function createNewDictionnary($modulename, $file, $namedic, $dictionnaires = nul
} }
} }
// check if tablename exist in Database and create it if not // check if tablename exist in Database and create it if not
$query = "SHOW TABLES LIKE '" . MAIN_DB_PREFIX.strtolower($namedic) . "'"; $query = "SHOW TABLES LIKE '" . $db->sanitize(MAIN_DB_PREFIX.strtolower($namedic)) . "'";
$checkTable = $db->query($query); $checkTable = $db->query($query);
if ($checkTable && $db->num_rows($checkTable) > 0) { if ($checkTable && $db->num_rows($checkTable) > 0) {
setEventMessages($langs->trans("ErrorTableExist", $namedic), null, 'errors'); setEventMessages($langs->trans("ErrorTableExist", $namedic), null, 'errors');
return; return;
} else { } else {
$_results = $db->DDLCreateTable(MAIN_DB_PREFIX.strtolower($namedic), $columns, $primaryKey, "InnoDB"); $_results = $db->DDLCreateTable(MAIN_DB_PREFIX.strtolower($namedic), $columns, $primaryKey, "");
if ($_results < 0) { if ($_results < 0) {
dol_print_error($db); dol_print_error($db);
$langs->load("errors"); $langs->load("errors");