Ejemplo n.º 1
0
 /**
  * Returns the test database connection.
  *
  * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
  */
 protected function getConnection()
 {
     if ($this->_connectionMock == null) {
         $connection = Zend_Db::factory('Pdo_Sqlite', array('dbname' => ':memory:', 'username' => 'test'));
         $sql = file_get_contents(str_replace('.php', '.sql', $this->getTemplateFileName()));
         foreach (MUtil_Parser_Sql_WordsParser::splitStatements($sql, false) as $sqlCommand) {
             $stmt = $connection->query($sqlCommand);
         }
         $this->_connectionMock = $this->createZendDbConnection($connection, 'zfunittests');
         Zend_Db_Table_Abstract::setDefaultAdapter($connection);
     }
     return $this->_connectionMock;
 }
Ejemplo n.º 2
0
 public function testSplitAllString()
 {
     $result = MUtil_Parser_Sql_WordsParser::splitStatements($this->_sql, false, true);
     $this->assertCount(2, $result);
     $this->assertEquals($result, $this->_sqlOutputString);
 }
Ejemplo n.º 3
0
 /**
  * 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 boolean $includeResultSets
  * @return string
  */
 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 {
                 if (isset($data['db'])) {
                     $db = $data['db'];
                 } else {
                     $db = $this->defaultDb;
                     // Lookup using location
                     if (isset($data['location'])) {
                         foreach ($this->directories as $path) {
                             if ($path['name'] === $data['location']) {
                                 $db = $path['db'];
                                 break;
                             }
                         }
                     }
                 }
                 $stmt = $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 . ': ' . $sql;
             }
             $i++;
         }
     } else {
         $results[] = sprintf($this->_('No script for %1$s.'), $data['name']);
     }
     return $results;
 }
 /**
  * Load all patches from a single patch file
  *
  * @param string $file Full filename
  * @param string $location Location description
  * @param int $minimumLevel
  * @param int $maximumLevel
  */
 private function _loadPatchFile($file, $location, $minimumLevel, $maximumLevel)
 {
     if ($sql = file_get_contents($file)) {
         $levels = preg_split('/--\\s*(GEMS\\s+)?VERSION:?\\s*/', $sql);
         // SQL before first -- VERSION: is ignored.
         array_shift($levels);
         foreach ($levels as $level) {
             list($levelnrtext, $leveltext) = explode("\n", $level, 2);
             // no loading of unused patches
             $levelnr = intval($levelnrtext);
             if ($levelnr && $levelnr >= $minimumLevel && $levelnr <= $maximumLevel) {
                 $patches = preg_split('/--\\s*PATCH:?\\s*/', $leveltext);
                 // SQL before first -- PATCH: is ignored.
                 array_shift($patches);
                 foreach ($patches as $patch) {
                     // First line now contains patch name
                     list($name, $statements) = explode("\n", $patch, 2);
                     $name = substr(trim($name), 0, 30);
                     // \MUtil_Echo::r($statements, $name);
                     foreach (\MUtil_Parser_Sql_WordsParser::splitStatements($statements, false) as $i => $statement) {
                         $this->_loaded_patches[] = array('gpa_level' => $levelnr, 'gpa_location' => $location, 'gpa_name' => $name, 'gpa_order' => $i, 'gpa_sql' => $statement);
                     }
                 }
             }
         }
     }
 }