Mage_Backup_Model_Mysql4_Db Class Reference

List of all members.

Public Member Functions

 __construct ()
 crear ()
 getTables ()
 getTableDropSql ($tableName)
 getTableCreateSql ($tableName, $withForeignKeys=false)
 getTableForeignKeysSql ($tableName=null)
 getTableStatus ($tableName)
 getTableDataSql ($tableName, $count, $offset=0)
 getTableCreateScript ($tableName, $addDropIfExists=false)
 getTableHeader ($tableName)
 getTableDataDump ($tableName, $step=100)
 getHeader ()
 getFooter ()
 getTableDataBeforeSql ($tableName)
 getTableDataAfterSql ($tableName)
 beginTransaction ()
 commitTransaction ()
 rollBackTransaction ()

Protected Member Functions

 _quoteRow ($tableName, array $row)

Protected Attributes

 $_read
 $_foreignKeys = array()


Detailed Description

Definition at line 34 of file Db.php.


Constructor & Destructor Documentation

__construct (  ) 

Initialize Backup DB resource model

Definition at line 55 of file Db.php.

00056     {
00057         $this->_read = Mage::getSingleton('core/resource')->getConnection('backup_read');
00058     }


Member Function Documentation

_quoteRow ( tableName,
array row 
) [protected]

Quote Table Row

Parameters:
string $tableName
array $row
Returns:
string

Definition at line 192 of file Db.php.

00193     {
00194         $describe = $this->_read->describeTable($tableName);
00195         $rowData = array();
00196         foreach ($row as $k => $v) {
00197             if (is_null($v)) {
00198                 $value = 'NULL';
00199             }
00200             elseif (in_array(strtolower($describe[$k]['DATA_TYPE']), array('bigint','mediumint','smallint','tinyint'))) {
00201                 $value = $v;
00202             }
00203             else {
00204                 $value = $this->_read->quoteInto('?', $v);
00205             }
00206             $rowData[] = $value;
00207         }
00208         return '('.join(',', $rowData).')';
00209     }

beginTransaction (  ) 

Definition at line 391 of file Db.php.

00392     {
00393         $this->_read->beginTransaction();
00394     }

commitTransaction (  ) 

Definition at line 396 of file Db.php.

00397     {
00398         $this->_read->commit();
00399     }

crear (  ) 

Clear data

Definition at line 64 of file Db.php.

00065     {
00066         $this->_foreignKeys = array();
00067     }

getFooter (  ) 

Returns SQL footer data

Definition at line 348 of file Db.php.

00349     {
00350         $footer = "\n/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n"
00351             . "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; \n"
00352             . "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n"
00353             . "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n"
00354             . "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n"
00355             . "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n"
00356             . "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n"
00357             . "\n-- Dump completed on " . Mage::getSingleton('core/date')->gmtDate() . " GMT";
00358 
00359         return $footer;
00360     }

getHeader (  ) 

Returns SQL header data

Definition at line 322 of file Db.php.

00323     {
00324         $dbConfig = $this->_read->getConfig();
00325 
00326         $versionRow = $this->_read->fetchRow('SHOW VARIABLES LIKE \'version\'');
00327 
00328         $header = "-- Magento DB backup\n"
00329             . "--\n"
00330             . "-- Host: {$dbConfig['host']}    Database: {$dbConfig['dbname']}\n"
00331             . "-- ------------------------------------------------------\n"
00332             . "-- Server version: {$versionRow['Value']}\n\n"
00333             . "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n"
00334             . "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n"
00335             . "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n"
00336             . "/*!40101 SET NAMES utf8 */;\n"
00337             . "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n"
00338             . "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n"
00339             . "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n"
00340             . "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n";
00341 
00342         return $header;
00343     }

getTableCreateScript ( tableName,
addDropIfExists = false 
)

Enter description here...

Parameters:
unknown_type $tableName
unknown_type $addDropIfExists
Returns:
unknown

Definition at line 253 of file Db.php.

00254     {
00255         $script = '';
00256         if ($this->_read) {
00257             $quotedTableName = $this->_read->quoteIdentifier($tableName);
00258 
00259             if ($addDropIfExists) {
00260                 $script .= 'DROP TABLE IF EXISTS ' . $quotedTableName .";\n";
00261             }
00262             $sql = 'SHOW CREATE TABLE ' . $quotedTableName;
00263             $data = $this->_read->fetchRow($sql);
00264             $script.= isset($data['Create Table']) ? $data['Create Table'].";\n" : '';
00265         }
00266 
00267         return $script;
00268     }

