Skip to content

Commit

Permalink
Merge pull request #58 from RunOnFlux/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
alihm authored Mar 19, 2024
2 parents bb48b37 + f99143a commit 98a768e
Showing 1 changed file with 71 additions and 71 deletions.
142 changes: 71 additions & 71 deletions lib/sqlAnalyzer.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
/* eslint-disable no-undef */
/* eslint-disable no-unused-vars */
/**
* [This Library helps analyze sql queries and seperate read/write queries for mysql, mssql and postgre]
*/
const log = require('./log');

/**
* [getQueryType analyzes a single query and returns r or w for query type]
* @param {string} sql sql query
* @return {string} [r/w]
*/
function getQueryType(sql) {
const readFlags = ['select', 'show', 'describe', 'set names', 'kill', 'set profiling'];
// eslint-disable-next-line no-restricted-syntax
for (const flag of readFlags) { if (sql.toLowerCase().startsWith(flag)) return 'r'; }

// these codes are to fix ridiculus duplicate wordpress queries
if (sql.includes('`wp_options`') && sql.includes('_transient_')) {
// log.warn('wp transient query');
return 'r';
}
if (sql.toLowerCase().startsWith('set session')) {
return 's';
}

// it's a write query
return 'w';
}
function removeFirstLine(str) {
const lines = str.split('\n');
lines.splice(0, 1);
return lines.join('\n');
}
/**
* [clean up linebraks]
* @param {string} sql sql query
*/
function cleanUP(sql) {
// if (sql.startsWith('SET max_allowed_packet')) {
// return sql.replace('SET max_allowed_packet', 'SET GLOBAL max_allowed_packet');
// }
// if (sql.startsWith('/*') && sql.endsWith('*/') && !sql.startsWith('/*!')) return false;
if (sql.startsWith('--')) {
// eslint-disable-next-line no-param-reassign
while (sql.startsWith('--') || sql.startsWith('\r\n') || sql.startsWith('\n')) sql = removeFirstLine(sql);
}
return sql;
}
/**
* [analyzeSql analyzes the sql and marks them as read/write]
* @param {string} sql sql query
* @param {string} options sql engine:mysql,mssql,postgre
* @return {array} [array of queries]
*/
function analyzeSql(sql, options) {
if (options === 'mysql') {
//
} else if (options === 'mssql') {
//
} else if (options === 'postgre') {
//
} else return [];
const analyzedArray = [];
const tempSql = cleanUP(sql.trim());
if (tempSql) analyzedArray.push([tempSql, getQueryType(tempSql)]);
// log.query(sql, 'yellow');
return analyzedArray;
}

module.exports = analyzeSql;
/* eslint-disable no-undef */
/* eslint-disable no-unused-vars */
/**
* [This Library helps analyze sql queries and seperate read/write queries for mysql, mssql and postgre]
*/
const log = require('./log');

/**
* [getQueryType analyzes a single query and returns r or w for query type]
* @param {string} sql sql query
* @return {string} [r/w]
*/
function getQueryType(sql) {
const readFlags = ['select', 'show', 'describe', 'set names', 'kill', 'set profiling'];
// eslint-disable-next-line no-restricted-syntax
for (const flag of readFlags) { if (sql.toLowerCase().startsWith(flag)) return 'r'; }

// these codes are to fix ridiculus duplicate wordpress queries
if (sql.includes('`wp_options`') && sql.includes('_transient_')) {
// log.warn('wp transient query');
return 'r';
}
if (sql.toLowerCase().startsWith('set session')) {
return 's';
}

// it's a write query
return 'w';
}
function removeFirstLine(str) {
const lines = str.split('\n');
lines.splice(0, 1);
return lines.join('\n');
}
/**
* [clean up linebraks]
* @param {string} sql sql query
*/
function cleanUP(sql) {
// eslint-disable-next-line no-param-reassign
if (sql.toLowerCase().startsWith('/*!40101 set character_set_client = utf8 */')) sql = '/*!40101 SET character_set_client = utf8mb4 */';
// eslint-disable-next-line no-param-reassign
if (sql.toLowerCase().startsWith('/*!40101 set names utf8 */')) sql = '/*!40101 SET NAMES utf8mb4 */';
if (sql.startsWith('--')) {
// eslint-disable-next-line no-param-reassign
while (sql.startsWith('--') || sql.startsWith('\r\n') || sql.startsWith('\n')) sql = removeFirstLine(sql);
}
return sql;
}
/**
* [analyzeSql analyzes the sql and marks them as read/write]
* @param {string} sql sql query
* @param {string} options sql engine:mysql,mssql,postgre
* @return {array} [array of queries]
*/
function analyzeSql(sql, options) {
if (options === 'mysql') {
//
} else if (options === 'mssql') {
//
} else if (options === 'postgre') {
//
} else return [];
const analyzedArray = [];
const tempSql = cleanUP(sql.trim());
if (tempSql) analyzedArray.push([tempSql, getQueryType(tempSql)]);
// log.query(sql, 'yellow');
return analyzedArray;
}

module.exports = analyzeSql;

0 comments on commit 98a768e

Please sign in to comment.