webapp/library/sqlite.php

187 lines
7.1 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 07:34:25 +00:00
private _$db;
2021-08-08 10:10:00 +00:00
public function __construct($database, $username, $password) {
2021-08-07 10:37:01 +00:00
try {
2021-08-08 10:10:00 +00:00
$this->_db = new PDO('sqlite:' . $database, $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']);
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);
} catch(PDOException $e) {
trigger_error($e->getMessage());
}
}
2022-05-13 16:41:00 +00:00
public function getNamespaceValueByNS($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();
return $result ? $result['value'] : false;
2021-08-07 10:37:01 +00:00
} catch(PDOException $e) {
trigger_error($e->getMessage());
return false;
}
}
2022-05-13 16:41:00 +00:00
public function getNamespaceHashByTX($txid) {
try {
$query = $this->_db->prepare('SELECT `namespace`.`hash`
FROM `namespace`
JOIN `data` ON (`data`.`nameSpaceId` = `namespace`.`nameSpaceId`)
WHERE `data`.`txid` = ?');
$query->execute([$txid]);
return $query->rowCount() ? $query->fetch()['hash'] : [];
} catch(PDOException $e) {
trigger_error($e->getMessage());
return false;
}
}
2021-08-07 10:37:01 +00:00
public function getData($namehash = false, $txid = false, $search = false, $start = 0, $limit = 10) {
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;
}
}
}