Example #1
0
 /**
  * Return number of records of the given type that match the given conditions.
  *
  * @param  string            $type
  * @param  array|string|null $conditions
  * @return int
  */
 public function count($type, $conditions = null)
 {
     $this->requireRegisteredType($type);
     if ($conditions = $this->connection->prepareConditions($conditions)) {
         return $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM ' . $this->getTypeTable($type, true) . " WHERE {$conditions}");
     } else {
         return $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM ' . $this->getTypeTable($type, true));
     }
 }
Example #2
0
 /**
  * Return number of records of the given type that match the given conditions.
  *
  * @param  string            $type
  * @param  array|string|null $conditions
  * @return int
  */
 public function count($type, $conditions = null)
 {
     if ($this->isTypeRegistered($type)) {
         if ($conditions = $this->connection->prepareConditions($conditions)) {
             return $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM ' . $this->getTypeTable($type, true) . " WHERE {$conditions}");
         } else {
             return $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM ' . $this->getTypeTable($type, true));
         }
     }
     throw new InvalidArgumentException("Type '{$type}' is not registered");
 }
Example #3
0
 /**
  * Set finder  .
  *
  * @param string $pattern
  * @param  mixed  ...$arguments
  * @return $this
  */
 public function &where($pattern, ...$arguments)
 {
     if (!is_string($pattern)) {
         throw new InvalidArgumentException('Conditions pattern needs to be string');
     }
     $conditions_to_prepare = [$pattern];
     if (!empty($arguments)) {
         $conditions_to_prepare = array_merge($conditions_to_prepare, $arguments);
     }
     $this->where[] = $this->connection->prepareConditions($conditions_to_prepare);
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 public function uniqueWhere($field_name, $where, ...$context)
 {
     if (empty($field_name)) {
         throw new InvalidArgumentException("Value '{$field_name}' is not a valid field name");
     }
     if (empty($context) && (!array_key_exists($field_name, $this->field_values) || $this->field_values[$field_name] === null)) {
         return true;
         // NULL is always good for single column keys because MySQL does not check NULL for uniqueness
     }
     $field_names = [$field_name];
     if (count($context)) {
         $field_names = array_merge($field_names, $context);
     }
     // Check if we have existsing columns
     foreach ($field_names as $v) {
         if (!array_key_exists($v, $this->field_values)) {
             throw new InvalidArgumentException("Field '{$v}' is not known");
         }
     }
     $table_name = $this->connection->escapeTableName($this->table_name);
     $conditions = [];
     if ($where) {
         $conditions[] = $this->connection->prepareConditions($where);
     }
     foreach ($field_names as $v) {
         $escaped_field_name = $this->connection->escapeFieldName($v);
         if ($this->field_values[$v] === null) {
             $conditions[] = "{$escaped_field_name} IS NULL";
         } else {
             $conditions[] = $this->connection->prepare("{$escaped_field_name} = ?", $this->field_values[$v]);
         }
     }
     $conditions = implode(' AND ', $conditions);
     if (empty($this->object_id)) {
         $sql = sprintf("SELECT COUNT(`id`) AS 'row_count' FROM {$table_name} WHERE {$conditions}");
     } else {
         $sql = $this->connection->prepare("SELECT COUNT(`id`) AS 'row_count' FROM {$table_name} WHERE ({$conditions}) AND (`id` != ?)", $this->old_object_id ? $this->old_object_id : $this->object_id);
     }
     if ($this->connection->executeFirstCell($sql) > 0) {
         if (empty($context)) {
             $this->addFieldError($field_name, "Value of '{$field_name}' needs to be unique");
         } else {
             $this->addFieldError($field_name, "Value of '{$field_name}' needs to be unique in context of " . implode(', ', array_map(function ($field_name) {
                 return "'{$field_name}'";
             }, $context)));
         }
         return false;
     }
     return true;
 }
Example #5
0
 /**
  * Reserve next job ID.
  *
  * @param  array|null $from_channels
  * @return int|null
  */
 public function reserveNextJob(array $from_channels = null)
 {
     $timestamp = date('Y-m-d H:i:s');
     $channel_conditions = empty($from_channels) ? '' : $this->connection->prepareConditions(['`channel` IN ? AND ', $from_channels]);
     if ($job_ids = $this->connection->executeFirstColumn('SELECT `id` FROM `' . self::JOBS_TABLE_NAME . "` WHERE {$channel_conditions}`reserved_at` IS NULL AND `available_at` <= ? ORDER BY `priority` DESC, `id` LIMIT 0, 100", $timestamp)) {
         foreach ($job_ids as $job_id) {
             $reservation_key = $this->prepareNewReservationKey();
             if ($this->on_reservation_key_ready) {
                 call_user_func($this->on_reservation_key_ready, $job_id, $reservation_key);
             }
             $this->connection->execute('UPDATE `' . self::JOBS_TABLE_NAME . '` SET `reservation_key` = ?, `reserved_at` = ? WHERE `id` = ? AND `reservation_key` IS NULL', $reservation_key, $timestamp, $job_id);
             if ($this->connection->affectedRows() === 1) {
                 return $job_id;
             }
         }
     }
     return null;
 }