Browse Source

make database connection access protected

PHP-GTK3
yggverse 4 months ago
parent
commit
8c6627cfb8
  1. 22
      src/Model/Database.php
  2. 6
      src/Model/Database/Auth.php
  3. 16
      src/Model/Database/Bookmark.php
  4. 18
      src/Model/Database/Cache.php
  5. 18
      src/Model/Database/History.php
  6. 6
      src/Model/Database/Identity.php
  7. 14
      src/Model/Database/Session.php

22
src/Model/Database.php

@ -8,10 +8,8 @@ use \Pdo; @@ -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 @@ -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 @@ -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

6
src/Model/Database/Auth.php

@ -8,16 +8,16 @@ use \Pdo; @@ -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,

16
src/Model/Database/Bookmark.php

@ -8,16 +8,16 @@ use \Pdo; @@ -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 @@ -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 @@ -55,7 +55,7 @@ class Bookmark
);
return intval(
$this->connection->lastInsertId()
$this->_connection->lastInsertId()
);
}
@ -63,7 +63,7 @@ class Bookmark @@ -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 @@ -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 @@ -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

18
src/Model/Database/Cache.php

@ -8,16 +8,16 @@ use \Pdo; @@ -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 @@ -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 @@ -75,7 +75,7 @@ class Cache
);
return intval(
$this->connection->lastInsertId()
$this->_connection->lastInsertId()
);
}
@ -83,7 +83,7 @@ class Cache @@ -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 @@ -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 @@ -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 @@ -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'
);

18
src/Model/Database/History.php

@ -8,16 +8,16 @@ use \Pdo; @@ -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 @@ -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 @@ -46,7 +46,7 @@ class History
);
return intval(
$this->connection->lastInsertId()
$this->_connection->lastInsertId()
);
}
@ -56,7 +56,7 @@ class History @@ -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 @@ -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 @@ -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 @@ -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'
);

6
src/Model/Database/Identity.php

@ -8,16 +8,16 @@ use \Pdo; @@ -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,

14
src/Model/Database/Session.php

@ -8,16 +8,16 @@ use \Pdo; @@ -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 @@ -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 @@ -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 @@ -64,7 +64,7 @@ class Session
public function clean(): int
{
$query = $this->connection->query(
$query = $this->_connection->query(
'DELETE FROM `session`'
);

Loading…
Cancel
Save