Re: [sqlalchemy] dynamic query with many parametters

2013-09-08 Thread Simon King

On 8 Sep 2013, at 02:36, Mohsen Pahlevanzadeh m.pahlevanza...@gmail.com wrote:

 Suppose i need to send a set of table field and a value to search, i found 
 the following code:
 ///
 q = session.query(myClass)
 for attr, value in web_dict.items():
 
 q 
 = q.filter(getattr(myClass, attr).like(%%%s%% % value))
 ///
 
 
 But it have problem,
 
 i'm newbie in python can you investigate it?

I'm not sure if you understand how the above code is supposed to work, so I'll 
explain it. It assumes that web_dict is a dictionary mapping names of 
attributes of myClass to strings, and it will return instances of myClass where 
all the named attributes contain those values as substrings.

So, for example, if myClass had name and phonenumber properties, and 
assuming that web_dict looks like this:

web_dict = {
'name': 'Mohsen',
'phonenumber': '1',
}

web_dict.items() returns a list like this:

  [('name', 'Mohsen'), ('phonenumber', '1')]

So the first time round the for loop, attr will be set to name and 
value will be Mohsen. The second time round, attr will be phonenumber 
and value will be 1.

(Note that dictionaries aren't ordered, so web_dict.items() could actually 
return the pairs in a different order)

Let's break the loop down into a few more lines:

for attr, value in web_dict.items():
column = getattr(myClass, attr)
condition = column.like('%%%s%%' % value)
q = q.filter(condition)

getattr is a python function that retrieves a named attribute from an object. 
So:

getattr(myClass, phonenumber)

is exactly equivalent to:

myClass.phonenumber

So the first time round the loop, getattr(myClass, attr) returns 
myClass.name, and the second time round it will return myClass.phonenumber.

