[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/database/ -> query.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Database
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  defined('JPATH_PLATFORM') or die;
  11  
  12  /**
  13   * Query Element Class.
  14   *
  15   * @package     Joomla.Platform
  16   * @subpackage  Database
  17   * @since       11.1
  18   */
  19  class JDatabaseQueryElement
  20  {
  21      /**
  22       * @var    string  The name of the element.
  23       * @since  11.1
  24       */
  25      protected $name = null;
  26  
  27      /**
  28       * @var    array  An array of elements.
  29       * @since  11.1
  30       */
  31      protected $elements = null;
  32  
  33      /**
  34       * @var    string  Glue piece.
  35       * @since  11.1
  36       */
  37      protected $glue = null;
  38  
  39      /**
  40       * Constructor.
  41       *
  42       * @param   string  $name      The name of the element.
  43       * @param   mixed   $elements  String or array.
  44       * @param   string  $glue      The glue for elements.
  45       *
  46       * @since   11.1
  47       */
  48  	public function __construct($name, $elements, $glue = ',')
  49      {
  50          $this->elements = array();
  51          $this->name = $name;
  52          $this->glue = $glue;
  53  
  54          $this->append($elements);
  55      }
  56  
  57      /**
  58       * Magic function to convert the query element to a string.
  59       *
  60       * @return  string
  61       *
  62       * @since   11.1
  63       */
  64  	public function __toString()
  65      {
  66          if (substr($this->name, -2) == '()')
  67          {
  68              return PHP_EOL . substr($this->name, 0, -2) . '(' . implode($this->glue, $this->elements) . ')';
  69          }
  70          else
  71          {
  72              return PHP_EOL . $this->name . ' ' . implode($this->glue, $this->elements);
  73          }
  74      }
  75  
  76      /**
  77       * Appends element parts to the internal list.
  78       *
  79       * @param   mixed  $elements  String or array.
  80       *
  81       * @return  void
  82       *
  83       * @since   11.1
  84       */
  85  	public function append($elements)
  86      {
  87          if (is_array($elements))
  88          {
  89              $this->elements = array_merge($this->elements, $elements);
  90          }
  91          else
  92          {
  93              $this->elements = array_merge($this->elements, array($elements));
  94          }
  95      }
  96  
  97      /**
  98       * Gets the elements of this element.
  99       *
 100       * @return  string
 101       *
 102       * @since   11.1
 103       */
 104  	public function getElements()
 105      {
 106          return $this->elements;
 107      }
 108  
 109      /**
 110       * Method to provide deep copy support to nested objects and arrays
 111       * when cloning.
 112       *
 113       * @return  void
 114       *
 115       * @since   11.3
 116       */
 117  	public function __clone()
 118      {
 119          foreach ($this as $k => $v)
 120          {
 121              if (is_object($v) || is_array($v))
 122              {
 123                  $this->{$k} = unserialize(serialize($v));
 124              }
 125          }
 126      }
 127  }
 128  
 129  /**
 130   * Query Building Class.
 131   *
 132   * @package     Joomla.Platform
 133   * @subpackage  Database
 134   * @since       11.1
 135   */
 136  abstract class JDatabaseQuery
 137  {
 138      /**
 139       * @var    JDatabase  The database connection resource.
 140       * @since  11.1
 141       */
 142      protected $db = null;
 143  
 144      /**
 145       * @var    string  The query type.
 146       * @since  11.1
 147       */
 148      protected $type = '';
 149  
 150      /**
 151       * @var    JDatabaseQueryElement  The query element for a generic query (type = null).
 152       * @since  11.1
 153       */
 154      protected $element = null;
 155  
 156      /**
 157       * @var    JDatabaseQueryElement  The select element.
 158       * @since  11.1
 159       */
 160      protected $select = null;
 161  
 162      /**
 163       * @var    JDatabaseQueryElement  The delete element.
 164       * @since  11.1
 165       */
 166      protected $delete = null;
 167  
 168      /**
 169       * @var    JDatabaseQueryElement  The update element.
 170       * @since  11.1
 171       */
 172      protected $update = null;
 173  
 174      /**
 175       * @var    JDatabaseQueryElement  The insert element.
 176       * @since  11.1
 177       */
 178      protected $insert = null;
 179  
 180      /**
 181       * @var    JDatabaseQueryElement  The from element.
 182       * @since  11.1
 183       */
 184      protected $from = null;
 185  
 186      /**
 187       * @var    JDatabaseQueryElement  The join element.
 188       * @since  11.1
 189       */
 190      protected $join = null;
 191  
 192      /**
 193       * @var    JDatabaseQueryElement  The set element.
 194       * @since  11.1
 195       */
 196      protected $set = null;
 197  
 198      /**
 199       * @var    JDatabaseQueryElement  The where element.
 200       * @since  11.1
 201       */
 202      protected $where = null;
 203  
 204      /**
 205       * @var    JDatabaseQueryElement  The group by element.
 206       * @since  11.1
 207       */
 208      protected $group = null;
 209  
 210      /**
 211       * @var    JDatabaseQueryElement  The having element.
 212       * @since  11.1
 213       */
 214      protected $having = null;
 215  
 216      /**
 217       * @var    JDatabaseQueryElement  The column list for an INSERT statement.
 218       * @since  11.1
 219       */
 220      protected $columns = null;
 221  
 222      /**
 223       * @var    JDatabaseQueryElement  The values list for an INSERT statement.
 224       * @since  11.1
 225       */
 226      protected $values = null;
 227  
 228      /**
 229       * @var    JDatabaseQueryElement  The order element.
 230       * @since  11.1
 231       */
 232      protected $order = null;
 233  
 234      /**
 235       * @var   object  The auto increment insert field element.
 236       * @since 11.1
 237       */
 238      protected $autoIncrementField = null;
 239  
 240      /**
 241       * Magic method to provide method alias support for quote() and quoteName().
 242       *
 243       * @param   string  $method  The called method.
 244       * @param   array   $args    The array of arguments passed to the method.
 245       *
 246       * @return  string  The aliased method's return value or null.
 247       *
 248       * @since   11.1
 249       */
 250  	public function __call($method, $args)
 251      {
 252          if (empty($args))
 253          {
 254              return;
 255          }
 256  
 257          switch ($method)
 258          {
 259              case 'q':
 260                  return $this->quote($args[0], isset($args[1]) ? $args[1] : true);
 261                  break;
 262  
 263              case 'qn':
 264                  return $this->quoteName($args[0]);
 265                  break;
 266  
 267              case 'e':
 268                  return $this->escape($args[0], isset($args[1]) ? $args[1] : false);
 269                  break;
 270          }
 271      }
 272  
 273      /**
 274       * Class constructor.
 275       *
 276       * @param   JDatabase  $db  The database connector resource.
 277       *
 278       * @since   11.1
 279       */
 280  	public function __construct(JDatabase $db = null)
 281      {
 282          $this->db = $db;
 283      }
 284  
 285      /**
 286       * Magic function to convert the query to a string.
 287       *
 288       * @return  string    The completed query.
 289       *
 290       * @since   11.1
 291       */
 292  	public function __toString()
 293      {
 294          $query = '';
 295  
 296          switch ($this->type)
 297          {
 298              case 'element':
 299                  $query .= (string) $this->element;
 300                  break;
 301  
 302              case 'select':
 303                  $query .= (string) $this->select;
 304                  $query .= (string) $this->from;
 305                  if ($this->join)
 306                  {
 307                      // special case for joins
 308                      foreach ($this->join as $join)
 309                      {
 310                          $query .= (string) $join;
 311                      }
 312                  }
 313  
 314                  if ($this->where)
 315                  {
 316                      $query .= (string) $this->where;
 317                  }
 318  
 319                  if ($this->group)
 320                  {
 321                      $query .= (string) $this->group;
 322                  }
 323  
 324                  if ($this->having)
 325                  {
 326                      $query .= (string) $this->having;
 327                  }
 328  
 329                  if ($this->order)
 330                  {
 331                      $query .= (string) $this->order;
 332                  }
 333  
 334                  break;
 335  
 336              case 'delete':
 337                  $query .= (string) $this->delete;
 338                  $query .= (string) $this->from;
 339  
 340                  if ($this->join)
 341                  {
 342                      // special case for joins
 343                      foreach ($this->join as $join)
 344                      {
 345                          $query .= (string) $join;
 346                      }
 347                  }
 348  
 349                  if ($this->where)
 350                  {
 351                      $query .= (string) $this->where;
 352                  }
 353  
 354                  break;
 355  
 356              case 'update':
 357                  $query .= (string) $this->update;
 358  
 359                  if ($this->join)
 360                  {
 361                      // special case for joins
 362                      foreach ($this->join as $join)
 363                      {
 364                          $query .= (string) $join;
 365                      }
 366                  }
 367  
 368                  $query .= (string) $this->set;
 369  
 370                  if ($this->where)
 371                  {
 372                      $query .= (string) $this->where;
 373                  }
 374  
 375                  break;
 376  
 377              case 'insert':
 378                  $query .= (string) $this->insert;
 379  
 380                  // Set method
 381                  if ($this->set)
 382                  {
 383                      $query .= (string) $this->set;
 384                  }
 385                  // Columns-Values method
 386                  elseif ($this->values)
 387                  {
 388                      if ($this->columns)
 389                      {
 390                          $query .= (string) $this->columns;
 391                      }
 392  
 393                      $query .= ' VALUES ';
 394                      $query .= (string) $this->values;
 395                  }
 396  
 397                  break;
 398          }
 399  
 400          return $query;
 401      }
 402  
 403      /**
 404       * Magic function to get protected variable value
 405       *
 406       * @param   string  $name  The name of the variable.
 407       *
 408       * @return  mixed
 409       *
 410       * @since   11.1
 411       */
 412  	public function __get($name)
 413      {
 414          return isset($this->$name) ? $this->$name : null;
 415      }
 416  
 417      /**
 418       * Casts a value to a char.
 419       *
 420       * Ensure that the value is properly quoted before passing to the method.
 421       *
 422       * Usage:
 423       * $query->select($query->castAsChar('a'));
 424       *
 425       * @param   string  $value  The value to cast as a char.
 426       *
 427       * @return  string  Returns the cast value.
 428       *
 429       * @since   11.1
 430       */
 431  	public function castAsChar($value)
 432      {
 433          return $value;
 434      }
 435  
 436      /**
 437       * Gets the number of characters in a string.
 438       *
 439       * Note, use 'length' to find the number of bytes in a string.
 440       *
 441       * Usage:
 442       * $query->select($query->charLength('a'));
 443       *
 444       * @param   string  $field  A value.
 445       *
 446       * @return  string  The required char length call.
 447       *
 448       * @since 11.1
 449       */
 450  	public function charLength($field)
 451      {
 452          return 'CHAR_LENGTH(' . $field . ')';
 453      }
 454  
 455      /**
 456       * Clear data from the query or a specific clause of the query.
 457       *
 458       * @param   string  $clause  Optionally, the name of the clause to clear, or nothing to clear the whole query.
 459       *
 460       * @return  JDatabaseQuery  Returns this object to allow chaining.
 461       *
 462       * @since   11.1
 463       */
 464  	public function clear($clause = null)
 465      {
 466          switch ($clause)
 467          {
 468              case 'select':
 469                  $this->select = null;
 470                  $this->type = null;
 471                  break;
 472  
 473              case 'delete':
 474                  $this->delete = null;
 475                  $this->type = null;
 476                  break;
 477  
 478              case 'update':
 479                  $this->update = null;
 480                  $this->type = null;
 481                  break;
 482  
 483              case 'insert':
 484                  $this->insert = null;
 485                  $this->type = null;
 486                  $this->autoIncrementField = null;
 487                  break;
 488  
 489              case 'from':
 490                  $this->from = null;
 491                  break;
 492  
 493              case 'join':
 494                  $this->join = null;
 495                  break;
 496  
 497              case 'set':
 498                  $this->set = null;
 499                  break;
 500  
 501              case 'where':
 502                  $this->where = null;
 503                  break;
 504  
 505              case 'group':
 506                  $this->group = null;
 507                  break;
 508  
 509              case 'having':
 510                  $this->having = null;
 511                  break;
 512  
 513              case 'order':
 514                  $this->order = null;
 515                  break;
 516  
 517              case 'columns':
 518                  $this->columns = null;
 519                  break;
 520  
 521              case 'values':
 522                  $this->values = null;
 523                  break;
 524  
 525              default:
 526                  $this->type = null;
 527                  $this->select = null;
 528                  $this->delete = null;
 529                  $this->update = null;
 530                  $this->insert = null;
 531                  $this->from = null;
 532                  $this->join = null;
 533                  $this->set = null;
 534                  $this->where = null;
 535                  $this->group = null;
 536                  $this->having = null;
 537                  $this->order = null;
 538                  $this->columns = null;
 539                  $this->values = null;
 540                  $this->autoIncrementField = null;
 541                  break;
 542          }
 543  
 544          return $this;
 545      }
 546  
 547      /**
 548       * Adds a column, or array of column names that would be used for an INSERT INTO statement.
 549       *
 550       * @param   mixed  $columns  A column name, or array of column names.
 551       *
 552       * @return  JDatabaseQuery  Returns this object to allow chaining.
 553       *
 554       * @since   11.1
 555       */
 556  	public function columns($columns)
 557      {
 558          if (is_null($this->columns))
 559          {
 560              $this->columns = new JDatabaseQueryElement('()', $columns);
 561          }
 562          else
 563          {
 564              $this->columns->append($columns);
 565          }
 566  
 567          return $this;
 568      }
 569  
 570      /**
 571       * Concatenates an array of column names or values.
 572       *
 573       * Usage:
 574       * $query->select($query->concatenate(array('a', 'b')));
 575       *
 576       * @param   array   $values     An array of values to concatenate.
 577       * @param   string  $separator  As separator to place between each value.
 578       *
 579       * @return  string  The concatenated values.
 580       *
 581       * @since   11.1
 582       */
 583  	public function concatenate($values, $separator = null)
 584      {
 585          if ($separator)
 586          {
 587              return 'CONCATENATE(' . implode(' || ' . $this->quote($separator) . ' || ', $values) . ')';
 588          }
 589          else
 590          {
 591              return 'CONCATENATE(' . implode(' || ', $values) . ')';
 592          }
 593      }
 594  
 595      /**
 596       * Gets the current date and time.
 597       *
 598       * Usage:
 599       * $query->where('published_up < '.$query->currentTimestamp());
 600       *
 601       * @return  string
 602       *
 603       * @since   11.1
 604       */
 605  	public function currentTimestamp()
 606      {
 607          return 'CURRENT_TIMESTAMP()';
 608      }
 609  
 610      /**
 611       * Returns a PHP date() function compliant date format for the database driver.
 612       *
 613       * This method is provided for use where the query object is passed to a function for modification.
 614       * If you have direct access to the database object, it is recommended you use the getDateFormat method directly.
 615       *
 616       * @return  string  The format string.
 617       *
 618       * @since   11.1
 619       */
 620  	public function dateFormat()
 621      {
 622          if (!($this->db instanceof JDatabase))
 623          {
 624              throw new JDatabaseException('JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
 625          }
 626  
 627          return $this->db->getDateFormat();
 628      }
 629  
 630      /**
 631       * Creates a formatted dump of the query for debugging purposes.
 632       *
 633       * Usage:
 634       * echo $query->dump();
 635       *
 636       * @return  string
 637       *
 638       * @since   11.3
 639       */
 640  	public function dump()
 641      {
 642          return '<pre class="jdatabasequery">' . str_replace('#__', $this->db->getPrefix(), $this) . '</pre>';
 643      }
 644  
 645      /**
 646       * Add a table name to the DELETE clause of the query.
 647       *
 648       * Note that you must not mix insert, update, delete and select method calls when building a query.
 649       *
 650       * Usage:
 651       * $query->delete('#__a')->where('id = 1');
 652       *
 653       * @param   string  $table  The name of the table to delete from.
 654       *
 655       * @return  JDatabaseQuery  Returns this object to allow chaining.
 656       *
 657       * @since   11.1
 658       */
 659  	public function delete($table = null)
 660      {
 661          $this->type = 'delete';
 662          $this->delete = new JDatabaseQueryElement('DELETE', null);
 663  
 664          if (!empty($table))
 665          {
 666              $this->from($table);
 667          }
 668  
 669          return $this;
 670      }
 671  
 672      /**
 673       * Method to escape a string for usage in an SQL statement.
 674       *
 675       * This method is provided for use where the query object is passed to a function for modification.
 676       * If you have direct access to the database object, it is recommended you use the escape method directly.
 677       *
 678       * Note that 'e' is an alias for this method as it is in JDatabase.
 679       *
 680       * @param   string   $text   The string to be escaped.
 681       * @param   boolean  $extra  Optional parameter to provide extra escaping.
 682       *
 683       * @return  string  The escaped string.
 684       *
 685       * @since   11.1
 686       * @throws  DatabaseError if the internal db property is not a valid object.
 687       */
 688  	public function escape($text, $extra = false)
 689      {
 690          if (!($this->db instanceof JDatabase))
 691          {
 692              throw new JDatabaseException('JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
 693          }
 694  
 695          return $this->db->escape($text, $extra);
 696      }
 697  
 698      /**
 699       * Add a table to the FROM clause of the query.
 700       *
 701       * Note that while an array of tables can be provided, it is recommended you use explicit joins.
 702       *
 703       * Usage:
 704       * $query->select('*')->from('#__a');
 705       *
 706       * @param   mixed  $tables  A string or array of table names.
 707       *
 708       * @return  JDatabaseQuery  Returns this object to allow chaining.
 709       *
 710       * @since   11.1
 711       */
 712  	public function from($tables)
 713      {
 714          if (is_null($this->from))
 715          {
 716              $this->from = new JDatabaseQueryElement('FROM', $tables);
 717          }
 718          else
 719          {
 720              $this->from->append($tables);
 721          }
 722  
 723          return $this;
 724      }
 725  
 726      /**
 727       * Add a grouping column to the GROUP clause of the query.
 728       *
 729       * Usage:
 730       * $query->group('id');
 731       *
 732       * @param   mixed  $columns  A string or array of ordering columns.
 733       *
 734       * @return  JDatabaseQuery  Returns this object to allow chaining.
 735       *
 736       * @since   11.1
 737       */
 738  	public function group($columns)
 739      {
 740          if (is_null($this->group))
 741          {
 742              $this->group = new JDatabaseQueryElement('GROUP BY', $columns);
 743          }
 744          else
 745          {
 746              $this->group->append($columns);
 747          }
 748  
 749          return $this;
 750      }
 751  
 752      /**
 753       * A conditions to the HAVING clause of the query.
 754       *
 755       * Usage:
 756       * $query->group('id')->having('COUNT(id) > 5');
 757       *
 758       * @param   mixed   $conditions  A string or array of columns.
 759       * @param   string  $glue        The glue by which to join the conditions. Defaults to AND.
 760       *
 761       * @return  JDatabaseQuery  Returns this object to allow chaining.
 762       *
 763       * @since   11.1
 764       */
 765  	public function having($conditions, $glue = 'AND')
 766      {
 767          if (is_null($this->having))
 768          {
 769              $glue = strtoupper($glue);
 770              $this->having = new JDatabaseQueryElement('HAVING', $conditions, " $glue ");
 771          }
 772          else
 773          {
 774              $this->having->append($conditions);
 775          }
 776  
 777          return $this;
 778      }
 779  
 780      /**
 781       * Add an INNER JOIN clause to the query.
 782       *
 783       * Usage:
 784       * $query->innerJoin('b ON b.id = a.id')->innerJoin('c ON c.id = b.id');
 785       *
 786       * @param   string  $condition  The join condition.
 787       *
 788       * @return  JDatabaseQuery  Returns this object to allow chaining.
 789       *
 790       * @since   11.1
 791       */
 792  	public function innerJoin($condition)
 793      {
 794          $this->join('INNER', $condition);
 795  
 796          return $this;
 797      }
 798  
 799      /**
 800       * Add a table name to the INSERT clause of the query.
 801       *
 802       * Note that you must not mix insert, update, delete and select method calls when building a query.
 803       *
 804       * Usage:
 805       * $query->insert('#__a')->set('id = 1');
 806       * $query->insert('#__a)->columns('id, title')->values('1,2')->values->('3,4');
 807       * $query->insert('#__a)->columns('id, title')->values(array('1,2', '3,4'));
 808       *
 809       * @param   mixed    $table           The name of the table to insert data into.
 810       * @param   boolean  $incrementField  The name of the field to auto increment.
 811       *
 812       * @return  JDatabaseQuery  Returns this object to allow chaining.
 813       *
 814       * @since   11.1
 815       */
 816  	public function insert($table, $incrementField=false)
 817      {
 818          $this->type = 'insert';
 819          $this->insert = new JDatabaseQueryElement('INSERT INTO', $table);
 820          $this->autoIncrementField = $incrementField;
 821  
 822          return $this;
 823      }
 824  
 825      /**
 826       * Add a JOIN clause to the query.
 827       *
 828       * Usage:
 829       * $query->join('INNER', 'b ON b.id = a.id);
 830       *
 831       * @param   string  $type        The type of join. This string is prepended to the JOIN keyword.
 832       * @param   string  $conditions  A string or array of conditions.
 833       *
 834       * @return  JDatabaseQuery  Returns this object to allow chaining.
 835       *
 836       * @since   11.1
 837       */
 838  	public function join($type, $conditions)
 839      {
 840          if (is_null($this->join))
 841          {
 842              $this->join = array();
 843          }
 844          $this->join[] = new JDatabaseQueryElement(strtoupper($type) . ' JOIN', $conditions);
 845  
 846          return $this;
 847      }
 848  
 849      /**
 850       * Add a LEFT JOIN clause to the query.
 851       *
 852       * Usage:
 853       * $query->leftJoin('b ON b.id = a.id')->leftJoin('c ON c.id = b.id');
 854       *
 855       * @param   string  $condition  The join condition.
 856       *
 857       * @return  JDatabaseQuery  Returns this object to allow chaining.
 858       *
 859       * @since   11.1
 860       */
 861  	public function leftJoin($condition)
 862      {
 863          $this->join('LEFT', $condition);
 864  
 865          return $this;
 866      }
 867  
 868      /**
 869       * Get the length of a string in bytes.
 870       *
 871       * Note, use 'charLength' to find the number of characters in a string.
 872       *
 873       * Usage:
 874       * query->where($query->length('a').' > 3');
 875       *
 876       * @param   string  $value  The string to measure.
 877       *
 878       * @return  int
 879       *
 880       * @since   11.1
 881       */
 882  	public function length($value)
 883      {
 884          return 'LENGTH(' . $value . ')';
 885      }
 886  
 887      /**
 888       * Get the null or zero representation of a timestamp for the database driver.
 889       *
 890       * This method is provided for use where the query object is passed to a function for modification.
 891       * If you have direct access to the database object, it is recommended you use the nullDate method directly.
 892       *
 893       * Usage:
 894       * $query->where('modified_date <> '.$query->nullDate());
 895       *
 896       * @param   boolean  $quoted  Optionally wraps the null date in database quotes (true by default).
 897       *
 898       * @return  string  Null or zero representation of a timestamp.
 899       *
 900       * @since   11.1
 901       */
 902  	public function nullDate($quoted = true)
 903      {
 904          if (!($this->db instanceof JDatabase))
 905          {
 906              throw new JDatabaseException('JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
 907          }
 908  
 909          $result = $this->db->getNullDate($quoted);
 910  
 911          if ($quoted)
 912          {
 913              return $this->db->quote($result);
 914          }
 915  
 916          return $result;
 917      }
 918  
 919      /**
 920       * Add a ordering column to the ORDER clause of the query.
 921       *
 922       * Usage:
 923       * $query->order('foo')->order('bar');
 924       * $query->order(array('foo','bar'));
 925       *
 926       * @param   mixed  $columns  A string or array of ordering columns.
 927       *
 928       * @return  JDatabaseQuery  Returns this object to allow chaining.
 929       *
 930       * @since   11.1
 931       */
 932  	public function order($columns)
 933      {
 934          if (is_null($this->order))
 935          {
 936              $this->order = new JDatabaseQueryElement('ORDER BY', $columns);
 937          }
 938          else
 939          {
 940              $this->order->append($columns);
 941          }
 942  
 943          return $this;
 944      }
 945  
 946      /**
 947       * Add an OUTER JOIN clause to the query.
 948       *
 949       * Usage:
 950       * $query->outerJoin('b ON b.id = a.id')->outerJoin('c ON c.id = b.id');
 951       *
 952       * @param   string  $condition  The join condition.
 953       *
 954       * @return  JDatabaseQuery  Returns this object to allow chaining.
 955       *
 956       * @since   11.1
 957       */
 958  	public function outerJoin($condition)
 959      {
 960          $this->join('OUTER', $condition);
 961  
 962          return $this;
 963      }
 964  
 965      /**
 966       * Method to quote and optionally escape a string to database requirements for insertion into the database.
 967       *
 968       * This method is provided for use where the query object is passed to a function for modification.
 969       * If you have direct access to the database object, it is recommended you use the quote method directly.
 970       *
 971       * Note that 'q' is an alias for this method as it is in JDatabase.
 972       *
 973       * Usage:
 974       * $query->quote('fulltext');
 975       * $query->q('fulltext');
 976       *
 977       * @param   string   $text    The string to quote.
 978       * @param   boolean  $escape  True to escape the string, false to leave it unchanged.
 979       *
 980       * @return  string  The quoted input string.
 981       *
 982       * @since   11.1
 983       * @throws  DatabaseError if the internal db property is not a valid object.
 984       */
 985  	public function quote($text, $escape = true)
 986      {
 987          if (!($this->db instanceof JDatabase))
 988          {
 989              throw new JDatabaseException('JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
 990          }
 991  
 992          return $this->db->quote(($escape ? $this->db->escape($text) : $text));
 993      }
 994  
 995      /**
 996       * Wrap an SQL statement identifier name such as column, table or database names in quotes to prevent injection
 997       * risks and reserved word conflicts.
 998       *
 999       * This method is provided for use where the query object is passed to a function for modification.
1000       * If you have direct access to the database object, it is recommended you use the quoteName method directly.
1001       *
1002       * Note that 'qn' is an alias for this method as it is in JDatabase.
1003       *
1004       * Usage:
1005       * $query->quoteName('#__a');
1006       * $query->qn('#__a');
1007       *
1008       * @param   string  $name  The identifier name to wrap in quotes.
1009       *
1010       * @return  string  The quote wrapped name.
1011       *
1012       * @since   11.1
1013       * @throws  DatabaseError if the internal db property is not a valid object.
1014       */
1015  	public function quoteName($name)
1016      {
1017          if (!($this->db instanceof JDatabase))
1018          {
1019              throw new JDatabaseException('JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
1020          }
1021  
1022          return $this->db->quoteName($name);
1023      }
1024  
1025      /**
1026       * Add a RIGHT JOIN clause to the query.
1027       *
1028       * Usage:
1029       * $query->rightJoin('b ON b.id = a.id')->rightJoin('c ON c.id = b.id');
1030       *
1031       * @param   string  $condition  The join condition.
1032       *
1033       * @return  JDatabaseQuery  Returns this object to allow chaining.
1034       *
1035       * @since   11.1
1036       */
1037  	public function rightJoin($condition)
1038      {
1039          $this->join('RIGHT', $condition);
1040  
1041          return $this;
1042      }
1043  
1044      /**
1045       * Add a single column, or array of columns to the SELECT clause of the query.
1046       *
1047       * Note that you must not mix insert, update, delete and select method calls when building a query.
1048       * The select method can, however, be called multiple times in the same query.
1049       *
1050       * Usage:
1051       * $query->select('a.*')->select('b.id');
1052       * $query->select(array('a.*', 'b.id'));
1053       *
1054       * @param   mixed  $columns  A string or an array of field names.
1055       *
1056       * @return  JDatabaseQuery  Returns this object to allow chaining.
1057       *
1058       * @since   11.1
1059       */
1060  	public function select($columns)
1061      {
1062          $this->type = 'select';
1063  
1064          if (is_null($this->select))
1065          {
1066              $this->select = new JDatabaseQueryElement('SELECT', $columns);
1067          }
1068          else
1069          {
1070              $this->select->append($columns);
1071          }
1072  
1073          return $this;
1074      }
1075  
1076      /**
1077       * Add a single condition string, or an array of strings to the SET clause of the query.
1078       *
1079       * Usage:
1080       * $query->set('a = 1')->set('b = 2');
1081       * $query->set(array('a = 1', 'b = 2');
1082       *
1083       * @param   mixed   $conditions  A string or array of string conditions.
1084       * @param   string  $glue        The glue by which to join the condition strings. Defaults to ,.
1085       *                               Note that the glue is set on first use and cannot be changed.
1086       *
1087       * @return  JDatabaseQuery  Returns this object to allow chaining.
1088       *
1089       * @since   11.1
1090       */
1091  	public function set($conditions, $glue = ',')
1092      {
1093          if (is_null($this->set))
1094          {
1095              $glue = strtoupper($glue);
1096              $this->set = new JDatabaseQueryElement('SET', $conditions, "\n\t$glue ");
1097          }
1098          else
1099          {
1100              $this->set->append($conditions);
1101          }
1102  
1103          return $this;
1104      }
1105  
1106      /**
1107       * Add a table name to the UPDATE clause of the query.
1108       *
1109       * Note that you must not mix insert, update, delete and select method calls when building a query.
1110       *
1111       * Usage:
1112       * $query->update('#__foo')->set(...);
1113       *
1114       * @param   string  $table  A table to update.
1115       *
1116       * @return  JDatabaseQuery  Returns this object to allow chaining.
1117       *
1118       * @since   11.1
1119       */
1120  	public function update($table)
1121      {
1122          $this->type = 'update';
1123          $this->update = new JDatabaseQueryElement('UPDATE', $table);
1124  
1125          return $this;
1126      }
1127  
1128      /**
1129       * Adds a tuple, or array of tuples that would be used as values for an INSERT INTO statement.
1130       *
1131       * Usage:
1132       * $query->values('1,2,3')->values('4,5,6');
1133       * $query->values(array('1,2,3', '4,5,6'));
1134       *
1135       * @param   string  $values  A single tuple, or array of tuples.
1136       *
1137       * @return  JDatabaseQuery  Returns this object to allow chaining.
1138       *
1139       * @since   11.1
1140       */
1141  	public function values($values)
1142      {
1143          if (is_null($this->values))
1144          {
1145              $this->values = new JDatabaseQueryElement('()', $values, '),(');
1146          }
1147          else
1148          {
1149              $this->values->append($values);
1150          }
1151  
1152          return $this;
1153      }
1154  
1155      /**
1156       * Add a single condition, or an array of conditions to the WHERE clause of the query.
1157       *
1158       * Usage:
1159       * $query->where('a = 1')->where('b = 2');
1160       * $query->where(array('a = 1', 'b = 2'));
1161       *
1162       * @param   mixed   $conditions  A string or array of where conditions.
1163       * @param   string  $glue        The glue by which to join the conditions. Defaults to AND.
1164       *                               Note that the glue is set on first use and cannot be changed.
1165       *
1166       * @return  JDatabaseQuery  Returns this object to allow chaining.
1167       *
1168       * @since   11.1
1169       */
1170  	public function where($conditions, $glue = 'AND')
1171      {
1172          if (is_null($this->where))
1173          {
1174              $glue = strtoupper($glue);
1175              $this->where = new JDatabaseQueryElement('WHERE', $conditions, " $glue ");
1176          }
1177          else
1178          {
1179              $this->where->append($conditions);
1180          }
1181  
1182          return $this;
1183      }
1184  
1185      /**
1186       * Method to provide deep copy support to nested objects and
1187       * arrays when cloning.
1188       *
1189       * @return  void
1190       *
1191       * @since   11.3
1192       */
1193  	public function __clone()
1194      {
1195          foreach ($this as $k => $v)
1196          {
1197              if (is_object($v) || is_array($v))
1198              {
1199                  $this->{$k} = unserialize(serialize($v));
1200              }
1201          }
1202      }
1203  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1