Skip to content

Commit 7f7cdcd

Browse files
committed
Add in a rewrite for a INSERT INTO SELECT query type
1 parent 63940cc commit 7f7cdcd

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

classes/QueryRewrite.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
<?php
22

33
class QueryRewrite{
4-
private $sql = null;
5-
private $type = 0;
4+
public $sql = null;
5+
public $type = 0;
66

7-
const UNKNOWN = 0;
8-
const SELECT = 1;
9-
const DELETE = 2;
10-
const INSERT = 3;
11-
const UPDATE = 4;
12-
const ALTER = 5;
13-
const DROP = 6;
14-
const CREATE = 7;
15-
const DELETEMULTI = 8;
16-
const UNION = 9;
7+
const UNKNOWN = 0;
8+
const SELECT = 1;
9+
const DELETE = 2;
10+
const INSERT = 3;
11+
const UPDATE = 4;
12+
const ALTER = 5;
13+
const DROP = 6;
14+
const CREATE = 7;
15+
const DELETEMULTI = 8;
16+
const UNION = 9;
17+
const INSERTSELECT = 10;
1718

1819
// Valid Table Regex
1920
const TABLEREF = '`?[A-Za-z0-9_]+`?(\.`?[A-Za-z0-9_]+`?)?';
@@ -48,6 +49,8 @@ private function figureOutType(){
4849
$this->type = self::DELETE;
4950
elseif (preg_match('/^DELETE\s+'.self::TABLEREF.'\s+FROM\s/i', $this->sql))
5051
$this->type = self::DELETEMULTI;
52+
elseif (preg_match('/^INSERT\s+INTO\s'.self::TABLEREF.'[\s\(]*SELECT\s+/i', $this->sql))
53+
$this->type = self::INSERTSELECT;
5154
elseif (preg_match('/^INSERT\s+INTO\s/i', $this->sql))
5255
$this->type = self::INSERT;
5356
elseif (preg_match('/^(.*)\s+UNION\s+(.*)$/i', $this->sql))
@@ -80,6 +83,12 @@ public function toSelect() {
8083
case self::UPDATE:
8184
preg_match('/^UPDATE\s+(.*)\s+SET\s+(.*)\s+WHERE\s+(.*)$/i', $this->sql, $subpatterns);
8285
return "SELECT {$subpatterns[2]} FROM {$subpatterns[1]} WHERE {$subpatterns[3]}";
86+
case self::INSERTSELECT:
87+
if (preg_match('/\(\s*(SELECT\s+.*)\)/', $this->sql, $subpatterns))
88+
return trim("{$subpatterns[1]}");
89+
else
90+
preg_match('/^INSERT\s+INTO\s+(SELECT.*)/i', $subpatterns);
91+
return trim("{$subpatterns[1]}");
8392
}
8493
return null;
8594
}
@@ -93,6 +102,7 @@ public function asExplain() {
93102
case self::DELETE:
94103
case self::DELETEMULTI:
95104
case self::UPDATE:
105+
case self::INSERTSELECT:
96106
$sql = $this->toSelect();
97107
break;
98108
default:

classes/TestQueryRewrite.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ public function testSelectBug2() {
8282
$this->assertEquals($expectedExtendedExplain, $this->_QueryRewrite->asExtendedExplain());
8383
}
8484

85+
public function testSelectBug3() {
86+
$sql = "INSERT INTO tbl_old(
87+
SELECT *
88+
FROM tbl
89+
WHERE id =123 )";
90+
$expectedType = QueryRewrite::INSERTSELECT;
91+
$expectedSelect = "SELECT * FROM tbl WHERE id =123";
92+
$expectedExplain = "EXPLAIN $expectedSelect";
93+
$expectedExtendedExplain = "EXPLAIN EXTENDED $expectedSelect";
94+
$this->_QueryRewrite->setQuery($sql);
95+
$this->assertEquals($expectedType, $this->_QueryRewrite->getType());
96+
$this->assertEquals($expectedSelect, $this->_QueryRewrite->toSelect());
97+
$this->assertEquals($expectedExplain, $this->_QueryRewrite->asExplain());
98+
$this->assertEquals($expectedExtendedExplain, $this->_QueryRewrite->asExtendedExplain());
99+
}
100+
85101
public function testConstructor() {
86102
$sql = "SELECT id FROM ips WHERE ip='192.168.0.1' AND type=1 LIMIT 1";
87103
$expectedType = QueryRewrite::SELECT;

0 commit comments

Comments
 (0)