/** * * Return the definition array for a column. * * @access private * * @param string $backend The name of the backend ('db' or 'mdb2'). * * @param string $phptype The DB/MDB2 phptype key. * * @param mixed $column A single DB_Table column definition array. * * @return mixed|object Declaration string (DB), declaration array (MDB2) or a * PEAR_Error with a description about the invalidity, otherwise. * */ function _getColumnDefinition($backend, $phptype, $column) { static $max_scope; // prepare variables $type = isset($column['type']) ? $column['type'] : null; $size = isset($column['size']) ? $column['size'] : null; $scope = isset($column['scope']) ? $column['scope'] : null; $require = isset($column['require']) ? $column['require'] : null; $default = isset($column['default']) ? $column['default'] : null; if ($backend == 'db') { return DB_Table_Manager::getDeclare($phptype, $type, $size, $scope, $require, $default); } else { return DB_Table_Manager::getDeclareMDB2($type, $size, $scope, $require, $default, $max_scope); } }
/** * * Create the table based on DB_Table column and index arrays. * * @static * * @access public * * @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 * the attempt failed. * */ function create(&$db, $table, $column_set, $index_set) { // columns to be created $column = array(); // indexes to be created $index = array(); // is the table name too long? if (strlen($table) > 30) { return DB_Table::throwError(DB_TABLE_ERR_TABLE_STRLEN, " ('{$table}')"); } // ------------------------------------------------------------- // // 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 $reserved = in_array(strtoupper($colname), $GLOBALS['_DB_TABLE']['reserved']); if ($reserved) { return DB_Table::throwError(DB_TABLE_ERR_DECLARE_COLNAME, " ('{$colname}')"); } // column must be no longer than 30 chars if (strlen($colname) > 30) { return DB_Table::throwError(DB_TABLE_ERR_DECLARE_STRLEN, "('{$colname}')"); } // prepare variables $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 $result = DB_Table_Manager::getDeclare($db->phptype, $type, $size, $scope, $require, $default); // did it work? if (PEAR::isError($result)) { $result->userinfo .= " ('{$colname}')"; return $result; } // add the declaration to the array of all columns $column[] = "{$colname} {$result}"; } // ------------------------------------------------------------- // // validate the indexes. // foreach ($index_set as $idxname => $val) { if (is_string($val)) { // shorthand for index names: colname => index_type $type = trim($val); $cols = trim($idxname); } elseif (is_array($val)) { // 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 $reserved = in_array(strtoupper($idxname), $GLOBALS['_DB_TABLE']['reserved']); if ($reserved) { return DB_Table::throwError(DB_TABLE_ERR_DECLARE_IDXNAME, "('{$idxname}')"); } // are there any columns for the index? if (!$cols) { return DB_Table::throwError(DB_TABLE_ERR_IDX_NO_COLS, "('{$idxname}')"); } // are there any CLOB columns, or any columns that are not // in the schema? settype($cols, 'array'); $valid_cols = array_keys($column_set); foreach ($cols as $colname) { if (!in_array($colname, $valid_cols)) { return DB_Table::throwError(DB_TABLE_ERR_IDX_COL_UNDEF, "'{$idxname}' ('{$colname}')"); } if ($column_set[$colname]['type'] == 'clob') { return DB_Table::throwError(DB_TABLE_ERR_IDX_COL_CLOB, "'{$idxname}' ('{$colname}')"); } } // string of column names $colstring = implode(', ', $cols); // we prefix all index names with the table name, // and suffix all index names with '_idx'. this // is to soothe PostgreSQL, which demands that index // names not collide, even when they indexes are on // different tables. $newIdxName = $table . '_' . $idxname . '_idx'; // now check the length; must be under 30 chars to // soothe Oracle. if (strlen($newIdxName) > 30) { return DB_Table::throwError(DB_TABLE_ERR_IDX_STRLEN, "'{$idxname}' ('{$newIdxName}')"); } // create index entry if ($type == 'unique') { $index[] = "CREATE UNIQUE INDEX {$newIdxName} ON {$table} ({$colstring})"; } elseif ($type == 'normal') { $index[] = "CREATE INDEX {$newIdxName} ON {$table} ({$colstring})"; } else { return DB_Table::throwError(DB_TABLE_ERR_IDX_TYPE, "'{$idxname}' ('{$type}')"); } } // ------------------------------------------------------------- // // now for the real action: create the table and indexes! // // build the CREATE TABLE command $cmd = "CREATE TABLE {$table} (\n\t"; $cmd .= implode(",\n\t", $column); $cmd .= "\n)"; // attempt to create the table $result = $db->query($cmd); if (PEAR::isError($result)) { return $result; } // attempt to create the indexes foreach ($index as $cmd) { $result = $db->query($cmd); if (PEAR::isError($result)) { return $result; } } // we're done! return true; }