diff --git a/Magento/Sniffs/SQL/RawQuerySniff.php b/Magento/Sniffs/SQL/RawQuerySniff.php new file mode 100644 index 00000000..00095c6e --- /dev/null +++ b/Magento/Sniffs/SQL/RawQuerySniff.php @@ -0,0 +1,85 @@ +getTokens(); + $ignoredTokens = array_merge([T_WHITESPACE, T_OPEN_PARENTHESIS], Tokens::$stringTokens); + $prev = $tokens[$phpcsFile->findPrevious($ignoredTokens, $stackPtr - 1, null, true)]; + if ($prev['code'] === T_EQUAL + || ($prev['code'] === T_STRING && in_array($prev['content'], $this->queryFunctions)) + || in_array($tokens[$stackPtr]['code'], [T_HEREDOC, T_NOWDOC]) + ) { + $trim = function ($str) { + return trim(str_replace(['\'', '"'], '', $str)); + }; + + if (preg_match('/^(' . implode('|', $this->statements) . ')\s/i', $trim($tokens[$stackPtr]['content']))) { + $phpcsFile->addWarning( + $this->warningMessage, + $stackPtr, + $this->warningCode, + [trim($tokens[$stackPtr]['content'])] + ); + } + } + } +} diff --git a/Magento/Tests/SQL/RawQueryUnitTest.inc b/Magento/Tests/SQL/RawQueryUnitTest.inc new file mode 100644 index 00000000..781c70d7 --- /dev/null +++ b/Magento/Tests/SQL/RawQueryUnitTest.inc @@ -0,0 +1,114 @@ +connection = $connection; + } + + private function insertOneRow($ccode, $county, $city, $tax_rate) + { + $query = ' + insert INTO `tax_zipcode` ( + `zipcode` , + `county` , + `city` , + `tax_rate` + ) + VALUES ( + ' . '\'' . $ccode . '\', \'' . $county . '\', \'' . $city . '\', ' . $tax_rate . ') + ON DUPLICATE KEY UPDATE `tax_rate` = ' . $tax_rate . ';'; + + + $this->connection->query($query); + } + + public function delete() + { + $this->connection->query( + ' DELETE FROM table_name' + ); + } + + public function delete2() + { + + $this->connection->query( + " + DROP table table_name" + ); + } + + public function truncate() + { + $this->connection->query( + ' + TRUNCATE table table_name' + ); + } + + public function runQuery($countyInfo, $countyName) + { + $query = ' + UPDATE `table_name` + SET `tax_rate` = \'' . $countyInfo['tax_percentage'] . '\', + `auth_code` = \'' . $countyInfo['auth_code'] . '\' + WHERE county = \'' . $countyName . '\''; + $this->connection->query($query); + + $this->connection->query('CREATE TABLE Persons + ( + PersonID int, + LastName varchar(255), + FirstName varchar(255), + Address varchar(255), + City varchar(255) + );'); + } + + public function getQuery($period) + { + $this->_period = $period; + + $query = " + select `report_data` + FROM `trending_report` + WHERE `created_at` <= CURDATE( ) + AND CURDATE( ) <= `expire_at` + AND `last_for` = ' . $period . ' + ORDER BY `created_at` DESC + LIMIT 1 + "; + + return $query; + } +} + +$installer = $this; + +$installer->run(" + ALTER TABLE `{$installer->getTable('enterprise_catalogpermissions/permission')}` + CHANGE `website_id` `website_id` SMALLINT(5) UNSIGNED DEFAULT NULL, + CHANGE `customer_group_id` `customer_group_id` SMALLINT(3) UNSIGNED DEFAULT NULL; +"); + +$q = << 1, + 35 => 1, + 44 => 1, + 52 => 1, + 59 => 1, + 65 => 1, + 80 => 1, + 102 => 1, + 106 => 1, + 109 => 1, + ]; + } +} diff --git a/Magento/ruleset.xml b/Magento/ruleset.xml index c5b9f80e..6c659ec2 100644 --- a/Magento/ruleset.xml +++ b/Magento/ruleset.xml @@ -99,6 +99,10 @@ 9 warning + + 9 + warning + 9 warning