/** * This function replaces a string identifier <var>$prefix</var> with the * string held is the <var>_table_prefix</var> class variable. * * @param string The SQL query * @param string The common table prefix * * @return string */ public function replacePrefix($sql, $prefix = '#__') { $app = JFactory::getApplication(); $package = $app->getUserStateFromRequest('com_fabrik.package', 'package', 'fabrik', 'cmd'); $sql = str_replace('{package}', $package, $sql); return parent::replacePrefix($sql, $prefix); }
/** * Execute the SQL statement. * * @param boolean $replacePrefixQuotes Replace the prefixes inside the quotes too * * @return mixed A database cursor resource on success, boolean false on failure. * * @since 12.1 * @throws RuntimeException */ public function execute($replacePrefixQuotes = false) { if ($replacePrefixQuotes) { $this->sql = $this->replacePrefix((string) $this->sql, '#__', $replacePrefixQuotes); } return parent::execute(); }
/** * Get the generic name of the table, converting the database prefix to the wildcard string. * * @param string $table The name of the table. * * @return string The name of the table with the database prefix replaced with #__. * @since 11.1 */ protected function getGenericTableName($table) { // TODO Incorporate into parent class and use $this. $prefix = $this->db->getPrefix(); // Replace the magic prefix if found. $table = preg_replace("|^{$prefix}|", '#__', $table); return $table; }
/** * Build where like conditions from strings with spaces * @param string $value * @param string $fieldName * @return string */ protected function _buildLikeBySpaces($value, $fieldName) { $values = $this->_separateValue($value); foreach ($values as $key => $value) { $values[$key] = $this->_db->quote('%' . $value . '%'); } return '(' . $fieldName . ' LIKE ' . implode(' AND ' . $fieldName . ' LIKE ', $values) . ' )'; }
/** * Load log types and prepare them as an array with options. * * <code> * $filters = new CrowdFundingFilters(JFactory::getDbo()); * $options = $filters->getLogTypes(); * </code> * * @return array */ public function getLogTypes() { $query = $this->db->getQuery(true); $query->select("a.type AS value, a.type AS text")->from($this->db->quoteName("#__crowdf_logs", "a"))->group("a.type"); $this->db->setQuery($query); $types = $this->db->loadAssocList(); if (!$types) { $types = array(); } return $types; }
/** * Returns all rows of given table * @param string $table * @throws SPException * @return array */ public function getColumns($table) { static $cache = array(); if (!isset($cache[$table])) { $this->setQuery("SHOW COLUMNS FROM {$table}"); try { $cache[$table] = $this->loadResultArray(); } catch (Exception $e) { } if ($this->db->getErrorNum()) { throw new SPException($this->db->stderr()); } } return $cache[$table]; }
/** * 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()); } } } }
/** * Execute a batch query * * @abstract * @access public * @return mixed A database resource if successful, FALSE if not. */ function query_batch($abort_on_error = true, $p_transaction_safe = false) { return parent::queryBatch($abort_on_error, $p_transaction_safe); }
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; }
/** * Test Test method - there really isn't a lot to test here, but * this is present for the sake of completeness */ public function testTest() { $this->assertThat(JDatabaseMySQLi::test(), $this->isTrue(), __LINE__); }