(http://docs.python.org/2/library/functions.html#getattr)


'%%%s%%' % value is a python string-formatting function. %s will be 
replaced by whatever is in value, and %% becomes %. So in our example 
above, the first time round the loop we will have the string %Mohsen%' and the 
second time round it will be %1%.

(http://docs.python.org/2/library/stdtypes.html#string-formatting)

That string is passed to the column.like function, which produces SQL such as 
'name LIKE %Mohsen%'. That condition is then passed to Query.filter, which 
will add it to the WHERE clause of the SQL using AND.

So the code will eventually end up doing something like this:

q = session.query(myClass)
q = q.filter(myClass.name.like('%Mohsen%'))
q = q.filter(myClass.phonenumber.like('%1%'))

which will end up producing SQL something like:

SELECT myClass columns
FROM myClass table
WHERE name LIKE '%Mohsen%'
AND phonenumber LIKE '%1%'

Is that the sort of SQL that you are hoping to see, or do you need something 
different?

Simon

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


[sqlalchemy] dynamic query with many parametters

2013-09-07 Thread Mohsen Pahlevanzadeh
Suppose i need to send a set of table field and a value to search, i found 
the following code:
///

q = session.query(myClass)for attr, value in web_dict.items():
q = q.filter(getattr(myClass, attr).like(%%%s%% % value))

///


But it have problem,

i'm newbie in python can you investigate it?

--mohsen

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] SqlAlchemy dynamic query generation

2011-12-23 Thread Pau Tallada
Hi,

Try composing a criteria using and_(), or_(), or its equivalent binary
operators ,|:

crit = literal(false)
crit |= (table.c.field == value)
crit = (table.c.field == value)
etc...


And then:
q = session.query(entity).filter(crit)

2011/12/22 Sana klh sana.kl...@gmail.com


 I tried it but i still get the same error.



 On Thu, Dec 22, 2011 at 2:31 PM, Robert Forkel xrotw...@googlemail.comwrote:

 conditions = [db.User.id==id]
 if user_name != ' ':
   conditions.append(db.User.name == user_name)
 elif age != ' ':
   conditions.append(db.User.age == age)
 elif place != ' ':
   conditions.append(db.User.place == place)

 result = db.User.filter(and_(*conditions)).all()

 might do the trick (untested).

 On Thu, Dec 22, 2011 at 9:53 AM, Sana klh sana.kl...@gmail.com wrote:
  Hi Robert Forkel,
 
  You got it right i am trying to have a combination of expression
 combined in
  and_.
 
  I need to add the attribute only when it is not equal to null.So i
 tried to
  check if it is not null and then from the expression and pass it to
 filter
  .But this does not work.
 
  I went through the document but unable to figure out how to do this
 through
  sql.
 
 
  Thank you
 
  On Thu, Dec 22, 2011 at 12:11 PM, Robert Forkel 
 xrotw...@googlemail.com
  wrote:
 
  You may want to look at the tutorial [1]. In the code you pasted you
  are assembling a string to pass to filter. While you can do that (but
  then it should be proper sql not python!), what you want to do is
  passing a python expression like db.User.age==age, or a combination of
  expressions combined using and_, or_, etc.
 
  [1]
 
 http://www.sqlalchemy.org/docs/orm/tutorial.html#common-filter-operators
 
  On Thu, Dec 22, 2011 at 6:34 AM, Sana klh sana.kl...@gmail.com
 wrote:
   I am getting the following error
  
   ProgrammingError: (ProgrammingError) (1064, You have an error in
 your
   SQL
   syntax; check the manual that corresponds to your MySQL server
 version
   for
   the right syntax to use near '== 1,db.User.age==23,d' at line 3)
  
  
   Is there any way by which i can generate dynamic query?
  
   Thank you
 Sana
  
  
  
   On Thu, Dec 22, 2011 at 4:40 AM, Jackson, Cameron
   cameron.jack...@thalesgroup.com.au wrote:
  
   You might get more help if you provide more details. What error are
 you
   getting?
  
   -Original Message-
   From: sqlalchemy@googlegroups.com [mailto:
 sqlalchemy@googlegroups.com]
   On
   Behalf Of Sana
   Sent: Thursday, 22 December 2011 2:50 AM
   To: sqlalchemy
   Subject: [sqlalchemy] SqlAlchemy dynamic query generation
  
   Hi All,
  
   I am trying to do query based on the user input as follows
  
   condition = 'and_(db.User.id == id'
   if user_name != ' ':
  condition += ',db.User.name == user_name'
   elif age != ' ':
  condition += ',db.User.age == age'
   elif place != ' ':
  condition += ',db.User.place == place'
  
   where = condition+')'
  
   result = db.User.filter(where).all()
  
  
   But Im getting error when i do this.
  
   Is there any way by which i can do this
  
  
   Thank you
 Sana
  
   --
   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
   sqlalchemy+unsubscr...@googlegroups.com.
   For more options, visit this group at
   http://groups.google.com/group/sqlalchemy?hl=en.
  
  
  
  
  
 -
   DISCLAIMER: This e-mail transmission and any documents, files and
   previous e-mail messages attached to it are private and
 confidential.
   They may contain proprietary or copyright material or information
 that
   is subject to legal professional privilege.  They are for the use of
   the intended recipient only.  Any unauthorised viewing, use,
   disclosure,
   copying, alteration, storage or distribution of, or reliance on,
 this
   message is strictly prohibited.  No part may be reproduced, adapted
 or
   transmitted without the written permission of the owner.  If you
 have
   received this transmission in error, or are not an authorised
   recipient,
   please immediately notify the sender by return email, delete this
   message and all copies from your e-mail system, and destroy any
 printed
   copies.  Receipt by anyone other than the intended recipient should
 not
   be deemed a waiver of any privilege or protection.  Thales Australia
   does not warrant or represent that this e-mail or any documents,
 files
   and previous e-mail messages attached are error or virus free.
  
  
  
 -
  
   --
   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
   sqlalchemy+unsubscr

Re: [sqlalchemy] SqlAlchemy dynamic query generation

2011-12-22 Thread Sana klh
Hi Robert Forkel,

You got it right i am trying to have a combination of expression combined
in and_.

I need to add the attribute only when it is not equal to null.So i tried to
check if it is not null and then from the expression and pass it to filter
.But this does not work.

I went through the document but unable to figure out how to do this through
sql.


Thank you

On Thu, Dec 22, 2011 at 12:11 PM, Robert Forkel xrotw...@googlemail.comwrote:

 You may want to look at the tutorial [1]. In the code you pasted you
 are assembling a string to pass to filter. While you can do that (but
 then it should be proper sql not python!), what you want to do is
 passing a python expression like db.User.age==age, or a combination of
 expressions combined using and_, or_, etc.

 [1]
 http://www.sqlalchemy.org/docs/orm/tutorial.html#common-filter-operators

 On Thu, Dec 22, 2011 at 6:34 AM, Sana klh sana.kl...@gmail.com wrote:
  I am getting the following error
 
  ProgrammingError: (ProgrammingError) (1064, You have an error in your
 SQL
  syntax; check the manual that corresponds to your MySQL server version
 for
  the right syntax to use near '== 1,db.User.age==23,d' at line 3)
 
 
  Is there any way by which i can generate dynamic query?
 
  Thank you
Sana
 
 
 
  On Thu, Dec 22, 2011 at 4:40 AM, Jackson, Cameron
  cameron.jack...@thalesgroup.com.au wrote:
 
  You might get more help if you provide more details. What error are you
  getting?
 
  -Original Message-
  From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
 On
  Behalf Of Sana
  Sent: Thursday, 22 December 2011 2:50 AM
  To: sqlalchemy
  Subject: [sqlalchemy] SqlAlchemy dynamic query generation
 
  Hi All,
 
  I am trying to do query based on the user input as follows
 
  condition = 'and_(db.User.id == id'
  if user_name != ' ':
 condition += ',db.User.name == user_name'
  elif age != ' ':
 condition += ',db.User.age == age'
  elif place != ' ':
 condition += ',db.User.place == place'
 
  where = condition+')'
 
  result = db.User.filter(where).all()
 
 
  But Im getting error when i do this.
 
  Is there any way by which i can do this
 
 
  Thank you
Sana
 
  --
  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
  sqlalchemy+unsubscr...@googlegroups.com.
  For more options, visit this group at
  http://groups.google.com/group/sqlalchemy?hl=en.
 
 
 
 
 -
  DISCLAIMER: This e-mail transmission and any documents, files and
  previous e-mail messages attached to it are private and confidential.
  They may contain proprietary or copyright material or information that
  is subject to legal professional privilege.  They are for the use of
  the intended recipient only.  Any unauthorised viewing, use, disclosure,
  copying, alteration, storage or distribution of, or reliance on, this
  message is strictly prohibited.  No part may be reproduced, adapted or
  transmitted without the written permission of the owner.  If you have
  received this transmission in error, or are not an authorised recipient,
  please immediately notify the sender by return email, delete this
  message and all copies from your e-mail system, and destroy any printed
  copies.  Receipt by anyone other than the intended recipient should not
  be deemed a waiver of any privilege or protection.  Thales Australia
  does not warrant or represent that this e-mail or any documents, files
  and previous e-mail messages attached are error or virus free.
 
 
 -
 
  --
  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
  sqlalchemy+unsubscr...@googlegroups.com.
  For more options, visit this group at
  http://groups.google.com/group/sqlalchemy?hl=en.
 
 
  --
  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
  sqlalchemy+unsubscr...@googlegroups.com.
  For more options, visit this group at
  http://groups.google.com/group/sqlalchemy?hl=en.

 --
 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
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy

Re: [sqlalchemy] SqlAlchemy dynamic query generation

2011-12-22 Thread Robert Forkel
conditions = [db.User.id==id]
if user_name != ' ':
   conditions.append(db.User.name == user_name)
elif age != ' ':
   conditions.append(db.User.age == age)
elif place != ' ':
   conditions.append(db.User.place == place)

result = db.User.filter(and_(*conditions)).all()

might do the trick (untested).

On Thu, Dec 22, 2011 at 9:53 AM, Sana klh sana.kl...@gmail.com wrote:
 Hi Robert Forkel,

 You got it right i am trying to have a combination of expression combined in
 and_.

 I need to add the attribute only when it is not equal to null.So i tried to
 check if it is not null and then from the expression and pass it to filter
 .But this does not work.

 I went through the document but unable to figure out how to do this through
 sql.


 Thank you

 On Thu, Dec 22, 2011 at 12:11 PM, Robert Forkel xrotw...@googlemail.com
 wrote:

 You may want to look at the tutorial [1]. In the code you pasted you
 are assembling a string to pass to filter. While you can do that (but
 then it should be proper sql not python!), what you want to do is
 passing a python expression like db.User.age==age, or a combination of
 expressions combined using and_, or_, etc.

 [1]
 http://www.sqlalchemy.org/docs/orm/tutorial.html#common-filter-operators

 On Thu, Dec 22, 2011 at 6:34 AM, Sana klh sana.kl...@gmail.com wrote:
  I am getting the following error
 
  ProgrammingError: (ProgrammingError) (1064, You have an error in your
  SQL
  syntax; check the manual that corresponds to your MySQL server version
  for
  the right syntax to use near '== 1,db.User.age==23,d' at line 3)
 
 
  Is there any way by which i can generate dynamic query?
 
  Thank you
    Sana
 
 
 
  On Thu, Dec 22, 2011 at 4:40 AM, Jackson, Cameron
  cameron.jack...@thalesgroup.com.au wrote:
 
  You might get more help if you provide more details. What error are you
  getting?
 
  -Original Message-
  From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
  On
  Behalf Of Sana
  Sent: Thursday, 22 December 2011 2:50 AM
  To: sqlalchemy
  Subject: [sqlalchemy] SqlAlchemy dynamic query generation
 
  Hi All,
 
  I am trying to do query based on the user input as follows
 
  condition = 'and_(db.User.id == id'
  if user_name != ' ':
     condition += ',db.User.name == user_name'
  elif age != ' ':
     condition += ',db.User.age == age'
  elif place != ' ':
     condition += ',db.User.place == place'
 
  where = condition+')'
 
  result = db.User.filter(where).all()
 
 
  But Im getting error when i do this.
 
  Is there any way by which i can do this
 
 
  Thank you
    Sana
 
  --
  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
  sqlalchemy+unsubscr...@googlegroups.com.
  For more options, visit this group at
  http://groups.google.com/group/sqlalchemy?hl=en.
 
 
 
 
  -
  DISCLAIMER: This e-mail transmission and any documents, files and
  previous e-mail messages attached to it are private and confidential.
  They may contain proprietary or copyright material or information that
  is subject to legal professional privilege.  They are for the use of
  the intended recipient only.  Any unauthorised viewing, use,
  disclosure,
  copying, alteration, storage or distribution of, or reliance on, this
  message is strictly prohibited.  No part may be reproduced, adapted or
  transmitted without the written permission of the owner.  If you have
  received this transmission in error, or are not an authorised
  recipient,
  please immediately notify the sender by return email, delete this
  message and all copies from your e-mail system, and destroy any printed
  copies.  Receipt by anyone other than the intended recipient should not
  be deemed a waiver of any privilege or protection.  Thales Australia
  does not warrant or represent that this e-mail or any documents, files
  and previous e-mail messages attached are error or virus free.
 
 
  -
 
  --
  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
  sqlalchemy+unsubscr...@googlegroups.com.
  For more options, visit this group at
  http://groups.google.com/group/sqlalchemy?hl=en.
 
 
  --
  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
  sqlalchemy+unsubscr...@googlegroups.com.
  For more options, visit this group at
  http://groups.google.com/group/sqlalchemy?hl=en.

 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group

Re: [sqlalchemy] SqlAlchemy dynamic query generation

2011-12-22 Thread Sana klh
I tried it but i still get the same error.



On Thu, Dec 22, 2011 at 2:31 PM, Robert Forkel xrotw...@googlemail.comwrote:

 conditions = [db.User.id==id]
 if user_name != ' ':
   conditions.append(db.User.name == user_name)
 elif age != ' ':
   conditions.append(db.User.age == age)
 elif place != ' ':
   conditions.append(db.User.place == place)

 result = db.User.filter(and_(*conditions)).all()

 might do the trick (untested).

 On Thu, Dec 22, 2011 at 9:53 AM, Sana klh sana.kl...@gmail.com wrote:
  Hi Robert Forkel,
 
  You got it right i am trying to have a combination of expression
 combined in
  and_.
 
  I need to add the attribute only when it is not equal to null.So i tried
 to
  check if it is not null and then from the expression and pass it to
 filter
  .But this does not work.
 
  I went through the document but unable to figure out how to do this
 through
  sql.
 
 
  Thank you
 
  On Thu, Dec 22, 2011 at 12:11 PM, Robert Forkel xrotw...@googlemail.com
 
  wrote:
 
  You may want to look at the tutorial [1]. In the code you pasted you
  are assembling a string to pass to filter. While you can do that (but
  then it should be proper sql not python!), what you want to do is
  passing a python expression like db.User.age==age, or a combination of
  expressions combined using and_, or_, etc.
 
  [1]
 
 http://www.sqlalchemy.org/docs/orm/tutorial.html#common-filter-operators
 
  On Thu, Dec 22, 2011 at 6:34 AM, Sana klh sana.kl...@gmail.com wrote:
   I am getting the following error
  
   ProgrammingError: (ProgrammingError) (1064, You have an error in your
   SQL
   syntax; check the manual that corresponds to your MySQL server version
   for
   the right syntax to use near '== 1,db.User.age==23,d' at line 3)
  
  
   Is there any way by which i can generate dynamic query?
  
   Thank you
 Sana
  
  
  
   On Thu, Dec 22, 2011 at 4:40 AM, Jackson, Cameron
   cameron.jack...@thalesgroup.com.au wrote:
  
   You might get more help if you provide more details. What error are
 you
   getting?
  
   -Original Message-
   From: sqlalchemy@googlegroups.com [mailto:
 sqlalchemy@googlegroups.com]
   On
   Behalf Of Sana
   Sent: Thursday, 22 December 2011 2:50 AM
   To: sqlalchemy
   Subject: [sqlalchemy] SqlAlchemy dynamic query generation
  
   Hi All,
  
   I am trying to do query based on the user input as follows
  
   condition = 'and_(db.User.id == id'
   if user_name != ' ':
  condition += ',db.User.name == user_name'
   elif age != ' ':
  condition += ',db.User.age == age'
   elif place != ' ':
  condition += ',db.User.place == place'
  
   where = condition+')'
  
   result = db.User.filter(where).all()
  
  
   But Im getting error when i do this.
  
   Is there any way by which i can do this
  
  
   Thank you
 Sana
  
   --
   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
   sqlalchemy+unsubscr...@googlegroups.com.
   For more options, visit this group at
   http://groups.google.com/group/sqlalchemy?hl=en.
  
  
  
  
  
 -
   DISCLAIMER: This e-mail transmission and any documents, files and
   previous e-mail messages attached to it are private and confidential.
   They may contain proprietary or copyright material or information
 that
   is subject to legal professional privilege.  They are for the use of
   the intended recipient only.  Any unauthorised viewing, use,
   disclosure,
   copying, alteration, storage or distribution of, or reliance on, this
   message is strictly prohibited.  No part may be reproduced, adapted
 or
   transmitted without the written permission of the owner.  If you have
   received this transmission in error, or are not an authorised
   recipient,
   please immediately notify the sender by return email, delete this
   message and all copies from your e-mail system, and destroy any
 printed
   copies.  Receipt by anyone other than the intended recipient should
 not
   be deemed a waiver of any privilege or protection.  Thales Australia
   does not warrant or represent that this e-mail or any documents,
 files
   and previous e-mail messages attached are error or virus free.
  
  
  
 -
  
   --
   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
   sqlalchemy+unsubscr...@googlegroups.com.
   For more options, visit this group at
   http://groups.google.com/group/sqlalchemy?hl=en.
  
  
   --
   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

[sqlalchemy] SqlAlchemy dynamic query generation

2011-12-21 Thread Sana
Hi All,

I am trying to do query based on the user input as follows

condition = 'and_(db.User.id == id'
if user_name != ' ':
condition += ',db.User.name == user_name'
elif age != ' ':
condition += ',db.User.age == age'
elif place != ' ':
condition += ',db.User.place == place'

where = condition+')'

result = db.User.filter(where).all()


But Im getting error when i do this.

Is there any way by which i can do this


Thank you
   Sana

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



RE: [sqlalchemy] SqlAlchemy dynamic query generation

2011-12-21 Thread Jackson, Cameron
You might get more help if you provide more details. What error are you getting?

-Original Message-
From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] On 
Behalf Of Sana
Sent: Thursday, 22 December 2011 2:50 AM
To: sqlalchemy
Subject: [sqlalchemy] SqlAlchemy dynamic query generation

