Exemplo n.º 1
0
    return expr


class UnionAll(SetExpr):
    """Union all the results

    UNION is to UNION ALL what a python's set is to a list. UNION
    will remove duplicates from the resulting rows while UNION ALL
    will just join all data, making it a little bit faster but possibly
    with more rows.
    """
    __slots__ = ()
    oper = " UNION ALL "


expr_compile.set_precedence(10, UnionAll)


def is_sql_identifier(identifier):
    return (not expr_compile.is_reserved_word(identifier) and
            is_safe_token(identifier))


class Over(ComparableExpr):
    """Check if value is between start and end

    Usage:

    Over(attr, [partitions], [order by])

    e.g.:
Exemplo n.º 2
0
@compile.when(Select)
def compile_select_sqlite(compile, select, state):
    if select.offset is not Undef and select.limit is Undef:
        select.limit = sys.maxint
    statement = compile_select(compile, select, state)
    if state.context is SELECT:
        # SQLite breaks with (SELECT ...) UNION (SELECT ...), so we
        # do SELECT * FROM (SELECT ...) instead.  This is important
        # because SELECT ... UNION SELECT ... ORDER BY binds the ORDER BY
        # to the UNION instead of SELECT.
        return "SELECT * FROM (%s)" % statement
    return statement

# Considering the above, selects have a greater precedence.
compile.set_precedence(5, Union, Except, Intersect)

@compile.when(Insert)
def compile_insert_sqlite(compile, insert, state):
    # SQLite fails with INSERT INTO table VALUES (), so we transform
    # that to INSERT INTO table (id) VALUES (NULL).
    if not insert.map and insert.primary_columns is not Undef:
        insert.map.update(dict.fromkeys(insert.primary_columns, None))
    return compile_insert(compile, insert, state)


class SQLiteResult(Result):

    def get_insert_identity(self, primary_key, primary_variables):
        return SQLRaw("(OID=%d)" % self._raw_cursor.lastrowid)
Exemplo n.º 3
0
            # On 64-bit platforms sqlite doesn't like maxint as LIMIT. See also
            # https://lists.ubuntu.com/archives/storm/2013-June/001492.html
            select.limit = sys.maxsize - 1
        else:
            select.limit = sys.maxsize
    statement = compile_select(compile, select, state)
    if state.context is SELECT:
        # SQLite breaks with (SELECT ...) UNION (SELECT ...), so we
        # do SELECT * FROM (SELECT ...) instead.  This is important
        # because SELECT ... UNION SELECT ... ORDER BY binds the ORDER BY
        # to the UNION instead of SELECT.
        return "SELECT * FROM (%s)" % statement
    return statement

# Considering the above, selects have a greater precedence.
compile.set_precedence(5, Union, Except, Intersect)

@compile.when(Insert)
def compile_insert_sqlite(compile, insert, state):
    # SQLite fails with INSERT INTO table VALUES (), so we transform
    # that to INSERT INTO table (id) VALUES (NULL).
    if not insert.map and insert.primary_columns is not Undef:
        insert.map.update(dict.fromkeys(insert.primary_columns, None))
    return compile_insert(compile, insert, state)


class SQLiteResult(Result):

    def get_insert_identity(self, primary_key, primary_variables):
        return SQLRaw("(OID=%d)" % self._raw_cursor.lastrowid)
Exemplo n.º 4
0
    state.push("context", EXPR)
    if expr.step is Undef:
        expr = 'generate_series(%s, %s)' % (expr_compile(expr.start, state),
                                            expr_compile(expr.end, state))
    else:
        expr = 'generate_series(%s, %s, %s)' % (expr_compile(expr.start, state),
                                                expr_compile(expr.end, state),
                                                expr_compile(expr.step, state))
    state.pop()
    return expr


class UnionAll(SetExpr):
    """Union all the results

    UNION is to UNION ALL what a python's set is to a list. UNION
    will remove duplicates from the resulting rows while UNION ALL
    will just join all data, making it a little bit faster but possibly
    with more rows.
    """
    __slots__ = ()
    oper = " UNION ALL "


expr_compile.set_precedence(10, UnionAll)


def is_sql_identifier(identifier):
    return (not expr_compile.is_reserved_word(identifier) and
            is_safe_token(identifier))
Exemplo n.º 5
0
    __slots__ = ()
    suffix = "NULLS FIRST"


class NullsLast(SuffixExpr):
    """Order null values after non-null values."""
    __slots__ = ()
    suffix = "NULLS LAST"


class RegexpMatch(BinaryOper):
    __slots__ = ()
    oper = " ~ "


compile.set_precedence(compile.get_precedence(Like), RegexpMatch)


def get_where_for_reference(reference, other):
    """Generate a column comparison expression for a reference property.

    The returned expression may be used to find referenced objects referring
    to C{other}.

    If the right hand side is a collection of values, then an OR in IN
    expression is returned - if the relation uses composite keys, then an OR
    expression is used; single key references produce an IN expression which is
    more efficient for large collections of values.
    """
    relation = reference._relation
    if isinstance(other, (