Esempio n. 1
0
 public static function build_find_sql($input)
 {
     $query = new NimbleQuery();
     $query->from = self::table_name();
     $options = is_array(end($input)) ? array_pop($input) : NULL;
     $temp = reset($input);
     $all = false;
     $conditions = array();
     $input = static::sanatize_input_array($input);
     $num_args = count($input);
     if ($num_args > 1) {
         $all = true;
         $conditions[] = NimbleQuery::in(static::$primary_key_field, $input);
     } elseif ($num_args == 1) {
         if (!array_include((string) $temp, array('first', 'all')) && !is_numeric($temp)) {
             throw new NimbleRecordNotFound();
         }
         if (!array_include((string) $temp, array('first', 'all')) && is_numeric($temp)) {
             $conditions[] = NimbleQuery::condition(static::$primary_key_field, $temp);
         }
         switch ($input[0]) {
             case 'first':
                 $query->limit = '0,1';
                 break;
             case 'all':
                 $all = true;
                 break;
         }
     }
     if (isset($options) && !empty($options)) {
         if (isset($options['select'])) {
             $query->select = $options['select'];
         }
         if (isset($options['group'])) {
             $query->group_by = $options['group'];
         }
         if (isset($options['joins'])) {
             $query->join = $options['joins'];
         }
         if (isset($options['conditions'])) {
             if (is_array($options['conditions'])) {
                 $conditions = array_merge($conditions, static::build_conditions_from_array($options['conditions']));
             } else {
                 $conditions[] = $options['conditions'];
             }
         }
         if (isset($options['limit'])) {
             $query->limit = $options['limit'];
         }
         if (isset($options['order'])) {
             $query->order_by = $options['order'];
         }
     }
     if (!empty($conditions)) {
         $query->where = implode(' AND ', $conditions);
     }
     $sql = $query->build();
     unset($query);
     unset($options);
     return array($sql, $all);
 }
Esempio n. 2
0
 public function testIn()
 {
     $in = NimbleQuery::in('`id`', range(1, 10));
     $this->assertEquals('`id` IN(1,2,3,4,5,6,7,8,9,10)', $in);
 }