some more PostgreSQL stuff from ADOdb 5.11

This commit is contained in:
Ralf Becker 2011-04-10 15:04:40 +00:00
parent 51c365d572
commit bd4f019062
10 changed files with 207 additions and 86 deletions

View File

@ -152,7 +152,7 @@ WHERE relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s'))
Using a OID as a unique identifier is not generally wise. Using a OID as a unique identifier is not generally wise.
Unless you are very careful, you might end up with a tuple having Unless you are very careful, you might end up with a tuple having
a different OID if a database must be reloaded. */ a different OID if a database must be reloaded. */
function _insertid($table,$column,$try_oid=true) function _insertid($table,$column,$try_oid=false)
{ {
if ($try_oid) if ($try_oid)
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
/* /*
V4.65 22 July 2005 (c) 2000-2005 John Lim (jlim#natsoft.com.my). All rights reserved. V5.11 5 May 2010 (c) 2000-2010 John Lim (jlim#natsoft.com). All rights reserved.
Released under both BSD license and Lesser GPL library license. Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses, Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence. the BSD license will take precedence.
@ -28,19 +28,20 @@ class ADODB_postgres7 extends ADODB_postgres64 {
if (ADODB_ASSOC_CASE !== 2) { if (ADODB_ASSOC_CASE !== 2) {
$this->rsPrefix .= 'assoc_'; $this->rsPrefix .= 'assoc_';
} }
$this->_bindInputArray = PHP_VERSION >= 5.1;
} }
// the following should be compat with postgresql 7.2, // the following should be compat with postgresql 7.2,
// which makes obsolete the LIMIT limit,offset syntax // which makes obsolete the LIMIT limit,offset syntax
function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
{ {
$offsetStr = ($offset >= 0) ? ' OFFSET '.(int)$offset : ''; $offsetStr = ($offset >= 0) ? " OFFSET ".((integer)$offset) : '';
$limitStr = ($nrows >= 0) ? ' LIMIT '.(int)$nrows : ''; $limitStr = ($nrows >= 0) ? " LIMIT ".((integer)$nrows) : '';
if ($secs2cache) if ($secs2cache)
$rs =& $this->CacheExecute($secs2cache,$sql.$limitStr.$offsetStr,$inputarr); $rs = $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
else else
$rs =& $this->Execute($sql.$limitStr.$offsetStr,$inputarr); $rs = $this->Execute($sql."$limitStr$offsetStr",$inputarr);
return $rs; return $rs;
} }
@ -55,8 +56,58 @@ class ADODB_postgres7 extends ADODB_postgres64 {
} }
*/ */
// from Edward Jaramilla, improved version - works on pg 7.4 /*
I discovered that the MetaForeignKeys method no longer worked for Postgres 8.3.
I went ahead and modified it to work for both 8.2 and 8.3.
Please feel free to include this change in your next release of adodb.
William Kolodny [William.Kolodny#gt-t.net]
*/
function MetaForeignKeys($table, $owner=false, $upper=false) function MetaForeignKeys($table, $owner=false, $upper=false)
{
$sql="
SELECT fum.ftblname AS lookup_table, split_part(fum.rf, ')'::text, 1) AS lookup_field,
fum.ltable AS dep_table, split_part(fum.lf, ')'::text, 1) AS dep_field
FROM (
SELECT fee.ltable, fee.ftblname, fee.consrc, split_part(fee.consrc,'('::text, 2) AS lf,
split_part(fee.consrc, '('::text, 3) AS rf
FROM (
SELECT foo.relname AS ltable, foo.ftblname,
pg_get_constraintdef(foo.oid) AS consrc
FROM (
SELECT c.oid, c.conname AS name, t.relname, ft.relname AS ftblname
FROM pg_constraint c
JOIN pg_class t ON (t.oid = c.conrelid)
JOIN pg_class ft ON (ft.oid = c.confrelid)
JOIN pg_namespace nft ON (nft.oid = ft.relnamespace)
LEFT JOIN pg_description ds ON (ds.objoid = c.oid)
JOIN pg_namespace n ON (n.oid = t.relnamespace)
WHERE c.contype = 'f'::\"char\"
ORDER BY t.relname, n.nspname, c.conname, c.oid
) foo
) fee) fum
WHERE fum.ltable='".strtolower($table)."'
ORDER BY fum.ftblname, fum.ltable, split_part(fum.lf, ')'::text, 1)
";
$rs = $this->Execute($sql);
if (!$rs || $rs->EOF) return false;
$a = array();
while (!$rs->EOF) {
if ($upper) {
$a[strtoupper($rs->Fields('lookup_table'))][] = strtoupper(str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field')));
} else {
$a[$rs->Fields('lookup_table')][] = str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field'));
}
$rs->MoveNext();
}
return $a;
}
// from Edward Jaramilla, improved version - works on pg 7.4
function _old_MetaForeignKeys($table, $owner=false, $upper=false)
{ {
$sql = 'SELECT t.tgargs as args $sql = 'SELECT t.tgargs as args
FROM FROM
@ -72,24 +123,56 @@ function MetaForeignKeys($table, $owner=false, $upper=false)
$rs = $this->Execute($sql); $rs = $this->Execute($sql);
if ($rs && !$rs->EOF) { if (!$rs || $rs->EOF) return false;
$arr =& $rs->GetArray();
$arr = $rs->GetArray();
$a = array(); $a = array();
foreach($arr as $v) foreach($arr as $v) {
{
$data = explode(chr(0), $v['args']); $data = explode(chr(0), $v['args']);
if ($upper) { $size = count($data)-1; //-1 because the last node is empty
$a[strtoupper($data[2])][] = strtoupper($data[4].'='.$data[5]); for($i = 4; $i < $size; $i++) {
} else { if ($upper)
$a[$data[2]][] = $data[4].'='.$data[5]; $a[strtoupper($data[2])][] = strtoupper($data[$i].'='.$data[++$i]);
else
$a[$data[2]][] = $data[$i].'='.$data[++$i];
} }
} }
return $a; return $a;
} }
return false;
function _query($sql,$inputarr=false)
{
if (! $this->_bindInputArray) {
// We don't have native support for parameterized queries, so let's emulate it at the parent
return ADODB_postgres64::_query($sql, $inputarr);
}
$this->_errorMsg = false;
// -- added Cristiano da Cunha Duarte
if ($inputarr) {
$sqlarr = explode('?',trim($sql));
$sql = '';
$i = 1;
$last = sizeof($sqlarr)-1;
foreach($sqlarr as $v) {
if ($last < $i) $sql .= $v;
else $sql .= $v.' $'.$i;
$i++;
} }
$rez = pg_query_params($this->_connectionID,$sql, $inputarr);
} else {
$rez = pg_query($this->_connectionID,$sql);
}
// check if no data returned, then no need to create real recordset
if ($rez && pg_numfields($rez) <= 0) {
if (is_resource($this->_resultid) && get_resource_type($this->_resultid) === 'pgsql result') {
pg_freeresult($this->_resultid);
}
$this->_resultid = $rez;
return true;
}
return $rez;
}
// this is a set of functions for managing client encoding - very important if the encodings // this is a set of functions for managing client encoding - very important if the encodings
// of your database and your output target (i.e. HTML) don't match // of your database and your output target (i.e. HTML) don't match
@ -120,20 +203,6 @@ function MetaForeignKeys($table, $owner=false, $upper=false)
} else return false; } else return false;
} else return true; } else return true;
} }
// use pg_escape_string if available
function qstr($s,$magic_quotes=false)
{
if (!$magic_quotes && function_exists('pg_escape_string')) {
return "'".pg_escape_string($s)."'";
}
return parent::qstr($s,$magic_quotes);
}
function _insertid($table,$column)
{
return parent::_insertid($table,$column,false); // dont try oid
}
} }
/*-------------------------------------------------------------------------------------- /*--------------------------------------------------------------------------------------

View File

@ -467,11 +467,11 @@ class asyncservice
* != 0 reads all rows/jobs matching $id (sql-wildcards '%' and '_' can be used) * != 0 reads all rows/jobs matching $id (sql-wildcards '%' and '_' can be used)
* @param array|string $cols='*' string or array of column-names / select-expressions * @param array|string $cols='*' string or array of column-names / select-expressions
* @param int|bool $offset=False offset for a limited query or False (default) * @param int|bool $offset=False offset for a limited query or False (default)
* @param string $append string to append to the end of the query, eg. ORDER BY ... * @param string $append='ORDER BY async_next' string to append to the end of the query
* @param int $num_rows=0 number of rows to return if offset set, default 0 = use default in user prefs * @param int $num_rows=0 number of rows to return if offset set, default 0 = use default in user prefs
* @return array/boolean db-rows / jobs as array or False if no matches * @return array/boolean db-rows / jobs as array or False if no matches
*/ */
function read($id=0,$cols='*',$offset=False,$append='',$num_rows=0) function read($id=0,$cols='*',$offset=False,$append='ORDER BY async_next',$num_rows=0)
{ {
if (!is_array($id) && (strpos($id,'%') !== False || strpos($id,'_') !== False)) if (!is_array($id) && (strpos($id,'%') !== False || strpos($id,'_') !== False))
{ {

View File

@ -274,6 +274,7 @@ class egw_db
$this->$var = $db_data[$key]; $this->$var = $db_data[$key];
} }
} }
//if ($GLOBALS['egw_info']['server']['default_domain'] == 'ralfsmacbook.local') $this->query_log = '/tmp/query.log';
} }
/** /**
@ -846,7 +847,7 @@ class egw_db
if ($id === False) // function not supported if ($id === False) // function not supported
{ {
echo "<p>db::get_last_insert_id(table='$table',field='$field') not yet implemented for db-type '$this->Type' OR no insert operation before</p>\n"; echo "<p>db::get_last_insert_id(table='$table',field='$field') not yet implemented for db-type '$this->Type' OR no insert operation before</p>\n";
function_backtrace(); echo '<p>'.function_backtrace()."</p>\n";
return -1; return -1;
} }
return $id; return $id;

View File

@ -185,6 +185,11 @@ class egw_json_response
private function sendHeader() private function sendHeader()
{ {
//Send the character encoding header //Send the character encoding header
if (headers_sent($file,$line))
{
//error_log(__METHOD__."() header already send in $file on line $line!");
}
else
header('content-type: application/json; charset='.translation::charset()); header('content-type: application/json; charset='.translation::charset());
} }

View File

@ -23,7 +23,7 @@ abstract class groupdav_handler
* *
* @var integer * @var integer
*/ */
var $debug = 0; var $debug = 1;
/** /**
* eGW's charset * eGW's charset

View File

@ -54,8 +54,11 @@
function pdf() function pdf()
{ {
parent::FPDF(); parent::FPDF();
$this->AliasNbPages();
$this->SetCreator('eGroupWare '.$GLOBALS['egw_info']['server']['versions']['phpgwapi']); $this->SetCreator('eGroupWare '.$GLOBALS['egw_info']['server']['versions']['phpgwapi']);
$this->SetAuthor($GLOBALS['egw']->common->display_fullname()); $this->SetAuthor($GLOBALS['phpgw']->common->display_fullname());
$this->egw_charset = $GLOBALS['phpgw']->translation->charset();
} }
//Page footer //Page footer
@ -68,5 +71,48 @@
//Page number //Page number
$this->Cell(0,10,lang('Page').' '.$this->PageNo().'/{nb}',0,0,'C'); $this->Cell(0,10,lang('Page').' '.$this->PageNo().'/{nb}',0,0,'C');
} }
/**
* Reimplement FPDF::Cell to do charset conversation
*
* stock FPDF only understands iso-8859-1, so we convert everything to that for now
*/
function Cell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')
{
$txt = $GLOBALS['phpgw']->translation->convert($txt,$this->egw_charset,'iso-8859-1');
return FPDF::Cell($w,$h,$txt,$border,$ln,$align,$fill,$link);
}
/**
* Reimplement FPDF::Text to do charset conversation
*
* stock FPDF only understands iso-8859-1, so we convert everything to that for now
*/
function Text($x,$y,$txt)
{
$txt = $GLOBALS['phpgw']->translation->convert($txt,$this->egw_charset,'iso-8859-1');
return FPDF::Text($x,$y,$txt);
}
/**
* Sets dashed line mode, to reset to continues call withour params
*
* @param float $black drawn part (in user-units)
* @param float $white empty part
*/
function SetDash($black=false, $white=false)
{
if($black && $white)
{
$s=sprintf('[%.3f %.3f] 0 d', $black*$this->k, $white*$this->k);
}
else
{
$s='[] 0 d';
}
$this->_out($s);
}
} }
?> ?>