Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-65168

Move preload SQL functions to new class

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Fixed
    • Icon: Minor Minor
    • 3.7
    • 3.7
    • Database SQL/XMLDB, Forum
    • MOODLE_37_STABLE
    • MOODLE_37_STABLE
    • Hide

      For the integrator:

      • Please review that there aren't remaining uses of the "old" methods being used in core. It's not crazy to imagine that some ongoing work has introduced new uses over the last days. A quick grep should be enough.

      Once the above is verified:

      • CI is your friend: Unit tests
      Show
      For the integrator: Please review that there aren't remaining uses of the "old" methods being used in core. It's not crazy to imagine that some ongoing work has introduced new uses over the last days. A quick grep should be enough. Once the above is verified: CI is your friend: Unit tests

      As discussed with stronk7, the new preload SQL functions are not well placed.

      This issue moves them to a new class at \core\dml\table which provides functions to generate the SQL field select, and to pull data for that table from a mixed result stdClass.

      In short, these functions are intended as helpers to allow you to select data from multiple tables into usable objects in a single query. This is something that we do regularly and we do it in a variety of painful ways.

      This is an attempt to produce a single solution which is reuable, and easy to read.

       
      $posttable = new \core\dml\table('forum_posts', 'p', 'p_');
      $discussiontable = new \core\dml\table('forum_discussions', 'd', 'd_');
      $forumtable = new \core\dml\table('forum', 'f', 'f_');
      $coursetable = new \core\dml\table('course', 'c', 'c_');
       
      $sql = "SELECT " .
          implode(', ', [
            $posttable->get_field_select(),
            $discussiontable->get_field_select(),
            $forumtable->get_field_select(),
            $coursetable->get_field_select(),
          ]) . " FROM " . $posttable->get_from_sql() .
               " JOIN " . $discussiontable->get_from_sql() . " ON d.id = p.discussion " .
               " JOIN " . $forumtable->get_from_sql() . " ON f.id = d.forum " .
               " JOIN " . $coursetable->get_from_sql() . " ON c.id = f.course " .
               " WHERE p.userid = :author";
       
      $data = $DB->get_record_sql($sql, ['author' => $USER->id]);
       
      foreach ($data as $datum) {
          $post = $posttable->extract_fields_from_result($data);
          $discussion = $discussiontable->extract_fields_from_result($data);
          $forum = $forumtable->extract_fields_from_result($data);
          $course = $coursetable->extract_fields_from_result($data);
      }
      

            dobedobedoh Andrew Lyons
            dobedobedoh Andrew Lyons
            Eloy Lafuente (stronk7) Eloy Lafuente (stronk7)
            Jun Pataleta Jun Pataleta
            CiBoT CiBoT
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 1 hour, 30 minutes
                1h 30m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.