On Fri, Jan 29, 2010 at 02:53:26PM +0300, Oleg Broytman wrote:
> class File(SQLObject):
>     name = StringCol()
>     root = ForeignKey("Directory", default=None)
> 
> class Directory(File):
>     dirs = MultipleJoin("Directory", joinColumn="root_id")
>     files = MultipleJoin("File", joinColumn="root_id")

   On the other hand, this means two separate calls to .select() to draw
Files and Directories while with InheritableSQLObject it could be one.
   And this means the original design was wrong, perhaps. I think you don't
need two different MultipleJoins - you need one (all directory entries) and
two filters. Let me show this with the code:

class DirEntry(InheritableSQLObject):
    name = StringCol()
    parent = ForeignKey("DirEntry")

class File(DirEntry):
    pass

class Directory(DirEntry):
    entries = MultipleJoin("DirEntry", joinColumn="parent_id")

    def _filter_entries(self, klass):
        return [entry for entry in self.entries if isinstance(entry, klass)]

    def _get_files(self):
        return self._filter_entries(File)

    def _get_directories(self):
        return self._filter_entries(Directory)

DirEntry.createTable()
File.createTable()
Directory.createTable()

root = Directory(name='/', parent=None)
etc = Directory(name='/etc', parent=root)
X11 = Directory(name='/etc/X11', parent=etc)
passwd = File(name='/etc/passwd', parent=etc)

print etc.directories
print etc.files

Oleg.
-- 
     Oleg Broytman            http://phd.pp.ru/            p...@phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.

------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to