def _filter(self, *rules): for op in operators.op_iter(rules): if not op.lval_name.startswith('_'): self._use_data = True self._rules += rules return self
def find_things(type_id, get_cols, sort, limit, constraints): table = get_thing_table(type_id)[0] constraints = deepcopy(constraints) s = sa.select([table.c.thing_id.label('thing_id')]) for op in operators.op_iter(constraints): #assume key starts with _ #if key.startswith('_'): key = op.lval_name op.lval = translate_sort(table, key[1:], op.lval) op.rval = translate_thing_value(op.rval) for op in constraints: s.append_whereclause(sa_op(op)) if sort: s, cols = add_sort(sort, {'_': table}, s) if limit: s = s.limit(limit) try: r = add_request_info(s).execute() except Exception, e: dbm.mark_dead(table.bind) # this thread must die so that others may live raise
def find_data(type_id, get_cols, sort, limit, constraints): d_table, t_table = types_id[type_id].data_table constraints = deepcopy(constraints) used_first = False s = None need_join = False have_data_rule = False first_alias = d_table.alias() s = sa.select([first_alias.c.thing_id.label('thing_id')])#, distinct=True) for op in operators.op_iter(constraints): key = op.lval_name vals = tup(op.rval) if key == '_id': op.lval = first_alias.c.thing_id elif key.startswith('_'): need_join = True op.lval = translate_sort(t_table, key[1:], op.lval) op.rval = translate_thing_value(op.rval) else: have_data_rule = True id_col = None if not used_first: alias = first_alias used_first = True else: alias = d_table.alias() id_col = first_alias.c.thing_id if id_col: s.append_whereclause(id_col == alias.c.thing_id) s.append_column(alias.c.value.label(key)) s.append_whereclause(alias.c.key == key) #add the substring constraint if no other functions are there translate_data_value(alias, op) for op in constraints: s.append_whereclause(sa_op(op)) if not have_data_rule: raise Exception('Data queries must have at least one data rule.') #TODO in order to sort by data columns, this is going to need to be smarter if sort: need_join = True add_sort(sort, {'_':t_table}, s) if need_join: s.append_whereclause(first_alias.c.thing_id == t_table.c.thing_id) if limit: s.limit = limit r = s.execute() return Results(r, lambda(row): row if get_cols else row.thing_id)
def find_things(type_id, get_cols, sort, limit, constraints): table = types_id[type_id].thing_table constraints = deepcopy(constraints) s = sa.select([table.c.thing_id.label('thing_id')]) for op in operators.op_iter(constraints): #assume key starts with _ #if key.startswith('_'): key = op.lval_name op.lval = translate_sort(table, key[1:], op.lval) op.rval = translate_thing_value(op.rval) for op in constraints: s.append_whereclause(sa_op(op)) if sort: add_sort(sort, {'_': table}, s) if limit: s.limit = limit r = s.execute() return Results(r, lambda(row): row if get_cols else row.thing_id)
def find_rels(rel_type_id, get_cols, sort, limit, constraints): tables = get_rel_table(rel_type_id) r_table, t1_table, t2_table, d_table = tables constraints = deepcopy(constraints) t1_table, t2_table = t1_table.alias(), t2_table.alias() s = sa.select([r_table.c.rel_id.label('rel_id')]) need_join1 = ('thing1_id', t1_table) need_join2 = ('thing2_id', t2_table) joins_needed = set() for op in operators.op_iter(constraints): #vals = con.rval key = op.lval_name prefix = key[:4] if prefix in ('_t1_', '_t2_'): #not a thing attribute key = key[4:] if prefix == '_t1_': join = need_join1 joins_needed.add(join) elif prefix == '_t2_': join = need_join2 joins_needed.add(join) table = join[1] op.lval = translate_sort(table, key, op.lval) op.rval = translate_thing_value(op.rval) #ors = [sa_op(con, key, v) for v in vals] #s.append_whereclause(sa.or_(*ors)) elif prefix.startswith('_'): op.lval = r_table.c[key[1:]] else: alias = d_table.alias() s.append_whereclause(r_table.c.rel_id == alias.c.thing_id) s.append_column(alias.c.value.label(key)) s.append_whereclause(alias.c.key == key) translate_data_value(alias, op) for op in constraints: s.append_whereclause(sa_op(op)) if sort: s, cols = add_sort(sort, {'_':r_table, '_t1_':t1_table, '_t2_':t2_table}, s) #do we need more joins? for (col, table) in cols: if table == need_join1[1]: joins_needed.add(need_join1) elif table == need_join2[1]: joins_needed.add(need_join2) for j in joins_needed: col, table = j s.append_whereclause(r_table.c[col] == table.c.thing_id) if limit: s = s.limit(limit) try: r = add_request_info(s).execute() except Exception, e: dbm.mark_dead(r_table.bind) # this thread must die so that others may live raise
def find_data(type_id, get_cols, sort, limit, constraints): t_table, d_table = get_thing_table(type_id) constraints = deepcopy(constraints) used_first = False s = None need_join = False have_data_rule = False first_alias = d_table.alias() s = sa.select([first_alias.c.thing_id.label('thing_id')])#, distinct=True) for op in operators.op_iter(constraints): key = op.lval_name vals = tup(op.rval) if key == '_id': op.lval = first_alias.c.thing_id elif key.startswith('_'): need_join = True op.lval = translate_sort(t_table, key[1:], op.lval) op.rval = translate_thing_value(op.rval) else: have_data_rule = True id_col = None if not used_first: alias = first_alias used_first = True else: alias = d_table.alias() id_col = first_alias.c.thing_id if id_col: s.append_whereclause(id_col == alias.c.thing_id) s.append_column(alias.c.value.label(key)) s.append_whereclause(alias.c.key == key) #add the substring constraint if no other functions are there translate_data_value(alias, op) for op in constraints: s.append_whereclause(sa_op(op)) if not have_data_rule: raise Exception('Data queries must have at least one data rule.') #TODO in order to sort by data columns, this is going to need to be smarter if sort: need_join = True s, cols = add_sort(sort, {'_':t_table}, s) if need_join: s.append_whereclause(first_alias.c.thing_id == t_table.c.thing_id) if limit: s = s.limit(limit) try: r = add_request_info(s).execute() except Exception, e: dbm.mark_dead(t_table.bind) # this thread must die so that others may live raise
def find_rels(rel_type_id, get_cols, sort, limit, constraints): r_table, t1_table, t2_table, d_table = rel_types_id[rel_type_id].rel_table constraints = deepcopy(constraints) aliases = alias_generator() t1_table, t2_table = t1_table.alias(aliases.next()), t2_table.alias(aliases.next()) s = sa.select([r_table.c.rel_id.label('rel_id')]) need_join1 = ('thing1_id', t1_table) need_join2 = ('thing2_id', t2_table) joins_needed = set() for op in operators.op_iter(constraints): #vals = con.rval key = op.lval_name prefix = key[:4] if prefix in ('_t1_', '_t2_'): #not a thing attribute key = key[4:] if prefix == '_t1_': join = need_join1 joins_needed.add(join) elif prefix == '_t2_': join = need_join2 joins_needed.add(join) table = join[1] op.lval = translate_sort(table, key, op.lval) op.rval = translate_thing_value(op.rval) #ors = [sa_op(con, key, v) for v in vals] #s.append_whereclause(sa.or_(*ors)) elif prefix.startswith('_'): op.lval = r_table.c[key[1:]] else: alias = d_table.alias(aliases.next()) s.append_whereclause(r_table.c.rel_id == alias.c.thing_id) s.append_column(alias.c.value.label(key)) s.append_whereclause(alias.c.key == key) translate_data_value(alias, op) for op in constraints: s.append_whereclause(sa_op(op)) if sort: cols = add_sort(sort, {'_':r_table, '_t1_':t1_table, '_t2_':t2_table}, s) #do we need more joins? for (col, table) in cols: if table == need_join1[1]: joins_needed.add(need_join1) elif table == need_join2[1]: joins_needed.add(need_join2) for j in joins_needed: col, table = j s.append_whereclause(r_table.c[col] == table.c.thing_id) if limit: s.limit = limit r = s.execute() return Results(r, lambda (row): (row if get_cols else row.rel_id))
def find_rels(rel_type_id, get_cols, sort, limit, constraints): tables = get_rel_table(rel_type_id) r_table, t1_table, t2_table, d_table = tables constraints = deepcopy(constraints) t1_table, t2_table = t1_table.alias(), t2_table.alias() s = sa.select([r_table.c.rel_id.label('rel_id')]) need_join1 = ('thing1_id', t1_table) need_join2 = ('thing2_id', t2_table) joins_needed = set() for op in operators.op_iter(constraints): #vals = con.rval key = op.lval_name prefix = key[:4] if prefix in ('_t1_', '_t2_'): #not a thing attribute key = key[4:] if prefix == '_t1_': join = need_join1 joins_needed.add(join) elif prefix == '_t2_': join = need_join2 joins_needed.add(join) table = join[1] op.lval = translate_sort(table, key, op.lval) op.rval = translate_thing_value(op.rval) #ors = [sa_op(con, key, v) for v in vals] #s.append_whereclause(sa.or_(*ors)) elif prefix.startswith('_'): op.lval = r_table.c[key[1:]] else: alias = d_table.alias() s.append_whereclause(r_table.c.rel_id == alias.c.thing_id) s.append_column(alias.c.value.label(key)) s.append_whereclause(alias.c.key == key) translate_data_value(alias, op) for op in constraints: s.append_whereclause(sa_op(op)) if sort: s, cols = add_sort(sort, { '_': r_table, '_t1_': t1_table, '_t2_': t2_table }, s) #do we need more joins? for (col, table) in cols: if table == need_join1[1]: joins_needed.add(need_join1) elif table == need_join2[1]: joins_needed.add(need_join2) for j in joins_needed: col, table = j s.append_whereclause(r_table.c[col] == table.c.thing_id) if limit: s = s.limit(limit) try: r = add_request_info(s).execute() except Exception, e: dbm.mark_dead(r_table.bind) # this thread must die so that others may live raise
def find_data(type_id, get_cols, sort, limit, constraints): t_table, d_table = get_thing_table(type_id) constraints = deepcopy(constraints) used_first = False s = None need_join = False have_data_rule = False first_alias = d_table.alias() s = sa.select([first_alias.c.thing_id.label('thing_id') ]) #, distinct=True) for op in operators.op_iter(constraints): key = op.lval_name vals = tup(op.rval) if key == '_id': op.lval = first_alias.c.thing_id elif key.startswith('_'): need_join = True op.lval = translate_sort(t_table, key[1:], op.lval) op.rval = translate_thing_value(op.rval) else: have_data_rule = True id_col = None if not used_first: alias = first_alias used_first = True else: alias = d_table.alias() id_col = first_alias.c.thing_id if id_col is not None: s.append_whereclause(id_col == alias.c.thing_id) s.append_column(alias.c.value.label(key)) s.append_whereclause(alias.c.key == key) #add the substring constraint if no other functions are there translate_data_value(alias, op) for op in constraints: s.append_whereclause(sa_op(op)) if not have_data_rule: raise Exception('Data queries must have at least one data rule.') #TODO in order to sort by data columns, this is going to need to be smarter if sort: need_join = True s, cols = add_sort(sort, {'_': t_table}, s) if need_join: s.append_whereclause(first_alias.c.thing_id == t_table.c.thing_id) if limit: s = s.limit(limit) try: r = add_request_info(s).execute() except Exception, e: dbm.mark_dead(t_table.bind) # this thread must die so that others may live raise
def find_rels(ret_props, rel_type_id, sort, limit, offset, constraints): tables = get_rel_table(rel_type_id) r_table, t1_table, t2_table, d_table = tables constraints = deepcopy(constraints) t1_table, t2_table = t1_table.alias(), t2_table.alias() prop_to_column = { "_rel_id": r_table.c.rel_id.label('rel_id'), "_thing1_id": r_table.c.thing1_id.label('thing1_id'), "_thing2_id": r_table.c.thing2_id.label('thing2_id'), "_name": r_table.c.name.label('name'), "_date": r_table.c.date.label('date'), } if not ret_props: valid_props = ', '.join(prop_to_column.keys()) raise ValueError("ret_props must contain at least one of " + valid_props) columns = [] for prop in ret_props: if prop not in prop_to_column: raise ValueError("ret_props got unrecognized %s" % prop) columns.append(prop_to_column[prop]) s = sa.select(columns) need_join1 = ('thing1_id', t1_table) need_join2 = ('thing2_id', t2_table) joins_needed = set() for op in operators.op_iter(constraints): #vals = con.rval key = op.lval_name prefix = key[:4] if prefix in ('_t1_', '_t2_'): #not a thing attribute key = key[4:] if prefix == '_t1_': join = need_join1 joins_needed.add(join) elif prefix == '_t2_': join = need_join2 joins_needed.add(join) table = join[1] op.lval = translate_sort(table, key, op.lval) op.rval = translate_thing_value(op.rval) #ors = [sa_op(con, key, v) for v in vals] #s.append_whereclause(sa.or_(*ors)) elif prefix.startswith('_'): op.lval = r_table.c[key[1:]] else: alias = d_table.alias() s.append_whereclause(r_table.c.rel_id == alias.c.thing_id) s.append_column(alias.c.value.label(key)) s.append_whereclause(alias.c.key == key) translate_data_value(alias, op) for op in constraints: s.append_whereclause(sa_op(op)) if sort: s, cols = add_sort( sort=sort, t_table={ '_': r_table, '_t1_': t1_table, '_t2_': t2_table }, select=s, ) #do we need more joins? for (col, table) in cols: if table == need_join1[1]: joins_needed.add(need_join1) elif table == need_join2[1]: joins_needed.add(need_join2) for j in joins_needed: col, table = j s.append_whereclause(r_table.c[col] == table.c.thing_id) if limit: s = s.limit(limit) if offset: s = s.offset(offset) try: r = add_request_info(s).execute() except Exception, e: dbm.mark_dead(r_table.bind) # this thread must die so that others may live raise
def find_data(type_id, get_cols, sort, limit, constraints): d_table, t_table = types_id[type_id].data_table constraints = deepcopy(constraints) aliases = alias_generator() used_first = False s = None need_join = False have_data_rule = False first_alias = d_table.alias(aliases.next()) s = sa.select([first_alias.c.thing_id.label('thing_id')])#, distinct=True) for op in operators.op_iter(constraints): key = op.lval_name vals = tup(op.rval) if key == '_id': op.lval = first_alias.c.thing_id elif key.startswith('_'): need_join = True op.lval = translate_sort(t_table, key[1:], op.lval) op.rval = translate_thing_value(op.rval) else: have_data_rule = True id_col = None if not used_first: alias = first_alias used_first = True else: alias = d_table.alias(aliases.next()) id_col = first_alias.c.thing_id if id_col: s.append_whereclause(id_col == alias.c.thing_id) s.append_column(alias.c.value.label(key)) s.append_whereclause(alias.c.key == key) #add the substring constraint if no other functions are there translate_data_value(alias, op) for op in constraints: s.append_whereclause(sa_op(op)) if not have_data_rule: raise Exception('Data queries must have at least one data rule.') #TODO in order to sort by data columns, this is going to need to be smarter if sort: need_join = True add_sort(sort, {'_':t_table}, s) if need_join: s.append_whereclause(first_alias.c.thing_id == t_table.c.thing_id) if limit: s.limit = limit r = s.execute() return Results(r, lambda(row): row if get_cols else row.thing_id)
def find_rels(ret_props, rel_type_id, sort, limit, offset, constraints): tables = get_rel_table(rel_type_id) r_table, t1_table, t2_table, d_table = tables constraints = deepcopy(constraints) t1_table, t2_table = t1_table.alias(), t2_table.alias() prop_to_column = { "_rel_id": r_table.c.rel_id.label('rel_id'), "_thing1_id": r_table.c.thing1_id.label('thing1_id'), "_thing2_id": r_table.c.thing2_id.label('thing2_id'), "_name": r_table.c.name.label('name'), "_date": r_table.c.date.label('date'), } if not ret_props: valid_props = ', '.join(prop_to_column.keys()) raise ValueError("ret_props must contain at least one of " + valid_props) columns = [] for prop in ret_props: if prop not in prop_to_column: raise ValueError("ret_props got unrecognized %s" % prop) columns.append(prop_to_column[prop]) s = sa.select(columns) need_join1 = ('thing1_id', t1_table) need_join2 = ('thing2_id', t2_table) joins_needed = set() for op in operators.op_iter(constraints): #vals = con.rval key = op.lval_name prefix = key[:4] if prefix in ('_t1_', '_t2_'): #not a thing attribute key = key[4:] if prefix == '_t1_': join = need_join1 joins_needed.add(join) elif prefix == '_t2_': join = need_join2 joins_needed.add(join) table = join[1] op.lval = translate_sort(table, key, op.lval) op.rval = translate_thing_value(op.rval) #ors = [sa_op(con, key, v) for v in vals] #s.append_whereclause(sa.or_(*ors)) elif prefix.startswith('_'): op.lval = r_table.c[key[1:]] else: alias = d_table.alias() s.append_whereclause(r_table.c.rel_id == alias.c.thing_id) s.append_column(alias.c.value.label(key)) s.append_whereclause(alias.c.key == key) translate_data_value(alias, op) for op in constraints: s.append_whereclause(sa_op(op)) if sort: s, cols = add_sort( sort=sort, t_table={'_': r_table, '_t1_': t1_table, '_t2_': t2_table}, select=s, ) #do we need more joins? for (col, table) in cols: if table == need_join1[1]: joins_needed.add(need_join1) elif table == need_join2[1]: joins_needed.add(need_join2) for j in joins_needed: col, table = j s.append_whereclause(r_table.c[col] == table.c.thing_id) if limit: s = s.limit(limit) if offset: s = s.offset(offset) try: r = add_request_info(s).execute() except Exception, e: dbm.mark_dead(r_table.bind) # this thread must die so that others may live raise