/** * Write to the log * * @param array $logEntry * @return Db */ public function writeLog(array $logEntry) { $columns = []; $params = []; $i = 1; foreach ($logEntry as $column => $value) { $placeholder = $this->sql->getPlaceholder(); if ($placeholder == ':') { $placeholder .= $column; } else { if ($placeholder == '$') { $placeholder .= $i; } } $columns[$column] = $placeholder; $params[$column] = $value; $i++; } $this->sql->insert($columns); $this->sql->db()->prepare((string) $this->sql)->bindParams($params)->execute(); return $this; }
/** * Method to save a value to cache. * * @param string $id * @param mixed $value * @param string $time * @return void */ public function save($id, $value, $time) { $time = time() + (int) $time; // Determine if the value already exists. $this->sqlite->select()->where()->equalTo('id', ':id'); $this->sqlite->adapter()->prepare($this->sqlite->render(true))->bindParams(array('id' => sha1($id)))->execute(); $rows = array(); while (($row = $this->sqlite->adapter()->fetchResult()) != false) { $rows[] = $row; } // If the value exists, update it. if (count($rows) > 0) { $this->sqlite->update(array('value' => ':value', 'time' => ':time'))->where()->equalTo('id', ':id'); $params = array('value' => serialize($value), 'time' => $time, 'id' => sha1($id)); // Else, save the new value. } else { $this->sqlite->insert(array('id' => ':id', 'value' => ':value', 'time' => ':time')); $params = array('id' => sha1($id), 'value' => serialize($value), 'time' => $time); } $this->sqlite->adapter()->prepare($this->sqlite->render(true))->bindParams($params)->execute(); }
/** * Write to a custom log * * @param string $content * @return Db */ public function writeCustomLog($content) { $fields = ['timestamp' => date('Y-m-d H:i:s'), 'level' => -1, 'name' => 'CUSTOM', 'message' => $content, 'context' => '']; $columns = []; $params = []; $i = 1; foreach ($fields as $column => $value) { $placeholder = $this->sql->getPlaceholder(); if ($placeholder == ':') { $placeholder .= $column; } else { if ($placeholder == '$') { $placeholder .= $i; } } $columns[$column] = $placeholder; $params[$column] = $value; $i++; } $this->sql->insert($columns); $this->sql->db()->prepare((string) $this->sql)->bindParams($params)->execute(); return $this; }
public function testInsertException() { $this->setExpectedException('Pop\\Db\\Exception'); $s = new Sql(Db::factory('Sqlite', array('database' => __DIR__ . '/../tmp/test.sqlite')), 'users'); $s->insert(); }