Hi All,

I am trying to do query based on the user input as follows

condition = 'and_(db.User.id == id'
if user_name != ' ':
condition += ',db.User.name == user_name'
elif age != ' ':
condition += ',db.User.age == age'
elif place != ' ':
condition += ',db.User.place == place'

where = condition+')'

result = db.User.filter(where).all()


But Im getting error when i do this.

Is there any way by which i can do this


Thank you
   Sana

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



-
DISCLAIMER: This e-mail transmission and any documents, files and 
previous e-mail messages attached to it are private and confidential.  
They may contain proprietary or copyright material or information that 
is subject to legal professional privilege.  They are for the use of 
the intended recipient only.  Any unauthorised viewing, use, disclosure, 
copying, alteration, storage or distribution of, or reliance on, this 
message is strictly prohibited.  No part may be reproduced, adapted or 
transmitted without the written permission of the owner.  If you have 
received this transmission in error, or are not an authorised recipient, 
please immediately notify the sender by return email, delete this 
message and all copies from your e-mail system, and destroy any printed 
copies.  Receipt by anyone other than the intended recipient should not 
be deemed a waiver of any privilege or protection.  Thales Australia 
does not warrant or represent that this e-mail or any documents, files 
and previous e-mail messages attached are error or virus free.  

-

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] SqlAlchemy dynamic query generation

