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

6
src/Model/Database/Auth.php

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

16
src/Model/Database/Bookmark.php

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

18
src/Model/Database/Cache.php

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

18
src/Model/Database/History.php

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

6
src/Model/Database/Identity.php

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

14
src/Model/Database/Session.php

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

Loading…
Cancel
Save