Exemplo n.º 1
0
 /**
  * Constructor of SGMysqldump. Note that in the case of an SQLite database
  * connection, the filename must be in the $db parameter.
  *
  * @param string $db         Database name
  * @param string $type       SQL database type
  * @param array  $dumpSettings SQL database settings
  */
 public function __construct($dbHandler, $db = '', $type = 'mysql', $dumpSettings = array())
 {
     $dumpSettingsDefault = array('include-tables' => array(), 'exclude-tables' => array(), 'compress' => SGMysqldump::NONE, 'no-data' => false, 'add-drop-table' => false, 'single-transaction' => true, 'lock-tables' => true, 'add-locks' => true, 'extended-insert' => true, 'disable-keys' => true, 'where' => '', 'no-create-info' => false, 'skip-triggers' => false, 'add-drop-trigger' => true, 'hex-blob' => true, 'databases' => false, 'add-drop-database' => false, 'skip-tz-utz' => false, 'no-autocommit' => true, 'default-character-set' => SGMysqldump::UTF8, 'skip-comments' => false, 'skip-dump-date' => false, 'disable-foreign-keys-check' => true);
     $this->db = $db;
     $this->dbHandler = $dbHandler;
     $this->dbType = strtolower($type);
     $this->dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings);
     $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault));
     if (count($diff) > 0) {
         throw new Exception("Unexpected value in dumpSettings: (" . implode(",", $diff) . ")");
     }
     // Create a new compressManager to manage compressed output
     $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']);
 }
Exemplo n.º 2
0
 /**
  * Main call
  *
  * @param string $filename  Name of file to write sql dump to
  * @return bool
  */
 public function start($filename = '')
 {
     // Output file can be redefined here
     if (!empty($filename)) {
         $this->fileName = $filename;
     }
     // We must set a name to continue
     if (empty($this->fileName)) {
         throw new \Exception("Output file name is not set", 1);
     }
     // Connect to database
     $this->connect();
     // Create a new compressManager to manage compressed output
     $this->compressManager = CompressManagerFactory::create($this->settings['compress']);
     if (!$this->compressManager->open($this->fileName)) {
         throw new \Exception("Output file is not writable", 2);
     }
     // Formating dump file
     $this->compressManager->write($this->getHeader());
     // Listing all tables from database
     $this->tables = array();
     foreach ($this->dbHandler->query("SHOW TABLES") as $row) {
         if (empty($this->settings['include-tables']) || !empty($this->settings['include-tables']) && in_array(current($row), $this->settings['include-tables'], true)) {
             array_push($this->tables, current($row));
         }
     }
     // Exporting tables one by one
     foreach ($this->tables as $table) {
         if (in_array($table, $this->settings['exclude-tables'], true)) {
             continue;
         }
         $is_table = $this->getTableStructure($table);
         if (true === $is_table && false === $this->settings['no-data']) {
             $this->listValues($table);
         }
     }
     // Exporting views one by one
     foreach ($this->views as $view) {
         $this->compressManager->write($view);
     }
     $this->compressManager->close();
 }
 /**
  * Main call
  *
  * @param string $filename  Name of file to write sql dump to
  * @return bool
  */
 public function start($filename = '')
 {
     // Output file can be redefined here
     if (!empty($filename)) {
         $this->fileName = $filename;
     }
     // We must set a name to continue
     if (empty($this->fileName)) {
         throw new Exception("Output file name is not set", 1);
     }
     // Connect to database
     $this->connect();
     // Create a new compressManager to manage compressed output
     $this->_compressManager = CompressManagerFactory::create($this->_settings['compress']);
     if (!$this->_compressManager->open($this->fileName)) {
         throw new Exception("Output file is not writable", 2);
     }
     // Formating dump file
     $this->_compressManager->write($this->getHeader());
     if ($this->_settings['add-drop-database']) {
         $this->_compressManager->write($this->_typeAdapter->add_drop_database($this->db, $this->_dbHandler));
     }
     // Listing all tables from database
     $this->_tables = array();
     if (empty($this->_settings['include-tables'])) {
         // include all tables for now, blacklisting happens later
         foreach ($this->_dbHandler->query($this->_typeAdapter->show_tables($this->db)) 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->db)) as $row) {
             if (in_array(current($row), $this->_settings['include-tables'], true)) {
                 array_push($this->_tables, current($row));
                 unset($this->_settings['include-tables'][array_search(current($row), $this->_settings['include-tables'])]);
             }
         }
     }
     // If there still are some tables in include-tables array, that means
     // that some tables weren't found. Give proper error and exit.
     if (0 < count($this->_settings['include-tables'])) {
         throw new Exception("Table (" . implode(",", $this->_settings['include-tables']) . ") not found in database", 4);
     }
     // Disable checking foreign keys
     if ($this->_settings['disable-foreign-keys-check']) {
         $this->_compressManager->write($this->_typeAdapter->start_disable_foreign_keys_check());
     }
     // Exporting tables one by one
     foreach ($this->_tables as $table) {
         if (in_array($table, $this->_settings['exclude-tables'], true)) {
             continue;
         }
         $is_table = $this->getTableStructure($table);
         if (true === $is_table && false === $this->_settings['no-data']) {
             $this->listValues($table);
         }
     }
     // Exporting views one by one
     foreach ($this->_views as $view) {
         $this->_compressManager->write($view);
     }
     // Enable checking foreign keys if needed
     if ($this->_settings['disable-foreign-keys-check']) {
         $this->_compressManager->write($this->_typeAdapter->end_disable_foreign_keys_check());
     }
     $this->_compressManager->close();
 }
Exemplo n.º 4
0
 /**
  * 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, 'no-data' => false, 'add-drop-table' => false, 'single-transaction' => true, 'lock-tables' => true, 'add-locks' => true, 'extended-insert' => true, 'complete-insert' => false, 'disable-keys' => true, 'where' => '', 'no-create-info' => false, 'skip-triggers' => false, 'add-drop-trigger' => true, 'routines' => false, 'hex-blob' => true, 'databases' => false, 'add-drop-database' => false, 'skip-tz-utc' => false, 'no-autocommit' => true, 'default-character-set' => Mysqldump::UTF8, 'skip-comments' => false, 'skip-dump-date' => false, 'init_commands' => array(), 'disable-foreign-keys-check' => true);
     $pdoSettingsDefault = array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false);
     $this->user = $user;
     $this->pass = $pass;
     $this->parseDsn($dsn);
     $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']);
 }