mdb_usage_testcase.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,121 行 · 第 1/4 页

PHP
1,121
字号
        $this->db->freePreparedQuery($prepared_query);        $result = $this->db->query('SELECT * FROM users');        if (MDB::isError($result)) {            $this->assertTrue(false, 'Error selecting from users'.$result->getMessage());        }        $this->assertTrue($this->db->endOfResult($result), 'Transaction end with implicit commit when re-enabling auto-commit did not make permanent the rows that were deleted');        $this->db->freeResult($result);    }    /**     * Testing LOB storage     */    function testLobStorage() {        if (!$this->supported('LOBs')) {            return;        }        //$prepared_query = $this->db->prepareQuery('INSERT INTO files (document,picture) VALUES (?,?)');        $prepared_query = $this->db->prepareQuery('INSERT INTO files (ID, document,picture) VALUES (1,?,?)', array('clob', 'blob'));        $character_lob = array(                              'Database' => $this->db,                              'Error' => '',                              'Data' => ''                              );        for ($code = 32; $code <= 127; $code++) {            $character_lob['Data'] .= chr($code);        }        $binary_lob = array(                            'Database' => $this->db,                            'Error' => '',                            'Data' => ''                            );        for ($code = 0; $code <= 255; $code++) {            $binary_lob['Data'] .= chr($code);        }        $clob = $this->db->createLob($character_lob);        $this->assertTrue(!MDB::isError($clob), 'Error creating character LOB: '.$character_lob['Error']);        $blob = $this->db->createLob($binary_lob);        $this->assertTrue(!MDB::isError($blob), 'Error creating binary LOB: '.$binary_lob['Error']);        $this->db->setParamClob($prepared_query, 1, $clob, 'document');        $this->db->setParamBlob($prepared_query, 2, $blob, 'picture');        $result = $this->db->executeQuery($prepared_query);        if($is_error = MDB::isError($result)) {            $msg = $result->getUserInfo();        } else {            $msg = '';        }        $this->assertTrue(!$is_error, 'Error executing prepared query: '.$msg);        $this->db->destroyLob($blob);        $this->db->destroyLob($clob);        $this->db->freePreparedQuery($prepared_query);        $result = $this->db->query('SELECT document, picture FROM files', array('clob', 'blob'));        //$result = $this->db->query('SELECT document, picture FROM files');        if (MDB::isError($result)) {            $this->assertTrue(false, 'Error selecting from files'.$result->getMessage());        }        $this->assertTrue(!$this->db->endOfResult($result), 'The query result seem to have reached the end of result too soon.');        $clob = $this->db->fetchClob($result, 0, 'document');        if (!MDB::isError($clob)) {            for ($value = ''; !$this->db->endOfLob($clob);) {                $this->assertTrue(($this->db->readLob($clob, $data, 8192) >= 0), 'Could not read CLOB');                $value .= $data;            }            $this->db->destroyLob($clob);            $this->assertEquals($value, $character_lob['Data'], 'Retrieved character LOB value ("' . $value . '") is different from what was stored ("' . $character_lob['Data'] . '")');        } else {            $this->assertTrue(false, 'Error retrieving CLOB result');        }        $blob = $this->db->fetchBlob($result, 0, 'picture');        if (!MDB::isError($blob)) {            for ($value = ''; !$this->db->endOfLob($clob);) {                $this->assertTrue(($this->db->readLob($blob, $data, 8192) >= 0), 'Could not read BLOB');                $value .= $data;            }            $this->db->destroyLob($blob);            $this->assertEquals($value, $binary_lob['Data'], 'Retrieved binary LOB value ("'.$value.'") is different from what was stored ("'.$binary_lob['Data'].'")');        } else {            $this->assertTrue(false, 'Error retrieving CLOB result');        }        $this->db->freeResult($result);    }    /**     * Test for lob storage from and to files     */    function testLobFiles() {        if (!$this->supported('LOBs')) {            return;        }        $prepared_query = $this->db->prepareQuery('INSERT INTO files (ID, document,picture) VALUES (1,?,?)', array('clob', 'blob'));        //$prepared_query = $this->db->prepareQuery('INSERT INTO files (document,picture) VALUES (?,?)');        $character_data_file = 'character_data';        if (($file = fopen($character_data_file, 'w'))) {            for ($character_data = '', $code = 32; $code <= 127; $code++) {                $character_data .= chr($code);            }            $character_lob = array(                                   'Type' => 'inputfile',                                   'Database' => $this->db,                                   'Error' => '',                                   'FileName' => $character_data_file                                   );            $this->assertTrue((fwrite($file, $character_data, strlen($character_data)) == strlen($character_data)), 'Error creating clob file to read from');            fclose($file);        }        $binary_data_file = 'binary_data';        if (($file = fopen($binary_data_file, 'wb'))) {            for($binary_data = '', $code = 0; $code <= 255; $code++) {                    $binary_data .= chr($code);            }            $binary_lob = array(                                'Type' => 'inputfile',                                'Database' => $this->db,                                'Error' => '',                                'FileName' => $binary_data_file                                );            $this->assertTrue((fwrite($file, $binary_data, strlen($binary_data)) == strlen($binary_data)), 'Error creating blob file to read from');            fclose($file);        }        $clob = $this->db->createLob($character_lob);        $this->assertTrue(!MDB::isError($clob), 'Error creating clob');        $blob = $this->db->createLob($binary_lob);        $this->assertTrue(!MDB::isError($blob), 'Error creating blob');        $this->db->setParamCLOB($prepared_query, 1, $clob, 'document');        $this->db->setParamBLOB($prepared_query, 2, $blob, 'picture');        $result = $this->db->executeQuery($prepared_query);        $this->assertTrue(!MDB::isError($result), 'Error executing prepared query - inserting LOB from files');        $this->db->destroyLOB($blob);        $this->db->destroyLOB($clob);        $this->db->freePreparedQuery($prepared_query);        $result = $this->db->query('SELECT document, picture FROM files');        if (MDB::isError($result)) {            $this->assertTrue(false, 'Error selecting from files'.$result->getMessage());        }        $this->assertTrue(!$this->db->endOfResult($result), 'The query result seem to have reached the end of result too soon.');        $character_lob = array(                             'Type' => 'outputfile',                             'Database' => $this->db,                             'Result' => $result,                             'Row' => 0,                             'Field' => 'document',                             'Binary' => 0,                             'Error' => '',                             'FileName' => $character_data_file                             );        $clob = $this->db->createLOB($character_lob);        if (!MDB::isError($clob)) {            $this->assertTrue(($this->db->readLOB($clob, $data, 0) >= 0), 'Error reading CLOB ');            $this->db->destroyLOB($clob);            $this->assertTrue(($file = fopen($character_data_file, 'r')), "Error opening character data file: $character_data_file");            $this->assertEquals(getType($value = fread($file, filesize($character_data_file))), 'string', "Could not read from character LOB file: $character_data_file");            fclose($file);            $this->assertEquals($value, $character_data, "retrieved character LOB value (\"".$value."\") is different from what was stored (\"".$character_data."\")");        } else {            $this->assertTrue(false, 'Error creating character LOB in a file');        }        $binary_lob = array(                            'Type' => 'outputfile',                            'Database' => $this->db,                            'Result' => $result,                            'Row' => 0,                            'Field' => 'picture',                            'Binary' => 1,                            'Error' => '',                            'FileName' => $binary_data_file                            );        $blob = $this->db->createLOB($binary_lob);        if (!MDB::isError($blob)) {            $this->assertTrue(($this->db->readLOB($blob, $data, 0) >= 0), 'Error reading BLOB ');            $this->db->destroyLOB($blob);            $this->assertTrue(($file = fopen($binary_data_file, 'rb')), "Error opening binary data file: $binary_data_file");            $this->assertEquals(getType($value = fread($file, filesize($binary_data_file))), 'string', "Could not read from binary LOB file: $binary_data_file");            fclose($file);            $this->assertEquals($value, $binary_data, "retrieved binary LOB value (\"".$value."\") is different from what was stored (\"".$binary_data."\")");        } else {            $this->assertTrue(false, 'Error creating binary LOB in a file');        }        $this->db->freeResult($result);    }    /**     * Test handling of lob nulls     */    function testLobNulls() {        if (!$this->supported('LOBs')) {            return;        }        $prepared_query = $this->db->prepareQuery('INSERT INTO files (ID, document,picture) VALUES (1,?,?)', array('clob', 'blob'));        //$prepared_query = $this->db->prepareQuery('INSERT INTO files (document,picture) VALUES (?,?)');        $this->db->setParamNull($prepared_query, 1, 'clob');        $this->db->setParamNull($prepared_query, 2, 'blob');        $result = $this->db->executeQuery($prepared_query);        $this->assertTrue(!MDB::isError($result), 'Error executing prepared query - inserting NULL lobs');        $this->db->freePreparedQuery($prepared_query);        $result = $this->db->query('SELECT document, picture FROM files', array('clob', 'blob'));        if (MDB::isError($result)) {            $this->assertTrue(false, 'Error selecting from files'.$result->getMessage());        }        $this->assertTrue(!$this->db->endOfResult($result), 'The query result seem to have reached the end of result too soon.');        $this->assertTrue($this->db->resultIsNull($result, 0, 'document'), 'A query result large object column is not NULL unlike what was expected (document)');        $this->assertTrue($this->db->resultIsNull($result, 0, 'picture'), 'A query result large object column is not NULL unlike what was expected (picture)');        $this->db->freeResult($result);    }    /**     * test tableInfo()     */    function testTableInfo()    {        if (!$this->methodExists('tableInfo')) {            return;        }        $table_info = $this->db->tableInfo('users');        if (MDB::isError($table_info)) {            $this->assertTrue(false, 'Error in tableInfo(): '.$table_info->getMessage());        } else {            $this->assertEquals(count($this->fields), count($table_info), 'The number of fields retrieved ('.count($table_info).') is different from the expected one ('.count($this->fields).')');            foreach ($table_info as $field_info) {                $this->assertEquals($field_info['table'], 'users', "the table name is not correct (expected: 'users'; actual: $field_info[table])");                if (!in_array(strtolower($field_info['name']), $this->fields)) {                    $this->assertTrue(false, 'Field names do not match ('.$field_info['name'].' not recognized');                }                //add check on types...            }        }    }}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?