Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to use path from MySQL "secure_file_priv" system variable for batch inserting via load infile #9529

Merged
merged 1 commit into from
Jan 20, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions core/Db/BatchInsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ public static function tableInsertBatchIterate($tableName, $fields, $values, $ig
*/
public static function tableInsertBatch($tableName, $fields, $values, $throwException = false)
{
$filePath = StaticContainer::get('path.tmp') . '/assets/' . $tableName . '-' . Common::generateUniqId() . '.csv';

$loadDataInfileEnabled = Config::getInstance()->General['enable_load_data_infile'];

if ($loadDataInfileEnabled
&& Db::get()->hasBulkLoader()) {

$path = self::getBestPathForLoadData();
$filePath = $path . $tableName . '-' . Common::generateUniqId() . '.csv';

try {
$fileSpec = array(
'delim' => "\t",
Expand Down Expand Up @@ -94,17 +96,36 @@ public static function tableInsertBatch($tableName, $fields, $values, $throwExce
throw $e;
}
}
}

// if all else fails, fallback to a series of INSERTs
if(file_exists($filePath)){
@unlink($filePath);
// if all else fails, fallback to a series of INSERTs
if (file_exists($filePath)) {
@unlink($filePath);
}
}

self::tableInsertBatchIterate($tableName, $fields, $values);

return false;
}

private static function getBestPathForLoadData()
{
try {
$path = Db::fetchOne('SELECT @@secure_file_priv'); // was introduced in 5.0.38
} catch (Exception $e) {
// we do not rethrow exception as an error is expected if MySQL is < 5.0.38
// in this case tableInsertBatch might still work
}

if (empty($path) || !is_dir($path) || !is_writable($path)) {
$path = StaticContainer::get('path.tmp') . '/assets/';
} elseif (!Common::stringEndsWith($path, '/')) {
$path .= '/';
}

return $path;
}

/**
* Batch insert into table from CSV (or other delimited) file.
*
Expand Down