def create_type(name, base=None): name = unicode(name) tname = dals.composeTableName(name) if tname in dals.table_space: raise dal.TypeAlreadyExists(name) base_id = get_type_id(base) if base else -1 id = 0 try: result = dals.obj_type.insert().execute(name=name, base=base_id) id = result.lastrowid except Exception as e: print e if id != 0: table = Table(tname, dals.metadata, Column('id', Integer, primary_key=True), Column('lockw', Integer, default=0), Column('lockr', Integer, default=0)) table.create(dals.metadata.bind, checkfirst=True) dals.table_space[tname] = table dal.mc.set_type_data(name, id) dal.mc.set_type_data(id, name) cls = gen_class(name) setattr(cls, 'table', table)
def __init__(self, type_name, id=-1): self.fields=[] if id >= 0: self.type_name = type_name self.id = id return self.type_id = dt.get_type_id(type_name) self.attributes = dt.getTypeFields(self.type_id) for i in self.attributes: if not self.attributes[i]: setattr(self, i, None) else: setattr(self, i, []) self.table = dals.table_space[dals.composeTableName(type_name)] self.__class__.table = dals.table_space[dals.composeTableName(type_name)] try: o = self.table.insert().execute() except Exception as e: print e self.id = o.lastrowid
def __init__(self, belongs_to, type_field, name): self.name = name self.belongs_to = belongs_to tname = dals.composeTableName(belongs_to, name) if not tname in dals.table_space: raise dal.FieldNotDefined(belongs_to, name) self.table = dals.table_space[tname] self.belongs_to_id = dt.get_type_id(belongs_to) self.type_id = dt.get_type_id(type_field) fdata = dal.mc.get_field_data(self.belongs_to_id, name) self.size = fdata['size'] self.is_list = fdata['is_list'] self.vname = '_v_' + name if fdata['is_list'] == 1: raise dal.IsList(self.name)
def create_field(name, type_field, size, is_list, belongs_to, indexed): name = unicode(name) belongs_to = unicode(belongs_to) belongs_to_id = get_type_id(belongs_to) fd = dal.mc.get_field_data(belongs_to_id, name) if fd: return type_id = get_type_id(type_field) try: r = dals.field_def.insert().execute(name=name, type_id=type_id, belongs_to_id=belongs_to_id, size=size, is_list=is_list, indexed=indexed) id = r.lastrowid except Exception as e: print e return 0 if id != 0: if type_id < 100: table = Table(dals.composeTableName(belongs_to, name), dals.metadata, Column('id', Integer, primary_key=True), Column('value', getColumnTypeId(type_id, size)), Column('obj_id', Integer, ForeignKey(dals.table_space[dals.composeTableName(belongs_to)].c.id), unique=not is_list)) else: table = Table(dals.composeTableName(belongs_to, name), dals.metadata, Column('id', Integer, primary_key=True), Column('value', Integer, ForeignKey(dals.table_space[dals.composeTableName(type_field)].c.id)), Column('obj_id', Integer, ForeignKey(dals.table_space[dals.composeTableName(belongs_to)].c.id), unique=not is_list)) table.create(dals.metadata.bind) dals.table_space[dals.composeTableName(belongs_to, name)] = table dal.mc.set_field_data({'belongs_to_id':belongs_to_id, 'name':name, 'type_id':type_id, 'size':size, 'is_list': is_list, 'indexed':indexed}) if is_list: gen_field_list(belongs_to, type_field, name) else: gen_field(belongs_to, type_field, name) return id
def fetch(self): """ El fetch obtindra primerament la llista dels ids que siguin necessaris limitara si fos el cas i fara diferents selects per els camps que li demanem. Fara una unio de les diferents files empilant tots els objectes amb els camps que pertoquin i retornara la llista d'objectes""" objs = [] extrawork = False version = 1 fieldName = str(dt.getTypeName(self.type)) cls = getattr(dal.object, fieldName) if len(self.fields) == 0: self.fields = dt.getTypeFields(self.type) if self.id != 0: """ La crida ve desde una instancia: recuperem nomes els objectes que pertanyen a aquesta instancia""" if self.order == None: """Si no es vol cap ordre especial sobtenen els registres utilitzan una crida en una de les taules """ if self.limit == 0 and self.offset != 0: query = select([self.table.c.value]).where(self.table.c.obj_id == self.id).offset(self.offset) elif self.limit != 0 and self.offset == 0: query = select([self.table.c.value]).where(self.table.c.obj_id == self.id).limit(self.limit) elif self.limit == 0 and self.offset != 0: query = select([self.table.c.value]).where(self.table.c.obj_id == self.id).offset(self.offset) else: query = select([self.table.c.value]).where(self.table.c.obj_id == self.id) else: extrawork = True query = select([self.table.c.value]).where(self.table.c.obj_id == self.id) else: """La crida ve des dun objecte: """ if self.order != None: table = dals.composeTableName(self.type, self.order) if self.limit == 0 and self.offset != 0: oQuery = select([table.c.value, table.c.obj_id]).order_by(table.c.value).limit(self.limit) elif self.limit != 0 and self.offset == 0: oQuery = ( select([table.c.value, table.c.obj_id]) .order_by(table.c.value) .offset(self.offset) .limit(self.limit) ) elif self.limit == 0 and self.offset != 0: oQuery = select([table.c.value, table.c.obj_id]).order_by(table.c.value).offset(self.offset) else: oQuery = select([table.c.value, table.c.obj_id]).order_by(table.c.value) "aqui creamos una tabla con el orden de los ids" else: if self.limit == 0 and self.offset != 0: query = select([self.table.c.id]).order_by(self.table.c.id).limit(self.limit) elif self.limit != 0 and self.offset == 0: query = select([self.table.c.id]).order_by(self.table.c.id).offset(self.offset).limit(self.limit) elif self.limit == 0 and self.offset != 0: query = select([self.table.c.id]).order_by(self.table.c.id).offset(self.offset) else: query = select([self.table.c.id]).order_by(self.table.c.id) objs = {} ids = dals.executeQuery(query).fetchall() for i in ids: objs[i["id"]] = cls() for nom in self.fields: if not self.fields[nom]: table = dals.composeTableName(self.type, nom) result = dals.executeQuery( select([dals.table_space[table].c.value, dals.table_space[table].c.obj_id]).order_by( dals.table_space[table].c.obj_id ) ).fetchall() for row in result: if row["obj_id"] in objs: setattr(objs[row["obj_id"]], nom, row["value"]) else: break return objs result = dals.executeQuery(query).fetchall() if version == 1: for row in result: id_obj = row["value"] absObj = cls(id_obj) for nom in self.fields: if self.fields[nom] == False: table = dals.composeTableName(self.type, nom) values = dals.executeQuery( select([dals.table_space[table].c.value]).where(dals.table_space[table].c.obj_id == id_obj) ).fetchone() if values != None: setattr(absObj, nom, values["value"]) objs.append(absObj) if extrawork: c = Compare(self.order) objs.sort(c) if version == 2: for nom in self.fields: if nom != self.order & self.fields[nom] == False: values = dals.executeQuery(select([dals.table_space[table].c.value])).fetchall() for value in values: pass return objs
def all(self): q = Query() q.set_table(dals.table_space[dals.composeTableName(self.field.belongs_to,self.field.name)]) q.set_id(self.obj.id); q.set_type(dt.getTypeName(self.field.type_id)) return q
def create_field_index(belongs_to, field_name): i = Index(gen_index_name(belongs_to, field_name), dals.table_space[dals.composeTableName(belongs_to, field_name)].c.value) i.create(dals.metadata.bind) r = dals.field_def.update().where(and_(dals.field_def.c.name==field_name, dals.field_def.c.belongs_to_id==belongs_to_id)).values(indexed=True).execute()