2017-12-16 17:17:57 +00:00
< ? 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;' );
}
2017-12-16 18:42:24 +00:00
public function getAllRefs ()
2017-12-16 17:17:57 +00:00
{
$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 ;
}
2017-12-16 18:42:24 +00:00
public function getAllKnownRefs ()
2017-12-16 17:17:57 +00:00
{
$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 ;
}
2017-12-16 18:42:24 +00:00
public function getUnknownRefs ()
2017-12-16 17:17:57 +00:00
{
2017-12-16 18:42:24 +00:00
$knownPrds = $this -> getAllKnownRefs ();
$allPrds = $this -> getAllRefs ();
2017-12-16 17:17:57 +00:00
$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 ;
}
2017-12-17 01:15:59 +00:00
public function getAllVariantsFlat ()
{
$sql = 'SELECT f.name AS family, m.name AS model, d.ref, d.name AS variant 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 -> fetchAll ( \PDO :: FETCH_ASSOC ) as $row ) {
$result [ $row [ 'ref' ]] = $row [ 'family' ] . ' ' . $row [ 'model' ];
if ( strlen ( $row [ 'variant' ]) > 0 ) {
$result [ $row [ 'ref' ]] .= ' (' . $row [ 'variant' ] . ')' ;
}
}
return $result ;
}
2017-12-16 17:17:57 +00:00
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 );
2017-12-17 01:15:59 +00:00
if ( count ( $result ) == 1 ) {
$result = reset ( $result );
}
2017-12-16 17:17:57 +00:00
return $result ;
}
2017-12-16 18:42:24 +00:00
public function getAllVersionsForRef ( $ref = null )
{
$sql = 'SELECT fv, tv FROM updates u LEFT JOIN files f ON u.file_sha1=f.sha1' ;
$params_arr = array ();
if ( ! is_null ( $ref )) {
$sql .= ' WHERE curef=?' ;
$params_arr [] = $ref ;
}
$stmt = $this -> pdo -> prepare ( $sql );
$ok = $stmt -> execute ( $params_arr );
$result = $stmt -> fetchAll ( \PDO :: FETCH_ASSOC );
$version = array ();
foreach ( $result as $row ) {
if ( ! is_null ( $row [ 'fv' ])) {
$version [] = $row [ 'fv' ];
}
$version [] = $row [ 'tv' ];
}
$version = array_unique ( $version );
sort ( $version );
return $version ;
}
public function getAllVersionsForModel ( $model )
{
$sql = 'SELECT fv, tv FROM models m LEFT JOIN devices d ON m.modelId=d.modelId LEFT JOIN updates u ON d.ref=u.curef LEFT JOIN files f ON u.file_sha1=f.sha1 WHERE m.name=?' ;
$stmt = $this -> pdo -> prepare ( $sql );
$ok = $stmt -> execute ( array ( $model ));
$result = $stmt -> fetchAll ( \PDO :: FETCH_ASSOC );
$version = array ();
foreach ( $result as $row ) {
if ( ! is_null ( $row [ 'fv' ])) {
$version [] = $row [ 'fv' ];
}
if ( ! is_null ( $row [ 'tv' ])) {
$version [] = $row [ 'tv' ];
}
}
$version = array_unique ( $version );
sort ( $version );
return $version ;
}
2017-12-16 17:17:57 +00:00
}