mysql-rest
Version:
One command to generate REST APIs for any MySql database, support multi databases
40 lines (33 loc) • 1.4 kB
JavaScript
function convertToDameng(query, params) {
// Convert the query to uppercase
let upperQuery = query.toUpperCase();
// Replace backticks (`) with double quotes (")
upperQuery = upperQuery.replace(/`/g, '"');
// Collect all `??` or `?` placeholders into a new array
const allPlaceholders = [];
const allPlaceholderRegex = /\?\??/g;
while ((match = allPlaceholderRegex.exec(upperQuery)) !== null) {
allPlaceholders.push(match[0]);
}
for (let i = 0; i < allPlaceholders.length; i++) {
if (allPlaceholders[i] == "??") {
if (typeof params[i] === 'string') {
params[i] = params[i].toUpperCase();
}
if (Array.isArray(params[i])) {
params[i] = params[i].map(item => typeof item === 'string' ? item.toUpperCase() : item);
}
}
}
// Match MySQL LIMIT clause (e.g., LIMIT ?,?)
const mysqlLimitRegex = /LIMIT\s+\?,\s*\?/i;
// Replace MySQL LIMIT with Dameng's LIMIT ... OFFSET syntax
if (upperQuery.includes('LIMIT')) {
const lastIndex = params.length - 1;
[], params[lastIndex]] = [params[lastIndex], params[lastIndex - 1]];
}
upperQuery = upperQuery.replace(mysqlLimitRegex, "LIMIT ? OFFSET ?");
return { query: upperQuery, params };
}
// Export the function
module.exports = { convertToDameng };