def _search(self, model, search): table = model.replace('.', '_') search_params = literal_eval(search) q = OOQuery(table, lambda t: get_foreign_keys(self.cursor, t)) sql = q.select(['id']).where(search_params) self.cursor.execute(*sql) return self.cursor.fetchone()[0]
def migrate(self): obj = objectify.fromstring(self.content) t = Table('ir_model_data') for xml_record in obj.iter(tag='record'): record = self._record(xml_record) self.records.append(record) sp = [] for field in self.search_params.get(record.model, record.vals.keys()): sp.append((field, '=', record.vals[field])) logger.info('Trying to find existing record with query: {}'.format( sp )) table = record.model.replace('.', '_') q = OOQuery(table) sql = q.select(['id']).where(sp) logger.debug(tuple(sql)) self.cursor.execute(*sql) res_id = self.cursor.fetchone() if res_id: res_id = res_id[0] logger.info('Record {}.{} found! ({} id:{})'.format( self.module, record.id, record.model, res_id )) else: logger.info('Record {}.{} not found!'.format( self.module, record.id )) # We have to create the model table_model = Table(record.model.replace('.', '_')) columns = [] values = [] for col, value in record.vals.items(): columns.append(getattr(table_model, col)) values.append(value) sql = table_model.insert( columns=columns, values=[values], returning=[table_model.id] ) logger.debug(tuple(sql)) self.cursor.execute(*sql) res_id = self.cursor.fetchone()[0] logger.info('Creating record {}.{} ({} id:{})'.format( self.module, record.id, record.model, res_id )) sql = t.insert( columns=[t.name, t.model, t.noupdate, t.res_id, t.module], values=[(record.id, record.model, record.noupdate, res_id, self.module)] ) logger.debug(tuple(sql)) logger.info('Linking model data {}.{} -> record {} id:{}'.format( self.module, record.id, record.model, res_id )) self.cursor.execute(*sql)
from ooquery import OOQuery from ooquery.expression import Field from ooquery.operators import * from sql import Table, Literal, NullsFirst, NullsLast from sql.operators import And, Concat from sql.aggregate import Max from sql.conditionals import Coalesce, Greatest, Least from expects import * from mamba import * with description('The OOQuery object'): with description('when creating an ooquery object'): with it('should get the table to query'): q = OOQuery('table') expect(q.table).to(be_a(Table)) with it('should have a select method which returns table.attr'): q = OOQuery('table') sel = q.select(['field1', 'field2']) sel2 = q.table.select(q.table.field1.as_('field1'), q.table.field2.as_('field2')) expect(str(sel._select)).to(equal(str(sel2))) with it('should have a where method to pass the domain'): q = OOQuery('table') sql = q.select(['field1', 'field2']).where([('field3', '=', 4)]) t = Table('table') sel = t.select(t.field1.as_('field1'), t.field2.as_('field2')) sel.where = And((t.field3 == 4,)) expect(tuple(sql)).to(equal(tuple(sel))) with it('should have where method and compare two fields of the table'):
# coding=utf-8 from ooquery import OOQuery from sql import Table from sql.operators import * from expects import * with description('The OOQuery object'): with description('when creating an ooquery object'): with it('should get the table to query'): q = OOQuery('table') expect(q.table).to(be_a(Table)) with it('should have a select method which returns table.attr'): q = OOQuery('table') sel = q.select(['field1', 'field2']) sel2 = q.table.select(q.table.field1, q.table.field2) expect(str(sel._select)).to(equal(str(sel2))) with it('should have a where method to pass the domain'): q = OOQuery('table') sql = q.select(['field1', 'field2']).where([('field3', '=', 4)]) t = Table('table') sel = t.select(t.field1, t.field2) sel.where = And((t.field3 == 4,)) expect(tuple(sql)).to(equal(tuple(sel))) with it('must support joins'): def dummy_fk(table): return { 'table_2': { 'constraint_name': 'fk_contraint_name',