Анонс Расширение класса JDatabaseQuery

Тема в разделе "Модернизация расширений", создана пользователем woojin, 18.10.2015.

  1. woojin
    Offline

    woojin Местный Команда форума => Cпециалист <=

    Регистрация:
    31.05.2009
    Сообщения:
    3 206
    Симпатии:
    334
    Пол:
    Мужской
    всем привет!

    сидел, тут, писал (кодил значит |H|)....
    и в один прекрасный момент понадобилось мне написать запрос к БД такого типа
    Код (sql):
    1. INSERT ar_vm_attendance (product_id, attendance) VALUES (15, 1) ON DUPLICATE KEY UPDATE attendance = attendance+1, date_time_last_view = NOW();

    при этом использовать стандартную структуру класса JDatabaseQuery ни как не получается там в принципе нет возможности добавить ON DUPLICATE KEY UPDATE :priest:

    покурив... (не только сигареты...)
    пришёл к выводу что проще будет до пилить данный класс, вот что получилось
    Код (PHP):
    1. /**
    2. * Query Extended Building Class.
    3. *
    4. * @subpackage  Database
    5. * @since       woojin 17.10.2015
    6. */
    7. class JDatabaseQueryExt extends JDatabaseQuery {
    8.  
    9.     /**
    10.      * @var     JDatabaseQueryExtElement  The duplicate element.
    11.      * @since   woojin 17.10.2015
    12.      */
    13.     protected $duplicate = null;
    14.  
    15.     /**
    16.      * Add a single condition, or an array of conditions to the ON DUPLICATE KEY UPDATE clause of the query.
    17.      *
    18.      * Usage:
    19.      * $query->duplicate('a = 1')->duplicate('b = 2');
    20.      * $query->duplicate(array('a = a + 1', 'b = b * a'));
    21.      *
    22.      * @param   mixed   $conditions  A string or array of where conditions.
    23.      *
    24.      * @return  JDatabaseQueryExt  Returns this object to allow chaining.
    25.      *
    26.      * @since   by woojin 17.10.2015
    27.      */
    28.     public function duplicate($conditions) {
    29.         if (is_null($this->duplicate)) {
    30.             $this->duplicate = new JDatabaseQueryElement('ON DUPLICATE KEY UPDATE', $conditions);
    31.         } else {
    32.             $this->duplicate->append($conditions);
    33.         }
    34.  
    35.         return $this;
    36.     }
    37.  
    38.     public function __toString() {
    39.         $query = '';
    40.  
    41.         switch ($this->type) {
    42.             case 'element':
    43.                 $query .= (string) $this->element;
    44.                 break;
    45.  
    46.             case 'select':
    47.                 $query .= (string) $this->select;
    48.                 $query .= (string) $this->from;
    49.                 if ($this->join) {
    50.                     // special case for joins
    51.                     foreach ($this->join as $join) {
    52.                         $query .= (string) $join;
    53.                     }
    54.                 }
    55.  
    56.                 if ($this->where) {
    57.                     $query .= (string) $this->where;
    58.                 }
    59.  
    60.                 if ($this->group) {
    61.                     $query .= (string) $this->group;
    62.                 }
    63.  
    64.                 if ($this->having) {
    65.                     $query .= (string) $this->having;
    66.                 }
    67.  
    68.                 if ($this->order) {
    69.                     $query .= (string) $this->order;
    70.                 }
    71.  
    72.                 break;
    73.  
    74.             case 'union':
    75.                 $query .= (string) $this->union;
    76.                 break;
    77.  
    78.             case 'delete':
    79.                 $query .= (string) $this->delete;
    80.                 $query .= (string) $this->from;
    81.  
    82.                 if ($this->join) {
    83.                     // special case for joins
    84.                     foreach ($this->join as $join) {
    85.                         $query .= (string) $join;
    86.                     }
    87.                 }
    88.  
    89.                 if ($this->where) {
    90.                     $query .= (string) $this->where;
    91.                 }
    92.  
    93.                 break;
    94.  
    95.             case 'update':
    96.                 $query .= (string) $this->update;
    97.  
    98.                 if ($this->join) {
    99.                     // special case for joins
    100.                     foreach ($this->join as $join) {
    101.                         $query .= (string) $join;
    102.                     }
    103.                 }
    104.  
    105.                 $query .= (string) $this->set;
    106.  
    107.                 if ($this->where) {
    108.                     $query .= (string) $this->where;
    109.                 }
    110.  
    111.                 break;
    112.  
    113.             case 'insert':
    114.                 $query .= (string) $this->insert;
    115.  
    116.                 // Set method
    117.                 if ($this->set) {
    118.                     $query .= (string) $this->set;
    119.                 }
    120.                 // Columns-Values method
    121.                 elseif ($this->values) {
    122.                     if ($this->columns) {
    123.                         $query .= (string) $this->columns;
    124.                     }
    125.  
    126.                     $query .= ' VALUES ';
    127.                     $query .= (string) $this->values;
    128.                 }
    129. /*++++ start inserting by woojin ++++*/
    130.                 if ($this->duplicate) {
    131.                     $query .= (string) $this->duplicate;
    132.                 }
    133. /*---- stop inserting by woojin ----*/
    134.                 break;
    135.         }
    136.  
    137.         return $query;
    138.     }
    139.  
    140. }


    использование:
    Код (PHP):
    1. $db = JFactory::getDBO();
    2. $query = new JDatabaseQueryExt($db);
    3. $query->insert('#__virtuemart_attendance');
    4. $query->columns('product_id, attendance');
    5. $query->values("$product_id, 1");
    6. $query->duplicate(array('attendance = attendance+1', 'date_time_last_view = NOW()'));
    7. $db->setQuery($query);
    8. $db->query();

    вставить можно в любое место где потребуется данная конструкцияc|||

    "вроде всё просто" к/ф Жмурки J:{
    за сим разрешите откланяться с|:)
     
  2.  

Поделиться этой страницей

Загрузка...