This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
forked from phpmyadmin/phpmyadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…14681) Added functionality to generate SELECT query with JOINs of requested tables. It was mentioned that it's just a bug and it was working in previous versions, but I did not see any code fragments that would make it possible. Please, feel free to suggest any better solution if you don't like that one. Fixes phpmyadmin#14658 Signed-off-by: Aleš Buľko <ales.bulko@student.tuke.sk>
- Loading branch information
1 parent
416bf94
commit 23ce7ce
Showing
7 changed files
with
249 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* vim: set expandtab sw=4 ts=4 sts=4: */ | ||
/** | ||
* @fileoverview function used in QBE for DB | ||
* @name Database Operations | ||
* | ||
* @requires jQuery | ||
* @requires jQueryUI | ||
* @requires js/functions.js | ||
* | ||
*/ | ||
|
||
function getFormatsText () { | ||
return { | ||
'=': ' = \'%s\'', | ||
'>': ' > \'%s\'', | ||
'>=': ' >= \'%s\'', | ||
'<': ' < \'%s\'', | ||
'<=': ' <= \'%s\'', | ||
'!=': ' != \'%s\'', | ||
'LIKE': ' LIKE \'%s\'', | ||
'LIKE \%...\%': ' LIKE \'%%%s%%\'', | ||
'NOT LIKE': ' NOT LIKE \'%s\'', | ||
'BETWEEN': ' BETWEEN \'%s\'', | ||
'NOT BETWEEN': ' NOT BETWEEN \'%s\'', | ||
'IS NULL': ' \'%s\' IS NULL', | ||
'IS NOT NULL': ' \'%s\' IS NOT NULL', | ||
'REGEXP': ' REGEXP \'%s\'', | ||
'REGEXP ^...$': ' REGEXP \'^%s$\'', | ||
'NOT REGEXP': ' NOT REGEXP \'%s\'' | ||
}; | ||
} | ||
|
||
function generateCondition (criteriaDiv, table) { | ||
query = '`' + escapeBacktick(table.val()) + '`.'; | ||
query += '`' + escapeBacktick(table.siblings('.columnNameSelect').first().val()) + '`'; | ||
if (criteriaDiv.find('.criteria_rhs').first().val() === 'text') { | ||
formatsText = getFormatsText(); | ||
query += sprintf(formatsText[criteriaDiv.find('.criteria_op').first().val()], escapeSingleQuote(criteriaDiv.find('.rhs_text_val').first().val())); | ||
} else { | ||
query += ' ' + criteriaDiv.find('.criteria_op').first().val(); | ||
query += ' `' + escapeBacktick(criteriaDiv.find('.tableNameSelect').first().val()) + '`.'; | ||
query += '`' + escapeBacktick(criteriaDiv.find('.columnNameSelect').first().val()) + '`'; | ||
} | ||
return query; | ||
} | ||
|
||
function generateWhereBlock () { | ||
var count = 0; | ||
var query = ''; | ||
$('.tableNameSelect').each(function () { | ||
var criteriaDiv = $(this).siblings('.slide-wrapper').first(); | ||
var useCriteria = $(this).siblings('.criteria_col').first(); | ||
if ($(this).val() !== '' && useCriteria.prop('checked')) { | ||
if (count > 0) { | ||
criteriaDiv.find('input.logical_op').each(function () { | ||
if ($(this).prop('checked')) { | ||
query += ' ' + $(this).val() + ' '; | ||
} | ||
}); | ||
} | ||
query += generateCondition(criteriaDiv, $(this)); | ||
count++; | ||
} | ||
}); | ||
return query; | ||
} | ||
|
||
function generateJoin (newTable, tableAliases, fk) { | ||
query = ''; | ||
query += ' \n\tLEFT JOIN ' + '`' + escapeBacktick(newTable) + '`'; | ||
if (tableAliases[fk.TABLE_NAME][0] !== '') { | ||
query += ' AS `' + escapeBacktick(tableAliases[newTable][0]) + '`'; | ||
query += ' ON `' + escapeBacktick(tableAliases[fk.TABLE_NAME][0]) + '`'; | ||
} else { | ||
query += ' ON `' + escapeBacktick(fk.TABLE_NAME) + '`'; | ||
} | ||
query += '.`' + fk.COLUMN_NAME + '`'; | ||
if (tableAliases[fk.REFERENCED_TABLE_NAME][0] !== '') { | ||
query += ' = `' + escapeBacktick(tableAliases[fk.REFERENCED_TABLE_NAME][0]) + '`'; | ||
} else { | ||
query += ' = `' + escapeBacktick(fk.REFERENCED_TABLE_NAME) + '`'; | ||
} | ||
query += '.`' + fk.REFERENCED_COLUMN_NAME + '`'; | ||
return query; | ||
} | ||
|
||
function existReference (table, fk, usedTables) { | ||
var isReferredBy = fk.TABLE_NAME === table && usedTables.includes(fk.REFERENCED_TABLE_NAME); | ||
var isReferencedBy = fk.REFERENCED_TABLE_NAME === table && usedTables.includes(fk.TABLE_NAME); | ||
return isReferredBy || isReferencedBy; | ||
} | ||
|
||
function tryJoinTable (table, tableAliases, usedTables, foreignKeys) { | ||
for (var i = 0; i < foreignKeys.length; i++) { | ||
var fk = foreignKeys[i]; | ||
if (existReference(table, fk, usedTables)) { | ||
return generateJoin(table, tableAliases, fk); | ||
} | ||
} | ||
return ''; | ||
} | ||
|
||
function appendTable (table, tableAliases, usedTables, foreignKeys) { | ||
var query = tryJoinTable (table, tableAliases, usedTables, foreignKeys); | ||
if (query === '') { | ||
if (usedTables.length > 0) { | ||
query += '\n\t, '; | ||
} | ||
query += '`' + escapeBacktick(table) + '`'; | ||
if (tableAliases[table][0] !== '') { | ||
query += ' AS `' + escapeBacktick(tableAliases[table][0]) + '`'; | ||
} | ||
} | ||
usedTables.push(table); | ||
return query; | ||
} | ||
|
||
function generateFromBlock (tableAliases, foreignKeys) { | ||
var usedTables = []; | ||
query = ''; | ||
for (var table in tableAliases) { | ||
if (tableAliases.hasOwnProperty(table)) { | ||
query += appendTable(table, tableAliases, usedTables, foreignKeys); | ||
} | ||
} | ||
return query; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.