回答編集履歴
1
調整
test
CHANGED
@@ -17,6 +17,34 @@
|
|
17
17
|
$q2 = "SELECT id,`TEST FROM`, age FROM `TEST WHERE` WHERE age > 18 AND name LIKE 'John%' ORDER BY id ASC";
|
18
18
|
$r2 = transformQuery($q2);
|
19
19
|
print nl2br($r1).";<br>\n";
|
20
|
+
```
|
20
21
|
|
22
|
+
# 追記
|
23
|
+
ikedasさんからの批判について
|
24
|
+
フレームワークがクエリービルダーを採用しているように、運用上生のSQL文はトラブルの原因になりがちなため、テーブル名や比較処理など明示的にあたえることが肝要です。もちろん自分で一から書くSQL文の場合組み立て時点で評価できるのでさほど問題はありませんし、値はprepareで渡すのでインジェクションも起きづらいので問題ないでしょう。ただ命題のようにすでにできあがっているSQL文を評価・整形することは無駄しかないのでやめたほうがいい(=無意味)というのが私見です。とはいえThouShaltNotさんやikedasさんがテキストとしての生のSQL文を重要視しているのであればそれを否定するものではありませんし、その部分については先の回答で肯定的に回答を上げているつもりです。
|
25
|
+
例はよくありませんがイメージとしてはevalで実行させたいPHP文を、実行前に有効かどうか評価すべきか・・・的な話で、私は評価するなんて無駄(=無意味)なぜならevalなんて使わないから、という考え方です。
|
26
|
+
極端な例で恐縮ですが参考までに以下
|
27
|
+
```SQL
|
28
|
+
create table `select test from tbl`(id int primary key,`select test from tbl` varchar(100));
|
29
|
+
insert into `select test from tbl` values
|
30
|
+
(1,'test'),
|
31
|
+
(2,'select test from tbl'),
|
32
|
+
(3,'select test from tbl where 1'),
|
33
|
+
(4,'select test from tbl');
|
21
34
|
|
35
|
+
create table `select test from tbl where 1` (id int primary key,`select test from tbl` varchar(100));
|
36
|
+
insert into `select test from tbl where 1` values
|
37
|
+
(1,'select test from tbl'),
|
38
|
+
(2,'select test from tbl'),
|
39
|
+
(3,'select test from tbl where 1');
|
22
40
|
```
|
41
|
+
としたとき、以下のSQL文は有効ですが、テキストとして評価をすべきかというと疑問です
|
42
|
+
```SQL
|
43
|
+
select `select test from tbl` as '`select test from tbl where 1'
|
44
|
+
from `select test from tbl` as `select test from tbl where 1`
|
45
|
+
inner join `select test from tbl where 1` as ```select test from tbl```
|
46
|
+
using (`select test from tbl`)
|
47
|
+
where `select test from tbl`='select test from tbl'
|
48
|
+
order by `select test from tbl`
|
49
|
+
```
|
50
|
+
|