2011-12-21 Thread Sana klh
I am getting the following error

ProgrammingError: (ProgrammingError) (1064, You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version for
the right syntax to use near '== 1,db.User.age==23,d' at line 3)


Is there any way by which i can generate dynamic query?

Thank you
  Sana



On Thu, Dec 22, 2011 at 4:40 AM, Jackson, Cameron 
cameron.jack...@thalesgroup.com.au wrote:

 You might get more help if you provide more details. What error are you
 getting?

 -Original Message-
 From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] On
 Behalf Of Sana
 Sent: Thursday, 22 December 2011 2:50 AM
 To: sqlalchemy
 Subject: [sqlalchemy] SqlAlchemy dynamic query generation

 Hi All,

 I am trying to do query based on the user input as follows

 condition = 'and_(db.User.id == id'
 if user_name != ' ':
condition += ',db.User.name == user_name'
 elif age != ' ':
condition += ',db.User.age == age'
 elif place != ' ':
condition += ',db.User.place == place'

 where = condition+')'

 result = db.User.filter(where).all()


 But Im getting error when i do this.

 Is there any way by which i can do this


 Thank you
   Sana

 --
 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
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.



 -
 DISCLAIMER: This e-mail transmission and any documents, files and
 previous e-mail messages attached to it are private and confidential.
 They may contain proprietary or copyright material or information that
 is subject to legal professional privilege.  They are for the use of
 the intended recipient only.  Any unauthorised viewing, use, disclosure,
 copying, alteration, storage or distribution of, or reliance on, this
 message is strictly prohibited.  No part may be reproduced, adapted or
 transmitted without the written permission of the owner.  If you have
 received this transmission in error, or are not an authorised recipient,
 please immediately notify the sender by return email, delete this
 message and all copies from your e-mail system, and destroy any printed
 copies.  Receipt by anyone other than the intended recipient should not
 be deemed a waiver of any privilege or protection.  Thales Australia
 does not warrant or represent that this e-mail or any documents, files
 and previous e-mail messages attached are error or virus free.

 -

 --
 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
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.



