/** * 测试 */ private function test() { //delete from tableTest where (id='2') limit 10 ; $delete = \HuiLib\Db\Query::delete()->table('test')->where(Where::createPair('id', '2'))->limit(10); //echo $delete->query(); echo $delete->toString(); }
/** * Select 普通测试 */ private function testAdapterSelect() { $select = \HuiLib\Db\Query::select()->table('test.test')->where(Where::createPair('id', 2))->limit(10)->offset(0)->enableForUpdate(); $re = $select->query(); \HuiLib\Helper\Debug::out($re->fetchAll()); echo $select->toString(); }
/** * 测试 */ private function test() { //update tableTest set field1='fvalue1', num=num+1 where (id='16') ; $update = \HuiLib\Db\Query::update()->table('tableTest')->where(Where::createPair('id', '16')); $update->sets(array('field1' => 'fvalue1', 'field2' => 'fvalue2', 'num' => array('plain' => 'num=num+1'))); //$update->query(); echo $update->toString(); }
private function testWhereBenchMark() { Debug::mark('startSelect'); $select = Query::select('test'); Debug::mark('endSelect'); Debug::mark('startWhere'); $where1 = Where::createPair('test', 'zzzzzzzzzzzzzzzzzzzzzzz')->orCase(Where::createPlain('test is null')); $where = Where::createQuote('num in (?)', array(3, 5, 16))->andCase($where1, Where::HAND_LEFT); Debug::mark('endWhere'); //初始化adapter后才能escape $select->where($where); //echo $select->toString(); Debug::mark('startQuery'); $re = $select->query(); Debug::mark('endQuery'); \HuiLib\Helper\Debug::out($re->fetchAll()); Debug::elapsed('startSelect', 'endSelect'); Debug::elapsed('startWhere', 'endWhere'); Debug::elapsed('startQuery', 'endQuery'); Debug::elapsed('startSelect', 'endQuery'); Debug::elapsed('startSelect', 'endALL'); }
/** * 通过单个Field获取单条记录的某个字段值 * * @param string $field * @param string $value * @param string $column 要获取的字段,是字段名,不是column序号 */ public function getColumnByField($field, $value, $column) { $select = Query::select(static::TABLE); if ($this->dbAdapter !== NULL) { $select->setAdapter($this->dbAdapter); } $unit = $select->where(Where::createPair($field, $value))->limit(1)->query()->fetch(); if (isset($unit[$column])) { return $unit[$column]; } else { return false; } }
/** * 删除一个值 * * @return int */ public function delete() { $tableInstance = $this->tableInstance; if ($tableInstance === NULL || $tableInstance::TABLE === NULL) { throw new Exception('Table class constant TABLE has not been set.'); } $delete = Query::delete($tableInstance::TABLE); if ($this->dbAdapter !== NULL) { $delete->setAdapter($this->dbAdapter); } $delete->where(Where::createPair(static::PRIMAY_IDKEY, $this->data[static::PRIMAY_IDKEY])); $this->onBeforeDelete(); $result = $delete->query(); $this->onAfterDelete(); return $result; }