Browse Source

implement session features

PHP-GTK3
yggverse 4 months ago
parent
commit
644d1188c3
  1. 84
      src/Model/Database.php

84
src/Model/Database.php

@ -8,12 +8,19 @@ class Database
{ {
private \PDO $_database; private \PDO $_database;
private bool $_exists;
public function __construct( public function __construct(
string $filename, string $filename,
?string $username = null, ?string $username = null,
?string $password = null ?string $password = null
) { ) {
// Init database // Status
$this->_exists = file_exists(
$filename
);
// Init database connection
$this->_database = new \PDO( $this->_database = new \PDO(
sprintf( sprintf(
'sqlite:%s', 'sqlite:%s',
@ -33,17 +40,37 @@ class Database
\PDO::FETCH_OBJ \PDO::FETCH_OBJ
); );
// Init tables
$this->_database->query(' $this->_database->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,
"time" INTEGER NOT NULL, `time` INTEGER NOT NULL,
"url" VARCHAR(1024) NOT NULL, `url` VARCHAR(1024) NOT NULL,
"title" VARCHAR(255) `title` VARCHAR(255)
) )
'); ');
$this->_database->query('
CREATE TABLE IF NOT EXISTS `session`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL,
`request` VARCHAR(1024)
);
');
// Initial setup
if (!$this->_exists)
{
// Init welcome page
$this->addSession(
'gemini://yggverse.cities.yesterweb.org' // @TODO config
);
}
} }
// History
public function addHistory( public function addHistory(
string $url, string $url,
?string $title = null ?string $title = null
@ -61,7 +88,7 @@ class Database
] ]
); );
return (int) $this->_database->lastInsertId(); return $this->_database->lastInsertId();
} }
public function findHistory( public function findHistory(
@ -153,4 +180,47 @@ class Database
$title $title
); );
} }
// Session
public function addSession(
?string $request = null,
?int $time = null
): int
{
$query = $this->_database->prepare(
'INSERT INTO `session` (`time`, `request`) VALUES (:time, :request)'
);
$query->execute(
[
':time' => $time ? $time : time(),
':request' => $request
]
);
return $this->_database->lastInsertId();
}
public function getSession(): ?object
{
$query = $this->_database->query(
'SELECT * FROM `session`'
);
if ($session = $query->fetchAll())
{
return $session;
}
return null;
}
public function cleanSession(): int
{
$query = $this->_database->query(
'DELETE FROM `session`'
);
return $query->rowCount();
}
} }
Loading…
Cancel
Save