db->query('SET @OLD_SQL_MODE = @@SESSION.sql_mode'); $this->db->query('SET FOREIGN_KEY_CHECKS = 0'); $this->db->query('SET SESSION SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"'); foreach (self::SQL_FILES as $file) { $path = $sqlDir . $file; if (! is_file($path)) { CLI::write(" [SKIP] Missing SQL file: $file", 'yellow'); continue; } $sql = file_get_contents($path); if ($sql === false || trim($sql) === '') { CLI::write(" [SKIP] Empty SQL file: $file", 'yellow'); continue; } // Strip UTF-8 BOM if present (PowerShell 5.1 Set-Content adds it) if (str_starts_with($sql, "\xEF\xBB\xBF")) { $sql = substr($sql, 3); } // Split on statement boundaries (semicolons not inside quotes) $statements = $this->splitSqlStatements($sql); foreach ($statements as $stmt) { $stmt = trim($stmt); if ($stmt === '') { continue; } $this->db->query($stmt); } CLI::write(" [OK] $file", 'green'); } $this->db->query('SET FOREIGN_KEY_CHECKS = 1'); $this->db->query('SET SESSION SQL_MODE = @OLD_SQL_MODE'); // Seed comprehensive X-Ray, CT-Scan, and MRI templates + service charges $this->call(RadiologyTemplatesSeeder::class); CLI::write('Lab template seeder complete.', 'green'); } /** * Simple SQL splitter: splits on semicolons that are not inside * single-quoted or double-quoted strings. * * @return string[] */ private function splitSqlStatements(string $sql): array { $statements = []; $current = ''; $inSingle = false; $inDouble = false; $len = strlen($sql); for ($i = 0; $i < $len; $i++) { $char = $sql[$i]; $prev = ($i > 0) ? $sql[$i - 1] : ''; if ($char === "'" && ! $inDouble && $prev !== '\\') { $inSingle = ! $inSingle; } elseif ($char === '"' && ! $inSingle && $prev !== '\\') { $inDouble = ! $inDouble; } if ($char === ';' && ! $inSingle && ! $inDouble) { $statements[] = trim($current); $current = ''; } else { $current .= $char; } } if (trim($current) !== '') { $statements[] = trim($current); } return $statements; } }