示例#1
0
 public function add(Identifiable $object)
 {
     if ($this instanceof SequencelessDAO) {
         return $this->import($object);
     }
     return $this->inject(OSQL::insert(), $object->setId(DBPool::getByDao($this)->obtainSequence($this->getSequence())));
 }
 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;
 }
示例#3
0
 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;
 }
示例#4
0
 /**
  * @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);
 }
示例#5
0
文件: Cursor.php 项目: avid/hesper
 public function __construct(ProtoDAO $dao, SelectQuery $query = null)
 {
     if ($query) {
         Assert::isTrue($query instanceof SelectQuery);
     }
     $this->dao = $dao;
     $this->db = DBPool::getByDao($this->dao);
     $this->selectQuery = $query;
     $this->openTransaction();
     $this->declareCursor();
 }
 /**
  * @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;
 }
 /**
  * @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;
 }
 /**
  * @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 = [], $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 = [];
         foreach ($delete as $object) {
             $ids[] = $object->getId();
         }
         $db->queryNull($this->makeDeleteQuery($ids));
         $dao->uncacheByIds($ids);
     }
     return $this;
 }
示例#10
0
 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->type === 'json' || $this->type === 'jsonb') {
         return json_decode($raw, true);
         //associative array instead of object
     }
     if (!$this->identifier && $this->generic && $this->className) {
         return call_user_func([$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::class) && !is_subclass_of($this->className, Enum::class) && !is_subclass_of($this->className, Registry::class)) {
             $remoteDao = call_user_func([$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, ['f' => null]);
     }
     return $raw;
 }
示例#11
0
 public function toString()
 {
     return $this->toDialectString($this->dao ? DBPool::getByDao($this->dao)->getDialect() : ImaginaryDialect::me());
 }
示例#12
0
 /**
  * @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;
 }
示例#13
0
 protected function fetchList(SelectQuery $query)
 {
     $list = [];
     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;
 }
示例#14
0
 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);
     }
 }
示例#15
0
 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
     $result = $this->addObjectToMap($object->setId($object->getId()));
     $this->runTrigger($object, OnAfterSave::class);
     return $result;
 }