. */ namespace SP\Infrastructure\Database; /** * Class QueryData * * @package SP\Storage */ final class QueryData { protected array $params = []; protected ?string $query = null; protected ?string $mapClassName = null; protected bool $useKeyPair = false; protected ?string $select = null; protected ?string $from = null; protected ?string $where = null; protected ?string $groupBy = null; protected ?string $order = null; protected ?string $limit = null; protected ?string $queryCount = null; protected ?string $onErrorMessage = null; /** * Añadir un parámetro a la consulta * * @param $value * @param $name */ public function addParam($value, $name = null): void { if (null !== $name) { $this->params[$name] = $value; } else { $this->params[] = $value; } } public function getParams(): array { return $this->params; } public function setParams(array $data): void { $this->params = $data; } public function getQuery(): string { if (empty($this->query)) { return $this->select. ' '. $this->from. ' '. $this->where. ' '. $this->groupBy. ' '. $this->order. ' '. $this->limit; } return $this->query; } public function setQuery(string $query): void { $this->query = $query; } public function getMapClassName(): ?string { return $this->mapClassName; } public function setMapClassName(string $mapClassName): void { $this->mapClassName = $mapClassName; } public function isUseKeyPair(): bool { return $this->useKeyPair; } public function setUseKeyPair(bool $useKeyPair): void { $this->useKeyPair = $useKeyPair; } public function getSelect(): ?string { return $this->select; } public function setSelect(string $select): void { $this->select = 'SELECT '.$select; } public function setOrder(string $order): void { if (!empty($order)) { $this->order = 'ORDER BY '.$order; } } public function setLimit(string $limit, ?array $params = null): void { if (!empty($limit)) { $this->limit = 'LIMIT '.$limit; if ($params !== null) { $this->addParams($params); } } } public function addParams(array $params): void { $this->params = array_merge($this->params, $params); } public function getQueryCount(): string { if (empty($this->queryCount)) { return 'SELECT COUNT(*) '.$this->from.' '.$this->where; } return $this->queryCount; } public function getFrom(): ?string { return $this->from; } public function setFrom(string $from): void { if (!empty($from)) { $this->from = 'FROM '.$from; } } public function getWhere(): ?string { return $this->where; } /** * @param string[]|string $where */ public function setWhere($where): void { if (!empty($where)) { if (is_array($where)) { $this->where = 'WHERE '.implode(' AND ', $where); } else { $this->where = 'WHERE '.$where; } } } public function getOnErrorMessage(): string { return $this->onErrorMessage ?: __u('Error while querying'); } public function setOnErrorMessage(string $onErrorMessage): void { $this->onErrorMessage = $onErrorMessage; } public function setGroupBy(string $groupBy): void { if (!empty($groupBy)) { $this->groupBy = 'GROUP BY '.$groupBy; } } }