[sqlalchemy] Re: Dynamically adding MapperExtension

2007-03-13 Thread percious

I think I came up with a decent solution for this:  I do something
like:

extension = MyMapperExtension()
for mapper in mapper_registry.values():
mapper.extension = extension
mapper._compile_extensions()

If you want all classes currently defined in the metadata to map to
your new extension.  This is actually working quite well.

cheers.
chris

On Feb 9, 11:33 am, Michael Bayer [EMAIL PROTECTED] wrote:
 I should add that you *can* be more creative, and construct each mapper
 with aMapperExtensionat the beginning which later can be enabled with
 a hello world callable.  i.e.

 class MyExt(MapperExtension):
 def __init__(self):
 self.func = None
 def after_insert(self, ...):
 if self.func:
  self.func()

 extension = MyExt()
 mapper(foo, bar, extension=extension)
 mapper(foo2, bar2, extension=extension)
 mapper(foo3, bar3, extension=extension)

 ... do stuff ...

 def helloworld():
 print hello world
 extension.func = helloworld

 ... do stuff ...

 percious wrote:

  question.

  Lets say I have a series of table definitions, and a series of objects
  linked to the tables with a bunch of mappers.

  First question:  Is there a way to get from the table definitions in
  the metadata to the Mapper?

  Second question:  If I create aMapperExtension, can I then link it to
  the mapper associated with a table?

  What I want to do is create a simple application that goes through the
  tables defined within a metadata, and create an extension so that
  every time a table entry is added it prints 'hello world' to the
  screen.

  TIA
  -chris


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Dynamically adding MapperExtension

2007-02-09 Thread Michael Bayer

percious wrote:

 question.

 Lets say I have a series of table definitions, and a series of objects
 linked to the tables with a bunch of mappers.

 First question:  Is there a way to get from the table definitions in
 the metadata to the Mapper?

not conveniently.  you could loop through the dictionary of mappers in
sqlalchemy.orm.mapperlib.mapper_registry and just look at all the
mapped_table attributes of those mappers.  or just track yourself.


 Second question:  If I create a MapperExtension, can I then link it to
 the mapper associated with a table?

MapperExtensions currently like to be set at the time of mapper
construction.  there is also a way to say
query.options(extension(someext)), but theres a pending ticket which is
needed for some fixes to that...also that only sets an extension that
takes place within the realm of a select.



 What I want to do is create a simple application that goes through the
 tables defined within a metadata, and create an extension so that
 every time a table entry is added it prints 'hello world' to the
 screen.

yah that pattern is not really supported.  tables/mappers/etc like to be
constructed and then not changed after that, so if you cant apply your
hello world function at table/mapper construction time, the closest
thing you could do is either use a customized logging stream to trap
INSERT statements, or if you used DynamicMetaData you could have it
connect() to a ProxyEngine instance which does similar.




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Dynamically adding MapperExtension

2007-02-09 Thread svilen


 question.

 Lets say I have a series of table definitions, and a series of
 objects linked to the tables with a bunch of mappers.

 First question:  Is there a way to get from the table definitions
 in the metadata to the Mapper?
For simple table==class, the link is 1:1. 
For multi-table-inheritances/ single-inheritances, it's many:1.

And the linking is mostly the other way (mapper-to-tables, not 
tables-to-mappers), so u may have to dig:

u can use either m=class_mapper( yourclas) or m=object_mapper( obj);
then check isinstance( m.localtable, Table), and use it...

all mappers are in mapper_registry...

but this is all pure gymnastics...

 Second question:  If I create a MapperExtension, can I then link it
 to the mapper associated with a table?
themapper.extension = yours... ; or see global_extensions.
dontknow about their compilation though...

 What I want to do is create a simple application that goes through
 the tables defined within a metadata, and create an extension so
 that every time a table entry is added it prints 'hello world' to
 the screen.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Dynamically adding MapperExtension

2007-02-09 Thread Michael Bayer


I should add that you *can* be more creative, and construct each mapper
with a MapperExtension at the beginning which later can be enabled with
a hello world callable.  i.e.


class MyExt(MapperExtension):
def __init__(self):
self.func = None
def after_insert(self, ...):
if self.func:
 self.func()

extension = MyExt()
mapper(foo, bar, extension=extension)
mapper(foo2, bar2, extension=extension)
mapper(foo3, bar3, extension=extension)

... do stuff ...

def helloworld():
print hello world
extension.func = helloworld

... do stuff ...





percious wrote:

 question.

 Lets say I have a series of table definitions, and a series of objects
 linked to the tables with a bunch of mappers.

 First question:  Is there a way to get from the table definitions in
 the metadata to the Mapper?

 Second question:  If I create a MapperExtension, can I then link it to
 the mapper associated with a table?

 What I want to do is create a simple application that goes through the
 tables defined within a metadata, and create an extension so that
 every time a table entry is added it prints 'hello world' to the
 screen.

 TIA
 -chris


 



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---