confused about optional args to ZSQL methods
I think I may have encountered this problem before and asked about it. If so, any solutions offered at that time escape me now. I have this ZSQL method (get_workorder): <params>id worker_id end_date</params> select * from work_order <dtml-sqlgroup where> <dtml-sqltest name=id op=eq type=int optional> <dtml-and> <dtml-sqltest name=worker_id op=eq type=int optional> <dtml-and> <dtml-sqltest name=end_date op=ge type=string optional> </dtml-sqlgroup> ; In a Python script I want to call it like so: workorders = context.sql.get_workorder(worker_id=worker_id, end_date='2003-04-23') that is, omit the presumably optional id parameter. However, when the Python script is run, the ZSQL method complains "Invalid integer value for id". I also tried these forms: workorders = context.sql.get_workorder(**{'worker_id': worker_id, 'end_date': '2003-04-23'}) workorders = context.sql.get_workorder(**{'id': None, 'worker_id': worker_id, 'end_date': '2003-04-23'}) workorders = context.sql.get_workorder(id=None, worker_id=worker_id, end_date='2003-04-23') without success. The first form gave the same error as above. The other two gave "object can't be converted to int". How do I arrange things so I can call get_workorder without passing the id parameter? Thx, -- Skip Montanaro - skip@pobox.com http://www.mojam.com/ http://www.musi-cal.com/
Good morning, I'am not shure but I have in my mind that Zope didn't like identifiers with the string id in it in ZSQL. Try worker_foo. Have a nice day. Lutz Steinborn On Wed, 4 Dec 2002 23:10:41 -0600 Skip Montanaro <skip@pobox.com> wrote:
I think I may have encountered this problem before and asked about it. If so, any solutions offered at that time escape me now. I have this ZSQL method (get_workorder):
<params>id worker_id end_date</params> select * from work_order <dtml-sqlgroup where> <dtml-sqltest name=id op=eq type=int optional> <dtml-and> <dtml-sqltest name=worker_id op=eq type=int optional> <dtml-and> <dtml-sqltest name=end_date op=ge type=string optional> </dtml-sqlgroup> ;
In a Python script I want to call it like so:
workorders = context.sql.get_workorder(worker_id=worker_id, end_date='2003-04-23')
that is, omit the presumably optional id parameter. However, when the Python script is run, the ZSQL method complains "Invalid integer value for id". I also tried these forms:
workorders = context.sql.get_workorder(**{'worker_id': worker_id, 'end_date': '2003-04-23'})
workorders = context.sql.get_workorder(**{'id': None, 'worker_id': worker_id, 'end_date': '2003-04-23'})
workorders = context.sql.get_workorder(id=None, worker_id=worker_id, end_date='2003-04-23')
without success. The first form gave the same error as above. The other two gave "object can't be converted to int".
How do I arrange things so I can call get_workorder without passing the id parameter?
Thx,
-- Skip Montanaro - skip@pobox.com http://www.mojam.com/ http://www.musi-cal.com/
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
-- Lutz Steinborn mailto:l.steinborn@4c-ag.de 4c AG Tel +49 6092 999592 Stefan-George-Ring 22-24 Fax +49 89 99341 399 81929 München http://www.4c-shopping.de
Lutz> I'am not shure but I have in my mind that Zope didn't like Lutz> identifiers with the string id in it in ZSQL. Try worker_foo. Thanks, I will check it out. Alas, that means I also need to ALTER TABLE in PostgreSQL... -- Skip Montanaro - skip@pobox.com http://www.mojam.com/ http://www.musi-cal.com/
On Thu, 2002-12-05 at 07:03, Skip Montanaro wrote:
Lutz> I'am not shure but I have in my mind that Zope didn't like Lutz> identifiers with the string id in it in ZSQL. Try worker_foo.
Thanks, I will check it out. Alas, that means I also need to ALTER TABLE in PostgreSQL...
No. Just use <dtml-sqltest newid column=id...> in your SQL template... Thierry
>> Thanks, I will check it out. Alas, that means I also need to ALTER >> TABLE in PostgreSQL... Thierry> No. Just use <dtml-sqltest newid column=id...> in your SQL Thierry> template... Ah, thanks. Where is the column=... attribute described? I don't remember seeing it in the ZSQL chapter of the Zope Book. Did I just miss it? -- Skip Montanaro - skip@pobox.com http://www.mojam.com/ http://www.musi-cal.com/
Hello Skip, "id" is obviously a very bad name for a user variable in Zope. rename it and use <dtml-sqltest name=newid col=id op=eq type=int optional> But I belive your problem will not be solved this way. You probably defined the parameter with "id:int worker_id:int end_date_string", didn't you? In that case Zope will test the parameter type before rendering the method, I am not quite sute what is the best solution, maybe remove the type identifier at all or try with a default argument id:int=None (id:int=_.None) Hope it helps Ulrich -- Searchengine Know How - Webpromotion - Optimization - Internal Search World Wide Web Publisher, Ulrich Wisser, Odensvag 13, S-14571 Norsborg http://www.publisher.de Tel: +46-8-53460905 Fax: +46-8-534 609 06
Ulrich> "id" is obviously a very bad name for a user variable in Ulrich> Zope. Obvious to who? "id" is a lovely parameter name in Python as long as you're aware you're hiding the rarely used id() builtin. The Zope community seems to be filled with lore that isn't codified anywhere or at best is written down in a sea of howto documents. Ulrich> But I belive your problem will not be solved this way. You Ulrich> probably defined the parameter with "id:int worker_id:int Ulrich> end_date_string", didn't you? Nope. Just the way I indicated: <params>id worker_id end_date</params> no type specifier in the params list, just in the <dtml-sqltest> tags Ulrich> what is the best solution, maybe remove the type identifier at Ulrich> all or try with a default argument id:int=None (id:int=_.None) Thanks, I'll give it a try. Skip
On Thu, Dec 05, 2002 at 10:31:02AM -0600, Skip Montanaro wrote:
Ulrich> "id" is obviously a very bad name for a user variable in Ulrich> Zope.
Obvious to who?
"Obvious" is always debatable... but here's my thoughts on this issue: every object in the ZODB has an id attribute which can be accessed by its getId() method, and which is used to qualify which object within a folder (or other ObjectManager) you want, e.g. http://my_zope_server:8080/foo/bar/getId ...or in python... context.foo.bar.getId() ... or just... context.foo.bar.id ... either one yields the string "bar". In python I'd say id might be ok to use as a variable name, as long as you don't mind hiding that builtin (which would be reason enough for me not to do it.) As for dtml, <dtml-var id> already means something: it's equivalent to context.id in a python script. And your original question referred to DTML. So I wouldn't ever define "id" in a dtml situation. -- Paul Winkler http://www.slinkp.com "Welcome to Muppet Labs, where the future is made - today!"
Paul> As for dtml, <dtml-var id> already means something: it's Paul> equivalent to context.id in a python script. And your original Paul> question referred to DTML. So I wouldn't ever define "id" in a Paul> dtml situation. Well, sort of. My original question was about a ZSQL method in which I take advantage of the dtml-sql* tags. I actually use Page Templates+ZSQL+Python scripts to implement stuff, not DTML, unless I can't avoid it (e.g. I have a small DTML page (less than 10 lines) that uses <dtml-tree>). chided-about-id-I-am-ly, y'rs, Thx, Skip
Skip Montanaro writes:
Ulrich> "id" is obviously a very bad name for a user variable in Ulrich> Zope.
Obvious to who? "id" is a lovely parameter name in Python as long as you're aware you're hiding the rarely used id() builtin. The Zope community seems to be filled with lore that isn't codified anywhere or at best is written down in a sea of howto documents.
In this case, it is a bug: Z SQL methods are ready to acquire explicitly named arguments. This should not be the case. Without this bug, nothing would deprecated "id" as parameter. With it, you must pass all parameters that might be acquired. And (as others pointed out) "id" is very easily acquired... Dieter
Skip Montanaro writes:
I think I may have encountered this problem before and asked about it. If so, any solutions offered at that time escape me now. The mailing lists are archived and searchable via Google ;-)
This means, when you miss something, you can easily find it again...
I have this ZSQL method (get_workorder):
<params>id worker_id end_date</params> select * from work_order <dtml-sqlgroup where> <dtml-sqltest name=id op=eq type=int optional> <dtml-and> <dtml-sqltest name=worker_id op=eq type=int optional> <dtml-and> <dtml-sqltest name=end_date op=ge type=string optional> I answered previously: When you use "sqltest" with "optional", you should not use "type=string" but "type=nb" (non blank).
"optional" is implemented as: convert the value to the required type; if that fails, omit the "sqltest". There is no problem to convert a value to a string. Converting to "nb" will fail, when the value is the empty string.
.... In a Python script I want to call it like so:
workorders = context.sql.get_workorder(worker_id=worker_id, end_date='2003-04-23')
that is, omit the presumably optional id parameter. I had answered this question as well:
Apparently, ZSQL methods acquire explicitely named parameters. "id" is easy to acquire (because it is almost obiquitous), but the problem is general. I consider this a bug. You may want to file a bug report to <http://Collector.zope.org>.
However, when the Python script is run, the ZSQL method complains "Invalid integer value for id". Strange, this should not happen with my understanding of "optional".
I checked the implementation (Shared/DC/ZRDB/sqltest.py) and found out that "optional" works as follows: * when the requested value does not exist, "sqltest" returns '' * when the value is false *and* it is a string, "sqltest" returns '' * otherwise, "optional" is not effective. Needless to say: I think, this is broken. As a work around: pass "id=''" in your "get_workorder" method. Dieter
participants (6)
-
Dieter Maurer -
Lutz Steinborn -
Paul Winkler -
Skip Montanaro -
Thierry Florac -
Ulrich Wisser