Skip to content

Commit 770b596

Browse files
committed
Rewrite update queries into selects
1 parent dd64e03 commit 770b596

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

classes/QueryRewrite.php

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ function figureOutType(){
3535
$this->type = self::INSERT;
3636
elseif (preg_match('/^(.*)\s+UNION\s+(.*)$/', $this->sql))
3737
$this->type = self::UNION;
38+
elseif (preg_match('/^UPDATE\s/', $this->sql))
39+
$this->type = self::UPDATE;
3840
else
3941
$this->type = self::UNKNOWN;
4042
}
@@ -48,6 +50,9 @@ function toSelect() {
4850
return preg_replace('/^DELETE\s+FROM\s/', 'SELECT 0 FROM ', $this->sql);
4951
case self::DELETEMULTI:
5052
return preg_replace('/^DELETE\s+'.self::TABLEREF.'\s+FROM\s/', 'SELECT 0 FROM ', $this->sql);
53+
case self::UPDATE:
54+
preg_match('/^UPDATE\s+(.*)\s+SET\s+(.*)\s+WHERE\s+(.*)$/', $this->sql, $subpatterns);
55+
return "SELECT {$subpatterns[2]} FROM {$subpatterns[1]} WHERE {$subpatterns[3]}";
5156
}
5257
return null;
5358
}
@@ -60,6 +65,7 @@ function asExplain() {
6065
break;
6166
case self::DELETE:
6267
case self::DELETEMULTI:
68+
case self::UPDATE:
6369
$sql = $this->toSelect();
6470
break;
6571
default:

explain.php

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
$Query = new QueryRewrite($query);
1616
$sample = $Query->asExtendedExplain();
1717

18+
$return['oQuery'] = $query;
19+
$return['eQuery'] = $sample;
20+
1821
if (is_null($sample)) {
1922
$return['Warnings'][] = array('Code' => '0', 'Level' => 'Error', 'Message' => "I can't explain this type of query yet");
2023
}

0 commit comments

Comments
 (0)