Create MySQL-compatible field function on connect

Fixes: #8

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
This commit is contained in:
Kevin Locke
2017-04-08 16:20:58 -06:00
parent 1d2666ca1d
commit 6587b4085b

View File

@ -109,6 +109,8 @@
foreach( $GLOBALS['pg4wp_pre_sql'] as $sql2run)
wpsql_query( $sql2run);
pg4wp_init($conn);
return $conn;
}
@ -573,6 +575,35 @@
return $sql;
}
// Database initialization
function pg4wp_init()
{
// Provide (mostly) MySQL-compatible field function
// Note: MySQL accepts heterogeneous argument types. No easy fix.
// Can define version with typed first arg to cover some cases.
// Note: ROW_NUMBER+unnest doesn't guarantee order, but is simple/fast.
// If it breaks, try https://stackoverflow.com/a/8767450
$result = pg_query(<<<SQL
CREATE OR REPLACE FUNCTION field(anyelement, VARIADIC anyarray)
RETURNS BIGINT AS
$$
SELECT rownum
FROM (SELECT ROW_NUMBER() OVER () AS rownum, elem
FROM unnest($2) elem) numbered
WHERE numbered.elem = $1
UNION ALL
SELECT 0
$$
LANGUAGE SQL IMMUTABLE;
SQL
);
if( (PG4WP_DEBUG || PG4WP_LOG_ERRORS) && $result === false )
{
$err = pg_last_error();
error_log('['.microtime(true)."] Error creating MySQL-compatible field function: $err\n", 3, PG4WP_LOG.'pg4wp_errors.log');
}
}
/*
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/