/** * @throws WrongArgumentException * @return LogicalChain **/ public static function getOpenPoint($left, $right, $point) { Assert::isFalse($point === null, 'how can i build logic from emptyness?'); $point = new DBValue($point); $chain = new LogicalChain(); $chain->expOr(Expression::orBlock(Expression::andBlock(Expression::notNull($left), Expression::notNull($right), Expression::between($point, $left, $right)), Expression::andBlock(Expression::isNull($left), Expression::ltEq($point, $right)), Expression::andBlock(Expression::isNull($right), Expression::ltEq($left, $point)), Expression::andBlock(Expression::isNull($left), Expression::isNull($right)))); return $chain; }
public function fetchCollections(array $collections, array $list) { Assert::isNotEmptyArray($list); $ids = ArrayUtils::getIdsArray($list); $mainId = DBField::create($this->getIdName(), $this->getTable()); foreach ($collections as $path => $info) { $lazy = $info['lazy']; $query = OSQL::select()->get($mainId)->from($this->getTable()); $proto = reset($list)->proto(); $this->processPath($proto, $path, $query, $this->getTable()); if ($criteria = $info['criteria']) { $query = $criteria->setDao($this)->fillSelectQuery($query); } $query->andWhere(Expression::in($mainId, $ids)); $propertyPath = $info['propertyPath']; $property = $propertyPath->getFinalProperty(); $proto = $propertyPath->getFinalProto(); $dao = $propertyPath->getFinalDao(); $selfName = $this->getObjectName(); $self = new $selfName(); $getter = 'get' . ucfirst($property->getName()); Assert::isTrue($property->getRelationId() == MetaRelation::ONE_TO_MANY || $property->getRelationId() == MetaRelation::MANY_TO_MANY); $table = $dao->getJoinName($property->getColumnName()); $id = $this->getIdName(); $collection = []; if ($lazy) { if ($property->getRelationId() == MetaRelation::MANY_TO_MANY) { $childId = $self->{$getter}()->getChildIdField(); } else { $childId = $dao->getIdName(); } $alias = 'cid'; // childId, collectionId, whatever $field = DBField::create($childId, $self->{$getter}()->getHelperTable()); $query->get($field, $alias); if (!$property->isRequired()) { $query->andWhere(Expression::notNull($field)); } try { $rows = $dao->getCustomList($query); foreach ($rows as $row) { if (!empty($row[$alias])) { $collection[$row[$id]][] = $row[$alias]; } } } catch (ObjectNotFoundException $e) { /*_*/ } } else { $prefix = $table . '_'; foreach ($dao->getFields() as $field) { $query->get(DBField::create($field, $table), $prefix . $field); } if (!$property->isRequired()) { $query->andWhere(Expression::notNull(DBField::create($dao->getIdName(), $table))); } try { // otherwise we don't know which object // belongs to which collection $rows = $dao->getCustomList($query); foreach ($rows as $row) { $collection[$row[$id]][] = $dao->makeObject($row, $prefix); } } catch (ObjectNotFoundException $e) { /*_*/ } } $suffix = ucfirst($property->getName()); $fillMethod = 'fill' . $suffix; $getMethod = 'get' . $suffix; Assert::isTrue(method_exists(reset($list), $fillMethod), 'can not find filler'); Assert::isTrue(method_exists(reset($list), $getMethod), 'can not find getter'); foreach ($list as $object) { if (!empty($collection[$object->getId()])) { $object->{$fillMethod}($collection[$object->getId()], $lazy); } else { $object->{$getMethod}()->mergeList([]); } } } return $list; }
/** * @return MetaConfiguration **/ public function checkIntegrity() { $out = $this->getOutput()->newLine()->infoLine('Checking sanity of generated files: ')->newLine(); $out->info("\t"); $formErrors = []; foreach ($this->classes as $name => $class) { if (!($class->getPattern() instanceof SpookedClassPattern || $class->getPattern() instanceof SpookedEnumerationPattern || $class->getPattern() instanceof SpookedEnumPattern || $class->getPattern() instanceof SpookedRegistryPattern || $class->getPattern() instanceof InternalClassPattern) && class_exists(MetaClassNameBuilder::getClassOfMetaClass($class, true))) { $out->info($name, true); $className = MetaClassNameBuilder::getClassOfMetaClass($class); $info = new \ReflectionClass($className); $this->checkClassSanity($class, $info); if ($info->implementsInterface(Prototyped::class)) { $this->checkClassSanity($class, new \ReflectionClass($class->getProtoClass())); } if ($info->implementsInterface(DAOConnected::class)) { $this->checkClassSanity($class, new \ReflectionClass($class->getDaoClass())); } foreach ($class->getInterfaces() as $interface) { Assert::isTrue($info->implementsInterface($interface), 'class ' . $class->getName() . ' expected to implement interface ' . $interface); } // special handling for Enumeration instances if ($class->getPattern() instanceof EnumerationClassPattern || $class->getPattern() instanceof EnumClassPattern || $class->getPattern() instanceof RegistryClassPattern) { $object = new $className(call_user_func([$className, 'getAnyId'])); Assert::isTrue(unserialize(serialize($object)) == $object); $out->info(', '); if ($this->checkEnumerationRefIntegrity) { if ($object instanceof Enumeration || $object instanceof Enum || $object instanceof Registry) { $this->checkEnumerationReferentialIntegrity($object, $class->getTableName()); } } continue; } if ($class->getPattern() instanceof AbstractClassPattern) { $out->info(', '); continue; } /** @var Prototyped $object */ $object = new $className(); $proto = $object->proto(); $form = $proto->makeForm(); foreach ($class->getProperties() as $name => $property) { Assert::isTrue($property->toLightProperty($class) == $proto->getPropertyByName($name), 'defined property does not match autogenerated one - ' . $class->getName() . '::' . $property->getName()); } if (!$object instanceof DAOConnected) { $out->info(', '); continue; } $dao = $object->dao(); Assert::isEqual($dao->getIdName(), $class->getIdentifier()->getColumnName(), 'identifier name mismatch in ' . $class->getName() . ' class'); try { DBPool::getByDao($dao); } catch (MissingElementException $e) { // skipping $out->info(', '); continue; } $query = Criteria::create($dao)->setLimit(1)->add(Expression::notNull($class->getIdentifier()->getName()))->addOrder($class->getIdentifier()->getName())->toSelectQuery(); $out->warning(' (' . $query->getFieldsCount() . '/' . $query->getTablesCount() . '/'); $clone = clone $object; if (serialize($clone) == serialize($object)) { $out->info('C', true); } else { $out->error('C', true); } $out->warning('/'); try { $object = $dao->getByQuery($query); $form = $object->proto()->makeForm(); FormUtils::object2form($object, $form); if ($errors = $form->getErrors()) { $formErrors[$class->getName()] = $errors; $out->error('F', true); } else { $out->info('F', true); } } catch (ObjectNotFoundException $e) { $out->warning('F'); } $out->warning('/'); if (Criteria::create($dao)->setFetchStrategy(FetchStrategy::cascade())->toSelectQuery() == $dao->makeSelectHead()) { $out->info('H', true); } else { $out->error('H', true); } $out->warning('/'); // cloning once again $clone = clone $object; FormUtils::object2form($object, $form); FormUtils::form2object($form, $object); if ($object != $clone) { $out->error('T', true); } else { $out->info('T', true); } $out->warning(')')->info(', '); } } $out->infoLine('done.'); if ($formErrors) { $out->newLine()->errorLine('Errors found:')->newLine(); foreach ($formErrors as $class => $errors) { $out->errorLine("\t" . $class . ':', true); foreach ($errors as $name => $error) { $out->errorLine("\t\t" . $name . ' - ' . ($error == Form::WRONG ? ' wrong' : ' missing')); } $out->newLine(); } } return $this; }