Skip to content

Instantly share code, notes, and snippets.

@wolffc
Created June 27, 2013 09:30
Show Gist options
  • Save wolffc/5875191 to your computer and use it in GitHub Desktop.
Save wolffc/5875191 to your computer and use it in GitHub Desktop.
SQL File Splitter class
<?php
/**
* SQL Splitter class
* for splitting SQL dumps into multiple files for better upload to webfrontends like
* phpMyAdmin
* author: christian Wolff
*/
class sqlSplit{
/**
* The preg Condtion used vor detecting splitable lines. we dont want to split between an multilien STatement
* @var string
*/
var $splitCondition= '/^(--|INSERT|DROP|CREATE)/'; // split a comment or insert statement
/**
* preg match condtion if lines matches the conditon the line is removed
* @var string
*/
var $removeLineCondition = '-(^\/\*\!|INSERT INTO.*cache_pages.*\()-';
/**
* the Internale Linecounter used for deteming split by lines
* @var integer
*/
var $lineCounter = 0;
/**
* internal byte counter
* @var integer
*/
var $byteCounter = 0;
/**
* the minumum line length before a split occures
* @var integer
*/
var $minimumPartLength = 0; // 5000
/**
* the minium byte length before a split occures
* @var integer
*/
var $minimumByteLength = 31457280; //30 MB (30 * 1024 * 1024)
/**
* the filehandler of the input file
* @var false/resoure
*/
var $inputFile = FALSE;
/**
* the filehanlder of the output file
* @var false/resource
*/
var $outputFile = FALSE;
/**
* the counter for the output file gets increased if a new file is started
* @var integer
*/
var $outputNumber = 1;
/**
* the basename of the output file (without extesnion.)
* @var string
*/
var $outputFileName = '';
/**
* how much data we read for a line here
* @var integer
*/
var $maxLineLength = 16384; // 16 * 1024
/**
* the file extension usaly .sql
* @var string
*/
var $outputExtension = 'FALSE';
protected function openInputFile($input){
$this->inputFile = fopen($input,'r');
if(!$this->inputFile){
new Exeption('Could not Open input file');
}
}
protected function closeFile($file){
fclose($file);
}
protected function setOutputFile(){
$this->outputFile = fopen($this->getOutputFileName(), 'w');
}
protected function getOutputFileName(){
return $this->outputFileName.$this->outputNumber.$this->outputExtension;
}
protected function shouldWeSplit($line){
if(preg_match($this->splitCondition, $line) and $this->lineCounter > $this->minimumPartLength and $this->byteCounter > $this->minimumByteLength){
$this->closeFile($this->outputFile);
$this->lineCounter=0;
$this->byteCounter=0;
$this->outputNumber++;
$this->setOutputFile();
}
}
public function split($inputFilename,$outputFilename,$extension = '.sql'){
$this->outputFileName = $outputFilename;
$this->outputExtension = $extension;
$this->openInputFile($inputFilename);
$this->setOutputFile();
while( ($line = fgets($this->inputFile,$this->maxLineLength)) !== false){
if(!preg_match($this->removeLineCondition, $line)){
$this->shouldWeSplit($line);
fwrite($this->outputFile,$line);
$this->lineCounter++;
$this->byteCounter += strlen($line);
}
}
$this->closeFile($this->inputFile);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment