Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
TEMPLATE/temp.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
29 lines (25 sloc)
777 Bytes
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
'use strict' | |
const records = [ | |
{ | |
user: 'jdoe', | |
pass: '$2b$10$vPqO/uGlKchrQCqyBIKdb.8hLEJgaC4aAg4fpre5rausycX1XmkWy' | |
} | |
] | |
//const sql = 'SELECT count(id) AS count FROM users WHERE user="jdoe";' | |
const sql = 'SELECT user FROM users WHERE user="jdoe"' | |
const field = sql.match(/(?<=SELECT\s+).*?(?=\s+FROM)/g)[0] | |
const condition = sql.match(/\bWHERE\s+(.*)$/g)[0].replace('WHERE ', '').replace(';', '') | |
const key = condition.split('=')[0] | |
const val = condition.split('=')[1].replace(/"/g, '') | |
console.log(field) | |
console.log(key) | |
console.log(val) | |
let data = [] | |
for(const record of records) if(record[key] == val) data.push(record[key]) | |
let result = {} | |
if(field === 'count(id) AS count') { | |
result.count = data.length | |
} else { | |
result[field] = val | |
} | |
console.log(result) |