From c6cbd890e35826b70b896b987d79de29200e59b1 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Mon, 11 Dec 2017 01:28:52 +0100 Subject: [PATCH] Ignore duplicate entries in db. --- lib/TclUpdates/SQLiteWriter.php | 12 ++++++++---- parse_files.php | 4 +++- sql/dbschema.sql | 6 ++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/TclUpdates/SQLiteWriter.php b/lib/TclUpdates/SQLiteWriter.php index 2171454..f081c36 100755 --- a/lib/TclUpdates/SQLiteWriter.php +++ b/lib/TclUpdates/SQLiteWriter.php @@ -19,7 +19,7 @@ class SQLiteWriter private function insertArray($table, $data) { $placeholders = array_fill(0, count($data), '?'); - $sql = 'INSERT OR REPLACE INTO "' . $table . '" (' . implode(', ', array_keys($data)) . ') VALUES (' . implode(', ', $placeholders) . ')'; + $sql = 'INSERT INTO "' . $table . '" (' . implode(', ', array_keys($data)) . ') VALUES (' . implode(', ', $placeholders) . ')'; $stmt = $this->pdo->prepare($sql); $ok = $stmt->execute(array_values($data)); return $ok; @@ -27,7 +27,7 @@ class SQLiteWriter public function addGotu(GotuObject $g) { - $this->insertArray('updates', array( + $ok = $this->insertArray('updates', array( 'tv' => $g->tv, 'fv' => $g->fv, 'svn' => $g->svn, @@ -45,7 +45,11 @@ class SQLiteWriter 'zh' => $g->description_zh, )) )); - $key = $this->pdo->lastInsertId(); - echo "Added entry " . $key . PHP_EOL; + if ($ok) { + $key = $this->pdo->lastInsertId(); + echo "Added entry " . $key . PHP_EOL; + } else { + echo "FAILED inserting." . PHP_EOL; + } } } diff --git a/parse_files.php b/parse_files.php index 4363a6b..19fa141 100755 --- a/parse_files.php +++ b/parse_files.php @@ -28,5 +28,7 @@ foreach ($file_list as $file) { echo 'Processing ' . $filename . PHP_EOL; $g = GotuObject::fromXmlParser($xp); print_r($g); - $sqlw->addGotu($g); + if ($g->tv) { + $sqlw->addGotu($g); + } } diff --git a/sql/dbschema.sql b/sql/dbschema.sql index 936a0df..c38fa04 100644 --- a/sql/dbschema.sql +++ b/sql/dbschema.sql @@ -55,6 +55,12 @@ CREATE TABLE "updates" ( "type" TEXT, -- FULL or OTA "note" TEXT -- some note for this file (optional) ); +CREATE UNIQUE INDEX "index_updates" ON "updates" ( + "tv", + "fv", + "fwId", + "file_id" +); -- Maps update files to devices CREATE TABLE "update_map" (