/** * @plugin ebi.Session * @param int $maxlifetime * @return boolean */ public function session_gc($maxlifetime) { try { static::find_delete(Q::lt('expires', time() - $maxlifetime)); static::commit(); return true; } catch (\Exception $e) { } return false; }
<?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());
<?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 $mail = new \ebi\Mail(); $mail->from('*****@*****.**'); $mail->to('*****@*****.**'); $mail->send('subject', 'body'); $dao = \ebi\SmtpBlackholeDao::find_get(\ebi\Q::order('-id')); eq('subject', $dao->subject()); eq('body', $dao->message()); eq('*****@*****.**', $dao->to());
<?php use ebi\Q; \test\db\CompositePrimaryKeys::create_table(); \test\db\CompositePrimaryKeys::find_delete(); (new \test\db\CompositePrimaryKeys())->id1(1)->id2(1)->value('AAA1')->save(); (new \test\db\CompositePrimaryKeys())->id1(1)->id2(2)->value('AAA2')->save(); (new \test\db\CompositePrimaryKeys())->id1(1)->id2(3)->value('AAA3')->save(); (new \test\db\CompositePrimaryKeys())->id1(2)->id2(1)->value('BBB1')->save(); (new \test\db\CompositePrimaryKeys())->id1(2)->id2(2)->value('BBB2')->save(); (new \test\db\CompositePrimaryKeys())->id1(2)->id2(3)->value('BBB3')->save(); \test\db\CompositePrimaryKeysRef::create_table(); \test\db\CompositePrimaryKeysRef::find_delete(); (new \test\db\CompositePrimaryKeysRef())->ref_id(1)->type_id(1)->save(); (new \test\db\CompositePrimaryKeysRef())->ref_id(2)->type_id(1)->save(); (new \test\db\CompositePrimaryKeysRef())->ref_id(1)->type_id(2)->save(); (new \test\db\CompositePrimaryKeysRef())->ref_id(2)->type_id(2)->save(); $i = 0; $r = [[1, 1, 'AAA1'], [2, 1, 'BBB1'], [1, 2, 'AAA2'], [2, 2, 'BBB2']]; foreach (\test\db\CompositePrimaryKeysRefValue::find(Q::order('type_id,id')) as $o) { eq($r[$i][0], $o->ref_id()); eq($r[$i][1], $o->type_id()); eq($r[$i][2], $o->value()); $i++; } eq(4, $i);
<?php use ebi\Q; \test\db\FindBetween::create_table(); \test\db\FindBetween::find_delete(); (new \test\db\FindBetween())->int(1)->char('A')->date('2016/01/01')->timestamp('2016/01/01 11:00:00')->save(); (new \test\db\FindBetween())->int(2)->char('B')->date('2016/02/02')->timestamp('2016/02/02 12:00:00')->save(); (new \test\db\FindBetween())->int(3)->char('C')->date('2016/03/03')->timestamp('2016/03/03 13:00:00')->save(); (new \test\db\FindBetween())->int(4)->char('D')->date('2016/04/04')->timestamp('2016/04/04 14:00:00')->save(); eq(3, \test\db\FindBetween::find_count(Q::between('timestamp', '2016/01/01', '2016/03/03'))); eq(2, \test\db\FindBetween::find_count(Q::between('timestamp', '2016/01/01', '2016/03/02'))); eq(3, \test\db\FindBetween::find_count(Q::between('timestamp', '2016/01', '2016/03'))); eq(2, \test\db\FindBetween::find_count(Q::between('char', 'B', 'C'))); eq(3, \test\db\FindBetween::find_count(Q::between('int', 2, 4))); eq(3, \test\db\FindBetween::find_count(Q::between('date', '2016/01/01', '2016/03/03'))); eq(2, \test\db\FindBetween::find_count(Q::between('date', '2016/01/01', '2016/03/02'))); eq(3, \test\db\FindBetween::find_count(Q::between('date', '2016/01', '2016/03')));
<?php use ebi\Q; \test\db\DateTime::find_delete(); (new \test\db\DateTime())->ts('2015/07/21 12:13:14')->save(); (new \test\db\DateTime())->ts('2015/07/22 12:13:14')->save(); (new \test\db\DateTime())->ts('2015/07/23 13:13:14')->save(); (new \test\db\DateTime())->ts('2015/07/21 14:13:14')->save(); eq(['2015-07-21T00:00:00', '2015-07-22T00:00:00', '2015-07-23T00:00:00'], \test\db\DateTime::find_distinct('ts', Q::date_format('ts', 'Ymd'))); eq(['2000-01-01T12:00:00', '2000-01-01T13:00:00', '2000-01-01T14:00:00'], \test\db\DateTime::find_distinct('ts', Q::date_format('ts', 'H')));
/** * 値の妥当性チェックを行う */ 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(); }
protected function column_alias_sql(Column $column, \ebi\Q $q, $alias = true) { $column_str = $alias ? $column->table_alias() . '.' . $this->quotation($column->column()) : $this->quotation($column->column()); if ($q->ignore_case()) { return 'upper(' . $column_str . ')'; } return $column_str; }
/** * 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')); }
$obj = new \test\db\DateFormat(); $obj->ts($date); $obj->num(10); $obj->save(); foreach (\test\db\DateFormat::find() as $o) { eq(date('c', $date), $o->fm_ts()); } foreach (\test\db\DateFormat::find(Q::date_format('ts', 'Ym')) as $o) { eq(date('c', strtotime('2015/07/01 00:00:00')), $o->fm_ts()); } $date = strtotime('2015/07/01 12:34:56'); $obj = new \test\db\DateFormat(); $obj->ts($date); $obj->num(20); $obj->save(); $date = strtotime('2015/07/30 12:34:56'); $obj = new \test\db\DateFormat(); $obj->ts($date); $obj->num(30); $obj->save(); foreach (\test\db\DateFormat::find(Q::date_format('ts', 'Ym')) as $o) { eq(date('c', strtotime('2015/07/01 00:00:00')), $o->fm_ts()); } $date = strtotime('2015/08/30 12:34:56'); $obj = new \test\db\DateFormat(); $obj->ts($date); $obj->num(40); $obj->save(); eq(4, sizeof(\test\db\DateFormat::find_sum_by('num', 'ts'))); eq(2, sizeof(\test\db\DateFormat::find_sum_by('num', 'ts', Q::date_format('ts', 'Ym'))));
<?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());
$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++; }
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\Match::create_table(); \test\db\Match::find_delete(); (new \test\db\Match())->data1(10)->data2('XXX')->data3('AAABBB')->save(); (new \test\db\Match())->data1(20)->data2('YYY')->data3('BBBCCC')->save(); (new \test\db\Match())->data1(30)->data2('BBB')->data3('CCCDDD')->save(); (new \test\db\Match())->data1(40)->data2('IIIAAABBBEEE')->data3('AAADDDBBB')->save(); (new \test\db\Match())->data1(50)->data2('JJJ')->data3('EEEFFFIII')->save(); eq(4, \test\db\Match::find_count(Q::match('BBB'))); eq(2, \test\db\Match::find_count(Q::match('AAA BBB'))); eq(0, \test\db\Match::find_count(Q::match('BB YY'))); eq(1, \test\db\Match::find_count(Q::match('BBB III'))); eq(2, \test\db\Match::find_count(Q::match('EE II'))); eq(1, \test\db\Match::find_count(Q::match(30))); eq(1, \test\db\Match::find_count(Q::match('BB CC'))); // 全角スペース eq(1, \test\db\Match::find_count(Q::match('BBB CCC', ['data3'])));
foreach (test\db\Paginator::find($paginator, \ebi\Q::order('id')) as $o) { $i++; } eq(20, $i); $i = 0; $paginator = new \ebi\Paginator(20, 4); foreach (test\db\Paginator::find($paginator, \ebi\Q::order('id')) as $o) { $i++; } eq(20, $i); $i = 0; $paginator = new \ebi\Paginator(20, 5); foreach (test\db\Paginator::find($paginator, \ebi\Q::order('id')) as $o) { $i++; } eq(18, $i); $i = 0; $paginator = new \ebi\Paginator(20, 6); foreach (test\db\Paginator::find($paginator, \ebi\Q::order('id')) as $o) { $i++; } eq(0, $i); $i = 0; $req = new \ebi\Request(); $req->vars('paginate_by', 30); // 30を指定してもmax25なので25まで $paginator = \ebi\Paginator::request($req, 20, 25); foreach (test\db\Paginator::find($paginator, \ebi\Q::order('id')) as $o) { $i++; } eq(25, $i);
} } 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; $r = array('abc', 'def', 'ghi', 'jkl'); foreach (\test\db\HasFind::find() as $obj) { eq(isset($r[$i]) ? $r[$i] : null, $obj->parent()->value1()); $i++; } eq(4, $i); $result = \test\db\Find::find_all(Q::in('value1', \test\db\SubFind::find_sub('value'))); eq(4, sizeof($result)); $result = \test\db\Find::find_all(Q::in('value1', \test\db\SubFind::find_sub('value', Q::lt('order', 3)))); eq(2, sizeof($result));
<?php use ebi\Q; \test\db\Find::find_delete(); (new \test\db\Find())->order(1)->value1('aaa')->value2('AAA')->updated('2014/10/04')->save(); (new \test\db\Find())->order(3)->value1('ccc')->value2('CCC')->updated('2014/10/06')->save(); (new \test\db\Find())->order(2)->value1('bbb')->value2('BBB')->updated('2014/10/05')->save(); $object_list = \test\db\Find::find_all(Q::order('order')); eq('<object_list>' . '<Find><order>1</order><value1>aaa</value1><value2>AAA</value2><updated>2014-10-04T00:00:00+09:00</updated></Find>' . '<Find><order>2</order><value1>bbb</value1><value2>BBB</value2><updated>2014-10-05T00:00:00+09:00</updated></Find>' . '<Find><order>3</order><value1>ccc</value1><value2>CCC</value2><updated>2014-10-06T00:00:00+09:00</updated></Find>' . '</object_list>', (new \ebi\Xml('object_list', $object_list))->get());
<?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')));