/** * Execute the query and update the record * * @param mixed $v1 if $v2 is defined indicates the name of the column to update, otherwise the array (name column => value columns) * @param mixed $v2 optional value of the column to update * @return int number of row involved in the update */ public function update($v1 = [], $v2 = NULL) { $t = clone $this; $set = empty($t->getBuilder()->getUpdate()) ? $v1 : array_merge($v1, $t->getBuilder()->getUpdate()); $data = []; if (empty($set)) { return 0; } # Update multiple records in different case if (is_array($set) && is_array($v2)) { foreach ($set as $k => $v) { if (is_array($v2[$k])) { $s = []; foreach ($v2[$k] as $n1 => $k1) { $s[] = DB::SQL()::UPDATE_WHEN($t->setPrepare($n1), $t->setPrepare($k1)); $where[] = $n1; } $data[] = DB::SQL()::UPDATE_CASE($v[1], $v[0], $v[1], $s); } else { $data[] = DB::SQL()::UPDATE_VALUE($v, $t->setPrepare($v2[$k])); } } # Update single column } else { if (!is_array($set) && isset($v2)) { $data[] = DB::SQL()::UPDATE_VALUE($set, $t->setPrepare($v2)); # Update multiple column } else { foreach ($set as $k => $v) { $data[] = DB::SQL()::UPDATE_VALUE($k, $t->setPrepare($v)); } } } $r = DB::count($q = $t->query(DB::SQL()::UPDATE($this->getBuilderTable(), $this->SQL_JOIN(), $data, $this->SQL_WHERE()))); return $r == 0 && $q ? 1 : $r; }
public function testRestore() { # -------------------------------------------------------------------------- DB::query("TRUNCATE users"); # -------------------------------------------------------------------------- # Insert some data DB::query("INSERT INTO users (username,password) VALUES ('admin','admin')"); # -------------------------------------------------------------------------- # Save the table DB::save('users'); # Perform some query DB::query("TRUNCATE users"); # Now users is recovered! DB::undo(); # Check $this->assertEquals(DB::count(DB::query("SELECT * FROM users")), 1); # -------------------------------------------------------------------------- # Save the table DB::save('users'); # Perform some query DB::query("TRUNCATE users"); # Now users is empty! DB::confirm(); # Check $this->assertEquals(DB::count(DB::query("SELECT * FROM users")), 0); # After several rows of code... # Restore the LAST SAVE POINT DB::restore(); # Check $this->assertEquals(DB::count(DB::query("SELECT * FROM users")), 1); # -------------------------------------------------------------------------- # But WAIT! You can undo the previous restore! DB::restore(); # Check $this->assertEquals(DB::count(DB::query("SELECT * FROM users")), 0); # -------------------------------------------------------------------------- # Oh yeah, restore the restore of restore point! (undo x3) DB::restore(); # Check $this->assertEquals(DB::count(DB::query("SELECT * FROM users")), 1); # -------------------------------------------------------------------------- DB::query("TRUNCATE users"); # -------------------------------------------------------------------------- }