| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Script per il merge dei file comune.sql di IT e CH
- Aggiunge le colonne created_at, updated_at, nazione_id
- """
-
- import re
- import os
-
- def parse_sql_file(file_path):
- """Parsa un file SQL e estrae le righe INSERT da tutte le query"""
- with open(file_path, 'r', encoding='utf-8') as f:
- content = f.read()
-
- # Trova tutte le sezioni VALUES
- values_matches = re.findall(r'VALUES\s*(.*?);', content, re.DOTALL | re.IGNORECASE)
- if not values_matches:
- print(f"Errore: non trovo la sezione VALUES in {file_path}")
- return []
-
- # Combina tutte le sezioni VALUES
- all_values_section = ',\n'.join(values_matches)
-
- # Estrae le righe individuali
- rows = []
- # Pattern migliorato per matchare le righe con apostrofi escaped: (id, provincia_id, nome, codice_catastale, ...)
- # Gestisce sia apostrofi normali che escaped (\')
- pattern = r'\((\d+),\s*(\d+),\s*\'((?:[^\'\\]|\\.)*)\',\s*\'?([^\',]*)\'?'
-
- matches = re.findall(pattern, all_values_section)
- for match in matches:
- id_val, provincia_id, nome, codice_catastale = match
- # Decodifica gli apostrofi escaped
- nome = nome.replace("\\'", "'")
- rows.append({
- 'id': int(id_val),
- 'provincia_id': int(provincia_id),
- 'nome': nome,
- 'codice_catastale': codice_catastale if codice_catastale != 'NULL' else None
- })
-
- return rows
-
- def generate_merged_sql(it_rows, ch_rows, output_file):
- """Genera il file SQL di merge"""
-
- with open(output_file, 'w', encoding='utf-8') as f:
- f.write("INSERT INTO `comune` (\n")
- f.write(" `id`,\n")
- f.write(" `provincia_id`,\n")
- f.write(" `nome`,\n")
- f.write(" `codice_catastale`,\n")
- f.write(" `created_at`,\n")
- f.write(" `updated_at`,\n")
- f.write(" `nazione_id`\n")
- f.write(") VALUES\n")
-
- all_rows = []
-
- # Processa righe IT (nazione_id = 118, created_at/updated_at = NULL)
- for row in it_rows:
- nome_escaped = row['nome'].replace("'", "\\'")
- codice = f"'{row['codice_catastale']}'" if row['codice_catastale'] else 'NULL'
- all_rows.append(
- f"({row['id']}, {row['provincia_id']}, '{nome_escaped}', {codice}, NULL, NULL, 118)"
- )
-
- # Processa righe CH (nazione_id = 220, created_at/updated_at dal file originale)
- # Rinumera gli ID partendo da 110015
- new_ch_id = 110015
- for row in ch_rows:
- nome_escaped = row['nome'].replace("'", "\\'")
- codice = f"'{row['codice_catastale']}'" if row['codice_catastale'] else 'NULL'
- all_rows.append(
- f"({new_ch_id}, {row['provincia_id']}, '{nome_escaped}', {codice}, NULL, NULL, 220)"
- )
- new_ch_id += 1
-
- # Scrivi tutte le righe
- f.write(",\n".join(all_rows))
- f.write(";\n")
-
- # Aggiungi statistiche
- f.write(f"\n-- Statistiche merge:\n")
- f.write(f"-- Comuni IT: {len(it_rows)} (ID originali, nazione_id = 118)\n")
- f.write(f"-- Comuni CH: {len(ch_rows)} (ID da {110015} a {110015 + len(ch_rows) - 1}, nazione_id = 220)\n")
- f.write(f"-- Totale comuni: {len(all_rows)}\n")
-
- def main():
- # Percorsi dei file
- base_dir = os.path.dirname(os.path.abspath(__file__))
- it_file = os.path.join(base_dir, 'it', 'comune.sql')
- ch_file = os.path.join(base_dir, 'ch', 'comune.sql')
- output_file = os.path.join(base_dir, 'comune_merged.sql')
-
- print("Inizio parsing file IT...")
- it_rows = parse_sql_file(it_file)
- print(f"Trovate {len(it_rows)} righe nel file IT")
-
- print("Inizio parsing file CH...")
- ch_rows = parse_sql_file(ch_file)
- print(f"Trovate {len(ch_rows)} righe nel file CH")
-
- print("Generazione file di merge...")
- generate_merged_sql(it_rows, ch_rows, output_file)
-
- print(f"File di merge generato: {output_file}")
- print(f"Totale righe nel file finale: {len(it_rows) + len(ch_rows)}")
-
- if __name__ == "__main__":
- main()
|