/** * @plugin ebi.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; }
/** * 値の妥当性チェックを行う */ public function validate() { foreach ($this->columns(true) as $name => $column) { if (!\ebi\Exceptions::has($name)) { $value = $this->{$name}(); \ebi\Validator::value($name, $value, ['type' => $this->prop_anon($name, 'type'), 'min' => $this->prop_anon($name, 'min'), 'max' => $this->prop_anon($name, 'max'), 'require' => $this->prop_anon($name, 'require')]); $unique_together = $this->prop_anon($name, 'unique_together'); if ($value !== '' && $value !== null && ($this->prop_anon($name, 'unique') === true || !empty($unique_together))) { $uvalue = $value; $q = [\ebi\Q::eq($name, $uvalue)]; if (!empty($unique_together)) { foreach (is_array($unique_together) ? $unique_together : [$unique_together] as $c) { $q[] = Q::eq($c, $this->{$c}()); } } foreach ($this->primary_columns() as $primary) { if (null !== $this->{$primary->name()}) { $q[] = Q::neq($primary->name(), $this->{$primary->name()}); } } if (0 < call_user_func_array([get_class($this), 'find_count'], $q)) { \ebi\Exceptions::add(new \ebi\exception\UniqueException($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([$mo, 'find_count'], [Q::eq(key($primarys), $this->{$name})])) { \ebi\Exceptions::add(new \ebi\exception\NotFoundException($name . ' master not found'), $name); } } try { if (method_exists($this, '__verify_' . $column->name() . '__') && call_user_func([$this, '__verify_' . $column->name() . '__']) === false) { \ebi\Exceptions::add(new \ebi\exception\VerifyException($column->name() . ' verification failed'), $column->name()); } } catch (\ebi\Exceptions $e) { } catch (\Exception $e) { \ebi\Exceptions::add($e, $column->name()); } } } \ebi\Exceptions::throw_over(); }
use ebi\Q; \test\db\JoinA::create_table(); \test\db\JoinA::find_delete(); \test\db\JoinB::create_table(); \test\db\JoinB::find_delete(); \test\db\JoinC::create_table(); \test\db\JoinC::find_delete(); $a1 = (new \test\db\JoinA())->save(); $a2 = (new \test\db\JoinA())->save(); $a3 = (new \test\db\JoinA())->save(); $a4 = (new \test\db\JoinA())->save(); $a5 = (new \test\db\JoinA())->save(); $a6 = (new \test\db\JoinA())->save(); $b1 = (new \test\db\JoinB())->name("aaa")->save(); $b2 = (new \test\db\JoinB())->name("bbb")->save(); $c1 = (new \test\db\JoinC())->a_id($a1->id())->b_id($b1->id())->save(); $c2 = (new \test\db\JoinC())->a_id($a2->id())->b_id($b1->id())->save(); $c3 = (new \test\db\JoinC())->a_id($a3->id())->b_id($b1->id())->save(); $c4 = (new \test\db\JoinC())->a_id($a4->id())->b_id($b2->id())->save(); $c5 = (new \test\db\JoinC())->a_id($a4->id())->b_id($b1->id())->save(); $c6 = (new \test\db\JoinC())->a_id($a5->id())->b_id($b2->id())->save(); $c7 = (new \test\db\JoinC())->a_id($a5->id())->b_id($b1->id())->save(); $re = \test\db\JoinABC::find_all(); eq(7, sizeof($re)); $re = \test\db\JoinABC::find_all(Q::eq("name", "aaa")); eq(5, sizeof($re)); $re = \test\db\JoinABC::find_all(Q::eq("name", "bbb")); eq(2, sizeof($re)); $re = \test\db\JoinABBCC::find_all(Q::eq("name", "bbb")); eq(2, sizeof($re));
<?php use ebi\Q; \test\db\UpdateModel::create_table(); \test\db\UpdateModel::find_delete(); (new \test\db\UpdateModel())->value('abc')->save(); (new \test\db\UpdateModel())->value('def')->save(); (new \test\db\UpdateModel())->value('def')->save(); (new \test\db\UpdateModel())->value('def')->save(); (new \test\db\UpdateModel())->value('ghi')->save(); eq(5, \test\db\UpdateModel::find_count()); \test\db\UpdateModel::find_delete(Q::eq('value', 'def')); eq(2, \test\db\UpdateModel::find_count()); \test\db\UpdateModel::find_delete(); $d1 = (new \test\db\UpdateModel())->value('abc')->save(); $d2 = (new \test\db\UpdateModel())->value('def')->save(); $d3 = (new \test\db\UpdateModel())->value('ghi')->save(); eq(3, \test\db\UpdateModel::find_count()); $obj = new \test\db\UpdateModel(); $obj->id($d1->id())->delete(); eq(2, \test\db\UpdateModel::find_count()); $obj = new \test\db\UpdateModel(); $obj->id($d3->id())->delete(); eq(1, \test\db\UpdateModel::find_count()); eq('def', \test\db\UpdateModel::find_get()->value()); \test\db\UpdateModel::find_delete(); $s1 = (new \test\db\UpdateModel())->value('abc')->save(); $s2 = (new \test\db\UpdateModel())->value('def')->save(); $s3 = (new \test\db\UpdateModel())->value('ghi')->save(); eq(3, \test\db\UpdateModel::find_count()); $obj = new \test\db\UpdateModel();
<?php use ebi\Q; \test\db\Crud::create_table(); \test\db\Crud::find_delete(); $start = microtime(true); eq(0, \test\db\Crud::find_count()); for ($i = 1; $i <= 10; $i++) { (new \test\db\Crud())->value($i)->save(); } eq(0, \test\db\Crud::find_count(Q::eq('value', -1))); eq(10, \test\db\Crud::find_count()); $start = microtime(true); foreach (\test\db\Crud::find() as $o) { $o->value($o->value() + 1)->save(); } eq(10, \test\db\Crud::find_count()); foreach (\test\db\Crud::find() as $o) { $o->delete(); } eq(0, \test\db\Crud::find_count());
$obj->save(); $obj = new \test\db\NewDao(); $obj->value('bbb'); $obj->save(); foreach (\test\db\NewDao::find() as $o) { neq(null, $o->value()); } foreach (\test\db\NewDao::find(\ebi\Q::eq('value', 'aaa')) as $o) { eq('aaa', $o->value()); } $obj = new \test\db\NewDao(); neq(null, $obj); \test\db\NewDao::find_delete(); eq(0, \test\db\NewDao::find_count()); $obj = new \test\db\NewDao(); $obj->save(); $obj = new \test\db\NewDao(); $obj->value(null); $obj->save(); $obj = new \test\db\NewDao(); $obj->value(''); $obj->save(); eq(1, \test\db\NewDao::find_count(Q::eq('value', ''))); eq(2, \test\db\NewDao::find_count(Q::eq('value', null))); eq(3, \test\db\NewDao::find_count()); $r = array(null, null, ''); $i = 0; foreach (\test\db\NewDao::find(Q::order('id')) as $o) { eq($r[$i], $o->value()); $i++; }
for ($i = 0; $i < 10; $i++) { $a = $b = []; foreach (\test\db\Find::find_all(Q::random_order()) as $o) { $a[] = $o->id(); } foreach (\test\db\Find::find_all(Q::random_order()) as $o) { $b[] = $o->id(); } if ($a === $b) { $c++; } } neq(10, $c); $result = \test\db\Find::find_all(Q::ob(Q::b(Q::eq('value1', 'abc')), Q::b(Q::eq('value2', 'EDC')))); eq(2, sizeof($result)); eq('EDC', \test\db\Find::find_get(Q::eq('value1', 'jkl'))->value2()); $i = 0; $r = array('abc', 'def', 'ghi', 'jkl'); foreach (\test\db\RefFind::find() as $obj) { eq(isset($r[$i]) ? $r[$i] : null, $obj->value()); $i++; } eq(4, $i); $i = 0; $r = array('abc', 'def', 'ghi'); foreach (\test\db\RefRefFind::find() as $obj) { eq(isset($r[$i]) ? $r[$i] : null, $obj->value()); $i++; } eq(3, $i); $i = 0;
/** * SmtpBlackholeDaoから送信されたメールの一番新しいものを返す * @param string $to * @param string $tcode * @param string $keyword * @return \ebi\SmtpBlackholeDao */ public static function find_mail($to, $tcode = '', $keyword = '') { $q = new Q(); $q->add(Q::eq('to', $to)); $q->add(Q::gte('create_date', time() - 300)); if (!empty($tcode)) { $q->add(Q::eq('tcode', $tcode)); } foreach (\ebi\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 \ebi\exception\NotFoundException('mail not found'); }
protected function __find_conds__() { return Q::b(Q::eq('value1', 'abc')); }
<?php use ebi\Q; \test\db\Calc::create_table(); \test\db\Calc::find_delete(); (new \test\db\Calc())->price(30)->type('B')->name('AAA')->save(); (new \test\db\Calc())->price(20)->type('B')->name('ccc')->save(); (new \test\db\Calc())->price(20)->type('A')->name('AAA')->save(); (new \test\db\Calc())->price(10)->type('A')->name('BBB')->save(); eq(80, \test\db\Calc::find_sum('price')); eq(30, \test\db\Calc::find_sum('price', Q::eq('type', 'A'))); eq(array('A' => 30, 'B' => 50), \test\db\Calc::find_sum_by('price', 'type')); eq(array('A' => 30), \test\db\Calc::find_sum_by('price', 'type', Q::eq('type', 'A'))); eq(30, \test\db\Calc::find_max('price')); eq(20, \test\db\Calc::find_max('price', Q::eq('type', 'A'))); eq('ccc', \test\db\Calc::find_max('name')); eq('BBB', \test\db\Calc::find_max('name', Q::eq('type', 'A'))); eq(10, \test\db\Calc::find_min('price')); eq(20, \test\db\Calc::find_min('price', Q::eq('type', 'B'))); $result = \test\db\Calc::find_min_by('price', 'type'); eq(array('A' => 10, 'B' => 20), $result); eq(array('A' => 10), \test\db\Calc::find_min_by('price', 'type', Q::eq('type', 'A'))); eq(20, \test\db\Calc::find_avg('price')); eq(15, \test\db\Calc::find_avg('price', Q::eq('type', 'A'))); eq(array('A' => 15, 'B' => 25), \test\db\Calc::find_avg_by('price', 'type')); eq(array('A' => 15), \test\db\Calc::find_avg_by('price', 'type', Q::eq('type', 'A'))); eq(array('A', 'B'), \test\db\Calc::find_distinct('type')); $result = \test\db\Calc::find_distinct('name', Q::eq('type', 'A')); eq(array('AAA', 'BBB'), $result); eq(array('A' => 2, 'B' => 2), \test\db\Calc::find_count_by('id', 'type')); eq(array('AAA' => 2, 'BBB' => 1, 'ccc' => 1), \test\db\Calc::find_count_by('type', 'name'));
<?php use ebi\Q; $obj = new \test\db\Boolean(); $obj->save(); $find = \test\db\Boolean::find_get(Q::eq('id', $obj->id())); eq(null, $find->value()); $obj = new \test\db\Boolean(); $obj->value(true); $obj->save(); $find = \test\db\Boolean::find_get(Q::eq('id', $obj->id())); eq(true, $find->value()); $obj = new \test\db\Boolean(); $obj->value(false); $obj->save(); $find = \test\db\Boolean::find_get(Q::eq('id', $obj->id())); eq(false, $find->value());
<?php use ebi\Q; \test\db\Calc::create_table(); \test\db\Calc::find_delete(); (new \test\db\Calc())->float(1.5)->type('A')->save(); (new \test\db\Calc())->float(1.3)->type('A')->save(); (new \test\db\Calc())->float(1.1)->type('B')->save(); eq(1.3, \test\db\Calc::find_avg('float')); eq(1.4, \test\db\Calc::find_avg('float', Q::eq('type', 'A'))); eq(array('A' => 1.4, 'B' => 1.1), \test\db\Calc::find_avg_by('float', 'type')); eq(array('A' => 1.4), \test\db\Calc::find_avg_by('float', 'type', Q::eq('type', 'A'))); \test\db\Calc::find_delete(); (new \test\db\Calc())->price(1)->type('A')->save(); (new \test\db\Calc())->price(2)->type('A')->save(); (new \test\db\Calc())->price(3)->type('B')->save(); eq(2, \test\db\Calc::find_avg('price')); eq(1.5, \test\db\Calc::find_avg('price', Q::eq('type', 'A'))); eq(array('A' => 1.5, 'B' => 3), \test\db\Calc::find_avg_by('price', 'type')); eq(array('A' => 1.5), \test\db\Calc::find_avg_by('price', 'type', Q::eq('type', 'A'))); eq(0, \test\db\Calc::find_avg('type')); eq(0, \test\db\Calc::find_avg('type', Q::eq('type', 'A')));