Merge pull request #13341 from mauriciofauth/sql-lib

Refactor the sql functions into static methods
This commit is contained in:
Michal Čihař 2017-06-02 17:25:24 +02:00 committed by GitHub
commit 0c1589920e
17 changed files with 2609 additions and 2630 deletions

View File

@ -5,15 +5,16 @@
*
* @package PhpMyAdmin
*/
use PMA\libraries\SavedSearches;
use PMA\libraries\URL;
use PMA\libraries\Response;
use PMA\libraries\SavedSearches;
use PMA\libraries\Sql;
use PMA\libraries\URL;
/**
* requirements
*/
require_once 'libraries/common.inc.php';
require_once 'libraries/sql.lib.php';
$response = Response::getInstance();
@ -81,7 +82,7 @@ if (isset($_REQUEST['submit_sql']) && ! empty($sql_query)) {
$message_to_display = true;
} else {
$goto = 'db_sql.php';
PMA_executeQueryAndSendQueryResponse(
Sql::executeQueryAndSendQueryResponse(
null, // analyzed_sql_results
false, // is_gotofile
$_REQUEST['db'], // db

View File

@ -5,12 +5,14 @@
*
* @package PhpMyAdmin
*/
use PMA\libraries\Response;
use PMA\libraries\Encoding;
use PMA\libraries\plugins\ImportPlugin;
use PMA\libraries\File;
use PMA\libraries\URL;
use PMA\libraries\Bookmark;
use PMA\libraries\Encoding;
use PMA\libraries\File;
use PMA\libraries\plugins\ImportPlugin;
use PMA\libraries\Response;
use PMA\libraries\Sql;
use PMA\libraries\URL;
/* Enable LOAD DATA LOCAL INFILE for LDI plugin */
if (isset($_POST['format']) && $_POST['format'] == 'ldi') {
@ -21,8 +23,6 @@ if (isset($_POST['format']) && $_POST['format'] == 'ldi') {
* Get the variables sent or posted to this script and a core script
*/
require_once 'libraries/common.inc.php';
require_once 'libraries/sql.lib.php';
//require_once 'libraries/display_import_functions.lib.php';
if (isset($_REQUEST['show_as_php'])) {
$GLOBALS['show_as_php'] = $_REQUEST['show_as_php'];
@ -695,7 +695,7 @@ if ($go_sql) {
extract($analyzed_sql_results);
// Check if User is allowed to issue a 'DROP DATABASE' Statement
if (PMA_hasNoRightsToDropDatabase(
if (Sql::hasNoRightsToDropDatabase(
$analyzed_sql_results, $cfg['AllowUserDropDatabase'], $GLOBALS['is_superuser']
)) {
PMA\libraries\Util::mysqlDie(
@ -711,7 +711,7 @@ if ($go_sql) {
$table = $table_from_sql;
}
$html_output .= PMA_executeQueryAndGetQueryResponse(
$html_output .= Sql::executeQueryAndGetQueryResponse(
$analyzed_sql_results, // analyzed_sql_results
false, // is_gotofile
$db, // db
@ -733,12 +733,12 @@ if ($go_sql) {
);
}
// sql_query_for_bookmark is not included in PMA_executeQueryAndGetQueryResponse
// sql_query_for_bookmark is not included in Sql::executeQueryAndGetQueryResponse
// since only one bookmark has to be added for all the queries submitted through
// the SQL tab
if (! empty($_POST['bkm_label']) && ! empty($import_text)) {
$cfgBookmark = Bookmark::getParams();
PMA_storeTheQueryAsBookmark(
Sql::storeTheQueryAsBookmark(
$db, $cfgBookmark['user'],
$_REQUEST['sql_query'], $_POST['bkm_label'],
isset($_POST['bkm_replace']) ? $_POST['bkm_replace'] : null
@ -753,7 +753,7 @@ if ($go_sql) {
// Save a Bookmark with more than one queries (if Bookmark label given).
if (! empty($_POST['bkm_label']) && ! empty($import_text)) {
$cfgBookmark = Bookmark::getParams();
PMA_storeTheQueryAsBookmark(
Sql::storeTheQueryAsBookmark(
$db, $cfgBookmark['user'],
$_REQUEST['sql_query'], $_POST['bkm_label'],
isset($_POST['bkm_replace']) ? $_POST['bkm_replace'] : null

View File

@ -10,6 +10,7 @@ namespace PMA\libraries;
use PhpMyAdmin\SqlParser\Utils\Query;
use PMA\libraries\plugins\transformations\Text_Plain_Link;
use PMA\libraries\Sanitize;
use PMA\libraries\Sql;
use PMA\libraries\Transformations;
use PMA\libraries\URL;
@ -1428,7 +1429,7 @@ class DisplayResults
. URL::getHiddenInputs(
$this->__get('db'), $this->__get('table')
)
// to avoid calling PMA_handleSortOrder() later
// to avoid calling Sql::handleSortOrder() later
. URL::getHiddenFields(array('sort_by_key' => '1'))
. __('Sort by key')
. ': <select name="sql_query" class="autosubmit">' . "\n";
@ -4255,9 +4256,6 @@ class DisplayResults
$showtable = $this->__get('showtable');
$printview = $this->__get('printview');
// why was this called here? (already called from sql.php)
//$this->setConfigParamsForDisplayTable();
/**
* @todo move this to a central place
* @todo for other future table types
@ -4265,9 +4263,7 @@ class DisplayResults
$is_innodb = (isset($showtable['Type'])
&& $showtable['Type'] == self::TABLE_TYPE_INNO_DB);
if ($is_innodb
&& PMA_isJustBrowsing($analyzed_sql_results, true)
) {
if ($is_innodb && Sql::isJustBrowsing($analyzed_sql_results, true)) {
// "j u s t b r o w s i n g"
$pre_count = '~';
$after_count = Util::showHint(

2267
libraries/Sql.php Normal file

File diff suppressed because it is too large Load Diff

View File

@ -8,12 +8,11 @@
namespace PMA\libraries\controllers\table;
use PMA\libraries\Util;
use PMA\libraries\Template;
use PMA\libraries\controllers\TableController;
use PMA\libraries\DatabaseInterface;
require_once 'libraries/sql.lib.php';
use PMA\libraries\Sql;
use PMA\libraries\Template;
use PMA\libraries\Util;
/**
* Class TableSearchController
@ -448,7 +447,7 @@ class TableSearchController extends TableController
*/
$db = $this->db;
PMA_executeQueryAndSendQueryResponse(
Sql::executeQueryAndSendQueryResponse(
null, // analyzed_sql_results
false, // is_gotofile
$this->db, // db

View File

@ -9,19 +9,20 @@
namespace PMA\libraries\controllers\table;
use PMA\libraries\config\PageSettings;
use PMA\libraries\Index;
use PMA\libraries\Message;
use PMA\libraries\Template;
use PMA\libraries\Util;
use PMA\Util as Util_lib;
use PhpMyAdmin\SqlParser;
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
use PhpMyAdmin\SqlParser\Utils\Table as SqlTable;
use PMA\libraries\Table;
use PMA\libraries\Transformations;
use PMA\libraries\config\PageSettings;
use PMA\libraries\controllers\TableController;
use PMA\libraries\Index;
use PMA\libraries\Message;
use PMA\libraries\Sql;
use PMA\libraries\Table;
use PMA\libraries\Template;
use PMA\libraries\Transformations;
use PMA\libraries\URL;
use PMA\libraries\Util;
use PMA\Util as Util_lib;
require_once 'libraries/util.lib.php';
require_once 'libraries/config/messages.inc.php';
@ -118,7 +119,6 @@ class TableStructureController extends TableController
*/
include_once 'libraries/check_user_privileges.lib.php';
include_once 'libraries/index.lib.php';
include_once 'libraries/sql.lib.php';
$this->response->getHeader()->getScripts()->addFiles(
array(
@ -794,10 +794,8 @@ class TableStructureController extends TableController
// @todo: possibly refactor
extract($analyzed_sql_results);
include_once 'libraries/sql.lib.php';
$this->response->addHTML(
PMA_executeQueryAndGetQueryResponse(
Sql::executeQueryAndGetQueryResponse(
isset($analyzed_sql_results) ? $analyzed_sql_results : '',
false, // is_gotofile
$this->db, // db

View File

@ -12,8 +12,6 @@ use PMA\libraries\Util;
use \TCPDF;
use PMA\libraries\Sanitize;
require_once 'libraries/sql.lib.php';
/**
* Handles visualization of GIS data
*

View File

@ -5,14 +5,15 @@
*
* @package PhpMyAdmin
*/
use PMA\libraries\Message;
use PMA\libraries\Response;
use PMA\libraries\Sql;
if (! defined('PHPMYADMIN')) {
exit;
}
require_once 'libraries/sql.lib.php';
require_once 'libraries/mult_submits.lib.php';
$request_params = array(
@ -266,7 +267,7 @@ if (!empty($submit_mult) && !empty($what)) {
}
if ($execute_query_later) {
PMA_executeQueryAndSendQueryResponse(
Sql::executeQueryAndSendQueryResponse(
null, // analyzed_sql_results
false, // is_gotofile
$db, // db

View File

@ -7,6 +7,8 @@
*
* @package PhpMyAdmin
*/
use PMA\libraries\Sql;
use PMA\libraries\Table;
use PMA\libraries\Transformations;
use PMA\libraries\URL;
@ -306,7 +308,7 @@ function PMA_buildOrExecuteQueryForMulti(
} // end for
if ($deletes && ! empty($_REQUEST['pos'])) {
$_REQUEST['pos'] = PMA_calculatePosForLastPage(
$_REQUEST['pos'] = Sql::calculatePosForLastPage(
$db, $table, isset($_REQUEST['pos']) ? $_REQUEST['pos'] : null
);
}

File diff suppressed because it is too large Load Diff

25
sql.php
View File

@ -9,21 +9,20 @@
*/
use PMA\libraries\config\PageSettings;
use PMA\libraries\Response;
use PMA\libraries\Util;
use PMA\libraries\Sql;
use PMA\libraries\URL;
use PMA\libraries\Util;
/**
* Gets some core libraries
*/
require_once 'libraries/common.inc.php';
require_once 'libraries/check_user_privileges.lib.php';
require_once 'libraries/sql.lib.php';
require_once 'libraries/config/user_preferences.forms.php';
require_once 'libraries/config/page_settings.forms.php';
PageSettings::showGroup('Browse');
$response = Response::getInstance();
$header = $response->getHeader();
$scripts = $header->getScripts();
@ -41,7 +40,6 @@ if (isset($ajax_reload) && $ajax_reload['reload'] === true) {
$response->addJSON('ajax_reload', $ajax_reload);
}
/**
* Defines the url to return to in case of error in a sql statement
*/
@ -80,25 +78,24 @@ if (isset($_POST['bkm_fields']['bkm_database'])) {
$db = $_POST['bkm_fields']['bkm_database'];
}
// During grid edit, if we have a relational field, show the dropdown for it.
if (isset($_REQUEST['get_relational_values'])
&& $_REQUEST['get_relational_values'] == true
) {
PMA_getRelationalValues($db, $table);
Sql::getRelationalValues($db, $table);
// script has exited at this point
}
// Just like above, find possible values for enum fields during grid edit.
if (isset($_REQUEST['get_enum_values']) && $_REQUEST['get_enum_values'] == true) {
PMA_getEnumOrSetValues($db, $table, "enum");
Sql::getEnumOrSetValues($db, $table, "enum");
// script has exited at this point
}
// Find possible values for set fields during grid edit.
if (isset($_REQUEST['get_set_values']) && $_REQUEST['get_set_values'] == true) {
PMA_getEnumOrSetValues($db, $table, "set");
Sql::getEnumOrSetValues($db, $table, "set");
// script has exited at this point
}
@ -116,14 +113,14 @@ if (isset($_REQUEST['get_default_fk_check_value'])
* Check ajax request to set the column order and visibility
*/
if (isset($_REQUEST['set_col_prefs']) && $_REQUEST['set_col_prefs'] == true) {
PMA_setColumnOrderOrVisibility($table, $db);
Sql::setColumnOrderOrVisibility($table, $db);
// script has exited at this point
}
// Default to browse if no query set and we have table
// (needed for browsing from DefaultTabTable)
if (empty($sql_query) && strlen($table) > 0 && strlen($db) > 0) {
$sql_query = PMA_getDefaultSqlQueryForBrowse($db, $table);
$sql_query = Sql::getDefaultSqlQueryForBrowse($db, $table);
// set $goto to what will be displayed if query returns 0 rows
$goto = '';
@ -156,7 +153,7 @@ if ($table != $table_from_sql && !empty($table_from_sql)) {
* but since a malicious user may pass this variable by url/form, we don't take
* into account this case.
*/
if (PMA_hasNoRightsToDropDatabase(
if (Sql::hasNoRightsToDropDatabase(
$analyzed_sql_results, $cfg['AllowUserDropDatabase'], $is_superuser
)) {
Util::mysqlDie(
@ -171,7 +168,7 @@ if (PMA_hasNoRightsToDropDatabase(
* Need to find the real end of rows?
*/
if (isset($find_real_end) && $find_real_end) {
$unlim_num_rows = PMA_findRealEndOfRows($db, $table);
$unlim_num_rows = Sql::findRealEndOfRows($db, $table);
}
@ -179,7 +176,7 @@ if (isset($find_real_end) && $find_real_end) {
* Bookmark add
*/
if (isset($_POST['store_bkm'])) {
PMA_addBookmark($goto);
Sql::addBookmark($goto);
// script has exited at this point
} // end if
@ -198,7 +195,7 @@ if ($goto == 'sql.php') {
);
} // end if
PMA_executeQueryAndSendQueryResponse(
Sql::executeQueryAndSendQueryResponse(
$analyzed_sql_results, // analyzed_sql_results
$is_gotofile, // is_gotofile
$db, // db

View File

@ -5,14 +5,15 @@
*
* @package PhpMyAdmin
*/
use PMA\libraries\URL;
use PMA\libraries\Response;
use PMA\libraries\Sql;
use PMA\libraries\URL;
/**
*
*/
require_once 'libraries/common.inc.php';
require_once 'libraries/sql.lib.php';
if (isset($_REQUEST['submit_mult'])) {
$submit_mult = $_REQUEST['submit_mult'];
@ -151,7 +152,7 @@ if (!empty($submit_mult)) {
}
$active_page = 'sql.php';
PMA_executeQueryAndSendQueryResponse(
Sql::executeQueryAndSendQueryResponse(
null, // analyzed_sql_results
false, // is_gotofile
$db, // db

View File

@ -4,7 +4,7 @@
* Handles table search tab
*
* display table search form, create SQL query from form data
* and call PMA_executeQueryAndSendQueryResponse() to execute it
* and call Sql::executeQueryAndSendQueryResponse() to execute it
*
* @package PhpMyAdmin
*/

286
test/classes/SqlTest.php Normal file
View File

@ -0,0 +1,286 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Tests for PMA\libraries\Sql
*
* @package PhpMyAdmin-test
*/
use PMA\libraries\Sql;
/**
* Tests for PMA\libraries\Sql
*
* @package PhpMyAdmin-test
*/
class SqlTest extends PHPUnit_Framework_TestCase
{
/**
* Test Sql::getSqlWithLimitClause
*
* @return void
*/
public function testGetSqlWithLimitClause()
{
// Test environment.
$GLOBALS['_SESSION']['tmpval']['pos'] = 1;
$GLOBALS['_SESSION']['tmpval']['max_rows'] = 2;
$GLOBALS['db'] = 'db';
$analyzed_sql_results = Sql::parseAndAnalyze(
'SELECT * FROM test LIMIT 0, 10'
);
$this->assertEquals(
'SELECT * FROM test LIMIT 1, 2 ',
Sql::getSqlWithLimitClause($analyzed_sql_results)
);
}
/**
* Test Sql::isRememberSortingOrder
*
* @return void
*/
public function testIsRememberSortingOrder()
{
// Test environment.
$GLOBALS['cfg']['RememberSorting'] = true;
$GLOBALS['db'] = 'db';
$this->assertTrue(
Sql::isRememberSortingOrder(
Sql::parseAndAnalyze('SELECT * FROM tbl')
)
);
$this->assertFalse(
Sql::isRememberSortingOrder(
Sql::parseAndAnalyze('SELECT col FROM tbl')
)
);
$this->assertFalse(
Sql::isRememberSortingOrder(
Sql::parseAndAnalyze('SELECT 1')
)
);
$this->assertFalse(
Sql::isRememberSortingOrder(
Sql::parseAndAnalyze('SELECT col1, col2 FROM tbl')
)
);
$this->assertFalse(
Sql::isRememberSortingOrder(
Sql::parseAndAnalyze('SELECT COUNT(*) from tbl')
)
);
}
/**
* Test Sql::isAppendLimitClause
*
* @return void
*/
public function testIsAppendLimitClause()
{
// Test environment.
$GLOBALS['_SESSION']['tmpval']['max_rows'] = 10;
$GLOBALS['db'] = 'db';
$this->assertTrue(
Sql::isAppendLimitClause(
Sql::parseAndAnalyze('SELECT * FROM tbl')
)
);
$this->assertFalse(
Sql::isAppendLimitClause(
Sql::parseAndAnalyze('SELECT * from tbl LIMIT 0, 10')
)
);
}
/**
* Test Sql::isJustBrowsing
*
* @return void
*/
public function testIsJustBrowsing()
{
// Test environment.
$GLOBALS['_SESSION']['tmpval']['max_rows'] = 10;
$GLOBALS['db'] = 'db';
$this->assertTrue(
Sql::isJustBrowsing(
Sql::parseAndAnalyze('SELECT * FROM db.tbl'),
null
)
);
$this->assertTrue(
Sql::isJustBrowsing(
Sql::parseAndAnalyze('SELECT * FROM tbl WHERE 1'),
null
)
);
$this->assertFalse(
Sql::isJustBrowsing(
Sql::parseAndAnalyze('SELECT * from tbl1, tbl2 LIMIT 0, 10'),
null
)
);
}
/**
* Test Sql::isDeleteTransformationInfo
*
* @return void
*/
public function testIsDeleteTransformationInfo()
{
$this->assertTrue(
Sql::isDeleteTransformationInfo(
Sql::parseAndAnalyze('ALTER TABLE tbl DROP COLUMN col')
)
);
$this->assertTrue(
Sql::isDeleteTransformationInfo(
Sql::parseAndAnalyze('DROP TABLE tbl')
)
);
$this->assertFalse(
Sql::isDeleteTransformationInfo(
Sql::parseAndAnalyze('SELECT * from tbl')
)
);
}
/**
* Test Sql::hasNoRightsToDropDatabase
*
* @return void
*/
public function testHasNoRightsToDropDatabase()
{
$this->assertEquals(
true,
Sql::hasNoRightsToDropDatabase(
Sql::parseAndAnalyze('DROP DATABASE db'),
false,
false
)
);
$this->assertEquals(
false,
Sql::hasNoRightsToDropDatabase(
Sql::parseAndAnalyze('DROP TABLE tbl'),
false,
false
)
);
$this->assertEquals(
false,
Sql::hasNoRightsToDropDatabase(
Sql::parseAndAnalyze('SELECT * from tbl'),
false,
false
)
);
}
/**
* Should return false if all columns are not from the same table
*
* @return void
*/
public function testWithMultipleTables()
{
$col1 = new stdClass;
$col1->table = 'table1';
$col2 = new stdClass;
$col2->table = 'table1';
$col3 = new stdClass;
$col3->table = 'table3';
$fields_meta = array($col1, $col2, $col3);
$this->assertFalse(Sql::resultSetHasJustOneTable($fields_meta));
// should not matter on where the odd column occurs
$fields_meta = array($col2, $col3, $col1);
$this->assertFalse(Sql::resultSetHasJustOneTable($fields_meta));
$fields_meta = array($col3, $col1, $col2);
$this->assertFalse(Sql::resultSetHasJustOneTable($fields_meta));
}
/**
* Should return true if all the columns are from the same table
*
* @return void
*/
public function testWithSameTable()
{
$col1 = new stdClass;
$col1->table = 'table1';
$col2 = new stdClass;
$col2->table = 'table1';
$col3 = new stdClass;
$col3->table = 'table1';
$fields_meta = array($col1, $col2, $col3);
$this->assertTrue(Sql::resultSetHasJustOneTable($fields_meta));
}
/**
* Should return true even if function columns (table is '') occur when others
* are from the same table.
*
* @return void
*/
public function testWithFunctionColumns()
{
$col1 = new stdClass;
$col1->table = 'table1';
$col2 = new stdClass;
$col2->table = '';
$col3 = new stdClass;
$col3->table = 'table1';
$fields_meta = array($col1, $col2, $col3);
$this->assertTrue(Sql::resultSetHasJustOneTable($fields_meta));
// should not matter on where the function column occurs
$fields_meta = array($col2, $col3, $col1);
$this->assertTrue(Sql::resultSetHasJustOneTable($fields_meta));
$fields_meta = array($col3, $col1, $col2);
$this->assertTrue(Sql::resultSetHasJustOneTable($fields_meta));
}
/**
* We can not say all the columns are from the same table if all the columns
* are funtion columns (table is '')
*
* @return void
*/
public function testWithOnlyFunctionColumns()
{
$col1 = new stdClass;
$col1->table = '';
$col2 = new stdClass;
$col2->table = '';
$col3 = new stdClass;
$col3->table = '';
$fields_meta = array($col1, $col2, $col3);
$this->assertFalse(Sql::resultSetHasJustOneTable($fields_meta));
}
}

View File

@ -12,15 +12,10 @@
use PMA\libraries\Theme;
use PMA\libraries\URL;
require_once 'libraries/mult_submits.lib.php';
require_once 'libraries/database_interface.inc.php';
require_once 'libraries/relation_cleanup.lib.php';
require_once 'libraries/relation.lib.php';
require_once 'libraries/sql.lib.php';
/**
* class PMA_MultSubmits_Test

View File

@ -1,109 +0,0 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Tests for PMA_resultSetHasJustOneTable method
*
* @package PhpMyAdmin-test
*/
/*
* Include to test.
*/
require_once 'libraries/sql.lib.php';
/**
* Tests for PMA_resultSetHasJustOneTable method
*
* @package PhpMyAdmin-test
*/
class PMA_ResultSetHasJustOneTableTest extends PHPUnit_Framework_TestCase
{
/**
* Should return false if all columns are not from the same table
*
* @return void
*/
public function testWithMultipleTables()
{
$col1 = new stdClass;
$col1->table = 'table1';
$col2 = new stdClass;
$col2->table = 'table1';
$col3 = new stdClass;
$col3->table = 'table3';
$fields_meta = array($col1, $col2, $col3);
$this->assertFalse(PMA_resultSetHasJustOneTable($fields_meta));
// should not matter on where the odd column occurs
$fields_meta = array($col2, $col3, $col1);
$this->assertFalse(PMA_resultSetHasJustOneTable($fields_meta));
$fields_meta = array($col3, $col1, $col2);
$this->assertFalse(PMA_resultSetHasJustOneTable($fields_meta));
}
/**
* Should return true if all the columns are from the same table
*
* @return void
*/
public function testWithSameTable()
{
$col1 = new stdClass;
$col1->table = 'table1';
$col2 = new stdClass;
$col2->table = 'table1';
$col3 = new stdClass;
$col3->table = 'table1';
$fields_meta = array($col1, $col2, $col3);
$this->assertTrue(PMA_resultSetHasJustOneTable($fields_meta));
}
/**
* Should return true even if function columns (table is '') occur when others
* are from the same table.
*
* @return void
*/
public function testWithFunctionColumns()
{
$col1 = new stdClass;
$col1->table = 'table1';
$col2 = new stdClass;
$col2->table = '';
$col3 = new stdClass;
$col3->table = 'table1';
$fields_meta = array($col1, $col2, $col3);
$this->assertTrue(PMA_resultSetHasJustOneTable($fields_meta));
// should not matter on where the function column occurs
$fields_meta = array($col2, $col3, $col1);
$this->assertTrue(PMA_resultSetHasJustOneTable($fields_meta));
$fields_meta = array($col3, $col1, $col2);
$this->assertTrue(PMA_resultSetHasJustOneTable($fields_meta));
}
/**
* We can not say all the columns are from the same table if all the columns
* are funtion columns (table is '')
*
* @return void
*/
public function testWithOnlyFunctionColumns()
{
$col1 = new stdClass;
$col1->table = '';
$col2 = new stdClass;
$col2->table = '';
$col3 = new stdClass;
$col3->table = '';
$fields_meta = array($col1, $col2, $col3);
$this->assertFalse(PMA_resultSetHasJustOneTable($fields_meta));
}
}

View File

@ -1,202 +0,0 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Tests for libraries/sql.lib.php
*
* @package PhpMyAdmin-test
*/
/*
* Include to test.
*/
require_once 'libraries/sql.lib.php';
/**
* Tests for libraries/sql.lib.php
*
* @package PhpMyAdmin-test
*/
class PMA_SqlTest extends PHPUnit_Framework_TestCase
{
/**
* Test PMA_getSqlWithLimitClause
*
* @return void
*/
public function testGetSqlWithLimitClause()
{
// Test environment.
$GLOBALS['_SESSION']['tmpval']['pos'] = 1;
$GLOBALS['_SESSION']['tmpval']['max_rows'] = 2;
$GLOBALS['db'] = 'db';
$analyzed_sql_results = PMA_parseAndAnalyze(
'SELECT * FROM test LIMIT 0, 10'
);
$this->assertEquals(
'SELECT * FROM test LIMIT 1, 2 ',
PMA_getSqlWithLimitClause($analyzed_sql_results)
);
}
/**
* Test PMA_isRememberSortingOrder
*
* @return void
*/
public function testIsRememberSortingOrder()
{
// Test environment.
$GLOBALS['cfg']['RememberSorting'] = true;
$GLOBALS['db'] = 'db';
$this->assertTrue(
PMA_isRememberSortingOrder(
PMA_parseAndAnalyze('SELECT * FROM tbl')
)
);
$this->assertFalse(
PMA_isRememberSortingOrder(
PMA_parseAndAnalyze('SELECT col FROM tbl')
)
);
$this->assertFalse(
PMA_isRememberSortingOrder(
PMA_parseAndAnalyze('SELECT 1')
)
);
$this->assertFalse(
PMA_isRememberSortingOrder(
PMA_parseAndAnalyze('SELECT col1, col2 FROM tbl')
)
);
$this->assertFalse(
PMA_isRememberSortingOrder(
PMA_parseAndAnalyze('SELECT COUNT(*) from tbl')
)
);
}
/**
* Test PMA_isAppendLimitClause
*
* @return void
*/
public function testIsAppendLimitClause()
{
// Test environment.
$GLOBALS['_SESSION']['tmpval']['max_rows'] = 10;
$GLOBALS['db'] = 'db';
$this->assertTrue(
PMA_isAppendLimitClause(
PMA_parseAndAnalyze('SELECT * FROM tbl')
)
);
$this->assertFalse(
PMA_isAppendLimitClause(
PMA_parseAndAnalyze('SELECT * from tbl LIMIT 0, 10')
)
);
}
/**
* Test PMA_isJustBrowsing
*
* @return void
*/
public function testIsJustBrowsing()
{
// Test environment.
$GLOBALS['_SESSION']['tmpval']['max_rows'] = 10;
$GLOBALS['db'] = 'db';
$this->assertTrue(
PMA_isJustBrowsing(
PMA_parseAndAnalyze('SELECT * FROM db.tbl'),
null
)
);
$this->assertTrue(
PMA_isJustBrowsing(
PMA_parseAndAnalyze('SELECT * FROM tbl WHERE 1'),
null
)
);
$this->assertFalse(
PMA_isJustBrowsing(
PMA_parseAndAnalyze('SELECT * from tbl1, tbl2 LIMIT 0, 10'),
null
)
);
}
/**
* Test PMA_isDeleteTransformationInfo
*
* @return void
*/
public function testIsDeleteTransformationInfo()
{
$this->assertTrue(
PMA_isDeleteTransformationInfo(
PMA_parseAndAnalyze('ALTER TABLE tbl DROP COLUMN col')
)
);
$this->assertTrue(
PMA_isDeleteTransformationInfo(
PMA_parseAndAnalyze('DROP TABLE tbl')
)
);
$this->assertFalse(
PMA_isDeleteTransformationInfo(
PMA_parseAndAnalyze('SELECT * from tbl')
)
);
}
/**
* Test PMA_hasNoRightsToDropDatabase
*
* @return void
*/
public function testHasNoRightsToDropDatabase()
{
$this->assertEquals(
true,
PMA_hasNoRightsToDropDatabase(
PMA_parseAndAnalyze('DROP DATABASE db'),
false,
false
)
);
$this->assertEquals(
false,
PMA_hasNoRightsToDropDatabase(
PMA_parseAndAnalyze('DROP TABLE tbl'),
false,
false
)
);
$this->assertEquals(
false,
PMA_hasNoRightsToDropDatabase(
PMA_parseAndAnalyze('SELECT * from tbl'),
false,
false
)
);
}
}