[sqlalchemy] Re: how to make a query for multilevel reference?

2007-01-26 Thread svilen

okay, how would u do something like
(person.address1.country.name == 'France' or
   person.address2.country.name == 'Germany')

note there are 2 different address1 and address2 fields of the person.


and, how would this be like:
(person.address1.country.name == 'France' or
   person.company.name == 'Acme')


--~--~-~--~~~---~--~~
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: how to make a query for multilevel reference?

2007-01-23 Thread Michael Bayer


svilen wrote:
 But then query.select_by( name='whatever') may find different .name 's
 in the obj.hierarchy depending on dict.hashes/iteration, within same
 run or between different runs, returning very different queries...

then just use select() for more specificity.


 Which means - cripple the _locate_prop() to look only 1 level down,
 making it predictable.

it will match the first level it finds.  its predictable.

 or.. let the wishful ones to hang themselves using literal strings
 into **kwargs ... (-:) Matching for key-subpaths within a key-tree
 (that is, object-attr-hierarchy) is a very powerful thing..

no need for wishing... just make yourself a MapperExtension that plugs
whatever functionality you want into the select_by() method.


--~--~-~--~~~---~--~~
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: how to make a query for multilevel reference?

2007-01-23 Thread svilen

  Which means - cripple the _locate_prop() to look only 1 level
  down, making it predictable.

 it will match the first level it finds.  its predictable.
you have multiple levels of dictionaries. Traversing them is not 
really predictable (although stable if noone changes them, within 
same run). 

So if there is:
class Address:
   street =Text()

class Person:
  address_real = Address()
  address_official = Address()
  mother = Person()

traversing through some person's attributes may in one run first 
find .street in person.address_real, in another run (or later in same 
run) in person.address_official. 

thus it's not really predictable.

okay, i'm not arguing, maybe just put some warning about it in the 
doco or something. Making some dictionaries ordered maybe also a way, 
if dynamic adding of attributes/relations is not allowed.

ciao
svil

--~--~-~--~~~---~--~~
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: how to make a query for multilevel reference?

2007-01-23 Thread Michael Bayer

yes, if you have two relations at the *same* level, both of which  
will lead to the same named property within, then select_by() by  
itself is useless.   if you are querying for a relaton that you think  
is local, and its not and then it magically traverses down and finds  
the wrong property, then its wrong regardless of whether multiple  
matching relationships exist or not...you expected it to only remain  
within the first level.  the error in that behavior will become  
apparent very quickly.  if you are relying upon the deep traversal  
behavior, then you'd better know what youre doing.   so far, nobody's  
ever had a problem with it.  my litmus test to remove an API and  
replace with something else usually starts with users keep getting  
errors with the current API, so this is not hitting that point yet.

the best way by far is to just use SelectResults and be explicit.  I  
should add a select_by() to SelectResults though.  im not thrilled  
with the inconvenience of using SelectResults and wish I had written  
Query to be more like it in the first place; but it is a different  
approach than Query and I would rather not conflate the two approaches.

On Jan 23, 2007, at 12:53 PM, svilen wrote:


 Which means - cripple the _locate_prop() to look only 1 level
 down, making it predictable.

 it will match the first level it finds.  its predictable.
 you have multiple levels of dictionaries. Traversing them is not
 really predictable (although stable if noone changes them, within
 same run).

 So if there is:
 class Address:
street =Text()

 class Person:
   address_real = Address()
   address_official = Address()
   mother = Person()

 traversing through some person's attributes may in one run first
 find .street in person.address_real, in another run (or later in same
 run) in person.address_official.

 thus it's not really predictable.

 okay, i'm not arguing, maybe just put some warning about it in the
 doco or something. Making some dictionaries ordered maybe also a way,
 if dynamic adding of attributes/relations is not allowed.

 ciao
 svil

 


--~--~-~--~~~---~--~~
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: how to make a query for multilevel reference?

2007-01-21 Thread sdobrev

  Lets have a Person having .address having .country having .name.
  How would give-me-persons-which-live-in France be expressed in
  SA (sorry for my SQL ignorance)?
  e.g. all-persons-for-which person.address.country.name ==
  'France'

http://www.sqlalchemy.org/docs/datamapping.myt#datamapping_selectrelations_relselectby
 Unfortunately I think if your Person has a name attribute too you
 have no alternative but to write the join out the long way.  (I'd
 love to find out I'm wrong though. :)

ahha. Maybe the underlying query._locate_prop() can be tweaked to 
accept hierarchical keys in some form (e.g. a.b.c.d.e.f ), and then 
not search but just follow the hierarchy...

e.g. 
#put in orm.query.Query 
#will search for the spec.hierarchycal key as a subkey, not just as 
root)

def _locate_prop(self, key, start=None):
import properties
keys = []
seen = util.Set()
def search_for_prop(mapper_, fullkey):
if mapper_ in seen:
return None
seen.add(mapper_)
key = fullkey[0]
if mapper_.props.has_key(key):
prop = mapper_.props[key]
if len(fullkey)==1:
if isinstance(prop, properties.SynonymProperty):
prop = mapper_.props[prop.name]
if isinstance(prop, properties.PropertyLoader):
keys.insert(0, prop.key)
return prop
else:
props = [prop]
fullkey = fullkey[1:]
for prop in mapper_.props.values():
if not isinstance(prop, properties.PropertyLoader):
continue
x = search_for_prop(prop.mapper, fullkey)
if x:
keys.insert(0, prop.key)
return x

return None
p = search_for_prop(start or self.mapper, key.split('.') )
if p is None:
raise exceptions.InvalidRequestError(Cant locate property 
named '%s' % key)
return [keys, p]


..
person = Person()
person.address = Address()
person.address.country = Country()
person.address.country.name = 'france'
session.save(person)
q1 = session.query(D).select_by( name='france')
q2 = session.query(D).select_by( **{'country.name':'france'} )
q3 = session.query(D).select_by( **{'address.country.name':'france'} )
assert q1 == q2 == q3
...

do try it, it seems to work...


have fun
svil

--~--~-~--~~~---~--~~
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: how to make a query for multilevel reference?

2007-01-21 Thread Michael Bayer

how about

q = session.query(D)
q.select(q.join_via([address, country])  (Country.c.name ==
'france'))

or

q = SelectResults(session.query(D))
q.join_to(address).join_to(country).select(Country.c.name=='france')

im not down with shoving literal strings into **kwargs at all


--~--~-~--~~~---~--~~
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: how to make a query for multilevel reference?

2007-01-21 Thread Rick Morrison
Hey what's with the bitwise AND operator? Does that actually work to AND
together parts of WHERE clauses?

On 1/21/07, Michael Bayer [EMAIL PROTECTED] wrote:


 how about

 q = session.query(D)
 q.select(q.join_via([address, country])  (Country.c.name ==
 'france'))

 or

 q = SelectResults(session.query(D))
 q.join_to(address).join_to(country).select(Country.c.name=='france')

 im not down with shoving literal strings into **kwargs at all


 


--~--~-~--~~~---~--~~
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: how to make a query for multilevel reference?

2007-01-21 Thread Michael Bayer

yes, although its a little inconvenient because it takes a high
precedence in the order of operations (thus you have to put () around
all expressions)


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---