/** * @module org.rhaco.net.Session * @param string $id * @return boolean */ public function session_destroy($id) { try { static::find_delete(Q::eq('id', $id)); return true; } catch (\Exception $e) { } return false; }
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); }
/** * SmtpBlackholeDaoから送信されたメールの一番新しいものを返す * @param string $to * @param string $subject * @param number $late_time sec * @throws \LogicException * @return \org\rhaco\net\mail\module\SmtpBlackholeDao */ public static function find_mail($to, $keyword = null, $late_time = 60) { if (empty($to)) { throw new \LogicException('`to` not found'); } $result = array(); $q = new Q(); $q->add(Q::eq('to', $to)); $q->add(Q::gte('create_date', time() - $late_time)); if (!empty($subject)) { $q->add(Q::contains('subject', $subject)); } foreach (\org\rhaco\net\mail\module\SmtpBlackholeDao::find($q, Q::order('-id')) as $mail) { $value = $mail->subject() . $mail->message(); if (empty($keyword) || mb_strpos($value, $keyword) !== false) { return $mail; } } throw new \LogicException('指定のメールが飛んでいない > [' . $to . '] ' . $keyword); }
<?php use org\rhaco\store\db\Q; \test\model\NewDao::create_table(); $obj = new \test\model\NewDao(); neq(null, $obj); \test\model\NewDao::find_delete(); eq(0, \test\model\NewDao::find_count()); $obj = new \test\model\NewDao(); $obj->save(); $obj = new \test\model\NewDao(); $obj->value(null); $obj->save(); $obj = new \test\model\NewDao(); $obj->value(''); $obj->save(); eq(1, \test\model\NewDao::find_count(Q::eq('value', ''))); eq(2, \test\model\NewDao::find_count(Q::eq('value', null))); eq(3, \test\model\NewDao::find_count()); $r = array(null, null, ''); $i = 0; foreach (\test\model\NewDao::find(Q::order('id')) as $o) { eq($r[$i], $o->value()); $i++; }
/** * @automap */ public function delete() { $model = \test\model\TestModel::find_get(\org\rhaco\store\db\Q::eq('string', 'abcdefg')); $model->delete(); }
eq($size[$i], sizeof($r->children())); foreach ($r->children() as $child) { eq(true, $child instanceof \test\model\ManyChild); eq($r->id(), $child->parent_id()); } $i++; } \test\model\UpdateModel::create_table(); \test\model\UpdateModel::find_delete(); $ref(new \test\model\UpdateModel())->value("abc")->save(); $ref(new \test\model\UpdateModel())->value("def")->save(); $ref(new \test\model\UpdateModel())->value("def")->save(); $ref(new \test\model\UpdateModel())->value("def")->save(); $ref(new \test\model\UpdateModel())->value("ghi")->save(); eq(5, \test\model\UpdateModel::find_count()); \test\model\UpdateModel::find_delete(Q::eq("value", "def")); eq(2, \test\model\UpdateModel::find_count()); \test\model\UpdateModel::find_delete(); $d1 = $ref(new \test\model\UpdateModel())->value("abc")->save(); $d2 = $ref(new \test\model\UpdateModel())->value("def")->save(); $d3 = $ref(new \test\model\UpdateModel())->value("ghi")->save(); eq(3, \test\model\UpdateModel::find_count()); $obj = new \test\model\UpdateModel(); $obj->id($d1->id())->delete(); eq(2, \test\model\UpdateModel::find_count()); $obj = new \test\model\UpdateModel(); $obj->id($d3->id())->delete(); eq(1, \test\model\UpdateModel::find_count()); eq("def", \test\model\UpdateModel::find_get()->value()); \test\model\UpdateModel::find_delete(); $s1 = $ref(new \test\model\UpdateModel())->value("abc")->save();
/** * @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(); }
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++; } } neq(10, $c); $result = \test\model\Find::find_all(Q::ob(Q::b(Q::eq("value1", "abc")), Q::b(Q::eq("value2", "EDC")))); eq(2, sizeof($result)); eq("EDC", \test\model\Find::find_get(Q::eq("value1", "jkl"))->value2()); $i = 0; $r = array("abc", "def", "ghi", "jkl"); foreach (\test\model\RefFind::find() as $obj) { eq(isset($r[$i]) ? $r[$i] : null, $obj->value()); $i++; } eq(4, $i); $i = 0; $r = array("abc"); foreach (\test\model\RefRefFind::find() as $obj) { eq(isset($r[$i]) ? $r[$i] : null, $obj->value()); $i++; } eq(1, $i); $i = 0;
protected function __find_conds__() { return Q::b(Q::eq("value1", "abc")); }
/** * DBの値と同じにする * @return $this */ public final function sync() { $query = new Q(); $query->add(new \org\rhaco\Paginator(1, 1)); foreach ($this->primary_columns() as $column) { $query->add(Q::eq($column->name(), $this->{$column->name()}())); } foreach (self::get_statement_iterator($this, $query) as $dao) { foreach (get_object_vars($dao) as $k => $v) { if ($k[0] != '_') { $this->{$k}($v); } } return $this; } throw new \org\rhaco\store\db\exception\NotfoundDaoException('synchronization failed'); }
<?php use org\rhaco\store\db\Q; $ref = function ($obj) { return $obj; }; $a1 = $ref(new \test\model\JoinA())->save(); $a2 = $ref(new \test\model\JoinA())->save(); $a3 = $ref(new \test\model\JoinA())->save(); $a4 = $ref(new \test\model\JoinA())->save(); $a5 = $ref(new \test\model\JoinA())->save(); $a6 = $ref(new \test\model\JoinA())->save(); $b1 = $ref(new \test\model\JoinB())->name("aaa")->save(); $b2 = $ref(new \test\model\JoinB())->name("bbb")->save(); $c1 = $ref(new \test\model\JoinC())->a_id($a1->id())->b_id($b1->id())->save(); $c2 = $ref(new \test\model\JoinC())->a_id($a2->id())->b_id($b1->id())->save(); $c3 = $ref(new \test\model\JoinC())->a_id($a3->id())->b_id($b1->id())->save(); $c4 = $ref(new \test\model\JoinC())->a_id($a4->id())->b_id($b2->id())->save(); $c5 = $ref(new \test\model\JoinC())->a_id($a4->id())->b_id($b1->id())->save(); $c6 = $ref(new \test\model\JoinC())->a_id($a5->id())->b_id($b2->id())->save(); $c7 = $ref(new \test\model\JoinC())->a_id($a5->id())->b_id($b1->id())->save(); $re = \test\model\JoinABC::find_all(); eq(7, sizeof($re)); $re = \test\model\JoinABC::find_all(Q::eq("name", "aaa")); eq(5, sizeof($re)); $re = \test\model\JoinABC::find_all(Q::eq("name", "bbb")); eq(2, sizeof($re)); $re = \test\model\JoinABBCC::find_all(Q::eq("name", "bbb")); eq(2, sizeof($re));