Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chill-patel/f1f2bf71ac8732a2cf47c785ee92c67e to your computer and use it in GitHub Desktop.
Save chill-patel/f1f2bf71ac8732a2cf47c785ee92c67e to your computer and use it in GitHub Desktop.
Compare query hash
let dump = ` CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
CREATE OR REPLACE FUNCTION totalRecords ()
RETURNS integer AS $total$
declare
total integer;
BEGIN
SELECT count(*) into total FROM COMPANY;
RETURN total;
END;
$total$ LANGUAGE plpgsql;
CREATE TABLE User(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL
);
`
const hashFun = (a, type, endQuery) => {
let final = []
let word = '',
tableFound = false
let tableHash = {}
let b = []
a = a.replace(/;/g, '')
for (let i = 0; i <= a.length; i++) {
if (!(a[i] === ' ')) {
word += a[i]
} else {
if (tableFound) {
if (final.length === 0) {
b = a.split(' ')
}
let startIndex = b.findIndex((c) => c === word)
let endIndex = b.findIndex((c) => endQuery === c.replace(new RegExp('\n', 'g'), ''))
final = []
b.map((item, index) => {
if (index <= endIndex) {
final.push(item)
}
})
b.splice(startIndex, endIndex)
word = word.replace(/\n/g, '')
word = word.replace('(', '')
tableHash[word] = final.join(' ')
tableFound = false
}
if (new RegExp(type, 'i').test(word)) {
tableFound = true
tableHash
}
word = ''
}
}
console.log(tableHash)
}
hashFun(dump, 'FUNCTION', 'plpgsql')
hashFun(dump, 'table', ')')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment