<s> from PyQt4 . QtCore import QAbstractTableModel , Qt <EOL> import operator <EOL> class QPySelectModel ( QAbstractTableModel ) : <EOL> _rows = [ ] <EOL> _statement = None <EOL> def __init__ ( self , parent , db ) : <EOL> QAbstractTableModel . __init__ ( self , parent ) <EOL> self . db = db <EOL> self . cursor = db . cursor ( ) <EOL> def __del__ ( self ) : <EOL> self . cursor . close ( ) <EOL> def select ( self ) : <EOL> self . _rows = [ ] <EOL> self . _types = [ ] <EOL> if self . _statement is not None : <EOL> self . cursor . execute ( self . _statement ) <EOL> self . setResult ( self . cursor . fetchall ( ) , self . cursor ) <EOL> def setResult ( self , result , cursor ) : <EOL> self . cursor = cursor <EOL> self . _rows = [ ] <EOL> self . _types = [ ] <EOL> for row in result : <EOL> self . _rows . append ( list ( row ) ) <EOL> if self . _rows : <EOL> self . _types = [ description [ <NUM_LIT:1> ] for description in cursor . description ] <EOL> def setSelect ( self , statement ) : <EOL> self . _statement = statement <EOL> def rowCount ( self , parent ) : <EOL> return len ( self . _rows ) <EOL> def columnCount ( self , parent ) : <EOL> return <NUM_LIT:0> if self . cursor . description is None else len ( self . cursor . description ) <EOL> def data ( self , index , role ) : <EOL> if not self . _rows or not index . isValid ( ) or not role in [ Qt . DisplayRole , Qt . UserRole , Qt . EditRole ] or index . row ( ) >= len ( self . _rows ) or index . column ( ) >= len ( self . _rows [ index . row ( ) ] ) : <EOL> return None <EOL> data = self . _rows [ index . row ( ) ] [ index . column ( ) ] <EOL> try : <EOL> data = "<STR_LIT>" if data is None else unicode ( data ) <EOL> except UnicodeDecodeError : <EOL> data = "<STR_LIT>" <EOL> return data <EOL> def headerData ( self , section , orientation , role ) : <EOL> if role != Qt . DisplayRole or orientation == Qt . Vertical : <EOL> return None <EOL> try : <EOL> return self . cursor . description [ section ] [ <NUM_LIT:0> ] <EOL> except : <EOL> return None <EOL> def sort ( self , ncol , order ) : <EOL> self . layoutAboutToBeChanged . emit ( ) <EOL> self . _rows = sorted ( self . _rows , key = operator . itemgetter ( ncol ) , reverse = ( order == Qt . DescendingOrder ) ) <EOL> self . layoutChanged . emit ( ) <EOL> class QPyTableModel ( QPySelectModel ) : <EOL> def __init__ ( self , parent , db ) : <EOL> QPySelectModel . __init__ ( self , parent , db ) <EOL> self . _tableName = None <EOL> self . _filter = "<STR_LIT>" <EOL> self . _limit = <NUM_LIT:0> <EOL> def flags ( self , index ) : <EOL> if not index . isValid ( ) : <EOL> return Qt . ItemIsEnabled <EOL> return QAbstractTableModel . flags ( self , index ) | Qt . ItemIsEditable <EOL> def setData ( self , index , value , role ) : <EOL> if index . isValid ( ) and role == Qt . EditRole : <EOL> row = self . _rows [ index . row ( ) ] <EOL> where = [ ] <EOL> values = [ value ] <EOL> if self . _primary_columns : <EOL> for primary_column in self . _primary_columns : <EOL> for i , column in enumerate ( self . cursor . description ) : <EOL> if column [ <NUM_LIT:0> ] == primary_column : <EOL> where . append ( "<STR_LIT>" % self . db . quoteIdentifier ( primary_column ) ) <EOL> values . append ( row [ i ] ) <EOL> break <EOL> else : <EOL> for i , column in enumerate ( self . cursor . description ) : <EOL> if row [ i ] is None : <EOL> where . append ( "<STR_LIT>" % self . db . quoteIdentifier ( column [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> where . append ( "<STR_LIT>" % self . db . quoteIdentifier ( column [ <NUM_LIT:0> ] ) ) <EOL> values . append ( row [ i ] ) <EOL> query = "<STR_LIT>" % ( self . db . quoteIdentifier ( self . _tableName ) , self . db . quoteIdentifier ( self . cursor . description [ index . column ( ) ] [ <NUM_LIT:0> ] ) , "<STR_LIT>" . join ( where ) ) <EOL> cursor = self . db . cursor ( ) <EOL> cursor . execute ( query , values ) <EOL> self . _rows [ index . row ( ) ] [ index . column ( ) ] = value <EOL> self . dataChanged . emit ( index , index ) <EOL> return True <EOL> return False <EOL> def setTable ( self , tableName ) : <EOL> self . _tableName = tableName <EOL> def setFilter ( self , where = "<STR_LIT>" ) : <EOL> self . _filter = where <EOL> def setLimit ( self , limit = <NUM_LIT:0> ) : <EOL> self . _limit = int ( limit ) <EOL> def select ( self , primary_columns = None ) : <EOL> self . _primary_columns = [ ] if primary_columns is None else primary_columns <EOL> statement = "<STR_LIT>" % self . db . quoteIdentifier ( self . _tableName ) <EOL> if self . _filter : <EOL> statement += "<STR_LIT>" % self . _filter <EOL> if self . _limit : <EOL> statement += "<STR_LIT>" % self . _limit <EOL> self . setSelect ( statement ) <EOL> QPySelectModel . select ( self ) </s>
