pgtoolkit/pgdatadiff.py
changeset 41 6aad5e35efe8
parent 31 c2e6e24b83d9
child 54 291473ab847c
equal deleted inserted replaced
40:922d7fb63384 41:6aad5e35efe8
   160         Yields one line at the time.
   160         Yields one line at the time.
   161         
   161         
   162         '''
   162         '''
   163         curs1, curs2 = self._select()
   163         curs1, curs2 = self._select()
   164         
   164         
   165         row1 = curs1.fetchone_adapted()
   165         row1 = curs1.fetchone_dict()
   166         row2 = curs2.fetchone_adapted()
   166         row2 = curs2.fetchone_dict()
   167         
   167         
   168         while True:
   168         while True:
   169             if row1 is None and row2 is None:
   169             if row1 is None and row2 is None:
   170                 break
   170                 break
   171             diff = self._compare_row(row1, row2)
   171             diff = self._compare_row(row1, row2, curs1.adapt, curs2.adapt)
   172             
   172             
   173             if diff:
   173             if diff:
   174                 yield diff
   174                 yield diff
   175             
   175             
   176                 if diff.change == '-':
   176                 if diff.change == '-':
   177                     row1 = curs1.fetchone_adapted()
   177                     row1 = curs1.fetchone_dict()
   178                     continue
   178                     continue
   179                 if diff.change == '+':
   179                 if diff.change == '+':
   180                     row2 = curs2.fetchone_adapted()
   180                     row2 = curs2.fetchone_dict()
   181                     continue
   181                     continue
   182             # change == '*' or not diff
   182             # change == '*' or not diff
   183             row1 = curs1.fetchone_adapted()
   183             row1 = curs1.fetchone_dict()
   184             row2 = curs2.fetchone_adapted()
   184             row2 = curs2.fetchone_dict()
       
   185         
       
   186         curs1.close()
       
   187         curs2.close()
   185     
   188     
   186     def print_diff(self):
   189     def print_diff(self):
   187         '''Print differencies between data of two tables.
   190         '''Print differencies between data of two tables.
   188         
   191         
   189         The output is in human readable form.
   192         The output is in human readable form.
   220         self.pkeycolnames = pkey['columns']
   223         self.pkeycolnames = pkey['columns']
   221         
   224         
   222         query1 = 'SELECT ' + columns_sel + ' FROM ' + self.fulltable1 + ' ORDER BY ' + pkey_sel
   225         query1 = 'SELECT ' + columns_sel + ' FROM ' + self.fulltable1 + ' ORDER BY ' + pkey_sel
   223         query2 = 'SELECT ' + columns_sel + ' FROM ' + self.fulltable2 + ' ORDER BY ' + pkey_sel
   226         query2 = 'SELECT ' + columns_sel + ' FROM ' + self.fulltable2 + ' ORDER BY ' + pkey_sel
   224         
   227         
   225         curs1 = self.conn1.cursor()
   228         curs1 = self.conn1.cursor('curs1')
   226         curs2 = self.conn2.cursor()
   229         curs2 = self.conn2.cursor('curs2')
   227         
   230         
   228         curs1.execute(query1)
   231         curs1.execute(query1)
   229         curs2.execute(query2)
   232         curs2.execute(query2)
   230         
   233         
   231         return curs1, curs2
   234         return curs1, curs2
   241             key = OrderedDict(zip(self.pkeycolnames, [row1[colname] for colname in self.pkeycolnames]))
   244             key = OrderedDict(zip(self.pkeycolnames, [row1[colname] for colname in self.pkeycolnames]))
   242             return DiffData('*', cols1, cols2, key=key)
   245             return DiffData('*', cols1, cols2, key=key)
   243         
   246         
   244         return None
   247         return None
   245     
   248     
   246     def _compare_row(self, row1, row2):
   249     def _compare_row(self, row1, row2, adapt1, adapt2):
   247         if row2 is None:
   250         if row2 is None:
       
   251             row1 = adapt1(row1)
   248             key = OrderedDict(zip(self.pkeycolnames, [row1[colname] for colname in self.pkeycolnames]))
   252             key = OrderedDict(zip(self.pkeycolnames, [row1[colname] for colname in self.pkeycolnames]))
   249             return DiffData('-', row1, None, key=key)
   253             return DiffData('-', row1, None, key=key)
   250         if row1 is None:
   254         if row1 is None:
       
   255             row2 = adapt2(row2)
   251             return DiffData('+', None, row2)
   256             return DiffData('+', None, row2)
   252         
       
   253         
   257         
   254         for keyname in self.pkeycolnames:
   258         for keyname in self.pkeycolnames:
   255             if row1[keyname] < row2[keyname]:
   259             if row1[keyname] < row2[keyname]:
       
   260                 row1 = adapt1(row1)
   256                 key = OrderedDict(zip(self.pkeycolnames, [row1[colname] for colname in self.pkeycolnames]))
   261                 key = OrderedDict(zip(self.pkeycolnames, [row1[colname] for colname in self.pkeycolnames]))
   257                 return DiffData('-', row1, None, key=key)
   262                 return DiffData('-', row1, None, key=key)
   258         for keyname in self.pkeycolnames:
   263         for keyname in self.pkeycolnames:
   259             if row1[keyname] > row2[keyname]:
   264             if row1[keyname] > row2[keyname]:
       
   265                 row2 = adapt2(row2)
   260                 return DiffData('+', None, row2)
   266                 return DiffData('+', None, row2)
   261         
   267         
       
   268         row1 = adapt1(row1)
       
   269         row2 = adapt2(row2)
   262         return self._compare_data(row1, row2)
   270         return self._compare_data(row1, row2)
   263 
   271