<?php
declare(strict_types=1);
$root=dirname(__DIR__);$database='tmp_windows_sql_'.date('Ymd_His').'_'.bin2hex(random_bytes(3));$db=new mysqli('127.0.0.1','root','');$db->set_charset('utf8mb4');
function winSqlBatch(mysqli $db,string $sql):void{if(!$db->multi_query($sql))throw new RuntimeException($db->error);do{if($r=$db->store_result())$r->free();if(!$db->more_results())break;}while($db->next_result());if($db->errno)throw new RuntimeException($db->error);}
try{$db->query("CREATE DATABASE `{$database}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");$db->select_db($database);winSqlBatch($db,(string)file_get_contents($root.'/SQL_CONTROLE_COMPUTADORES.sql'));$sql=(string)file_get_contents($root.'/SQL_INVENTARIO_WINDOWS.sql');winSqlBatch($db,$sql);winSqlBatch($db,$sql);$tables=['ti_pc_config','ti_pc_agentes','ti_pc_sincronizacoes','ti_pc_eventos','ti_pc_nonces','ti_pc_rate_limits','ti_pc_auditoria'];foreach($tables as $table){$safe=$db->real_escape_string($table);if(!$db->query("SHOW TABLES LIKE '{$safe}'")->fetch_row())throw new RuntimeException('Tabela ausente: '.$table);}$cfg=$db->query('SELECT COUNT(*) c FROM ti_pc_config WHERE id=1')->fetch_assoc();if((int)$cfg['c']!==1)throw new RuntimeException('Configuracao inicial invalida.');echo 'WINDOWS SQL TEST OK'.PHP_EOL.'SQL executado duas vezes; '.count($tables).' tabelas validadas.'.PHP_EOL;}finally{$db->select_db('mysql');if(str_starts_with($database,'tmp_windows_sql_'))$db->query("DROP DATABASE IF EXISTS `{$database}`");$db->close();}
