def InitTables(self): """ Creates and initializes the database tables.\n It uses the sqlalchemy mapper to map tables to objects """ banlist_table = sa.Table( 'banlist', self.metadata, sa.Column('name', sa.String(50), primary_key=True), sa.Column('url', sa.String, nullable=False)) sa.mapper(Banlist, banlist_table) for x in self.Query(Banlist).select(): ban_table = sa.Table( x.name, self.metadata, sa.Column('id', sa.Integer, autoincrement=True, primary_key=True), sa.Column('name', sa.String(50)), sa.Column('reason', sa.String, nullable=False)) ban_table.create(checkfirst=True) classname = x.name.capitalize() #Dynamic class creation. globals()[classname] = type(str(classname), (BanBase, ), {}) #mapper() takes a class as its first argument, so we must eval #the string, so it returns a class. sa.mapper(eval(classname), ban_table)
def _setup_mappers(self, tables, mappers): """Map the database Tables to SQLAlchemy Mapper objects """ mappers['screening'] = mapper(Screening, tables['screening']) mappers['reservation'] = mapper(Reservation, tables['reservation'], properties = { 'screening' : relation(Screening), })
def __init__(self): self._db = create_engine(os.getenv('DB_DRIVER')) self._metadata = BoundMetaData(self.__db) self.__user = mapper( User, Table(os.getenv('USER_TABLE'), self._metadata, autoload=True)) self.__event = mapper( Event, Table(os.getenv('EVENT_TABLE'), self._metadata, autoload=True)) self.__payment = mapper( Payment, Table(os.getenv('PAYMENT_TABLE'), self._metadata, autoload=True)) self._session = create_session()
def assign_mapper(ctx, class_, *args, **kwargs): kwargs.setdefault("is_primary", True) if not isinstance(getattr(class_, '__init__'), types.MethodType): def __init__(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) class_.__init__ = __init__ extension = kwargs.pop('extension', None) if extension is not None: extension = util.to_list(extension) extension.append(ctx.mapper_extension) else: extension = ctx.mapper_extension m = mapper(class_, extension=extension, *args, **kwargs) class_.mapper = m for name in [ 'get', 'select', 'select_by', 'selectone', 'get_by', 'join_to', 'join_via', 'count', 'count_by' ]: monkeypatch_query_method(ctx, class_, name) for name in [ 'flush', 'delete', 'expire', 'refresh', 'expunge', 'merge', 'save', 'update', 'save_or_update' ]: monkeypatch_objectstore_method(ctx, class_, name)
def defineMapping(self, annotated_table, **kw): table_mapper = mapper(annotated_table.domain_class, annotated_table.table, **kw) self.mappers[named(annotated_table.domain_class)] = table_mapper annotated_table.domain_class.mapper = table_mapper self.table_classes[ annotated_table.table.name] = annotated_table.domain_class
def assign_mapper(ctx, class_, *args, **kwargs): validate = kwargs.pop('validate', False) if not isinstance(getattr(class_, '__init__'), types.MethodType): def __init__(self, **kwargs): for key, value in kwargs.items(): if validate: if not key in self.mapper.props: raise exceptions.ArgumentError( "Invalid __init__ argument: '%s'" % key) setattr(self, key, value) class_.__init__ = __init__ extension = kwargs.pop('extension', None) if extension is not None: extension = util.to_list(extension) extension.append(ctx.mapper_extension) else: extension = ctx.mapper_extension m = mapper(class_, extension=extension, *args, **kwargs) class_.mapper = m class_.query = classmethod(lambda cls: Query(class_, session=ctx.current)) for name in [ 'get', 'select', 'select_by', 'selectfirst', 'selectfirst_by', 'selectone', 'get_by', 'join_to', 'join_via', 'count', 'count_by', 'options', 'instances' ]: monkeypatch_query_method(ctx, class_, name) for name in [ 'flush', 'delete', 'expire', 'refresh', 'expunge', 'merge', 'save', 'update', 'save_or_update' ]: monkeypatch_objectstore_method(ctx, class_, name) return m
def peerFactory( klass, metadata ): klass_name = "%s_serializer"%klass.__name__.lower() type_klass = type( klass_name, (object,), {} ) type_table = transmute( klass.Schema(), metadata, klass_name ) mapper = rdb.mapper( type_klass, type_table ) return type_klass
def InitTables(self): """ Creates and initializes the database tables.\n It uses the sqlalchemy mapper to map tables to objects """ banlist_table = sa.Table('banlist', self.metadata, sa.Column('name', sa.String(50), primary_key=True), sa.Column('url', sa.String, nullable=False)); sa.mapper(Banlist, banlist_table) for x in self.Query(Banlist).select(): ban_table = sa.Table(x.name, self.metadata, sa.Column('id', sa.Integer, autoincrement=True, primary_key=True), sa.Column('name', sa.String(50)), sa.Column('reason', sa.String, nullable=False)); ban_table.create(checkfirst=True) classname = x.name.capitalize() #Dynamic class creation. globals()[classname] = type(str(classname), (BanBase,), {}) #mapper() takes a class as its first argument, so we must eval #the string, so it returns a class. sa.mapper(eval(classname), ban_table)
def mapping(self, engine): metadata = MetaData(bind=engine) metatable1 = Table( self.v_mails, metadata, Column( "user_id", Integer, primary_key=True), autoload=True) metatable2 = Table( self.v_alias, metadata, Column( "src_email", String(256), primary_key=True), autoload=True) mapper(VirMails, metatable1) mapper(VirAlias, metatable2)
def assign_mapper(ctx, class_, *args, **kwargs): validate = kwargs.pop('validate', False) if not isinstance(getattr(class_, '__init__'), types.MethodType): def __init__(self, **kwargs): for key, value in kwargs.items(): if validate: if not key in self.mapper.props: raise exceptions.ArgumentError("Invalid __init__ argument: '%s'" % key) setattr(self, key, value) class_.__init__ = __init__ extension = kwargs.pop('extension', None) if extension is not None: extension = util.to_list(extension) extension.append(ctx.mapper_extension) else: extension = ctx.mapper_extension m = mapper(class_, extension=extension, *args, **kwargs) class_.mapper = m class_.query = classmethod(lambda cls: Query(class_, session=ctx.current)) for name in ['get', 'select', 'select_by', 'selectfirst', 'selectfirst_by', 'selectone', 'get_by', 'join_to', 'join_via', 'count', 'count_by', 'options', 'instances']: monkeypatch_query_method(ctx, class_, name) for name in ['flush', 'delete', 'expire', 'refresh', 'expunge', 'merge', 'save', 'update', 'save_or_update']: monkeypatch_objectstore_method(ctx, class_, name) return m
def bind_mapper( klass, *args, **kw): klass_mapper = mapper( klass, *args, **kw ) bindClass( klass, klass_mapper ) DomainUtility.add( klass ) return klass_mapper
def bind_mapper(klass, *args, **kw): klass_mapper = mapper(klass, *args, **kw) bindClass(klass, klass_mapper) DomainUtility.add(klass) return klass_mapper
def defineMapping( self, annotated_table, **kw ): table_mapper = mapper( annotated_table.domain_class, annotated_table.table, **kw) self.mappers[ named( annotated_table.domain_class ) ] = table_mapper annotated_table.domain_class.mapper = table_mapper self.table_classes[ annotated_table.table.name ] = annotated_table.domain_class
def loadMapping( self, domain_class, domain_table, **kw ): self.klasses[ named( domain_class ) ] = domain_class self.tables[ named( domain_table ) ] = domain_table mapper = mapper( domain_class, domain_table, **kw) self.mappers[ named( domain_class ) ] = mapper return mapper
class Player(DbObject): def get_name(self): return self.entity.name def set_name(self, name): self.entity.name = name name = property(get_name, set_name) def get_short(self): return self.entity.short def set_short(self, short): self.entity.short = short short = property(get_short, set_short) mapper(Entity, _entity) mapper(Player, _player, properties = {'entity': relation(Entity, lazy=False, cascade="all, delete-orphan")}) def get_players(all=False): if all: return session.query(Player).select() else: return session.query(Player).select_by(current=True) def get_player(name): name = name.lower()
def mapper(*args, **params): return sqlalchemy.mapper(*args, **params)
def loadMapping(self, domain_class, domain_table, **kw): self.klasses[named(domain_class)] = domain_class self.tables[named(domain_table)] = domain_table mapper = mapper(domain_class, domain_table, **kw) self.mappers[named(domain_class)] = mapper return mapper