/** * Get link. * @param string|null $host * @return Oppa\Link\Link|null */ public final function getLink(string $host = null) { // link exists? // e.g: getLink('localhost') if (isset($this->links[$host])) { return $this->links[$host]; } $host = trim((string) $host); // with master/slave directives if (true === $this->config->get('sharding')) { // e.g: getLink(), getLink('master'), getLink('master.mysql.local') if ($host == '' || $host == Link::TYPE_MASTER) { return Util::arrayRand(array_filter($this->links, function ($link) { return $link->getType() == Link::TYPE_MASTER; })); } elseif ($host == Link::TYPE_SLAVE) { return Util::arrayRand(array_filter($this->links, function ($link) { return $link->getType() == Link::TYPE_SLAVE; })); } } else { // e.g: getLink() if ($host == '') { return Util::arrayRand(array_filter($this->links, function ($link) { return $link->getType() == Link::TYPE_SINGLE; })); } } }
/** * Get. * @param string $key * @return any * @throws Oppa\InvalidKeyException */ public final function __get(string $key) { if (array_key_exists($key, $this->data)) { return $this->data[$key]; } // check for camel-cased keys $keyCC = Util::upperToSnake($key); if (array_key_exists($keyCC, $this->data)) { return $this->data[$keyCC]; } throw new InvalidKeyException("Given '{$key}' key is not found in this entity!"); }
/** * Yes, "Query" of the S(Q)L... * @param string $query Raw SQL query. * @param array $params Prepare params. * @param int|array $limit Generally used in internal methods. * @param int $fetchType By-pass Result::fetchType. * @return Oppa\Query\ResultInterface * @throws Oppa\Exception\InvalidValueException, Oppa\QueryException */ public final function query(string $query, array $params = null, $limit = null, $fetchType = null) : Result\ResultInterface { // reset result vars $this->result->reset(); // trim query $query = trim($query); if ($query == '') { throw new InvalidValueException('Query cannot be empty!'); } // prepare if any params if (!empty($params)) { $query = $this->prepare($query, $params); } // log query with info level $this->logger && $this->logger->log(Logger::INFO, sprintf('New query [%s] via %s addr.', $query, Util::getIp())); // increase query count, set last query if ($this->profiler) { $this->profiler->addQuery($query); } // start last query profiling $this->profiler && $this->profiler->start(Profiler::QUERY); // go go go.. $result = $this->resource->query($query); // finish last query profiling $this->profiler && $this->profiler->stop(Profiler::QUERY); if ($result === false) { try { throw new QueryException(sprintf('Query error: query[%s] errno[%s] errmsg[%s]', $query, $this->resource->errno, $this->resource->error), $this->resource->errno); } catch (QueryException $e) { // log query error with fail level $this->logger && $this->logger->log(Logger::FAIL, $e->getMessage()); // check user error handler $errorHandler = $this->config['query_error_handler']; if ($errorHandler && is_callable($errorHandler)) { $errorHandler($e, $query, $params); // no throw return $this->result; } throw $e; } } // send query result to Result object to process and return it return $this->result->process($this->resource, $result, $limit, $fetchType); }