*/ private array $cachingStatements = []; private Connection $defaultConnection; private array $waitingForInsert = []; public function __construct( Connection $defaultConnection ) { $this->defaultConnection = $defaultConnection; } public function finalize(): void { $this->doInsertPending(); } public function importCode( string $countryCode, string $label, string $code, string $refPostalCodeId, string $refPostalCodeSource, float $centerLat, float $centerLon, int $centerSRID ): void { $this->waitingForInsert[] = [ $countryCode, $label, $code, $refPostalCodeId, $refPostalCodeSource, $centerLon, $centerLat, $centerSRID, ]; if (100 <= count($this->waitingForInsert)) { $this->doInsertPending(); } } private function doInsertPending(): void { if (!array_key_exists($forNumber = count($this->waitingForInsert), $this->cachingStatements)) { $sql = strtr(self::QUERY, [ '{{ values }}' => implode( ', ', array_fill(0, $forNumber, self::VALUE) ), ]); $this->cachingStatements[$forNumber] = $this->defaultConnection->prepare($sql); } $statement = $this->cachingStatements[$forNumber]; try { $statement->executeStatement(array_merge(...$this->waitingForInsert)); } catch (Exception $e) { // in some case, we can add debug code here //dump($this->waitingForInsert); throw $e; } finally { $this->waitingForInsert = []; } } }