public function testDeleteParsing() { require 'unit-tests/config.db.php'; if (empty($configMysql)) { $this->markTestSkipped('Test skipped'); return; } $di = $this->_getDI(); $expected = array('tables' => array('robots'), 'models' => array('Robots')); $query = new Query('DELETE FROM Robots'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); $expected = array('tables' => array(array('robots', NULL, 'r')), 'models' => array('Robots'), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100'))); $query = new Query('DELETE FROM Robots AS r WHERE r.id > 100'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); $expected = array('tables' => array(array('robots', NULL, 'r')), 'models' => array('Robots'), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100'))); $query = new Query('DELETE FROM Robots r WHERE r.id > 100'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); $expected = array('tables' => array(array('robots', NULL, 'r')), 'models' => array('Robots'), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100'))); $query = new Query('delete from Robots as r where r.id > 100'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); $expected = array('tables' => array(array('robots', NULL, 'r')), 'models' => array('Robots'), 'limit' => array('number' => array('type' => 'literal', 'value' => '10'))); $query = new Query('DELETE FROM Robots r LIMIT 10'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); $expected = array('tables' => array(array('robots', NULL, 'r')), 'models' => array('Robots'), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100')), 'limit' => array('number' => array('type' => 'literal', 'value' => '10'))); $query = new Query('DELETE FROM Robots r WHERE r.id > 100 LIMIT 10'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); // Issue 1011 $expected = array('tables' => array(array('robots', NULL, 'r')), 'models' => array('Robots'), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100')), 'limit' => array('number' => array('type' => 'placeholder', 'value' => ':limit'))); $query = new Query('DELETE FROM Robots r WHERE r.id > 100 LIMIT :limit:'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); $query = new Query('DELETE FROM Robots r WHERE r.id > 100 LIMIT :limit:'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); }
public function getQuery() { if (!$this->_Phql) { throw new \Exception('please format sql after call this method'); } $where = $this->getWhere(); if (!empty($where)) { $this->_Phql .= ' WHERE ' . $where; } $limit = $this->getLimit(); if (!empty($limit)) { $this->_Phql .= ' LIMIT ' . $limit; } // or new ModelQuery($this->_Phql) call all $query = new \Phalcon\Mvc\Model\Query($this->_Phql, \Phalcon\Di::getDefault()); if (!empty($this->_bindParams)) { $query->setBindParams($this->_bindParams); } if (!empty($this->_bindTypes)) { $query->setBindTypes($this->_bindTypes); } $query->setType($this->_type); $query->setDI(\Phalcon\Di::getDefault()); return $query; }
public function testDeleteParsing() { $di = $this->_getDI(); $expected = array('tables' => array('robots'), 'models' => array('Robots')); $query = new Query('DELETE FROM Robots'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); $expected = array('tables' => array(array('robots', NULL, 'r')), 'models' => array('Robots'), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100'))); $query = new Query('DELETE FROM Robots AS r WHERE r.id > 100'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); $expected = array('tables' => array(array('robots', NULL, 'r')), 'models' => array('Robots'), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100'))); $query = new Query('DELETE FROM Robots r WHERE r.id > 100'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); $expected = array('tables' => array(array('robots', NULL, 'r')), 'models' => array('Robots'), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100'))); $query = new Query('delete from Robots as r where r.id > 100'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); $expected = array('tables' => array(array('robots', NULL, 'r')), 'models' => array('Robots'), 'limit' => array('number' => '10')); $query = new Query('DELETE FROM Robots r LIMIT 10'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); $expected = array('tables' => array(array('robots', NULL, 'r')), 'models' => array('Robots'), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100')), 'limit' => array('number' => '10')); $query = new Query('DELETE FROM Robots r WHERE r.id > 100 LIMIT 10'); $query->setDI($di); $this->assertEquals($query->parse(), $expected); }
public function testDeleteParsing() { $this->specify("DELETE PHQL queries don't work as expected", function () { $expected = array('tables' => array('robots'), 'models' => array(Robots::class)); $query = new Query('DELETE FROM ' . Robots::class); $query->setDI($this->di); expect($query->parse())->equals($expected); $expected = array('tables' => array(array('robots', null, 'r')), 'models' => array(Robots::class), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100'))); $query = new Query('DELETE FROM ' . Robots::class . ' AS r WHERE r.id > 100'); $query->setDI($this->di); expect($query->parse())->equals($expected); $expected = array('tables' => array(array('robots', null, 'r')), 'models' => array(Robots::class), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100'))); $query = new Query('DELETE FROM ' . Robots::class . ' r WHERE r.id > 100'); $query->setDI($this->di); expect($query->parse())->equals($expected); $expected = array('tables' => array(array('robots', null, 'r')), 'models' => array(Robots::class), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100'))); $query = new Query('DELETE FROM ' . Robots::class . ' as r WHERE r.id > 100'); $query->setDI($this->di); expect($query->parse())->equals($expected); $expected = array('tables' => array(array('robots', null, 'r')), 'models' => array(Robots::class), 'limit' => array('number' => array('type' => 'literal', 'value' => '10'))); $query = new Query('DELETE FROM ' . Robots::class . ' r LIMIT 10'); $query->setDI($this->di); expect($query->parse())->equals($expected); $expected = array('tables' => array(array('robots', null, 'r')), 'models' => array(Robots::class), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100')), 'limit' => array('number' => array('type' => 'literal', 'value' => '10'))); $query = new Query('DELETE FROM ' . Robots::class . ' r WHERE r.id > 100 LIMIT 10'); $query->setDI($this->di); expect($query->parse())->equals($expected); // Issue 1011 $expected = array('tables' => array(array('robots', null, 'r')), 'models' => array(Robots::class), 'where' => array('type' => 'binary-op', 'op' => '>', 'left' => array('type' => 'qualified', 'domain' => 'r', 'name' => 'id', 'balias' => 'id'), 'right' => array('type' => 'literal', 'value' => '100')), 'limit' => array('number' => array('type' => 'placeholder', 'value' => ':limit'))); $query = new Query('DELETE FROM ' . Robots::class . ' r WHERE r.id > 100 LIMIT :limit:'); $query->setDI($this->di); expect($query->parse())->equals($expected); }); }