/** * 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(); }
public function testUpdate() { $s = new Sql(Db::factory('Sqlite', array('database' => __DIR__ . '/../tmp/test.sqlite')), 'users'); $s->update(array('username' => 'newuser'))->orderBy('id')->limit(1); $this->assertEquals('UPDATE "users" SET "username" = \'newuser\' ORDER BY "id" ASC LIMIT 1', $s->render(true)); }