PMA\libraries\Table::checkIfMinRecordsExist PHP Method

checkIfMinRecordsExist() public method

Checks if the number of records in a table is at least equal to $min_records
public checkIfMinRecordsExist ( integer $min_records ) : boolean
$min_records integer Number of records to check for in a table
return boolean True, if at least $min_records exist, False otherwise.
    public function checkIfMinRecordsExist($min_records = 0)
    {
        $check_query = 'SELECT ';
        $fieldsToSelect = '';
        $uniqueFields = $this->getUniqueColumns(true, false);
        if (count($uniqueFields) > 0) {
            $fieldsToSelect = implode(', ', $uniqueFields);
        } else {
            $indexedCols = $this->getIndexedColumns(true, false);
            if (count($indexedCols) > 0) {
                $fieldsToSelect = implode(', ', $indexedCols);
            } else {
                $fieldsToSelect = '*';
            }
        }
        $check_query .= $fieldsToSelect . ' FROM ' . $this->getFullName(true) . ' LIMIT ' . $min_records;
        $res = $GLOBALS['dbi']->tryQuery($check_query);
        if ($res !== false) {
            $num_records = $GLOBALS['dbi']->numRows($res);
            if ($num_records >= $min_records) {
                return true;
            }
        }
        return false;
    }

Usage Example

Esempio n. 1
0
    /**
     * Test for checkIfMinRecordsExist
     *
     * @return void
     */
    public function testCheckIfMinRecordsExist()
    {
        $old_dbi = $GLOBALS['dbi'];
        $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface')
            ->disableOriginalConstructor()
            ->getMock();
        $dbi->expects($this->any())
            ->method('tryQuery')
            ->will($this->returnValue('res'));
        $dbi->expects($this->any())
            ->method('numRows')
            ->willReturnOnConsecutiveCalls(
                0,
                10,
                200
            );
        $dbi->expects($this->any())
            ->method('fetchResult')
            ->willReturnOnConsecutiveCalls(
                array('`one_pk`'),

                array(), // No Uniques found
                array('`one_ind`', '`sec_ind`'),

                array(), // No Uniques found
                array()  // No Indexed found
            );

        $GLOBALS['dbi'] = $dbi;

        $table = 'PMA_BookMark';
        $db = 'PMA';
        $tableObj = new Table($table, $db);

        // Case 1 : Check if table is non-empty
        $return = $tableObj->checkIfMinRecordsExist();
        $expect = true;
        $this->assertEquals(
            $expect,
            $return
        );

        // Case 2 : Check if table contains at least 100
        $return = $tableObj->checkIfMinRecordsExist(100);
        $expect = false;
        $this->assertEquals(
            $expect,
            $return
        );

        // Case 3 : Check if table contains at least 100
        $return = $tableObj->checkIfMinRecordsExist(100);
        $expect = true;
        $this->assertEquals(
            $expect,
            $return
        );

        $GLOBALS['dbi'] = $old_dbi;
    }