Exemplo n.º 1
0
 def __init__(self, column_name):
     column = ColumnClause(column_name)
     super(FilenameConstraint, self).__init__(
         and_(
             column.op("~")(literal_column("'^[A-Za-z0-9_.%-]+$'")),
             column != literal_column("'.'"),
             column != literal_column("'..'")))
Exemplo n.º 2
0
 def add_join(self,
              child_table,
              parent_column_name,
              child_column_name,
              is_outer=False):
     parent_column = ColumnClause(parent_column_name)
     child_column = ColumnClause(child_column_name)
     condition = (parent_column == child_column)
     self.joins.append((child_table, condition, is_outer))
Exemplo n.º 3
0
    def __init__(self, columns, param, bool=False, *args, **kwargs):
        if isinstance(columns, list):
            columns = ', '.join(str(c) for c in columns)
        if isinstance(param, basestring):
            param = sql.bindparam(param)
        bool_mode = bool and ' IN BOOLEAN MODE' or ''

        self.text = 'MATCH (%s) AGAINST (%s%s)' % (columns, param, bool_mode)
        self.bindparams[param.key] = param

        _ColumnClause.__init__(self, self.text, *args, **kwargs)
Exemplo n.º 4
0
 def __init__(self, Session, table, select, **kw):
     self.table = table
     self.select = select
     self.parent = None
     self.child = None
     self._bind = Session.bind
     self._whereclause = select._whereclause
     self.fk_type = None
     self.reverse = kw.pop('reverse', False)
     # we need to find the relation within the same table
     for ev in self.table.foreign_keys:
         if ev.column.table.name == ev.parent.table.name:
             if not self.reverse:
                 self.parent = ev.parent.name
                 self.child = ev.column.name
             else:
                 self.parent = ev.column.name
                 self.child = ev.parent.name
             break
     if self.parent is None or self.child is None:
         raise MissingForeignKeyError(self.table.name)
     self.starting_node = kw.pop('starting_node', None)
     self.ordering_colname = kw.pop('ordering_colname', 'ordering')
     # if starting node does not exist or it's null, we add starting_node=0
     # by default
     if not hasattr(self, 'starting_node') or self.starting_node is None:
         self.fk_type = self.table.columns.get(self.parent).type._type_affinity
         # sqlalchemy cast "0" as 0 (I don't know how to fix it) so you will
         # get errors if the parent-child relation is not an integer. We
         # identify this situation and use "a" for comparison
         if self.fk_type == String:
             setattr(self, 'starting_node', "a")
             self.type_length = self.table.columns.get(self.parent).type.length
         else:
             setattr(self, 'starting_node', "0")
     elif self.starting_node is False:
         pass
     else:
         # we need to be sure the starting_node value is an String, 
         # otherwise we might get an error
         self.starting_node = str(self.starting_node)
     columns = select.columns + [
         ColumnClause('level', type_=Integer()),
         ColumnClause('connect_path', type_=ARRAY(self.fk_type)),
         ColumnClause('is_leaf', type_=Boolean())
     ]
     if self.ordering_colname in select.columns:
         columns.append(ColumnClause('%s_path' % self.ordering_colname,
                                     type_=ARRAY(select.c[self.ordering_colname].type)))
     Select.__init__(self, columns, **kw)
Exemplo n.º 5
0
def _build_table_clause(select, name, path_type):
    """For pgsql, it builds the recursive table needed to perform a
    hierarchical query.
    Parameters:
        * select instruction of type sqlalchemy.sql.expression.Select
        * a name for the new virtual table
        * the type for the connect_path column
    It returns a TableClause object
    """
    cols = []
    for ev in select.columns.keys():
        cols.append(ColumnClause(ev, type_=getattr(select.columns, ev).type))
    cols.append(ColumnClause('level', Integer))
    cols.append(ColumnClause('connect_path', ARRAY(path_type)))
    tb = TableClause(name, *cols)
    return tb
Exemplo n.º 6
0
 def __init__(self, column_name):
     column = ColumnClause(column_name)
     # Postgres allows the condition "<sth> <op> ALL (<array>)" that
     # is true iff for all elements of array "<sth> <op> <element>".
     # This works for (in)equality but, as the order of the operands
     # is preserved, it doesn't work for regexp matching, where the
     # syntax is "<text> ~ <pattern>". Our regexp operates on a per
     # character basis so we can work around it by concatenating the
     # items of the array (using array_to_string) and match the
     # regexp on the result.
     empty = literal_column("''")
     super(FilenameListConstraint, self).__init__(
         and_(
             func.array_to_string(column, empty).op("~")(
                 literal_column("'^[A-Za-z0-9_.%-]*$'")),
             empty != all_(column),
             literal_column("'.'") != all_(column),
             literal_column("'..'") != all_(column)))
Exemplo n.º 7
0
 def __init__(self, column_name):
     column = ColumnClause(column_name)
     super(DigestConstraint, self).__init__(
         column.op("~")(literal_column("'^([0-9a-f]{40}|%s)$'" %
                                       FileCacher.TOMBSTONE_DIGEST)))
Exemplo n.º 8
0
 def __init__(self, column_name):
     column = ColumnClause(column_name)
     super(CodenameConstraint, self).__init__(
         column.op("~")(literal_column("'^[A-Za-z0-9_-]+$'")))
Exemplo n.º 9
0
 def __init__(self, column):
     self.column = column
     ColumnClause.__init__(self, column.name, column.table)
Exemplo n.º 10
0
 def __init__(self, column):
     self.column = column
     ColumnClause.__init__(self, column.name, column.table)
Exemplo n.º 11
0
 def __init__(self, column_name):
     column = ColumnClause(column_name)
     super(DigestConstraint, self).__init__(
         column.op("~")(literal_column(
             "'^([0-9a-f]{40}|%s)$'" % FileCacher.TOMBSTONE_DIGEST)))
Exemplo n.º 12
0
 def __init__(self, column_name):
     column = ColumnClause(column_name)
     super(FilenameConstraint, self).__init__(and_(
         column.op("~")(literal_column("'^[A-Za-z0-9_.%-]+$'")),
         column != literal_column("'.'"),
         column != literal_column("'..'")))
Exemplo n.º 13
0
 def __init__(self, column_name):
     column = ColumnClause(column_name)
     super(CodenameConstraint, self).__init__(
         column.op("~")(literal_column("'^[A-Za-z0-9_-]+$'")))