diff --git a/config-default.php b/config-default.php index 17d1785..9d06ba1 100644 --- a/config-default.php +++ b/config-default.php @@ -10,9 +10,7 @@ define('STEP_BLOCK_LIMIT', 50); // Blocks per query define('CRAWLER_DEBUG', true); // Debug output // Database -define('DB_HOST', '127.0.0.1'); -define('DB_PORT', '3306'); -define('DB_NAME', ''); +define('DB_NAME', 'kvazar.dat'); define('DB_USERNAME', ''); define('DB_PASSWORD', ''); diff --git a/crawler.php b/crawler.php index 4e068ca..332635b 100644 --- a/crawler.php +++ b/crawler.php @@ -5,7 +5,7 @@ $semaphore = sem_get(1); if (false !== sem_acquire($semaphore, 1)) { require_once('config.php'); - require_once('library/mysql.php'); + require_once('library/sqlite.php'); require_once('library/kevacoin.php'); require_once('library/hash.php'); require_once('library/base58.php'); @@ -13,7 +13,7 @@ if (false !== sem_acquire($semaphore, 1)) { require_once('library/crypto.php'); require_once('library/helper.php'); - $db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD); + $db = new SQLite(DB_NAME, DB_USERNAME, DB_PASSWORD); $kevaCoin = new KevaCoin(KEVA_PROTOCOL, KEVA_HOST, KEVA_PORT, KEVA_USERNAME, KEVA_PASSWORD); $blockLast = $db->getLastBlock(); diff --git a/library/mysql.php b/library/sqlite.php similarity index 60% rename from library/mysql.php rename to library/sqlite.php index 2ce0d97..5cf6754 100644 --- a/library/mysql.php +++ b/library/sqlite.php @@ -1,12 +1,12 @@ _db = new PDO('mysql:dbname=' . $database . ';host=' . $host . ';port=' . $port . ';charset=utf8', $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']); + $this->_db = new PDO('sqlite:' . $database, $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']); $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); @@ -38,7 +38,9 @@ class MySQL { $query->execute([$blockId]); - return $query->rowCount() ? $query->fetch()['blockId'] : false; + $result = $query->fetch(); + + return $result ? $result['blockId'] : false; } catch(PDOException $e) { trigger_error($e->getMessage()); @@ -54,7 +56,9 @@ class MySQL { $query->execute([$hash]); - return $query->rowCount() ? $query->fetch()['nameSpaceId'] : false; + $result = $query->fetch(); + + return $result ? $result['nameSpaceId'] : false; } catch(PDOException $e) { trigger_error($e->getMessage()); @@ -70,7 +74,9 @@ class MySQL { $query->execute([$txId]); - return $query->rowCount() ? $query->fetch()['dataId'] : false; + $result = $query->fetch(); + + return $result ? $result['dataId'] : false; } catch(PDOException $e) { trigger_error($e->getMessage()); @@ -82,11 +88,10 @@ class MySQL { try { - $query = $this->_db->prepare('INSERT INTO `block` SET `blockId` = ?, - `lostTransactions` = 0, - `timeIndexed` = UNIX_TIMESTAMP()'); + $query = $this->_db->prepare('INSERT INTO `block` (`blockId`, `lostTransactions`, `timeIndexed`) + VALUES (?, 0, ?)'); - $query->execute([$blockId]); + $query->execute([$blockId, time()]); return $blockId; @@ -116,10 +121,10 @@ class MySQL { try { - $query = $this->_db->prepare('INSERT INTO `namespace` SET `hash` = ?, - `timeIndexed` = UNIX_TIMESTAMP()'); + $query = $this->_db->prepare('INSERT INTO `namespace` (`hash`, `timeIndexed`) + VALUES (?, ?)'); - $query->execute([$hash]); + $query->execute([$hash, time()]); return $this->_db->lastInsertId(); @@ -133,16 +138,35 @@ class MySQL { try { - $query = $this->_db->prepare('INSERT INTO `data` SET `blockId` = :blockId, - `nameSpaceId` = :nameSpaceId, - `time` = :time, - `size` = :size, - `txid` = :txid, - `key` = :key, - `value` = :value, - `deleted` = :deleted, - `ns` = :ns, - `timeIndexed` = UNIX_TIMESTAMP()'); + $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 + + )'); $query->bindValue(':blockId', $blockId, PDO::PARAM_INT); $query->bindValue(':nameSpaceId', $nameSpaceId, PDO::PARAM_INT); @@ -153,6 +177,7 @@ class MySQL { $query->bindValue(':value', $value, PDO::PARAM_STR); $query->bindValue(':deleted', (int) $deleted, PDO::PARAM_STR); $query->bindValue(':ns', (int) $ns, PDO::PARAM_STR); + $query->bindValue(':timeIndexed', time(), PDO::PARAM_INT); $query->execute();