Example #1
0
File: db.py Project: Xifax/suzu
    def clearDB(self):
#        Kanji.query.delete()
#        Word.query.delete()
#        Example.query.delete()
        
        #NB: for table in reversed(meta.Base.metadata.sorted_tables): meta.Session.execute(table.delete()); meta.Session.commit()
        
        try:
            for table in reversed(metadata.sorted_tables) : session.execute(table.delete()); session.commit()
#            session.commit()
        except Exception, e:
            session.rollback()
            log.error(e)
Example #2
0
    def get_name_and_options(self, admin):
        """return a tuple of the name of the filter and a list of options that can be selected. 
        Each option is a tuple of the name of the option, and a filter function to
        decorate a query
        @return:  (filter_name, [(option_name, query_decorator), ...)
        """
        from sqlalchemy.sql import select
        from sqlalchemy import orm
        from elixir import session
        filter_names = []
        joins = []
        table = admin.entity.table
        path = self.attribute.split('.')
        for field_name in path:
            attributes = admin.get_field_attributes(field_name)
            filter_names.append(attributes['name'])
            # @todo: if the filter is not on an attribute of the relation, but on the relation itselves
            if 'target' in attributes:
                admin = attributes['admin']
                joins.append(field_name)
                if attributes['direction'] == orm.interfaces.MANYTOONE:
                    table = admin.entity.table.join(table)
                else:
                    table = admin.entity.table

        col = getattr(admin.entity, field_name)
        query = select([col], distinct=True,
                       order_by=col.asc()).select_from(table)

        def create_decorator(col, attributes, value, joins):
            def decorator(q):
                if joins:
                    q = q.join(joins, aliased=True)
                if 'precision' in attributes:
                    delta = pow(10, -1 * attributes['precision'])
                    return q.filter(
                        sql.and_(col < value + delta, col > value - delta))
                return q.filter(col == value)

            return decorator

        options = [(_(self._value_to_string(value[0])),
                    create_decorator(col, attributes, value[0], joins))
                   for value in session.execute(query)]

        return (filter_names[0], [(_('all'), lambda q: q)] + options)
Example #3
0
 def get_name_and_options(self, admin):
     """return a tuple of the name of the filter and a list of options that can be selected. 
     Each option is a tuple of the name of the option, and a filter function to
     decorate a query
     @return:  (filter_name, [(option_name, query_decorator), ...)
     """
     from sqlalchemy.sql import select
     from sqlalchemy import orm
     from elixir import session
     filter_names = []
     joins = []
     table = admin.entity.table
     path = self.attribute.split('.')
     for field_name in path:
         attributes = admin.get_field_attributes(field_name)
         filter_names.append(attributes['name'])
         # @todo: if the filter is not on an attribute of the relation, but on the relation itselves
         if 'target' in attributes:
             admin = attributes['admin']
             joins.append(field_name)
             if attributes['direction'] == orm.interfaces.MANYTOONE:
                 table = admin.entity.table.join(table)
             else:
                 table = admin.entity.table
     col = getattr( admin.entity, field_name )
     query = select([col], distinct=True, order_by=col.asc()).select_from(table)
       
     def create_decorator(col, attributes, value, joins):
       
         def decorator(q):
             if joins:
                 q = q.join(joins, aliased=True)
             if 'precision' in attributes:
                 delta = pow( 10,  -1*attributes['precision'])
                 return q.filter( sql.and_(col < value+delta, col > value-delta) )
             return q.filter(col==value)
           
         return decorator
   
     options = [(_(self._value_to_string(value[0])), create_decorator(col, attributes, value[0], joins))
                for value in session.execute(query)]
 
Example #4
0
File: db.py Project: Xifax/suzu
        
    def clearDB(self):
#        Kanji.query.delete()
#        Word.query.delete()
#        Example.query.delete()
        
        #NB: for table in reversed(meta.Base.metadata.sorted_tables): meta.Session.execute(table.delete()); meta.Session.commit()
        
        try:
            for table in reversed(metadata.sorted_tables) : session.execute(table.delete()); session.commit()
#            session.commit()
        except Exception, e:
            session.rollback()
            log.error(e)
            
        session.execute('VACUUM')
            
class DictionaryLookup:
    
    #FIXME: REFACTOR, REFACTOR, friggin' REFACTOR all this mess!
    def __init__(self):
        self.db = SqlSoup(SQLITE + PATH_TO_RES + JMDICT)
        self.dictionary = {}
        
    def joinTables(self):
        '''Join tables on init for better perfomance'''
        join_word = self.db.join(self.db.k_ele, self.db.r_ele, self.db.k_ele.fk==self.db.r_ele.fk, isouter=True)
        join_sense = self.db.join(join_word, self.db.sense, join_word.fk==self.db.sense.fk )
        join_sense_labels = self.db.with_labels(join_sense)
        
        self.join_gloss = self.db.join(join_sense_labels, self.db.gloss, join_sense_labels.sense_id==self.db.gloss.fk)
Example #5
0
 def clearDB(self):
     Item.query.delete()
     session.commit()
     session.execute('VACUUM')