From 11993fe8f0ca1eff2ccc51cad1c313223dabfaba Mon Sep 17 00:00:00 2001 From: James Date: Fri, 15 Apr 2011 00:07:15 +0100 Subject: [PATCH] Option to delete Data from DB before insert --- sampleConfig.xml | 3 ++- src/WriteDB.php | 10 ++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sampleConfig.xml b/sampleConfig.xml index da480da..73dcd93 100644 --- a/sampleConfig.xml +++ b/sampleConfig.xml @@ -9,5 +9,6 @@ - + diff --git a/src/WriteDB.php b/src/WriteDB.php index 75f86e2..49ab3ce 100644 --- a/src/WriteDB.php +++ b/src/WriteDB.php @@ -25,16 +25,19 @@ class WriteDB extends BaseWriteClass { protected $password = "cc"; protected $tableName = "data"; protected $timeFieldName = "data"; + protected $deleteBeforeInsert = false; public function __construct($configData = array()) { if (get_class($configData) == 'DOMElement') { $this->dsn = $configData->getAttribute('DSN'); - // TODO: Only if ($configData->hasAttribute('UserName')) $this->userName = $configData->getAttribute('UserName'); if ($configData->hasAttribute('Password')) $this->password = $configData->getAttribute('Password'); if ($configData->hasAttribute('TableName')) $this->tableName = $configData->getAttribute('TableName'); if ($configData->hasAttribute('TimeFieldName')) $this->timeFieldName = $configData->getAttribute('TimeFieldName'); + if ($configData->hasAttribute('DeleteBeforeInsert')) { + $this->deleteBeforeInsert = substr(trim(strtolower($configData->getAttribute('DeleteBeforeInsert'))),0,1) == "y" ? true : false; + } } parent::__construct($configData); } @@ -44,8 +47,11 @@ public function write() { $pdo = new PDO($this->dsn, $this->userName, $this->password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $stat = $pdo->prepare("INSERT INTO ".$this->tableName." (".$this->timeFieldName.") VALUES (:d)"); + if ($this->deleteBeforeInsert) { + $pdo->exec("DELETE FROM ".$this->tableName); + } + $stat = $pdo->prepare("INSERT INTO ".$this->tableName." (".$this->timeFieldName.") VALUES (:d)"); foreach($this->dataManager->getData() as $item) { $stat->bindValue('d', $item->getDateTimeAs('Y-m-d H:i:s'), PDO::PARAM_STR); $stat->execute();