예제 #1
0
 /**
  * Merges the incoming structure definition with the existing structure.
  *
  * @return  void
  *
  * @since   11.1
  * 
  * @throws  Exception on error.
  * @note    Currently supports XML format only.
  * @todo    IF it is not in XML format, convert it first.
  */
 protected function mergeStructure()
 {
     // Currently only support XML format anyway.
     // Initialise variables.
     $prefix = $this->db->getPrefix();
     $tables = $this->db->getTableList();
     $result = true;
     if ($this->from instanceof SimpleXMLElement) {
         $xml = $this->from;
     } else {
         $xml = new SimpleXMLElement($this->from);
     }
     // Get all the table definitions.
     $xmlTables = $xml->xpath('database/table_structure');
     foreach ($xmlTables as $table) {
         // Convert the magic prefix into the real table name.
         $tableName = (string) $table['name'];
         $tableName = preg_replace('|^#__|', $prefix, $tableName);
         if (in_array($tableName, $tables)) {
             // The table already exists. Now check if there is any difference.
             if ($queries = $this->getAlterTableSQL($xml->database->table_structure)) {
                 // Run the queries to upgrade the data structure.
                 foreach ($queries as $query) {
                     $this->db->setQuery((string) $query);
                     if (!$this->db->query()) {
                         $this->addLog('Fail: ' . $this->db->getQuery());
                         throw new Exception($this->db->getErrorMsg());
                     } else {
                         $this->addLog('Pass: '******'Fail: ' . $this->db->getQuery());
                 throw new Exception($this->db->getErrorMsg());
             } else {
                 $this->addLog('Pass: ' . $this->db->getQuery());
             }
         }
     }
 }
예제 #2
0
 /**
  * Execute the query
  *
  * @return mixed database resource or <var>false</var>.
  */
 public function query()
 {
     $this->count++;
     return $this->db->query();
 }
 public function query()
 {
     $sql = is_a($this->sql, "JDatabaseQueryMySQLi") ? (string) $this->sql : $this->sql;
     if ($this->skipjf) {
         return parent::query();
     }
     if ($this->translate === false || !stristr($sql, 'SELECT')) {
         return parent::query();
     }
     $jfmCount = 0;
     $jfManager = JoomFishManager::getInstance();
     $defaultlang = $jfManager->getDefaultLanguage();
     if (is_a($this->sql, "JDatabaseQueryMySQLi") && !isset($this->sql->jfprocessed) && ($this->sql->where !== null && is_a($this->sql->where, "JDatabaseQueryElement"))) {
         $elements = $this->sql->where->getElements();
         foreach ($elements as &$element) {
             if (strstr($element, 'language')) {
                 //str_ireplace("\,\'\*\'", "\,\'\*\',\'".$defaultlang."'", $value);
                 $element = str_ireplace(",'*'", ",'*','" . $defaultlang . "'", $element);
                 $jfmCount++;
             }
         }
         $this->sql->clear('where');
         $this->sql->where($elements);
     }
     $tablewithprefix = $this->getTableName();
     $table = str_ireplace('#__', '', $tablewithprefix);
     //$key = $jfManager->getPrimaryKey($table);   obsolete
     $ce = $jfManager->getContentElement($table);
     $key = is_object($ce) ? $ce->getReferenceId() : false;
     $isnative = is_object($ce) && $ce->getTarget() == 'native' ? true : false;
     // last check - do we have content element for this and is this table native??
     if ($key === false || $isnative === false) {
         return parent::query();
     }
     if ($this->translate === true && is_a($this->sql, "JDatabaseQueryMySQLi") && !isset($this->sql->jfprocessed) && ($this->sql->select !== null && is_a($this->sql->select, "JDatabaseQueryElement"))) {
         $elements2 = $this->sql->select->getElements();
         $keyfound = false;
         $asfound = false;
         foreach ($elements2 as $element2) {
             // is there id column or * in the select query
             if (stristr($element2, ' ' . $key . ' ') || stristr($element2, ',' . $key) || stristr($element2, ' ' . $key . ',') || strstr($element2, '*') || stristr($element2, '.' . $key . ',') || stristr($element2, ' ' . $key . ' ,') || stristr($element2, '.' . $key . ' ,')) {
                 $keyfound = true;
             }
             // is any column named as soemthing else or has table prepended, then do join
             if (preg_match('/FROM(.*?) AS/is', $element2) || stristr($element2, '.')) {
                 $asfound = true;
             }
         }
         if ($keyfound === false && $asfound == true) {
             // OUR JOIN NEEDS TO BE FIRST FOR "USING" TO WORK PROPERLY
             if (is_array($this->sql->join)) {
                 $joinelements = array();
                 $joinprevious = $this->sql->join;
                 $this->sql->clear('join');
                 $this->sql->innerJoin($tablewithprefix . ' AS jfself USING (' . $key . ')');
                 foreach ($joinprevious as $joinprev) {
                     $name = JoomlaProtectedFixDatabaseQueryElement::getInstance($joinprev)->get('name');
                     $type = str_ireplace(' JOIN', '', $name);
                     $elements = $joinprev->getElements();
                     $this->sql->join($type, $elements);
                 }
             } else {
                 $this->sql->innerJoin($tablewithprefix . ' AS jfself USING (' . $key . ')');
             }
             $this->sql->select('jfself.' . $key . ' AS ' . $key);
             $jfmCount++;
         } else {
             if ($keyfound === false && $asfound === false) {
                 $this->sql->select($key);
                 $jfmCount++;
             }
         }
     } else {
         if ($this->translate === true && is_string($this->sql) && !isset($this->sql->jfprocessed) && stristr($this->sql, 'SELECT') && stristr($this->sql, 'FROM')) {
             $keyfound = false;
             $asfound = false;
             preg_match('/SELECT(.*?)FROM/is', $this->sql, $selectstring);
             $selectstring = $selectstring[0];
             if (stristr($selectstring, ' ' . $key . ' ') || stristr($selectstring, ',' . $key) || stristr($selectstring, ' ' . $key . ',') || strstr($selectstring, '*') || stristr($selectstring, '.' . $key . ',') || stristr($selectstring, ' ' . $key . ' ,') || stristr($selectstring, '.' . $key . ' ,')) {
                 $keyfound = true;
             }
             if (preg_match('/FROM(.*?) AS/is', $selectstring) || stristr($selectstring, '.')) {
                 $asfound = true;
             }
             if ($keyfound === false && $asfound == true) {
                 //$this->sql = preg_replace('/SELECT(.*?)FROM/i', 'SELECT '. $selectstring . ', jfself.'.$key.' AS '.$key .' FROM', $this->sql  );
                 $this->sql = preg_replace('/SELECT /i', 'SELECT jfself.' . $key . ' AS ' . $key . ', ', $this->sql, 1);
                 //make sure our join comes first
                 $replacejoin = preg_match('/( left| right| inner| outer| join| where)/i', $this->sql, $matches);
                 if ($replacejoin) {
                     $this->sql = preg_replace('/( left| right| inner| outer| join| where)/i', ' JOIN ' . $tablewithprefix . ' AS jfself USING (' . $key . ') ' . $matches[0], $this->sql, 1);
                 } else {
                     $this->sql .= ' JOIN ' . $tablewithprefix . ' AS jfself USING (' . $key . ') ';
                 }
                 $jfmCount++;
             } else {
                 if ($keyfound === false && $asfound === false) {
                     //$this->sql = preg_replace('/SELECT /i', 'SELECT '.$key. ', ', $this->sql, 1);
                     $this->sql = preg_replace('/ FROM/is', ',' . $key . ' FROM', $this->sql, 1);
                     $jfmCount++;
                 }
             }
         }
     }
     // NEW SYSTEM disabled for now - the query handling for joins etc. is too complex
     /*if (false && is_a($this->sql, "JDatabaseQuery") && !isset($this->sql->jfprocessed))
     		{
     			// Do the from first
     			$sql = $this->replacePrefix((string) $this->sql);
     			//$jfManager = JoomFishManager::getInstance();
     			//$contentElements = $jfManager->getContentElements( );
     			// search for
     			// AND a.language in \(.*,\*\) using regexp !
     			// Before joomfish manager is created since we can't translate so skip this anaylsis
     			$jfManager = JoomFishManager::getInstance();
     			if (!$jfManager)
     				return;
     			$language = false;
     			if (isset($jfManager))
     			{
     				$this->setLanguage($language);
     			}
     			$from = $this->sql->from;
     			$joins = $this->sql->join;
     			if ($from || $join)
     			{
     				$joinElements = array();
     				if ($joins){
     					foreach ($joins as $join) {
     						 $joinElements = array_merge($joinElements, $join->getElements() );
     					}
     				}
     				$fromElements = $from->getElements();
     				if ($fromElements)
     				{
     					foreach ($fromElements as $fromElement)
     					{
     						// remove surplus spaces
     						$fromElement = preg_replace('/\s{2}/', '', $fromElement);
     						$fromElement = preg_replace('/' . $this->getPrefix() . '/', '', $fromElement);
     						$fromElement = preg_replace('/#__/', '', $fromElement);
     						$parts = explode(" ", $fromElement);
     						$table = trim($parts[0]);
     						//if ($this->translatedContentAvailable($table))
     						// TODO need new translatedContentAvailable method !
     						// This is the mapping table method!!
     						// NEW SYSTEM
     						if (in_array($table,array("menu", "content", "modules",  "categories")))
     						//if (in_array($table,array("content",  "categories")))
     						{
     							$alias = trim($parts[count($parts) - 1]);
     							$jfalias = 'jftm' . $jfmCount;
     							$jfmCount++;
     							// TODO needs to get primary key for this table not assume it is id
     							$this->sql->leftJoin("#__jf_translationmap AS $jfalias ON $jfalias.reference_table = " . $this->quote($table) . "   AND $jfalias.reference_id = $alias.id AND $jfalias.language= " . $this->quote($language));
     							$this->sql->where(" $jfalias.reference_id IS NULL ");
     						}
     					}
     				}
     			}
     			if ($jfmCount>0){
     				$this->sql->jfprocessed = true;
     			}
     		} */
     $sqlfinal = is_a($this->sql, "JDatabaseQueryMySQLi") ? (string) $this->sql : $this->sql;
     $result = parent::query();
     /*if ($jfmCount>0){
     			$this->sql->jfprocessed = true;
     		}*/
     return $result;
 }
예제 #4
0
 /**
  * Test the JDatabaseMySQLi::query() method
  *
  * @return  void
  *
  * @since   11.4
  */
 public function testQuery()
 {
     $this->object->setQuery("REPLACE INTO `jos_dbtest` SET `id` = 5, `title` = 'testTitle'");
     $this->assertThat($this->object->query(), $this->isTrue(), __LINE__);
     $this->assertThat($this->object->insertid(), $this->equalTo(5), __LINE__);
 }