<?php
class DBTableCollection{
/**
* gehashtes Array "temp. Tablename" => "SQL-Create-Statement"
*/
var $tSQLtmp = array();
/**
* gehashtes Array "temp. Tablename => array of INSERT-Statements
*/
var $tSQLInserts = array();
function DBTableCollection(){
if (! defined("TEMPTABLE")) define("TEMPTABLE","cbtemp");
}
/**
* reads MYSQL-Table definitions from file and fills array with tablename => Mysql-SQL-Create
*
* Source-Datei ist ueblicherweise ein durch mysqldump erzeugtes File, kann aber auch manuell erstellt werden.
* Die erzeugten SQL-Create-Texte sind absolut 100% ig Standard-konform, da sie on the fly aus einer mit dem Mysql-Befehl "show create table <tablename>" angelegten temporären Tabelle erzeugt werden.
*
*/
function parseFromFile($fname,$includeInserts=false){
$counter=0;
if (! is_readable($fname)){
croak("Could not read $fname");
return;
}
$tmpArr=file($fname);
if (! $tmpArr){
croak("Could not get ".$fname);
return;
}
for($i=0; $i<count($tmpArr);$i++){
// Entfernen von Kommentar-Tags #
if (preg_match("/^(.*?)#(.*?)$/",$tmpArr[$i],$matches)){
$tmpArr[$i]=$matches[1];
debug(7,"Kommentar ".$matches[1]." erkannt in ".$matches[0]);
}
}
reset($tmpArr);
$fString=implode("\n",$tmpArr);
// Check-up, if file is directly starting with "create table" statement
if (preg_match("/^create table/i",$fString)){
$startParseAtArrayPos=0;
}else{
$startParseAtArrayPos=1;
}
$fString=preg_replace("/\n+/","\n",$fString);
$fArr=preg_split("/create table/i",$fString,-1,PREG_SPLIT_NO_EMPTY);
if (count($fArr)>0){
//print_r($fArr);
}else{
croak("Could not find any Create Table statement");
}
// now let's list found table-names ...
for ($i=$startParseAtArrayPos;$i<count($fArr);$i++){
if (preg_match("/^\s*(.*?)\_(.*?)\s*\(/",$fArr[$i],$matches)){
debug(0,"Table ".$matches[1]."_".$matches[2]." found in ".$fArr[$i]);
$counter++;
//if ((defined("TABLE"))&&
$tmpTable=TEMPTABLE."_".$matches[2];
$tmpCreateTable="create table ".str_replace($matches[1],TEMPTABLE,$fArr[$i]);
$pTable = new DBTable;
$this->tSQLtmp[$tmpTable]=$pTable->parse_CreateTable_statement($tmpCreateTable,true);
}
}
if ($includeInserts){
if (preg_match_all("/INSERT\s+INTO(.*);/i",$fString,$matches,PREG_SET_ORDER)){
//print_r($matches);
foreach ($matches as $arr){
if (preg_match("/INSERT\s+INTO\s+(\S+?)\_(\S+?)\s+/i",$arr[0],$batches)){
$tmpTable = TEMPTABLE."_".$batches[2];
/* foreach ($arr as $arri) {
$this->tSQLInserts[$tmpTable][] = str_replace($batches[1]."_",TEMPTABLE."_",$arri);
} */
$this->tSQLInserts[$tmpTable][] = preg_replace("/INSERT\s+INTO\s+(\S+?)\_(\S+?)\s+(.*?);/i","INSERT INTO `".TEMPTABLE."_\\2` \\3",$arr[0]);
//echo "<hr>\$erg:<p>";print_r($this->tSQLInserts[$tmpTable]);echo "<hr>";
}
}
}
//print_r($this->tSQLInserts);
//foreach ($this->tSQLInserts as $k => $v){
// echo "fuer $k: ".implode(":",$v)."<br>\n";
//}
}
return $counter;
}
}
?>