index/src/Data/SQLite/SQLiteConnectionInfo.php

117 lines
3.6 KiB
PHP

<?php
// SQLiteConnectionInfo.php
// Created: 2021-05-02
// Updated: 2022-02-28
namespace Index\Data\SQLite;
use Index\Data\IDbConnectionInfo;
/**
* Represents information about a SQLite Client.
*/
class SQLiteConnectionInfo implements IDbConnectionInfo {
private string $fileName;
private bool $readOnly;
private bool $create;
private string $encryptionKey;
/**
* Creates a new SQLiteConnectionInfo instance.
*
* @param string $fileName Path to the SQLite database.
* @param string $encryptionKey Key to encrypt the database with.
* @param bool $readOnly Set to true if the database should be opened read-only.
* @param bool $create Set to true to create the database if it does not exist.
* @return SQLiteConnectionInfo Information to create an SQLite database client.
*/
public function __construct(
string $fileName,
string $encryptionKey,
bool $readOnly,
bool $create
) {
$this->fileName = $fileName;
$this->encryptionKey = $encryptionKey;
$this->readOnly = $readOnly;
$this->create = $create;
}
/**
* Gets the path of the SQLite database.
*/
public function getFileName(): string {
return $this->fileName;
}
/**
* Returns the key to use for encryption of the database.
*
* @return string Encryption key.
*/
public function getEncryptionKey(): string {
return $this->encryptionKey;
}
/**
* Returns whether the SQLite database should be opened read-only.
*
* @return bool true if the database should be opened read-only, false if not.
*/
public function shouldReadOnly(): bool {
return $this->readOnly;
}
/**
* Returns whether the SQLite database should be created if it does not exist.
*
* @return bool true if the database should be created, false if not.
*/
public function shouldCreate(): bool {
return $this->create;
}
/**
* Creates a connection info instance with a path.
*
* @param string $path Path to the SQLite database.
* @param string $encryptionKey Key to encrypt the database with.
* @param bool $readOnly Set to true if the database should be opened read-only.
* @param bool $create Set to true to create the database if it does not exist.
* @return SQLiteConnectionInfo Information to create an SQLite database client.
*/
public static function createPath(
string $path,
string $encryptionKey = '',
bool $readOnly = false,
bool $create = true
): SQLiteConnectionInfo {
return new SQLiteConnectionInfo(
$path,
$encryptionKey,
$readOnly,
$create
);
}
/**
* Creates a connection info instance for an in-memory database.
*
* @param string $encryptionKey Key to encrypt the database with.
* @return SQLiteConnectionInfo Information to create an SQLite database client.
*/
public static function createMemory(string $encryptionKey = ''): SQLiteConnectionInfo {
return new SQLiteConnectionInfo(':memory:', $encryptionKey, false, true);
}
/**
* Creates a connection info instance for a database stored in a temporary file.
*
* @param string $encryptionKey Key to encrypt the database with.
* @return SQLiteConnectionInfo Information to create an SQLite database client.
*/
public static function createTemp(string $encryptionKey = ''): SQLiteConnectionInfo {
return new SQLiteConnectionInfo('', $encryptionKey, false, true);
}
}