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;
 }
Esempio n. 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;
 }
Esempio n. 3
0
 public function unite(Identifiable $object, Identifiable $old)
 {
     $query = $this->getProtoClass()->fillQuery(OSQL::update($this->getTable()), $object, $old);
     $this->runTrigger($object, OnBeforeSave::class);
     if (!$query->getFieldsCount()) {
         return $object;
     }
     return $this->doInject($this->targetizeUpdateQuery($query, $object), $object);
 }
Esempio n. 4
0
 /**
  * @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;
 }
Esempio n. 5
0
 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;
 }
Esempio n. 6
0
 public function toDialectString(Dialect $dialect)
 {
     return OSQL::createTable($this)->toDialectString($dialect);
 }
 public function toSelectQuery()
 {
     if (!$this->range || !$this->interval) {
         throw new WrongStateException('define time range and interval units first');
     }
     if (!$this->range->getStart() || !$this->range->getEnd()) {
         throw new WrongArgumentException('cannot operate with unlimited range');
     }
     $firstIntervalStart = $this->interval->truncate($this->range->getStart(), !$this->overlapped);
     $maxIntervals = $this->interval->countInRange($this->range, $this->overlapped) - 1;
     $generator = $this->getSeriesGenerator(0, $maxIntervals);
     $result = OSQL::select()->from($generator, self::ITERATOR_ALIAS)->get(Expression::add(DBValue::create($firstIntervalStart->toString())->castTo(DataType::create(DataType::TIMESTAMP)->getName()), Expression::mul(DBValue::create("1 {$this->interval->getName()}")->castTo(DataType::create(DataType::INTERVAL)->getName()), DBField::create(self::ITERATOR_ALIAS))), $this->field);
     return $result;
 }
Esempio n. 8
0
 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;
 }
 /**
  * only unlinking, we don't want to drop original object
  * @return DeleteQuery
  **/
 protected function makeDeleteQuery($delete)
 {
     $uc = $this->container;
     return OSQL::delete()->from($uc->getHelperTable())->where(Expression::eq(new DBField($uc->getParentIdField()), new DBValue($uc->getParentObject()->getId())))->andWhere(Expression::in($uc->getChildIdField(), $delete));
 }
Esempio n. 10
0
 /**
  * @return UpdateQuery
  **/
 private function makeMassUpdateQuery($ids)
 {
     $uc = $this->container;
     return OSQL::update($uc->getDao()->getTable())->set($uc->getParentIdField(), null)->where(Expression::in($uc->getChildIdField(), $ids));
 }
Esempio n. 11
0
 public function dropByIds(array $ids)
 {
     $result = DBPool::getByDao($this->dao)->queryCount(OSQL::delete()->from($this->dao->getTable())->where(Expression::in($this->dao->getIdName(), $ids)));
     $this->dao->uncacheByIds($ids);
     return $result;
 }
Esempio n. 12
0
 /**
  * @return SelectQuery
  **/
 public function makeTotalCountQuery()
 {
     return OSQL::select()->get(SQLFunction::create('count', DBValue::create('*')))->from($this->getTable());
 }