/** * @return null * @throws \Exception */ public function query() { $stmt = null; $args = func_get_args(); $attempt = 0; $retry = true; while ($retry) { $retry = false; try { switch (count($args)) { case 1: $stmt = parent::query($args[0]); break; case 2: $stmt = parent::query($args[0], $args[1]); break; case 3: $stmt = parent::query($args[0], $args[1], $args[2]); break; case 4: $stmt = parent::query($args[0], $args[1], $args[2], $args[3]); break; default: $stmt = parent::query(); } } catch (\Exception $e) { if ($this->validateReconnectAttempt($e, $attempt)) { $this->close(); $attempt++; $retry = true; } else { throw $e; } } } return $stmt; }
/** * @author David Grudl * @see https://github.com/dg/dibi/blob/cde5af7cbe02d231fe2d3f904fc2c3d3eeda66f0/dibi/libs/DibiConnection.php#L630 */ public static function loadFromFile(Connection $connection, $file, $callback = NULL) { @set_time_limit(0); // intentionally @ if (!($handle = @fopen($file, 'r'))) { // intentionally @ throw new InvalidArgumentException("Cannot open file '{$file}'."); } $count = 0; $delimiter = ';'; $sql = ''; while (!feof($handle)) { $s = rtrim(fgets($handle)); if (substr($s, 0, 10) === 'DELIMITER ') { $delimiter = substr($s, 10); } elseif (substr($s, -strlen($delimiter)) === $delimiter) { $sql .= substr($s, 0, -strlen($delimiter)); if ($callback) { call_user_func($callback, $sql, ftell($handle)); } $connection->query($sql); $sql = ''; $count++; } else { $sql .= $s . "\n"; } } if (trim($sql) !== '') { if ($callback) { call_user_func($callback, $sql, ftell($handle)); } $connection->query($sql); $count++; } fclose($handle); return $count; }