Skip to content

Commit

Permalink
Aggiornamento struttura db 2024_2
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Foti <[email protected]>
  • Loading branch information
MocioF committed Jan 27, 2024
1 parent 690a515 commit 5ce324c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
4 changes: 2 additions & 2 deletions admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ function gcmi_update_table( $fname ) {
}
$tmp_table_name = $database_file_info[ $id ]['table_name'] . '_tmp';
GCMI_Activator::create_db_table( $database_file_info[ $id ]['name'], $tmp_table_name );
$csv_file_path = $download_temp_dir . '/' . $database_file_info[ $id ]['featured_csv'];
$csv_file_path = $download_temp_dir . $database_file_info[ $id ]['featured_csv'];
GCMI_Activator::convert_file_charset( $csv_file_path, $database_file_info[ $id ]['orig_encoding'] );
GCMI_Activator::prepare_file( $csv_file_path );
GCMI_Activator::populate_db_table(
Expand All @@ -369,7 +369,7 @@ function gcmi_update_table( $fname ) {
$error_title = esc_html__( 'Error importing data into database', 'campi-moduli-italiani' );
$error_message = '<h1>' . $error_title . '</h1><br>';
/* translators: %1$s: the data name; %2$s: the db table name. */
$error_message .= esc_html( sprintf( __( 'Unable to import %1$s into %2$s', 'campi-moduli-italiani' ), $csv_file_path, self::$database_file_info[ $i ]['table_name'] ) ) . '<br>';
$error_message .= esc_html( sprintf( __( 'Unable to import %1$s into %2$s', 'campi-moduli-italiani' ), $csv_file_path, GCMI_Activator::$database_file_info[ $id ]['table_name'] ) ) . '<br>';
$str = htmlspecialchars( print_r( $wpdb->last_error, true ), ENT_QUOTES ) .
'<br>' . esc_html__( 'Last executed query:', 'campi-moduli-italiani' );
$query = htmlspecialchars( $wpdb->last_query, ENT_QUOTES );
Expand Down
42 changes: 28 additions & 14 deletions admin/class-gcmi-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ public static function make_tmp_dwld_dir() {
if ( ! wp_mkdir_p( "$tmp_dir" ) ) {
return false;
} else {
return $tmp_dir;
return $tmp_dir . '/';
}
}

Expand Down Expand Up @@ -890,18 +890,18 @@ public static function create_db_table( $name, $table ) {
'i_den_unita_territoriale varchar(255) NOT NULL, ' .
'i_cod_tipo_unita_territoriale TINYINT(1) NOT NULL, ' .
'i_flag_capoluogo TINYINT(1) NOT NULL, ' .
'i_sigla_automobilistica varchar(10) NOT NULL, ' .
'i_sigla_automobilistica char(2) NOT NULL, ' .
'i_cod_comune_num int(6) NOT NULL, ' .
'i_cod_comune_num_2010_2016 int(6) NOT NULL, ' .
'i_cod_comune_num_2006_2009 int(6) NOT NULL, ' .
'i_cod_comune_num_1995_2005 int(6) NOT NULL, ' .
'i_cod_catastale char(4) NOT NULL, ' .
'i_nuts1_2010 char(3) NOT NULL, ' .
'i_nuts2_2010 char(4) NOT NULL, ' .
'i_nuts3_2010 char(5) NOT NULL, ' .
'i_nuts1_2021 char(3) NOT NULL, ' .
'i_nuts2_2021 char(4) NOT NULL, ' .
'i_nuts3_2021 char(5) NOT NULL, ' .
'i_nuts1_2024 char(3) NOT NULL, ' .
'i_nuts2_2024 char(4) NOT NULL, ' .
'i_nuts3_2024 char(5) NOT NULL, ' .
'PRIMARY KEY (id), ' .
'INDEX `i_cod_comune` (`i_cod_comune`) ' .
') %2$s',
Expand All @@ -917,7 +917,7 @@ public static function create_db_table( $name, $table ) {
'CREATE TABLE IF NOT EXISTS %1$s ( ' .
'id INT(11) NOT NULL AUTO_INCREMENT, ' .
'i_anno_var YEAR(4) NOT NULL, ' .
'i_sigla_automobilistica varchar(10) NOT NULL, ' .
'i_sigla_automobilistica char(2) NOT NULL, ' .
'i_cod_unita_territoriale char(3) NOT NULL, ' .
'i_cod_comune char(6) NOT NULL, ' .
'i_denominazione_full varchar(255) NOT NULL, ' .
Expand Down Expand Up @@ -1138,6 +1138,20 @@ private static function add_trailing_zeroes( $string, $len ) {
}
return sprintf( '%0' . strval( $len ) . 's', $string );
}

/**
* Tronca le stringhe ad un numero massimo di caratteri
*
* @param string $string La stringa
* @param int $len Numero massimo di caratteri ammesso
* @return string
*/
private static function truncate( $string, $len ) {
if ( $len < strlen( $string ) ) {
return substr( $string, 0, $len );
}
return $string;
}

/**
* Populates a db table.
Expand Down Expand Up @@ -1222,18 +1236,18 @@ public static function populate_db_table( $name, $csv_file_path, $table ) {
'i_den_unita_territoriale' => trim( gcmi_safe_strval( $gcmi_dati_line[11] ) ),
'i_cod_tipo_unita_territoriale' => $gcmi_dati_line[12],
'i_flag_capoluogo' => $gcmi_dati_line[13],
'i_sigla_automobilistica' => trim( gcmi_safe_strval( $gcmi_dati_line[14] ) ),
'i_sigla_automobilistica' => self::truncate( trim( gcmi_safe_strval( $gcmi_dati_line[14] ) ), 2),
'i_cod_comune_num' => $gcmi_dati_line[15],
'i_cod_comune_num_2010_2016' => $gcmi_dati_line[16],
'i_cod_comune_num_2006_2009' => $gcmi_dati_line[17],
'i_cod_comune_num_1995_2005' => $gcmi_dati_line[18],
'i_cod_catastale' => $gcmi_dati_line[19],
'i_nuts1_2010' => $gcmi_dati_line[20],
'i_nuts2_2010' => $gcmi_dati_line[21],
'i_nuts3_2010' => $gcmi_dati_line[22],
'i_nuts1_2021' => $gcmi_dati_line[23],
'i_nuts2_2021' => $gcmi_dati_line[24],
'i_nuts3_2021' => $gcmi_dati_line[25],
'i_cod_catastale' => self::truncate( $gcmi_dati_line[19], 4 ),
'i_nuts1_2021' => self::truncate( $gcmi_dati_line[20], 3 ),
'i_nuts2_2021' => self::truncate( $gcmi_dati_line[21], 4 ),
'i_nuts3_2021' => self::truncate( $gcmi_dati_line[22], 5 ),
'i_nuts1_2024' => self::truncate( $gcmi_dati_line[23], 3 ),
'i_nuts2_2024' => self::truncate( $gcmi_dati_line[24], 4 ),
'i_nuts3_2024' => self::truncate( $gcmi_dati_line[25], 5 ),
),
array(
'%s',
Expand Down
23 changes: 20 additions & 3 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ function gcmi_upgrade() {
return;
}
if ( version_compare( $old_ver, '2.1.5', '<' ) ) {
gcmi_update_db_2024();
gcmi_update_db_2024_1();
}
if ( version_compare( $old_ver, '2.2.0', '<' ) ) {
gcmi_update_db_2024_2();
gcmi_add_index_on_tables();
gcmi_create_unfiltered_views_on_plugin_update();
}
Expand Down Expand Up @@ -300,8 +301,7 @@ function gcmi_delete_all_views(): void {
*
* @global type $wpdb
*/
function gcmi_update_db_2024() {
global $wpdb;
function gcmi_update_db_2024_1() {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$queries = array(
'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts1 to i_nuts1_2010',
Expand All @@ -314,6 +314,23 @@ function gcmi_update_db_2024() {
dbDelta( $queries, true );
}

/**
* Aggiorna la tabella comuni_attuali al formato dei dati distribuiti nel 2024
*
* @global type $wpdb
*/
function gcmi_update_db_2024_2() {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$queries = array(
'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts1_2021 to i_nuts1_2024',
'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts2_2021 to i_nuts2_2024',
'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts3_2021 to i_nuts3_2024',
'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts1_2010 to i_nuts1_2021',
'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts2_2010 to i_nuts2_2021',
'ALTER TABLE ' . GCMI_TABLE_PREFIX . 'comuni_attuali RENAME COLUMN i_nuts3_2010 to i_nuts3_2021',
);
dbDelta( $queries, true );
}
/**
* Adds extra links to the plugin activation page
*
Expand Down

0 comments on commit 5ce324c

Please sign in to comment.