You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
213 lines
5.9 KiB
213 lines
5.9 KiB
3 years ago
|
<?php
|
||
|
|
||
3 years ago
|
class SQLite {
|
||
|
|
||
|
public function __construct($database, $username, $password) {
|
||
3 years ago
|
|
||
|
try {
|
||
|
|
||
3 years ago
|
$this->_db = new PDO('sqlite:' . $database, $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']);
|
||
3 years ago
|
$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());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function getLastBlock() {
|
||
|
|
||
|
try {
|
||
|
|
||
|
$query = $this->_db->query('SELECT MAX(`blockId`) AS `lastBlock` FROM `block`')->fetch();
|
||
|
|
||
3 years ago
|
return (int) $query['lastBlock'];
|
||
3 years ago
|
|
||
|
} catch(PDOException $e) {
|
||
|
trigger_error($e->getMessage());
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function getBlock($blockId) {
|
||
|
|
||
|
try {
|
||
|
|
||
|
$query = $this->_db->prepare('SELECT `blockId` FROM `block` WHERE `blockId` = ? LIMIT 1');
|
||
|
|
||
|
$query->execute([$blockId]);
|
||
|
|
||
3 years ago
|
$result = $query->fetch();
|
||
|
|
||
|
return $result ? $result['blockId'] : false;
|
||
3 years ago
|
|
||
|
} catch(PDOException $e) {
|
||
|
trigger_error($e->getMessage());
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function getNameSpace($hash) {
|
||
|
|
||
|
try {
|
||
|
|
||
|
$query = $this->_db->prepare('SELECT `nameSpaceId` FROM `namespace` WHERE `hash` = ? LIMIT 1');
|
||
|
|
||
|
$query->execute([$hash]);
|
||
|
|
||
3 years ago
|
$result = $query->fetch();
|
||
|
|
||
|
return $result ? $result['nameSpaceId'] : false;
|
||
3 years ago
|
|
||
|
} catch(PDOException $e) {
|
||
|
trigger_error($e->getMessage());
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
public function getData($txId) {
|
||
3 years ago
|
|
||
|
try {
|
||
|
|
||
3 years ago
|
$query = $this->_db->prepare('SELECT `dataId` FROM `data` WHERE `txId` = ? LIMIT 1');
|
||
3 years ago
|
|
||
3 years ago
|
$query->execute([$txId]);
|
||
3 years ago
|
|
||
3 years ago
|
$result = $query->fetch();
|
||
|
|
||
|
return $result ? $result['dataId'] : false;
|
||
3 years ago
|
|
||
|
} catch(PDOException $e) {
|
||
|
trigger_error($e->getMessage());
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function addBlock($blockId) {
|
||
|
|
||
|
try {
|
||
|
|
||
3 years ago
|
$query = $this->_db->prepare('INSERT INTO `block` (`blockId`, `lostTransactions`, `timeIndexed`)
|
||
|
VALUES (?, 0, ?)');
|
||
3 years ago
|
|
||
3 years ago
|
$query->execute([$blockId, time()]);
|
||
3 years ago
|
|
||
|
return $blockId;
|
||
|
|
||
|
} catch(PDOException $e) {
|
||
|
trigger_error($e->getMessage());
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
public function setLostTransactions($blockId, $lostTransactions) {
|
||
|
|
||
|
try {
|
||
|
|
||
|
$query = $this->_db->prepare('UPDATE `block` SET `lostTransactions` = ? WHERE `blockId` = ? LIMIT 1');
|
||
|
|
||
|
$query->execute([$lostTransactions, $blockId]);
|
||
|
|
||
|
return $blockId;
|
||
|
|
||
|
} catch(PDOException $e) {
|
||
|
trigger_error($e->getMessage());
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function addNameSpace($hash) {
|
||
3 years ago
|
|
||
|
try {
|
||
|
|
||
3 years ago
|
$query = $this->_db->prepare('INSERT INTO `namespace` (`hash`, `timeIndexed`)
|
||
|
VALUES (?, ?)');
|
||
3 years ago
|
|
||
3 years ago
|
$query->execute([$hash, time()]);
|
||
3 years ago
|
|
||
|
return $this->_db->lastInsertId();
|
||
|
|
||
|
} catch(PDOException $e) {
|
||
|
trigger_error($e->getMessage());
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
public function addData($blockId, $nameSpaceId, $time, $size, $txid, $key, $value, $ns, $deleted = false) {
|
||
3 years ago
|
|
||
|
try {
|
||
|
|
||
3 years ago
|
$query = $this->_db->prepare('INSERT INTO `data` (
|
||
|
|
||
|
`blockId`,
|
||
|
`nameSpaceId`,
|
||
|
`time`,
|
||
|
`size`,
|
||
|
`txid`,
|
||
|
`key`,
|
||
|
`value`,
|
||
|
`deleted`,
|
||
|
`ns`,
|
||
|
`timeIndexed`
|
||
|
|
||
|
)
|
||
|
|
||
|
VALUES (
|
||
|
|
||
|
:blockId,
|
||
|
:nameSpaceId,
|
||
|
:time,
|
||
|
:size,
|
||
|
:txid,
|
||
|
:key,
|
||
|
:value,
|
||
|
:deleted,
|
||
|
:ns,
|
||
|
:timeIndexed
|
||
|
|
||
|
)');
|
||
3 years ago
|
|
||
3 years ago
|
$query->bindValue(':blockId', $blockId, PDO::PARAM_INT);
|
||
|
$query->bindValue(':nameSpaceId', $nameSpaceId, PDO::PARAM_INT);
|
||
|
$query->bindValue(':time', $time, PDO::PARAM_INT);
|
||
|
$query->bindValue(':size', $size, PDO::PARAM_INT);
|
||
|
$query->bindValue(':txid', $txid, PDO::PARAM_STR);
|
||
|
$query->bindValue(':key', $key, PDO::PARAM_STR);
|
||
|
$query->bindValue(':value', $value, PDO::PARAM_STR);
|
||
|
$query->bindValue(':deleted', (int) $deleted, PDO::PARAM_STR);
|
||
|
$query->bindValue(':ns', (int) $ns, PDO::PARAM_STR);
|
||
3 years ago
|
$query->bindValue(':timeIndexed', time(), PDO::PARAM_INT);
|
||
3 years ago
|
|
||
|
$query->execute();
|
||
3 years ago
|
|
||
|
return $this->_db->lastInsertId();
|
||
|
|
||
|
} catch(PDOException $e) {
|
||
|
trigger_error($e->getMessage());
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
3 years ago
|
|
||
|
public function setDataKeyDeleted($nameSpaceId, $key, $deleted) {
|
||
|
|
||
|
try {
|
||
|
|
||
|
$query = $this->_db->prepare('UPDATE `data` SET `deleted` = :deleted
|
||
|
WHERE `nameSpaceId` = :nameSpaceId AND `key` LIKE :key');
|
||
|
|
||
|
$query->bindValue(':nameSpaceId', $nameSpaceId, PDO::PARAM_INT);
|
||
|
$query->bindValue(':key', $key, PDO::PARAM_STR);
|
||
|
$query->bindValue(':deleted', (int) $deleted, PDO::PARAM_STR);
|
||
|
|
||
|
$query->execute();
|
||
|
|
||
|
return $query->rowCount();
|
||
|
|
||
|
} catch(PDOException $e) {
|
||
|
trigger_error($e->getMessage());
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
3 years ago
|
}
|