2009-04-07 4 views
1

SQLクエリ内の位置に基づいてテーブル名を見つけて置き換える目立たない方法を探しています。SQL内にテーブル名を追加するためのRegex

例:

$query = 'SELECT t1.id, t1.name, t2.country FROM users AS t1, country AS t2 INNER JOIN another_table AS t3 ON t3.user_id = t1.id'; 

私は基本的に、テーブル名にクライアント名の略語を付加し、その後、私のCMSは、その変更を処理する必要があります。だから、 'users'から 'so_users'へ(Stack Overflowがクライアントだった場合)、Drupalのようなすべてのクエリテーブル名に中括弧を追加する必要はありません。例として、WordPressでセットアップ時にテーブル名の前に置く方法がありますが、WordPressがこの問題を処理する方法は私にとって理想的ではありません。

私の例のために私はいくつかの方法の出力になりたい:

$query = 'SELECT t1.id, t1.name, t2.country FROM so_users AS t1, so_country AS t2 INNER JOIN so_another_table AS t3 ON t3.user_id = t1.id'; 

(テーブル名の前に付けで 'SO_')

はありがとうございます。

kris

+0

重複しないhttp://stackoverflow.com/questions/453975/php-regex-logic-for-prepending-table-names –

+0

重複はありません。 – chaos

答えて

1

あなたが正規表現に置き換えたいパターンについての仮定をしたくないよう、最善の解決策になります。あなたの特定のニーズに適した既存のライブラリが見つからない場合は、独自のライブラリを展開してください。簡単なクエリビルダを作成するのは難しいことではありません。

+0

ねえ、 私はすでにそれがデータベース呼び出しを行っていると私はいくつかの理由のためにコードを変更したくなかった建てAPI持っている:私は変更したくない を... 1. .. .the SQLクエリが書かれている方法 2.私は現在書いているすべてのコード – Torez

+0

私はメソッドチェインを持っているクエリビルダークラスを見て、彼らは本当にクール、すなわち: $ builder-> select() ( 'u.id、c.country')などのフィールドもあります。このコードをすべて書き直すことを心配しています。 – Torez

1

これは、指定された例で有効です。

しかし、他の人が既に言及しているように、Regexesは必要なものに最適なツールではありません。与えられた正規表現があなたの例のために働いています。この正規表現ではなく、が必要な置き換えを行うことができる、想像を絶するSQL構築がたくさんあります。クエリビルダクラスを使用

$result = preg_replace('/(FROM|JOIN|,) ([_\w]*) (AS)/m', '$1 so_$2 $3', $subject); 

# (FROM|JOIN|,) ([_\w]*) (AS) 
# 
# Match the regular expression below and capture its match into backreference number 1 «(FROM|JOIN|,)» 
# Match either the regular expression below (attempting the next alternative only if this one fails) «FROM» 
#  Match the characters “FROM” literally «FROM» 
# Or match regular expression number 2 below (attempting the next alternative only if this one fails) «JOIN» 
#  Match the characters “JOIN” literally «JOIN» 
# Or match regular expression number 3 below (the entire group fails if this one fails to match) «,» 
#  Match the character “,” literally «,» 
# Match the character “ ” literally « » 
# Match the regular expression below and capture its match into backreference number 2 «([_\w]*)» 
# Match a single character present in the list below «[_\w]*» 
#  Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
#  The character “_” «_» 
#  A word character (letters, digits, etc.) «\w» 
# Match the character “ ” literally « » 
# Match the regular expression below and capture its match into backreference number 3 «(AS)» 
# Match the characters “AS” literally «AS» 
+0

ちょっと、 すぐに解決していただきありがとうございます。私はちょうどあなたのコードをテストし、それは働いた。見逃したテーブル名は1つしかありませんでした。 'country'テーブルは 'so_country'に変更されませんでした。助言がありますか? クリス – Torez

1

RegexにはSQLを解析する機能がありません。

SELECT 'SELECT * FROM users'; 
SELECT * FROM users; -- users 
SELECT '* -- users' FROM users; 
SELECT '\' FROM users; -- ';    -- differs in My/Pg vs others 
SELECT users.name FROM country AS users; -- or without AS 
SELECT users(name) FROM country;   -- users() is procedure 
SELECT "users"."name" FROM users;   -- or ` on MySQL, [] in TSQL 

などと考えてください。 SQLを解析するには、適切なSQLパーサライブラリが必要です。正規表現の事実が奇妙な間違いを犯すだけでそれをハックしようとしている。

関連する問題