Forum OpenACS Development: Referencing postgresql array in XQL file

I get errors like this when referencing arrays in an XQL file

Is there any way to work around this?

invalid command name "c.name]"
    while executing
"c.name\] as path"
    invoked from within
"subst -nobackslashes {
		with recursive tree as (
		    select c.category_id, c.parent_category_id, c.name, 0 as level,array\[c.name\] as path
			  fr..."

		with recursive tree as (
		    select c.category_id, c.parent_category_id, c.name, 0 as level,array\[c.name\] as path
			  from ctrl_categories c
			 where c.parent_category_id = ctrl_category__lookup('//Personnel Title')
			 union
			 select child.category_id, child.parent_category_id, child.name, tree.level + 1 as level, (tree.path || c.name)::varchar(300)\[\]
			 from ctrl_categories child, tree
			where child.parent_category_id = tree.category_id)
		select	lpad(' ', (level-1)*4*6 + 1, ' ') || tree.name as name,
				tree.category_id
		  from	tree
       order by path
Collapse
Posted by Gustaf Neumann on
OpenACS uses at least since 2003 "subst -nobackslashes" in .xql files [1], probably since it was the the intention to leave backslash substitution to SQL. Since OpenACS 5.8 the necessity for backslash substitution in SQL was greatly reduced by using $$ instead of single quotes around the definition of SQL functions/procedures. AFAIK, there are still a few cases, where OpenACS sends backslashes to SQL for escaping.

The easiest quick solution is probably to set in the Tcl code a Tcl variable with the name of the SQL array
set array_cname {array[c.name]}
and to use in the .xql-file something like
...level,$array_cname as path....
Same with other SQL-chunks that need square brackets.

Another solution is to write the .sql-query without .xql (i.e. in the tcl file).

Hope this helps
-g

[1] https://github.com/openacs/openacs-core/blame/oacs-5-9/packages/acs-tcl/tcl/00-database-procs.tcl#L788