handle ) { $new = ! file_exists( $this->path ); $this->handle = new \SQLite3( $this->path ); // The file holds sealed keys, so it is for its owner and group alone (0660), whatever the umask: a // scheduled job such as WP-CLI running as another user of the same group can still write to it. Set // before WAL is turned on, so the -wal and -shm files SQLite makes beside it take the same mode. if ( $new && '/' === DIRECTORY_SEPARATOR && file_exists( $this->path ) ) { Storage::files()->chmod( $this->path, 0660 ); } $this->handle->enableExceptions( true ); $this->handle->busyTimeout( 5000 ); $this->handle->exec( 'PRAGMA journal_mode = WAL' ); $this->handle->exec( 'PRAGMA synchronous = NORMAL' ); $this->handle->exec( 'PRAGMA secure_delete = FAST' ); } return $this->handle; } /** * Rows a statement returns, keyed by column name. * * @param string $sql The statement, with `?` placeholders. * @param array $params Its values. * @return list> */ public function all( string $sql, array $params = array() ): array { $result = $this->execute( $sql, $params ); $rows = array(); // A statement that returns no rows, such as an INSERT through all(), gives none. if ( $result->numColumns() > 0 ) { $row = $result->fetchArray( SQLITE3_ASSOC ); while ( false !== $row ) { $rows[] = $row; $row = $result->fetchArray( SQLITE3_ASSOC ); } } $result->finalize(); return $rows; } /** * Runs a statement. * * @param string $sql The statement, with `?` placeholders. * @param array $params Its values. */ public function run( string $sql, array $params = array() ): void { $this->execute( $sql, $params )->finalize(); } /** * Runs an UPDATE or DELETE and says how many rows it matched. The store asks this rather than adding RETURNING, * since SQLite3 runs a statement once to start it and again to read its first row, so a RETURNING statement * would change the rows twice and report none. * * @param string $sql The statement, with `?` placeholders. * @param array $params Its values. */ public function affected( string $sql, array $params = array() ): int { $this->execute( $sql, $params )->finalize(); return $this->handle()->changes(); } public function transaction( callable $fn ): mixed { if ( $this->depth > 0 ) { return $fn( $this ); } return $this->within( 'BEGIN', $fn ); } /** * SQLite's lock is its write lock, taken up front with BEGIN IMMEDIATE, so a second request waits before it * reads anything, and a read-then-write in `$fn` cannot interleave with another's. * * @param callable $fn What to run. */ public function exclusive( callable $fn ): mixed { ++$this->held; try { return $this->depth > 0 || $this->held > 1 ? $fn( $this ) : $this->within( 'BEGIN IMMEDIATE', $fn ); } finally { --$this->held; } } public function close(): void { if ( null !== $this->handle ) { $this->handle->close(); $this->handle = null; } } /** * Runs `$fn` in a transaction begun with `$begin`, committed when it returns and rolled back when it throws. * * @param string $begin BEGIN, or BEGIN IMMEDIATE for the write lock. * @param callable $fn What to run. */ private function within( string $begin, callable $fn ): mixed { $handle = $this->handle(); $handle->exec( $begin ); ++$this->depth; try { $result = $fn( $this ); --$this->depth; $handle->exec( 'COMMIT' ); return $result; } catch ( \Throwable $error ) { $this->depth = 0; try { $handle->exec( 'ROLLBACK' ); } catch ( \Throwable $ignored ) { // Already rolled back by SQLite itself. unset( $ignored ); } throw $error; } } /** * A statement run with its values bound, as the package binds them: a float as text, which SQLite reads back * as the same number, and true or false as 1 or 0. * * @param string $sql The statement, with `?` placeholders. * @param array $params Its values. */ private function execute( string $sql, array $params ): \SQLite3Result { $statement = $this->handle()->prepare( $sql ); foreach ( array_values( $params ) as $i => $value ) { if ( null === $value ) { $statement->bindValue( $i + 1, null, SQLITE3_NULL ); } elseif ( is_bool( $value ) ) { $statement->bindValue( $i + 1, $value ? 1 : 0, SQLITE3_INTEGER ); } elseif ( is_int( $value ) ) { $statement->bindValue( $i + 1, $value, SQLITE3_INTEGER ); } elseif ( is_float( $value ) ) { $statement->bindValue( $i + 1, \Runlight\Json::number( $value ), SQLITE3_TEXT ); } else { $statement->bindValue( $i + 1, (string) $value, SQLITE3_TEXT ); } } return $statement->execute(); } }