1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Backup
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Database backup resource model
30: *
31: * @category Mage
32: * @package Mage_Backup
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Backup_Model_Resource_Db
36: {
37: /**
38: * Database connection adapter
39: *
40: * @var Varien_Db_Adapter_Pdo_Mysql
41: */
42: protected $_write;
43:
44: /**
45: * tables Foreign key data array
46: * [tbl_name] = array(create foreign key strings)
47: *
48: * @var array
49: */
50: protected $_foreignKeys = array();
51:
52: /**
53: * Initialize Backup DB resource model
54: *
55: */
56: public function __construct()
57: {
58: $this->_write = Mage::getSingleton('core/resource')->getConnection('backup_write');
59: }
60:
61: /**
62: * Enter description here ...
63: *
64: * @deprecated after 1.4.0.0-alpha2
65: *
66: */
67: public function crear()
68: {
69: $this->clear();
70: }
71:
72: /**
73: * Clear data
74: *
75: */
76: public function clear()
77: {
78: $this->_foreignKeys = array();
79: }
80:
81: /**
82: * Retrieve table list
83: *
84: * @return array
85: */
86: public function getTables()
87: {
88: return $this->_write->listTables();
89: }
90:
91: /**
92: * Retrieve SQL fragment for drop table
93: *
94: * @param string $tableName
95: * @return string
96: */
97: public function getTableDropSql($tableName)
98: {
99: return Mage::getResourceHelper('backup')->getTableDropSql($tableName);
100: }
101:
102: /**
103: * Retrieve SQL fragment for create table
104: *
105: * @param string $tableName
106: * @param bool $withForeignKeys
107: * @return string
108: */
109: public function getTableCreateSql($tableName, $withForeignKeys = false)
110: {
111: return Mage::getResourceHelper('backup')->getTableCreateSql($tableName, $withForeignKeys = false);
112: }
113:
114: /**
115: * Retrieve foreign keys for table(s)
116: *
117: * @param string|null $tableName
118: * @return string
119: */
120: public function getTableForeignKeysSql($tableName = null)
121: {
122: $fkScript = '';
123: if (!$tableName) {
124: $tables = $this->getTables();
125: foreach($tables as $table) {
126: $tableFkScript = Mage::getResourceHelper('backup')->getTableForeignKeysSql($table);
127: if (!empty($tableFkScript)) {
128: $fkScript .= "\n" . $tableFkScript;
129: }
130: }
131: } else {
132: $fkScript = $this->getTableForeignKeysSql($tableName);
133: }
134: return $fkScript;
135: }
136:
137: /**
138: * Retrieve table status
139: *
140: * @param string $tableName
141: * @return Varien_Object
142: */
143: public function getTableStatus($tableName)
144: {
145: $row = $this->_write->showTableStatus($tableName);
146:
147: if ($row) {
148: $statusObject = new Varien_Object();
149: $statusObject->setIdFieldName('name');
150: foreach ($row as $field => $value) {
151: $statusObject->setData(strtolower($field), $value);
152: }
153:
154: $cntRow = $this->_write->fetchRow(
155: $this->_write->select()->from($tableName, 'COUNT(1) as rows'));
156: $statusObject->setRows($cntRow['rows']);
157:
158: return $statusObject;
159: }
160:
161: return false;
162: }
163:
164: /**
165: * Quote Table Row
166: *
167: * @deprecated
168: *
169: * @param string $tableName
170: * @param array $row
171: * @return string
172: */
173: protected function _quoteRow($tableName, array $row)
174: {
175: return $row;
176: }
177:
178: /**
179: * Retrive table partical data SQL insert
180: *
181: * @param string $tableName
182: * @param int $count
183: * @param int $offset
184: * @return string
185: */
186: public function getTableDataSql($tableName, $count = null, $offset = null)
187: {
188: return Mage::getResourceHelper('backup')->getPartInsertSql($tableName, $count, $offset);
189: }
190:
191: /**
192: * Enter description here...
193: *
194: * @param unknown_type $tableName
195: * @param unknown_type $addDropIfExists
196: * @return unknown
197: */
198: public function getTableCreateScript($tableName, $addDropIfExists = false)
199: {
200: return Mage::getResourceHelper('backup')->getTableCreateScript($tableName, $addDropIfExists);;
201: }
202:
203: /**
204: * Retrieve table header comment
205: *
206: * @param unknown_type $tableName
207: * @return string
208: */
209: public function getTableHeader($tableName)
210: {
211: $quotedTableName = $this->_write->quoteIdentifier($tableName);
212: return "\n--\n"
213: . "-- Table structure for table {$quotedTableName}\n"
214: . "--\n\n";
215: }
216:
217: /**
218: * Return table data dump
219: *
220: * @param string $tableName
221: * @param bool $step
222: * @return string
223: */
224: public function getTableDataDump($tableName, $step = false)
225: {
226: return $this->getTableDataSql($tableName);
227: }
228:
229: /**
230: * Returns SQL header data
231: *
232: * @return string
233: */
234: public function getHeader()
235: {
236: return Mage::getResourceHelper('backup')->getHeader();
237: }
238:
239: /**
240: * Returns SQL footer data
241: *
242: * @return string
243: */
244: public function getFooter()
245: {
246: return Mage::getResourceHelper('backup')->getFooter();
247: }
248:
249: /**
250: * Retrieve before insert data SQL fragment
251: *
252: * @param string $tableName
253: * @return string
254: */
255: public function getTableDataBeforeSql($tableName)
256: {
257: return Mage::getResourceHelper('backup')->getTableDataBeforeSql($tableName);
258: }
259:
260: /**
261: * Retrieve after insert data SQL fragment
262: *
263: * @param string $tableName
264: * @return string
265: */
266: public function getTableDataAfterSql($tableName)
267: {
268: return Mage::getResourceHelper('backup')->getTableDataAfterSql($tableName);
269: }
270:
271: /**
272: * Start transaction mode
273: *
274: * @return Mage_Backup_Model_Resource_Db
275: */
276: public function beginTransaction()
277: {
278: Mage::getResourceHelper('backup')->turnOnSerializableMode();
279: $this->_write->beginTransaction();
280: return $this;
281: }
282:
283: /**
284: * Commit transaction
285: *
286: * @return Mage_Backup_Model_Resource_Db
287: */
288: public function commitTransaction()
289: {
290: $this->_write->commit();
291: Mage::getResourceHelper('backup')->turnOnReadCommittedMode();
292: return $this;
293: }
294:
295: /**
296: * Rollback transaction
297: *
298: * @return Mage_Backup_Model_Resource_Db
299: */
300: public function rollBackTransaction()
301: {
302: $this->_write->rollBack();
303: return $this;
304: }
305:
306: /**
307: * Run sql code
308: *
309: * @param $command
310: * @return Mage_Backup_Model_Resource_Db
311: */
312: public function runCommand($command){
313: $this->_write->query($command);
314: return $this;
315: }
316: }
317: