Hello again all,

I'm working on some generalized code, of which the following is an example case. I have the following structure in a hash (brought to you today by Data::Dumper):

'structure' => { 'groups' => { 'properties' => { 'groupid' => undef, 'grouptitle' => undef }, 'source' => 'user.groups' }, 'identity' => { 'properties' => { 'username' => unde +f, 'id' => undef }, 'source' => undef } },

And I have a SQL query that will retrieve the values for groupid,grouptitle,username and id. It may return one or many rows, but what I'd like to do is to take every record that comes through and stuff it into the above structure, and then pass a list of these hashrefs back to the calling code.


I've already got ideas on how to handle one-to-many relationships within records. Case in point is the 'groups' hashref. When i do a lookup on it's 'source' attribute, i get back that it has a toMany relationship with the primary key for this query (which in this case is 'id' ). So I know that the record structure i return will look like this:

{ 'groups' => [ { groupid=> '0', grouptitle=>'SysAdmins', }, { groupid=> '1', grouptitle=>'PerlGurus', } ] 'identity' => { username=> 'LWall', id=>1 } }

I intend to handle this by having the records stored in a hash (using the query's primary key for the hash's key), and an array of references to the hash entries to maintain order (important as the sql queries will often have Order By clauses). I'll then return either the array or a reference to that array to the calling code.

My question is how to get there from here. What would people suggest for the best approach for:

  1. making a new record structure in the hash of records when a new value of the primary key is encountered
  2. stuffing the values returned from the query into this structure

In reply to Building and returning structured records from a query by AidanLee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.