Revision 2105
Added by darkviper almost 10 years ago
WbDatabase.php | ||
---|---|---|
526 | 526 |
$sEngine = 'MyISAM', |
527 | 527 |
$sCollation = 'utf8_unicode_ci') |
528 | 528 |
{ |
529 |
return $this->importSql($sSqlDump, $sTablePrefix, $bPreserve, $sEngine, $sCollation);
|
|
529 |
return $this->importSql($sSqlDump, $sTablePrefix, $sAction, $sEngine, $sCollation);
|
|
530 | 530 |
} |
531 |
/** |
|
532 |
* Import an SQl-Dumpfile witch can include unlimited placeholders for values |
|
533 |
* @param mixed $mSqlDump can be string with filename or array with additional vars |
|
534 |
* @param string $sTablePrefix can be used to override settings from WbDatabase object |
|
535 |
* @param string $sAction 'install', 'uninstall', 'upgrade', 'repair |
|
536 |
* @param string $sEngine kind of table engine: MyIsam(default) |
|
537 |
* @param string $sCollation utf8_unicode_ci(default) |
|
538 |
* @return bool false on error |
|
539 |
*/ |
|
540 |
public function importSql( |
|
541 |
$mSqlDump, |
|
542 |
$sTablePrefix = '', // can override settings from WbDatabase object |
|
543 |
$sAction = 'install', // skip 'DROP TABLE' statements |
|
544 |
$sEngine = 'MyISAM', // the default table engine |
|
545 |
$sCollation = 'utf8_unicode_ci' // the default collation to use |
|
546 |
) |
|
547 |
{ |
|
548 |
$retval = true; |
|
549 |
$this->error = ''; |
|
550 |
// sanitize arguments |
|
551 |
if (! is_string($sAction)) { |
|
552 |
$sAction = $sAction ? 'repair' : 'install'; |
|
553 |
} |
|
554 |
$aAllowedActions = array('install', 'uninstall', 'upgrade', 'repair'); |
|
555 |
$sAction = strtolower(preg_replace('/^.*?('.implode('|', $aAllowedActions).')(\.php)?$/iU', '$1', $sAction)); |
|
556 |
$sAction = (in_array($sAction, $aAllowedActions) ? $sAction : 'install'); |
|
557 |
$sTablePrefix = trim($sTablePrefix); |
|
558 |
$aEngineTypes = array( |
|
559 |
'csv' => 'CSV', |
|
560 |
'blackhole' => 'BLACKHOLE', |
|
561 |
'memory' => 'MEMORY', |
|
562 |
'myisam' => 'MyISAM', |
|
563 |
'innodb' => 'InnoDB', |
|
564 |
'archive' => 'ARCHIVE', |
|
565 |
'mrg_myisam' => 'MRG_MYISAM' |
|
566 |
); |
|
567 |
if (isset($aEngineTypes[strtolower($sEngine)])) { |
|
568 |
$sEngine = $aEngineTypes[strtolower($sEngine)]; |
|
569 |
} else { |
|
570 |
$sEngine = 'MyISAM'; |
|
571 |
} |
|
572 |
// test if selected collation is available. Otherwise select 'utf8_unicode_ci' |
|
573 |
$sql = 'SELECT COUNT(*) FROM `COLLATIONS` ' |
|
574 |
. 'WHERE `COLLATION_NAME`=\''.$sCollation.'\''; |
|
575 |
$sCollation = ($this->get_one($sql) ? $sCollation : 'utf8_unicode_ci'); |
|
576 |
$aTmp = preg_split('/_/', $sCollation, null, PREG_SPLIT_NO_EMPTY); |
|
577 |
$sCharset = $aTmp[0]; |
|
578 |
// define array of searches |
|
579 |
$aSearch = array( |
|
580 |
'/\{TABLE_PREFIX\}/', |
|
581 |
'/\{TABLE_COLLATION\}/', // deprecated from 2.8.4 |
|
582 |
'/\{FIELD_COLLATION\}/', // deprecated from 2.8.4 |
|
583 |
'/\{TABLE_ENGINE\}/', |
|
584 |
'/\{TABLE_ENGINE=([a-zA-Z_0-9]*)\}/', |
|
585 |
'/\{CHARSET\}/', |
|
586 |
'/\{COLLATION\}/' |
|
587 |
); |
|
588 |
// define array of replacements |
|
589 |
$aReplace = array( |
|
590 |
$sTablePrefix, |
|
591 |
' COLLATE {COLLATION}', // deprecated from 2.8.4 |
|
592 |
' COLLATE {COLLATION}', // deprecated from 2.8.4 |
|
593 |
' {ENGINE='.$sEngine.'}', |
|
594 |
' ENGINE=$1 DEFAULT CHARSET={CHARSET} COLLATION={COLLATION}', |
|
595 |
$sCharset, |
|
596 |
$sCollation |
|
597 |
); |
|
598 | 531 |
|
599 |
if (is_array($mSqlDump)) { |
|
600 |
// try to get dumpfile name |
|
601 |
if (!isset($mSqlDump['sSqlDump'])) { |
|
602 |
$this->error = 'missing index \'sSqlDump\' in $mSqlDump'; |
|
603 |
return false; |
|
604 |
} else { |
|
605 |
// get dumpfile name from array and then remove entry |
|
606 |
$sDumpFile = (string)$mSqlDump['sSqlDump']; |
|
607 |
unset($mSqlDump['sSqlDump']); |
|
608 |
// import all vars and it's values from array |
|
609 |
foreach ($mSqlDump as $sIndex => $sValue) { |
|
610 |
// transform varname into placeholder name ('sPageTitle' => 'PAGE_TITLE') |
|
611 |
$sIndex = strtoupper(preg_replace('/([a-z0-9])([A-Z])/', '\1_\2', ltrim($sIndex, 'a..z'))); |
|
612 |
// fill search/replace arrays |
|
613 |
$aSearch[] = '/\{'.$sIndex.'\}/'; |
|
614 |
$aReplace[] = $sValue ; |
|
615 |
} |
|
616 |
} |
|
617 |
} elseif (is_string($mSqlDump)) { |
|
618 |
$sDumpFile = (string)$mSqlDump; |
|
619 |
} else { |
|
620 |
$this->error = 'invalid argument $mSqlDump'; |
|
621 |
return false; |
|
622 |
} |
|
623 |
if (!is_readable($sDumpFile)) { |
|
624 |
$this->Error = 'unable to open \''.$sDumpFile.'\''; |
|
625 |
return false; |
|
626 |
} |
|
627 |
$sql = ''; |
|
628 |
$aSql = file($sDumpFile, FILE_SKIP_EMPTY_LINES); |
|
629 |
// remove possible ByteOrderMark |
|
630 |
$aSql[0] = preg_replace('/^[\xAA-\xFF]{3}/', '', $aSql[0]); |
|
631 |
while (sizeof($aSql) > 0) { |
|
632 |
$sSqlLine = trim(array_shift($aSql)); |
|
633 |
if (!preg_match('/^[-\/]+.*/', $sSqlLine)) { |
|
634 |
$sql = $sql.' '.$sSqlLine; |
|
635 |
if ((substr($sql,-1,1) == ';')) { |
|
636 |
$sql = trim(preg_replace($aSearch, $aReplace, $sql)); |
|
637 |
$sAvailSqlObjects = 'TABLE|VIEW|INDEX|PROCEDURE|FUNCTION|TRIGGER|EVENT'; |
|
638 |
switch ($sAction) { |
|
639 |
case 'uninstall': // skip CREATE; execute DROP |
|
640 |
if (preg_match('/^\s*CREATE ('.$sAvailSqlObjects.') /siU', $sql)) { |
|
641 |
$sql = ''; |
|
642 |
continue; // read next statement |
|
643 |
} |
|
644 |
break; |
|
645 |
case 'upgrade': // skip DROP; execute CREATE |
|
646 |
case 'repair': // skip DROP; execute CREATE |
|
647 |
if (preg_match('/^\s*DROP ('.$sAvailSqlObjects.') /siU', $sql)) { |
|
648 |
$sql = ''; |
|
649 |
continue; // read next statement |
|
650 |
} |
|
651 |
break; |
|
652 |
default: // install: execute DROP; execute CREATE |
|
653 |
break; |
|
654 |
} |
|
655 |
if (!$this->doQuery($sql)) { |
|
656 |
$retval = false; |
|
657 |
$this->error = $this->getError(); |
|
658 |
unset($aSql); |
|
659 |
break; |
|
660 |
} |
|
661 |
$sql = ''; |
|
662 |
} |
|
663 |
} |
|
664 |
} |
|
665 |
return $retval; |
|
666 |
} // end of function importSql() |
|
667 | 532 |
/** |
668 | 533 |
* retuns the type of the engine used for requested table |
669 | 534 |
* @param string $table name of the table, including prefix |
Also available in: Unified diff
- removed importSql() method from class WbDatabase
+ new class SqlImport added