From: <gem...@li...> - 2012-02-02 13:59:12
|
Revision: 444 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=444&view=rev Author: mennodekker Date: 2012-02-02 13:59:06 +0000 (Thu, 02 Feb 2012) Log Message: ----------- Extracted _runScript and _runScripts methods to the DbaModel to allow usage outside the DatabaseAction Modified Paths: -------------- branches/1.5.x/library/classes/Gems/Default/DatabaseAction.php branches/1.5.x/library/classes/Gems/Model/DbaModel.php Modified: branches/1.5.x/library/classes/Gems/Default/DatabaseAction.php =================================================================== --- branches/1.5.x/library/classes/Gems/Default/DatabaseAction.php 2012-02-02 12:39:24 UTC (rev 443) +++ branches/1.5.x/library/classes/Gems/Default/DatabaseAction.php 2012-02-02 13:59:06 UTC (rev 444) @@ -48,50 +48,6 @@ { public $sortKey = array('group' => SORT_ASC, 'type' => SORT_ASC, 'name' => SORT_ASC); - private function _runScript(array $data, $includeResultSets = false) - { - $results = array(); - $this->_runScripts($data, $results, $includeResultSets); - return $results; - } - - private function _runScripts(array $data, array &$results, $includeResultSets = false) - { - if ($data['script']) { - $queries = MUtil_Parser_Sql_WordsParser::splitStatements($data['script'], false); - $qCount = count($queries); - - $results[] = sprintf($this->_('Executed %2$s creation script %1$s:'), $data['name'], $this->_(strtolower($data['type']))); - $i = 1; - $resultSet = 1; - - foreach ($queries as $query) { - $sql = (string) $query; - try { - $stmt = $this->db->query($sql); - if ($rows = $stmt->rowCount()) { - if ($includeResultSets && ($data = $stmt->fetchAll())) { - $results[] = sprintf($this->_('%d record(s) returned as result set %d in step %d of %d.'), $rows, $resultSet, $i, $qCount); - $results[] = $data; - $resultSet++; - } else { - $results[] = sprintf($this->_('%d record(s) updated in step %d of %d.'), $rows, $i, $qCount); - } - } else { - $results[] = sprintf($this->_('Script ran step %d of %d succesfully.'), $i, $qCount); - } - } catch (Zend_Db_Statement_Exception $e) { - $results[] = $e->getMessage() . $this->_(' in step ') . $i . ':<pre>' . $sql . '</pre>'; - } - $i++; - } - } else { - $results[] = sprintf($this->_('No script for %1$s.'), $data['name']); - } - - return $results; - } - /** * Set the parameters needed by the menu. * @@ -168,10 +124,7 @@ $model->set('location', 'label', $this->_('Location')); } // $model->set('path', 'label', $this->_('Path')); - $model->set('state', 'label', $this->_('Status'), 'multiOptions', array( - Gems_Model_DbaModel::STATE_CREATED => $this->_('created'), - Gems_Model_DbaModel::STATE_DEFINED => $this->_('not created'), - Gems_Model_DbaModel::STATE_UNKNOWN => $this->_('unknown'))); + $model->set('state', 'label', $this->_('Status')); if ($detailed) { $model->set('script', 'label', $this->_('Script'), 'itemDisplay', 'pre'); @@ -482,7 +435,7 @@ $model = $this->getModel(); $data = $model->loadFirst(); - $results = $this->_runScript($data); + $results = $model->runScript($data); $this->addMessage($results); $this->_reroute(array('action' => 'show')); @@ -497,11 +450,13 @@ if ($this->_getParam('confirmed')) { if ($objects) { + $results = array(); $results[] = sprintf($this->_('Starting %d object creation scripts.'), $oCount) . '<br/>'; $i = 1; foreach ($objects as $data) { - $this->_runScripts($data, $results); + $result = $model->runScript($data); + $results = array_merge($results, $result); $results[] = sprintf($this->_('Finished %s creation script for object %d of %d'), $this->_(strtolower($data['type'])), $i, $oCount) . '<br/>'; $i++; } @@ -559,7 +514,8 @@ $data['name'] = ''; $data['type'] = $this->_('raw'); - $results = $this->_runScript($data, true); + $model = $this->getModel(); + $results = $model->runScript($data, true); $resultSet = 1; $echos = MUtil_Html::create()->array(); foreach ($results as $result) { Modified: branches/1.5.x/library/classes/Gems/Model/DbaModel.php =================================================================== --- branches/1.5.x/library/classes/Gems/Model/DbaModel.php 2012-02-02 12:39:24 UTC (rev 443) +++ branches/1.5.x/library/classes/Gems/Model/DbaModel.php 2012-02-02 13:59:06 UTC (rev 444) @@ -60,6 +60,10 @@ protected $file_encoding; protected $locations; protected $mainDirectory; + /** + * @var Zend_Translate_Adapter + */ + protected $translate; private $_sorts; @@ -73,6 +77,9 @@ $this->db = $db; + //Grab translate object from the Escort + $this->translate = GemsEscort::getInstance()->translate; + $this->set('group', 'maxlength', 40, 'type', MUtil_Model::TYPE_STRING); $this->set('name', 'key', true, 'maxlength', 40, 'type', MUtil_Model::TYPE_STRING); $this->set('type', 'maxlength', 40, 'type', MUtil_Model::TYPE_STRING); @@ -86,8 +93,25 @@ $this->set('script', 'type', MUtil_Model::TYPE_STRING); $this->set('lastChanged', 'type', MUtil_Model::TYPE_DATETIME); $this->set('location', 'maxlength', 12, 'type', MUtil_Model::TYPE_STRING); + $this->set('state', 'multiOptions', array( + Gems_Model_DbaModel::STATE_CREATED => $this->_('created'), + Gems_Model_DbaModel::STATE_DEFINED => $this->_('not created'), + Gems_Model_DbaModel::STATE_UNKNOWN => $this->_('unknown'))); } + /** + * proxy for easy access to translations + * + * @param string $messageId Translation string + * @param string|Zend_Locale $locale (optional) Locale/Language to use, identical with locale + * identifier, @see Zend_Locale for more information + * @return string + */ + private function _($messageId, $locale = null) + { + return $this->translate->_($messageId, $locale); + } + private function _getGroupName($name) { if ($pos = strpos($name, '__')) { @@ -374,6 +398,56 @@ return $this->loadFirst(array('name' => $tableName), false); } + /** + * Run a sql statement from an object loaded through this model + * + * $data is an array with the following keys: + * script The sql statement to be executed + * name The name of the table, used in messages + * type Type of db element (table or view), used in messages + * + * @param array $data + * @param type $includeResultSets + * @return type + */ + public function runScript(array $data, $includeResultSets = false) + { + $results = array(); + if ($data['script']) { + $queries = MUtil_Parser_Sql_WordsParser::splitStatements($data['script'], false); + $qCount = count($queries); + + $results[] = sprintf($this->_('Executed %2$s creation script %1$s:'), $data['name'], $this->_(strtolower($data['type']))); + $i = 1; + $resultSet = 1; + + foreach ($queries as $query) { + $sql = (string) $query; + try { + $stmt = $this->db->query($sql); + if ($rows = $stmt->rowCount()) { + if ($includeResultSets && ($data = $stmt->fetchAll())) { + $results[] = sprintf($this->_('%d record(s) returned as result set %d in step %d of %d.'), $rows, $resultSet, $i, $qCount); + $results[] = $data; + $resultSet++; + } else { + $results[] = sprintf($this->_('%d record(s) updated in step %d of %d.'), $rows, $i, $qCount); + } + } else { + $results[] = sprintf($this->_('Script ran step %d of %d succesfully.'), $i, $qCount); + } + } catch (Zend_Db_Statement_Exception $e) { + $results[] = $e->getMessage() . $this->_(' in step ') . $i . ':<pre>' . $sql . '</pre>'; + } + $i++; + } + } else { + $results[] = sprintf($this->_('No script for %1$s.'), $data['name']); + } + + return $results; + } + public function save(array $newValues, array $filter = null) { // TODO: Save of data This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |