/** * Exports all rows of the given array and writes the dump to the output buffer. * * @param array array with identifiers of rows */ private function exportRowData() { $db = Yii::app()->db; $pdo = $db->getPdoInstance(); // Columns $cols = Column::model()->findAllByAttributes(array('TABLE_NAME' => $this->table, 'TABLE_SCHEMA' => $this->schema)); $blobCols = array(); // Create insert statement if ($this->settings['completeInserts']) { $columns = array(); $i = 0; foreach ($cols as $col) { $columns[] = $db->quoteColumnName($col->COLUMN_NAME); if (in_array(DataType::getBaseType($col->DATA_TYPE), array('smallblob', 'blob', 'mediumblob', 'longblob'))) { $blobCols[] = $i; } $i++; } $columns = ' (' . implode(', ', $columns) . ')'; } else { $columns = ''; } $insert = $this->settings['insertCommand'] . ($this->settings['delayedInserts'] ? ' DELAYED' : '') . ($this->settings['ignoreInserts'] ? ' IGNORE' : '') . ' INTO ' . $db->quoteTableName($this->table) . $columns . ' VALUES'; // Find all rows $rowCount = count($this->rows); // Settings $hexBlobs = $this->settings['hexBlobs']; $rowsPerInsert = (int) $this->settings['rowsPerInsert']; // Cycle rows $i = 0; $k = 1; foreach ($this->rows as $row) { // Add comment if ($i == 0) { $this->comment('Data for table ' . $db->quoteTableName($this->table)); echo "\n\n"; echo $insert; } $attributes = $row->getAttributes(); SqlUtil::FixRow($attributes); // Escape all contents foreach ($attributes as $key => $value) { if ($value === null) { $attributes[$key] = 'NULL'; } elseif ($hexBlobs && in_array($key, $blobCols) && $value) { $attributes[$key] = '0x' . bin2hex($value); } else { $attributes[$key] = $pdo->quote($value); } } // Add this row echo "\n (", implode(', ', $attributes), ')'; if ($i == $rowCount - 1) { echo ";\n\n"; } elseif ($k == $rowsPerInsert) { echo ";\n\n", $insert; $k = 0; } else { echo ','; } $i++; $k++; } }
/** * Exports all rows of the given array and writes the dump to the output buffer. * * @param array array with identifiers of rows */ private function exportRowData() { $db = Yii::app()->db; $pdo = $db->getPdoInstance(); // Columns $cols = Column::model()->findAllByAttributes(array('TABLE_NAME' => $this->table, 'TABLE_SCHEMA' => $this->schema)); $blobCols = array(); $columns = array(); $i = 0; foreach ($cols as $col) { $columns[] = $db->quoteColumnName($col->COLUMN_NAME); if (in_array(DataType::getBaseType($col->DATA_TYPE), array('smallblob', 'blob', 'mediumblob', 'longblob'))) { $blobCols[] = $i; } $i++; } $columns = implode($this->settings['fieldTerminator'], $columns); $insert = ""; // Find all rows $rowCount = count($this->rows); // Settings $hexBlobs = $this->settings['hexBlobs']; $rowsPerInsert = (int) $this->settings['rowsPerInsert']; // Cycle rows $i = 0; $k = 1; foreach ($this->rows as $row) { if ($i == 0 && $this->settings['fieldsFirstRow']) { echo $columns; } $attributes = $row->getAttributes(); SqlUtil::FixRow($attributes); // Escape all contents foreach ($attributes as $key => $value) { if ($value === null) { $attributes[$key] = 'NULL'; } elseif ($hexBlobs && in_array($key, $blobCols) && $value) { $attributes[$key] = '0x' . bin2hex($value); } else { $attributes[$key] = $this->settings['fieldEncloseString'] . addcslashes($value, $this->settings['fieldEncloseString']) . $this->settings['fieldEncloseString']; } } echo "\n", implode($this->settings['fieldTerminator'], $attributes); if ($i == $rowCount - 1) { echo "\n\n"; } $i++; $k++; } }