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;
 }
Beispiel #2
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;
 }
Beispiel #3
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())));
 }
Beispiel #4
0
 public static function eq(Query $query, $prefix = null)
 {
     if ($prefix === null) {
         $trace = debug_backtrace();
         $prefix = basename($trace[0]['file']) . ':' . $trace[0]['line'];
     }
     error_log($prefix . ": " . $query->toDialectString(DBPool::me()->getLink()->getDialect()));
 }
 /**
  * @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);
 }
Beispiel #6
0
 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;
 }
 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;
 }
Beispiel #12
0
 public function toString()
 {
     return $this->toDialectString($this->dao ? DBPool::getByDao($this->dao)->getDialect() : ImaginaryDialect::me());
 }
 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;
 }
Beispiel #14
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;
 }
Beispiel #15
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);
     }
 }
Beispiel #16
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;
 }