コード例 #1
0
ファイル: table.php プロジェクト: NavaINT1876/ccustoms
 /**
  * Builds the query using the given options
  *
  * @param array $options The list of options for the query (select, from, conditions, group, order, limit, offset)
  *
  * @return string The query
  *
  * @since 1.0.0
  */
 protected function _select(array $options)
 {
     // select
     $query[] = sprintf('SELECT %s', isset($options['select']) ? $options['select'] : '*');
     // from
     $query[] = sprintf('FROM %s', isset($options['from']) ? $options['from'] : $this->name);
     // where
     if (isset($options['conditions'])) {
         $condition = '';
         $conditions = (array) $options['conditions'];
         // parse condition
         $parts = explode('?', array_shift($conditions));
         foreach ($parts as $part) {
             $condition .= $part . $this->database->escape(array_shift($conditions));
         }
         if (!empty($condition)) {
             $query[] = sprintf('WHERE %s', $condition);
         }
     }
     // group by
     if (isset($options['group'])) {
         $query[] = sprintf('GROUP BY %s', $options['group']);
     }
     // order
     if (isset($options['order'])) {
         $query[] = sprintf('ORDER BY %s', $options['order']);
     }
     // offset & limit
     if (isset($options['offset']) || isset($options['limit'])) {
         $offset = isset($options['offset']) ? (int) $options['offset'] : 0;
         $limit = isset($options['limit']) ? (int) $options['limit'] : 0;
         $query[] = sprintf('LIMIT %s, %s', $offset, $limit);
     }
     return implode(' ', $query);
 }