getTableCreateSql ( tableName,
withForeignKeys = false 
)

Retrieve SQL fragment for create table

Parameters:
string $tableName
bool $withForeignKeys
Returns:
string

Definition at line 98 of file Db.php.

00099     {
00100         $quotedTableName = $this->_read->quoteIdentifier($tableName);
00101         $sql = 'SHOW CREATE TABLE ' . $quotedTableName;
00102         $row = $this->_read->fetchRow($sql);
00103 
00104         if (!$row) {
00105             return false;
00106         }
00107 
00108         $regExp  = '/,\s+CONSTRAINT `([^`]*)` FOREIGN KEY \(`([^`]*)`\) '
00109             . 'REFERENCES `([^`]*)` \(`([^`]*)`\)'
00110             . '( ON DELETE (RESTRICT|CASCADE|SET NULL|NO ACTION))?'
00111             . '( ON UPDATE (RESTRICT|CASCADE|SET NULL|NO ACTION))?/';
00112         $matches = array();
00113         preg_match_all($regExp, $row['Create Table'], $matches, PREG_SET_ORDER);
00114 
00115         foreach ($matches as $match) {
00116             $this->_foreignKeys[$tableName][] = sprintf('ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s%s',
00117                 $this->_read->quoteIdentifier($match[1]),
00118                 $this->_read->quoteIdentifier($match[2]),
00119                 $this->_read->quoteIdentifier($match[3]),
00120                 $this->_read->quoteIdentifier($match[4]),
00121                 isset($match[5]) ? $match[5] : '',
00122                 isset($match[7]) ? $match[7] : ''
00123             );
00124         }
00125 
00126         if ($withForeignKeys) {
00127             return $row['Create Table'] . ';';
00128         }
00129         else {
00130             return preg_replace($regExp, '', $row['Create Table']) . ';';
00131         }
00132     }

getTableDataAfterSql ( tableName  ) 

Retrieve after insert data SQL fragment

Parameters:
string $tableName
Returns:
string

Definition at line 384 of file Db.php.

00385     {
00386         $quotedTableName = $this->_read->quoteIdentifier($tableName);
00387         return "/*!40000 ALTER TABLE {$quotedTableName} ENABLE KEYS */;\n"
00388             . "UNLOCK TABLES;\n";
00389     }

getTableDataBeforeSql ( tableName  ) 

Retrieve before insert data SQL fragment

Parameters:
string $tableName
Returns:
string

Definition at line 368 of file Db.php.

00369     {
00370         $quotedTableName = $this->_read->quoteIdentifier($tableName);
00371         return "\n--\n"
00372             . "-- Dumping data for table {$quotedTableName}\n"
00373             . "--\n\n"
00374             . "LOCK TABLES {$quotedTableName} WRITE;\n"
00375             . "/*!40000 ALTER TABLE {$quotedTableName} DISABLE KEYS */;\n";
00376     }

getTableDataDump ( tableName,
step = 100 
)

Definition at line 283 of file Db.php.

00284     {
00285         $sql = '';
00286         if ($this->_read) {
00287             $quotedTableName = $this->_read->quoteIdentifier($tableName);
00288             $colunms = $this->_read->fetchRow('SELECT * FROM '.$quotedTableName.' LIMIT 1');
00289             if ($colunms) {
00290                 $arrSql = array();
00291 
00292                 $colunms = array_keys($colunms);
00293                 $quote = $this->_read->getQuoteIdentifierSymbol();
00294                 $sql = 'INSERT INTO ' . $quotedTableName . ' (' .$quote . implode($quote.', '.$quote,$colunms).$quote.')';
00295                 $sql.= ' VALUES ';
00296 
00297                 $startRow = 0;
00298                 $select = $this->_read->select();
00299                 $select->from($tableName)
00300                     ->limit($step, $startRow);
00301                 while ($data = $this->_read->fetchAll($select)) {
00302                     $dataSql = array();
00303                     foreach ($data as $row) {
00304                         $dataSql[] = $this->_read->quoteInto('(?)', $row);
00305                     }
00306                     $arrSql[] = $sql.implode(', ', $dataSql).';';
00307                     $startRow += $step;
00308                     $select->limit($step, $startRow);
00309                 }
00310 
00311                 $sql = implode("\n", $arrSql)."\n";
00312             }
00313 
00314         }
00315 
00316         return $sql;
00317     }

