make database connection access protected

This commit is contained in:
yggverse 2024-08-02 22:20:12 +03:00
parent 298f25dc3c
commit 8c6627cfb8
7 changed files with 49 additions and 51 deletions

View file

@ -8,10 +8,8 @@ use \Pdo;
class Database
{
// Dependencies
public Pdo $connection;
protected Pdo $_connection;
// Requirements
public Database\Auth $auth;
public Database\Bookmark $bookmark;
public Database\Cache $cache;
@ -30,7 +28,7 @@ class Database
);
// Init dependencies
$this->connection = new Pdo(
$this->_connection = new Pdo(
sprintf(
'sqlite:%s',
$filename
@ -39,39 +37,39 @@ class Database
$password
);
$this->connection->setAttribute(
$this->_connection->setAttribute(
Pdo::ATTR_ERRMODE,
Pdo::ERRMODE_EXCEPTION
);
$this->connection->setAttribute(
$this->_connection->setAttribute(
Pdo::ATTR_DEFAULT_FETCH_MODE,
Pdo::FETCH_OBJ
);
// Init requirements
$this->auth = new Database\Auth(
$this->connection
$this->_connection
);
$this->bookmark = new Database\Bookmark(
$this->connection
$this->_connection
);
$this->cache = new Database\Cache(
$this->connection
$this->_connection
);
$this->history = new Database\History(
$this->connection
$this->_connection
);
$this->identity = new Database\Identity(
$this->connection
$this->_connection
);
$this->session = new Database\Session(
$this->connection
$this->_connection
);
// Init data

View file

@ -8,16 +8,16 @@ use \Pdo;
class Auth
{
public Pdo $connection;
protected Pdo $_connection;
public function __construct(
Pdo $connection
) {
// Init parent connection
$this->connection = $connection;
$this->_connection = $connection;
// Init database structure
$this->connection->query('
$this->_connection->query('
CREATE TABLE IF NOT EXISTS `auth`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,

View file

@ -8,16 +8,16 @@ use \Pdo;
class Bookmark
{
public Pdo $connection;
protected Pdo $_connection;
public function __construct(
Pdo $connection
) {
// Init parent connection
$this->connection = $connection;
$this->_connection = $connection;
// Init database structure
$this->connection->query('
$this->_connection->query('
CREATE TABLE IF NOT EXISTS `bookmark`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
@ -34,7 +34,7 @@ class Bookmark
?int $time = null
): int
{
$query = $this->connection->prepare(
$query = $this->_connection->prepare(
'INSERT INTO `bookmark` (
`time`,
`request`,
@ -55,7 +55,7 @@ class Bookmark
);
return intval(
$this->connection->lastInsertId()
$this->_connection->lastInsertId()
);
}
@ -63,7 +63,7 @@ class Bookmark
?string $request = null
): ?object
{
$query = $this->connection->prepare(
$query = $this->_connection->prepare(
'SELECT * FROM `bookmark` WHERE `request` LIKE :request'
);
@ -87,7 +87,7 @@ class Bookmark
int $limit = 1000
): array
{
$query = $this->connection->prepare(
$query = $this->_connection->prepare(
sprintf(
'SELECT * FROM `bookmark`
WHERE `request` LIKE :value OR `title` LIKE :value
@ -116,7 +116,7 @@ class Bookmark
int $id
): int
{
$query = $this->connection->query(
$query = $this->_connection->query(
sprintf(
'DELETE FROM `bookmark` WHERE `id` = %d',
$id

View file

@ -8,16 +8,16 @@ use \Pdo;
class Cache
{
public Pdo $connection;
protected Pdo $_connection;
public function __construct(
Pdo $connection
) {
// Init parent connection
$this->connection = $connection;
$this->_connection = $connection;
// Init database structure
$this->connection->query('
$this->_connection->query('
CREATE TABLE IF NOT EXISTS `cache`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
@ -42,7 +42,7 @@ class Cache
?int $time = null
): int
{
$query = $this->connection->prepare(
$query = $this->_connection->prepare(
'INSERT INTO `cache` (
`time`,
`request`,
@ -75,7 +75,7 @@ class Cache
);
return intval(
$this->connection->lastInsertId()
$this->_connection->lastInsertId()
);
}
@ -83,7 +83,7 @@ class Cache
string $request = ''
): ?object
{
$query = $this->connection->prepare(
$query = $this->_connection->prepare(
'SELECT * FROM `cache` WHERE `request` LIKE :request'
);
@ -105,7 +105,7 @@ class Cache
int $id
): int
{
$query = $this->connection->query(
$query = $this->_connection->query(
sprintf(
'DELETE FROM `cache` WHERE `id` = %d',
$id
@ -119,7 +119,7 @@ class Cache
int $timeout = 0
): int
{
$query = $this->connection->query(
$query = $this->_connection->query(
sprintf(
'DELETE FROM `cache` WHERE `time` + %d < %d',
$timeout,
@ -141,7 +141,7 @@ class Cache
): void
{
// Find same records match URL
$query = $this->connection->prepare(
$query = $this->_connection->prepare(
'SELECT * FROM `cache` WHERE `request` LIKE :request'
);

View file

@ -8,16 +8,16 @@ use \Pdo;
class History
{
public Pdo $connection;
protected Pdo $_connection;
public function __construct(
Pdo $connection
) {
// Init parent connection
$this->connection = $connection;
$this->_connection = $connection;
// Init database structure
$this->connection->query('
$this->_connection->query('
CREATE TABLE IF NOT EXISTS `history`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
@ -33,7 +33,7 @@ class History
?string $title = null
): int
{
$query = $this->connection->prepare(
$query = $this->_connection->prepare(
'INSERT INTO `history` (`time`, `url`, `title`) VALUES (:time, :url, :title)'
);
@ -46,7 +46,7 @@ class History
);
return intval(
$this->connection->lastInsertId()
$this->_connection->lastInsertId()
);
}
@ -56,7 +56,7 @@ class History
int $limit = 1000
): array
{
$query = $this->connection->prepare(
$query = $this->_connection->prepare(
sprintf(
'SELECT * FROM `history`
WHERE `url` LIKE :value OR `title` LIKE :value
@ -83,7 +83,7 @@ class History
int $id
): int
{
$query = $this->connection->query(
$query = $this->_connection->query(
sprintf(
'DELETE FROM `history` WHERE `id` = %d',
$id
@ -97,7 +97,7 @@ class History
int $timeout = 0
): int
{
$query = $this->connection->query(
$query = $this->_connection->query(
sprintf(
'DELETE FROM `history` WHERE `time` + %d < %d',
$timeout,
@ -115,7 +115,7 @@ class History
): void
{
// Find same records match URL
$query = $this->connection->prepare(
$query = $this->_connection->prepare(
'SELECT * FROM `history` WHERE `url` LIKE :url'
);

View file

@ -8,16 +8,16 @@ use \Pdo;
class Identity
{
public Pdo $connection;
protected Pdo $_connection;
public function __construct(
Pdo $connection
) {
// Init parent connection
$this->connection = $connection;
$this->_connection = $connection;
// Init database structure
$this->connection->query('
$this->_connection->query('
CREATE TABLE IF NOT EXISTS `identity`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,

View file

@ -8,16 +8,16 @@ use \Pdo;
class Session
{
public Pdo $connection;
protected Pdo $_connection;
public function __construct(
Pdo $connection
) {
// Init parent connection
$this->connection = $connection;
$this->_connection = $connection;
// Init database structure
$this->connection->query('
$this->_connection->query('
CREATE TABLE IF NOT EXISTS `session`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
@ -32,7 +32,7 @@ class Session
?int $time = null
): int
{
$query = $this->connection->prepare(
$query = $this->_connection->prepare(
'INSERT INTO `session` (`time`, `request`) VALUES (:time, :request)'
);
@ -44,13 +44,13 @@ class Session
);
return intval(
$this->connection->lastInsertId()
$this->_connection->lastInsertId()
);
}
public function get(): array
{
$query = $this->connection->query(
$query = $this->_connection->query(
'SELECT * FROM `session`'
);
@ -64,7 +64,7 @@ class Session
public function clean(): int
{
$query = $this->connection->query(
$query = $this->_connection->query(
'DELETE FROM `session`'
);