-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] SqlAlchemy dynamic query generation

2011-12-21 Thread Robert Forkel
You may want to look at the tutorial [1]. In the code you pasted you
are assembling a string to pass to filter. While you can do that (but
then it should be proper sql not python!), what you want to do is
passing a python expression like db.User.age==age, or a combination of
expressions combined using and_, or_, etc.

[1] http://www.sqlalchemy.org/docs/orm/tutorial.html#common-filter-operators

On Thu, Dec 22, 2011 at 6:34 AM, Sana klh sana.kl...@gmail.com wrote:
 I am getting the following error

 ProgrammingError: (ProgrammingError) (1064, You have an error in your SQL
 syntax; check the manual that corresponds to your MySQL server version for
 the right syntax to use near '== 1,db.User.age==23,d' at line 3)


 Is there any way by which i can generate dynamic query?

 Thank you
   Sana



 On Thu, Dec 22, 2011 at 4:40 AM, Jackson, Cameron
 cameron.jack...@thalesgroup.com.au wrote:

 You might get more help if you provide more details. What error are you
 getting?

 -Original Message-
 From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] On
 Behalf Of Sana
 Sent: Thursday, 22 December 2011 2:50 AM
 To: sqlalchemy
 Subject: [sqlalchemy] SqlAlchemy dynamic query generation

 Hi All,

 I am trying to do query based on the user input as follows

 condition = 'and_(db.User.id == id'
 if user_name != ' ':
    condition += ',db.User.name == user_name'
 elif age != ' ':
    condition += ',db.User.age == age'
 elif place != ' ':
    condition += ',db.User.place == place'

 where = condition+')'

 result = db.User.filter(where).all()


 But Im getting error when i do this.

 Is there any way by which i can do this


 Thank you
   Sana

 --
 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
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.



 -
 DISCLAIMER: This e-mail transmission and any documents, files and
 previous e-mail messages attached to it are private and confidential.
 They may contain proprietary or copyright material or information that
 is subject to legal professional privilege.  They are for the use of
 the intended recipient only.  Any unauthorised viewing, use, disclosure,
 copying, alteration, storage or distribution of, or reliance on, this
 message is strictly prohibited.  No part may be reproduced, adapted or
 transmitted without the written permission of the owner.  If you have
 received this transmission in error, or are not an authorised recipient,
 please immediately notify the sender by return email, delete this
 message and all copies from your e-mail system, and destroy any printed
 copies.  Receipt by anyone other than the intended recipient should not
 be deemed a waiver of any privilege or protection.  Thales Australia
 does not warrant or represent that this e-mail or any documents, files
 and previous e-mail messages attached are error or virus free.

 -

 --
 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
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.


 --
 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
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] SqlAlchemy dynamic query generation

2011-12-21 Thread robert rottermann

the string you are generating must be proper sql.
to test is you migth print it and try in directly against the db.

but what you show us is for sure no valid sql. In sql the equality operator is 
not '==' but '='.

robert
On 22/12/11 06:34, Sana klh wrote:

I am getting the following error

ProgrammingError: (ProgrammingError) (1064, You have an error in your SQL 
syntax; check the manual that corresponds to your MySQL server version for the 
right syntax to use near '== 1,db.User.age==23,d' at line 3)



Is there any way by which i can generate dynamic query?

Thank you
  Sana



On Thu, Dec 22, 2011 at 4:40 AM, Jackson, Cameron 
cameron.jack...@thalesgroup.com.au 
mailto:cameron.jack...@thalesgroup.com.au wrote:


You might get more help if you provide more details. What error are you
getting?

-Original Message-
From: sqlalchemy@googlegroups.com mailto:sqlalchemy@googlegroups.com
[mailto:sqlalchemy@googlegroups.com mailto:sqlalchemy@googlegroups.com]
On Behalf Of Sana
Sent: Thursday, 22 December 2011 2:50 AM
To: sqlalchemy
Subject: [sqlalchemy] SqlAlchemy dynamic query generation

Hi All,

I am trying to do query based on the user input as follows

condition = 'and_(db.User.id http://db.User.id == id'
if user_name != ' ':
   condition += ',db.User.name http://db.User.name == user_name'
elif age != ' ':
   condition += ',db.User.age == age'
elif place != ' ':
   condition += ',db.User.place == place'

where = condition+')'

result = db.User.filter(where).all()


But Im getting error when i do this.

Is there any way by which i can do this


Thank you
  Sana

--
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
mailto:sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to
sqlalchemy+unsubscr...@googlegroups.com
mailto:sqlalchemy%2bunsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.



-
DISCLAIMER: This e-mail transmission and any documents, files and
previous e-mail messages attached to it are private and confidential.
They may contain proprietary or copyright material or information that
is subject to legal professional privilege.  They are for the use of
the intended recipient only.  Any unauthorised viewing, use, disclosure,
copying, alteration, storage or distribution of, or reliance on, this
message is strictly prohibited.  No part may be reproduced, adapted or
transmitted without the written permission of the owner.  If you have
received this transmission in error, or are not an authorised recipient,
please immediately notify the sender by return email, delete this
message and all copies from your e-mail system, and destroy any printed
copies.  Receipt by anyone other than the intended recipient should not
be deemed a waiver of any privilege or protection.  Thales Australia
does not warrant or represent that this e-mail or any documents, files
and previous e-mail messages attached are error or virus free.

-

--
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
mailto:sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to
sqlalchemy+unsubscr...@googlegroups.com
mailto:sqlalchemy%2bunsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.


--
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.


--
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Dynamic query

