123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826 |
- <?php
-
-
- namespace Doctrine\ORM;
-
- use Doctrine\Common\Util\ClassUtils;
- use Doctrine\Common\Collections\ArrayCollection;
-
- use Doctrine\DBAL\Types\Type;
- use Doctrine\DBAL\Cache\QueryCacheProfile;
-
- use Doctrine\ORM\Query\QueryException;
-
-
- abstract class AbstractQuery
- {
-
-
-
- const HYDRATE_OBJECT = 1;
-
-
- const HYDRATE_ARRAY = 2;
-
-
- const HYDRATE_SCALAR = 3;
-
-
- const HYDRATE_SINGLE_SCALAR = 4;
-
-
-
- const HYDRATE_SIMPLEOBJECT = 5;
-
-
-
- protected $parameters;
-
-
-
- protected $_resultSetMapping;
-
-
-
- protected $_em;
-
-
-
- protected $_hints = array();
-
-
-
- protected $_hydrationMode = self::HYDRATE_OBJECT;
-
-
-
- protected $_queryCacheProfile;
-
-
-
- protected $_expireResultCache = false;
-
-
-
- protected $_hydrationCacheProfile;
-
-
-
- public function __construct(EntityManager $em)
- {
- $this->_em = $em;
- $this->parameters = new ArrayCollection();
- }
-
-
-
- abstract public function getSQL();
-
-
-
- public function getEntityManager()
- {
- return $this->_em;
- }
-
-
-
- public function free()
- {
- $this->parameters = new ArrayCollection();
-
- $this->_hints = array();
- }
-
-
-
- public function getParameters()
- {
- return $this->parameters;
- }
-
-
-
- public function getParameter($key)
- {
- $filteredParameters = $this->parameters->filter(
- function ($parameter) use ($key)
- {
-
- return ($key == $parameter->getName());
- }
- );
-
- return count($filteredParameters) ? $filteredParameters->first() : null;
- }
-
-
-
- public function setParameters($parameters)
- {
-
- if (is_array($parameters)) {
- $parameterCollection = new ArrayCollection();
-
- foreach ($parameters as $key => $value) {
- $parameter = new Query\Parameter($key, $value);
-
- $parameterCollection->add($parameter);
- }
-
- $parameters = $parameterCollection;
- }
-
- $this->parameters = $parameters;
-
- return $this;
- }
-
-
-
- public function setParameter($key, $value, $type = null)
- {
- $filteredParameters = $this->parameters->filter(
- function ($parameter) use ($key)
- {
-
- return ($key == $parameter->getName());
- }
- );
-
- if (count($filteredParameters)) {
- $parameter = $filteredParameters->first();
- $parameter->setValue($value, $type);
-
- return $this;
- }
-
- $parameter = new Query\Parameter($key, $value, $type);
-
- $this->parameters->add($parameter);
-
- return $this;
- }
-
-
-
- public function processParameterValue($value)
- {
- switch (true) {
- case is_array($value):
- foreach ($value as $key => $paramValue) {
- $paramValue = $this->processParameterValue($paramValue);
- $value[$key] = is_array($paramValue) ? $paramValue[key($paramValue)] : $paramValue;
- }
-
- return $value;
-
- case is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(ClassUtils::getClass($value)):
- return $this->convertObjectParameterToScalarValue($value);
-
- default:
- return $value;
- }
- }
-
- private function convertObjectParameterToScalarValue($value)
- {
- $class = $this->_em->getClassMetadata(get_class($value));
-
- if ($class->isIdentifierComposite) {
- throw new \InvalidArgumentException(
- "Binding an entity with a composite primary key to a query is not supported. " .
- "You should split the parameter into the explicit fields and bind them seperately."
- );
- }
-
- $values = ($this->_em->getUnitOfWork()->getEntityState($value) === UnitOfWork::STATE_MANAGED)
- ? $this->_em->getUnitOfWork()->getEntityIdentifier($value)
- : $class->getIdentifierValues($value);
-
- $value = $values[$class->getSingleIdentifierFieldName()];
-
- if (null === $value) {
- throw new \InvalidArgumentException(
- "Binding entities to query parameters only allowed for entities that have an identifier."
- );
- }
-
- return $value;
- }
-
-
-
- public function setResultSetMapping(Query\ResultSetMapping $rsm)
- {
- $this->_resultSetMapping = $rsm;
-
- return $this;
- }
-
-
-
- public function setHydrationCacheProfile(QueryCacheProfile $profile = null)
- {
- if ( ! $profile->getResultCacheDriver()) {
- $resultCacheDriver = $this->_em->getConfiguration()->getHydrationCacheImpl();
- $profile = $profile->setResultCacheDriver($resultCacheDriver);
- }
-
- $this->_hydrationCacheProfile = $profile;
-
- return $this;
- }
-
-
-
- public function getHydrationCacheProfile()
- {
- return $this->_hydrationCacheProfile;
- }
-
-
-
- public function setResultCacheProfile(QueryCacheProfile $profile = null)
- {
- if ( ! $profile->getResultCacheDriver()) {
- $resultCacheDriver = $this->_em->getConfiguration()->getResultCacheImpl();
- $profile = $profile->setResultCacheDriver($resultCacheDriver);
- }
-
- $this->_queryCacheProfile = $profile;
-
- return $this;
- }
-
-
-
- public function setResultCacheDriver($resultCacheDriver = null)
- {
- if ($resultCacheDriver !== null && ! ($resultCacheDriver instanceof \Doctrine\Common\Cache\Cache)) {
- throw ORMException::invalidResultCacheDriver();
- }
-
- $this->_queryCacheProfile = $this->_queryCacheProfile
- ? $this->_queryCacheProfile->setResultCacheDriver($resultCacheDriver)
- : new QueryCacheProfile(0, null, $resultCacheDriver);
-
- return $this;
- }
-
-
-
- public function getResultCacheDriver()
- {
- if ($this->_queryCacheProfile && $this->_queryCacheProfile->getResultCacheDriver()) {
- return $this->_queryCacheProfile->getResultCacheDriver();
- }
-
- return $this->_em->getConfiguration()->getResultCacheImpl();
- }
-
-
-
- public function useResultCache($bool, $lifetime = null, $resultCacheId = null)
- {
- if ($bool) {
- $this->setResultCacheLifetime($lifetime);
- $this->setResultCacheId($resultCacheId);
-
- return $this;
- }
-
- $this->_queryCacheProfile = null;
-
- return $this;
- }
-
-
-
- public function setResultCacheLifetime($lifetime)
- {
- $lifetime = ($lifetime !== null) ? (int) $lifetime : 0;
-
- $this->_queryCacheProfile = $this->_queryCacheProfile
- ? $this->_queryCacheProfile->setLifetime($lifetime)
- : new QueryCacheProfile($lifetime, null, $this->_em->getConfiguration()->getResultCacheImpl());
-
- return $this;
- }
-
-
-
- public function getResultCacheLifetime()
- {
- return $this->_queryCacheProfile ? $this->_queryCacheProfile->getLifetime() : 0;
- }
-
-
-
- public function expireResultCache($expire = true)
- {
- $this->_expireResultCache = $expire;
-
- return $this;
- }
-
-
-
- public function getExpireResultCache()
- {
- return $this->_expireResultCache;
- }
-
-
-
- public function getQueryCacheProfile()
- {
- return $this->_queryCacheProfile;
- }
-
-
-
- public function setFetchMode($class, $assocName, $fetchMode)
- {
- if ($fetchMode !== Mapping\ClassMetadata::FETCH_EAGER) {
- $fetchMode = Mapping\ClassMetadata::FETCH_LAZY;
- }
-
- $this->_hints['fetchMode'][$class][$assocName] = $fetchMode;
-
- return $this;
- }
-
-
-
- public function setHydrationMode($hydrationMode)
- {
- $this->_hydrationMode = $hydrationMode;
-
- return $this;
- }
-
-
-
- public function getHydrationMode()
- {
- return $this->_hydrationMode;
- }
-
-
-
- public function getResult($hydrationMode = self::HYDRATE_OBJECT)
- {
- return $this->execute(null, $hydrationMode);
- }
-
-
-
- public function getArrayResult()
- {
- return $this->execute(null, self::HYDRATE_ARRAY);
- }
-
-
-
- public function getScalarResult()
- {
- return $this->execute(null, self::HYDRATE_SCALAR);
- }
-
-
-
- public function getOneOrNullResult($hydrationMode = null)
- {
- $result = $this->execute(null, $hydrationMode);
-
- if ($this->_hydrationMode !== self::HYDRATE_SINGLE_SCALAR && ! $result) {
- return null;
- }
-
- if ( ! is_array($result)) {
- return $result;
- }
-
- if (count($result) > 1) {
- throw new NonUniqueResultException;
- }
-
- return array_shift($result);
- }
-
-
-
- public function getSingleResult($hydrationMode = null)
- {
- $result = $this->execute(null, $hydrationMode);
-
- if ($this->_hydrationMode !== self::HYDRATE_SINGLE_SCALAR && ! $result) {
- throw new NoResultException;
- }
-
- if ( ! is_array($result)) {
- return $result;
- }
-
- if (count($result) > 1) {
- throw new NonUniqueResultException;
- }
-
- return array_shift($result);
- }
-
-
-
- public function getSingleScalarResult()
- {
- return $this->getSingleResult(self::HYDRATE_SINGLE_SCALAR);
- }
-
-
-
- public function setHint($name, $value)
- {
- $this->_hints[$name] = $value;
-
- return $this;
- }
-
-
-
- public function getHint($name)
- {
- return isset($this->_hints[$name]) ? $this->_hints[$name] : false;
- }
-
-
-
- public function getHints()
- {
- return $this->_hints;
- }
-
-
-
- public function iterate($parameters = null, $hydrationMode = null)
- {
- if ($hydrationMode !== null) {
- $this->setHydrationMode($hydrationMode);
- }
-
- if ( ! empty($parameters)) {
- $this->setParameters($parameters);
- }
-
- $stmt = $this->_doExecute();
-
- return $this->_em->newHydrator($this->_hydrationMode)->iterate(
- $stmt, $this->_resultSetMapping, $this->_hints
- );
- }
-
-
-
- public function execute($parameters = null, $hydrationMode = null)
- {
- if ($hydrationMode !== null) {
- $this->setHydrationMode($hydrationMode);
- }
-
- if ( ! empty($parameters)) {
- $this->setParameters($parameters);
- }
-
- $setCacheEntry = function() {};
-
- if ($this->_hydrationCacheProfile !== null) {
- list($cacheKey, $realCacheKey) = $this->getHydrationCacheId();
-
- $queryCacheProfile = $this->getHydrationCacheProfile();
- $cache = $queryCacheProfile->getResultCacheDriver();
- $result = $cache->fetch($cacheKey);
-
- if (isset($result[$realCacheKey])) {
- return $result[$realCacheKey];
- }
-
- if ( ! $result) {
- $result = array();
- }
-
- $setCacheEntry = function($data) use ($cache, $result, $cacheKey, $realCacheKey, $queryCacheProfile) {
- $result[$realCacheKey] = $data;
-
- $cache->save($cacheKey, $result, $queryCacheProfile->getLifetime());
- };
- }
-
- $stmt = $this->_doExecute();
-
- if (is_numeric($stmt)) {
- $setCacheEntry($stmt);
-
- return $stmt;
- }
-
- $data = $this->_em->getHydrator($this->_hydrationMode)->hydrateAll(
- $stmt, $this->_resultSetMapping, $this->_hints
- );
-
- $setCacheEntry($data);
-
- return $data;
- }
-
-
-
- protected function getHydrationCacheId()
- {
- $parameters = array();
-
- foreach ($this->getParameters() as $parameter) {
- $parameters[$parameter->getName()] = $this->processParameterValue($parameter->getValue());
- }
-
- $sql = $this->getSQL();
- $queryCacheProfile = $this->getHydrationCacheProfile();
- $hints = $this->getHints();
- $hints['hydrationMode'] = $this->getHydrationMode();
-
- ksort($hints);
-
- return $queryCacheProfile->generateCacheKeys($sql, $parameters, $hints);
- }
-
-
-
- public function setResultCacheId($id)
- {
- $this->_queryCacheProfile = $this->_queryCacheProfile
- ? $this->_queryCacheProfile->setCacheKey($id)
- : new QueryCacheProfile(0, $id, $this->_em->getConfiguration()->getResultCacheImpl());
-
- return $this;
- }
-
-
-
- public function getResultCacheId()
- {
- return $this->_queryCacheProfile ? $this->_queryCacheProfile->getCacheKey() : null;
- }
-
-
-
- abstract protected function _doExecute();
-
-
-
- public function __clone()
- {
- $this->parameters = new ArrayCollection();
-
- $this->_hints = array();
- }
- }
|