ECT count(DISTINCT runs.id) AS count FROM %i AS runs JOIN %i AS subjects ON runs.id = subjects.automation_run_id WHERE runs.automation_id = %d AND subjects.hash = %s ', $this->table, $this->subjectTable, $automation->getId(), $subject->getHash() ) ); return $result ? (int)current($result) : 0; } public function getCountForAutomation(Automation $automation, string ...$status): int { global $wpdb; if (!count($status)) { $result = $wpdb->get_col( $wpdb->prepare( 'SELECT COUNT(id) as count FROM %i WHERE automation_id = %d', $this->table, $automation->getId() ) ); return $result ? (int)current($result) : 0; } $result = $wpdb->get_col( // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- The number of replacements is dynamic. $wpdb->prepare( ' SELECT COUNT(id) as count FROM %i WHERE automation_id = %d AND status IN (' . implode(',', array_fill(0, count($status), '%s')) . ') ', $this->table, $automation->getId(), ...$status ) ); return $result ? (int)current($result) : 0; } public function updateStatus(int $id, string $status): void { global $wpdb; $result = $wpdb->query( $wpdb->prepare( ' UPDATE %i SET status = %s, updated_at = current_timestamp() WHERE id = %d ', $this->table, $status, $id ) ); if ($result === false) { $this->throwDatabaseError(); } } public function updateNextStep(int $id, ?string $nextStepId): void { global $wpdb; $result = $wpdb->query( $wpdb->prepare( ' UPDATE %i SET next_step_id = %s, updated_at = current_timestamp() WHERE id = %d ', $this->table, $nextStepId, $id ) ); if ($result === false) { $this->throwDatabaseError(); } } public function getAutomationStepStatisticForTimeFrame(int $automationId, string $status, \DateTimeImmutable $after, \DateTimeImmutable $before, ?int $versionId = null): array { global $wpdb; $andWhere = $versionId ? 'AND version_id = %d' : ''; $result = $wpdb->get_results( // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- The number of replacements is dynamic. $wpdb->prepare( ' SELECT COUNT(id) AS count, next_step_id FROM %i AS log WHERE automation_id = %d AND status = %s AND created_at BETWEEN %s AND %s ' . $andWhere . /* phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- The condition uses placeholders. */ ' GROUP BY next_step_id ', array_merge( [ $this->table, $automationId, $status, $after->format('Y-m-d H:i:s'), $before->format('Y-m-d H:i:s'), ], $versionId ? [$versionId] : [] ) ), ARRAY_A ); return is_array($result) ? $result : []; } public function truncate(): void { global $wpdb; $wpdb->query($wpdb->prepare('TRUNCATE %i', $this->table)); $wpdb->query($wpdb->prepare('TRUNCATE %i', $this->subjectTable)); } private function throwDatabaseError(): void { global $wpdb; throw Exceptions::databaseError($wpdb->last_error); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps } }