public function dropList() { $dao = $this->container->getDao(); DBPool::getByDao($dao)->queryNull(OSQL::delete()->from($this->container->getHelperTable())->where(Expression::eq($this->container->getParentIdField(), $this->container->getParentObject()->getId()))); $dao->uncacheLists(); return $this; }
/** * @return SelectQuery **/ protected function joinHelperTable(SelectQuery $query) { $uc = $this->container; if (!$query->hasJoinedTable($uc->getHelperTable())) { $query->join($uc->getHelperTable(), Expression::eq(new DBField($uc->getParentTableIdField(), $uc->getDao()->getTable()), new DBField($uc->getChildIdField(), $uc->getHelperTable()))); } return $query->andWhere(Expression::eq(new DBField($uc->getParentIdField(), $uc->getHelperTable()), new DBValue($uc->getParentObject()->getId()))); }
/** * @return OneToManyLinkedFull **/ public function sync($insert, $update = array(), $delete) { $uc = $this->container; $dao = $uc->getDao(); if ($delete) { DBPool::getByDao($dao)->queryNull(OSQL::delete()->from($dao->getTable())->where(Expression::eq(new DBField($uc->getParentIdField()), $uc->getParentObject()->getId()))->andWhere(Expression::in($uc->getChildIdField(), ArrayUtils::getIdsArray($delete)))); $dao->uncacheByIds(ArrayUtils::getIdsArray($delete)); } if ($insert) { for ($i = 0, $size = count($insert); $i < $size; ++$i) { $dao->add($insert[$i]); } } if ($update) { for ($i = 0, $size = count($update); $i < $size; ++$i) { $dao->save($update[$i]); } } return $this; }
public function getById($id, $expires = Cache::EXPIRES_MEDIUM) { if ($expires !== Cache::DO_NOT_CACHE && ($object = $this->getCachedById($id))) { if ($object === Cache::NOT_FOUND) { throw new CachedObjectNotFoundException("there is no such object for '" . $this->dao->getObjectName() . "' with id=" . $id); } return $this->dao->completeObject($object); } else { $query = $this->dao->makeSelectHead()->andWhere(Expression::eq(DBField::create($this->dao->getIdName(), $this->dao->getTable()), $id)); if ($expires === Cache::DO_NOT_CACHE) { $object = $this->fetchObject($query); } else { $object = $this->cachedFetchObject($query, $expires, true); } if ($object) { return $object; } else { throw new ObjectNotFoundException("there is no such object for '" . $this->dao->getObjectName() . "' with query == " . $query->toDialectString(DBPool::me()->getByDao($this->dao)->getDialect())); } } }
private function processPath(AbstractProtoClass $proto, $probablyPath, JoinCapableQuery $query, $table, $parentRequired = true, $prefix = null) { $path = explode('.', $probablyPath); try { $property = $proto->getPropertyByName($path[0]); } catch (MissingElementException $e) { // oh, it's a value, not a property return new DBValue($probablyPath); } unset($path[0]); Assert::isTrue($property->getRelationId() != null && !$property->isGenericType()); Assert::classExists($property->getClassName()); // checking whether we're playing with value object if (!method_exists($property->getClassName(), 'dao')) { if (method_exists($property->getClassName(), 'proto') && count($path) > 1) { return $this->processPath($property->getProto(), implode('.', $path), $query, $table); } else { return $this->guessAtom(implode('.', $path), $query, $table, $prefix); } } else { $propertyDao = call_user_func([$property->getClassName(), 'dao']); Assert::isNotNull($propertyDao, 'can not find target dao for "' . $property->getName() . '" property at "' . get_class($proto) . '"'); } $alias = $propertyDao->getJoinName($property->getColumnName(), $prefix); if ($property->getRelationId() == MetaRelation::ONE_TO_MANY || $property->getRelationId() == MetaRelation::MANY_TO_MANY) { $remoteName = $property->getClassName(); $selfName = $this->getObjectName(); $self = new $selfName(); $getter = $property->getGetter(); $dao = call_user_func([$remoteName, 'dao']); if ($property->getRelationId() == MetaRelation::MANY_TO_MANY) { $helperTable = $self->{$getter}()->getHelperTable(); $helperAlias = $helperTable; if (!$query->hasJoinedTable($helperAlias)) { $logic = Expression::eq(DBField::create($this->getIdName(), $table), DBField::create($self->{$getter}()->getParentIdField(), $helperAlias)); if ($property->isRequired()) { $query->join($helperTable, $logic, $helperAlias); } else { $query->leftJoin($helperTable, $logic, $helperAlias); } } $logic = Expression::eq(DBField::create($propertyDao->getIdName(), $alias), DBField::create($self->{$getter}()->getChildIdField(), $helperAlias)); } else { $logic = Expression::eq(DBField::create($self->{$getter}()->getParentIdField(), $alias), DBField::create($this->getIdName(), $table)); } if (!$query->hasJoinedTable($alias)) { if ($property->isRequired() && $parentRequired) { $query->join($dao->getTable(), $logic, $alias); } else { $query->leftJoin($dao->getTable(), $logic, $alias); } } } else { // OneToOne, lazy OneToOne // prevents useless joins if (isset($path[1]) && count($path) == 1 && $path[1] == $propertyDao->getIdName()) { return new DBField($property->getColumnName(), $table); } if (!$query->hasJoinedTable($alias)) { $logic = Expression::eq(DBField::create($property->getColumnName(), $table), DBField::create($propertyDao->getIdName(), $alias)); if ($property->isRequired() && $parentRequired) { $query->join($propertyDao->getTable(), $logic, $alias); } else { $query->leftJoin($propertyDao->getTable(), $logic, $alias); } } } if ($path) { return $propertyDao->guessAtom(implode('.', $path), $query, $alias, $property->isRequired() && $parentRequired, $propertyDao->getJoinPrefix($property->getColumnName(), $prefix)); } // ok, we're done }
private function joinProperties(SelectQuery $query, ProtoDAO $parentDao, $parentTable, $parentRequired, $prefix = null) { $proto = call_user_func([$parentDao->getObjectName(), 'proto']); foreach ($proto->getPropertyList() as $property) { if ($property instanceof LightMetaProperty && $property->getRelationId() == MetaRelation::ONE_TO_ONE && !$property->isGenericType() && (!$property->getFetchStrategyId() && $this->getFetchStrategy()->getId() == FetchStrategy::JOIN || $property->getFetchStrategyId() == FetchStrategy::JOIN)) { if (is_subclass_of($property->getClassName(), Enumeration::class) || is_subclass_of($property->getClassName(), Enum::class) || is_subclass_of($property->getClassName(), Registry::class)) { // field already added by makeSelectHead continue; } elseif ($property->isInner()) { $proto = call_user_func([$property->getClassName(), 'proto']); foreach ($proto->getPropertyList() as $innerProperty) { $query->get(new DBField($innerProperty->getColumnName(), $parentTable)); } continue; } $propertyDao = call_user_func([$property->getClassName(), 'dao']); // add's custom dao's injection possibility if (!$propertyDao instanceof ProtoDAO) { continue; } $tableAlias = $propertyDao->getJoinName($property->getColumnName(), $prefix); $fields = $propertyDao->getFields(); if (!$query->hasJoinedTable($tableAlias)) { $logic = Expression::eq(DBField::create($property->getColumnName(), $parentTable), DBField::create($propertyDao->getIdName(), $tableAlias)); if ($property->isRequired() && $parentRequired) { $query->join($propertyDao->getTable(), $logic, $tableAlias); } else { $query->leftJoin($propertyDao->getTable(), $logic, $tableAlias); } } foreach ($fields as $field) { $query->get(new DBField($field, $tableAlias), $propertyDao->getJoinPrefix($property->getColumnName(), $prefix) . $field); } $this->joinProperties($query, $propertyDao, $tableAlias, $property->isRequired() && $parentRequired, $propertyDao->getJoinPrefix($property->getColumnName(), $prefix)); } } }
private function checkEnumerationReferentialIntegrity($enumeration, $tableName) { Assert::isTrue($enumeration instanceof Enumeration || $enumeration instanceof Enum || $enumeration instanceof Registry, 'argument enumeation must be instacne of Enumeration or Enum! gived, "' . gettype($enumeration) . '"'); $updateQueries = null; $db = DBPool::me()->getLink(); $class = get_class($enumeration); $ids = []; if ($enumeration instanceof Enumeration) { $list = $enumeration->getList(); } elseif ($enumeration instanceof Enum) { $list = ClassUtils::callStaticMethod($class . '::getList'); } elseif ($enumeration instanceof Registry) { $list = ClassUtils::callStaticMethod($class . '::getList'); } foreach ($list as $enumerationObject) { $ids[$enumerationObject->getId()] = $enumerationObject->getName(); } $rows = $db->querySet(OSQL::select()->from($tableName)->multiGet('id', 'name')); echo "\n"; foreach ($rows as $row) { if (!isset($ids[$row['id']])) { echo "Class '{$class}', strange id: {$row['id']} found. \n"; } else { if ($ids[$row['id']] != $row['name']) { echo "Class '{$class}',id: {$row['id']} sync names. \n"; $updateQueries .= OSQL::update($tableName)->set('name', $ids[$row['id']])->where(Expression::eq('id', $row['id']))->toDialectString($db->getDialect()) . ";\n"; } unset($ids[$row['id']]); } } foreach ($ids as $id => $name) { echo "Class '{$class}', id: {$id} not present in database. \n"; } echo $updateQueries; return $this; }
public function dropById($id) { $result = DBPool::getByDao($this->dao)->queryCount(OSQL::delete()->from($this->dao->getTable())->where(Expression::eq($this->dao->getIdName(), $id))); $this->dao->uncacheById($id); return $result; }