-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL.h
52 lines (52 loc) · 1.66 KB
/
SQL.h
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
#include <bits/stdc++.h>
#include <regex>
#include <unistd.h>
using namespace std;
class SQL{
public:
string SELECT_G(string name, string colomn = "", string value = ""){
string res = "";
res += "SELECT * FROM " + name;
if(colomn != ""){
res += " WHERE " + colomn + " CONTAINS " + value;
}
return res;
}
string INSERT_G(string name, vector <string> data){
string res = "";
res += "INSERT INTO " + name + " VALUES " + "(";
for(int i = 0; i < data.size(); i++){
res += data[i];
if(i + 1 != data.size()) res += ",";
}
res += ")";
return res;
}
string UPDATE_G(string name, vector<string> col, vector<string> val, string sel_col, string sel_val){
string res = "";
res += "UPDATE " + name + " SET ";
for(int i = 0; i < val.size(); i++){
res += col[i] + " = " + val[i];
if(i + 1 != val.size()) res += ",";
}
res += " WHERE ";
res += sel_col + " = " + sel_val;
return res;
}
int regex_m(string T){
regex instr_pattern[] = {
regex("(\\s*INSERT\\s+INTO\\s+)([A-Z_0-9a-z\u4e00-\u9fa5]+)(\\s+VALUES\\s+)([(].*[)]\\s*)"),
regex("(\\s*UPDATE\\s+)([A-Z_0-9a-z\u4e00-\u9fa5]+)\\s+(SET)\\s+(.*(?=WHERE))(WHERE)\\s+([A-Z_0-9a-z\u4e00-\u9fa5]+)\\s+(=)\\s+([A-Z_0-9a-z\u4e00-\u9fa5]+)\\s*"),//=
regex("(\\s*SELECT\\s+)(.*)(?=\\s)\\s+(FROM\\s+)(.*(?=\\s))\\s+(WHERE\\s+)([A-Z_0-9a-z\u4e00-\u9fa5]+)\\s+(CONATINS)\\s+([A-Z_0-9a-z\u4e00-\u9fa5]+)\\s*"),
regex("(\\s*SELECT\\s+)(.*)(?=\\s)\\s+(FROM\\s+)([A-Z_0-9a-z\u4e00-\u9fa5]+\\s*)"), //检查是否是*
};
int cnt = sizeof(instr_pattern) / sizeof(regex);
smatch res;
for(int i = 0; i < cnt; i++){
if(regex_match(T, res, instr_pattern[i])){
return i;
}
}
return -1;
}
};