Doctrine 1
  1. Doctrine 1
  2. DC-841

Doctrine_Connection_Mssql::replaceBoundParamsWithInlineValuesInQuery regex failing to replace all '?' instances [patch+]

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.2.3
    • Fix Version/s: None
    • Component/s: Connection
    • Labels:
      None
    • Environment:
      PHP 5.2.11, Apache, Microsoft SQL Server 2005

      Description

      When executing queries with WHERE statements using multiple instances of the "<>" operator (as well as other non =,( symbols inbetween definitions), the method Doctrine_Connection_Mssql::replaceBoundParamsWithInlineValuesInQuery fails to identify all ? replacements.

      In the following piece of code I have a query (trimmed for readability and renamed for privacy) that fails to have all "?" symbols replaced as well as the relevant code from the method mentioned above (minus the return statement) doing a simple demonstration:

      <?php 
      $query = "SELECT * FROM [table] AS [t] WHERE ([t].[field1] <> ? AND [t].[field2] <> ? AND [t].[field2] LIKE ?)";
      
      $params = array(
      	"'param1'",
      	"'param2'",
      	"'param3'"
      );
      
      
      /**
       * Replaces bound parameters and their placeholders with explicit values.
       *
       * Workaround for http://bugs.php.net/36561
       *
       * @param string $query
       * @param array $params
       */
      //protected function replaceBoundParamsWithInlineValuesInQuery($query, array $params) {
      
      	foreach($params as $key => $value) {
      		if(is_null($value)) {
      			$value = 'NULL';
      		}
      		else {
      			//$value = $this->quote($value); //REMOVED AS PRE-ADDED QUOTES TO ABOVE PARAMETER LIST
      		}
      
      		$re = '/([=,\(][^\\\']*)(\?)/iU';
      		
      		$matches = array();
      		preg_match($re,$query,$matches);
      		
      		var_dump($matches); //ADDED FOR DEMONSTRATION
      
      		$query = preg_replace($re, "\\1 {$value}", $query, 1);
      		
      		var_dump($query); //ADDED FOR DEMONSTRATION
      	}
      
      //	return $query;
      //
      //}

      Running this code produces:

      array(3) {
        [0]=>
        string(18) "([t].[field1] <> ?"
        [1]=>
        string(17) "([t].[field1] <> "
        [2]=>
        string(1) "?"
      }
      string(108) "SELECT * FROM [table] AS [t] WHERE ([t].[field1] <>  'param1' AND [t].[field2] <> ? AND [t].[field2] LIKE ?)"
      array(0) {
      }
      string(108) "SELECT * FROM [table] AS [t] WHERE ([t].[field1] <>  'param1' AND [t].[field2] <> ? AND [t].[field2] LIKE ?)"
      array(0) {
      }
      string(108) "SELECT * FROM [table] AS [t] WHERE ([t].[field1] <>  'param1' AND [t].[field2] <> ? AND [t].[field2] LIKE ?)"

      Unfortunately the regex will not identify all the ? instances properly in the query when run like preg_match_all(), which was my first idea to fix (pre-identify all ? instances, then go through and replace them).

      The only 3 potential solutions I can think of are:

      1. Pre-identify all ?'s and note their position in the string, to do this using a much looser regex, then replace all the ?'s found
      2. Use a stack a loop and a switch statement to scan for eligible ? replacements. I have some sample code that shows this should work, but performance is not something I have tested.
      3. Revert back to the previous behavior. Scanning the changelogs I really don't know where this broke, other than queries that worked prior to 1.2.3 have since broken and this method is the cause.

        Activity

        Daniel Cousineau created issue -
        Enrico Stahn made changes -
        Field Original Value New Value
        Summary Doctrine_Connection_Mssql::replaceBoundParamsWithInlineValuesInQuery regex failing to replace all '?' instances Doctrine_Connection_Mssql::replaceBoundParamsWithInlineValuesInQuery regex failing to replace all '?' instances [patch+]

          People

          • Assignee:
            Jonathan H. Wage
            Reporter:
            Daniel Cousineau
          • Votes:
            3 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated: