下記のコードのようにクエリに改行をつけて整形したいのですが、良い方法はないでしょうか?
自分ではこうして大文字での正規表現で考えたのですが、そもそも予約語リストとかを使った方がいいでしょうし、その予約語が例えばINSERTされる値としての使用の意図なのかなどのチェックも必要であることに気づき、自力では無理だと思いました。
もちろん今その機能を書いてくださいなどとは申しませんが、先人のライブラリだとか、ツールだとか、何か良い方法があれば知りたいです。
php
1<?php 2 3function transformQuery($queryString, $orderByField = 'name') { 4 // クエリ文字列を大文字に変換して解析 5 $upperQueryString = strtoupper($queryString); 6 7 if (strpos($upperQueryString, 'SELECT') === 0) { 8 // SELECT文の場合 9 $pattern = "/SELECT\s*(.*?)\s*FROM\s*(.*?)\s*(WHERE\s*.*?)?\s*(ORDER BY\s*.*?)?$/i"; 10 preg_match($pattern, $queryString, $matches); 11 12 if (count($matches) >= 3) { 13 $fields = trim($matches[1]); 14 $table = trim($matches[2]); 15 $conditions = isset($matches[3]) ? trim($matches[3]) : ''; 16 $orderBy = isset($matches[4]) ? trim($matches[4]) : ''; 17 18 // 新しいクエリを生成して返す 19 $newQuery = "SELECT\n{$fields}\nFROM\n{$table}\n"; 20 21 if (!empty($conditions)) { 22 $newQuery .= "WHERE\n{$conditions}\n"; 23 } 24 25 if (empty($orderBy)) { 26 $newQuery .= "ORDER BY {$orderByField}"; 27 } else { 28 $newQuery .= "{$orderBy}"; 29 } 30 31 return $newQuery; 32 } 33 } elseif (strpos($upperQueryString, 'INSERT') === 0) { 34 // INSERT文の場合 35 $pattern = "/INSERT\s+INTO\s+(.*?)\s+\((.*?)\)\s+VALUES\s+\((.*?)\)/i"; 36 preg_match($pattern, $queryString, $matches); 37 38 if (count($matches) >= 4) { 39 $table = trim($matches[1]); 40 $columns = trim($matches[2]); 41 $values = trim($matches[3]); 42 43 // 新しいクエリを生成して返す 44 $newQuery = "INSERT INTO {$table} ({$columns})\nVALUES ({$values})"; 45 46 return $newQuery; 47 } 48 } elseif (strpos($upperQueryString, 'UPDATE') === 0) { 49 // UPDATE文の場合 50 $pattern = "/UPDATE\s+(.*?)\s+SET\s+(.*?)\s+WHERE\s+(.*)/i"; 51 preg_match($pattern, $queryString, $matches); 52 53 if (count($matches) >= 4) { 54 $table = trim($matches[1]); 55 $setClause = trim($matches[2]); 56 $conditions = trim($matches[3]); 57 58 // 新しいクエリを生成して返す 59 $newQuery = "UPDATE {$table}\nSET {$setClause}\nWHERE {$conditions}"; 60 61 return $newQuery; 62 } 63 } elseif (strpos($upperQueryString, 'DELETE') === 0) { 64 // DELETE文の場合 65 $pattern = "/DELETE\s+FROM\s+(.*?)\s+WHERE\s+(.*)/i"; 66 preg_match($pattern, $queryString, $matches); 67 68 if (count($matches) >= 3) { 69 $table = trim($matches[1]); 70 $conditions = trim($matches[2]); 71 72 // 新しいクエリを生成して返す 73 $newQuery = "DELETE FROM {$table}\nWHERE {$conditions}"; 74 75 return $newQuery; 76 } 77 } 78 79 return $queryString; // 変換できないクエリはそのまま返す 80} 81 82// 使用例 83$originalQuery1 = "SELECT id, name, age FROM users WHERE age > 18 AND name LIKE 'John%'"; 84$transformedQuery1 = transformQuery($originalQuery1); 85echo $transformedQuery1 . "\n\n"; 86 87$originalQuery2 = "INSERT INTO orders (order_date, total_amount) VALUES ('2023-08-12', 150.50)"; 88$transformedQuery2 = transformQuery($originalQuery2); 89echo $transformedQuery2 . "\n\n"; 90 91$originalQuery3 = "UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales'"; 92$transformedQuery3 = transformQuery($originalQuery3); 93echo $transformedQuery3 . "\n\n"; 94 95$originalQuery4 = "DELETE FROM products WHERE stock_quantity <= 0"; 96$transformedQuery4 = transformQuery($originalQuery4); 97echo $transformedQuery4 . "\n\n"; 98

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/08/14 22:54