diff --git a/htdocs/core/db/Database.interface.php b/htdocs/core/db/Database.interface.php index 180f928eac0..ac9d6134f9a 100644 --- a/htdocs/core/db/Database.interface.php +++ b/htdocs/core/db/Database.interface.php @@ -129,6 +129,17 @@ interface Database public function DDLListTables($database, $table = ''); // phpcs:enable + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * List tables into a database with table type + * + * @param string $database Name of database + * @param string $table Name of table filter ('xxx%') + * @return array List of tables in an array + */ + public function DDLListTablesFull($database, $table = ''); + // phpcs:enable + /** * Return last request executed with query() * diff --git a/htdocs/core/db/mysqli.class.php b/htdocs/core/db/mysqli.class.php index 7d2f075944b..aaeaaf4c910 100644 --- a/htdocs/core/db/mysqli.class.php +++ b/htdocs/core/db/mysqli.class.php @@ -716,6 +716,38 @@ class DoliDBMysqli extends DoliDB return $listtables; } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * List tables into a database + * + * @param string $database Name of database + * @param string $table Nmae of table filter ('xxx%') + * @return array List of tables in an array + */ + public function DDLListTablesFull($database, $table = '') + { + // phpcs:enable + $listtables = array(); + + $like = ''; + if ($table) { + $tmptable = preg_replace('/[^a-z0-9\.\-\_%]/i', '', $table); + + $like = "LIKE '".$this->escape($tmptable)."'"; + } + $tmpdatabase = preg_replace('/[^a-z0-9\.\-\_]/i', '', $database); + + $sql = "SHOW FULL TABLES FROM `".$tmpdatabase."` ".$like.";"; + + $result = $this->query($sql); + if ($result) { + while ($row = $this->fetch_row($result)) { + $listtables[] = $row; + } + } + return $listtables; + } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * List information of columns into a table. diff --git a/htdocs/core/db/pgsql.class.php b/htdocs/core/db/pgsql.class.php index e7e0b0da9d8..8c2b1c6c7d8 100644 --- a/htdocs/core/db/pgsql.class.php +++ b/htdocs/core/db/pgsql.class.php @@ -977,6 +977,34 @@ class DoliDBPgsql extends DoliDB return $listtables; } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * List tables into a database + * + * @param string $database Name of database + * @param string $table Name of table filter ('xxx%') + * @return array List of tables in an array + */ + public function DDLListTablesFull($database, $table = '') + { + // phpcs:enable + $listtables = array(); + + $escapedlike = ''; + if ($table) { + $tmptable = preg_replace('/[^a-z0-9\.\-\_%]/i', '', $table); + + $escapedlike = " AND table_name LIKE '".$this->escape($tmptable)."'"; + } + $result = pg_query($this->db, "SELECT table_name, table_type FROM information_schema.tables WHERE table_schema = 'public'".$escapedlike." ORDER BY table_name"); + if ($result) { + while ($row = $this->fetch_row($result)) { + $listtables[] = $row; + } + } + return $listtables; + } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * List information of columns into a table. diff --git a/htdocs/core/db/sqlite3.class.php b/htdocs/core/db/sqlite3.class.php index 819e5ba72f0..7255990fa64 100644 --- a/htdocs/core/db/sqlite3.class.php +++ b/htdocs/core/db/sqlite3.class.php @@ -896,6 +896,38 @@ class DoliDBSqlite3 extends DoliDB return $listtables; } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * List tables into a database with table type + * + * @param string $database Name of database + * @param string $table Name of table filter ('xxx%') + * @return array List of tables in an array + */ + public function DDLListTablesFull($database, $table = '') + { + // phpcs:enable + $listtables = array(); + + $like = ''; + if ($table) { + $tmptable = preg_replace('/[^a-z0-9\.\-\_%]/i', '', $table); + + $like = "LIKE '".$this->escape($tmptable)."'"; + } + $tmpdatabase = preg_replace('/[^a-z0-9\.\-\_]/i', '', $database); + + $sql = "SHOW FULL TABLES FROM ".$tmpdatabase." ".$like.";"; + //print $sql; + $result = $this->query($sql); + if ($result) { + while ($row = $this->fetch_row($result)) { + $listtables[] = $row; + } + } + return $listtables; + } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * List information of columns into a table. diff --git a/htdocs/debugbar/class/TraceableDB.php b/htdocs/debugbar/class/TraceableDB.php index 8e28b0c59bd..3622558731f 100644 --- a/htdocs/debugbar/class/TraceableDB.php +++ b/htdocs/debugbar/class/TraceableDB.php @@ -205,6 +205,18 @@ class TraceableDB extends DoliDB return $this->db->DDLListTables($database, $table); } + /** + * List tables into a database with table info + * + * @param string $database Name of database + * @param string $table Nmae of table filter ('xxx%') + * @return array List of tables in an array + */ + public function DDLListTablesFull($database, $table = '') + { + return $this->db->DDLListTablesFull($database, $table); + } + /** * Return last request executed with query() * diff --git a/htdocs/install/repair.php b/htdocs/install/repair.php index 70be057b7b8..b5b408b00ca 100644 --- a/htdocs/install/repair.php +++ b/htdocs/install/repair.php @@ -1244,7 +1244,7 @@ if ($ok && GETPOST('force_utf8_on_tables', 'alpha')) { if ($db->type == "mysql" || $db->type == "mysqli") { $force_utf8_on_tables = GETPOST('force_utf8_on_tables', 'alpha'); - $listoftables = $db->DDLListTables($db->database_name); + $listoftables = $db->DDLListTablesFull($db->database_name); // Disable foreign key checking for avoid errors if ($force_utf8_on_tables == 'confirmed') { @@ -1255,14 +1255,18 @@ if ($ok && GETPOST('force_utf8_on_tables', 'alpha')) { foreach ($listoftables as $table) { // do not convert llx_const if mysql encrypt/decrypt is used - if ($conf->db->dolibarr_main_db_encryption != 0 && preg_match('/\_const$/', $table)) { + if ($conf->db->dolibarr_main_db_encryption != 0 && preg_match('/\_const$/', $table[0])) { + continue; + } + if ($table[1] == 'VIEW') { + print ''.$table[0].' is a '.$table[1].' (Skipped)'; continue; } print ''; - print $table; - $sql1 = "ALTER TABLE ".$table." ROW_FORMAT=dynamic"; - $sql2 = "ALTER TABLE ".$table." CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci"; + print $table[0]; + $sql1 = "ALTER TABLE ".$table[0]." ROW_FORMAT=dynamic"; + $sql2 = "ALTER TABLE ".$table[0]." CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci"; print ''; print ''; if ($force_utf8_on_tables == 'confirmed') { @@ -1297,7 +1301,7 @@ if ($ok && GETPOST('force_utf8mb4_on_tables', 'alpha')) { if ($db->type == "mysql" || $db->type == "mysqli") { $force_utf8mb4_on_tables = GETPOST('force_utf8mb4_on_tables', 'alpha'); - $listoftables = $db->DDLListTables($db->database_name); + $listoftables = $db->DDLListTablesFull($db->database_name); // Disable foreign key checking for avoid errors if ($force_utf8mb4_on_tables == 'confirmed') { @@ -1308,14 +1312,18 @@ if ($ok && GETPOST('force_utf8mb4_on_tables', 'alpha')) { foreach ($listoftables as $table) { // do not convert llx_const if mysql encrypt/decrypt is used - if ($conf->db->dolibarr_main_db_encryption != 0 && preg_match('/\_const$/', $table)) { + if ($conf->db->dolibarr_main_db_encryption != 0 && preg_match('/\_const$/', $table[0])) { + continue; + } + if ($table[1] == 'VIEW') { + print ''.$table[0].' is a '.$table[1].' (Skipped)'; continue; } print ''; - print $table; - $sql1 = "ALTER TABLE ".$table." ROW_FORMAT=dynamic"; - $sql2 = "ALTER TABLE ".$table." CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"; + print $table[0]; + $sql1 = "ALTER TABLE ".$table[0]." ROW_FORMAT=dynamic"; + $sql2 = "ALTER TABLE ".$table[0]." CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"; print ''; print ''; if ($force_utf8mb4_on_tables == 'confirmed') {