Example #1
0
 public function revert(array $allRows)
 {
     foreach ($allRows as $row) {
         switch ($row->type) {
             case Database::TRIGGER_ACTION_INSERT:
                 $sql = "DELETE FROM {$row->table} WHERE {$row->primarycolumn} = :primary";
                 $this->dbms->executeQuery($sql, array('primary' => $row->primaryvalue));
                 break;
             case Database::TRIGGER_ACTION_DELETE:
                 $data = (array) $row->data;
                 $columns = array_keys($data);
                 $values = $columns;
                 array_walk($values, function (&$value, &$key) {
                     $value = ':' . $value;
                 });
                 $sql = "INSERT INTO {$row->table} (" . implode(', ', $columns) . ") VALUES(" . implode(', ', $values) . ")";
                 $this->dbms->executeQuery($sql, $data);
                 break;
             case Database::TRIGGER_ACTION_UPDATE:
                 $set = array();
                 $params = array();
                 foreach ($row->previous as $column => $value) {
                     $set[] = $column . ' = :' . $column;
                     $params[':' . $column] = $value;
                 }
                 $params['primarykeyvalue'] = $row->primaryvalue;
                 $sql = "UPDATE {$row->table} SET " . implode(', ', $set) . " WHERE {$row->primarycolumn} = :primarykeyvalue";
                 $this->dbms->executeQuery($sql, $params);
                 break;
         }
     }
     return true;
 }
Example #2
0
 /**
  * Display stats.
  * @return bool
  */
 public function showStatsList()
 {
     $sql = "SELECT groupid, MIN(timeadded) AS mintime, MAX(timeadded) AS maxtime, COUNT(id) AS numactions\n                FROM dbtrack_actions\n                GROUP BY groupid\n                ORDER BY MIN(timeadded)";
     $results = $this->dbms->getResults($sql);
     if (empty($results)) {
         $this->display->outputMessage('No stats found.');
         return true;
     }
     $this->display->setHeader(array('Group', 'From', 'To', 'Actions'));
     $this->display->setPadding(array(5, 19, 19, 10));
     $this->display->showHeader();
     foreach ($results as $result) {
         $line = array($result->groupid, date('d/m/Y H:i:s', $result->mintime), date('d/m/Y H:i:s', $result->maxtime), $result->numactions);
         $this->display->showLine($line);
     }
     return true;
 }
Example #3
0
 /**
  * Display final output.
  * @param array $fullRows
  * @param array $actions
  * @throws \Exception
  */
 protected function displayRows(array $fullRows, array $actions)
 {
     // The position is the same for $fullRows and $changedRows (for cross referencing purposes).
     $rowCount = 0;
     foreach ($fullRows as $row) {
         // First column is the row's table and then all the columns.
         $header = array_merge(array($row->table), array_keys((array) $row->data));
         // Add action type as the first column in the data row.
         $line = array($this->dbms->getTrackTypeDescription($row->type));
         $padding = strlen($header[0]) > strlen($line[0]) ? array(strlen($header[0])) : array(strlen($line[0]));
         // Column counter.
         $pos = 0;
         foreach ($row->data as $column => $value) {
             ++$pos;
             if ($this->valueDisplayLength > 0 && strlen($value) > $this->valueDisplayLength) {
                 $value = substr($value, 0, 20) . '...';
             }
             // Make sure the column's display width is long enough.
             $padding[] = strlen($value) > strlen($header[$pos]) ? strlen($value) : strlen($header[$pos]);
             // Highlight only changed columns.
             if ($row->type == Database::TRIGGER_ACTION_UPDATE) {
                 if (isset($actions[$rowCount]->data->{$column}) && $row->primarycolumn != $column) {
                     $value = $this->display->getColourText($value, 'yellow');
                 }
             }
             $line[] = $value;
         }
         // Display line.
         $this->display->setHeader($header);
         $this->display->setPadding($padding);
         $this->display->showHeader();
         $this->display->showLine($line);
         $this->display->showLine();
         ++$rowCount;
     }
 }