Initial commit
This commit is contained in:
54
src/Database.php
Normal file
54
src/Database.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Hpz937\Phpvault;
|
||||
|
||||
use SQLite3;
|
||||
|
||||
class Database {
|
||||
private $db;
|
||||
|
||||
public function __construct() {
|
||||
$this->db = new SQLite3(__DIR__ . '/../database/db.sqlite');
|
||||
if (!$this->db) {
|
||||
echo 'Unable to connect to database';
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
public function query($query) {
|
||||
return $this->db->query($query);
|
||||
}
|
||||
|
||||
public function createTables() {
|
||||
$queries = [
|
||||
'CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
password TEXT NOT NULL
|
||||
)',
|
||||
'CREATE TABLE IF NOT EXISTS vault (
|
||||
id INTEGER PRIMARY KEY,
|
||||
username TEXT NOT NULL,
|
||||
vaultname TEXT NOT NULL,
|
||||
encrypted_data TEXT NOT NULL
|
||||
)',
|
||||
'CREATE INDEX IF NOT EXISTS username_idx ON users (username)',
|
||||
'CREATE UNIQUE INDEX IF NOT EXISTS vault_unique_idx ON vault (username, vaultname);',
|
||||
];
|
||||
|
||||
foreach ($queries as $query) {
|
||||
if (!$this->db->exec($query)) {
|
||||
echo 'Error creating tables: ' . $this->db->lastErrorMsg();
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getDb() {
|
||||
return $this->db;
|
||||
}
|
||||
|
||||
public function close() {
|
||||
$this->db->close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user