Source for file Manager.php
Documentation is available at Manager.php
require_once 'DB/Table.php';
* Creates tables from DB_Table definitions.
* DB_Table_Manager provides database automated table creation
* facilities (and eventually table alteration as well).
* $Id: Manager.php,v 1.4 2004/07/10 15:08:33 pmjones Exp $
* @author Paul M. Jones <pmjones@ciaweb.net>
* Create the table based on DB_Table column and index arrays.
* @param object &$db A PEAR DB object.
* @param string $table The table name to connect to in the database.
* @param mixed $column_set A DB_Table $this->col array.
* @param mixed $index_set A DB_Table $this->idx array.
* @return mixed Boolean false if there was no attempt to create the
* table, boolean true if the attempt succeeded, and a PEAR_Error if
function create(&$db, $table, $column_set, $index_set)
// -------------------------------------------------------------
// validate each column mapping and build the individual
// definitions, and note column indexes as we go.
foreach ($column_set as $colname => $val) {
$colname = trim($colname);
// column name cannot be a reserved keyword
$GLOBALS['_DB_TABLE']['reserved']
$type = (isset ($val['type'])) ? $val['type'] : null;
$size = (isset ($val['size'])) ? $val['size'] : null;
$scope = (isset ($val['scope'])) ? $val['scope'] : null;
$require = (isset ($val['require'])) ? $val['require'] : null;
$default = (isset ($val['default'])) ? $val['default'] : null;
// get the declaration string
$scope, $require, $default);
if (PEAR ::isError ($result)) {
$result->userinfo .= " ('$colname')";
// add the declaration to the array of all columns
$column[] = " $colname $result";
// -------------------------------------------------------------
foreach ($index_set as $idxname => $val) {
// shorthand for index names: colname => index_type
// normal: index_name => array('type' => ..., 'cols' => ...)
$type = (isset ($val['type'])) ? $val['type'] : 'normal';
$cols = (isset ($val['cols'])) ? $val['cols'] : null;
// index name cannot be a reserved keyword
$GLOBALS['_DB_TABLE']['reserved']
// are there any columns for the index?
// are there any CLOB columns, or any columns that are not
foreach ($cols as $colname) {
if (! in_array($colname, $valid_cols)) {
" '$idxname' ('$colname')"
if ($column_set[$colname]['type'] == 'clob') {
" '$idxname' ('$colname')"
// string of column names
// we prefix all index names with the table name,
// and suffix all index names with '_index'. this
// is to soothe PostgreSQL, which demands that index
// names not collide, even when they indexes are on
$newIdxName = $table . '_' . $idxname . '_index';
$index[] = " CREATE UNIQUE INDEX $newIdxName ON $table ($colstring)";
} elseif ($type == 'normal') {
$index[] = " CREATE INDEX $newIdxName ON $table ($colstring)";
// -------------------------------------------------------------
// now for the real action: create the table and indexes!
// build the CREATE TABLE command
$cmd = " CREATE TABLE $table (\n\t";
// attempt to create the table
$result = $db->query ($cmd);
if (PEAR ::isError ($result)) {
// attempt to create the indexes
foreach ($index as $cmd) {
$result = $db->query ($cmd);
if (PEAR ::isError ($result)) {
* Get the column declaration string for a DB_Table column.
* @param string $phptype The DB phptype key.
* @param string $coltype The DB_Table column type.
* @param int $size The size for the column (needed for string and
* @param int $scope The scope for the column (needed for decimal).
* @param bool $require True if the column should be NOT NULL, false
* @param string $default The SQL calculation for a default value.
* @return string|object A declaration string on success, or a
function getDeclare($phptype, $coltype, $size = null , $scope = null ,
$require = null , $default = null )
// validate char and varchar: does it have a size?
if (($coltype == 'char' || $coltype == 'varchar') &&
($size < 1 || $size > 255 ) ) {
// validate decimal: does it have a size and scope?
if ($coltype == 'decimal' &&
($size < 1 || $size > 255 || $scope < 0 || $scope > $size)) {
" (size='$size' scope='$scope')"
// map of column types and declarations for this RDBMS
$map = $GLOBALS['_DB_TABLE']['type'][$phptype];
// is it a recognized column type?
$declare = $map[$coltype] . " ($size)";
$declare = $map[$coltype] . " ($size,$scope)";
$declare = $map[$coltype];
// set the "NULL"/"NOT NULL" portion
$declare .= ($require) ? ' NOT NULL' : ' NULL';
// set the "DEFAULT" portion
$declare .= ($default) ? " DEFAULT $default" : '';
* List of all reserved words for all supported databases. Yes, this is a
if (! isset ($GLOBALS['_DB_TABLE']['reserved'])) {
$GLOBALS['_DB_TABLE']['reserved'] = array (
Documentation generated on Mon, 11 Mar 2019 13:52:54 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|