webapp/library/sqlite.php

269 lines
10 KiB
PHP
Raw Normal View History

2021-08-07 10:37:01 +00:00
<?php
2021-08-08 10:10:00 +00:00
class SQLite {
2021-08-07 10:37:01 +00:00
2022-07-15 13:51:05 +00:00
private PDO $_db;
2022-07-15 07:34:25 +00:00
2022-07-15 13:51:05 +00:00
public function __construct(string $database, string $username, string $password) {
2021-08-07 10:37:01 +00:00
try {
2022-07-15 13:51:05 +00:00
$this->_db = new PDO('sqlite:' . $database, $username, $password);
2021-08-07 10:37:01 +00:00
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$this->_db->setAttribute(PDO::ATTR_TIMEOUT, 600);
$this->_db->query('
CREATE TABLE IF NOT EXISTS "namespace"(
"nameSpaceId" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK("nameSpaceId">=0),
"timeIndexed" INTEGER NOT NULL CHECK("timeIndexed">=0),
"hash" CHAR(34) NOT NULL,
CONSTRAINT "hash_UNIQUE"
UNIQUE("hash")
)
');
$this->_db->query('
CREATE TABLE IF NOT EXISTS "block"(
"blockId" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK("blockId">=0),
"timeIndexed" INTEGER NOT NULL CHECK("timeIndexed">=0),
"lostTransactions" INTEGER NOT NULL CHECK("lostTransactions">=0)
)
');
$this->_db->query('
CREATE TABLE IF NOT EXISTS "data"(
"dataId" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK("dataId">=0),
"nameSpaceId" INTEGER NOT NULL CHECK("nameSpaceId">=0),
"blockId" INTEGER NOT NULL CHECK("blockId">=0),
"time" INTEGER NOT NULL CHECK("time">=0),
"timeIndexed" INTEGER NOT NULL CHECK("timeIndexed">=0),
"size" INTEGER NOT NULL,
"ns" TEXT NOT NULL CHECK("ns" IN(\'0\', \'1\')),
"deleted" TEXT NOT NULL CHECK("deleted" IN(\'0\', \'1\')),
"txid" CHAR(64) NOT NULL,
"key" TEXT NOT NULL,
"value" TEXT NOT NULL,
CONSTRAINT "txid_UNIQUE"
UNIQUE("txid"),
CONSTRAINT "fk_data_namespace"
FOREIGN KEY("nameSpaceId")
REFERENCES "namespace"("nameSpaceId"),
CONSTRAINT "fk_data_block"
FOREIGN KEY("blockId")
REFERENCES "block"("blockId")
)
');
$this->_db->query('CREATE INDEX IF NOT EXISTS "data.fk_data_namespase_idx" ON "data" ("nameSpaceId")');
$this->_db->query('CREATE INDEX IF NOT EXISTS "data.fk_data_block_idx" ON "data" ("blockId")');
$this->_db->query('CREATE INDEX IF NOT EXISTS "data.deleted_INDEX" ON "data" ("deleted")');
$this->_db->query('CREATE INDEX IF NOT EXISTS "data.ns_INDEX" ON "data" ("ns")');
2021-08-07 10:37:01 +00:00
} catch(PDOException $e) {
trigger_error($e->getMessage());
}
}
2022-07-15 13:51:05 +00:00
public function getNamespaceValueByNS(string $ns) {
2021-08-07 10:37:01 +00:00
try {
$query = $this->_db->prepare('SELECT `data`.`value` AS `value`
FROM `data`
JOIN `namespace` ON (`namespace`.`nameSpaceId` = `data`.`nameSpaceId`)
WHERE `namespace`.`hash` = ?
AND `data`.`ns` = "1"
2022-04-28 09:11:41 +00:00
-- AND `data`.`deleted` = "0" --
2021-08-07 10:37:01 +00:00
ORDER BY `data`.`blockId` DESC
LIMIT 1');
2022-05-13 16:41:00 +00:00
$query->execute([$ns]);
2021-08-07 10:37:01 +00:00
2021-08-08 10:54:40 +00:00
$result = $query->fetch();
2022-07-15 14:08:26 +00:00
return $result ? $result['value'] : '';
2021-08-07 10:37:01 +00:00
} catch(PDOException $e) {
trigger_error($e->getMessage());
return false;
}
}
2022-07-15 13:51:05 +00:00
public function getNamespaceHashByTX(string $txid) {
2022-05-13 16:41:00 +00:00
try {
$query = $this->_db->prepare('SELECT `namespace`.`hash`
FROM `namespace`
JOIN `data` ON (`data`.`nameSpaceId` = `namespace`.`nameSpaceId`)
WHERE `data`.`txid` = ?');
$query->execute([$txid]);
2022-07-15 14:08:26 +00:00
return $query->rowCount() ? $query->fetch()['hash'] : '';
2022-05-13 16:41:00 +00:00
} catch(PDOException $e) {
trigger_error($e->getMessage());
return false;
}
}
2022-07-15 14:43:11 +00:00
public function getData(string $namehash = '', string $txid = '', string $search = '', int $start = 0, int $limit = 10) {
2021-08-07 10:37:01 +00:00
try {
if ($txid) {
$query = $this->_db->prepare('SELECT `block`.`blockId` AS `block`,
`namespace`.`hash` AS `namehash`,
`data`.`time` AS `time`,
`data`.`key` AS `key`,
`data`.`value` AS `value`,
`data`.`txid` AS `txid`
FROM `data`
JOIN `block` ON (`block`.`blockId` = `data`.`blockId`)
JOIN `namespace` ON (`namespace`.`nameSpaceId` = `data`.`nameSpaceId`)
WHERE `data`.`txid` = ?
AND `data`.`ns` = "0"
2022-04-28 09:11:41 +00:00
-- AND `data`.`deleted` = "0" --
2021-08-07 10:37:01 +00:00
ORDER BY `block`.`blockId` DESC
LIMIT ' . (int) $start . ',' . (int) $limit);
$query->execute([$txid]);
} else if ($namehash) {
$query = $this->_db->prepare('SELECT `block`.`blockId` AS `block`,
`namespace`.`hash` AS `namehash`,
`data`.`time` AS `time`,
`data`.`key` AS `key`,
`data`.`value` AS `value`,
`data`.`txid` AS `txid`
FROM `data`
JOIN `block` ON (`block`.`blockId` = `data`.`blockId`)
JOIN `namespace` ON (`namespace`.`nameSpaceId` = `data`.`nameSpaceId`)
WHERE `namespace`.`hash` = ?
AND `data`.`ns` = "0"
2022-04-28 09:11:41 +00:00
-- AND `data`.`deleted` = "0" --
2021-08-07 10:37:01 +00:00
ORDER BY `block`.`blockId` DESC
LIMIT ' . (int) $start . ',' . (int) $limit);
$query->execute([$namehash]);
} else if ($search) {
$query = $this->_db->prepare('SELECT `block`.`blockId` AS `block`,
`namespace`.`hash` AS `namehash`,
`data`.`time` AS `time`,
`data`.`key` AS `key`,
`data`.`value` AS `value`,
`data`.`txid` AS `txid`
FROM `data`
JOIN `block` ON (`block`.`blockId` = `data`.`blockId`)
JOIN `namespace` ON (`namespace`.`nameSpaceId` = `data`.`nameSpaceId`)
WHERE (`data`.`key` LIKE :search
OR `data`.`value` LIKE :search
OR `block`.`blockId` LIKE :search
OR `namespace`.`hash` LIKE :search
OR `data`.`txid` LIKE :search)
AND `data`.`ns` = "0"
2022-04-28 09:11:41 +00:00
-- AND `data`.`deleted` = "0" --
2021-08-07 10:37:01 +00:00
2021-08-07 14:44:29 +00:00
ORDER BY `block`.`blockId` DESC
LIMIT ' . (int) $start . ',' . (int) $limit);
2021-08-07 10:37:01 +00:00
$query->bindValue(':search', '%' . $search . '%', PDO::PARAM_STR);
$query->execute();
} else {
$query = $this->_db->prepare('SELECT `block`.`blockId` AS `block`,
`namespace`.`hash` AS `namehash`,
`data`.`time` AS `time`,
`data`.`key` AS `key`,
`data`.`value` AS `value`,
`data`.`txid` AS `txid`
FROM `data`
JOIN `block` ON (`block`.`blockId` = `data`.`blockId`)
JOIN `namespace` ON (`namespace`.`nameSpaceId` = `data`.`nameSpaceId`)
WHERE `data`.`ns` = "0"
2022-04-28 09:11:41 +00:00
-- AND `data`.`deleted` = "0" --
2021-08-07 10:37:01 +00:00
ORDER BY `block`.`blockId` DESC
LIMIT ' . (int) $start . ',' . (int) $limit);
$query->execute();
}
2021-08-08 10:54:40 +00:00
$result = $query->fetchAll();
2021-08-07 10:37:01 +00:00
2021-08-08 10:54:40 +00:00
return $result ? $result : [];
2021-08-07 10:37:01 +00:00
} catch(PDOException $e) {
trigger_error($e->getMessage());
return false;
}
}
2022-10-27 12:30:31 +00:00
public function getTrends(int $offset = 0) {
try {
$query = $this->_db->prepare('SELECT `block`.`blockId` AS `block`,
`namespace`.`hash` AS `namehash`,
`data`.`time` AS `time`,
`data`.`key` AS `key`,
`data`.`value` AS `value`,
`data`.`txid` AS `txid`
FROM `data`
JOIN `block` ON (`block`.`blockId` = `data`.`blockId`)
JOIN `namespace` ON (`namespace`.`nameSpaceId` = `data`.`nameSpaceId`)
WHERE `data`.`ns` = "0"
AND `data`.`time` >= ' . (int) $offset . '
-- AND `data`.`deleted` = "0" --
ORDER BY `block`.`blockId` DESC');
$query->execute();
$result = $query->fetchAll();
return $result ? $result : [];
} catch(PDOException $e) {
trigger_error($e->getMessage());
return false;
}
}
2021-08-07 10:37:01 +00:00
}