-
Notifications
You must be signed in to change notification settings - Fork 0
/
findTableDataContainingWords.html
81 lines (69 loc) · 2.09 KB
/
findTableDataContainingWords.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<label for="tb">Console<br/><textarea id="tb" style="height:500px;width:500px;"></textarea></label>
<script>
function printNl(text){
var tb = document.getElementById("tb");
if(tb){
if(typeof(text) === 'object'){
text = JSON.stringify(text);
}
var currentVal = tb.value;
if(currentVal === ''){
currentVal += text.toString();
}else{
currentVal += '\r\n' + text.toString();
}
tb.value = currentVal;
}
}
let tbl = {
tableName: '',
fields: [
'ID',
'',
''
],
excludeFields: ['ID'],
words: ['test', 'Test']
}
function generateTopTen(){
printNl('SELECT TOP 10 * FROM ' + tbl.tableName);
}
function generateFindWithWords(){
tbl.fields = tbl.fields.filter(Boolean)
var selector = '';
tbl.fields.forEach(function(word, index){
if(word !== ''){
selector += word;
if(index < tbl.fields.length-1){
selector += ', ';
}
}
});
printNl('SELECT '+selector+' FROM ' + tbl.tableName);
var where = '';
if(tbl.fields.length > tbl.excludeFields.length){
where = 'WHERE '
}
tbl.fields.forEach(function(field, wordIndex){
let clause = '';
let exclude = tbl.excludeFields.includes(field) || field === '';
if(!exclude){
tbl.words.forEach(function(w, wIndex){
clause += (field + " LIKE '%" + w + "%'")
if(wIndex < tbl.words.length-1){
clause += ' OR ';
}
});
}
if(wordIndex < tbl.fields.length-1 && !exclude){
clause += ' OR ';
}
if(!exclude){
where += clause + '\n';
}
});
printNl(where);
}
generateTopTen();
generateFindWithWords();
</script>