How can I email myself the RAW SQL query that this php function is producing?

77 Views Asked by At

I want to run explain on a query that is slow but I don't know how to view the raw sql so I can run it in phpmyadmin and debug it. Here is the function.

    private function getAttImages($limit, $forumIds = 0, $fidsReverse = false, $topicIds = 0, $membersIds = 0, $order = 'attach_date', $sort = 'desc', $group = null)
{
    $fids = '';

    if ($forumIds)
    {
        $r = '';
        if ($fidsReverse)
        {
            $r = ' NOT ';
        }

        if (is_array($forumIds))
        {
            $forumIds = implode(',', $forumIds);
        }

        $fids = ' AND forums_topics.forum_id ' . $r . ' IN (' . $forumIds .  ')';
    }

    $tids = '';
    if ($topicIds)
    {
        $tids = ' AND forums_topics.tid IN (' . $topicIds . ')';
    }

    $mids = '';
    if ($membersIds)
    {
        $mids = ' AND core_attachments.attach_member_id IN (' . $membersIds . ')';
    }


    $whereT = array();
    $joinsT = array();

    $findInPosts = ' AND ' . \IPS\Db::i()->findInSet('queued', array('0'));
    $joinsT[] = array(
                        'select'    => 'forums_posts.*',
                        'from'      => 'forums_posts',
                        'where'     => array("forums_posts.pid=core_attachments_map.id2" . $findInPosts),
                );

    $findInTopics = ' AND ' . \IPS\Db::i()->findInSet('approved', array('1'));
    $joinsT[] = array(
                        'select'    => 'forums_topics.*',
                        'from'      => 'forums_topics',
                        'where'     => array("forums_topics.tid=forums_posts.topic_id" . $findInTopics . $fids . $tids),
                );

    $select = 'core_attachments.attach_id AS custom_data, core_attachments.*';
    if ($group)
    {
        $select = 'core_attachments.attach_id AS custom_data, COUNT(attach_is_image) as cnt_images, SUM(attach_hits) as summ_attach_hits, core_attachments.*';
    }

    $joinsT[] = array(
                        'select'    => $select,
                        'from'      => 'core_attachments',
                        'where'     => array('core_attachments.attach_is_image=1 AND core_attachments.attach_is_archived=0 AND core_attachments.attach_id=core_attachments_map.attachment_id' . $mids),

                );              

    $joinsT[] = array(  'select'    => 'core_members.member_id, core_members.member_group_id, core_members.mgroup_others, core_members.name, core_members.members_seo_name',
                        'from'      => 'core_members',
                        'where'     => array('core_attachments.attach_member_id=core_members.member_id' . $mids),
                );

    $joinsT[] = array(  'select'    => 'core_permission_index.perm_id',
                        'from'      => 'core_permission_index',
                        'where'     => array("core_permission_index.app='forums' AND core_permission_index.perm_type='forum' AND core_permission_index.perm_type_id=forums_topics.forum_id"),
                );

    $groupT = $group;

    $whereT[] = array(
        "core_attachments_map.location_key='forums_Forums' AND " . 
        \IPS\Db::i()->findInSet('perm_view', array_merge(array(\IPS\Member::loggedIn()->member_group_id), array_filter(explode(',', \IPS\Member::loggedIn()->mgroup_others)))) . " OR perm_view='*'" .
        $fids . $tids . $mids
    );

    $table = new \IPS\Helpers\Table\Db(
        'core_attachments_map',
        \IPS\Http\Url::internal('app=core&module=system&controller=nbattachpictures', 'front', 'nbattachpictures'),
        $whereT,
        $groupT
    );

    $table->joins = $joinsT;
    $table->sortBy = $order;
    $table->sortDirection = $sort;
    $table->limit = $limit;

    $table->rowsTemplate = array(\IPS\Theme::i()->getTemplate('plugins', 'core', 'global'), 'nbAttachmentsBlocksRows');

    $table->parsers = array(
        'custom_data' => function( $val, $row )
        {
            return array(
                'topic_data' => \IPS\Http\Url::internal("app=forums&module=forums&controller=topic&id={$row['tid']}", 'front', 'forums_topic', array($row['title_seo'])),
                'summ_attach_hits' => $row['summ_attach_hits'],
                'jewel' => $this->attachJewel($row['summ_attach_hits']),
            );
        },
    );

    return $table;
}

Anybody know how I can see the SQL query only that is produced by this function? email is better than echo as I want to grab query from live site.

1

There are 1 best solutions below

5
On

You could var_dump($table) and write the result in an email using the native php mail function or write it in a log file (this option is better).

Is that framework open-source? Because I couldn't find any documentation about the class \IPS\Helpers\Table\Db. Probably there's a method in it to build the query, you could look for it at that class source code and put the result of that method into the email message or log file instead of var_dump the table.