getTableDataSql ( tableName,
count,
offset = 0 
)

Retrive table partical data SQL insert

Parameters:
string $tableName
int $count
int $offset
Returns:
string

Definition at line 219 of file Db.php.

00220     {
00221         $sql = null;
00222         $quotedTableName = $this->_read->quoteIdentifier($tableName);
00223         $select = $this->_read->select()
00224             ->from($tableName)
00225             ->limit($count, $offset);
00226         $query  = $this->_read->query($select);
00227 
00228         while ($row = $query->fetch()) {
00229             if (is_null($sql)) {
00230                 $sql = 'INSERT INTO ' . $quotedTableName . ' VALUES ';
00231             }
00232             else {
00233                 $sql .= ',';
00234             }
00235 
00236             $sql .= $this->_quoteRow($tableName, $row);
00237         }
00238 
00239         if (!is_null($sql)) {
00240             $sql .= ';' . "\n";
00241         }
00242 
00243         return $sql;
00244     }

getTableDropSql ( tableName  ) 

Retrieve SQL fragment for drop table

Parameters:
string $tableName
Returns:
string

Definition at line 85 of file Db.php.

00086     {
00087         $quotedTableName = $this->_read->quoteIdentifier($tableName);
00088         return 'DROP TABLE IF EXISTS ' . $quotedTableName . ';';
00089     }

getTableForeignKeysSql ( tableName = null  ) 

Retrieve foreign keys for table(s)

Parameters:
string|null $tableName
Returns:
string

Definition at line 140 of file Db.php.

00141     {
00142         if (is_null($tableName)) {
00143             $sql = '';
00144             foreach ($this->_foreignKeys as $table => $foreignKeys) {
00145                 $sql .= sprintf("ALTER TABLE %s\n  %s;\n",
00146                     $this->_read->quoteIdentifier($table),
00147                     join(",\n  ", $foreignKeys)
00148                 );
00149             }
00150             return $sql;
00151         }
00152         if (isset($this->_foreignKeys[$tableName]) && ($foreignKeys = $this->_foreignKeys[$tableName])) {
00153 
00154         }
00155         return false;
00156     }

getTableHeader ( tableName  ) 

Retrieve table header comment

Returns:
string

Definition at line 275 of file Db.php.

00276     {
00277         $quotedTableName = $this->_read->quoteIdentifier($tableName);
00278         return "\n--\n"
00279             . "-- Table structure for table {$quotedTableName}\n"
00280             . "--\n\n";
00281     }

getTables (  ) 

Retrieve table list

Returns:
array

Definition at line 74 of file Db.php.

00075     {
00076         return $this->_read->listTables();
00077     }

getTableStatus ( tableName  ) 

Retrieve table status

Parameters:
string $tableName
Returns:
Varien_Object

Definition at line 164 of file Db.php.

00165     {
00166         $sql = $this->_read->quoteInto('SHOW TABLE STATUS LIKE ?', $tableName);
00167         $row = $this->_read->fetchRow($sql);
00168 
00169         if ($row) {
00170             $statusObject = new Varien_Object();
00171             $statusObject->setIdFieldName('name');
00172             foreach ($row as $field => $value) {
00173                 $statusObject->setData(strtolower($field), $value);
00174             }
00175 
00176             $cntRow = $this->_read->fetchRow( $this->_read->select()->from($tableName, 'COUNT(*) as rows'));
00177             $statusObject->setRows($cntRow['rows']);
00178 
00179             return $statusObject;
00180         }
00181 
00182         return false;
00183     }

rollBackTransaction (  ) 

Definition at line 401 of file Db.php.

00402     {
00403         $this->_read->rollBack();
00404     }


Member Data Documentation

$_foreignKeys = array() [protected]

Definition at line 49 of file Db.php.

$_read [protected]

Definition at line 41 of file Db.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:33 2009 for Magento by  doxygen 1.5.8