table = $table; } /** * Generates the SQL statement. * @return string SQL query. * @throws InvalidArgumentException */ public function toSql(): string { $table = $this->table->name; $whereClause = $this->combineConditions(); if (!$this->isConditioned()) throw new InvalidArgumentException("Deletions need to be conditioned."); return "DELETE FROM $table WHERE $whereClause"; } /** * Executes the DELETE query. * @return int Number of affected rows. * @throws Exception */ public function delete(): int { global $wpdb; $query = $this->toSql(); $result = $wpdb->query($query); if ($result === false) throw new Exception("Deletion failed: " . $wpdb->last_error . "."); return intval($result); } /** * Clears all entries from the table. * @return int Number of affected rows. * @throws Exception */ public function truncate(): int { global $wpdb; $table = $this->table->name; $query = esc_sql("TRUNCATE TABLE $table"); $result = $wpdb->query($query); if ($result === false) throw new Exception("Truncation failed: " . $wpdb->last_error . "."); return intval($result); } }