{"id":152,"date":"2007-06-17T13:27:56","date_gmt":"2007-06-17T12:27:56","guid":{"rendered":"http:\/\/bar-solutions.com\/wordpress\/?p=152"},"modified":"2007-06-17T13:27:58","modified_gmt":"2007-06-17T12:27:58","slug":"crazy-things-in-codegen","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=152","title":{"rendered":"Crazy things in CodeGen&#8230;"},"content":{"rendered":"<p>While I was preparing for the presentation at <a title=\"Amis Academy\" href=\"http:\/\/www.amis.nl\/activiteiten.php?id=450\" target=\"_blank\">Amis Academy<\/a> about <a title=\"Quest CodeGen Utility Community\" href=\"http:\/\/codegen.inside.quest.com\/\" target=\"_blank\">CodeGen<\/a> I created an example to build an XML file based on data in the table. I started out with a script based on the (old) DEPT table (yes, I know, it&#8217;s very Oracle 7 to use these old tables).<\/p>\n<p><!--more--><\/p>\n<blockquote>\n<p><font face=\"Terminal\" size=\"1\">&lt;?xml version=&#8221;1.0&#8243; ?&gt;<br \/>&lt;DEPT&gt;<br \/>&nbsp; &lt;DEPTROW&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;DEPTNO&gt;10&lt;\/DEPTNO&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;DNAME&gt;ACCOUNTING&lt;\/DNAME&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;LOC&gt;NEW YORK&lt;\/LOC&gt;<br \/>&nbsp; &lt;\/DEPTROW&gt;<br \/>&nbsp; &lt;DEPTROW&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;DEPTNO&gt;20&lt;\/DEPTNO&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;DNAME&gt;RESEARCH&lt;\/DNAME&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;LOC&gt;DALLAS&lt;\/LOC&gt;<br \/>&nbsp; &lt;\/DEPTROW&gt;<br \/>&nbsp; &lt;DEPTROW&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;DEPTNO&gt;30&lt;\/DEPTNO&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;DNAME&gt;SALES&lt;\/DNAME&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;LOC&gt;CHICAGO&lt;\/LOC&gt;<br \/>&nbsp; &lt;\/DEPTROW&gt;<br \/>&nbsp; &lt;DEPTROW&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;DEPTNO&gt;40&lt;\/DEPTNO&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;DNAME&gt;OPERATIONS&lt;\/DNAME&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;LOC&gt;BOSTON&lt;\/LOC&gt;<br \/>&nbsp; &lt;\/DEPTROW&gt;<br \/>&lt;\/DEPT&gt;<\/font><\/p>\n<\/blockquote>\n<p>After a couple of iterations the script looked like this:<\/p>\n<blockquote>\n<p><font face=\"Terminal\" size=\"1\">&lt;?xml version=&#8221;1.0&#8243; ?&gt;<br \/># First, define the query.<br \/>[DEFARRAY]tabcontents[AS]<br \/>select deptno deptno<br \/>&nbsp;&nbsp;&nbsp;&nbsp; , dname dname<br \/>&nbsp;&nbsp;&nbsp;&nbsp; , loc loc<br \/>from [OBJNAME];<br \/>[ENDDEFARRAY]<br \/>#<br \/># Now use the array.<br \/>&lt;[OBJNAME]&gt;<br \/>[FOREACH]tabcontents<br \/>&nbsp; &lt;[OBJNAME]ROW&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;DEPTNO&gt;[DEPTNO]&lt;\/DEPTNO&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;DNAME&gt;[DNAME]&lt;\/DNAME&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;LOC&gt;[LOC]&lt;\/LOC&gt;<br \/>&nbsp; &lt;\/[OBJNAME]ROW&gt;<br \/>[ENDFOREACH]<br \/>&lt;\/[OBJNAME]&gt;<\/font><\/p>\n<\/blockquote>\n<p>But when I wanted to run this script against another table (EMP) I had to come up with smart queries to get the right data from the database.<\/p>\n<blockquote>\n<p><font face=\"Terminal\" size=\"1\">&lt;?xml version=&#8221;1.0&#8243; ?&gt;<br \/># First, define the query.<br \/>[DEFARRAY]tabcontents[AS]<br \/>select [OBJNAME].*, rowid therowid<br \/>from [OBJNAME];<br \/>[ENDDEFARRAY]<br \/>#<br \/># Now use the array.<br \/>&lt;[OBJNAME]&gt;<br \/>[FOREACH]tabcontents<br \/>&nbsp; [SETALIAS]MyRowId[TO][therowid]<br \/>&nbsp; [SETALIAS]MyTabName[TO][OBJNAME]<br \/>&nbsp; &lt;[OBJNAME]ROW&gt;<br \/>&nbsp; [DEFARRAY]tablayout[AS]<br \/>&nbsp; select utc.COLUMN_NAME Column_Name<br \/>&nbsp;&nbsp;&nbsp; from User_Tab_Columns utc<br \/>&nbsp;&nbsp; where utc.TABLE_NAME = &#8216;{MyTabName}&#8217;;<br \/>&nbsp; [ENDDEFARRAY]<br \/>&nbsp; [FOREACH]tablayout<br \/>&nbsp;&nbsp;&nbsp; [SETALIAS]MyColumnName[TO][Column_Name]<br \/>&nbsp;&nbsp;&nbsp; [DEFARRAY]fieldvalue[AS]<br \/>&nbsp;&nbsp;&nbsp; select {MyColumnName} value<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from {MyTabName}<br \/>&nbsp;&nbsp;&nbsp;&nbsp; where rowid = &#8216;{MyRowId}&#8217;<br \/>&nbsp;&nbsp;&nbsp; [ENDDEFARRAY]<br \/>&nbsp;&nbsp;&nbsp; [FOREACH]fieldvalue<br \/>&nbsp;&nbsp;&nbsp; &lt;{MyColumnName}&gt;[value]&lt;\/{MyColumnName}&gt;<br \/>&nbsp;&nbsp;&nbsp; [ENDFOREACH]<br \/>&nbsp; [ENDFOREACH]<br \/>&nbsp; &lt;\/[OBJNAME]ROW&gt;<br \/>[ENDFOREACH]<br \/>&lt;\/[OBJNAME]&gt;<\/font><\/p>\n<\/blockquote>\n<p>I ran this script against the DEPT table and it ran perfectly. Then I ran the script against the EMP table and it ran perfectly as well. But, when I wanted to run the script against both tables at once the script came up with errors. It seems that the views (DEFARRAY creates a view in the database) weren&#8217;t refreshed. Luckily I had some custom code in place to remove the views from the database.<\/p>\n<blockquote>\n<p><font face=\"Terminal\" size=\"1\">FUNCTION dropview<br \/>&nbsp; RETURN NUMBER<br \/>IS<br \/>BEGIN<br \/>&nbsp; EXECUTE IMMEDIATE &#8216;drop view &#8216; || v_createdviewname;<br \/>&nbsp; RETURN 0;<br \/>END dropview; <\/font><\/p>\n<\/blockquote>\n<p>After cleaning up the views at the end of the script, by adding some extra code to the script, it solved my problem. <\/p>\n<blockquote>\n<p><font face=\"Terminal\" size=\"1\">#Clean up the views created (using custom code ;-))<br \/>[SET]qe_qcgu.v_createdviewname[TO]fieldvalue<br \/>[SETALIAS]_dummy[TO]{qe_qcgu.dropview}<br \/>[SET]qe_qcgu.v_createdviewname[TO]tablayout<br \/>[SETALIAS]_dummy[TO]{qe_qcgu.dropview}<br \/>[SET]qe_qcgu.v_createdviewname[TO]tabcontents<br \/>[SETALIAS]_dummy[TO]{qe_qcgu.dropview}<\/font><\/p>\n<\/blockquote>\n<p>It took some time to figure out what was going wrong, but the reward seems to be even bigger when I got the software to do what I intended it to do. This was a really simple example, but I think it shows the power of what you can do with CodeGen.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While I was preparing for the presentation at Amis Academy about CodeGen I created an example to build an XML file based on data in the table. I started out with a script based on the (old) DEPT table (yes, I know, it&#8217;s very Oracle 7 to use these old tables).<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,5,4],"tags":[],"class_list":["post-152","post","type-post","status-publish","format-standard","hentry","category-codegen","category-plsql","category-tools"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=152"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/152\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}