Start adding SQLite database access
This commit is contained in:
1 parent
df226b2897
commit
adeb7a1b51
6 files changed
+176
No files matched your search
@@ -0,0 +1,85 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lewisdale\Webmentions\Gateways;
|
||||
|
||||
use Exception;
|
||||
use Lewisdale\Webmentions\Models\Webmention;
|
||||
use PDO;
|
||||
|
||||
class SqliteGateway extends WebmentionGatewayInterface {
|
||||
private readonly PDO $connection;
|
||||
|
||||
function __construct(string $name)
|
||||
{
|
||||
$this->connection = new PDO("sqlite:$name");
|
||||
$this->up();
|
||||
}
|
||||
|
||||
protected function up() : void
|
||||
{
|
||||
// Create Webmention table
|
||||
$sql = <<<SQL
|
||||
CREATE TABLE IF NOT EXISTS webmentions (
|
||||
id INTEGER PRIMARY KEY,
|
||||
target TEXT NOT NULL,
|
||||
source TEXT NOT NULL,
|
||||
content TEXT,
|
||||
author TEXT
|
||||
);
|
||||
SQL;
|
||||
|
||||
$this->connection->exec($sql);
|
||||
}
|
||||
|
||||
public function get(int $id): ?Webmention
|
||||
{
|
||||
$sql = "SELECT * FROM webmentions;";
|
||||
$statement = $this->connection->query($sql);
|
||||
|
||||
if ($statement == false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$row = $statement->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if ($row) {
|
||||
return new Webmention(
|
||||
$row["id"],
|
||||
$row["target"],
|
||||
$row["source"],
|
||||
$row["content"],
|
||||
$row["author"]
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getByPost(string $post): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function save(Webmention $webmention): ?int
|
||||
{
|
||||
$sql = <<< SQL
|
||||
INSERT INTO webmentions
|
||||
(id, target, source, content, author)
|
||||
VALUES
|
||||
(:id, :target, :source, :content, :author);
|
||||
SQL;
|
||||
|
||||
$statement = $this->connection->prepare($sql);
|
||||
$success = $statement->execute((array) $webmention);
|
||||
$statement->closeCursor();
|
||||
|
||||
return $success ? (int) $this->connection->lastInsertId() : null;
|
||||
}
|
||||
|
||||
public function delete(Webmention $webmention): void
|
||||
{
|
||||
throw new Exception("Method " . SqliteGateway::class . "::delete not implemented");
|
||||
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in new issue
Block a user