Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

List w/ Custom Data Source - SQL Query with Nested JSON Arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    List w/ Custom Data Source - SQL Query with Nested JSON Arrays

    Hi everyone,

    I'm looking into taking advantage of the nested JSON Array syntax to display "joined" tables of records so I can guarantee that all of my, say, employees are shown with their respective company in a List control that is paginated. These companies have a relatively low number of employees each (usually 1 or 0, but as many as 5.)

    I looked at just using a SQL query with a join and Group Breaks and an epic number of columns. However, in the case of the lonely corporation with no one to work for it, I was getting blank rows and that's no good.

    I also liked the idea of having a simple List Template and having fancy arrays as members of each record.

    Does anyone have any advice on how best to query the db and put the data into this format:

    Code:
    [
      {
          COMPANY_ID : 1,
          COMPANY_NAME : "Things & Stuff",
          EMPLOYEES : [
                         { NAME : "Some Guy" },
                         { NAME : "John Doe"}
                      ]
       },
       {
          COMPANY_ID : 2,
          COMPANY_NAME : "Lonely Co",
          EMPLOYEES : []
       }
    ]
    I'm guessing that I'll have to do two SQL queries - one Query to get the top level records. A second to pull all the children. I was looking at using toPropertyArray method of the SQL::Connection object and then looping through all the parent & child records to merge them. If anyone has a better solution/different solution, I'd love to hear it.

    Thanks!
    Alpha Anywhere latest pre-release

    #2
    Re: List w/ Custom Data Source - SQL Query with Nested JSON Arrays

    Hey Sarah... it's a little hard for me to fathom what you're looking for. I understand parts of it... but can't get a good picture. Is it something like this?
    CompEmp.PNG

    Comment


      #3
      Re: List w/ Custom Data Source - SQL Query with Nested JSON Arrays

      Originally posted by Davidk View Post
      Hey Sarah... it's a little hard for me to fathom what you're looking for. I understand parts of it... but can't get a good picture. Is it something like this?
      [ATTACH=CONFIG]40576[/ATTACH]
      That's the results I had originally. I had a SQL query, but each row as an "employee". Except, in the case of Lonely Co, there were no employees, so I was getting blank lines.

      So I thought a better way to approach this so that (1) pagination was 20 *companies*, not 20 employees and (2) eliminated blank lines was to get my SQL data into the form below:
      Code:
      [
        {
            COMPANY_ID : 1,
            COMPANY_NAME : "Things & Stuff",
            EMPLOYEES : [
                           { NAME : "Some Guy", POSITION: "Manager" },
                           { NAME : "John Doe", POSITION: "Clerk"}
                        ]
         },
         {
            COMPANY_ID : 2,
            COMPANY_NAME : "Lonely Co",
            EMPLOYEES : []
         },
         {
            COMPANY_ID : 3,
            COMPANY_NAME : "Mart's Walls",
            EMPLOYEES : [
                           { NAME : "Janice", POSITION : "Site Manager" },
                           { NAME : "Mauriece", POSITION : "Cashier"},
                           { NAME : "Fred", POSITION : "Cashier"}
                        ]
         },
         {
            COMPANY_ID : 4,
            COMPANY_NAME : "HasBros",
            EMPLOYEES : [
                           { NAME : "Sampsonite Jones", POSITION : "Janitor" }
                        ]
         }
      ]
      So I could use this template instead:
      Code:
      <div style="background-color:#efefef;font-size:18px;font-weight:bold;padding-left:10px;border-bottom:1px solid #808080;">{COMPANY_NAME}</div>
      <div>{EMPLOYEES}
        {*empty}
        {/*empty}
        {*header}
      <div style="font-weight:bold;font-size:14px;margin-bottom:5px;border-bottom:1px dotted;"><span style="width:250px;padding-left:10px;display:inline-block;">Employees</span><span style="width:300px;padding-left:10px;display:inline-block;">Position</span></div>
        {/*header}
      <div><span style="width:250px;padding-left:10px;display:inline-block;">{NAME}</span><span style="width:300px;padding-left:10px;display:inline-block;">{POSITION}</span></div>
      {/EMPLOYEES}</div>
      My list looks more like this now:
      listLayoutPreview.png

      My question's pertains to getting the data from the SQL database and formatting it into JSON with Employee records as arrays - Are there any suggestions on how best to approach this? Here's what I came up with, but I want to know if someone knows of a better way to do this:
      Code:
      	sql1 =<<%sql1%
      SELECT *
      FROM company
      ORDER BY ID DESC
      %sql1%
      
      	sql2 =<<%sql2%
      SELECT * 
      FROM emp_to_company
      	 LEFT OUTER JOIN emp ON emp_to_company.EMP_ID = emp.ID
      ORDER BY emp_to_company.COMPANY_ID DESC
      %sql2%
      
      dim cn as sql::Connection
      if (cn.open("::Name::conn") <> .t.) then
       'error
       end
      end if
      
      dim company[0] as p
      dim employee[0] as p
      
      dim args as sql::Arguments
      
      if (cn.toPropertyArray(sql1,args,company) <> .t.) then
        'error
        cn.close()
        end
      end if
      
      if (cn.toPropertyArray(sql2,args,employee) <> .t.) then
        'error
        cn.close()
        end
      end if
      
      cn.close()
      
      dim i as n = 0
      dim j as n = 1
      for i = 1 to company.size()
        ' because data is sorted
        while (j <= employee.size() .and. employee[j].EMP_ID == company[i].ID)
          *array_append(company[i].EMPLOYEE, employee[j])
          j = j + 1
        end while
      next i
      
      json_result = json_generate(company,.f.,.t.,"",.f.)
      Last edited by TheSmitchell; 07-02-2015, 10:07 AM.
      Alpha Anywhere latest pre-release

      Comment


        #4
        Re: List w/ Custom Data Source - SQL Query with Nested JSON Arrays

        I'd say what you've got is excellent. You want a very specific output and achieved that with very little code and are making great use of the List control options for getting exactly what you need.

        If the List control had a "suppress row if empty" option that might do it... but given the List control's flexibility... you've tied it in really well.

        Comment


          #5
          Re: List w/ Custom Data Source - SQL Query with Nested JSON Arrays

          Originally posted by Davidk View Post
          I'd say what you've got is excellent. You want a very specific output and achieved that with very little code and are making great use of the List control options for getting exactly what you need.

          If the List control had a "suppress row if empty" option that might do it... but given the List control's flexibility... you've tied it in really well.
          Ha ha! Well, it looks like the List Control doesn't exactly handle my case very well. It doesn't show any of the Columns! EEK. Hope I don't need to set any properties on these things.
          Alpha Anywhere latest pre-release

          Comment


            #6
            Re: List w/ Custom Data Source - SQL Query with Nested JSON Arrays

            HA!

            I was having trouble getting an empty list into the JSON object returned from my function. I found that I had to run my generated JSON through json_standardize before returning it.

            Step 1: Get Parent Records

            This was the first thing that had to happen. I used cn.toPropertyArray to get my parent records:
            Code:
            dim groups[0] as p
            dim numToGet as n = -1
            dim startRec as n = -1
            
            'Handles pagination if you have it:
            if (e.getDataMode = "Navigate" .or. e.getDataMode = "fetchMore") then
            	numToGet = e.pageSize
            	startRec = e.targetLogicalRecordNumber
            end if
            
            flag = cn.toPropertyArray(sql,args,groups,numToGet,startRec)
            
            if flag = .f. then 
            	e.fatalError = .t.
            	e.errorText = "Could not execute query. " + crlf(2) + "Error reported was: " + cn.CallResult.text 
            	cn.close()
            	exit function 
            end if
            Step 2: Get Child Records
            Next are the child records I wanted. I generated a list of all the parent records so I could fetch only the child records I needed and put the children into a property array0:

            Code:
            dim members[0] as p
            
            if (e.paginateData == .t.) then
                    'Actually only need to do this if we're paginating. 
                    'If not, then groups will contain all of the parent records, doing this step is pointless
                    'and chews up cycles
            	dim groupIds[0] as n
            	for i = 1 to groups.size()
            		*array_append(groupIds,groups[i].GROUP_ID)
            	next i
            
            	args.set("GROUPS",crlf_to_comma(groupIds.dump()))
            end if
            
            flag = cn.toPropertyArray(sql_children,args,members)
            
            if flag = .f. then 
            	e.fatalError = .t.
            	e.errorText = "Could not execute query. " + crlf(2) + "Error reported was: " + cn.CallResult.text 
            	cn.close()
            	exit function 
            end if
            Step 3: Adding Children to Parent Records

            After I had all my records, I looped through the children and parent records and mashed everything together:
            Code:
            dim nullArr[0] as p
            dim j as n = 1
            for i = 1 to groups.size()
              while (j <= members.size() .and. members[j].GROUP_ID == groups[i].GROUP_ID) 
              	*array_append(groups[i].PEOPLE,members[j])
              	j = j + 1
              end while
              
              'If no People records were created, add PEOPLE as an *empty array* to groups[i]
              if (variable_exists("groups[i].PEOPLE") <> .t.) then
              	groups[i].PEOPLE = nullArr
              end if
            next i
            Step 4: Generating JSON

            This is the part that will result in hair loss. For the parent records that *had no children*, Alpha was barfing all over the json I was returning because it was apparently in a NON-STANDARD format. Jubilation. (I spent 8 hours trying to figure this out only to find that I just need a single function call to json_standardize.)
            Code:
            txt = json_generate(groups,.f.,.f.,"",.f.)
            txt = json_standardize(txt)
            AND NOW I CAN USE {PEOPLE}{*empty}{/*empty}{/PEOPLE} IN MY LIST TEMPALTE AND THE LIST DOESN'T EXPLODE WHEN THE FIRST RECORD HAS NO CHILD PEOPLE RECORDS.
            Alpha Anywhere latest pre-release

            Comment


              #7
              Re: List w/ Custom Data Source - SQL Query with Nested JSON Arrays

              Lost my place but sent an email.

              Here is the output directly from MySQL not sure if I have missed or added things (, or brackets) but it is simple to change.

              [
              {
              Company_Id: 1,
              Company_Name: Alpha,
              Employee: [
              {Name: Alex Alpha , Age: 23} ,
              {Name: Bert Alpha , Age: 32} ,
              {Name: Colin Alpha , Age: 60}]},

              {
              Company_Id: 2,
              Company_Name: Beta,
              Employee: []},

              {
              Company_Id: 3,
              Company_Name: Gamma,
              Employee: [
              {Name: Andy Gamma , Age: 45}]},

              {
              Company_Id: 4,
              Company_Name: Delta,
              Employee: [
              {Name: Ann Delta , Age: 18} ,
              {Name: Brian Delta , Age: 55}]

              }
              ]

              The output comes from 2 tables.

              I wrote 5 views for clarity. it builds this in 3/1000 of a second but I guess you could speed it up if you have a big data set.


              Michael

              Comment

              Working...
              X