Skip to content

Commit

Permalink
Move pg4wp_rewrite_field out of pg4wp_rewrite
Browse files Browse the repository at this point in the history
To fix "Cannot redeclare pg4wp_rewrite_field"

Signed-off-by: Kevin Locke <[email protected]>
  • Loading branch information
kevinoid committed Sep 24, 2017
1 parent 54042c1 commit 06884bd
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions pg4wp/driver_pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ function wpsql_insert_id($lnk = NULL)
return $data;
}

// Convert MySQL FIELD function to CASE statement
// https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_field
// Other implementations: https://stackoverflow.com/q/1309624
function pg4wp_rewrite_field($matches)
{
$case = 'CASE ' . trim($matches[1]);
$comparands = explode(',', $matches[2]);
foreach($comparands as $i => $comparand) {
$case .= ' WHEN ' . trim($comparand) . ' THEN ' . ($i + 1);
}
$case .= ' ELSE 0 END';
return $case;
}

function pg4wp_rewrite( $sql)
{
// Note: Can be called from constructor before $wpdb is set
Expand Down Expand Up @@ -281,19 +295,6 @@ function pg4wp_rewrite( $sql)
$pattern = '/DATE_ADD[ ]*\(([^,]+),([^\)]+)\)/';
$sql = preg_replace( $pattern, '($1 + $2)', $sql);

// Convert MySQL FIELD function to CASE statement
// https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_field
// Other implementations: https://stackoverflow.com/q/1309624
function pg4wp_rewrite_field($matches)
{
$case = 'CASE ' . trim($matches[1]);
$comparands = explode(',', $matches[2]);
foreach($comparands as $i => $comparand) {
$case .= ' WHEN ' . trim($comparand) . ' THEN ' . ($i + 1);
}
$case .= ' ELSE 0 END';
return $case;
}
$pattern = '/FIELD[ ]*\(([^\),]+),([^\)]+)\)/';
$sql = preg_replace_callback( $pattern, 'pg4wp_rewrite_field', $sql);

Expand Down

0 comments on commit 06884bd

Please sign in to comment.