Пример #1
0
 def _compile_where_or_having(self, conditions):
     sqls, params = [], []
     for condition in conditions:
         if is_array(condition):
             condition_len = len(condition)
             if condition_len == 1:  # 只有一个,表示是一个不带参数的sql
                 sqls.append(condition[0])
             elif condition_len == 2 and isinstance(condition[1], Criteria):  # 又是一个criteria
                 sub_criteria_sql, sub_criteria_params = condition[1].to_sql()
                 sqls.append('%s (%s)' % (condition[0], sub_criteria_sql))
                 params += sub_criteria_params
             else:
                 sqls.append(condition[0])
                 params.extend(condition[1:])
         elif is_hash(condition):  # 表示是 key:value 的方式, eg. where(name='abc').where(age=1)
             for k, v in condition.iteritems():
                 if v is None:
                     sqls.append('%s IS NULL' % self._compile_fieldname(k))
                 elif isinstance(v, Criteria):  # 又是一个criteria
                     sub_criteria_sql, sub_criteria_params = v.to_sql()
                     sqls.append('%s = (%s)' % (self._compile_fieldname(k), sub_criteria_sql))
                     params += sub_criteria_params
                 elif is_array(v):
                     if v:  # 数组里面有元素
                         sqls.append('%s IN (%s)' % (self._compile_fieldname(k), self._postion_flag(v)))
                         params.extend(list(v))  # 如果是set,就必须转一下
                 else:
                     sqls.append('%s = %s' % (self._compile_fieldname(k), self.__class__.POSITION_FLAG))
                     params.append(v)
     sql = ' AND '.join(sqls)
     return sql, params
Пример #2
0
 def _compile_on(self):
     sqls, params = [], []
     for condition in self.ons:
         if is_array(condition):
             if len(condition) == 1:  # 只有一个,表示是一个不带参数的sql
                 sqls.append(condition[0])
             else:
                 sqls.append(condition[0])
                 params.extend(condition[1:])
         elif is_hash(condition):  # 表示是 key:value 的方式, eg. where(name='abc').where(age=1)
             for k, v in condition.iteritems():
                 if is_array(v):
                     if v:  # 数组里面有元素
                         sqls.append('%s IN (%s)' % (self._compile_fieldname(k), self._postion_flag(v)))
                         params.extend(list(v))  # 如果是set,就必须转一下
                 elif v is None:
                     sqls.append('%s IS NULL' % self._compile_fieldname(k))
                 else:
                     sqls.append('%s = %s' % (self._compile_fieldname(k), self.__class__.POSITION_FLAG))
                     params.append(v)
     sql = ' AND '.join(sqls)
     return sql, params
Пример #3
0
    def new(cls, name, value, qutotation, paramstyle):
        flag = '__'
        vs = name.split(flag)
        suffix = vs[-1]

        suffix_mapping = cls.mapping()
        if suffix in suffix_mapping:
            name = flag.join(vs[:-1])
            if suffix == 'not':
                if value is None:
                    operator = 'IS NOT'
                    expression_class = NullFilter
                elif is_array(value):
                    operator = 'NOT IN'
                    expression_class = InFilter
                else:
                    operator = '<>'
                    expression_class = SimpleFilter
            else:
                operator, expression_class = suffix_mapping[suffix]
        else:
            if value is None:
                operator = 'IS'
                expression_class = NullFilter
            elif is_array(value):
                operator = 'IN'
                expression_class = InFilter
            else:
                operator = '='
                expression_class = SimpleFilter

        expression = expression_class(name, operator, value)
        expression.qutotation = qutotation
        expression.paramstyle = paramstyle
        expression.name = name.replace(flag, '.')
        return expression
Пример #4
0
 def valid(self):
     if not is_array(self.value) or len(self.value) != 2:
         raise TypeError("%s just support a array which has 2 elements" % self.value) 
     return True