2009-07-15 14:07:44 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* @package PostgreSQL_For_Wordpress
|
|
|
|
|
* @version $Id$
|
|
|
|
|
* @author Hawk__, www.hawkix.net
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provides a driver for PostgreSQL
|
|
|
|
|
*
|
|
|
|
|
* This file maps original mysql_* functions with PostgreSQL equivalents
|
|
|
|
|
*
|
|
|
|
|
* This was originally based on usleepless's original 'mysql2pgsql.php' file, many thanks to him
|
|
|
|
|
*/
|
2011-08-01 23:07:34 +00:00
|
|
|
// Check pgsql extension is loaded
|
|
|
|
|
if ( !extension_loaded('pgsql') )
|
|
|
|
|
wp_die( 'Your PHP installation appears to be missing the PostgreSQL extension which is required by WordPress with PG4WP.' );
|
2009-07-15 14:07:44 +00:00
|
|
|
|
|
|
|
|
// Initializing some variables
|
2011-08-05 22:43:58 +00:00
|
|
|
$GLOBALS['pg4wp_version'] = '7.0';
|
2009-07-15 14:07:44 +00:00
|
|
|
$GLOBALS['pg4wp_result'] = 0;
|
2011-09-10 22:11:30 +00:00
|
|
|
$GLOBALS['pg4wp_numrows_query'] = '';
|
2009-07-15 14:07:44 +00:00
|
|
|
$GLOBALS['pg4wp_ins_table'] = '';
|
|
|
|
|
$GLOBALS['pg4wp_ins_field'] = '';
|
2012-05-21 18:06:44 +00:00
|
|
|
$GLOBALS['pg4wp_last_insert'] = '';
|
2011-08-05 21:55:31 +00:00
|
|
|
$GLOBALS['pg4wp_connstr'] = '';
|
|
|
|
|
$GLOBALS['pg4wp_conn'] = false;
|
2009-07-15 14:07:44 +00:00
|
|
|
|
|
|
|
|
function wpsql_num_rows($result)
|
|
|
|
|
{ return pg_num_rows($result); }
|
|
|
|
|
function wpsql_numrows($result)
|
|
|
|
|
{ return pg_num_rows($result); }
|
|
|
|
|
function wpsql_num_fields($result)
|
2010-02-04 21:36:46 +00:00
|
|
|
{ return pg_num_fields($result); }
|
2009-07-15 14:07:44 +00:00
|
|
|
function wpsql_fetch_field($result)
|
|
|
|
|
{ return 'tablename'; }
|
|
|
|
|
function wpsql_fetch_object($result)
|
|
|
|
|
{ return pg_fetch_object($result); }
|
|
|
|
|
function wpsql_free_result($result)
|
|
|
|
|
{ return pg_free_result($result); }
|
|
|
|
|
function wpsql_affected_rows()
|
|
|
|
|
{
|
|
|
|
|
if( $GLOBALS['pg4wp_result'] === false)
|
|
|
|
|
return 0;
|
|
|
|
|
else
|
|
|
|
|
return pg_affected_rows($GLOBALS['pg4wp_result']);
|
|
|
|
|
}
|
|
|
|
|
function wpsql_fetch_row($result)
|
|
|
|
|
{ return pg_fetch_row($result); }
|
|
|
|
|
function wpsql_data_seek($result, $offset)
|
|
|
|
|
{ return pg_result_seek ( $result, $offset ); }
|
|
|
|
|
function wpsql_error()
|
2011-08-05 21:55:31 +00:00
|
|
|
{ if( $GLOBALS['pg4wp_conn']) return pg_last_error(); else return ''; }
|
2009-07-15 14:07:44 +00:00
|
|
|
function wpsql_fetch_assoc($result) { return pg_fetch_assoc($result); }
|
|
|
|
|
function wpsql_escape_string($s) { return pg_escape_string($s); }
|
2012-05-20 16:58:04 +00:00
|
|
|
function wpsql_real_escape_string($s,$c=NULL) { return pg_escape_string($s); }
|
2011-08-04 22:24:02 +00:00
|
|
|
function wpsql_get_server_info() { return '5.0.30'; } // Just want to fool wordpress ...
|
2014-08-21 16:50:36 +00:00
|
|
|
|
|
|
|
|
/**** Modified version of wpsql_result() is at the bottom of this file
|
2009-07-15 14:07:44 +00:00
|
|
|
function wpsql_result($result, $i, $fieldname)
|
|
|
|
|
{ return pg_fetch_result($result, $i, $fieldname); }
|
2014-08-21 16:50:36 +00:00
|
|
|
****/
|
2009-07-15 14:07:44 +00:00
|
|
|
|
2011-08-05 21:55:31 +00:00
|
|
|
// This is a fake connection except during installation
|
2009-07-15 14:07:44 +00:00
|
|
|
function wpsql_connect($dbserver, $dbuser, $dbpass)
|
|
|
|
|
{
|
2011-08-05 21:55:31 +00:00
|
|
|
$GLOBALS['pg4wp_connstr'] = '';
|
2012-05-20 22:17:54 +00:00
|
|
|
$hostport = explode(':', $dbserver);
|
|
|
|
|
if( !empty( $hostport[0]))
|
|
|
|
|
$GLOBALS['pg4wp_connstr'] .= ' host='.$hostport[0];
|
|
|
|
|
if( !empty( $hostport[1]))
|
|
|
|
|
$GLOBALS['pg4wp_connstr'] .= ' port='.$hostport[1];
|
2011-08-05 21:55:31 +00:00
|
|
|
if( !empty( $dbuser))
|
|
|
|
|
$GLOBALS['pg4wp_connstr'] .= ' user='.$dbuser;
|
|
|
|
|
if( !empty( $dbpass))
|
|
|
|
|
$GLOBALS['pg4wp_connstr'] .= ' password='.$dbpass;
|
|
|
|
|
elseif( !PG4WP_INSECURE)
|
|
|
|
|
wp_die( 'Connecting to your PostgreSQL database without a password is considered insecure.
|
|
|
|
|
<br />If you want to do it anyway, please set "PG4WP_INSECURE" to true in your "db.php" file.' );
|
|
|
|
|
|
2011-08-18 21:38:17 +00:00
|
|
|
// While installing, we test the connection to 'template1' (as we don't know the effective dbname yet)
|
2011-08-05 21:55:31 +00:00
|
|
|
if( defined('WP_INSTALLING') && WP_INSTALLING)
|
|
|
|
|
return wpsql_select_db( 'template1');
|
|
|
|
|
|
2009-07-15 14:07:44 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
2011-08-05 21:55:31 +00:00
|
|
|
|
|
|
|
|
// The effective connection happens here
|
2009-07-15 14:07:44 +00:00
|
|
|
function wpsql_select_db($dbname, $connection_id = 0)
|
|
|
|
|
{
|
2011-08-05 21:55:31 +00:00
|
|
|
$pg_connstr = $GLOBALS['pg4wp_connstr'].' dbname='.$dbname;
|
|
|
|
|
|
|
|
|
|
$GLOBALS['pg4wp_conn'] = pg_connect($pg_connstr);
|
|
|
|
|
|
2011-08-05 22:43:58 +00:00
|
|
|
if( $GLOBALS['pg4wp_conn'])
|
|
|
|
|
{
|
|
|
|
|
$ver = pg_version($GLOBALS['pg4wp_conn']);
|
|
|
|
|
if( isset($ver['server']))
|
|
|
|
|
$GLOBALS['pg4wp_version'] = $ver['server'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now we should be connected, we "forget" about the connection parameters (if this is not a "test" connection)
|
2011-08-05 21:55:31 +00:00
|
|
|
if( !defined('WP_INSTALLING') || !WP_INSTALLING)
|
|
|
|
|
$GLOBALS['pg4wp_connstr'] = '';
|
|
|
|
|
|
2011-08-01 21:43:06 +00:00
|
|
|
// Execute early transmitted commands if needed
|
|
|
|
|
if( isset($GLOBALS['pg4wp_pre_sql']) && !empty($GLOBALS['pg4wp_pre_sql']))
|
|
|
|
|
foreach( $GLOBALS['pg4wp_pre_sql'] as $sql2run)
|
|
|
|
|
wpsql_query( $sql2run);
|
2011-08-05 21:55:31 +00:00
|
|
|
|
2011-08-01 21:43:06 +00:00
|
|
|
return $GLOBALS['pg4wp_conn'];
|
2009-07-15 14:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function wpsql_fetch_array($result)
|
|
|
|
|
{
|
|
|
|
|
$res = pg_fetch_array($result);
|
|
|
|
|
|
|
|
|
|
if( is_array($res) )
|
|
|
|
|
foreach($res as $v => $k )
|
|
|
|
|
$res[$v] = trim($k);
|
|
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function wpsql_query($sql)
|
|
|
|
|
{
|
2011-08-05 21:55:31 +00:00
|
|
|
if( !$GLOBALS['pg4wp_conn'])
|
2011-08-01 21:43:06 +00:00
|
|
|
{
|
|
|
|
|
// Catch SQL to be executed as soon as connected
|
|
|
|
|
$GLOBALS['pg4wp_pre_sql'][] = $sql;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2011-09-10 23:38:01 +00:00
|
|
|
|
2011-10-31 21:50:04 +00:00
|
|
|
$initial = $sql;
|
2011-09-10 23:38:01 +00:00
|
|
|
$sql = pg4wp_rewrite( $sql);
|
|
|
|
|
|
|
|
|
|
$GLOBALS['pg4wp_result'] = pg_query($sql);
|
|
|
|
|
if( (PG4WP_DEBUG || PG4WP_LOG_ERRORS) && $GLOBALS['pg4wp_result'] === false && $err = pg_last_error())
|
2011-10-31 21:50:04 +00:00
|
|
|
{
|
|
|
|
|
$ignore = false;
|
|
|
|
|
if( defined('WP_INSTALLING') && WP_INSTALLING)
|
|
|
|
|
{
|
|
|
|
|
global $table_prefix;
|
2012-05-15 19:19:51 +00:00
|
|
|
$ignore = strpos($err, 'relation "'.$table_prefix);
|
2011-10-31 21:50:04 +00:00
|
|
|
}
|
|
|
|
|
if( ! $ignore )
|
|
|
|
|
error_log('['.microtime(true)."] Error running :\n$initial\n---- converted to ----\n$sql\n----> $err\n---------------------\n", 3, PG4WP_LOG.'pg4wp_errors.log');
|
|
|
|
|
}
|
2011-09-10 23:38:01 +00:00
|
|
|
return $GLOBALS['pg4wp_result'];
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-20 21:57:33 +00:00
|
|
|
function wpsql_insert_id($lnk = NULL)
|
2011-09-10 23:38:01 +00:00
|
|
|
{
|
|
|
|
|
global $wpdb;
|
|
|
|
|
$ins_field = $GLOBALS['pg4wp_ins_field'];
|
2012-05-20 21:57:33 +00:00
|
|
|
$table = $GLOBALS['pg4wp_ins_table'];
|
2012-05-21 18:06:44 +00:00
|
|
|
$lastq = $GLOBALS['pg4wp_last_insert'];
|
2011-09-10 23:38:01 +00:00
|
|
|
|
2012-05-20 21:57:33 +00:00
|
|
|
$seq = $table . '_seq';
|
2011-09-10 23:38:01 +00:00
|
|
|
|
2012-05-20 21:57:33 +00:00
|
|
|
// Table 'term_relationships' doesn't have a sequence
|
2012-05-21 18:06:44 +00:00
|
|
|
if( $table == $wpdb->term_relationships)
|
2012-05-20 21:57:33 +00:00
|
|
|
{
|
|
|
|
|
$sql = 'NO QUERY';
|
|
|
|
|
$data = 0;
|
|
|
|
|
}
|
2012-05-21 18:06:44 +00:00
|
|
|
// When using WP_Import plugin, ID is defined in the query
|
|
|
|
|
elseif('post_author' == $ins_field && false !== strpos($lastq,'ID'))
|
|
|
|
|
{
|
|
|
|
|
$sql = 'ID was in query ';
|
|
|
|
|
$pattern = '/.+\'(\d+).+$/';
|
|
|
|
|
preg_match($pattern, $lastq, $matches);
|
|
|
|
|
$data = $matches[1];
|
|
|
|
|
// We should update the sequence on the next non-INSERT query
|
|
|
|
|
$GLOBALS['pg4wp_queued_query'] = "SELECT SETVAL('$seq',(SELECT MAX(\"ID\") FROM $table)+1);";
|
|
|
|
|
}
|
2011-09-10 23:38:01 +00:00
|
|
|
else
|
2012-05-20 21:57:33 +00:00
|
|
|
{
|
|
|
|
|
$sql = "SELECT CURRVAL('$seq')";
|
|
|
|
|
|
|
|
|
|
$res = pg_query($sql);
|
|
|
|
|
if( false !== $res)
|
|
|
|
|
$data = pg_fetch_result($res, 0, 0);
|
|
|
|
|
elseif( PG4WP_DEBUG || PG4WP_ERROR_LOG)
|
|
|
|
|
{
|
|
|
|
|
$log = '['.microtime(true)."] wpsql_insert_id() was called with '$table' and '$ins_field'".
|
|
|
|
|
" and generated an error. The latest INSERT query was :\n'$lastq'\n";
|
|
|
|
|
error_log( $log, 3, PG4WP_LOG.'pg4wp_errors.log');
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-09-10 23:38:01 +00:00
|
|
|
if( PG4WP_DEBUG && $sql)
|
2012-05-20 21:57:33 +00:00
|
|
|
error_log( '['.microtime(true)."] Getting inserted ID for '$table' ('$ins_field') : $sql => $data\n", 3, PG4WP_LOG.'pg4wp_insertid.log');
|
|
|
|
|
|
2011-09-10 23:38:01 +00:00
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function pg4wp_rewrite( $sql)
|
|
|
|
|
{
|
2011-08-04 22:11:49 +00:00
|
|
|
global $wpdb;
|
2011-08-26 23:12:33 +00:00
|
|
|
|
2009-07-15 14:07:44 +00:00
|
|
|
$logto = 'queries';
|
2010-04-09 00:04:58 +00:00
|
|
|
// The end of the query may be protected against changes
|
|
|
|
|
$end = '';
|
2009-07-15 14:07:44 +00:00
|
|
|
|
|
|
|
|
// Remove unusefull spaces
|
|
|
|
|
$initial = $sql = trim($sql);
|
|
|
|
|
|
|
|
|
|
if( 0 === strpos($sql, 'SELECT'))
|
|
|
|
|
{
|
|
|
|
|
$logto = 'SELECT';
|
|
|
|
|
// SQL_CALC_FOUND_ROWS doesn't exist in PostgreSQL but it's needed for correct paging
|
|
|
|
|
if( false !== strpos($sql, 'SQL_CALC_FOUND_ROWS'))
|
|
|
|
|
{
|
2011-08-01 22:34:09 +00:00
|
|
|
$sql = str_replace('SQL_CALC_FOUND_ROWS', '', $sql);
|
2011-09-11 00:00:04 +00:00
|
|
|
$GLOBALS['pg4wp_numrows_query'] = $sql;
|
|
|
|
|
if( PG4WP_DEBUG)
|
2011-10-31 21:50:04 +00:00
|
|
|
error_log( '['.microtime(true)."] Number of rows required for :\n$sql\n---------------------\n", 3, PG4WP_LOG.'pg4wp_NUMROWS.log');
|
2009-07-15 14:07:44 +00:00
|
|
|
}
|
|
|
|
|
elseif( false !== strpos($sql, 'FOUND_ROWS()'))
|
2011-09-10 22:11:30 +00:00
|
|
|
{
|
2011-09-11 00:00:04 +00:00
|
|
|
// Here we convert the latest query into a COUNT query
|
2011-09-10 22:11:30 +00:00
|
|
|
$sql = $GLOBALS['pg4wp_numrows_query'];
|
|
|
|
|
// Remove any LIMIT ... clause (this is the blocking part)
|
|
|
|
|
$pattern = '/\s+LIMIT.+/';
|
|
|
|
|
$sql = preg_replace( $pattern, '', $sql);
|
|
|
|
|
// Now add the COUNT() statement
|
|
|
|
|
$pattern = '/SELECT\s+([^\s]+)\s+(FROM.+)/';
|
|
|
|
|
$sql = preg_replace( $pattern, 'SELECT COUNT($1) $2', $sql);
|
|
|
|
|
}
|
2011-08-04 23:01:39 +00:00
|
|
|
|
2012-05-09 20:59:27 +00:00
|
|
|
// Handle CAST( ... AS CHAR)
|
|
|
|
|
$sql = preg_replace( '/CAST\((.+) AS CHAR\)/', 'CAST($1 AS TEXT)', $sql);
|
|
|
|
|
|
2010-04-04 09:45:30 +00:00
|
|
|
// Handle COUNT(*)...ORDER BY...
|
2012-05-09 21:33:33 +00:00
|
|
|
$sql = preg_replace( '/COUNT(.+)ORDER BY.+/s', 'COUNT$1', $sql);
|
2009-07-15 14:07:44 +00:00
|
|
|
|
2011-08-24 20:57:55 +00:00
|
|
|
// In order for users counting to work...
|
|
|
|
|
$matches = array();
|
|
|
|
|
if( preg_match_all( '/COUNT[^C]+\),/',$sql, $matches))
|
|
|
|
|
{
|
|
|
|
|
foreach( $matches[0] as $num => $one)
|
|
|
|
|
{
|
|
|
|
|
$sub = substr( $one, 0, -1);
|
|
|
|
|
$sql = str_replace( $sub, $sub.' AS count'.$num, $sql);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-15 14:07:44 +00:00
|
|
|
$pattern = '/LIMIT[ ]+(\d+),[ ]*(\d+)/';
|
|
|
|
|
$sql = preg_replace($pattern, 'LIMIT $2 OFFSET $1', $sql);
|
|
|
|
|
|
|
|
|
|
$pattern = '/DATE_ADD[ ]*\(([^,]+),([^\)]+)\)/';
|
|
|
|
|
$sql = preg_replace( $pattern, '($1 + $2)', $sql);
|
|
|
|
|
|
2010-01-19 20:05:57 +00:00
|
|
|
// UNIX_TIMESTAMP in MYSQL returns an integer
|
2011-08-16 16:04:10 +00:00
|
|
|
$pattern = '/UNIX_TIMESTAMP\(([^\)]+)\)/';
|
2010-01-19 20:05:57 +00:00
|
|
|
$sql = preg_replace( $pattern, 'ROUND(DATE_PART(\'epoch\',$1))', $sql);
|
|
|
|
|
|
2009-07-15 14:07:44 +00:00
|
|
|
$date_funcs = array(
|
2010-02-01 21:34:28 +00:00
|
|
|
'DAYOFMONTH(' => 'EXTRACT(DAY FROM ',
|
2010-01-19 19:50:28 +00:00
|
|
|
'YEAR(' => 'EXTRACT(YEAR FROM ',
|
|
|
|
|
'MONTH(' => 'EXTRACT(MONTH FROM ',
|
|
|
|
|
'DAY(' => 'EXTRACT(DAY FROM ',
|
2009-07-15 14:07:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$sql = str_replace( 'ORDER BY post_date DESC', 'ORDER BY YEAR(post_date) DESC, MONTH(post_date) DESC', $sql);
|
2010-03-07 22:52:38 +00:00
|
|
|
$sql = str_replace( 'ORDER BY post_date ASC', 'ORDER BY YEAR(post_date) ASC, MONTH(post_date) ASC', $sql);
|
2009-07-15 14:07:44 +00:00
|
|
|
$sql = str_replace( array_keys($date_funcs), array_values($date_funcs), $sql);
|
2010-03-07 22:52:38 +00:00
|
|
|
$curryear = date( 'Y');
|
|
|
|
|
$sql = str_replace( 'FROM \''.$curryear, 'FROM TIMESTAMP \''.$curryear, $sql);
|
2009-07-15 14:07:44 +00:00
|
|
|
|
2011-08-01 22:42:10 +00:00
|
|
|
// MySQL 'IF' conversion - Note : NULLIF doesn't need to be corrected
|
|
|
|
|
$pattern = '/ (?<!NULL)IF[ ]*\(([^,]+),([^,]+),([^\)]+)\)/';
|
2010-04-07 23:41:32 +00:00
|
|
|
$sql = preg_replace( $pattern, ' CASE WHEN $1 THEN $2 ELSE $3 END', $sql);
|
2009-07-15 14:07:44 +00:00
|
|
|
|
2011-08-11 08:10:29 +00:00
|
|
|
$sql = str_replace('GROUP BY '.$wpdb->prefix.'posts.ID', '' , $sql);
|
2009-07-15 14:07:44 +00:00
|
|
|
$sql = str_replace("!= ''", '<> 0', $sql);
|
|
|
|
|
|
|
|
|
|
// MySQL 'LIKE' is case insensitive by default, whereas PostgreSQL 'LIKE' is
|
|
|
|
|
$sql = str_replace( ' LIKE ', ' ILIKE ', $sql);
|
|
|
|
|
|
|
|
|
|
// INDEXES are not yet supported
|
|
|
|
|
if( false !== strpos( $sql, 'USE INDEX (comment_date_gmt)'))
|
|
|
|
|
$sql = str_replace( 'USE INDEX (comment_date_gmt)', '', $sql);
|
|
|
|
|
|
2010-02-01 21:45:55 +00:00
|
|
|
// HB : timestamp fix for permalinks
|
2010-02-02 13:16:16 +00:00
|
|
|
$sql = str_replace( 'post_date_gmt > 1970', 'post_date_gmt > to_timestamp (\'1970\')', $sql);
|
2010-02-01 21:45:55 +00:00
|
|
|
|
2011-08-26 23:12:33 +00:00
|
|
|
// Akismet sometimes doesn't write 'comment_ID' with 'ID' in capitals where needed ...
|
|
|
|
|
if( false !== strpos( $sql, $wpdb->comments))
|
|
|
|
|
$sql = str_replace(' comment_id ', ' comment_ID ', $sql);
|
|
|
|
|
|
2009-07-15 14:07:44 +00:00
|
|
|
} // SELECT
|
|
|
|
|
elseif( 0 === strpos($sql, 'UPDATE'))
|
|
|
|
|
{
|
|
|
|
|
$logto = 'UPDATE';
|
|
|
|
|
$pattern = '/LIMIT[ ]+\d+/';
|
|
|
|
|
$sql = preg_replace($pattern, '', $sql);
|
|
|
|
|
|
2010-04-09 00:45:33 +00:00
|
|
|
// For correct bactick removal
|
2010-04-09 00:58:19 +00:00
|
|
|
$pattern = '/[ ]*`([^` ]+)`[ ]*=/';
|
2010-04-09 00:45:33 +00:00
|
|
|
$sql = preg_replace( $pattern, ' $1 =', $sql);
|
|
|
|
|
|
2012-06-14 09:15:09 +00:00
|
|
|
// For correct ID quoting
|
|
|
|
|
$pattern = '/[ ]*([^ ]*ID[^ ]*)[ ]*=/';
|
|
|
|
|
$sql = preg_replace( $pattern, ' "$1" =', $sql);
|
|
|
|
|
|
2010-04-09 00:45:33 +00:00
|
|
|
// This will avoid modifications to anything following ' SET '
|
|
|
|
|
list($sql,$end) = explode( ' SET ', $sql, 2);
|
|
|
|
|
$end = ' SET '.$end;
|
2009-07-15 14:07:44 +00:00
|
|
|
} // UPDATE
|
|
|
|
|
elseif( 0 === strpos($sql, 'INSERT'))
|
|
|
|
|
{
|
|
|
|
|
$logto = 'INSERT';
|
|
|
|
|
$sql = str_replace('(0,',"('0',", $sql);
|
|
|
|
|
$sql = str_replace('(1,',"('1',", $sql);
|
|
|
|
|
|
|
|
|
|
// Fix inserts into wp_categories
|
2011-08-26 23:12:33 +00:00
|
|
|
if( false !== strpos($sql, 'INSERT INTO '.$wpdb->categories))
|
2009-07-15 14:07:44 +00:00
|
|
|
{
|
|
|
|
|
$sql = str_replace('"cat_ID",', '', $sql);
|
|
|
|
|
$sql = str_replace("VALUES ('0',", "VALUES(", $sql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Those are used when we need to set the date to now() in gmt time
|
|
|
|
|
$sql = str_replace( "'0000-00-00 00:00:00'", 'now() AT TIME ZONE \'gmt\'', $sql);
|
|
|
|
|
|
|
|
|
|
// Multiple values group when calling INSERT INTO don't always work
|
2011-08-26 23:12:33 +00:00
|
|
|
if( false !== strpos( $sql, $wpdb->options) && false !== strpos( $sql, '), ('))
|
2009-07-15 14:07:44 +00:00
|
|
|
{
|
|
|
|
|
$pattern = '/INSERT INTO.+VALUES/';
|
|
|
|
|
preg_match($pattern, $sql, $matches);
|
|
|
|
|
$insert = $matches[0];
|
|
|
|
|
$sql = str_replace( '), (', ');'.$insert.'(', $sql);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-16 00:11:44 +00:00
|
|
|
// Support for "INSERT ... ON DUPLICATE KEY UPDATE ..." is a dirty hack
|
|
|
|
|
// consisting in deleting the row before inserting it
|
|
|
|
|
if( false !== $pos = strpos( $sql, 'ON DUPLICATE KEY'))
|
|
|
|
|
{
|
2011-09-10 23:38:01 +00:00
|
|
|
// Get the elements we need (table name, first field, corresponding value)
|
|
|
|
|
$pattern = '/INSERT INTO\s+([^\(]+)\(([^,]+)[^\(]+VALUES\s*\(([^,]+)/';
|
2010-01-16 00:11:44 +00:00
|
|
|
preg_match($pattern, $sql, $matches);
|
2012-05-20 19:24:06 +00:00
|
|
|
$table = trim( $matches[1], ' `');
|
|
|
|
|
if( !in_array(trim($matches[1],'` '), array($wpdb->posts,$wpdb->comments)))
|
|
|
|
|
{
|
|
|
|
|
// Remove 'ON DUPLICATE KEY UPDATE...' and following
|
|
|
|
|
$sql = substr( $sql, 0, $pos);
|
|
|
|
|
// Add a delete query to handle the maybe existing data
|
|
|
|
|
$sql = 'DELETE FROM '.$table.' WHERE '.$matches[2].' = '.$matches[3].';'.$sql;
|
|
|
|
|
}
|
2010-01-16 00:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 14:07:44 +00:00
|
|
|
// To avoid Encoding errors when inserting data coming from outside
|
|
|
|
|
if( preg_match('/^.{1}/us',$sql,$ar) != 1)
|
|
|
|
|
$sql = utf8_encode($sql);
|
|
|
|
|
|
2010-04-09 00:45:33 +00:00
|
|
|
// This will avoid modifications to anything following ' VALUES'
|
2010-04-09 00:09:06 +00:00
|
|
|
list($sql,$end) = explode( ' VALUES', $sql, 2);
|
|
|
|
|
$end = ' VALUES'.$end;
|
2012-05-20 12:30:56 +00:00
|
|
|
|
|
|
|
|
// When installing, the sequence for table terms has to be updated
|
|
|
|
|
if( defined('WP_INSTALLING') && WP_INSTALLING && false !== strpos($sql, 'INSERT INTO `'.$wpdb->terms.'`'))
|
|
|
|
|
$end .= ';SELECT setval(\''.$wpdb->terms.'_seq\', (SELECT MAX(term_id) FROM '.$wpdb->terms.')+1);';
|
|
|
|
|
|
2009-07-15 14:07:44 +00:00
|
|
|
} // INSERT
|
|
|
|
|
elseif( 0 === strpos( $sql, 'DELETE' ))
|
|
|
|
|
{
|
|
|
|
|
$logto = 'DELETE';
|
|
|
|
|
// LIMIT is not allowed in DELETE queries
|
|
|
|
|
$sql = str_replace( 'LIMIT 1', '', $sql);
|
2010-01-16 00:11:44 +00:00
|
|
|
$sql = str_replace( ' REGEXP ', ' ~ ', $sql);
|
2010-01-16 22:15:14 +00:00
|
|
|
|
|
|
|
|
// This handles removal of duplicate entries in table options
|
|
|
|
|
if( false !== strpos( $sql, 'DELETE o1 FROM '))
|
2011-08-26 23:12:33 +00:00
|
|
|
$sql = "DELETE FROM $wpdb->options WHERE option_id IN " .
|
|
|
|
|
"(SELECT o1.option_id FROM $wpdb->options AS o1, $wpdb->options AS o2 " .
|
2010-01-16 22:15:14 +00:00
|
|
|
"WHERE o1.option_name = o2.option_name " .
|
|
|
|
|
"AND o1.option_id < o2.option_id)";
|
2011-08-26 23:12:33 +00:00
|
|
|
|
|
|
|
|
// Akismet sometimes doesn't write 'comment_ID' with 'ID' in capitals where needed ...
|
|
|
|
|
if( false !== strpos( $sql, $wpdb->comments))
|
|
|
|
|
$sql = str_replace(' comment_id ', ' comment_ID ', $sql);
|
2009-07-15 14:07:44 +00:00
|
|
|
}
|
|
|
|
|
// Fix tables listing
|
|
|
|
|
elseif( 0 === strpos($sql, 'SHOW TABLES'))
|
|
|
|
|
{
|
|
|
|
|
$logto = 'SHOWTABLES';
|
|
|
|
|
$sql = 'SELECT tablename FROM pg_tables WHERE schemaname = \'public\';';
|
|
|
|
|
}
|
2011-08-02 00:00:07 +00:00
|
|
|
// Rewriting optimize table
|
|
|
|
|
elseif( 0 === strpos($sql, 'OPTIMIZE TABLE'))
|
|
|
|
|
{
|
|
|
|
|
$logto = 'OPTIMIZE';
|
|
|
|
|
$sql = str_replace( 'OPTIMIZE TABLE', 'VACUUM', $sql);
|
|
|
|
|
}
|
2011-08-26 21:57:44 +00:00
|
|
|
// Handle 'SET NAMES ... COLLATE ...'
|
2012-05-15 19:01:56 +00:00
|
|
|
elseif( 0 === strpos($sql, 'SET NAMES') && false !== strpos($sql, 'COLLATE'))
|
2011-08-26 21:57:44 +00:00
|
|
|
{
|
|
|
|
|
$logto = 'SETNAMES';
|
|
|
|
|
$sql = "SET NAMES 'utf8'";
|
|
|
|
|
}
|
2011-08-03 21:30:21 +00:00
|
|
|
// Load up upgrade and install functions as required
|
2011-08-27 00:32:14 +00:00
|
|
|
$begin = substr( $sql, 0, 3);
|
2012-05-20 12:57:04 +00:00
|
|
|
$search = array( 'SHO', 'ALT', 'DES', 'CRE', 'DRO');
|
2011-08-27 00:32:14 +00:00
|
|
|
if( in_array($begin, $search))
|
2011-08-03 21:30:21 +00:00
|
|
|
{
|
|
|
|
|
require_once( PG4WP_ROOT.'/driver_pgsql_install.php');
|
2010-01-19 22:56:00 +00:00
|
|
|
$sql = pg4wp_installing( $sql, $logto);
|
2011-08-03 21:30:21 +00:00
|
|
|
}
|
2009-07-15 14:07:44 +00:00
|
|
|
|
2010-02-01 21:40:16 +00:00
|
|
|
// WP 2.9.1 uses a comparison where text data is not quoted
|
|
|
|
|
$pattern = '/AND meta_value = (-?\d+)/';
|
|
|
|
|
$sql = preg_replace( $pattern, 'AND meta_value = \'$1\'', $sql);
|
2011-08-24 20:57:55 +00:00
|
|
|
|
|
|
|
|
// Generic "INTERVAL xx YEAR|MONTH|DAY|HOUR|MINUTE|SECOND" handler
|
|
|
|
|
$pattern = '/INTERVAL[ ]+(\d+)[ ]+(YEAR|MONTH|DAY|HOUR|MINUTE|SECOND)/';
|
2010-04-09 00:04:58 +00:00
|
|
|
$sql = preg_replace( $pattern, "'\$1 \$2'::interval", $sql);
|
2010-01-20 22:58:55 +00:00
|
|
|
$pattern = '/DATE_SUB[ ]*\(([^,]+),([^\)]+)\)/';
|
|
|
|
|
$sql = preg_replace( $pattern, '($1::timestamp - $2)', $sql);
|
2010-01-20 07:32:26 +00:00
|
|
|
|
2010-04-09 00:04:58 +00:00
|
|
|
// Remove illegal characters
|
|
|
|
|
$sql = str_replace('`', '', $sql);
|
|
|
|
|
|
2010-01-16 00:11:44 +00:00
|
|
|
// Field names with CAPITALS need special handling
|
|
|
|
|
if( false !== strpos($sql, 'ID'))
|
|
|
|
|
{
|
|
|
|
|
$pattern = '/ID([^ ])/';
|
|
|
|
|
$sql = preg_replace($pattern, 'ID $1', $sql);
|
|
|
|
|
$pattern = '/ID$/';
|
|
|
|
|
$sql = preg_replace($pattern, 'ID ', $sql);
|
|
|
|
|
$pattern = '/\(ID/';
|
|
|
|
|
$sql = preg_replace($pattern, '( ID', $sql);
|
|
|
|
|
$pattern = '/,ID/';
|
|
|
|
|
$sql = preg_replace($pattern, ', ID', $sql);
|
2012-05-15 19:01:56 +00:00
|
|
|
$pattern = '/[0-9a-zA-Z_]+ID/';
|
2010-01-16 00:11:44 +00:00
|
|
|
$sql = preg_replace($pattern, '"$0"', $sql);
|
|
|
|
|
$pattern = '/\.ID/';
|
|
|
|
|
$sql = preg_replace($pattern, '."ID"', $sql);
|
|
|
|
|
$pattern = '/[\s]ID /';
|
|
|
|
|
$sql = preg_replace($pattern, ' "ID" ', $sql);
|
|
|
|
|
$pattern = '/"ID "/';
|
|
|
|
|
$sql = preg_replace($pattern, ' "ID" ', $sql);
|
|
|
|
|
} // CAPITALS
|
|
|
|
|
|
2009-07-15 14:07:44 +00:00
|
|
|
// Empty "IN" statements are erroneous
|
|
|
|
|
$sql = str_replace( 'IN (\'\')', 'IN (NULL)', $sql);
|
|
|
|
|
$sql = str_replace( 'IN ( \'\' )', 'IN (NULL)', $sql);
|
|
|
|
|
$sql = str_replace( 'IN ()', 'IN (NULL)', $sql);
|
|
|
|
|
|
2012-05-20 21:57:33 +00:00
|
|
|
// Put back the end of the query if it was separated
|
|
|
|
|
$sql .= $end;
|
|
|
|
|
|
2011-08-01 20:48:51 +00:00
|
|
|
// For insert ID catching
|
|
|
|
|
if( $logto == 'INSERT')
|
|
|
|
|
{
|
|
|
|
|
$pattern = '/INSERT INTO (\w+)\s+\([ a-zA-Z_"]+/';
|
|
|
|
|
preg_match($pattern, $sql, $matches);
|
|
|
|
|
$GLOBALS['pg4wp_ins_table'] = $matches[1];
|
|
|
|
|
$match_list = split(' ', $matches[0]);
|
|
|
|
|
if( $GLOBALS['pg4wp_ins_table'])
|
|
|
|
|
{
|
|
|
|
|
$GLOBALS['pg4wp_ins_field'] = trim($match_list[3],' () ');
|
|
|
|
|
if(! $GLOBALS['pg4wp_ins_field'])
|
|
|
|
|
$GLOBALS['pg4wp_ins_field'] = trim($match_list[4],' () ');
|
|
|
|
|
}
|
2012-05-21 18:06:44 +00:00
|
|
|
$GLOBALS['pg4wp_last_insert'] = $sql;
|
|
|
|
|
}
|
|
|
|
|
elseif( isset($GLOBALS['pg4wp_queued_query']))
|
|
|
|
|
{
|
|
|
|
|
pg_query($GLOBALS['pg4wp_queued_query']);
|
|
|
|
|
unset($GLOBALS['pg4wp_queued_query']);
|
2011-08-01 20:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-20 10:24:18 +00:00
|
|
|
// Correct quoting for PostgreSQL 9.1+ compatibility
|
|
|
|
|
$sql = str_replace( "\\'", "''", $sql);
|
|
|
|
|
$sql = str_replace( '\"', '"', $sql);
|
|
|
|
|
|
2009-07-15 14:07:44 +00:00
|
|
|
if( PG4WP_DEBUG)
|
|
|
|
|
{
|
|
|
|
|
if( $initial != $sql)
|
2011-10-31 21:50:04 +00:00
|
|
|
error_log( '['.microtime(true)."] Converting :\n$initial\n---- to ----\n$sql\n---------------------\n", 3, PG4WP_LOG.'pg4wp_'.$logto.'.log');
|
2009-07-15 14:07:44 +00:00
|
|
|
else
|
2011-10-31 21:50:04 +00:00
|
|
|
error_log( '['.microtime(true)."] $sql\n---------------------\n", 3, PG4WP_LOG.'pg4wp_unmodified.log');
|
2009-07-15 14:07:44 +00:00
|
|
|
}
|
2011-09-10 23:38:01 +00:00
|
|
|
return $sql;
|
2009-07-15 14:07:44 +00:00
|
|
|
}
|
2014-08-21 16:50:36 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Quick fix for wpsql_result() error and missing wpsql_errno() function
|
|
|
|
|
Source : http://vitoriodelage.wordpress.com/2014/06/06/add-missing-wpsql_errno-in-pg4wp-plugin/
|
|
|
|
|
*/
|
|
|
|
|
function wpsql_result($result, $i, $fieldname = null) {
|
|
|
|
|
if (is_resource($result)) {
|
|
|
|
|
if ($fieldname) {
|
|
|
|
|
return pg_fetch_result($result, $i, $fieldname);
|
|
|
|
|
} else {
|
|
|
|
|
return pg_fetch_result($result, $i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function wpsql_errno( $connection) {
|
|
|
|
|
$result = pg_get_result($connection);
|
|
|
|
|
$result_status = pg_result_status($result);
|
|
|
|
|
return pg_result_error_field($result_status, PGSQL_DIAG_SQLSTATE);
|
|
|
|
|
}
|