2011-05-09 Thread Enrico Morelli
On Fri, 6 May 2011 17:11:39 +0100
King Simon-NFHD78 simon.k...@motorolasolutions.com wrote:

  -Original Message-
  From: sqlalchemy@googlegroups.com
  [mailto:sqlalchemy@googlegroups.com] On Behalf Of Enrico Morelli
  Sent: 06 May 2011 16:20
  To: sqlalchemy
  Subject: [sqlalchemy] Dynamic query
  
  Dear all,
  
  I've a form where people fill one or more fields to search in a db.
  For the moment I solve it using a lot of if statement and a lot of
  different query based on the filled fields. Something like that:
  
  if start_date and end_date and instrument and details and
  technician: c.results =
  
  Session.query(Repairs).filter(and_(Repairs.start_date=start_date,
  Repairs.end_date=end_date,
  Repairs.instrument_id==instrument,
  Repairs.details.like('%%%s%%' % details),
  Repairs.technician.like('%%%s%%' % technician)
  )).order_by('start_date').all()
  
  elif start_date and end_date and instrument and details:
  c.results =
  
  Session.query(Repairs).filter(and_(Repairs.start_date=start_date,
  Repairs.end_date=end_date,
  Repairs.instrument_id==instrument,
  Repairs.details.like('%%%s%%' %
  details), )).order_by('start_date').all()
  
  and so on for each combination (for 5 fields I have 20 query!).
  There is
  a way to do that in a more dynamic way?
  
 
 You can call Query.filter multiple times. Here's an example:
 
 query = Session.query(Repairs)
 
 if start_date:
 query = query.filter(Repairs.start_date = start_date)
 
 if end_date:
 query = query.filter(Repairs.end_date = end_date)
 
 if instrument:
 query = query.filter(Repairs.instrument_id == instrument)
 
 # etc.
 
 results = query.order_by('start_date').all()
 
 
 Each filter condition will be combined using AND.
 
 Hope that helps,
 
 Simon
 
THANKS!!! Works very fine :-))

