protected function where_sql(Dao $dao, &$from, Q $q, array $self_columns, $require_where = null, $alias = true) { if ($q->is_block()) { $vars = $and_block_sql = $or_block_sql = array(); $where_sql = ''; foreach ($q->ar_and_block() as $qa) { list($where, $var) = $this->where_sql($dao, $from, $qa, $self_columns, null, $alias); if (!empty($where)) { $and_block_sql[] = $where; $vars = array_merge($vars, $var); } } if (!empty($and_block_sql)) { $where_sql .= ' (' . implode(' and ', $and_block_sql) . ') '; } foreach ($q->ar_or_block() as $or_block) { list($where, $var) = $this->where_sql($dao, $from, $or_block, $self_columns, null, $alias); if (!empty($where)) { $or_block_sql[] = $where; $vars = array_merge($vars, $var); } } if (!empty($or_block_sql)) { $where_sql .= (empty($where_sql) ? '' : ' and ') . ' (' . implode(' or ', $or_block_sql) . ') '; } if (empty($where_sql)) { $where_sql = $require_where; } else { if (!empty($require_where)) { $where_sql = '(' . $require_where . ') and (' . $where_sql . ')'; } } return array($where_sql, $vars); } if ($q->type() == Q::MATCH) { $query = new Q(); foreach ($q->ar_arg1() as $cond) { if (strpos($cond, '=') !== false) { list($column, $value) = explode('=', $cond); $not = substr($value, 0, 1) == '!'; $value = $not ? strlen($value) > 1 ? substr($value, 1) : '' : $value; if ($value === '') { $query->add($not ? Q::neq($column, '') : Q::eq($column, '')); } else { $query->add($not ? Q::contains($column, $value, $q->param() | Q::NOT) : Q::contains($column, $value, $q->param())); } } else { $columns = array(); foreach ($self_columns as $column) { $columns[] = $column->name(); } $query->add(Q::contains(implode(',', $columns), explode(' ', $cond), $q->param())); } } return $this->where_sql($dao, $from, $query, $self_columns, null, $alias); } $and = $vars = array(); foreach ($q->ar_arg2() as $base_value) { $or = array(); foreach ($q->ar_arg1() as $column_str) { $value = $base_value; $column = $this->get_column($column_str, $self_columns); $column_alias = $this->column_alias_sql($dao, $column, $q, $alias); $is_add_value = true; switch ($q->type()) { case Q::EQ: if ($value === null) { $is_add_value = false; $column_alias .= ' is null'; break; } $column_alias .= ' = ' . ($value instanceof Daq ? '(' . $value->unique_sql() . ')' : '?'); break; case Q::NEQ: if ($value === null) { $is_add_value = false; $column_alias .= ' is not null'; break; } $column_alias .= ' <> ' . ($value instanceof Daq ? '(' . $value->unique_sql() . ')' : '?'); break; case Q::GT: $column_alias .= ' > ' . ($value instanceof Daq ? '(' . $value->unique_sql() . ')' : '?'); break; case Q::GTE: $column_alias .= ' >= ' . ($value instanceof Daq ? '(' . $value->unique_sql() . ')' : '?'); break; case Q::LT: $column_alias .= ' < ' . ($value instanceof Daq ? '(' . $value->unique_sql() . ')' : '?'); break; case Q::LTE: $column_alias .= ' <= ' . ($value instanceof Daq ? '(' . $value->unique_sql() . ')' : '?'); break; case Q::CONTAINS: case Q::START_WITH: case Q::END_WITH: $column_alias = $this->format_column_alias_sql($dao, $column, $q, $alias); $column_alias .= ($q->not() ? ' not' : '') . ' like(?)'; $value = ($q->type() == Q::CONTAINS || $q->type() == Q::END_WITH ? '%' : '') . $value . ($q->type() == Q::CONTAINS || $q->type() == Q::START_WITH ? '%' : ''); break; case Q::IN: $column_alias .= ($q->not() ? ' not' : '') . ($value instanceof Daq ? ' in(' . $value->unique_sql() . ')' : ' in(' . substr(str_repeat('?,', sizeof($value)), 0, -1) . ')'); break; } if ($value instanceof Daq) { $is_add_value = false; $vars = array_merge($vars, $value->ar_vars()); } $add_join_conds = $dao->join_conds($column->name()); if (!empty($add_join_conds)) { $column_alias .= ' and ' . $this->where_cond_columns($add_join_conds, $from); } $or[] = $column_alias; if ($is_add_value) { if (is_array($value)) { $values = array(); foreach ($value as $v) { $values[] = $q->ignore_case() ? strtoupper($this->column_value($dao, $column->name(), $v)) : $this->column_value($dao, $column->name(), $v); } $vars = array_merge($vars, $values); } else { $vars[] = $q->ignore_case() ? strtoupper($this->column_value($dao, $column->name(), $value)) : $this->column_value($dao, $column->name(), $value); } } } $and[] = ' (' . implode(' or ', $or) . ') '; } return array(implode(' and ', $and), $vars); }
$r = array("mno", "aaa"); foreach (\test\model\Find::find(Q::neq("value1", "ccc"), new \org\rhaco\Paginator(1, 2), Q::order("order,-id")) as $obj) { eq(isset($r[$i]) ? $r[$i] : null, $obj->value1()); $i++; } $result = \test\model\Find::find_all(Q::match("AAA", Q::IGNORE)); eq(3, sizeof($result)); $result = \test\model\Find::find_all(Q::match("AA", Q::IGNORE)); eq(3, sizeof($result)); $result = \test\model\Find::find_all(Q::eq("value2", null)); eq(1, sizeof($result)); $result = \test\model\Find::find_all(Q::neq("value2", null)); eq(7, sizeof($result)); $result = \test\model\Find::find_all(Q::eq("updated", null)); eq(6, sizeof($result)); $result = \test\model\Find::find_all(Q::neq("updated", null)); eq(2, sizeof($result)); eq("2008/12/24 10:00:00", $result[0]->fm_updated()); $c = 0; for ($i = 0; $i < 10; $i++) { $a = $b = array(); foreach (\test\model\Find::find_all(Q::random_order()) as $o) { $a[] = $o->id(); } foreach (\test\model\Find::find_all(Q::random_order()) as $o) { $b[] = $o->id(); } if ($a === $b) { $c++; } }
/** * @module org.rhaco.store.queue.Queue * 終了したものを削除する * @param string $type * @param timestamp $fin */ public function clean($type, $fin, \org\rhaco\Paginator $paginator) { foreach (\org\rhaco\store\queue\module\Dao\QueueDao::find(Q::eq('type', $type), Q::neq('fin', null), Q::lte('fin', $fin), Q::order('id'), $paginator) as $obj) { $obj->delete(); } \org\rhaco\store\queue\module\Dao\QueueDao::commit(); }
/** * 値の妥当性チェックを行う */ public final function validate() { foreach ($this->self_columns() as $name => $column) { $value = $this->{$name}(); $e_require = false; if ($this->prop_anon($name, 'require') === true && ($value === '' || $value === null)) { \org\rhaco\Exceptions::add(new \org\rhaco\store\db\exception\RequiredDaoException($name . ' required'), $name); $e_require = true; } if (!$e_require && $value !== null) { switch ($this->prop_anon($name, 'type')) { case 'number': case 'integer': if ($this->prop_anon($name, 'min') !== null && (double) $this->prop_anon($name, 'min') > $value) { \org\rhaco\Exceptions::add(new \org\rhaco\store\db\exception\LengthDaoException($name . ' less than minimum'), $name); } if ($this->prop_anon($name, 'max') !== null && (double) $this->prop_anon($name, 'max') < $value) { \org\rhaco\Exceptions::add(new \org\rhaco\store\db\exception\LengthDaoException($name . ' exceeds maximum'), $name); } break; case 'text': case 'string': case 'alnum': if ($this->prop_anon($name, 'min') !== null && (int) $this->prop_anon($name, 'min') > mb_strlen($value)) { \org\rhaco\Exceptions::add(new \org\rhaco\store\db\exception\LengthDaoException($name . ' less than minimum'), $name); } if ($this->prop_anon($name, 'max') !== null && (int) $this->prop_anon($name, 'max') < mb_strlen($value)) { \org\rhaco\Exceptions::add(new \org\rhaco\store\db\exception\LengthDaoException($name . ' exceeds maximum'), $name); } break; } } $unique_together = $this->prop_anon($name, 'unique_together'); if ($value !== '' && $value !== null && ($this->prop_anon($name, 'unique') === true || !empty($unique_together))) { $unique = $this->prop_anon($name, 'unique'); $uvalue = $value; $q = array(Q::eq($name, $uvalue)); if (!empty($unique_together)) { foreach (is_array($unique_together) ? $unique_together : array($unique_together) as $c) { $q[] = Q::eq($c, $this->{$c}()); } } foreach ($this->primary_columns() as $primary) { if (null !== ($pv = $this->{$primary->name()})) { $q[] = Q::neq($primary->name(), $this->{$primary->name()}); } } if (0 < call_user_func_array(array(get_class($this), 'find_count'), $q)) { \org\rhaco\Exceptions::add(new \org\rhaco\store\db\exception\UniqueDaoException($name . ' unique'), $name); } } $master = $this->prop_anon($name, 'master'); if (!empty($master)) { $master = str_replace('.', "\\", $master); if ($master[0] !== "\\") { $master = "\\" . $master; } try { $r = new \ReflectionClass($master); } catch (\ReflectionException $e) { $self = new \ReflectionClass(get_class($this)); $r = new \ReflectionClass("\\" . $self->getNamespaceName() . $master); } $mo = $r->newInstanceArgs(); $primarys = $mo->primary_columns(); if (empty($primarys) || 0 === call_user_func_array(array($mo, 'find_count'), array(Q::eq(key($primarys), $this->{$name})))) { \org\rhaco\Exceptions::add(new \org\rhaco\store\db\exception\NotfoundDaoException($name . ' master not found'), $name); } } if ($this->{'verify_' . $column->name()}() === false) { \org\rhaco\Exceptions::add(new \org\rhaco\store\db\exception\LogicException($column->name() . ' verify fail'), $column->name()); } } \org\rhaco\Exceptions::throw_over(); }