Skip to content

Commit

Permalink
Option to delete Data from DB before insert
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Apr 14, 2011
1 parent 8192102 commit 11993fe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion sampleConfig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
<WriteHourOfDayData GraphFile="HourOfDay.png" CSVFile="HourOfDay.csv"/>
<WriteDayOfWeekData GraphFile="DayOfWeek.png" CSVFile="DayOfWeek.csv"/>
<WriteICalData File="Data.ical" />
<WriteDB DSN="mysql:host=localhost;dbname=CreepyCoder" UserName="cc" Password="cc" TableName="data" TimeFieldName="data" />
<WriteDB DSN="mysql:host=localhost;dbname=CreepyCoder" UserName="cc" Password="cc"
TableName="data" TimeFieldName="data" DeleteBeforeInsert="no" />
</config>
10 changes: 8 additions & 2 deletions src/WriteDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
Expand Down

0 comments on commit 11993fe

Please sign in to comment.