-- 
---
   (o_
(o_//\  Coltivate Linux che tanto Windows si pianta da solo.
(/)_   V_/_
+--+
| ENRICO MORELLI |  email: more...@cerm.unifi.it   |
| * *   *   *|  phone: +39 055 4574269 |
|  University of Florence|  fax  : +39 055 4574253 |
|  CERM - via Sacconi, 6 -  50019 Sesto Fiorentino (FI) - ITALY|
+--+

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Dynamic query

2011-05-06 Thread Enrico Morelli
Dear all,

I've a form where people fill one or more fields to search in a db.
For the moment I solve it using a lot of if statement and a lot of
different query based on the filled fields. Something like that:

if start_date and end_date and instrument and details and technician:
c.results =
Session.query(Repairs).filter(and_(Repairs.start_date=start_date,
Repairs.end_date=end_date, 
Repairs.instrument_id==instrument,
Repairs.details.like('%%%s%%' % details), 
Repairs.technician.like('%%%s%%' % technician)
)).order_by('start_date').all()

elif start_date and end_date and instrument and details:
c.results =
Session.query(Repairs).filter(and_(Repairs.start_date=start_date,
Repairs.end_date=end_date,
Repairs.instrument_id==instrument,
Repairs.details.like('%%%s%%' % details), 
)).order_by('start_date').all()

and so on for each combination (for 5 fields I have 20 query!). There is
a way to do that in a more dynamic way?

Thanks

-- 
---
   (o_
(o_//\  Coltivate Linux che tanto Windows si pianta da solo.
(/)_   V_/_
+--+
| ENRICO MORELLI |  email: more...@cerm.unifi.it   |
| * *   *   *|  phone: +39 055 4574269 |
|  University of Florence|  fax  : +39 055 4574253 |
|  CERM - via Sacconi, 6 -  50019 Sesto Fiorentino (FI) - ITALY|
+--+

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Dynamic query

2010-10-14 Thread Julien Cigar
Given all this magic with .with_polymorphic(), I'm now wondering if 
it's really worth to have a new Query class (by subclassing orm.Query) 
per mapped object just to apply some filters...


At first I found quite elegant to have one Query object per mapped 
class, but now I'm wondering if it's not better after all to have a 
bunch of @staticmethod in the model ...


What do you think ?

On 10/13/2010 11:21, Julien Cigar wrote:

On 10/12/2010 18:05, Julien Cigar wrote:

On 10/12/2010 17:09, Michael Bayer wrote:

On Oct 12, 2010, at 7:39 AM, Julien Cigar wrote:


Hello,

any idea why with

# Query

class BaseQuery(orm.Query):
@dynamic
def method_a(self):
...
def method_b(self):
...

class FooQuery(BaseQuery):
...

class BarQuery(FooQuery):
@dynamic
def method_c(self):
...

# Models

class BaseModel(object):
query = Session.query_property(BaseQuery)

#

myQuery = type('PolymorphicQuery, (content.BaseQuery, ),
func_list)(BaseModel)

where func_list containing all the functions decorated by @dynamic
the following fail? :

- myQuery.get(45) fails with: AttributeError: 'NoneType' object has
no attribute 'identity_map'
- myQuery.method_a().all() fails with: AttributeError: 'NoneType'
object has no attribute '_autoflush'
- etc



OK I think I found a solution, I need to pass session=Session.registry()
to my custom query:

  model.content.ContentQuery.__mro__
(class 'amnesia.model.content.ContentQuery', class
'amnesia.model.root.RootQuery', class 'sqlalchemy.orm.query.Query',
type 'object')
  PolymorphicQuery = type('PolymorphicQuery',
(model.content.ContentQuery, ), {})
  q1 = PolymorphicQuery(model.Content)
  q1.get(25)
Traceback (most recent call last):
File console, line 1, in module
File
/home/jcigar/venvs/pylons0.9.7/lib/python2.5/site-packages/SQLAlchemy-0.6.4-py2.5.egg/sqlalchemy/orm/query.py,
line 595, in get
return self._get(key, ident)
File
/home/jcigar/venvs/pylons0.9.7/lib/python2.5/site-packages/SQLAlchemy-0.6.4-py2.5.egg/sqlalchemy/orm/query.py,
line 1803, in _get
instance = self.session.identity_map.get(key)
AttributeError: 'NoneType' object has no attribute 'identity_map'
  q2 = PolymorphicQuery(model.Content, session=meta.Session.registry())
  q2.get(25)
amnesia.model.event.Event object at 0x939046c

I hope I'm not doing something wrong :p


My goal is to be able to build a custom Query object to use with the
.with_polymorphic() function ..

I've just grepped through all the source, examples and tests plus the
wiki trying to find what @dynamic is. Seems like something I'd have
come up with in the past but I've no clue at the moment what that is.




Sorry, I forgot to mention that it's just a custom decorator of mine
which add the function name to a list ... forget about it, it's just to
build the third argument of type() (func_list in my case)






--
No trees were killed in the creation of this message.
However, many electrons were terribly inconvenienced.

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

attachment: jcigar.vcf

Re: [sqlalchemy] Dynamic query

2010-10-14 Thread Michael Bayer

On Oct 14, 2010, at 9:28 AM, Julien Cigar wrote:

 Given all this magic with .with_polymorphic(), I'm now wondering if it's 
 really worth to have a new Query class (by subclassing orm.Query) per mapped 
 object just to apply some filters...
 
 At first I found quite elegant to have one Query object per mapped class, but 
 now I'm wondering if it's not better after all to have a bunch of 
 @staticmethod in the model ...
 
 What do you think ?


I generally think subclasses of Query should be used very conservatively, and 
that model specific behavior should be in the model, sure.



 
 On 10/13/2010 11:21, Julien Cigar wrote:
 On 10/12/2010 18:05, Julien Cigar wrote:
 On 10/12/2010 17:09, Michael Bayer wrote:
 On Oct 12, 2010, at 7:39 AM, Julien Cigar wrote:
 
 Hello,
 
 any idea why with
 
 # Query
 
 class BaseQuery(orm.Query):
 @dynamic
 def method_a(self):
 ...
 def method_b(self):
 ...
 
 class FooQuery(BaseQuery):
 ...
 
 class BarQuery(FooQuery):
 @dynamic
 def method_c(self):
 ...
 
 # Models
 
 class BaseModel(object):
 query = Session.query_property(BaseQuery)
 
 #
 
 myQuery = type('PolymorphicQuery, (content.BaseQuery, ),
 func_list)(BaseModel)
 
 where func_list containing all the functions decorated by @dynamic
 the following fail? :
 
 - myQuery.get(45) fails with: AttributeError: 'NoneType' object has
 no attribute 'identity_map'
 - myQuery.method_a().all() fails with: AttributeError: 'NoneType'
 object has no attribute '_autoflush'
 - etc
 
 
 OK I think I found a solution, I need to pass session=Session.registry()
 to my custom query:
 
  model.content.ContentQuery.__mro__
 (class 'amnesia.model.content.ContentQuery', class
 'amnesia.model.root.RootQuery', class 'sqlalchemy.orm.query.Query',
 type 'object')
  PolymorphicQuery = type('PolymorphicQuery',
 (model.content.ContentQuery, ), {})
  q1 = PolymorphicQuery(model.Content)
  q1.get(25)
 Traceback (most recent call last):
 File console, line 1, in module
 File
 /home/jcigar/venvs/pylons0.9.7/lib/python2.5/site-packages/SQLAlchemy-0.6.4-py2.5.egg/sqlalchemy/orm/query.py,
 line 595, in get
 return self._get(key, ident)
 File
 /home/jcigar/venvs/pylons0.9.7/lib/python2.5/site-packages/SQLAlchemy-0.6.4-py2.5.egg/sqlalchemy/orm/query.py,
 line 1803, in _get
 instance = self.session.identity_map.get(key)
 AttributeError: 'NoneType' object has no attribute 'identity_map'
  q2 = PolymorphicQuery(model.Content, session=meta.Session.registry())
  q2.get(25)
 amnesia.model.event.Event object at 0x939046c
 
 I hope I'm not doing something wrong :p
 
 My goal is to be able to build a custom Query object to use with the
 .with_polymorphic() function ..
 I've just grepped through all the source, examples and tests plus the
 wiki trying to find what @dynamic is. Seems like something I'd have
 come up with in the past but I've no clue at the moment what that is.
 
 
 
 Sorry, I forgot to mention that it's just a custom decorator of mine
 which add the function name to a list ... forget about it, it's just to
 build the third argument of type() (func_list in my case)
 
 
 
 
 -- 
 No trees were killed in the creation of this message.
 However, many electrons were terribly inconvenienced.
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalch...@googlegroups.com.
 To unsubscribe from this group, send email to 
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/sqlalchemy?hl=en.
 
 jcigar.vcf

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



Re: [sqlalchemy] Dynamic query

2010-10-13 Thread Julien Cigar

On 10/12/2010 18:05, Julien Cigar wrote:

On 10/12/2010 17:09, Michael Bayer wrote:

On Oct 12, 2010, at 7:39 AM, Julien Cigar wrote:


Hello,

any idea why with

# Query

class BaseQuery(orm.Query):
@dynamic
def method_a(self):
...
def method_b(self):
...

class FooQuery(BaseQuery):
...

class BarQuery(FooQuery):
@dynamic
def method_c(self):
...

# Models

class BaseModel(object):
query = Session.query_property(BaseQuery)

#

myQuery = type('PolymorphicQuery, (content.BaseQuery, ),
func_list)(BaseModel)

where func_list containing all the functions decorated by @dynamic
the following fail? :

- myQuery.get(45) fails with: AttributeError: 'NoneType' object has
no attribute 'identity_map'
- myQuery.method_a().all() fails with: AttributeError: 'NoneType'
object has no attribute '_autoflush'
- etc



OK I think I found a solution, I need to pass session=Session.registry() 
to my custom query:


 model.content.ContentQuery.__mro__
(class 'amnesia.model.content.ContentQuery', class 
'amnesia.model.root.RootQuery', class 'sqlalchemy.orm.query.Query', 
type 'object')
 PolymorphicQuery = type('PolymorphicQuery', 
(model.content.ContentQuery, ), {})

 q1 = PolymorphicQuery(model.Content)
 q1.get(25)
Traceback (most recent call last):
  File console, line 1, in module
  File 
/home/jcigar/venvs/pylons0.9.7/lib/python2.5/site-packages/SQLAlchemy-0.6.4-py2.5.egg/sqlalchemy/orm/query.py, 
line 595, in get

return self._get(key, ident)
  File 
/home/jcigar/venvs/pylons0.9.7/lib/python2.5/site-packages/SQLAlchemy-0.6.4-py2.5.egg/sqlalchemy/orm/query.py, 
line 1803, in _get

instance = self.session.identity_map.get(key)
AttributeError: 'NoneType' object has no attribute 'identity_map'
 q2 = PolymorphicQuery(model.Content, session=meta.Session.registry())
 q2.get(25)
amnesia.model.event.Event object at 0x939046c

I hope I'm not doing something wrong :p


My goal is to be able to build a custom Query object to use with the
.with_polymorphic() function ..

I've just grepped through all the source, examples and tests plus the
wiki trying to find what @dynamic is. Seems like something I'd have
come up with in the past but I've no clue at the moment what that is.




Sorry, I forgot to mention that it's just a custom decorator of mine
which add the function name to a list ... forget about it, it's just to
build the third argument of type() (func_list in my case)



--
No trees were killed in the creation of this message.
However, many electrons were terribly inconvenienced.

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

attachment: jcigar.vcf

[sqlalchemy] Dynamic query

2010-10-12 Thread Julien Cigar

Hello,

any idea why with

# Query

class BaseQuery(orm.Query):
@dynamic
def method_a(self):
...
def method_b(self):
...

class FooQuery(BaseQuery):
...

class BarQuery(FooQuery):
@dynamic
def method_c(self):
...

# Models

class BaseModel(object):
query = Session.query_property(BaseQuery)

#

myQuery = type('PolymorphicQuery, (content.BaseQuery, ), 
func_list)(BaseModel)


where func_list containing all the functions decorated by @dynamic the 
following fail? :


- myQuery.get(45) fails with: AttributeError: 'NoneType' object has no 
attribute 'identity_map'
- myQuery.method_a().all() fails with: AttributeError: 'NoneType' object 
has no attribute '_autoflush'

- etc

My goal is to be able to build a custom Query object to use with the 
.with_polymorphic() function ..


Thanks,
Julien

--
No trees were killed in the creation of this message.
However, many electrons were terribly inconvenienced.

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

attachment: jcigar.vcf

Re: [sqlalchemy] Dynamic query

2010-10-12 Thread Michael Bayer

On Oct 12, 2010, at 7:39 AM, Julien Cigar wrote:

 Hello,
 
 any idea why with
 
 # Query
 
 class BaseQuery(orm.Query):
   @dynamic
   def method_a(self):
   ...
   def method_b(self):
   ...
 
 class FooQuery(BaseQuery):
   ...
 
 class BarQuery(FooQuery):
   @dynamic
   def method_c(self):
   ...
 
 # Models
 
 class BaseModel(object):
   query = Session.query_property(BaseQuery)
 
 #
 
 myQuery = type('PolymorphicQuery, (content.BaseQuery, ), func_list)(BaseModel)
 
 where func_list containing all the functions decorated by @dynamic the 
 following fail? :
 
 - myQuery.get(45) fails with: AttributeError: 'NoneType' object has no 
 attribute 'identity_map'
 - myQuery.method_a().all() fails with: AttributeError: 'NoneType' object has 
 no attribute '_autoflush'
 - etc
 
 My goal is to be able to build a custom Query object to use with the 
 .with_polymorphic() function ..

I've just grepped through all the source, examples and tests plus the wiki 
trying to find what @dynamic is.   Seems like something I'd have come up with 
in the past but I've no clue at the moment what that is.


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



Re: [sqlalchemy] Dynamic query

2010-10-12 Thread Julien Cigar

 On 10/12/2010 17:09, Michael Bayer wrote:

On Oct 12, 2010, at 7:39 AM, Julien Cigar wrote:


Hello,

any idea why with

# Query

class BaseQuery(orm.Query):
@dynamic
def method_a(self):
...
def method_b(self):
...

class FooQuery(BaseQuery):
...

class BarQuery(FooQuery):
@dynamic
def method_c(self):
...

# Models

class BaseModel(object):
query = Session.query_property(BaseQuery)

#

myQuery = type('PolymorphicQuery, (content.BaseQuery, ), func_list)(BaseModel)

where func_list containing all the functions decorated by @dynamic the 
following fail? :

- myQuery.get(45) fails with: AttributeError: 'NoneType' object has no 
attribute 'identity_map'
- myQuery.method_a().all() fails with: AttributeError: 'NoneType' object has no 
attribute '_autoflush'
- etc

My goal is to be able to build a custom Query object to use with the 
.with_polymorphic() function ..

I've just grepped through all the source, examples and tests plus the wiki 
trying to find what @dynamic is.   Seems like something I'd have come up with 
in the past but I've no clue at the moment what that is.




Sorry, I forgot to mention that it's just a custom decorator of mine 
which add the function name to a list ... forget about it, it's just to 
build the third argument of type() (func_list in my case)


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

attachment: jcigar.vcf

[sqlalchemy] dynamic query selection

2009-09-13 Thread Eric Lemoine

Hello list

I have a simple mapping between a class and a table. What I'd like to
be able to do is dynamically change the query selection (based on some
HTTP param). More precisely I'd like that the ORM generate

SELECT some_sql_function(col1), col2, col3 FROM ...

instead of the default

SELECT col1, col2, col3, FROM ...

And, as I said, I'd like this to be dynamic, so I don't want this to
be hardwired in my mapping definition. Is this possible?

Thanks a lot,

-- 
Eric Lemoine

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 96
Mail : eric.lemo...@camptocamp.com
http://www.camptocamp.com

--~--~-~--~~~---~--~~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---