Forum OpenACS Development: xql files don't work

Collapse
Posted by Iuri Sampaio on
Hi there,

i am trying to use .xql files to write queries called in the tCL files.

However for some reason when i use the queries in the xql files they just don't work.

Then I tried to put the queries directly in the tcl file and they worked great.

i wonder why is that

here it is and example
Scenario 1: Everything in the TCL file
#####
db_foreach select_states {
select c.category as state, count(c.category_id) as num_items
from im_categories c,im_conf_items i
where c.category_id = i.conf_item_status_id
group by c.category

} {
lappend item_status_options [list $state $state $num_items]
}
###

Scenario 2: if i use

In the tcl file
####
db_foreach select_states { } {
lappend item_status_options [list $state $state $num_items]
}
####

and in the xql file:

###
queryset
fullquery name="select_item_states"
querytext

select c.category as state, count(c.category_id) as num_items
from im_categories c,im_conf_items i
where c.category_id = i.conf_item_status_id
group by c.category

/querytext
/fullquery
/queryset
########

Collapse
2: Re: xql files don't work (response to 1)
Posted by Emmanuelle Raffenne on
Hi Iuri,

When adding a new query in an existing .xql file or adding a new .xql file, you need to reload it from the APM. Maybe you missed that step.

Collapse
3: Re: xql files don't work (response to 1)
Posted by Deds Castillo on
Iuri,

Your .tcl file shows "select_states" so your fullquery name should be select_states as well.

Collapse
4: Re: xql files don't work (response to 3)
Posted by Iuri Sampaio on
Right Emma,
I was aware about it. I had set up the file to be watched.

Deds,
sorry, wrong paste. i had fixed that before i sent the message. it works in the tcl but not calling the xql file

TCL file
set item_status_options [list]
db_foreach select_states { } { lappend item_status_options [list $state $state $num_items] }

XQL file
queryset
fullquery name="select_states"
querytext

select c.category as state, count(c.category_id) as num_items
from im_categories c,im_conf_items i
where c.category_id = i.conf_item_status_id
group by c.category

/querytext
/fullquery
/queryset

The error i get is bellow.

[24/Jul/2009:22:30:45][2031.3029834672][-conn:openacs::2] Error: GET http://iurix.com:8090/dispatch/equipment/?
referred by "http://iurix.com:8090/dispatch/equipment/";
Database operation "select" failed
(exception NSDB, "Query was not a statement returning rows.")

while executing
"ns_pg_bind select nsdb0 { }"
("uplevel" body line 1)
invoked from within
"uplevel $ulevel [list ns_pg_bind $type $db $sql]"
invoked from within
"db_exec select $db $full_statement_name $sql"
invoked from within
"set selection [db_exec select $db $full_statement_name $sql]"
("uplevel" body line 2)
invoked from within
"uplevel 1 $code_block "
invoked from within
"db_with_handle -dbn $dbn db {
set selection [db_exec select $db $full_statement_name $sql]

set counter 0
while { [db_getrow $..."
(procedure "db_foreach" line 36)
invoked from within
"db_foreach select_states { } { lappend item_status_options [list $state $state $num_items] }"
("uplevel" body line 130)
invoked from within
"uplevel {
# /packages/intranet-confdb/www/index.tcl

Collapse
5: Re: xql files don't work (response to 1)
Posted by Richard Hamilton on
They definitely do!

Here are some things to check:

1) .xql file and .tcl file exactly similar names.

db_foreach select_states { } {
lappend item_status_options [list $state $state $num_items]
}

2) A complete set of xml tags in xql file:

<?xml version="1.0"?>

<queryset>

<fullquery name="select_states">
      <querytext>
        select c.category as state, count(c.category_id) as num_items
        from im_categories c,im_conf_items i
        where c.category_id = i.conf_item_status_id
        group by c.category
      </querytext>
</fullquery>

</queryset>

3) Check that the unix permissions are correctly set on the files.

4) Retart the server:

kill -9 `cat /usr/local/aolserver/log/nspid.yourserver`

or use developer support to re-load the changed file. This is beause the xql files are ONLY sourced at server startup time.

5) If it still won't play, copy another query/tcl pair and modify it to avoid errors creeping in. For what it is worth, most code has the query in quotes in the tcl as well as in the xql:

db_foreach "query_name" {code per row}

and I always put it in both places. You can still verify which query is running by making a change to one of them that is obvious from the results.

6) Weep with frustration at the knowledge that if it still doesn't work, its your fault! :-|

Been there.

Regards
Richard

Collapse
6: Re: xql files don't work (response to 1)
Posted by Richard Hamilton on
Luri,

Please disregard the comment about the quotes.

db_foreach query_name { query } { code }

is correct.

One other thing to check is that if your code is written as a tcl library function, that also will need to be re-loaded at server startup and your queryname in the xql should be fully qualified:

<fullquery name ="your_proc_name.your_query_name">

Collapse
7: Re: xql files don't work (response to 6)
Posted by Iuri Sampaio on
precisely hamilton,

I have checked, tcl and xql pages are not part of a tcl library function.

i am working on /www directory, in the index page of an application i installed.

PLus, i have changed tcl and xql code a little

set item_status_options [list]
db_foreach select_state { *SQL* } { lappend item_status_options [list $state $state $num_items] }

fullquery name="select_state.select_states"
querytext
select c.category as state, count(c.category_id) as num_items
from im_categories c,im_conf_items i
where c.category_id = i.conf_item_status_id
group by c.category
/querytext
/fullquery

The error remains the same

based on what you are saying i don't see how the pages "/acs-subiste/www/members/index" works then.
I have based the code i wrote code on it.

Collapse
8: Re: xql files don't work (response to 1)
Posted by Richard Hamilton on
You said:

In the tcl file: db_foreach select_states

in the xql file: name="select_item_states"

That isn't going to work.

Remember also that for database specific queries you need an xql file called yourfile-postgresql.xql, though that may not be relevant in your case.

The fully qualified names are not absolutely necessary in your example but they enable you to invoke queries from a namespace other than the default.

Have you correctly set up the page contract and is the page template working ok?

The obvious issue is the naming as shown above. Beyond that I am struggling to find another reason. I suspect it will come down to a missing or spurious character somewhere.

If you have no luck why not take the sample files from the notes application, run one in your application as is, and then substitute your query sql into the xql file without changing the name. If you make only one change at a time you'll find where the problem is.

R.

Collapse
9: Re: xql files don't work (response to 1)
Posted by Torben Brosten on
Iuri, for completeness:

Verify that the file permissions are consistent with other xql files that are working.

Sometimes an xql file will not load, for example if there is an error in the form of the file.

Remove any tabs in the xql file. xql files do not like tabs. Results can seem unpredictable. Also check for other problematic invisible characters.

Check for the existence of the query name used elsewhere in the xql file or another xql file in the same directory.

I've been frustrated with nonpredictable xql problems. Most have been caused by one of these.

Hope this helps,

Torben

Collapse
10: Re: xql files don't work (response to 9)
Posted by Iuri Sampaio on
Thanks Torben, Thanks to every feedback
The solution was right in front i haven't seen it.

File permission was the problem.
it was only with r permission
I set them to 755 now
thanks

The debuging list example in this thread is saved for future use now

I will debug notes application to learn some more code standards.