/** * * Constructor. * * If there is an error on instantiation, $this->error will be * populated with the PEAR_Error. * * @access public * * @param object &$db A PEAR DB/MDB2 object. * * @param string $table The table name to connect to in the database. * * @param mixed $create The automatic table creation mode to pursue: * - boolean false to not attempt creation * - 'safe' to create the table only if it does not exist * - 'drop' to drop any existing table with the same name and re-create it * - 'verify' to check whether the table exists, whether all the columns * exist, whether the columns have the right type, and whether the indexes * exist and have the right type * - 'alter' does the same as 'safe' if the table does not exist; if it * exists, a verification for columns existence, the column types, the * indexes existence, and the indexes types will be performed and the * table schema will be modified if needed * * @return object DB_Table * */ function DB_Table(&$db, $table, $create = false) { // is the first argument a DB/MDB2 object? $this->backend = null; if (is_subclass_of($db, 'db_common')) { $this->backend = 'db'; } elseif (is_subclass_of($db, 'mdb2_driver_common')) { $this->backend = 'mdb2'; } if (is_null($this->backend)) { $this->error =& DB_Table::throwError(DB_TABLE_ERR_NOT_DB_OBJECT); return; } // array with column definition may not be empty if (!isset($this->col) || is_null($this->col) || is_array($this->col) && count($this->col) === 0) { $this->error =& DB_Table::throwError(DB_TABLE_ERR_NO_COLS); return; } // set the class properties $this->db =& $db; $this->table = $table; // is the RDBMS supported? list($phptype, $dbsyntax) = DB_Table::getPHPTypeAndDBSyntax($db); if (!DB_Table::supported($phptype, $dbsyntax)) { $this->error =& DB_Table::throwError(DB_TABLE_ERR_PHPTYPE, "({$db->phptype})"); return; } // load MDB2_Extended module if ($this->backend == 'mdb2') { $this->db->loadModule('Extended', null, false); } // should we attempt table creation? if ($create) { if ($this->backend == 'mdb2') { $this->db->loadModule('Manager'); } // check whether the chosen mode is supported list($phptype, ) = DB_Table::getPHPTypeAndDBSyntax($this->db); $mode_supported = DB_Table::modeSupported($create, $phptype); if (PEAR::isError($mode_supported)) { return $mode_supported; } if (!$mode_supported) { return $this->throwError(DB_TABLE_ERR_CREATE_PHPTYPE, "('{$create}', '{$phptype}')"); } include_once 'DB/Table/Manager.php'; switch ($create) { case 'alter': $result = $this->alter(); break; case 'drop': case 'safe': $result = $this->create($create); break; case 'verify': $result = $this->verify(); break; } if (PEAR::isError($result)) { // problem creating/altering/verifing the table $this->error =& $result; return; } } }
/** * * Return all indexes for a table. * * @access private * * @param object &$db A PEAR DB/MDB2 object. * * @param string $table The table name. * * @return mixed Array with all indexes or a PEAR_Error when an error * occured. * */ function _getIndexes(&$db, $table) { if (is_subclass_of($db, 'db_common')) { $backend = 'db'; // workaround for missing index and constraint information methods // in PEAR::DB ==> use adopted code from MDB2's driver classes list($phptype, ) = DB_Table::getPHPTypeAndDBSyntax($db); require_once 'DB/Table/Manager/' . $phptype . '.php'; $classname = 'DB_Table_Manager_' . $phptype; $dbtm =& new $classname(); $dbtm->_db =& $db; // pass database instance to the 'workaround' class $manager =& $dbtm; $reverse =& $dbtm; } elseif (is_subclass_of($db, 'mdb2_driver_common')) { $backend = 'mdb2'; $manager =& $db->manager; $reverse =& $db->reverse; } $indexes = array('normal' => array(), 'primary' => array(), 'unique' => array()); // save user defined 'idxname_format' option (MDB2 only) if ($backend == 'mdb2') { $idxname_format = $db->getOption('idxname_format'); $db->setOption('idxname_format', '%s'); } // get table constraints $table_indexes_tmp = $manager->listTableConstraints($table); if (PEAR::isError($table_indexes_tmp)) { // restore user defined 'idxname_format' option (MDB2 only) if ($backend == 'mdb2') { $db->setOption('idxname_format', $idxname_format); } return $table_indexes_tmp; } // get fields of table constraints foreach ($table_indexes_tmp as $table_idx_tmp) { $index_fields = $reverse->getTableConstraintDefinition($table, $table_idx_tmp); if (PEAR::isError($index_fields)) { // restore user defined 'idxname_format' option (MDB2 only) if ($backend == 'mdb2') { $db->setOption('idxname_format', $idxname_format); } return $index_fields; } $index_type = current(array_keys($index_fields)); $indexes[$index_type][$table_idx_tmp] = array_keys($index_fields['fields']); } // get table indexes $table_indexes_tmp = $manager->listTableIndexes($table); if (PEAR::isError($table_indexes_tmp)) { // restore user defined 'idxname_format' option (MDB2 only) if ($backend == 'mdb2') { $db->setOption('idxname_format', $idxname_format); } return $table_indexes_tmp; } // get fields of table indexes foreach ($table_indexes_tmp as $table_idx_tmp) { $index_fields = $reverse->getTableIndexDefinition($table, $table_idx_tmp); if (PEAR::isError($index_fields)) { // restore user defined 'idxname_format' option (MDB2 only) if ($backend == 'mdb2') { $db->setOption('idxname_format', $idxname_format); } return $index_fields; } $indexes['normal'][$table_idx_tmp] = array_keys($index_fields['fields']); } // restore user defined 'idxname_format' option (MDB2 only) if ($backend == 'mdb2') { $db->setOption('idxname_format', $idxname_format); } return $indexes; }