handle = $handle; $this->base = $offset; $this->read_size = max( 1, $read_size ); fseek( $handle, $offset ); } /** * File offset immediately after the last statement returned. */ public function offset(): int { return $this->base + $this->pos; } /** * @return string|null The next statement without its terminating semicolon, or null at end of file. * @throws \RuntimeException On an unterminated quoted string. */ public function next(): ?string { $i = $this->pos; $state = self::NORMAL; $start = null; while ( true ) { $len = strlen( $this->buf ); $need_more = false; $buf = $this->buf; while ( $i < $len && ! $need_more ) { switch ( $state ) { case self::NORMAL: if ( null === $start ) { $i += strspn( $buf, " \t\r\n", $i ); } else { $i += strcspn( $buf, self::SPECIAL, $i ); } if ( $i >= $len ) { break; } $c = $buf[ $i ]; if ( '-' === $c ) { if ( $i + 2 >= $len && ! $this->eof ) { $need_more = true; break; } if ( $i + 1 < $len && '-' === $buf[ $i + 1 ] && ( $i + 2 >= $len || false !== strpos( " \t\r\n", $buf[ $i + 2 ] ) ) ) { $state = self::LINE; $i += 2; break; } } elseif ( '#' === $c ) { $state = self::LINE; ++$i; break; } elseif ( '/' === $c ) { if ( $i + 1 >= $len && ! $this->eof ) { $need_more = true; break; } if ( $i + 1 < $len && '*' === $buf[ $i + 1 ] ) { $state = self::BLOCK; $i += 2; break; } } // Anything else is statement content. if ( null === $start ) { $start = $i; } if ( ';' === $c ) { if ( $start === $i ) { // A bare ";" is an empty statement; skip it. $start = null; ++$i; break; } $this->pos = $i + 1; $statement = substr( $buf, $start, $i - $start ); if ( strlen( $statement ) > self::RELEASE_ABOVE ) { // A huge statement (one 10 MB row) has filled the buffer. Hand the buffer back now, // not at the next refill, so the caller is not holding two copies of it. $this->buf = (string) substr( $buf, $this->pos ); $this->base += $this->pos; $this->pos = 0; } return $statement; } if ( "'" === $c ) { $state = self::SINGLE; } elseif ( '"' === $c ) { $state = self::DOUBLE; } elseif ( '`' === $c ) { $state = self::TICK; } ++$i; break; case self::SINGLE: case self::DOUBLE: $quote = self::SINGLE === $state ? "'" : '"'; $i += strcspn( $buf, $quote . '\\', $i ); if ( $i >= $len ) { break; } if ( '\\' === $buf[ $i ] ) { // Skip the escaped character; it may not have been read yet, which is fine. $i += 2; break; } // A quote: either the end of the string or a doubled quote. if ( $i + 1 >= $len && ! $this->eof ) { $need_more = true; break; } if ( $i + 1 < $len && $buf[ $i + 1 ] === $quote ) { $i += 2; break; } $state = self::NORMAL; ++$i; break; case self::TICK: $p = strpos( $buf, '`', $i ); if ( false === $p ) { $i = $len; break; } $i = $p + 1; $state = self::NORMAL; break; case self::LINE: $p = strpos( $buf, "\n", $i ); if ( false === $p ) { $i = $len; break; } $i = $p + 1; $state = self::NORMAL; break; case self::BLOCK: $p = strpos( $buf, '*/', $i ); if ( false === $p ) { // Keep the last byte in play: a "*" there may be completed by "/" in the next read. $i = max( $i, $len - 1 ); $need_more = true; break; } $i = $p + 2; $state = self::NORMAL; break; } } if ( $this->eof ) { if ( null !== $start && self::NORMAL === $state ) { $this->pos = strlen( $this->buf ); return rtrim( substr( $this->buf, $start ) ); } if ( null !== $start && in_array( $state, array( self::SINGLE, self::DOUBLE, self::TICK ), true ) ) { throw new \RuntimeException( sprintf( 'Unterminated quoted string in the SQL file near byte %d.', (int) ( $this->base + $start ) ) ); } $this->pos = strlen( $this->buf ); return null; } // Refill. Drop consumed bytes first and shift our indexes to match. if ( $this->pos > 0 ) { $this->buf = (string) substr( $this->buf, $this->pos ); $this->base += $this->pos; $i -= $this->pos; if ( null !== $start ) { $start -= $this->pos; } $this->pos = 0; } $chunk = fread( $this->handle, $this->read_size ); if ( false === $chunk || '' === $chunk ) { $this->eof = true; } else { $this->buf .= $chunk; } } } }