Spaces:
Sleeping
Sleeping
File size: 20,306 Bytes
07c3cdd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | <?php
/*
* $Id: PgSQLTableInfo.php,v 1.31 2006/01/17 19:44:40 hlellelid Exp $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://creole.phpdb.org>.
*/
require_once 'creole/metadata/TableInfo.php';
/**
* PgSQL implementation of TableInfo.
*
* See this Python code by David M. Cook for some good reference on Pgsql metadata
* functions:
* @link http://www.sandpyt.org/pipermail/sandpyt/2003-March/000008.html
*
* Here's some more information from postgresql:
* @link http://developer.postgresql.org/docs/pgsql/src/backend/catalog/information_schema.sql
*
* @todo -c Eventually move to supporting only Postgres >= 7.4, which has the information_schema
*
* @author Hans Lellelid <hans@xmpl.org>
* @version $Revision: 1.31 $
* @package creole.drivers.pgsql.metadata
*/
class PgSQLTableInfo extends TableInfo {
/**
* Database Version.
* @var String
*/
private $version;
/**
* Table OID
* @var Integer
*/
private $oid;
/**
* @param string $table The table name.
* @param string $database The database name.
* @param resource $dblink The db connection resource.
*/
function __construct(DatabaseInfo $database, $name, $version, $intOID) {
parent::__construct ($database, $name);
$this->version = $version;
$this->oid = $intOID;
} // function __construct(DatabaseInfo $database, $name) {
/** Load the columns for this table */
protected function initColumns () {
// Include dependencies
include_once ('creole/metadata/ColumnInfo.php');
include_once ('creole/drivers/pgsql/PgSQLTypes.php');
// Get the columns, types, etc.
// Based on code from pgAdmin3 (http://www.pgadmin.org/)
$realdocuroot = str_replace( '\\', '/', $_SERVER['DOCUMENT_ROOT'] );
$docuroot = explode( '/', $realdocuroot );
array_pop( $docuroot );
$pathhome = implode( '/', $docuroot ) . '/';
array_pop( $docuroot );
$pathTrunk = implode( '/', $docuroot ) . '/';
require_once($pathTrunk.'gulliver/system/class.inputfilter.php');
$filter = new InputFilter();
$this->oid = $filter->validateInput($this->oid, 'int');
$query = "SELECT
att.attname,
att.atttypmod,
att.atthasdef,
att.attnotnull,
def.adsrc,
CASE WHEN att.attndims > 0 THEN 1 ELSE 0 END AS isarray,
CASE
WHEN ty.typname = 'bpchar'
THEN 'char'
WHEN ty.typname = '_bpchar'
THEN '_char'
ELSE
ty.typname
END AS typname,
ty.typtype
FROM pg_attribute att
JOIN pg_type ty ON ty.oid=att.atttypid
LEFT OUTER JOIN pg_attrdef def ON adrelid=att.attrelid AND adnum=att.attnum
WHERE att.attrelid = $1 AND att.attnum > 0
AND att.attisdropped IS FALSE
ORDER BY att.attnum";
$result = $this->executePgQuery($this->conn->getResource(), $query, $this->oid);
if (!$result) {
throw new SQLException("Could not list fields for table: " . $this->name, pg_last_error($this->conn->getResource()));
}
while($row = pg_fetch_assoc($result)) {
$size = null;
$precision = null;
$scale = null;
// Check to ensure that this column isn't an array data type
if (((int) $row['isarray']) === 1)
{
throw new SQLException (sprintf ("Array datatypes are not currently supported [%s.%s]", $this->name, $row['attname']));
} // if (((int) $row['isarray']) === 1)
$name = $row['attname'];
// If they type is a domain, Process it
if (strtolower ($row['typtype']) == 'd')
{
$arrDomain = $this->processDomain ($row['typname']);
$type = $arrDomain['type'];
$size = $arrDomain['length'];
$precision = $size;
$scale = $arrDomain['scale'];
$boolHasDefault = (strlen (trim ($row['atthasdef'])) > 0) ? $row['atthasdef'] : $arrDomain['hasdefault'];
$default = (strlen (trim ($row['adsrc'])) > 0) ? $row['adsrc'] : $arrDomain['default'];
$is_nullable = (strlen (trim ($row['attnotnull'])) > 0) ? $row['attnotnull'] : $arrDomain['notnull'];
$is_nullable = (($is_nullable == 't') ? false : true);
} // if (strtolower ($row['typtype']) == 'd')
else
{
$type = $row['typname'];
$arrLengthPrecision = $this->processLengthScale ($row['atttypmod'], $type);
$size = $arrLengthPrecision['length'];
$precision = $size;
$scale = $arrLengthPrecision['scale'];
$boolHasDefault = $row['atthasdef'];
$default = $row['adsrc'];
$is_nullable = (($row['attnotnull'] == 't') ? false : true);
} // else (strtolower ($row['typtype']) == 'd')
$autoincrement = null;
// if column has a default
if (($boolHasDefault == 't') && (strlen (trim ($default)) > 0))
{
if (!preg_match('/^nextval\(/', $default))
{
$strDefault= preg_replace ('/::[\W\D]*/', '', $default);
$default = str_replace ("'", '', $strDefault);
} // if (!preg_match('/^nextval\(/', $row['atthasdef']))
else
{
$autoincrement = true;
$default = null;
} // else
} // if (($boolHasDefault == 't') && (strlen (trim ($default)) > 0))
else
{
$default = null;
} // else (($boolHasDefault == 't') && (strlen (trim ($default)) > 0))
$this->columns[$name] = new ColumnInfo($this, $name, PgSQLTypes::getType($type), $type, $size, $precision, $scale, $is_nullable, $default, $autoincrement);
}
$this->colsLoaded = true;
} // protected function initColumns ()
private function processLengthScale ($intTypmod, $strName)
{
// Define the return array
$arrRetVal = array ('length'=>null, 'scale'=>null);
// Some datatypes don't have a Typmod
if ($intTypmod == -1)
{
return $arrRetVal;
} // if ($intTypmod == -1)
// Numeric Datatype?
if ($strName == PgSQLTypes::getNativeType (CreoleTypes::NUMERIC))
{
$intLen = ($intTypmod - 4) >> 16;
$intPrec = ($intTypmod - 4) & 0xffff;
$intLen = sprintf ("%ld", $intLen);
if ($intPrec)
{
$intPrec = sprintf ("%ld", $intPrec);
} // if ($intPrec)
$arrRetVal['length'] = $intLen;
$arrRetVal['scale'] = $intPrec;
} // if ($strName == PgSQLTypes::getNativeType (CreoleTypes::NUMERIC))
elseif ($strName == PgSQLTypes::getNativeType (CreoleTypes::TIME) || $strName == 'timetz'
|| $strName == PgSQLTypes::getNativeType (CreoleTypes::TIMESTAMP) || $strName == 'timestamptz'
|| $strName == 'interval' || $strName == 'bit')
{
$arrRetVal['length'] = sprintf ("%ld", $intTypmod);
} // elseif (TIME, TIMESTAMP, INTERVAL, BIT)
else
{
$arrRetVal['length'] = sprintf ("%ld", ($intTypmod - 4));
} // else
return $arrRetVal;
} // private function processLengthScale ($intTypmod, $strName)
private function processDomain ($strDomain)
{
if (strlen (trim ($strDomain)) < 1)
{
throw new SQLException ("Invalid domain name [" . $strDomain . "]");
} // if (strlen (trim ($strDomain)) < 1)
$realdocuroot = str_replace( '\\', '/', $_SERVER['DOCUMENT_ROOT'] );
$docuroot = explode( '/', $realdocuroot );
array_pop( $docuroot );
$pathhome = implode( '/', $docuroot ) . '/';
array_pop( $docuroot );
$pathTrunk = implode( '/', $docuroot ) . '/';
require_once($pathTrunk.'gulliver/system/class.inputfilter.php');
$filter = new InputFilter();
$strDomain = $filter->validateInput($strDomain);
$query = "SELECT
d.typname as domname,
b.typname as basetype,
d.typlen,
d.typtypmod,
d.typnotnull,
d.typdefault
FROM pg_type d
INNER JOIN pg_type b ON b.oid = CASE WHEN d.typndims > 0 then d.typelem ELSE d.typbasetype END
WHERE
d.typtype = 'd'
AND d.typname = '$1'
ORDER BY d.typname";
$result = $this->executePgQuery($this->conn->getResource(), $query, $strDomain);
if (!$result) {
throw new SQLException("Query for domain [" . $strDomain . "] failed.", pg_last_error($this->conn->getResource()));
}
$row = pg_fetch_assoc ($result);
if (!$row)
{
throw new SQLException ("Domain [" . $strDomain . "] not found.");
} // if (!$row)
$arrDomain = array ();
$arrDomain['type'] = $row['basetype'];
$arrLengthPrecision = $this->processLengthScale ($row['typtypmod'], $row['basetype']);
$arrDomain['length'] = $arrLengthPrecision['length'];
$arrDomain['scale'] = $arrLengthPrecision['scale'];
$arrDomain['notnull'] = $row['typnotnull'];
$arrDomain['default'] = $row['typdefault'];
$arrDomain['hasdefault'] = (strlen (trim ($row['typdefault'])) > 0) ? 't' : 'f';
pg_free_result ($result);
return $arrDomain;
} // private function processDomain ($strDomain)
/** Load foreign keys for this table. */
protected function initForeignKeys()
{
include_once 'creole/metadata/ForeignKeyInfo.php';
$realdocuroot = str_replace( '\\', '/', $_SERVER['DOCUMENT_ROOT'] );
$docuroot = explode( '/', $realdocuroot );
array_pop( $docuroot );
$pathhome = implode( '/', $docuroot ) . '/';
array_pop( $docuroot );
$pathTrunk = implode( '/', $docuroot ) . '/';
require_once($pathTrunk.'gulliver/system/class.inputfilter.php');
$filter = new InputFilter();
$this->oid = $filter->validateInput($this->oid, 'int');
$query = "SELECT
conname,
confupdtype,
confdeltype,
cl.relname as fktab,
a2.attname as fkcol,
cr.relname as reftab,
a1.attname as refcol
FROM pg_constraint ct
JOIN pg_class cl ON cl.oid=conrelid
JOIN pg_class cr ON cr.oid=confrelid
LEFT JOIN pg_catalog.pg_attribute a1 ON a1.attrelid = ct.confrelid
LEFT JOIN pg_catalog.pg_attribute a2 ON a2.attrelid = ct.conrelid
WHERE
contype='f'
AND conrelid = $1
AND a2.attnum = ct.conkey[1]
AND a1.attnum = ct.confkey[1]
ORDER BY conname";
$result = $this->executePgQuery($this->conn->getResource(), $query, $this->oid);
if (!$result) {
throw new SQLException("Could not list foreign keys for table: " . $this->name, pg_last_error($this->conn->getResource()));
}
while($row = pg_fetch_assoc($result)) {
$name = $row['conname'];
$local_table = $row['fktab'];
$local_column = $row['fkcol'];
$foreign_table = $row['reftab'];
$foreign_column = $row['refcol'];
// On Update
switch ($row['confupdtype']) {
case 'c':
$onupdate = ForeignKeyInfo::CASCADE; break;
case 'd':
$onupdate = ForeignKeyInfo::SETDEFAULT; break;
case 'n':
$onupdate = ForeignKeyInfo::SETNULL; break;
case 'r':
$onupdate = ForeignKeyInfo::RESTRICT; break;
default:
case 'a':
//NOACTION is the postgresql default
$onupdate = ForeignKeyInfo::NONE; break;
}
// On Delete
switch ($row['confdeltype']) {
case 'c':
$ondelete = ForeignKeyInfo::CASCADE; break;
case 'd':
$ondelete = ForeignKeyInfo::SETDEFAULT; break;
case 'n':
$ondelete = ForeignKeyInfo::SETNULL; break;
case 'r':
$ondelete = ForeignKeyInfo::RESTRICT; break;
default:
case 'a':
//NOACTION is the postgresql default
$ondelete = ForeignKeyInfo::NONE; break;
}
$foreignTable = $this->database->getTable($foreign_table);
$foreignColumn = $foreignTable->getColumn($foreign_column);
$localTable = $this->database->getTable($local_table);
$localColumn = $localTable->getColumn($local_column);
if (!isset($this->foreignKeys[$name])) {
$this->foreignKeys[$name] = new ForeignKeyInfo($name);
}
$this->foreignKeys[$name]->addReference($localColumn, $foreignColumn, $ondelete, $onupdate);
}
$this->fksLoaded = true;
}
/** Load indexes for this table */
protected function initIndexes()
{
include_once 'creole/metadata/IndexInfo.php';
// columns have to be loaded first
if (!$this->colsLoaded) $this->initColumns();
$realdocuroot = str_replace( '\\', '/', $_SERVER['DOCUMENT_ROOT'] );
$docuroot = explode( '/', $realdocuroot );
array_pop( $docuroot );
$pathhome = implode( '/', $docuroot ) . '/';
array_pop( $docuroot );
$pathTrunk = implode( '/', $docuroot ) . '/';
require_once($pathTrunk.'gulliver/system/class.inputfilter.php');
$filter = new InputFilter();
$this->oid = $filter->validateInput($this->oid, 'int');
$query = "SELECT
DISTINCT ON(cls.relname)
cls.relname as idxname,
indkey,
indisunique
FROM pg_index idx
JOIN pg_class cls ON cls.oid=indexrelid
WHERE indrelid = $1 AND NOT indisprimary
ORDER BY cls.relname";
$result = $this->executePgQuery($this->conn->getResource(), $query, $this->oid);
if (!$result) {
throw new SQLException("Could not list indexes keys for table: " . $this->name, pg_last_error($this->conn->getResource()));
}
$realdocuroot = str_replace( '\\', '/', $_SERVER['DOCUMENT_ROOT'] );
$docuroot = explode( '/', $realdocuroot );
array_pop( $docuroot );
$pathhome = implode( '/', $docuroot ) . '/';
array_pop( $docuroot );
$pathTrunk = implode( '/', $docuroot ) . '/';
require_once($pathTrunk.'gulliver/system/class.inputfilter.php');
$filter = new InputFilter();
$this->oid = $filter->validateInput($this->oid);
while($row = pg_fetch_assoc($result)) {
$name = $row["idxname"];
$unique = ($row["indisunique"] == 't') ? true : false;
if (!isset($this->indexes[$name])) {
$this->indexes[$name] = new IndexInfo($name, $unique);
}
$arrColumns = explode (' ', $row['indkey']);
foreach ($arrColumns as $intColNum)
{
$intColNum = $filter->validateInput($intColNum, 'int');
$query = "SELECT a.attname
FROM pg_catalog.pg_class c JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
WHERE c.oid = '$1' AND a.attnum = $2 AND NOT a.attisdropped
ORDER BY a.attnum";
$result2 = $this->executePgQuery($this->conn->getResource(), $query, array($this->oid, $intColNum));
if (!$result2)
{
throw new SQLException("Could not list indexes keys for table: " . $this->name, pg_last_error($this->conn->getResource()));
}
$row2 = pg_fetch_assoc($result2);
$this->indexes[$name]->addColumn($this->columns[ $row2['attname'] ]);
} // foreach ($arrColumns as $intColNum)
}
$this->indexesLoaded = true;
}
/** Loads the primary keys for this table. */
protected function initPrimaryKey() {
include_once 'creole/metadata/PrimaryKeyInfo.php';
// columns have to be loaded first
if (!$this->colsLoaded) $this->initColumns();
// Primary Keys
$realdocuroot = str_replace( '\\', '/', $_SERVER['DOCUMENT_ROOT'] );
$docuroot = explode( '/', $realdocuroot );
array_pop( $docuroot );
$pathhome = implode( '/', $docuroot ) . '/';
array_pop( $docuroot );
$pathTrunk = implode( '/', $docuroot ) . '/';
require_once($pathTrunk.'gulliver/system/class.inputfilter.php');
$filter = new InputFilter();
$this->oid = $filter->validateInput($this->oid);
$query = "SELECT
DISTINCT ON(cls.relname)
cls.relname as idxname,
indkey,
indisunique
FROM pg_index idx
JOIN pg_class cls ON cls.oid=indexrelid
WHERE indrelid = $1 AND indisprimary
ORDER BY cls.relname";
$result = $this->executePgQuery($this->conn->getResource(), $query, $this->oid);
if (!$result) {
throw new SQLException("Could not list primary keys for table: " . $this->name, pg_last_error($this->conn->getResource()));
}
// Loop through the returned results, grouping the same key_name together
// adding each column for that key.
$realdocuroot = str_replace( '\\', '/', $_SERVER['DOCUMENT_ROOT'] );
$docuroot = explode( '/', $realdocuroot );
array_pop( $docuroot );
$pathhome = implode( '/', $docuroot ) . '/';
array_pop( $docuroot );
$pathTrunk = implode( '/', $docuroot ) . '/';
require_once($pathTrunk.'gulliver/system/class.inputfilter.php');
$filter = new InputFilter();
$this->oid = $filter->validateInput($this->oid);
while($row = pg_fetch_assoc($result)) {
$arrColumns = explode (' ', $row['indkey']);
foreach ($arrColumns as $intColNum)
{
$intColNum = $filter->validateInput($intColNum, 'int');
$query = "SELECT a.attname
FROM pg_catalog.pg_class c JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
WHERE c.oid = '$1' AND a.attnum = $2 AND NOT a.attisdropped
ORDER BY a.attnum";
$result2 = $this->executePgQuery($this->conn->getResource(), $query, array($this->oid, $intColNum));
if (!$result2)
{
throw new SQLException("Could not list indexes keys for table: " . $this->name, pg_last_error($this->conn->getResource()));
}
$row2 = pg_fetch_assoc($result2);
if (!isset($this->primaryKey)) {
$this->primaryKey = new PrimaryKeyInfo($row2['attname']);
}
$this->primaryKey->addColumn($this->columns[ $row2['attname'] ]);
} // foreach ($arrColumns as $intColNum)
}
$this->pkLoaded = true;
}
function executePgQuery($conn, $var, $values = array()){
$result = pg_query_params($conn, $var, $values);
return $result;
}
} |