swi-prolog, odbc_prepare and findall-option

23 views
Skip to first unread message

Ben Engbers

unread,
Sep 29, 2015, 5:39:02 AM9/29/15
to SWI-Prolog

I' m trying to prepare a query based on information that is stored in an XML-file.


The following code runs without problems:

prepare_query(QueryFile, Q_Name, Statement) :-
  load_structure
(QueryFile, QueryXML, [dialect(xml), space(remove)]),
  getDNS
( QueryXML, Q_Name, DNS),
  getSelect
(QueryXML, Q_Name, Select),
  getParameter
(QueryXML, Q_Name, Parameters),
 
% getResultNames(QueryXML, Q_Name, ResultNames),
  odbc_prepare
(DNS, Select, Parameters, Statement,
   
[ findall((Apl_id_n, Apl_id_b), row(Apl_id_n, Apl_id_b)) ]).

getDNS
(QueryXML, Term, DNS) :-
  xpath
(QueryXML, //query(@name=Term), Query),
  xpath
(Query, //dns(@name=DNS_Name), _),
  odbc_connect
(DNS_Name, DNS, [alias(DNS_Name)]).

getSelect
(QueryXML, Term, Select) :-
  xpath
(QueryXML, //query(@name=Term)/select(text), Select).

getParameter
(QueryXML, Term, Parameters) :-
  xpath
(QueryXML, //query(@name=Term)/parameter_type, P_Type),
  findall
(Par, xpath(P_Type, //parameter(text), Par), Parameters).

getResultNames
(QueryXML, Term, ResultNames) :-
  xpath
(QueryXML, //query(@name=Term)/result, Res),
  findall
(Result, xpath(Res, //column/name(text), Result), Results),
  maplist
(firstCharUp, Results, ResultNamesList),
  atomic_list_concat
(ResultNamesList, ', ',ResultNames) .


Name and type from the selected fields are stored in the XML-file.

The predicate getResultNames returns these names (in this case apl_id_n and apl_id_b)  as an atom 'Apl_id_n, Apl_id_b' so I understand why the following code

FindAllOption =.. [findall,(ResultNames),row(ResultNames)], writeln(FindAllOption)


returns:

findall(Apl_id_n, Apl_id_b,row(Apl_id_n, Apl_id_b))
which gives an error in SWI-prolog.


Is it possible to split up ResultNames in the separated terms and to add the extra parentheses so FindAllOption is created as:

findall((Apl_id_n, Apl_id_b),row(Apl_id_n, Apl_id_b))


Is there another way to make the findall-option more flexible?

Ben



Reply all
Reply to author
Forward
0 new messages