{"id":1630,"date":"2013-11-27T16:11:37","date_gmt":"2013-11-27T21:11:37","guid":{"rendered":"http:\/\/anvil-of-time.com\/wordpress\/?p=1630"},"modified":"2021-03-13T08:27:17","modified_gmt":"2021-03-13T13:27:17","slug":"powerbuilder-refactoring-code-to-reduce-database-calls","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/powerbuilder\/powerbuilder-refactoring-code-to-reduce-database-calls\/","title":{"rendered":"PowerBuilder &#8211; Refactoring Code to Reduce Database Calls"},"content":{"rendered":"\n<p>So I&#8217;m working on a fairly large enhancement to an application which is about twelve years old. &nbsp;In a nutshell it involves allowing users to see data for multiple sites and multiple locations within each site. &nbsp;Originally the app was created so the user could only view a single site at a time. &nbsp;Which sites (and locations) are visible to any given user is based on security settings.<\/p>\n\n\n\n<p>The existing windows normally had your typical &#8216;search&#8217; datawindows which defaulted to the &#8216;default&#8217; site assigned to the user and then they could select a location from a drop down datawindow.<\/p>\n\n\n\n<p>Example &#8216;post open&#8217; event on window code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted line-numbers\">datawindowchild ldwc\n\nlong ll_rows\n\ndw_search.getchild('loc_code', ldwc)\nldwc.settransobject(SQLCA)\nll_rows = ldwc.retrieve(gs_user_id)\ndw_search.object.loc_code[1] = gs_default_site<\/pre>\n\n\n\n<p>Example &#8216;itemchanged&#8217; event on dw_search code<\/p>\n\n\n\n<pre class=\"wp-block-code VB\"><code>this.getChild(\"site_to\", ldwc)\nldwc.setTransObject(SQLCA)\nIf ldwc.Retrieve(long(data)) = 0 then\n  ldwc.insertrow(0)\nEnd if<\/code><\/pre>\n\n\n\n<p>So each time the user changed the location value another retrieve was sent to the database.<\/p>\n\n\n\n<p>I needed to add a site column to the search datawindow which would also be a dddw. &nbsp;This meant that even more calls to the database would occur as the user changed the value in the new column.<\/p>\n\n\n\n<p>My approach involved creating an nvo with two datastores, one for the sites and the other for the locations. &nbsp;Each were populated when the window was opened (populated based on the user&#8217;s security settings). &nbsp;Then when I needed to set up the drop down datawindow columns I used the following.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted line-numbers\"><code>\/\/ all sites and locations for this user\nls_sites = invo_multisite.ids_site.object.datawindow.data\nls_locs = invo_multsite.ids_loc.object.datawindow.data\n\ndw_search.getchild('site_code', ldwc)\nldwc.importstring(ls_sites)\ndw_search.object.site_code&#91;1] = is_site_code\nll_find = ldwc.Find('site_code = ' + string(is_site_code), 1, ldwc.RowCount() + 1)\n\ndw_search.getchild('loc_code', ldwc)\nldwc.importstring(ls_locs)\n\nldwc.setfilter(\"site_code = '\" + is_site_code + \"' AND status = 'Active'\"\n\nldwc.filter() &nbsp;\/\/ only show locations associated with the chosen site<\/code><\/pre>\n\n\n\n<p>You need to remember to turn off Autoretrieve on the DDDW columns you are using for this. &nbsp;You also need to have the datawindow object used in the nvo datastores the same as that used in the other datawindow dddw columns. &nbsp;If you need to account for user access being changed during the sesssion, set up a timer in the nvo and periodically refresh the datastores.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I&#8217;m working on a fairly large enhancement to an application which is about twelve years old. &nbsp;In a nutshell it involves allowing users to see data for multiple sites and multiple locations within each site. &nbsp;Originally the app was created so the user could only view a single site at a time. &nbsp;Which sites&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,10],"tags":[21,12,16],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/1630"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=1630"}],"version-history":[{"count":7,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/1630\/revisions"}],"predecessor-version":[{"id":2122,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/1630\/revisions\/2122"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=1630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=1630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=1630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}