1
0
mirror of https://github.com/mbirth/tcl_update_db.git synced 2024-11-09 23:06:45 +00:00

Start work on db query and output.

This commit is contained in:
Markus Birth 2017-12-16 18:17:57 +01:00
parent ad530c6967
commit 1ea640b5f1
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
3 changed files with 139 additions and 4 deletions

View File

@ -40,7 +40,4 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
exit;
}
echo "Here is the normal page. " . $_SERVER['REQUEST_METHOD'];
// TODO: Show statistics from database
require_once 'index_main.php';

36
index_main.php Executable file
View File

@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<title>BlackBerry/TCL Firmware List</title>
</head>
<body>
<?php
require_once __DIR__ . '/lib/autoloader.php';
use \TclUpdates\SQLiteReader;
$db = new SQLiteReader();
$allVars = $db->getAllVariants();
foreach ($allVars as $family => $models) {
foreach ($models as $model => $variants) {
echo '<h2>' . $family . ' ' . $model . '</h2>' . PHP_EOL;
echo '<table>';
foreach ($variants as $ref => $name) {
echo '<tr><td>' . $ref . '</td>' . '</tr>' . PHP_EOL;
}
echo '</table>';
}
}
print_r($db->getAllUpdates('PRD-63117-011', $db::BOTH));
print_r($db->getLatestUpdate('PRD-63117-011', $db::BOTH));
print_r($db->getUnknownPrds());
?>
</body>
</html>

View File

@ -0,0 +1,102 @@
<?php
namespace TclUpdates;
class SQLiteReader
{
const OTA_ONLY = 0;
const FULL_ONLY = 1;
const BOTH =2;
private $dbFile;
private $pdo;
public function __construct()
{
$this->dbFile = 'otadb.db3';
$this->pdo = new \PDO('sqlite:' . $this->dbFile);
if ($this->pdo === false) {
return false;
}
$this->pdo->exec('PRAGMA foreign_keys=on;');
}
public function getAllPrds()
{
$sql = 'SELECT DISTINCT curef FROM updates ORDER BY curef;';
$sqlresult = $this->pdo->query($sql);
$result = array();
foreach ($sqlresult as $row) {
$result[] = $row[0];
}
return $result;
}
public function getAllKnownPrds()
{
$sql = 'SELECT DISTINCT ref FROM devices ORDER BY ref;';
$sqlresult = $this->pdo->query($sql);
$result = array();
foreach ($sqlresult as $row) {
$result[] = $row[0];
}
return $result;
}
public function getUnknownPrds()
{
$knownPrds = $this->getAllKnownPrds();
$allPrds = $this->getAllPrds();
$unknownPrds = array_diff($allPrds, $knownPrds);
return $unknownPrds;
}
public function getAllVariants()
{
$sql = 'SELECT f.name, m.name, d.ref, d.name FROM families f LEFT JOIN models m ON f.familyId=m.familyId LEFT JOIN devices d ON m.modelId=d.modelId;';
$sqlresult = $this->pdo->query($sql);
$result = array();
foreach ($sqlresult as $row) {
$family = $row[0];
$model = $row[1];
$ref = $row[2];
$variant = $row[3];
if (!isset($result[$family])) {
$result[$family] = array();
}
if (!isset($result[$family][$model])) {
$result[$family][$model] = array();
}
$result[$family][$model][$ref] = $variant;
}
return $result;
}
public function getAllUpdates($ref, $which = self::BOTH)
{
$sql = 'SELECT * FROM updates u LEFT JOIN files f ON u.file_sha1=f.sha1 WHERE curef=?';
if ($which == self::OTA_ONLY) {
$sql .= ' AND fv IS NOT null';
} elseif ($which == self::FULL_ONLY) {
$sql .= ' AND fv IS null';
}
$stmt = $this->pdo->prepare($sql);
$ok = $stmt->execute(array($ref));
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
return $result;
}
public function getLatestUpdate($ref, $which = self::BOTH)
{
$sql = 'SELECT * FROM updates u LEFT JOIN files f ON u.file_sha1=f.sha1 WHERE curef=?';
if ($which == self::OTA_ONLY) {
$sql .= ' AND fv IS NOT null';
} elseif ($which == self::FULL_ONLY) {
$sql .= ' AND fv IS null';
}
$sql .= ' ORDER BY tv DESC, fv DESC LIMIT 1';
$stmt = $this->pdo->prepare($sql);
$ok = $stmt->execute(array($ref));
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
return $result;
}
}