2017-08-02 21 views
1

table_nameの複合フォームと、column_nameフィールドのコレクションを作成して、後で一時データベーステーブルを作成するのに使用します。しかし、頻繁に私はこのエラーでCRITICAL電子メールのログを受信したい:フィールド名の確認MySQLの予約語

[42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHECK' at line 1

ユーザーはMySQLの予約語の一つとして、テーブル名や列名のいずれかを書いたときCREATE TABLE文は失敗したようにこれが見えます。

したがって、テーブル名とカラムを保存する前に、この種のキーワードを避けるコールバックバリデーターを作成します。これは、これまでの私の方法です:

public function validTableName($name) 
{ 
    try { 
     $this->em->getConnection()->executeQuery('SELECT TRUE AS '.$name); 
    } catch (\Exception $except) { 
     return true; 
    } 

    return false; 
} 

それは正常に動作しますが、それは完全に安全ではないとSQLインジェクションになりやすいのです。私はMySQLの予約語の完全なリストをコピーするように誘惑されましたが、それは私には良い選択肢として感じていません...

名前を知る簡単な方法はMySQL予約語ですか?

+0

ことができます:この1つは、すでにさまざまなプラットフォームをサポートして

public function isReservedWord($name) { // Returns the keyword list instance of this platform. $keywordList = $this->em ->getConnection() ->getSchemaManager() ->getDatabasePlatform() ->getReservedKeywordsList(); // Checks if the given word is a keyword of this dialect/vendor platform. return $keywordList->isKeyword($name); } 

:あなたは本当にそれを検証したい場合でも、..あなたは、任意のクエリせずに安全な方法でそれを行うために教義DBAL層を使用することができます予約語を心配する必要がないように、テーブル作成スクリプトの回りにバッククイックを追加するだけです。 – JNevill

+0

あなたはおそらく作成メソッドを使ってSQLインジェクションを開いていましたか? – chris85

答えて

1

テーブル名と列名の前後に引用符( `)を使用すると、エラーのこの種を回避するのに十分でなければなりません。あなたは

'mysql'   => 'Doctrine\DBAL\Platforms\Keywords\MySQLKeywords', 
'mysql57'  => 'Doctrine\DBAL\Platforms\Keywords\MySQL57Keywords', 
'sqlserver'  => 'Doctrine\DBAL\Platforms\Keywords\SQLServerKeywords', 
'sqlserver2005' => 'Doctrine\DBAL\Platforms\Keywords\SQLServer2005Keywords', 
'sqlserver2008' => 'Doctrine\DBAL\Platforms\Keywords\SQLServer2008Keywords', 
'sqlserver2012' => 'Doctrine\DBAL\Platforms\Keywords\SQLServer2012Keywords', 
'sqlite'  => 'Doctrine\DBAL\Platforms\Keywords\SQLiteKeywords', 
'pgsql'   => 'Doctrine\DBAL\Platforms\Keywords\PostgreSQLKeywords', 
'pgsql91'  => 'Doctrine\DBAL\Platforms\Keywords\PostgreSQL91Keywords', 
'pgsql92'  => 'Doctrine\DBAL\Platforms\Keywords\PostgreSQL92Keywords', 
'oracle'  => 'Doctrine\DBAL\Platforms\Keywords\OracleKeywords', 
'db2'   => 'Doctrine\DBAL\Platforms\Keywords\DB2Keywords', 
'sqlanywhere' => 'Doctrine\DBAL\Platforms\Keywords\SQLAnywhereKeywords', 
'sqlanywhere11' => 'Doctrine\DBAL\Platforms\Keywords\SQLAnywhere11Keywords', 
'sqlanywhere12' => 'Doctrine\DBAL\Platforms\Keywords\SQLAnywhere12Keywords', 
'sqlanywhere16' => 'Doctrine\DBAL\Platforms\Keywords\SQLAnywhere16Keywords', 
+1

オハイオ州私は見る...私はこれを試しても、私は抽象的な解決だと思いますか?つまり、MySqlにのみ関連していませんか? – presto

+0

はい、回答をサポートされているプラ​​ットフォームのリストで更新しました – yceruto

0

ただ、新しいテーブルにthis linkからすべての単語を挿入し、そのテーブルを第一に検索を行う。..

+0

... OPが5.7を使用していると仮定します。また、アップグレード/ダウングレードしても動作しない場合があります。 – chris85