12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318 |
- <?php
- /**
- * PHP version of mysqldump cli that comes with MySQL.
- *
- * Tags: mysql mysqldump pdo php7 php5 database php sql hhvm mariadb mysql-backup.
- *
- * @category Library
- * @package Ifsnop\Mysqldump
- * @author Diego Torres <ifsnop@github.com>
- * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
- * @link https://github.com/ifsnop/mysqldump-php
- *
- */
- namespace Ifsnop\Mysqldump;
- use Exception;
- use PDO;
- use PDOException;
- /**
- * Class Mysqldump.
- *
- * @category Library
- * @author Diego Torres <ifsnop@github.com>
- * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
- * @link https://github.com/ifsnop/mysqldump-php
- *
- */
- class Mysqldump
- {
- // Same as mysqldump.
- const MAXLINESIZE = 1000000;
- // List of available compression methods as constants.
- const GZIP = 'Gzip';
- const BZIP2 = 'Bzip2';
- const NONE = 'None';
- const GZIPSTREAM = 'Gzipstream';
- // List of available connection strings.
- const UTF8 = 'utf8';
- const UTF8MB4 = 'utf8mb4';
- /**
- * Database username.
- * @var string
- */
- public $user;
- /**
- * Database password.
- * @var string
- */
- public $pass;
- /**
- * Connection string for PDO.
- * @var string
- */
- public $dsn;
- /**
- * Destination filename, defaults to stdout.
- * @var string
- */
- public $fileName = 'php://stdout';
- // Internal stuff.
- private $tables = array();
- private $views = array();
- private $triggers = array();
- private $procedures = array();
- private $functions = array();
- private $events = array();
- private $dbHandler = null;
- private $dbType = "";
- private $compressManager;
- private $typeAdapter;
- private $dumpSettings = array();
- private $pdoSettings = array();
- private $version;
- private $tableColumnTypes = array();
- private $transformColumnValueCallable;
- /**
- * Database name, parsed from dsn.
- * @var string
- */
- private $dbName;
- /**
- * Host name, parsed from dsn.
- * @var string
- */
- private $host;
- /**
- * Dsn string parsed as an array.
- * @var array
- */
- private $dsnArray = array();
- /**
- * Keyed on table name, with the value as the conditions.
- * e.g. - 'users' => 'date_registered > NOW() - INTERVAL 6 MONTH'
- *
- * @var array
- */
- private $tableWheres = array();
- private $tableLimits = array();
- /**
- * Constructor of Mysqldump. Note that in the case of an SQLite database
- * connection, the filename must be in the $db parameter.
- *
- * @param string $dsn PDO DSN connection string
- * @param string $user SQL account username
- * @param string $pass SQL account password
- * @param array $dumpSettings SQL database settings
- * @param array $pdoSettings PDO configured attributes
- */
- public function __construct(
- $dsn = '',
- $user = '',
- $pass = '',
- $dumpSettings = array(),
- $pdoSettings = array()
- ) {
- $dumpSettingsDefault = array(
- 'include-tables' => array(),
- 'exclude-tables' => array(),
- 'compress' => Mysqldump::NONE,
- 'init_commands' => array(),
- 'no-data' => array(),
- 'reset-auto-increment' => false,
- 'add-drop-database' => false,
- 'add-drop-table' => false,
- 'add-drop-trigger' => true,
- 'add-locks' => true,
- 'complete-insert' => false,
- 'databases' => false,
- 'default-character-set' => Mysqldump::UTF8,
- 'disable-keys' => true,
- 'extended-insert' => true,
- 'events' => false,
- 'hex-blob' => true, /* faster than escaped content */
- 'insert-ignore' => false,
- 'net_buffer_length' => self::MAXLINESIZE,
- 'no-autocommit' => true,
- 'no-create-info' => false,
- 'lock-tables' => true,
- 'routines' => false,
- 'single-transaction' => true,
- 'skip-triggers' => false,
- 'skip-tz-utc' => false,
- 'skip-comments' => false,
- 'skip-dump-date' => false,
- 'skip-definer' => false,
- 'where' => '',
- 'include-views' => array(),
- /* deprecated */
- 'disable-foreign-keys-check' => true
- );
- $pdoSettingsDefault = array(
- PDO::ATTR_PERSISTENT => true,
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- );
- $this->user = $user;
- $this->pass = $pass;
- $this->parseDsn($dsn);
- // This drops MYSQL dependency, only use the constant if it's defined.
- if ("mysql" === $this->dbType) {
- $pdoSettingsDefault[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = false;
- }
- $this->pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings);
- $this->dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings);
- $this->dumpSettings['init_commands'][] = "SET NAMES ".$this->dumpSettings['default-character-set'];
- if (false === $this->dumpSettings['skip-tz-utc']) {
- $this->dumpSettings['init_commands'][] = "SET TIME_ZONE='+00:00'";
- }
- $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault));
- if (count($diff) > 0) {
- throw new Exception("Unexpected value in dumpSettings: (".implode(",", $diff).")");
- }
- if (!is_array($this->dumpSettings['include-tables']) ||
- !is_array($this->dumpSettings['exclude-tables'])) {
- throw new Exception("Include-tables and exclude-tables should be arrays");
- }
- // Dump the same views as tables, mimic mysqldump behaviour
- //$this->dumpSettings['include-views'] = $this->dumpSettings['include-tables'];
- // Create a new compressManager to manage compressed output
- $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']);
- }
- /**
- * Destructor of Mysqldump. Unsets dbHandlers and database objects.
- */
- public function __destruct()
- {
- $this->dbHandler = null;
- }
- /**
- * Custom array_replace_recursive to be used if PHP < 5.3
- * Replaces elements from passed arrays into the first array recursively.
- *
- * @param array $array1 The array in which elements are replaced
- * @param array $array2 The array from which elements will be extracted
- *
- * @return array Returns an array, or NULL if an error occurs.
- */
- public static function array_replace_recursive($array1, $array2)
- {
- if (function_exists('array_replace_recursive')) {
- return array_replace_recursive($array1, $array2);
- }
- foreach ($array2 as $key => $value) {
- if (is_array($value)) {
- $array1[$key] = self::array_replace_recursive($array1[$key], $value);
- } else {
- $array1[$key] = $value;
- }
- }
- return $array1;
- }
- /**
- * Keyed by table name, with the value as the conditions:
- * e.g. 'users' => 'date_registered > NOW() - INTERVAL 6 MONTH AND deleted=0'
- *
- * @param array $tableWheres
- */
- public function setTableWheres(array $tableWheres)
- {
- $this->tableWheres = $tableWheres;
- }
- /**
- * @param $tableName
- *
- * @return boolean|mixed
- */
- public function getTableWhere($tableName)
- {
- if (!empty($this->tableWheres[$tableName])) {
- return $this->tableWheres[$tableName];
- } elseif ($this->dumpSettings['where']) {
- return $this->dumpSettings['where'];
- }
- return false;
- }
- /**
- * Keyed by table name, with the value as the numeric limit:
- * e.g. 'users' => 3000
- *
- * @param array $tableLimits
- */
- public function setTableLimits(array $tableLimits)
- {
- $this->tableLimits = $tableLimits;
- }
- /**
- * Returns the LIMIT for the table. Must be numeric to be returned.
- * @param $tableName
- * @return boolean
- */
- public function getTableLimit($tableName)
- {
- if (empty($this->tableLimits[$tableName])) {
- return false;
- }
- $limit = $this->tableLimits[$tableName];
- if (!is_numeric($limit)) {
- return false;
- }
- return $limit;
- }
- /**
- * Parse DSN string and extract dbname value
- * Several examples of a DSN string
- * mysql:host=localhost;dbname=testdb
- * mysql:host=localhost;port=3307;dbname=testdb
- * mysql:unix_socket=/tmp/mysql.sock;dbname=testdb
- *
- * @param string $dsn dsn string to parse
- * @return boolean
- */
- private function parseDsn($dsn)
- {
- if (empty($dsn) || (false === ($pos = strpos($dsn, ":")))) {
- throw new Exception("Empty DSN string");
- }
- $this->dsn = $dsn;
- $this->dbType = strtolower(substr($dsn, 0, $pos)); // always returns a string
- if (empty($this->dbType)) {
- throw new Exception("Missing database type from DSN string");
- }
- $dsn = substr($dsn, $pos + 1);
- foreach (explode(";", $dsn) as $kvp) {
- $kvpArr = explode("=", $kvp);
- $this->dsnArray[strtolower($kvpArr[0])] = $kvpArr[1];
- }
- if (empty($this->dsnArray['host']) &&
- empty($this->dsnArray['unix_socket'])) {
- throw new Exception("Missing host from DSN string");
- }
- $this->host = (!empty($this->dsnArray['host'])) ?
- $this->dsnArray['host'] : $this->dsnArray['unix_socket'];
- if (empty($this->dsnArray['dbname'])) {
- throw new Exception("Missing database name from DSN string");
- }
- $this->dbName = $this->dsnArray['dbname'];
- return true;
- }
- /**
- * Connect with PDO.
- *
- * @return null
- */
- private function connect()
- {
- // Connecting with PDO.
- try {
- switch ($this->dbType) {
- case 'sqlite':
- $this->dbHandler = @new PDO("sqlite:".$this->dbName, null, null, $this->pdoSettings);
- break;
- case 'mysql':
- case 'pgsql':
- case 'dblib':
- $this->dbHandler = @new PDO(
- $this->dsn,
- $this->user,
- $this->pass,
- $this->pdoSettings
- );
- // Execute init commands once connected
- foreach ($this->dumpSettings['init_commands'] as $stmt) {
- $this->dbHandler->exec($stmt);
- }
- // Store server version
- $this->version = $this->dbHandler->getAttribute(PDO::ATTR_SERVER_VERSION);
- break;
- default:
- throw new Exception("Unsupported database type (".$this->dbType.")");
- }
- } catch (PDOException $e) {
- throw new Exception(
- "Connection to ".$this->dbType." failed with message: ".
- $e->getMessage()
- );
- }
- if (is_null($this->dbHandler)) {
- throw new Exception("Connection to ".$this->dbType."failed");
- }
- $this->dbHandler->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL);
- $this->typeAdapter = TypeAdapterFactory::create($this->dbType, $this->dbHandler, $this->dumpSettings);
- }
- /**
- * Primary function, triggers dumping.
- *
- * @param string $filename Name of file to write sql dump to
- * @return null
- * @throws \Exception
- */
- public function start($filename = '')
- {
- // Output file can be redefined here
- if (!empty($filename)) {
- $this->fileName = $filename;
- }
- // Connect to database
- $this->connect();
- // Create output file
- $this->compressManager->open($this->fileName);
- // Write some basic info to output file
- $this->compressManager->write($this->getDumpFileHeader());
- // Store server settings and use sanner defaults to dump
- $this->compressManager->write(
- $this->typeAdapter->backup_parameters()
- );
- if ($this->dumpSettings['databases']) {
- $this->compressManager->write(
- $this->typeAdapter->getDatabaseHeader($this->dbName)
- );
- if ($this->dumpSettings['add-drop-database']) {
- $this->compressManager->write(
- $this->typeAdapter->add_drop_database($this->dbName)
- );
- }
- }
- // Get table, view, trigger, procedures, functions and events structures from
- // database.
- $this->getDatabaseStructureTables();
- $this->getDatabaseStructureViews();
- $this->getDatabaseStructureTriggers();
- $this->getDatabaseStructureProcedures();
- $this->getDatabaseStructureFunctions();
- $this->getDatabaseStructureEvents();
- if ($this->dumpSettings['databases']) {
- $this->compressManager->write(
- $this->typeAdapter->databases($this->dbName)
- );
- }
- // If there still are some tables/views in include-tables array,
- // that means that some tables or views weren't found.
- // Give proper error and exit.
- // This check will be removed once include-tables supports regexps.
- if (0 < count($this->dumpSettings['include-tables'])) {
- $name = implode(",", $this->dumpSettings['include-tables']);
- throw new Exception("Table (".$name.") not found in database");
- }
- $this->exportTables();
- $this->exportTriggers();
- $this->exportFunctions();
- $this->exportProcedures();
- $this->exportViews();
- $this->exportEvents();
- // Restore saved parameters.
- $this->compressManager->write(
- $this->typeAdapter->restore_parameters()
- );
- // Write some stats to output file.
- $this->compressManager->write($this->getDumpFileFooter());
- // Close output file.
- $this->compressManager->close();
- return;
- }
- /**
- * Returns header for dump file.
- *
- * @return string
- */
- private function getDumpFileHeader()
- {
- $header = '';
- if (!$this->dumpSettings['skip-comments']) {
- // Some info about software, source and time
- $header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php".PHP_EOL.
- "--".PHP_EOL.
- "-- Host: {$this->host}\tDatabase: {$this->dbName}".PHP_EOL.
- "-- ------------------------------------------------------".PHP_EOL;
- if (!empty($this->version)) {
- $header .= "-- Server version \t".$this->version.PHP_EOL;
- }
- if (!$this->dumpSettings['skip-dump-date']) {
- $header .= "-- Date: ".date('r').PHP_EOL.PHP_EOL;
- }
- }
- return $header;
- }
- /**
- * Returns footer for dump file.
- *
- * @return string
- */
- private function getDumpFileFooter()
- {
- $footer = '';
- if (!$this->dumpSettings['skip-comments']) {
- $footer .= '-- Dump completed';
- if (!$this->dumpSettings['skip-dump-date']) {
- $footer .= ' on: '.date('r');
- }
- $footer .= PHP_EOL;
- }
- return $footer;
- }
- /**
- * Reads table names from database.
- * Fills $this->tables array so they will be dumped later.
- *
- * @return null
- */
- private function getDatabaseStructureTables()
- {
- // Listing all tables from database
- if (empty($this->dumpSettings['include-tables'])) {
- // include all tables for now, blacklisting happens later
- foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) {
- array_push($this->tables, current($row));
- }
- } else {
- // include only the tables mentioned in include-tables
- foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) {
- if (in_array(current($row), $this->dumpSettings['include-tables'], true)) {
- array_push($this->tables, current($row));
- $elem = array_search(
- current($row),
- $this->dumpSettings['include-tables']
- );
- unset($this->dumpSettings['include-tables'][$elem]);
- }
- }
- }
- return;
- }
- /**
- * Reads view names from database.
- * Fills $this->tables array so they will be dumped later.
- *
- * @return null
- */
- private function getDatabaseStructureViews()
- {
- // Listing all views from database
- if (empty($this->dumpSettings['include-views'])) {
- // include all views for now, blacklisting happens later
- foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) {
- array_push($this->views, current($row));
- }
- } else {
- // include only the tables mentioned in include-tables
- foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) {
- if (in_array(current($row), $this->dumpSettings['include-views'], true)) {
- array_push($this->views, current($row));
- $elem = array_search(
- current($row),
- $this->dumpSettings['include-views']
- );
- unset($this->dumpSettings['include-views'][$elem]);
- }
- }
- }
- return;
- }
- /**
- * Reads trigger names from database.
- * Fills $this->tables array so they will be dumped later.
- *
- * @return null
- */
- private function getDatabaseStructureTriggers()
- {
- // Listing all triggers from database
- if (false === $this->dumpSettings['skip-triggers']) {
- foreach ($this->dbHandler->query($this->typeAdapter->show_triggers($this->dbName)) as $row) {
- array_push($this->triggers, $row['Trigger']);
- }
- }
- return;
- }
- /**
- * Reads procedure names from database.
- * Fills $this->tables array so they will be dumped later.
- *
- * @return null
- */
- private function getDatabaseStructureProcedures()
- {
- // Listing all procedures from database
- if ($this->dumpSettings['routines']) {
- foreach ($this->dbHandler->query($this->typeAdapter->show_procedures($this->dbName)) as $row) {
- array_push($this->procedures, $row['procedure_name']);
- }
- }
- return;
- }
- /**
- * Reads functions names from database.
- * Fills $this->tables array so they will be dumped later.
- *
- * @return null
- */
- private function getDatabaseStructureFunctions()
- {
- // Listing all functions from database
- if ($this->dumpSettings['routines']) {
- foreach ($this->dbHandler->query($this->typeAdapter->show_functions($this->dbName)) as $row) {
- array_push($this->functions, $row['function_name']);
- }
- }
- return;
- }
- /**
- * Reads event names from database.
- * Fills $this->tables array so they will be dumped later.
- *
- * @return null
- */
- private function getDatabaseStructureEvents()
- {
- // Listing all events from database
- if ($this->dumpSettings['events']) {
- foreach ($this->dbHandler->query($this->typeAdapter->show_events($this->dbName)) as $row) {
- array_push($this->events, $row['event_name']);
- }
- }
- return;
- }
- /**
- * Compare if $table name matches with a definition inside $arr
- * @param $table string
- * @param $arr array with strings or patterns
- * @return boolean
- */
- private function matches($table, $arr)
- {
- $match = false;
- foreach ($arr as $pattern) {
- if ('/' != $pattern[0]) {
- continue;
- }
- if (1 == preg_match($pattern, $table)) {
- $match = true;
- }
- }
- return in_array($table, $arr) || $match;
- }
- /**
- * Exports all the tables selected from database
- *
- * @return null
- */
- private function exportTables()
- {
- // Exporting tables one by one
- foreach ($this->tables as $table) {
- if ($this->matches($table, $this->dumpSettings['exclude-tables'])) {
- continue;
- }
- $this->getTableStructure($table);
- if (false === $this->dumpSettings['no-data']) { // don't break compatibility with old trigger
- $this->listValues($table);
- } elseif (true === $this->dumpSettings['no-data']
- || $this->matches($table, $this->dumpSettings['no-data'])) {
- continue;
- } else {
- $this->listValues($table);
- }
- }
- }
- /**
- * Exports all the views found in database
- *
- * @return null
- */
- private function exportViews()
- {
- if (false === $this->dumpSettings['no-create-info']) {
- // Exporting views one by one
- foreach ($this->views as $view) {
- if ($this->matches($view, $this->dumpSettings['exclude-tables'])) {
- continue;
- }
- $this->tableColumnTypes[$view] = $this->getTableColumnTypes($view);
- $this->getViewStructureTable($view);
- }
- foreach ($this->views as $view) {
- if ($this->matches($view, $this->dumpSettings['exclude-tables'])) {
- continue;
- }
- $this->getViewStructureView($view);
- }
- }
- }
- /**
- * Exports all the triggers found in database
- *
- * @return null
- */
- private function exportTriggers()
- {
- // Exporting triggers one by one
- foreach ($this->triggers as $trigger) {
- $this->getTriggerStructure($trigger);
- }
- }
- /**
- * Exports all the procedures found in database
- *
- * @return null
- */
- private function exportProcedures()
- {
- // Exporting triggers one by one
- foreach ($this->procedures as $procedure) {
- $this->getProcedureStructure($procedure);
- }
- }
- /**
- * Exports all the functions found in database
- *
- * @return null
- */
- private function exportFunctions()
- {
- // Exporting triggers one by one
- foreach ($this->functions as $function) {
- $this->getFunctionStructure($function);
- }
- }
- /**
- * Exports all the events found in database
- *
- * @return null
- */
- private function exportEvents()
- {
- // Exporting triggers one by one
- foreach ($this->events as $event) {
- $this->getEventStructure($event);
- }
- }
- /**
- * Table structure extractor
- *
- * @todo move specific mysql code to typeAdapter
- * @param string $tableName Name of table to export
- * @return null
- */
- private function getTableStructure($tableName)
- {
- if (!$this->dumpSettings['no-create-info']) {
- $ret = '';
- if (!$this->dumpSettings['skip-comments']) {
- $ret = "--".PHP_EOL.
- "-- Table structure for table `$tableName`".PHP_EOL.
- "--".PHP_EOL.PHP_EOL;
- }
- $stmt = $this->typeAdapter->show_create_table($tableName);
- foreach ($this->dbHandler->query($stmt) as $r) {
- $this->compressManager->write($ret);
- if ($this->dumpSettings['add-drop-table']) {
- $this->compressManager->write(
- $this->typeAdapter->drop_table($tableName)
- );
- }
- $this->compressManager->write(
- $this->typeAdapter->create_table($r)
- );
- break;
- }
- }
- $this->tableColumnTypes[$tableName] = $this->getTableColumnTypes($tableName);
- return;
- }
- /**
- * Store column types to create data dumps and for Stand-In tables
- *
- * @param string $tableName Name of table to export
- * @return array type column types detailed
- */
- private function getTableColumnTypes($tableName)
- {
- $columnTypes = array();
- $columns = $this->dbHandler->query(
- $this->typeAdapter->show_columns($tableName)
- );
- $columns->setFetchMode(PDO::FETCH_ASSOC);
- foreach ($columns as $key => $col) {
- $types = $this->typeAdapter->parseColumnType($col);
- $columnTypes[$col['Field']] = array(
- 'is_numeric'=> $types['is_numeric'],
- 'is_blob' => $types['is_blob'],
- 'type' => $types['type'],
- 'type_sql' => $col['Type'],
- 'is_virtual' => $types['is_virtual']
- );
- }
- return $columnTypes;
- }
- /**
- * View structure extractor, create table (avoids cyclic references)
- *
- * @todo move mysql specific code to typeAdapter
- * @param string $viewName Name of view to export
- * @return null
- */
- private function getViewStructureTable($viewName)
- {
- if (!$this->dumpSettings['skip-comments']) {
- $ret = "--".PHP_EOL.
- "-- Stand-In structure for view `${viewName}`".PHP_EOL.
- "--".PHP_EOL.PHP_EOL;
- $this->compressManager->write($ret);
- }
- $stmt = $this->typeAdapter->show_create_view($viewName);
- // create views as tables, to resolve dependencies
- foreach ($this->dbHandler->query($stmt) as $r) {
- if ($this->dumpSettings['add-drop-table']) {
- $this->compressManager->write(
- $this->typeAdapter->drop_view($viewName)
- );
- }
- $this->compressManager->write(
- $this->createStandInTable($viewName)
- );
- break;
- }
- }
- /**
- * Write a create table statement for the table Stand-In, show create
- * table would return a create algorithm when used on a view
- *
- * @param string $viewName Name of view to export
- * @return string create statement
- */
- public function createStandInTable($viewName)
- {
- $ret = array();
- foreach ($this->tableColumnTypes[$viewName] as $k => $v) {
- $ret[] = "`${k}` ${v['type_sql']}";
- }
- $ret = implode(PHP_EOL.",", $ret);
- $ret = "CREATE TABLE IF NOT EXISTS `$viewName` (".
- PHP_EOL.$ret.PHP_EOL.");".PHP_EOL;
- return $ret;
- }
- /**
- * View structure extractor, create view
- *
- * @todo move mysql specific code to typeAdapter
- * @param string $viewName Name of view to export
- * @return null
- */
- private function getViewStructureView($viewName)
- {
- if (!$this->dumpSettings['skip-comments']) {
- $ret = "--".PHP_EOL.
- "-- View structure for view `${viewName}`".PHP_EOL.
- "--".PHP_EOL.PHP_EOL;
- $this->compressManager->write($ret);
- }
- $stmt = $this->typeAdapter->show_create_view($viewName);
- // create views, to resolve dependencies
- // replacing tables with views
- foreach ($this->dbHandler->query($stmt) as $r) {
- // because we must replace table with view, we should delete it
- $this->compressManager->write(
- $this->typeAdapter->drop_view($viewName)
- );
- $this->compressManager->write(
- $this->typeAdapter->create_view($r)
- );
- break;
- }
- }
- /**
- * Trigger structure extractor
- *
- * @param string $triggerName Name of trigger to export
- * @return null
- */
- private function getTriggerStructure($triggerName)
- {
- $stmt = $this->typeAdapter->show_create_trigger($triggerName);
- foreach ($this->dbHandler->query($stmt) as $r) {
- if ($this->dumpSettings['add-drop-trigger']) {
- $this->compressManager->write(
- $this->typeAdapter->add_drop_trigger($triggerName)
- );
- }
- $this->compressManager->write(
- $this->typeAdapter->create_trigger($r)
- );
- return;
- }
- }
- /**
- * Procedure structure extractor
- *
- * @param string $procedureName Name of procedure to export
- * @return null
- */
- private function getProcedureStructure($procedureName)
- {
- if (!$this->dumpSettings['skip-comments']) {
- $ret = "--".PHP_EOL.
- "-- Dumping routines for database '".$this->dbName."'".PHP_EOL.
- "--".PHP_EOL.PHP_EOL;
- $this->compressManager->write($ret);
- }
- $stmt = $this->typeAdapter->show_create_procedure($procedureName);
- foreach ($this->dbHandler->query($stmt) as $r) {
- $this->compressManager->write(
- $this->typeAdapter->create_procedure($r)
- );
- return;
- }
- }
- /**
- * Function structure extractor
- *
- * @param string $functionName Name of function to export
- * @return null
- */
- private function getFunctionStructure($functionName)
- {
- if (!$this->dumpSettings['skip-comments']) {
- $ret = "--".PHP_EOL.
- "-- Dumping routines for database '".$this->dbName."'".PHP_EOL.
- "--".PHP_EOL.PHP_EOL;
- $this->compressManager->write($ret);
- }
- $stmt = $this->typeAdapter->show_create_function($functionName);
- foreach ($this->dbHandler->query($stmt) as $r) {
- $this->compressManager->write(
- $this->typeAdapter->create_function($r)
- );
- return;
- }
- }
- /**
- * Event structure extractor
- *
- * @param string $eventName Name of event to export
- * @return null
- */
- private function getEventStructure($eventName)
- {
- if (!$this->dumpSettings['skip-comments']) {
- $ret = "--".PHP_EOL.
- "-- Dumping events for database '".$this->dbName."'".PHP_EOL.
- "--".PHP_EOL.PHP_EOL;
- $this->compressManager->write($ret);
- }
- $stmt = $this->typeAdapter->show_create_event($eventName);
- foreach ($this->dbHandler->query($stmt) as $r) {
- $this->compressManager->write(
- $this->typeAdapter->create_event($r)
- );
- return;
- }
- }
- /**
- * Prepare values for output
- *
- * @param string $tableName Name of table which contains rows
- * @param array $row Associative array of column names and values to be
- * quoted
- *
- * @return array
- */
- private function prepareColumnValues($tableName, $row)
- {
- $ret = array();
- $columnTypes = $this->tableColumnTypes[$tableName];
- foreach ($row as $colName => $colValue) {
- $colValue = $this->hookTransformColumnValue($tableName, $colName, $colValue, $row);
- $ret[] = $this->escape($colValue, $columnTypes[$colName]);
- }
- return $ret;
- }
- /**
- * Escape values with quotes when needed
- *
- * @param string $tableName Name of table which contains rows
- * @param array $row Associative array of column names and values to be quoted
- *
- * @return string
- */
- private function escape($colValue, $colType)
- {
- if (is_null($colValue)) {
- return "NULL";
- } elseif ($this->dumpSettings['hex-blob'] && $colType['is_blob']) {
- if ($colType['type'] == 'bit' || !empty($colValue)) {
- return "0x${colValue}";
- } else {
- return "''";
- }
- } elseif ($colType['is_numeric']) {
- return $colValue;
- }
- return $this->dbHandler->quote($colValue);
- }
- /**
- * Set a callable that will will be used to transform column values.
- *
- * @param callable $callable
- *
- * @return void
- */
- public function setTransformColumnValueHook($callable)
- {
- $this->transformColumnValueCallable = $callable;
- }
- /**
- * Give extending classes an opportunity to transform column values
- *
- * @param string $tableName Name of table which contains rows
- * @param string $colName Name of the column in question
- * @param string $colValue Value of the column in question
- *
- * @return string
- */
- protected function hookTransformColumnValue($tableName, $colName, $colValue, $row)
- {
- if (!$this->transformColumnValueCallable) {
- return $colValue;
- }
- return call_user_func_array($this->transformColumnValueCallable, array(
- $tableName,
- $colName,
- $colValue,
- $row
- ));
- }
- /**
- * Table rows extractor
- *
- * @param string $tableName Name of table to export
- *
- * @return null
- */
- private function listValues($tableName)
- {
- $this->prepareListValues($tableName);
- $onlyOnce = true;
- $lineSize = 0;
- // colStmt is used to form a query to obtain row values
- $colStmt = $this->getColumnStmt($tableName);
- // colNames is used to get the name of the columns when using complete-insert
- if ($this->dumpSettings['complete-insert']) {
- $colNames = $this->getColumnNames($tableName);
- }
- $stmt = "SELECT ".implode(",", $colStmt)." FROM `$tableName`";
- // Table specific conditions override the default 'where'
- $condition = $this->getTableWhere($tableName);
- if ($condition) {
- $stmt .= " WHERE {$condition}";
- }
- $limit = $this->getTableLimit($tableName);
- if ($limit) {
- $stmt .= " LIMIT {$limit}";
- }
- $resultSet = $this->dbHandler->query($stmt);
- $resultSet->setFetchMode(PDO::FETCH_ASSOC);
- $ignore = $this->dumpSettings['insert-ignore'] ? ' IGNORE' : '';
- $count = 0;
- foreach ($resultSet as $row) {
- $count++;
- $vals = $this->prepareColumnValues($tableName, $row);
- if ($onlyOnce || !$this->dumpSettings['extended-insert']) {
- if ($this->dumpSettings['complete-insert']) {
- $lineSize += $this->compressManager->write(
- "INSERT$ignore INTO `$tableName` (".
- implode(", ", $colNames).
- ") VALUES (".implode(",", $vals).")"
- );
- } else {
- $lineSize += $this->compressManager->write(
- "INSERT$ignore INTO `$tableName` VALUES (".implode(",", $vals).")"
- );
- }
- $onlyOnce = false;
- } else {
- $lineSize += $this->compressManager->write(",(".implode(",", $vals).")");
- }
- if (($lineSize > $this->dumpSettings['net_buffer_length']) ||
- !$this->dumpSettings['extended-insert']) {
- $onlyOnce = true;
- $lineSize = $this->compressManager->write(";".PHP_EOL);
- }
- }
- $resultSet->closeCursor();
- if (!$onlyOnce) {
- $this->compressManager->write(";".PHP_EOL);
- }
- $this->endListValues($tableName, $count);
- }
- /**
- * Table rows extractor, append information prior to dump
- *
- * @param string $tableName Name of table to export
- *
- * @return null
- */
- public function prepareListValues($tableName)
- {
- if (!$this->dumpSettings['skip-comments']) {
- $this->compressManager->write(
- "--".PHP_EOL.
- "-- Dumping data for table `$tableName`".PHP_EOL.
- "--".PHP_EOL.PHP_EOL
- );
- }
- if ($this->dumpSettings['single-transaction']) {
- $this->dbHandler->exec($this->typeAdapter->setup_transaction());
- $this->dbHandler->exec($this->typeAdapter->start_transaction());
- }
- if ($this->dumpSettings['lock-tables'] && !$this->dumpSettings['single-transaction']) {
- $this->typeAdapter->lock_table($tableName);
- }
- if ($this->dumpSettings['add-locks']) {
- $this->compressManager->write(
- $this->typeAdapter->start_add_lock_table($tableName)
- );
- }
- if ($this->dumpSettings['disable-keys']) {
- $this->compressManager->write(
- $this->typeAdapter->start_add_disable_keys($tableName)
- );
- }
- // Disable autocommit for faster reload
- if ($this->dumpSettings['no-autocommit']) {
- $this->compressManager->write(
- $this->typeAdapter->start_disable_autocommit()
- );
- }
- return;
- }
- /**
- * Table rows extractor, close locks and commits after dump
- *
- * @param string $tableName Name of table to export.
- * @param integer $count Number of rows inserted.
- *
- * @return void
- */
- public function endListValues($tableName, $count = 0)
- {
- if ($this->dumpSettings['disable-keys']) {
- $this->compressManager->write(
- $this->typeAdapter->end_add_disable_keys($tableName)
- );
- }
- if ($this->dumpSettings['add-locks']) {
- $this->compressManager->write(
- $this->typeAdapter->end_add_lock_table($tableName)
- );
- }
- if ($this->dumpSettings['single-transaction']) {
- $this->dbHandler->exec($this->typeAdapter->commit_transaction());
- }
- if ($this->dumpSettings['lock-tables'] && !$this->dumpSettings['single-transaction']) {
- $this->typeAdapter->unlock_table($tableName);
- }
- // Commit to enable autocommit
- if ($this->dumpSettings['no-autocommit']) {
- $this->compressManager->write(
- $this->typeAdapter->end_disable_autocommit()
- );
- }
- $this->compressManager->write(PHP_EOL);
- if (!$this->dumpSettings['skip-comments']) {
- $this->compressManager->write(
- "-- Dumped table `".$tableName."` with $count row(s)".PHP_EOL.
- '--'.PHP_EOL.PHP_EOL
- );
- }
- return;
- }
- /**
- * Build SQL List of all columns on current table which will be used for selecting
- *
- * @param string $tableName Name of table to get columns
- *
- * @return array SQL sentence with columns for select
- */
- public function getColumnStmt($tableName)
- {
- $colStmt = array();
- foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) {
- if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) {
- $colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`";
- } elseif ($colType['is_blob'] && $this->dumpSettings['hex-blob']) {
- $colStmt[] = "HEX(`${colName}`) AS `${colName}`";
- } elseif ($colType['is_virtual']) {
- $this->dumpSettings['complete-insert'] = true;
- continue;
- } else {
- $colStmt[] = "`${colName}`";
- }
- }
- return $colStmt;
- }
- /**
- * Build SQL List of all columns on current table which will be used for inserting
- *
- * @param string $tableName Name of table to get columns
- *
- * @return array columns for sql sentence for insert
- */
- public function getColumnNames($tableName)
- {
- $colNames = array();
- foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) {
- if ($colType['is_virtual']) {
- $this->dumpSettings['complete-insert'] = true;
- continue;
- } else {
- $colNames[] = "`${colName}`";
- }
- }
- return $colNames;
- }
- }
- /**
- * Enum with all available compression methods
- *
- */
- abstract class CompressMethod
- {
- public static $enums = array(
- Mysqldump::NONE,
- Mysqldump::GZIP,
- Mysqldump::BZIP2,
- Mysqldump::GZIPSTREAM,
- );
- /**
- * @param string $c
- * @return boolean
- */
- public static function isValid($c)
- {
- return in_array($c, self::$enums);
- }
- }
- abstract class CompressManagerFactory
- {
- /**
- * @param string $c
- * @return CompressBzip2|CompressGzip|CompressNone
- */
- public static function create($c)
- {
- $c = ucfirst(strtolower($c));
- if (!CompressMethod::isValid($c)) {
- throw new Exception("Compression method ($c) is not defined yet");
- }
- $method = __NAMESPACE__."\\"."Compress".$c;
- return new $method;
- }
- }
- class CompressBzip2 extends CompressManagerFactory
- {
- private $fileHandler = null;
- public function __construct()
- {
- if (!function_exists("bzopen")) {
- throw new Exception("Compression is enabled, but bzip2 lib is not installed or configured properly");
- }
- }
- /**
- * @param string $filename
- */
- public function open($filename)
- {
- $this->fileHandler = bzopen($filename, "w");
- if (false === $this->fileHandler) {
- throw new Exception("Output file is not writable");
- }
- return true;
- }
- public function write($str)
- {
- $bytesWritten = bzwrite($this->fileHandler, $str);
- if (false === $bytesWritten) {
- throw new Exception("Writting to file failed! Probably, there is no more free space left?");
- }
- return $bytesWritten;
- }
- public function close()
- {
- return bzclose($this->fileHandler);
- }
- }
- class CompressGzip extends CompressManagerFactory
- {
- private $fileHandler = null;
- public function __construct()
- {
- if (!function_exists("gzopen")) {
- throw new Exception("Compression is enabled, but gzip lib is not installed or configured properly");
- }
- }
- /**
- * @param string $filename
- */
- public function open($filename)
- {
- $this->fileHandler = gzopen($filename, "wb");
- if (false === $this->fileHandler) {
- throw new Exception("Output file is not writable");
- }
- return true;
- }
- public function write($str)
- {
- $bytesWritten = gzwrite($this->fileHandler, $str);
- if (false === $bytesWritten) {
- throw new Exception("Writting to file failed! Probably, there is no more free space left?");
- }
- return $bytesWritten;
- }
- public function close()
- {
- return gzclose($this->fileHandler);
- }
- }
- class CompressNone extends CompressManagerFactory
- {
- private $fileHandler = null;
- /**
- * @param string $filename
- */
- public function open($filename)
- {
- $this->fileHandler = fopen($filename, "wb");
- if (false === $this->fileHandler) {
- throw new Exception("Output file is not writable");
- }
- return true;
- }
- public function write($str)
- {
- $bytesWritten = fwrite($this->fileHandler, $str);
- if (false === $bytesWritten) {
- throw new Exception("Writting to file failed! Probably, there is no more free space left?");
- }
- return $bytesWritten;
- }
- public function close()
- {
- return fclose($this->fileHandler);
- }
- }
- class CompressGzipstream extends CompressManagerFactory
- {
- private $fileHandler = null;
- private $compressContext;
- /**
- * @param string $filename
- */
- public function open($filename)
- {
- $this->fileHandler = fopen($filename, "wb");
- if (false === $this->fileHandler) {
- throw new Exception("Output file is not writable");
- }
- $this->compressContext = deflate_init(ZLIB_ENCODING_GZIP, array('level' => 9));
- return true;
- }
- public function write($str)
- {
- $bytesWritten = fwrite($this->fileHandler, deflate_add($this->compressContext, $str, ZLIB_NO_FLUSH));
- if (false === $bytesWritten) {
- throw new Exception("Writting to file failed! Probably, there is no more free space left?");
- }
- return $bytesWritten;
- }
- public function close()
- {
- fwrite($this->fileHandler, deflate_add($this->compressContext, '', ZLIB_FINISH));
- return fclose($this->fileHandler);
- }
- }
- /**
- * Enum with all available TypeAdapter implementations
- *
- */
- abstract class TypeAdapter
- {
- public static $enums = array(
- "Sqlite",
- "Mysql"
- );
- /**
- * @param string $c
- * @return boolean
- */
- public static function isValid($c)
- {
- return in_array($c, self::$enums);
- }
- }
- /**
- * TypeAdapter Factory
- *
- */
- abstract class TypeAdapterFactory
- {
- protected $dbHandler = null;
- protected $dumpSettings = array();
- /**
- * @param string $c Type of database factory to create (Mysql, Sqlite,...)
- * @param PDO $dbHandler
- */
- public static function create($c, $dbHandler = null, $dumpSettings = array())
- {
- $c = ucfirst(strtolower($c));
- if (!TypeAdapter::isValid($c)) {
- throw new Exception("Database type support for ($c) not yet available");
- }
- $method = __NAMESPACE__."\\"."TypeAdapter".$c;
- return new $method($dbHandler, $dumpSettings);
- }
- public function __construct($dbHandler = null, $dumpSettings = array())
- {
- $this->dbHandler = $dbHandler;
- $this->dumpSettings = $dumpSettings;
- }
- /**
- * function databases Add sql to create and use database
- * @todo make it do something with sqlite
- */
- public function databases()
- {
- return "";
- }
- public function show_create_table($tableName)
- {
- return "SELECT tbl_name as 'Table', sql as 'Create Table' ".
- "FROM sqlite_master ".
- "WHERE type='table' AND tbl_name='$tableName'";
- }
- /**
- * function create_table Get table creation code from database
- * @todo make it do something with sqlite
- */
- public function create_table($row)
- {
- return "";
- }
- public function show_create_view($viewName)
- {
- return "SELECT tbl_name as 'View', sql as 'Create View' ".
- "FROM sqlite_master ".
- "WHERE type='view' AND tbl_name='$viewName'";
- }
- /**
- * function create_view Get view creation code from database
- * @todo make it do something with sqlite
- */
- public function create_view($row)
- {
- return "";
- }
- /**
- * function show_create_trigger Get trigger creation code from database
- * @todo make it do something with sqlite
- */
- public function show_create_trigger($triggerName)
- {
- return "";
- }
- /**
- * function create_trigger Modify trigger code, add delimiters, etc
- * @todo make it do something with sqlite
- */
- public function create_trigger($triggerName)
- {
- return "";
- }
- /**
- * function create_procedure Modify procedure code, add delimiters, etc
- * @todo make it do something with sqlite
- */
- public function create_procedure($procedureName)
- {
- return "";
- }
- /**
- * function create_function Modify function code, add delimiters, etc
- * @todo make it do something with sqlite
- */
- public function create_function($functionName)
- {
- return "";
- }
- public function show_tables()
- {
- return "SELECT tbl_name FROM sqlite_master WHERE type='table'";
- }
- public function show_views()
- {
- return "SELECT tbl_name FROM sqlite_master WHERE type='view'";
- }
- public function show_triggers()
- {
- return "SELECT name FROM sqlite_master WHERE type='trigger'";
- }
- public function show_columns()
- {
- if (func_num_args() != 1) {
- return "";
- }
- $args = func_get_args();
- return "pragma table_info(${args[0]})";
- }
- public function show_procedures()
- {
- return "";
- }
- public function show_functions()
- {
- return "";
- }
- public function show_events()
- {
- return "";
- }
- public function setup_transaction()
- {
- return "";
- }
- public function start_transaction()
- {
- return "BEGIN EXCLUSIVE";
- }
- public function commit_transaction()
- {
- return "COMMIT";
- }
- public function lock_table()
- {
- return "";
- }
- public function unlock_table()
- {
- return "";
- }
- public function start_add_lock_table()
- {
- return PHP_EOL;
- }
- public function end_add_lock_table()
- {
- return PHP_EOL;
- }
- public function start_add_disable_keys()
- {
- return PHP_EOL;
- }
- public function end_add_disable_keys()
- {
- return PHP_EOL;
- }
- public function start_disable_foreign_keys_check()
- {
- return PHP_EOL;
- }
- public function end_disable_foreign_keys_check()
- {
- return PHP_EOL;
- }
- public function add_drop_database()
- {
- return PHP_EOL;
- }
- public function add_drop_trigger()
- {
- return PHP_EOL;
- }
- public function drop_table()
- {
- return PHP_EOL;
- }
- public function drop_view()
- {
- return PHP_EOL;
- }
- /**
- * Decode column metadata and fill info structure.
- * type, is_numeric and is_blob will always be available.
- *
- * @param array $colType Array returned from "SHOW COLUMNS FROM tableName"
- * @return array
- */
- public function parseColumnType($colType)
- {
- return array();
- }
- public function backup_parameters()
- {
- return PHP_EOL;
- }
- public function restore_parameters()
- {
- return PHP_EOL;
- }
- }
- class TypeAdapterPgsql extends TypeAdapterFactory
- {
- }
- class TypeAdapterDblib extends TypeAdapterFactory
- {
- }
- class TypeAdapterSqlite extends TypeAdapterFactory
- {
- }
- class TypeAdapterMysql extends TypeAdapterFactory
- {
- const DEFINER_RE = 'DEFINER=`(?:[^`]|``)*`@`(?:[^`]|``)*`';
- // Numerical Mysql types
- public $mysqlTypes = array(
- 'numerical' => array(
- 'bit',
- 'tinyint',
- 'smallint',
- 'mediumint',
- 'int',
- 'integer',
- 'bigint',
- 'real',
- 'double',
- 'float',
- 'decimal',
- 'numeric'
- ),
- 'blob' => array(
- 'tinyblob',
- 'blob',
- 'mediumblob',
- 'longblob',
- 'binary',
- 'varbinary',
- 'bit',
- 'geometry', /* http://bugs.mysql.com/bug.php?id=43544 */
- 'point',
- 'linestring',
- 'polygon',
- 'multipoint',
- 'multilinestring',
- 'multipolygon',
- 'geometrycollection',
- )
- );
- public function databases()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- $databaseName = $args[0];
- $resultSet = $this->dbHandler->query("SHOW VARIABLES LIKE 'character_set_database';");
- $characterSet = $resultSet->fetchColumn(1);
- $resultSet->closeCursor();
- $resultSet = $this->dbHandler->query("SHOW VARIABLES LIKE 'collation_database';");
- $collationDb = $resultSet->fetchColumn(1);
- $resultSet->closeCursor();
- $ret = "";
- $ret .= "CREATE DATABASE /*!32312 IF NOT EXISTS*/ `${databaseName}`".
- " /*!40100 DEFAULT CHARACTER SET ${characterSet} ".
- " COLLATE ${collationDb} */;".PHP_EOL.PHP_EOL.
- "USE `${databaseName}`;".PHP_EOL.PHP_EOL;
- return $ret;
- }
- public function show_create_table($tableName)
- {
- return "SHOW CREATE TABLE `$tableName`";
- }
- public function show_create_view($viewName)
- {
- return "SHOW CREATE VIEW `$viewName`";
- }
- public function show_create_trigger($triggerName)
- {
- return "SHOW CREATE TRIGGER `$triggerName`";
- }
- public function show_create_procedure($procedureName)
- {
- return "SHOW CREATE PROCEDURE `$procedureName`";
- }
- public function show_create_function($functionName)
- {
- return "SHOW CREATE FUNCTION `$functionName`";
- }
- public function show_create_event($eventName)
- {
- return "SHOW CREATE EVENT `$eventName`";
- }
- public function create_table($row)
- {
- if (!isset($row['Create Table'])) {
- throw new Exception("Error getting table code, unknown output");
- }
- $createTable = $row['Create Table'];
- if ($this->dumpSettings['reset-auto-increment']) {
- $match = "/AUTO_INCREMENT=[0-9]+/s";
- $replace = "";
- $createTable = preg_replace($match, $replace, $createTable);
- }
- $ret = "/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL.
- "/*!40101 SET character_set_client = ".$this->dumpSettings['default-character-set']." */;".PHP_EOL.
- $createTable.";".PHP_EOL.
- "/*!40101 SET character_set_client = @saved_cs_client */;".PHP_EOL.
- PHP_EOL;
- return $ret;
- }
- public function create_view($row)
- {
- $ret = "";
- if (!isset($row['Create View'])) {
- throw new Exception("Error getting view structure, unknown output");
- }
- $viewStmt = $row['Create View'];
- $definerStr = $this->dumpSettings['skip-definer'] ? '' : '/*!50013 \2 */'.PHP_EOL;
- if ($viewStmtReplaced = preg_replace(
- '/^(CREATE(?:\s+ALGORITHM=(?:UNDEFINED|MERGE|TEMPTABLE))?)\s+('
- .self::DEFINER_RE.'(?:\s+SQL SECURITY DEFINER|INVOKER)?)?\s+(VIEW .+)$/',
- '/*!50001 \1 */'.PHP_EOL.$definerStr.'/*!50001 \3 */',
- $viewStmt,
- 1
- )) {
- $viewStmt = $viewStmtReplaced;
- };
- $ret .= $viewStmt.';'.PHP_EOL.PHP_EOL;
- return $ret;
- }
- public function create_trigger($row)
- {
- $ret = "";
- if (!isset($row['SQL Original Statement'])) {
- throw new Exception("Error getting trigger code, unknown output");
- }
- $triggerStmt = $row['SQL Original Statement'];
- $definerStr = $this->dumpSettings['skip-definer'] ? '' : '/*!50017 \2*/ ';
- if ($triggerStmtReplaced = preg_replace(
- '/^(CREATE)\s+('.self::DEFINER_RE.')?\s+(TRIGGER\s.*)$/s',
- '/*!50003 \1*/ '.$definerStr.'/*!50003 \3 */',
- $triggerStmt,
- 1
- )) {
- $triggerStmt = $triggerStmtReplaced;
- }
- $ret .= "DELIMITER ;;".PHP_EOL.
- $triggerStmt.";;".PHP_EOL.
- "DELIMITER ;".PHP_EOL.PHP_EOL;
- return $ret;
- }
- public function create_procedure($row)
- {
- $ret = "";
- if (!isset($row['Create Procedure'])) {
- throw new Exception("Error getting procedure code, unknown output. ".
- "Please check 'https://bugs.mysql.com/bug.php?id=14564'");
- }
- $procedureStmt = $row['Create Procedure'];
- if ( $this->dumpSettings['skip-definer'] ) {
- if ($procedureStmtReplaced = preg_replace(
- '/^(CREATE)\s+('.self::DEFINER_RE.')?\s+(PROCEDURE\s.*)$/s',
- '\1 \3',
- $procedureStmt,
- 1
- )) {
- $procedureStmt = $procedureStmtReplaced;
- }
- }
- $ret .= "/*!50003 DROP PROCEDURE IF EXISTS `".
- $row['Procedure']."` */;".PHP_EOL.
- "/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL.
- "/*!40101 SET character_set_client = ".$this->dumpSettings['default-character-set']." */;".PHP_EOL.
- "DELIMITER ;;".PHP_EOL.
- $procedureStmt." ;;".PHP_EOL.
- "DELIMITER ;".PHP_EOL.
- "/*!40101 SET character_set_client = @saved_cs_client */;".PHP_EOL.PHP_EOL;
- return $ret;
- }
- public function create_function($row)
- {
- $ret = "";
- if (!isset($row['Create Function'])) {
- throw new Exception("Error getting function code, unknown output. ".
- "Please check 'https://bugs.mysql.com/bug.php?id=14564'");
- }
- $functionStmt = $row['Create Function'];
- $characterSetClient = $row['character_set_client'];
- $collationConnection = $row['collation_connection'];
- $sqlMode = $row['sql_mode'];
- if ( $this->dumpSettings['skip-definer'] ) {
- if ($functionStmtReplaced = preg_replace(
- '/^(CREATE)\s+('.self::DEFINER_RE.')?\s+(FUNCTION\s.*)$/s',
- '\1 \3',
- $functionStmt,
- 1
- )) {
- $functionStmt = $functionStmtReplaced;
- }
- }
- $ret .= "/*!50003 DROP FUNCTION IF EXISTS `".
- $row['Function']."` */;".PHP_EOL.
- "/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL.
- "/*!50003 SET @saved_cs_results = @@character_set_results */ ;".PHP_EOL.
- "/*!50003 SET @saved_col_connection = @@collation_connection */ ;".PHP_EOL.
- "/*!40101 SET character_set_client = ".$characterSetClient." */;".PHP_EOL.
- "/*!40101 SET character_set_results = ".$characterSetClient." */;".PHP_EOL.
- "/*!50003 SET collation_connection = ".$collationConnection." */ ;".PHP_EOL.
- "/*!50003 SET @saved_sql_mode = @@sql_mode */ ;;".PHP_EOL.
- "/*!50003 SET sql_mode = '".$sqlMode."' */ ;;".PHP_EOL.
- "/*!50003 SET @saved_time_zone = @@time_zone */ ;;".PHP_EOL.
- "/*!50003 SET time_zone = 'SYSTEM' */ ;;".PHP_EOL.
- "DELIMITER ;;".PHP_EOL.
- $functionStmt." ;;".PHP_EOL.
- "DELIMITER ;".PHP_EOL.
- "/*!50003 SET sql_mode = @saved_sql_mode */ ;".PHP_EOL.
- "/*!50003 SET character_set_client = @saved_cs_client */ ;".PHP_EOL.
- "/*!50003 SET character_set_results = @saved_cs_results */ ;".PHP_EOL.
- "/*!50003 SET collation_connection = @saved_col_connection */ ;".PHP_EOL.
- "/*!50106 SET TIME_ZONE= @saved_time_zone */ ;".PHP_EOL.PHP_EOL;
- return $ret;
- }
- public function create_event($row)
- {
- $ret = "";
- if (!isset($row['Create Event'])) {
- throw new Exception("Error getting event code, unknown output. ".
- "Please check 'http://stackoverflow.com/questions/10853826/mysql-5-5-create-event-gives-syntax-error'");
- }
- $eventName = $row['Event'];
- $eventStmt = $row['Create Event'];
- $sqlMode = $row['sql_mode'];
- $definerStr = $this->dumpSettings['skip-definer'] ? '' : '/*!50117 \2*/ ';
- if ($eventStmtReplaced = preg_replace(
- '/^(CREATE)\s+('.self::DEFINER_RE.')?\s+(EVENT .*)$/',
- '/*!50106 \1*/ '.$definerStr.'/*!50106 \3 */',
- $eventStmt,
- 1
- )) {
- $eventStmt = $eventStmtReplaced;
- }
- $ret .= "/*!50106 SET @save_time_zone= @@TIME_ZONE */ ;".PHP_EOL.
- "/*!50106 DROP EVENT IF EXISTS `".$eventName."` */;".PHP_EOL.
- "DELIMITER ;;".PHP_EOL.
- "/*!50003 SET @saved_cs_client = @@character_set_client */ ;;".PHP_EOL.
- "/*!50003 SET @saved_cs_results = @@character_set_results */ ;;".PHP_EOL.
- "/*!50003 SET @saved_col_connection = @@collation_connection */ ;;".PHP_EOL.
- "/*!50003 SET character_set_client = utf8 */ ;;".PHP_EOL.
- "/*!50003 SET character_set_results = utf8 */ ;;".PHP_EOL.
- "/*!50003 SET collation_connection = utf8_general_ci */ ;;".PHP_EOL.
- "/*!50003 SET @saved_sql_mode = @@sql_mode */ ;;".PHP_EOL.
- "/*!50003 SET sql_mode = '".$sqlMode."' */ ;;".PHP_EOL.
- "/*!50003 SET @saved_time_zone = @@time_zone */ ;;".PHP_EOL.
- "/*!50003 SET time_zone = 'SYSTEM' */ ;;".PHP_EOL.
- $eventStmt." ;;".PHP_EOL.
- "/*!50003 SET time_zone = @saved_time_zone */ ;;".PHP_EOL.
- "/*!50003 SET sql_mode = @saved_sql_mode */ ;;".PHP_EOL.
- "/*!50003 SET character_set_client = @saved_cs_client */ ;;".PHP_EOL.
- "/*!50003 SET character_set_results = @saved_cs_results */ ;;".PHP_EOL.
- "/*!50003 SET collation_connection = @saved_col_connection */ ;;".PHP_EOL.
- "DELIMITER ;".PHP_EOL.
- "/*!50106 SET TIME_ZONE= @save_time_zone */ ;".PHP_EOL.PHP_EOL;
- // Commented because we are doing this in restore_parameters()
- // "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;" . PHP_EOL . PHP_EOL;
- return $ret;
- }
- public function show_tables()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "SELECT TABLE_NAME AS tbl_name ".
- "FROM INFORMATION_SCHEMA.TABLES ".
- "WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='${args[0]}'";
- }
- public function show_views()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "SELECT TABLE_NAME AS tbl_name ".
- "FROM INFORMATION_SCHEMA.TABLES ".
- "WHERE TABLE_TYPE='VIEW' AND TABLE_SCHEMA='${args[0]}'";
- }
- public function show_triggers()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "SHOW TRIGGERS FROM `${args[0]}`;";
- }
- public function show_columns()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "SHOW COLUMNS FROM `${args[0]}`;";
- }
- public function show_procedures()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "SELECT SPECIFIC_NAME AS procedure_name ".
- "FROM INFORMATION_SCHEMA.ROUTINES ".
- "WHERE ROUTINE_TYPE='PROCEDURE' AND ROUTINE_SCHEMA='${args[0]}'";
- }
- public function show_functions()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "SELECT SPECIFIC_NAME AS function_name ".
- "FROM INFORMATION_SCHEMA.ROUTINES ".
- "WHERE ROUTINE_TYPE='FUNCTION' AND ROUTINE_SCHEMA='${args[0]}'";
- }
- /**
- * Get query string to ask for names of events from current database.
- *
- * @param string Name of database
- * @return string
- */
- public function show_events()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "SELECT EVENT_NAME AS event_name ".
- "FROM INFORMATION_SCHEMA.EVENTS ".
- "WHERE EVENT_SCHEMA='${args[0]}'";
- }
- public function setup_transaction()
- {
- return "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ";
- }
- public function start_transaction()
- {
- return "START TRANSACTION " .
- "/*!40100 WITH CONSISTENT SNAPSHOT */";
- }
- public function commit_transaction()
- {
- return "COMMIT";
- }
- public function lock_table()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return $this->dbHandler->exec("LOCK TABLES `${args[0]}` READ LOCAL");
- }
- public function unlock_table()
- {
- return $this->dbHandler->exec("UNLOCK TABLES");
- }
- public function start_add_lock_table()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "LOCK TABLES `${args[0]}` WRITE;".PHP_EOL;
- }
- public function end_add_lock_table()
- {
- return "UNLOCK TABLES;".PHP_EOL;
- }
- public function start_add_disable_keys()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "/*!40000 ALTER TABLE `${args[0]}` DISABLE KEYS */;".
- PHP_EOL;
- }
- public function end_add_disable_keys()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "/*!40000 ALTER TABLE `${args[0]}` ENABLE KEYS */;".
- PHP_EOL;
- }
- public function start_disable_autocommit()
- {
- return "SET autocommit=0;".PHP_EOL;
- }
- public function end_disable_autocommit()
- {
- return "COMMIT;".PHP_EOL;
- }
- public function add_drop_database()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "/*!40000 DROP DATABASE IF EXISTS `${args[0]}`*/;".
- PHP_EOL.PHP_EOL;
- }
- public function add_drop_trigger()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "DROP TRIGGER IF EXISTS `${args[0]}`;".PHP_EOL;
- }
- public function drop_table()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "DROP TABLE IF EXISTS `${args[0]}`;".PHP_EOL;
- }
- public function drop_view()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "DROP TABLE IF EXISTS `${args[0]}`;".PHP_EOL.
- "/*!50001 DROP VIEW IF EXISTS `${args[0]}`*/;".PHP_EOL;
- }
- public function getDatabaseHeader()
- {
- $this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__);
- $args = func_get_args();
- return "--".PHP_EOL.
- "-- Current Database: `${args[0]}`".PHP_EOL.
- "--".PHP_EOL.PHP_EOL;
- }
- /**
- * Decode column metadata and fill info structure.
- * type, is_numeric and is_blob will always be available.
- *
- * @param array $colType Array returned from "SHOW COLUMNS FROM tableName"
- * @return array
- */
- public function parseColumnType($colType)
- {
- $colInfo = array();
- $colParts = explode(" ", $colType['Type']);
- if ($fparen = strpos($colParts[0], "(")) {
- $colInfo['type'] = substr($colParts[0], 0, $fparen);
- $colInfo['length'] = str_replace(")", "", substr($colParts[0], $fparen + 1));
- $colInfo['attributes'] = isset($colParts[1]) ? $colParts[1] : null;
- } else {
- $colInfo['type'] = $colParts[0];
- }
- $colInfo['is_numeric'] = in_array($colInfo['type'], $this->mysqlTypes['numerical']);
- $colInfo['is_blob'] = in_array($colInfo['type'], $this->mysqlTypes['blob']);
- // for virtual columns that are of type 'Extra', column type
- // could by "STORED GENERATED" or "VIRTUAL GENERATED"
- // MySQL reference: https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html
- $colInfo['is_virtual'] = strpos($colType['Extra'], "VIRTUAL GENERATED") !== false || strpos($colType['Extra'], "STORED GENERATED") !== false;
- return $colInfo;
- }
- public function backup_parameters()
- {
- $ret = "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;".PHP_EOL.
- "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;".PHP_EOL.
- "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;".PHP_EOL.
- "/*!40101 SET NAMES ".$this->dumpSettings['default-character-set']." */;".PHP_EOL;
- if (false === $this->dumpSettings['skip-tz-utc']) {
- $ret .= "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;".PHP_EOL.
- "/*!40103 SET TIME_ZONE='+00:00' */;".PHP_EOL;
- }
- $ret .= "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;".PHP_EOL.
- "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;".PHP_EOL.
- "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;".PHP_EOL.
- "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;".PHP_EOL.PHP_EOL;
- return $ret;
- }
- public function restore_parameters()
- {
- $ret = "";
- if (false === $this->dumpSettings['skip-tz-utc']) {
- $ret .= "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;".PHP_EOL;
- }
- $ret .= "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;".PHP_EOL.
- "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;".PHP_EOL.
- "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;".PHP_EOL.
- "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;".PHP_EOL.
- "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;".PHP_EOL.
- "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;".PHP_EOL.
- "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;".PHP_EOL.PHP_EOL;
- return $ret;
- }
- /**
- * Check number of parameters passed to function, useful when inheriting.
- * Raise exception if unexpected.
- *
- * @param integer $num_args
- * @param integer $expected_num_args
- * @param string $method_name
- */
- private function check_parameters($num_args, $expected_num_args, $method_name)
- {
- if ($num_args != $expected_num_args) {
- throw new Exception("Unexpected parameter passed to $method_name");
- }
- return;
- }
- }
|