Ejemplo n.º 1
0
 /**
  * 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;
 }
Ejemplo n.º 2
0
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;
    }
}
Ejemplo n.º 3
0
 /**
  * Tests the JDatabase::quoteName method.
  *
  * @return  void
  *
  * @since   11.4
  */
 public function testQuoteName()
 {
     $this->assertThat($this->db->quoteName('test'), $this->equalTo('[test]'), 'Tests the left-right quotes on a string.');
     $this->assertThat($this->db->quoteName('a.test'), $this->equalTo('[a].[test]'), 'Tests the left-right quotes on a dotted string.');
     $this->assertThat($this->db->quoteName(array('a', 'test')), $this->equalTo(array('[a]', '[test]')), 'Tests the left-right quotes on an array.');
     $this->assertThat($this->db->quoteName(array('a.b', 'test.quote')), $this->equalTo(array('[a].[b]', '[test].[quote]')), 'Tests the left-right quotes on an array.');
     $this->assertThat($this->db->quoteName(array('a.b', 'test.quote'), array(null, 'alias')), $this->equalTo(array('[a].[b]', '[test].[quote] AS [alias]')), 'Tests the left-right quotes on an array.');
     $this->assertThat($this->db->quoteName(array('a.b', 'test.quote'), array('alias1', 'alias2')), $this->equalTo(array('[a].[b] AS [alias1]', '[test].[quote] AS [alias2]')), 'Tests the left-right quotes on an array.');
     $this->assertThat($this->db->quoteName((object) array('a', 'test')), $this->equalTo(array('[a]', '[test]')), 'Tests the left-right quotes on an object.');
     ReflectionHelper::setValue($this->db, 'nameQuote', '/');
     $this->assertThat($this->db->quoteName('test'), $this->equalTo('/test/'), 'Tests the uni-quotes on a string.');
 }
Ejemplo n.º 4
0
 /**
  * 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;
 }
Ejemplo n.º 5
0
 /**
  * Generic save function
  *
  * @access	public
  * @param	array	Source array for binding to class vars
  * @param	string	Filter for the order updating
  * @param	mixed	An array or space separated list of fields not to bind
  * @returns TRUE if completely successful, FALSE if partially or not succesful.
  */
 function save($source, $order_filter = '', $ignore = '')
 {
     if (!$this->bind($source, $ignore)) {
         return false;
     }
     if (!$this->check()) {
         return false;
     }
     if (!$this->store()) {
         return false;
     }
     if (!$this->checkin()) {
         return false;
     }
     if ($order_filter) {
         $filter_value = $this->{$order_filter};
         $this->reorder($order_filter ? $this->_db->quoteName($order_filter) . ' = ' . $this->_db->Quote($filter_value) : '');
     }
     $this->setError('');
     return true;
 }
Ejemplo n.º 6
0
 /**
  * Method to get a query expression for a table key.
  *
  * @param   string   $alias     The table alias.
  * @param   string   $key       The table key alias.
  * @param   boolean  $useAlias  True to use the alias in the expression, false otherwise.
  *
  * @return  string  The table expression.
  *
  * @since   12.1
  * @throws  InvalidArgumentException
  */
 protected function getTableKeyExpression($alias, $key, $useAlias = true)
 {
     $return = '';
     // Assert that the table alias is defined.
     if (!array_key_exists($alias, $this->tables) || !array_key_exists($alias, $this->keys)) {
         throw new InvalidArgumentException(JText::sprintf('JDATABASEOBJECT_INVALID_TABLE', $alias));
     }
     // Check if the key is a column.
     if (isset($this->keys[$alias][$key])) {
         // Quote the column name.
         $column = $this->db->quoteName($this->keys[$alias][$key]);
     } else {
         // Escape the expression.
         $column = $this->db->escape($key);
     }
     // Check if we should use the table alias.
     if ($useAlias) {
         $return .= $this->db->quoteName($alias) . '.';
     }
     // Build the table expression.
     $return .= $column;
     return $return;
 }
Ejemplo n.º 7
0
 /**
  * Quote an identifier name (field, table, etc)
  *
  * @param  string|array  $name  The name (supports arrays and .-notations)
  * @param  string|array  $as    The AS query part (supports arrays too)
  * @return string               The quoted name
  */
 public function NameQuote($name, $as = null)
 {
     return $this->_db->quoteName($name, $as);
 }
Ejemplo n.º 8
0
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;
                    }
                }
            }
        }
    }
}
Ejemplo n.º 9
0
 /**
  * Wrap an SQL statement identifier name such as column, table or database names in quotes to prevent injection
  * risks and reserved word conflicts.
  * @param   string $name The identifier name to wrap in quotes.
  * @return  string  The quote wrapped name.
  */
 public function quoteName($name)
 {
     return $this->db->quoteName($name);
 }