diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-09-24 11:11:12 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-09-24 11:11:12 -0400 |
commit | 25a04d87c38356641a19110900d8ca90e4023fe9 (patch) | |
tree | d78d22fe61dca970006adcaab8cbfa84d6e1ef34 /examples/elementtree/optimized_al.py | |
parent | 21d2a6d790983054e4d008af991b7dda309a94c4 (diff) | |
download | sqlalchemy-25a04d87c38356641a19110900d8ca90e4023fe9.tar.gz |
- optimized_al doesn't need mapper extension
- convert to non-bound metadata, Session, session.commit()
Diffstat (limited to 'examples/elementtree/optimized_al.py')
-rw-r--r-- | examples/elementtree/optimized_al.py | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/examples/elementtree/optimized_al.py b/examples/elementtree/optimized_al.py index 98c4e1129..d6110a132 100644 --- a/examples/elementtree/optimized_al.py +++ b/examples/elementtree/optimized_al.py @@ -5,19 +5,19 @@ which joins on only three tables. """ -################################# PART I - Imports/Configuration ########################################### +##################### PART I - Imports/Configuration ######################### from sqlalchemy import (MetaData, Table, Column, Integer, String, ForeignKey, - Unicode, and_) -from sqlalchemy.orm import mapper, relationship, create_session, lazyload + Unicode, and_, create_engine) +from sqlalchemy.orm import mapper, relationship, Session, lazyload import sys, os, StringIO, re from xml.etree import ElementTree +e = create_engine('sqlite://', echo=True) meta = MetaData() -meta.bind = 'sqlite://' -################################# PART II - Table Metadata ########################################### +####################### PART II - Table Metadata ############################# # stores a top level record of an XML document. documents = Table('documents', meta, @@ -43,9 +43,9 @@ attributes = Table('attributes', meta, Column('name', Unicode(100), nullable=False, primary_key=True), Column('value', Unicode(255))) -meta.create_all() +meta.create_all(e) -#################################### PART III - Model ############################################# +########################### PART III - Model ################################# # our document class. contains a string name, # and the ElementTree root element. @@ -59,7 +59,7 @@ class Document(object): self.element.write(buf) return buf.getvalue() -#################################### PART IV - Persistence Mapping ################################### +########################## PART IV - Persistence Mapping ##################### # Node class. a non-public class which will represent # the DB-persisted Element/SubElement object. We cannot create mappers for @@ -145,12 +145,12 @@ class ElementTreeMarshal(object): # override Document's "element" attribute with the marshaller. Document.element = ElementTreeMarshal() -########################################### PART V - Basic Persistence Example ############################ +###################### PART V - Basic Persistence Example #################### line = "\n--------------------------------------------------------" # save to DB -session = create_session() +session = Session(e) # get ElementTree documents for file in ('test.xml', 'test2.xml', 'test3.xml'): @@ -159,25 +159,25 @@ for file in ('test.xml', 'test2.xml', 'test3.xml'): session.add(Document(file, doc)) print "\nSaving three documents...", line -session.flush() +session.commit() print "Done." -# clear session (to illustrate a full load), restore -session.expunge_all() - print "\nFull text of document 'text.xml':", line document = session.query(Document).filter_by(filename="test.xml").first() print document -############################################ PART VI - Searching for Paths ####################################### +######################## PART VI - Searching for Paths ####################### # manually search for a document which contains "/somefile/header/field1:hi" print "\nManual search for /somefile/header/field1=='hi':", line -d = session.query(Document).join('_nodes', aliased=True).filter(and_(_Node.parent_id==None, _Node.tag==u'somefile')).\ - join('children', aliased=True, from_joinpoint=True).filter(_Node.tag==u'header').\ - join('children', aliased=True, from_joinpoint=True).filter(and_(_Node.tag==u'field1', _Node.text==u'hi')).\ - one() +d = session.query(Document).join('_nodes', aliased=True).\ + filter(and_(_Node.parent_id==None, _Node.tag==u'somefile')).\ + join('children', aliased=True, from_joinpoint=True).\ + filter(_Node.tag==u'header').\ + join('children', aliased=True, from_joinpoint=True).\ + filter(and_(_Node.tag==u'field1', _Node.text==u'hi')).\ + one() print d # generalize the above approach into an extremely impoverished xpath function: |