Exemplo n.º 1
0
 /**
  * 
  * Verify whether the table and columns exist, whether the columns
  * have the right type and whether the indexes exist.
  * 
  * @static
  * 
  * @access public
  * 
  * @param object &$db A PEAR DB/MDB2 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 true if the verification was successful, and a
  * PEAR_Error if verification failed.
  * 
  */
 function verify(&$db, $table, $column_set, $index_set)
 {
     if (is_subclass_of($db, 'db_common')) {
         $backend = 'db';
         $reverse =& $db;
         $table_info_mode = DB_TABLEINFO_FULL;
         $table_info_error = DB_ERROR_NEED_MORE_DATA;
     } elseif (is_subclass_of($db, 'mdb2_driver_common')) {
         $backend = 'mdb2';
         $reverse =& $this->db->loadModule('Reverse');
         $table_info_mode = MDB2_TABLEINFO_FULL;
         $table_info_error = MDB2_ERROR_NEED_MORE_DATA;
     }
     $phptype = $db->phptype;
     // check #1: does the table exist?
     // check the table name
     $name_check = DB_Table_Manager::_validateTableName($table);
     if (PEAR::isError($name_check)) {
         return $name_check;
     }
     // get table info
     $tableInfo = $reverse->tableInfo($table, $table_info_mode);
     if (PEAR::isError($tableInfo)) {
         if ($tableInfo->getCode() == $table_info_error) {
             return DB_Table::throwError(DB_TABLE_ERR_VER_TABLE_MISSING, "(table='{$table}')");
         }
         return $tableInfo;
     }
     $tableInfoOrder = array_change_key_case($tableInfo['order'], CASE_LOWER);
     if (is_null($column_set)) {
         $column_set = array();
     }
     foreach ($column_set as $colname => $val) {
         $colname = strtolower(trim($colname));
         // check the column name
         $name_check = DB_Table_Manager::_validateColumnName($colname);
         if (PEAR::isError($name_check)) {
             return $name_check;
         }
         // check #2: do all columns exist?
         $column_exists = DB_Table_Manager::_columnExists($colname, $tableInfoOrder, 'verify');
         if (PEAR::isError($column_exists)) {
             return $column_exists;
         }
         // check #3: do all columns have the right type?
         // check whether the column type is a known type
         $type_check = DB_Table_Manager::_validateColumnType($phptype, $val['type']);
         if (PEAR::isError($type_check)) {
             return $type_check;
         }
         // check whether the column has the right type
         $type_check = DB_Table_Manager::_checkColumnType($phptype, $colname, $val['type'], $tableInfoOrder, $tableInfo, 'verify');
         if (PEAR::isError($type_check)) {
             return $type_check;
         }
     }
     // check #4: do all indexes exist?
     $table_indexes = DB_Table_Manager::getIndexes($db, $table);
     if (PEAR::isError($table_indexes)) {
         return $table_indexes;
     }
     if (is_null($index_set)) {
         $index_set = array();
     }
     foreach ($index_set as $idxname => $val) {
         list($type, $cols) = DB_Table_Manager::_getIndexTypeAndColumns($val, $idxname);
         $newIdxName = '';
         // check the index definition
         $index_check = DB_Table_Manager::_validateIndexName($idxname, $table, $phptype, $type, $cols, $column_set, $newIdxName);
         if (PEAR::isError($index_check)) {
             return $index_check;
         }
         // check whether the index has the right type and has all
         // specified columns
         $index_check = DB_Table_Manager::_checkIndex($idxname, $newIdxName, $type, $cols, $table_indexes, 'verify');
         if (PEAR::isError($index_check)) {
             return $index_check;
         }
     }
     return true;
 }