/** * Delete from table. * * @param string $table * @param string $where * @param array $bind * @return int Number of affected rows */ public function delete($table, $where = '', array $bind = array()) { $table = $this->adapter->quoteIdentifier($table); $sql = "DELETE FROM {$table}" . ($where ? " WHERE {$where}" : ''); $stmt = $this->adapter->query($sql, $bind); return $stmt->rowCount(); }
public function testCheckForInspectedRowLimitOnSuccess() { $bigResult = $this->getMockBuilder(BigResult::class)->setConstructorArgs([$this->adapter])->setMethods(['getInspectedRows'])->getMock(); $bigResult->expects($this->any())->method('getInspectedRows')->will($this->returnValue(5)); $this->adapter->expects($this->once())->method('query'); $bigResult->query('SELECT', [], 10); }
public function testRemove() { $pdoStatement = $this->getMock(\PDOStatement::class); $pdoStatement->expects($this->any())->method('rowCount')->will($this->returnValue(1)); $this->adapter->expects($this->any())->method('query')->will($this->returnValue($pdoStatement)); $scheduler = new DbScheduler($this->adapter); $this->assertTrue($scheduler->remove(234)); }
/** * @dataProvider deleteDataProvider */ public function testDelete($expectedSql, $table, $where, $bind) { $executeArgs = is_null($bind) ? [] : $bind; // Returned stmt will have rowCount called $pdoStatement = $this->getMock('\\PDOStatement'); $pdoStatement->expects($this->once())->method('rowCount'); // Query should be called with the SQL and bind $this->adapter->expects($this->once())->method('query')->with($expectedSql, $executeArgs)->will($this->returnValue($pdoStatement)); $result = !is_null($where) ? !is_null($bind) ? $this->crud->delete($table, $where, $bind) : $this->crud->delete($table, $where) : $this->crud->delete($table); }
public function testClearStats() { $this->adapter->expects($this->any())->method('execute')->will($this->returnValue(1)); $inserter = new BulkInsert($this->adapter, 'table', ['field1'], []); $inserter->add(['field1' => 'foo']); $expected = ['total' => 0, 'inserted' => 0, 'updated' => 0, 'pending' => 0]; $this->assertNotEquals($expected, $inserter->fetchStats(true)); $inserter->clearStats(); $this->assertEquals($expected, $inserter->fetchStats(true)); }
/** * Writes the changes so far to the database. * * @return $this * @throws RuntimeException */ public function write() { $rowCount = count($this->rows); if ($rowCount == 0) { return $this; } $sql = $this->fetchSql(); do { try { $affectedRows = $this->adapter->execute($sql); } catch (RuntimeException $e) { if (stripos($e->getMessage(), 'Deadlock') === false) { throw $e; } $affectedRows = false; } } while ($affectedRows === false); $this->rows = []; $updatedRows = $affectedRows - $rowCount; $this->totalRows += $rowCount; $this->totalInserted += $rowCount - $updatedRows; $this->totalUpdated += $updatedRows; return $this; }
public function testRollback() { $this->pdo->expects($this->once())->method('rollback'); $adapter = new Adapter(); $adapter->setConnection($this->pdo); $adapter->rollBack(); }
/** * @return array */ public function getPlan() { return $this->adapter->query("EXPLAIN {$this->select}", $this->bind)->fetchAll(\PDO::FETCH_ASSOC); }
public function testGetPlanDoesExplain() { $pdoStatement = $this->getMock(\PDOStatement::class); $this->adapter->expects($this->once())->method('query')->with($this->stringContains('EXPLAIN', true))->will($this->returnValue($pdoStatement)); (new QueryPlanner($this->adapter, 'SELECT'))->getPlan(); }