/** * Filter by field. * * @param string $field Field name. * @param string $operation Operation (>|>=|<|<=|=|IN|NOT IN) * @param string|array $value Value. * * @return $this */ public function where($field, $operation, $value) { $operation = strtoupper($operation); switch ($operation) { case '>': case '>=': case '<': case '<=': case '=': $this->query->where("{$this->db->quoteName($field)} {$operation} {$this->db->quote($value)}"); break; case 'BETWEEN': list($a, $b) = (array) $value; $this->query->where("{$this->db->quoteName($field)} BETWEEN {$this->db->quote($a)} AND {$this->db->quote($b)}"); break; case 'IN': case 'NOT IN': $value = (array) $value; if (empty($value)) { // WHERE field IN (nothing). $this->query->where('0'); } else { $list = implode(',', $value); $this->query->where("{$this->db->quoteName($field)} {$operation} ({$list})"); } break; } return $this; }
private function Former_AddChange(&$data) { $existing = $this->getExistingRecords(array(), "_former"); foreach ($data["Former_AddChange"] as $inn => $item) { if (isset($existing[$inn])) { if ($this->obsolete($item, $existing)) { continue; } $id = $existing[$inn]->id; } else { $id = 0; } $inserts[] = "(" . $id . "," . $this->_db->quote($item["name"]) . "," . $this->_db->quote($item["inn"]) . "," . $this->_db->quote($item["adding_date"]) . "," . $item["cause"] . ")"; if ($this->met > 0 && microtime(true) - $this->started > $this->met) { $this->setError("COM_SRO_RUNTIME_LIMIT"); break; } } if (count($inserts)) { $sql = "REPLACE INTO {$this->tbl_former} (id,name,inn,adding_date,cause) " . " VALUES " . implode(",", $inserts); $this->_db->setQuery($sql); if (!$this->_db->query()) { $this->setError("Ошибка при обновлении:" . $this->_db->getErrorMsg()); } } unset($data["Former_AddChange"]); }
/** * Method to quote and optionally escape a string to database requirements for insertion into the database. * @param string $text The string to quote. * @param bool $escape True to escape the string, false to leave it unchanged. * @return string The quoted input string. */ public function quote($text, $escape = true) { if (version_compare(JVERSION, '1.5.0', '>')) { $escape = false; } else { $escape = true; } return $this->db->quote($escape ? $this->db->escape($text) : $text); }
function jotcache_upgrade(JDatabase $db) { $message = ''; $query = $db->getQuery(true); $query->select('COUNT(*)')->from('#__jotcache_exclude')->where('type=1'); $tplex_count = $db->setQuery($query)->loadResult(); if ($tplex_count == 0) { return false; } $query->clear('where'); $query->where('type=4'); $count = $db->setQuery($query)->loadResult(); if ($count == 0) { $query->clear('select')->clear('where'); $query->select($db->quoteName('value'))->from($db->quoteName('#__template_styles', 's'))->where('name=s.id')->where('type=1')->order('s.home'); $defs = $db->setQuery($query)->loadResultArray(); $positions = array(); foreach ($defs as $def) { $def_array = unserialize($def); $positions = array_merge($positions, $def_array); } $query->clear(); $query->select('position')->from('#__modules')->where('client_id = 0')->where('published = 1')->where('position <>' . $db->quote(''))->group('position')->order('position'); $db->setQuery($query); $items = $db->loadResultArray(); $cleaned_positions = array(); foreach ($items as $item) { if (array_key_exists($item, $positions)) { $cleaned_positions[$item] = $positions[$item]; } } $defs = serialize($cleaned_positions); $query->clear(); $query->insert('#__jotcache_exclude')->columns('name,value,type')->values('1,' . $db->quote($defs) . ',4'); if ($db->setQuery($query)->query()) { $message = "TABLE #__jotcache_exclude has been upgraded. Check JotCache TPL exclude definitions for correct values."; } else { JError::raiseNotice(100, $db->getErrorMsg()); } return $message; } }
/** * Get a quoted database escaped string (or array of strings) * * @param string|array $text * @param boolean $escape * @return string */ public function Quote($text, $escape = true) { if (is_array($text)) { // CMS 2.5 doesn't support arrays: foreach ($text as $k => $v) { $text[$k] = $this->Quote($v, $escape); } return $text; } return $this->_db->quote($text, $escape); }
/** * @param $field * @param $data */ protected function stringMatch($field, $data) { $wheres = array(); foreach ($data as $match) { $match = trim($match); if (!empty($match)) { $wheres[] = $field . ' LIKE ' . $this->db->quote('%' . $this->db->escape($match, true) . '%'); } } if (!empty($wheres)) { $this->filter_where[] = '(' . implode(' OR ', $wheres) . ')'; } }
/** * Generic check for whether dependencies exist for this object in the database schema * * Can be overloaded/supplemented by the child class * * @param mixed $pk An optional primary key value check the row for. If not * set the instance property value is used. * @param array $joins An optional array to compiles standard joins formatted like: * [label => 'Label', name => 'table name' , idfield => 'field', joinfield => 'field'] * * @return boolean True on success. * * @deprecated 12.1 * @link http://docs.joomla.org/JTable/canDelete * @since 11.1 */ public function canDelete($pk = null, $joins = null) { // Deprecation warning. JLog::add('JTable::canDelete() is deprecated.', JLog::WARNING, 'deprecated'); // Initialise variables. $k = $this->_tbl_key; $pk = is_null($pk) ? $this->{$k} : $pk; // If no primary key is given, return false. if ($pk === null) { return false; } if (is_array($joins)) { // Get a query object. $query = $this->_db->getQuery(true); // Setup the basic query. $query->select($this->_db->quoteName($this->_tbl_key)); $query->from($this->_db->quoteName($this->_tbl)); $query->where($this->_db->quoteName($this->_tbl_key) . ' = ' . $this->_db->quote($this->{$k})); $query->group($this->_db->quoteName($this->_tbl_key)); // For each join add the select and join clauses to the query object. foreach ($joins as $table) { $query->select('COUNT(DISTINCT ' . $table['idfield'] . ') AS ' . $table['idfield']); $query->join('LEFT', $table['name'] . ' ON ' . $table['joinfield'] . ' = ' . $k); } // Get the row object from the query. $this->_db->setQuery((string) $query, 0, 1); $row = $this->_db->loadObject(); // Check for a database error. if ($this->_db->getErrorNum()) { $this->setError($this->_db->getErrorMsg()); return false; } $msg = array(); $i = 0; foreach ($joins as $table) { $k = $table['idfield'] . $i; if ($row->{$k}) { $msg[] = JText::_($table['label']); } $i++; } if (count($msg)) { $this->setError("noDeleteRecord" . ": " . implode(', ', $msg)); return false; } else { return true; } } return true; }
/** * Tests the JDatabase::quote method. * * @return void * * @since 11.4 */ public function testQuote() { $this->assertThat( $this->db->quote('test', false), $this->equalTo("'test'"), 'Tests the without escaping.' ); $this->assertThat( $this->db->quote('test'), $this->equalTo("'-test-'"), 'Tests the with escaping (default).' ); }
/** * Custom doExecute method. * * This method loads a list of the published plugins from the 'cron' group, * then loads the plugins and registers them against the 'doCron' event. * The event is then triggered and results logged. * * Any configuration for the cron plugins is done via the Joomla CMS * administrator interface and plugin parameters. * * @return void * * @since 11.3 */ public function doExecute() { // // Check we have some critical information. // if (!defined('JPATH_PLUGINS') || !is_dir(JPATH_PLUGINS)) { throw new Exception('JPATH_PLUGINS not defined'); } // Add a start message. JLog::add('Starting cron run.'); // // Prepare the plugins // // Get the quey builder class from the database. $query = $this->dbo->getQuery(true); // Get a list of the plugins from the database. $query->select('p.*')->from('#__extensions AS p')->where('p.enabled = 1')->where('p.type = ' . $this->dbo->quote('plugin'))->where('p.folder = ' . $this->dbo->quote('cron'))->order('p.ordering'); // Push the query builder object into the database connector. $this->dbo->setQuery($query); // Get all the returned rows from the query as an array of objects. $plugins = $this->dbo->loadObjectList(); // Log how many plugins were loaded from the database. JLog::add(sprintf('.loaded %d plugin(s).', count($plugins))); // Loop through each of the results from the database query. foreach ($plugins as $plugin) { // Build the class name of the plugin. $className = 'plg' . ucfirst($plugin->folder) . ucfirst($plugin->element); $element = preg_replace('#[^A-Z0-9-~_]#i', '', $plugin->element); // If the class doesn't already exist, try to load it. if (!class_exists($className)) { // Compute the path to the plugin file. $path = sprintf(rtrim(JPATH_PLUGINS, '\\/') . '/cron/%s/%s.php', $element, $element); // Check if the file exists. if (is_file($path)) { // Include the file. include $path; // Double check if we have a valid class. if (!class_exists($className)) { // Log a warning and contine to the next record. JLog::add(sprintf('..plugin class for `%s` not found in file.', $element), JLog::WARNING); continue; } } else { // Log a warning and contine to the next record. JLog::add(sprintf('..plugin file for `%s` not found.', $element), JLog::WARNING); continue; } } JLog::add(sprintf('..registering `%s` plugin.', $element)); // Register the event. $this->registerEvent('doCron', new $className($this->dispatcher, array('params' => new JRegistry($plugin->params)))); } // // Run the cron plugins. // // Each plugin should have been installed in the Joomla CMS site // and must include a 'doCron' method. Configuration of the plugin // is done via plugin parameters. // JLog::add('.triggering `doCron` event.'); // Trigger the event and let the Joomla plugins do all the work. $results = $this->triggerEvent('doCron'); // Log the results. foreach ($this->triggerEvent('doCron') as $result) { JLog::add(sprintf('..plugin returned `%s`.', var_export($result, true))); } JLog::add('Finished cron run.'); }
function sortearEliminatoria(array $equipos, $numRows_eq, JDatabase $db_ins_part, $id_grupo, JDatabase $db_ins_jor) { $cantidad_equipos = $numRows_eq; $cantidad_partidos = $cantidad_equipos - 1; $cantidad_jornadas = ceil(log($cantidad_equipos, 2)); $preliminares = $cantidad_equipos - pow(2, floor(log($cantidad_equipos, 2))); $standby = $cantidad_equipos - $preliminares * 2; $partidos_potencia2 = $cantidad_equipos / 2; $partidos_ronda2 = ($standby + $preliminares) / 2; // echo "Equipos:".$cantidad_equipos."<br>"; // echo "Partidos:".$cantidad_partidos."<br>"; // echo "Jornadas:".$cantidad_jornadas."<br>"; // echo "Preliminares:".$preliminares."<br>"; // echo "Espera:".$standby."<br>"; // echo "Partidos potencia 2: ".$partidos_potencia2."<br>"; // echo "Partidos ronda 2: ".$partidos_ronda2."<br>"; $partidos = array(); $equipos_partido = array(); $nombre_jornadas = array('Final', 'Semifinal', 'Cuartos de Final', 'Octavos de Final', 'Primera Ronda', 'Preliminares'); $x = 0; // $y=0; // $y=$cantidad_equipos-1; $y = $preliminares * 2 - 1; //Insertar primer jornada for ($i = 0; $i < $cantidad_jornadas; $i++) { if ($preliminares == 0) { switch ($i) { case 0: $descr_jor = $nombre_jornadas[$i]; case 1: $descr_jor = $nombre_jornadas[$i]; case 2: $descr_jor = $nombre_jornadas[$i]; case 3: $descr_jor = $nombre_jornadas[$i]; case 4: $descr_jor = $nombre_jornadas[$i]; case 5: $descr_jor = $nombre_jornadas[$i]; } } else { switch ($i) { case 0: $descr_jor = $nombre_jornadas[$i]; case 1: $descr_jor = $nombre_jornadas[$i]; case 2: $descr_jor = $nombre_jornadas[$i]; case 3: $descr_jor = $nombre_jornadas[$i]; case 4: $descr_jor = $nombre_jornadas[$i]; case 5: $descr_jor = $nombre_jornadas[$i]; } } try { $query_ins_jor = $db_ins_jor->getQuery(true); $columns = array('descripcion', 'id_grupo', 'numero'); $values = array($db_ins_jor->quote($descr_jor), $id_grupo, $i + 1); $query_ins_jor->insert($db_ins_jor->quoteName('jornada'))->columns($db_ins_jor->quoteName($columns))->values(implode(',', $values)); $db_ins_jor->setQuery($query_ins_jor); $db_ins_jor->execute(); $id_jornada = $db_ins_jor->insertid(); } catch (Exception $e) { echo $e; } if ($preliminares != 0) { $partidos_ronda = pow(2, $i); if ($i == $cantidad_jornadas - 1) { $partidos_ronda = $preliminares; } for ($j = 0; $j < $partidos_ronda; $j++) { if ($i == $cantidad_jornadas - 1) { $equipos_partido[0] = $equipos[$x]; $equipos_partido[1] = $equipos[$x + 1]; $partidos[$j] = $equipos_partido; try { $query_ins_part = $db_ins_part->getQuery(true); // Insert columns. $columns = array('id_torneo', 'id_jornada'); // Insert values. $values = array($_SESSION['id_torneo'], $id_jornada); // Prepare the insert query. $query_ins_part->insert($db_ins_part->quoteName('partido'))->columns($db_ins_part->quoteName($columns))->values(implode(',', $values)); // Set the query using our newly populated query object and execute it. $db_ins_part->setQuery($query_ins_part); $db_ins_part->execute(); $id_partido = $db_ins_part->insertid(); $equipos_p = $partidos[$j]; $query_ins_part = $db_ins_part->getQuery(true); // Insert columns. $columns = array('id_equipo1', 'id_equipo2', 'id_partido'); // Insert values. $values = array($equipos_p[0]->id_eq, $equipos_p[1]->id_eq, $id_partido); // Prepare the insert query. $query_ins_part->insert($db_ins_part->quoteName('partido_equipos'))->columns($db_ins_part->quoteName($columns))->values(implode(',', $values)); // Set the query using our newly populated query object and execute it. $db_ins_part->setQuery($query_ins_part); $db_ins_part->execute(); } catch (Exception $e) { echo $e; } $x++; $x++; } elseif ($i == $cantidad_jornadas - 2) { $dividido = ceil($preliminares / 2); $equipos_partido[0] = $equipos[$y]; if (isset($equipos[$y + 1])) { $equipos_partido[1] = $equipos[$y + 1]; } $partidos[$j] = $equipos_partido; if ($preliminares % 2 == 0) { if ($j < $dividido) { $equipos_partido[0] = 26; $equipos_partido[1] = 26; $partidos[$j] = $equipos_partido; } } else { if ($j < $dividido) { if ($j == $dividido - 1) { $equipos_partido[0] = 26; $equipos_partido[1] = $equipos[$y + 1]; $partidos[$j] = $equipos_partido; } elseif ($j < $dividido - 1) { $equipos_partido[0] = 26; $equipos_partido[1] = 26; $partidos[$j] = $equipos_partido; } } } try { $query_ins_part = $db_ins_part->getQuery(true); // Insert columns. $columns = array('id_torneo', 'id_jornada'); // Insert values. $values = array($_SESSION['id_torneo'], $id_jornada); // Prepare the insert query. $query_ins_part->insert($db_ins_part->quoteName('partido'))->columns($db_ins_part->quoteName($columns))->values(implode(',', $values)); // Set the query using our newly populated query object and execute it. $db_ins_part->setQuery($query_ins_part); $db_ins_part->execute(); $id_partido = $db_ins_part->insertid(); $equipos_p = $partidos[$j]; $query_ins_part = $db_ins_part->getQuery(true); // Insert columns. $columns = array('id_equipo1', 'id_equipo2', 'id_partido'); // Insert values. if ($j < $dividido) { if ($preliminares % 2 == 0) { $values = array($equipos_p[0], $equipos_p[1], $id_partido); } else { if ($j == $dividido - 1) { $values = array($equipos_p[0], $equipos_p[1]->id_eq, $id_partido); } else { $values = array($equipos_p[0], $equipos_p[1], $id_partido); } } } elseif ($j >= $dividido) { $values = array($equipos_p[0]->id_eq, $equipos_p[1]->id_eq, $id_partido); } // Prepare the insert query. $query_ins_part->insert($db_ins_part->quoteName('partido_equipos'))->columns($db_ins_part->quoteName($columns))->values(implode(',', $values)); // Set the query using our newly populated query object and execute it. $db_ins_part->setQuery($query_ins_part); $db_ins_part->execute(); } catch (Exception $e) { echo $e; } $y++; $y++; } else { try { $query_ins_part = $db_ins_part->getQuery(true); // Insert columns. $columns = array('id_torneo', 'id_jornada'); // Insert values. $values = array($_SESSION['id_torneo'], $id_jornada); // Prepare the insert query. $query_ins_part->insert($db_ins_part->quoteName('partido'))->columns($db_ins_part->quoteName($columns))->values(implode(',', $values)); // Set the query using our newly populated query object and execute it. $db_ins_part->setQuery($query_ins_part); $db_ins_part->execute(); $id_partido = $db_ins_part->insertid(); // $equipos_p = $partidos[$j]; $query_ins_part = $db_ins_part->getQuery(true); // Insert columns. $columns = array('id_equipo1', 'id_equipo2', 'id_partido'); // Insert values. $values = array(26, 26, $id_partido); // Prepare the insert query. $query_ins_part->insert($db_ins_part->quoteName('partido_equipos'))->columns($db_ins_part->quoteName($columns))->values(implode(',', $values)); // Set the query using our newly populated query object and execute it. $db_ins_part->setQuery($query_ins_part); $db_ins_part->execute(); } catch (Exception $e) { echo $e; } } } } else { $partidos_ronda = pow(2, $i); for ($j = 0; $j < $partidos_ronda; $j++) { if ($i == $cantidad_jornadas - 1) { $equipos_partido[0] = $equipos[$x]; $equipos_partido[1] = $equipos[$x + 1]; $partidos[$j] = $equipos_partido; try { $query_ins_part = $db_ins_part->getQuery(true); // Insert columns. $columns = array('id_torneo', 'id_jornada'); // Insert values. $values = array($_SESSION['id_torneo'], $id_jornada); // Prepare the insert query. $query_ins_part->insert($db_ins_part->quoteName('partido'))->columns($db_ins_part->quoteName($columns))->values(implode(',', $values)); // Set the query using our newly populated query object and execute it. $db_ins_part->setQuery($query_ins_part); $db_ins_part->execute(); $id_partido = $db_ins_part->insertid(); $equipos_p = $partidos[$j]; $query_ins_part = $db_ins_part->getQuery(true); // Insert columns. $columns = array('id_equipo1', 'id_equipo2', 'id_partido'); // Insert values. $values = array($equipos_p[0]->id_eq, $equipos_p[1]->id_eq, $id_partido); // Prepare the insert query. $query_ins_part->insert($db_ins_part->quoteName('partido_equipos'))->columns($db_ins_part->quoteName($columns))->values(implode(',', $values)); // Set the query using our newly populated query object and execute it. $db_ins_part->setQuery($query_ins_part); $db_ins_part->execute(); } catch (Exception $e) { echo $e; } $x++; $x++; } else { try { $query_ins_part = $db_ins_part->getQuery(true); // Insert columns. $columns = array('id_torneo', 'id_jornada'); // Insert values. $values = array($_SESSION['id_torneo'], $id_jornada); // Prepare the insert query. $query_ins_part->insert($db_ins_part->quoteName('partido'))->columns($db_ins_part->quoteName($columns))->values(implode(',', $values)); // Set the query using our newly populated query object and execute it. $db_ins_part->setQuery($query_ins_part); $db_ins_part->execute(); $id_partido = $db_ins_part->insertid(); // $equipos_p = $partidos[$j]; $query_ins_part = $db_ins_part->getQuery(true); // Insert columns. $columns = array('id_equipo1', 'id_equipo2', 'id_partido'); // Insert values. $values = array(26, 26, $id_partido); // Prepare the insert query. $query_ins_part->insert($db_ins_part->quoteName('partido_equipos'))->columns($db_ins_part->quoteName($columns))->values(implode(',', $values)); // Set the query using our newly populated query object and execute it. $db_ins_part->setQuery($query_ins_part); $db_ins_part->execute(); } catch (Exception $e) { echo $e; } } } } } }
/** * Tests the JDatabase::quote method. * * @return void * * @covers JDatabaseDriver::quote * @since 11.4 */ public function testQuote() { $this->assertThat($this->db->quote('test', false), $this->equalTo("'test'"), 'Tests the without escaping.'); $this->assertThat($this->db->quote('test'), $this->equalTo("'-test-'"), 'Tests the with escaping (default).'); $this->assertEquals(array("'-test1-'", "'-test2-'"), $this->db->quote(array('test1', 'test2')), 'Check that the array is quoted.'); }