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; }
public static function increment(DAOConnected &$object, array $fields, $refreshCurrent = true, $query = null) { $objectDao = $object->dao(); if ($query) { $updateQuery = $query; } else { $updateQuery = OSQL::update()->setTable($objectDao->getTable())->where(Expression::eqId('id', $object)); } $mapping = $objectDao->getProtoClass()->getMapping(); foreach ($mapping as $field => $column) { if (isset($fields[$field])) { $updateQuery->set($column, Expression::add($column, $fields[$field])); } } $updateCount = DBPool::getByDao($objectDao)->queryCount($updateQuery); if ($query) { $objectDao->uncacheLists(); } else { $objectDao->uncacheById($object->getId()); } if ($refreshCurrent && !$query) { $object = $objectDao->getById($object->getId()); } return $updateCount; }
/** * @param DB|GenericDAO $database * @param IsolationLevel $level * @param AccessMode $mode **/ public function __construct($database, IsolationLevel $level = null, AccessMode $mode = null) { if ($database instanceof DB) { $this->db = $database; } elseif ($database instanceof GenericDAO) { $this->db = DBPool::getByDao($database); } else { throw new WrongStateException('$database must be instance of DB or GenericDAO'); } $this->beginTransaction($level, $mode); }
/** * @throws WrongArgumentException * @return OneToManyLinkedLazy **/ public function sync($insert, $update = array(), $delete) { Assert::isTrue($update === array()); $db = DBPool::getByDao($this->container->getDao()); $uc = $this->container; $dao = $uc->getDao(); if ($insert) { $db->queryNull($this->makeMassUpdateQuery($insert)); } if ($delete) { // unlink or drop $uc->isUnlinkable() ? $db->queryNull($this->makeMassUpdateQuery($delete)) : $db->queryNull(OSQL::delete()->from($dao->getTable())->where(Expression::in($uc->getChildIdField(), $delete))); $dao->uncacheByIds($delete); } return $this; }
/** * @throws BaseException * @return ModelAndView **/ public function run(Prototyped $subject, Form $form, HttpRequest $request) { Assert::isFalse($this->running, 'command already running'); Assert::isTrue($subject instanceof DAOConnected); $this->db = DBPool::getByDao($subject->dao()); $this->db->begin(); try { $mav = $this->command->run($subject, $form, $request); $this->running = true; return $mav; } catch (BaseException $e) { $this->db->rollback(); throw $e; } Assert::isUnreachable(); }
/** * @throws WrongArgumentException * @return ManyToManyLinkedLazy **/ public function sync($insert, $update = array(), $delete) { Assert::isTrue($update === array()); $dao = $this->container->getDao(); $db = DBPool::getByDao($dao); if ($insert) { for ($i = 0, $size = count($insert); $i < $size; ++$i) { $db->queryNull($this->makeInsertQuery($insert[$i])); } } if ($delete) { $db->queryNull($this->makeDeleteQuery($delete)); $dao->uncacheByIds($delete); } return $this; }
/** * @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; }
/** * @return ManyToManyLinkedFull **/ public function sync($insert, $update = array(), $delete) { $dao = $this->container->getDao(); $db = DBPool::getByDao($dao); if ($insert) { for ($i = 0, $size = count($insert); $i < $size; ++$i) { $db->queryNull($this->makeInsertQuery($dao->take($insert[$i])->getId())); } } if ($update) { for ($i = 0, $size = count($update); $i < $size; ++$i) { $dao->save($update[$i]); } } if ($delete) { $ids = array(); foreach ($delete as $object) { $ids[] = $object->getId(); } $db->queryNull($this->makeDeleteQuery($ids)); $dao->uncacheByIds($ids); } return $this; }
public function getQueryResult(SelectQuery $query, $expires = Cache::DO_NOT_CACHE) { if ($expires !== Cache::DO_NOT_CACHE && ($list = $this->getCachedByQuery($query))) { return $list; } else { $list = $this->fetchList($query); $count = clone $query; $count = DBPool::getByDao($this->dao)->queryRow($count->dropFields()->dropOrder()->limit(null, null)->get(SQLFunction::create('COUNT', '*')->setAlias('count'))); return $this->cacheByQuery($query, $list ? QueryResult::create()->setList($list)->setCount($count['count'])->setQuery($query) : QueryResult::create(), $expires); } }
/** * @return MetaConfiguration **/ public function checkIntegrity() { $out = $this->getOutput()->newLine()->infoLine('Checking sanity of generated files: ')->newLine(); set_include_path(get_include_path() . PATH_SEPARATOR . ONPHP_META_BUSINESS_DIR . PATH_SEPARATOR . ONPHP_META_DAO_DIR . PATH_SEPARATOR . ONPHP_META_PROTO_DIR . PATH_SEPARATOR . ONPHP_META_AUTO_BUSINESS_DIR . PATH_SEPARATOR . ONPHP_META_AUTO_DAO_DIR . PATH_SEPARATOR . ONPHP_META_AUTO_PROTO_DIR . PATH_SEPARATOR); $out->info("\t"); $formErrors = array(); foreach ($this->classes as $name => $class) { if (!($class->getPattern() instanceof SpookedClassPattern || $class->getPattern() instanceof SpookedEnumerationPattern || $class->getPattern() instanceof InternalClassPattern) && class_exists($class->getName(), true)) { $out->info($name, true); $info = new ReflectionClass($name); $this->checkClassSanity($class, $info); if ($info->implementsInterface('Prototyped')) { $this->checkClassSanity($class, new ReflectionClass('Proto' . $name)); } if ($info->implementsInterface('DAOConnected')) { $this->checkClassSanity($class, new ReflectionClass($name . 'DAO')); } 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) { $object = new $name(call_user_func(array($name, 'getAnyId'))); Assert::isTrue(unserialize(serialize($object)) == $object); $out->info(', '); if ($this->checkEnumerationRefIntegrity) { $this->checkEnumerationReferentialIntegrity($object, $class->getTableName()); } continue; } if ($class->getPattern() instanceof AbstractClassPattern) { $out->info(', '); continue; } $object = new $name(); $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; }
public function toValue(ProtoDAO $dao = null, $array, $prefix = null) { $raw = $array[$prefix . $this->columnName]; if ($this->type == 'binary') { return DBPool::getByDao($dao)->getDialect()->unquoteBinary($raw); } if ($this->className == 'HttpUrl') { return HttpUrl::create()->parse($raw); } if (!$this->identifier && $this->generic && $this->className) { return call_user_func(array($this->className, 'create'), $raw); } elseif (!$this->identifier && $this->className) { // BOVM: prevents segfault on >=php-5.2.5 Assert::classExists($this->className); if (!is_subclass_of($this->className, 'Enumeration')) { $remoteDao = call_user_func(array($this->className, 'dao')); $joinPrefix = $remoteDao->getJoinPrefix($this->columnName, $prefix); $joined = $this->strategyId == FetchStrategy::JOIN || isset($array[$joinPrefix . $remoteDao->getIdName()]); if ($joined) { return $remoteDao->makeObject($array, $joinPrefix); } else { // will be fetched later // by AbstractProtoClass::fetchEncapsulants $object = new $this->className(); $object->setId($raw); return $object; } } else { return new $this->className($raw); } } // veeeeery "special" handling, by tradition. // MySQL returns 0/1, others - t/f if ($this->type == 'boolean') { return (bool) strtr($raw, array('f' => null)); } return $raw; }
public function add(Identifiable $object) { return $this->inject(OSQL::insert(), $object->setId(DBPool::getByDao($this)->obtainSequence($this->getSequence()))); }
protected function doInject(InsertOrUpdateQuery $query, Identifiable $object) { $db = DBPool::getByDao($this); if (!$db->isQueueActive()) { $preUncacher = is_scalar($object->getId()) ? $this->getUncacherById($object->getId()) : null; $count = $db->queryCount($query); $uncacher = $this->getUncacherById($object->getId()); if ($preUncacher) { $uncacher->merge($uncacher); } $uncacher->uncache(); if ($count !== 1) { throw new WrongStateException($count . ' rows affected: racy or insane inject happened: ' . $query->toDialectString($db->getDialect())); } } else { $preUncacher = is_scalar($object->getId()) ? $this->getUncacherById($object->getId()) : null; $db->queryNull($query); $uncacher = $this->getUncacherById($object->getId()); if ($preUncacher) { $uncacher->merge($uncacher); } $uncacher->uncache(); } // clean out Identifier, if any return $this->addObjectToMap($object->setId($object->getId())); }
public function toString() { return $this->toDialectString($this->dao ? DBPool::getByDao($this->dao)->getDialect() : ImaginaryDialect::me()); }
protected function fetchList(SelectQuery $query) { $list = array(); if ($rows = DBPool::getByDao($this->dao)->querySet($query)) { $proto = $this->dao->getProtoClass(); $proto->beginPrefetch(); foreach ($rows as $row) { $list[] = $this->dao->makeObject($row); } $proto->endPrefetch($list); } return $list; }