<font size=2 face="sans-serif">I am trying to convert my database views
into actual queries. </font>
<br><font size=2 face="sans-serif">My view code:</font>
<br>
<br><font size=2 face="sans-serif">sub list :Local {</font>
<br><font size=2 face="sans-serif"> my
($self, $c, $page) = @_;</font>
<br><font size=2 face="sans-serif"> $page
= $c->req->param('page') || 1;</font>
<br><font size=2 face="sans-serif"> my
$rs = $c->model('ORANGES::Contacts')->search_rs(undef, { </font>
<br><font size=2 face="sans-serif">
order_by => 'contact_name',</font>
<br><font size=2 face="sans-serif">
rows => 15,</font>
<br><font size=2 face="sans-serif">
page => $page,</font>
<br><font size=2 face="sans-serif"> });</font>
<br><font size=2 face="sans-serif"> $c->stash(contacts
=> $rs);</font>
<br><font size=2 face="sans-serif"> $c->stash(pager
=> $rs->pager());</font>
<br><font size=2 face="sans-serif"> $c->stash->{'template'}=>'contacts/list';</font>
<br><font size=2 face="sans-serif">}</font>
<br>
<br><font size=2 face="sans-serif">This results in a paged result set of
60 pages of 15 record each. Resultant sql:</font>
<br>
<br><font size=2 face="sans-serif">SELECT contact_id, contact_name, contact_email,
contact_role, team_flag, department_id, department_code FROM (</font>
<br><font size=2 face="sans-serif"> SELECT contact_id, contact_name,
contact_email, contact_role, team_flag, department_id, department_code,
ROW_NUMBER() OVER( ORDER BY contact_name ) AS rno__row__index FROM
(</font>
<br><font size=2 face="sans-serif"> SELECT me.contact_id,
me.contact_name, me.contact_email, me.contact_role, me.team_flag, me.department_id,
me.department_code FROM HCDB_TEST.CONTACTS me</font>
<br><font size=2 face="sans-serif"> ) me</font>
<br><font size=2 face="sans-serif">) me WHERE rno__row__index >= ? AND
rno__row__index <= ?</font>
<br>
<br><font size=2 face="sans-serif">: '1', '15'</font>
<br><font size=2 face="sans-serif">SELECT COUNT( * ) FROM HCDB_TEST.CONTACTS
me:</font>
<br>
<br><font size=2 face="sans-serif">My revised code, it's almost working:</font>
<br>
<br><font size=2 face="sans-serif">sub list1 :Local {</font>
<br><font size=2 face="sans-serif"> my
($self, $c, $page) = @_;</font>
<br><font size=2 face="sans-serif"> $page
= $c->req->param('page') || 1;</font>
<br><font size=2 face="sans-serif"> my
$rs = $c->model('ORANGES::Contact')->search_rs(undef, {</font>
<br><font size=2 face="sans-serif">
join => 'department_id',</font>
<br><font size=2 face="sans-serif">
order_by => 'contact_name',</font>
<br><font size=2 face="sans-serif">
prefetch => 'department_id',</font>
<br><font size=2 face="sans-serif">
rows => 15,</font>
<br><font size=2 face="sans-serif">
page => $page,</font>
<br><font size=2 face="sans-serif"> });</font>
<br><font size=2 face="sans-serif"> $c->stash(contacts
=> $rs);</font>
<br><font size=2 face="sans-serif"> $c->stash(pager
=> $rs->pager());</font>
<br><font size=2 face="sans-serif"> $c->stash->{'template'}=>'contacts/list';</font>
<br><font size=2 face="sans-serif">}</font>
<br>
<br><font size=2 face="sans-serif">This results in only 5 records and I
am not sure why. Resultant SQL:</font>
<br>
<br><font size=2 face="sans-serif">SELECT contact_id, contact_name, contact_email,
contact_role, team_flag, department_id, department_id__department_id, department_id__department_code,
department_id__department_pid, department_id__manager_id FROM (</font>
<br><font size=2 face="sans-serif"> SELECT contact_id, contact_name,
contact_email, contact_role, team_flag, department_id, department_id__department_id,
department_id__department_code, department_id__department_pid, department_id__manager_id,
ROW_NUMBER() OVER( ORDER BY contact_name ) AS rno__row__index FROM
(</font>
<br><font size=2 face="sans-serif"> SELECT me.contact_id,
me.contact_name, me.contact_email, me.contact_role, me.team_flag, me.department_id,
department_id.department_id AS department_id__department_id, department_id.department_code
AS department_id__department_code, department_id.department_pid AS department_id__department_pid,
department_id.manager_id AS department_id__manager_id FROM HCDB_TEST.CONTACT
me JOIN HCDB_TEST.DEPARTMENT department_id ON department_id.department_id
= me.department_id</font>
<br><font size=2 face="sans-serif"> ) me</font>
<br><font size=2 face="sans-serif">) me WHERE rno__row__index >= ? AND
rno__row__index <= ?</font>
<br>
<br><font size=2 face="sans-serif">: '1', '15'</font>
<br><font size=2 face="sans-serif">SELECT COUNT( * ) FROM HCDB_TEST.CONTACT
me JOIN HCDB_TEST.DEPARTMENT department_id ON department_id.department_id
= me.department_id: </font>
<br>
<br><font size=2 face="sans-serif">It seems that my code is doing funny
things with the join, I had to modify my template code to get it to display
the department_code by changing it from [% contact.department_code %] to
[% contact.department_id.department_code %].</font>
<br>
<br><font size=2 face="sans-serif">Some help with where I went wrong would
be appreciated.<br>
</font>
<br><font size=2 face="Arial">Regards</font>
<br>
<br>