View make crash

Issue #1 resolved
FranciscoF created an issue

When you have a View in you DB, the dumpDB crash.

Comments (1)

  1. FranciscoF reporter

    With this changes it's working fine for me.

        /**
         * Create table dump
         * @param $tableName
         * @return mixed
         */
        private function dumpTable($tableName)
        {
            $db = Yii::app()->db;
            $pdo = $db->getPdoInstance();
    
            echo PHP_EOL."--\n-- Structure for table `$tableName`\n--".PHP_EOL;
            echo PHP_EOL.'DROP TABLE IF EXISTS '.$db->quoteTableName($tableName).';'.PHP_EOL.PHP_EOL;
    
            $q = $db->createCommand('SHOW CREATE TABLE '.$db->quoteTableName($tableName).';')->queryRow();
    
                    /***/
                    /***/$curdb = explode('=', $db->connectionString);
                    /***/$dbname = $curdb[2];
                    /***/$result = $db->createCommand("SHOW FULL TABLES IN ".$dbname." WHERE TABLE_TYPE LIKE 'VIEW';")->queryAll();
                    /***/$isView = false;
                    /***/foreach($result as $res){
                    /***/   if($res["Tables_in_".$dbname] == $tableName){
                    /***/       $create_query = $q['Create View'];
                    /***/       $isView = true;
                    /***/   }
                    /***/}
                    /***/
                    /***/if($isView == false)
                    /***/       
                    $create_query = $q['Create Table'];
    
                    $pattern = '/CONSTRAINT.*|FOREIGN[\s]+KEY/';
    
                    // constraints to $tablename
                    preg_match_all($pattern, $create_query,$this->constraints[$tableName]);
    
                    $create_query = explode(',',preg_replace($pattern, '', $create_query));
    
                    for($i=0;$i<count($create_query)-1;$i++){
                        echo ($i>0 && $i<count($create_query)-2)?$create_query[$i].',':$create_query[$i];
                    }
                        echo "\n".trim($create_query[$i]).PHP_EOL;
    
                    /***/       
                    /***/if($isView == false){
                    /***/       
            $rows = $db->createCommand('SELECT * FROM '.$db->quoteTableName($tableName).';')->queryAll();
    
    
            if(empty($rows))
                return;
    
            echo PHP_EOL."--\n-- Data for table `$tableName`\n--".PHP_EOL.PHP_EOL;
    
            $attrs = array_map(array($db, 'quoteColumnName'), array_keys($rows[0]));
            echo 'INSERT INTO '.$db->quoteTableName($tableName).''." (", implode(', ', $attrs), ') VALUES'.PHP_EOL;
            $i=0;
            $rowsCount = count($rows);
            foreach($rows AS $row)
            {
                // Process row
                foreach($row AS $key => $value)
                {
                    if($value === null)
                        $row[$key] = 'NULL';
                    else
                        $row[$key] = $pdo->quote($value);
                }
    
                echo " (", implode(', ', $row), ')';
                if($i<$rowsCount-1)
                    echo ',';
                else
                    echo ';';
                echo PHP_EOL;
                $i++;
            }
                    /***/       
                    /***/}
                    /***/       
            echo PHP_EOL;
            echo PHP_EOL;
        }
    
  2. Log in to comment