Skip to content

Commit

Permalink
Support for CTE in postgreSQL, bcosca/fatfree#1107, bcosca/fatfree#1116
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkez committed Aug 11, 2018
1 parent 7596cfb commit 8fd940b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion db/sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ function exec($cmds,$args=NULL,$ttl=0,$log=TRUE,$stamp=FALSE) {
user_error('PDOStatement: '.$error[2],E_USER_ERROR);
}
if (preg_match('/(?:^[\s\(]*'.
'(?:EXPLAIN|SELECT|PRAGMA|SHOW)|RETURNING)\b/is',$cmd) ||
'(?:WITH|EXPLAIN|SELECT|PRAGMA|SHOW)|RETURNING)\b/is',$cmd) ||
(preg_match('/^\s*(?:CALL|EXEC)\b/is',$cmd) &&
$query->columnCount())) {
$result=$query->fetchall(\PDO::FETCH_ASSOC);
Expand Down

3 comments on commit 8fd940b

@anthonyblond
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this will cause an UPDATE that uses a CTE will cause the return of a result rather than the number of affected rows, even if it doesn't have a RETURNING. Is this the intended behaviour?

@ikkez
Copy link
Member Author

@ikkez ikkez commented on 8fd940b Aug 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it matter?

@anthonyblond
Copy link

@anthonyblond anthonyblond commented on 8fd940b Aug 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases probably not, depends whether the row count is being used.

I just ran a meaningless query (since q1 isn't actually being used) to test what the new output would be.

        $result = $db->exec(
        "   WITH q1 AS (select true)
            UPDATE contacts SET handled = true WHERE notes = 'test'
        ");
        var_dump($result);

Before the change the output is:

int(3)

After the change outputs:

array(3) {
  [0]=>
  array(0) {
  }
  [1]=>
  array(0) {
  }
  [2]=>
  array(0) {
  }
}

If the query was just UPDATE contacts SET handled = true WHERE notes = 'test' then the result is always just int(3)

Please sign in to comment.