-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryCreator.java
139 lines (131 loc) · 5.78 KB
/
QueryCreator.java
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.util.HashMap;
public class QueryCreator {
//without primary key
public static String queryCreate(String tableName , HashMap<String, String> hashMap){
String colData="";
String beforeCreate = "CREATE table "+tableName+"(";
for(String columnName : hashMap.keySet()){
colData += columnName+" "+hashMap.get(columnName) + ",";
}
colData = colData.substring(0, colData.length() -1);
String query = beforeCreate+colData+");";
return query;
}
//with primary key
public static String queryCreate(String tableName , HashMap<String, String> hashMap, String pimarykey){
String colData="";
String beforeCreate = "CREATE table "+tableName+"("+pimarykey+" PRIMARY KEY,";
for(String columnName : hashMap.keySet()){
colData += columnName+" "+hashMap.get(columnName) + ",";
}
colData = colData.substring(0, colData.length() -1);
String query = beforeCreate+colData+");";
return query;
}
//Insert values
public static String queryInsert(String tableName , HashMap<String,String> recordData, String[] colDataType){
String colData="";
String beforeInsert = "INSERT INTO "+tableName+"(";
for(String columnName : recordData.keySet()){
colData += columnName+" ,";
}
//remove last comma
colData = colData.substring(0, colData.length() -1);
colData = beforeInsert + colData+ ") values(";
String handledData = dataTypeHandler(colDataType,recordData);
colData +=handledData;
colData = colData.substring(0, colData.length() -1);
String query = colData+");";
return query;
}
//update table
public static String queryUpdate(String tableName, HashMap<String, String> colValue, HashMap<String, String> condition, String[] colDataType, String[] colDataTypeBWhere){
String colData="";
String beforeCreate = "update "+tableName+" ";
for(String columnName : colValue.keySet()){
colData += columnName+"="+dataTypeHandler(colDataTypeBWhere,colValue) + ",";
}
colData = colData.substring(0, colData.length() -1)+" where ";
//
for(String columnName : condition.keySet()){
colData += columnName+"="+dataTypeHandler(colDataType,condition);
}
colData = colData.substring(0, colData.length() -1);
String query = beforeCreate+colData+";";
return query;
}
//delete query
public static String queryDelete(String tableName, HashMap<String, String> condition, String[] colDataType){
String query = "DELETE FROM "+ tableName + " where ";
String colData ="";
for(String columnName : condition.keySet()){
colData += columnName+"="+dataTypeHandler(colDataType,condition);
}
//delete extra comma
colData = colData.substring(0, colData.length() -1);
query+=colData+";";
return query;
}
//run program
public static void main(String[] args) {
//creating table with student name roll number and address with s_id primary key
HashMap<String,String> studentTable = new HashMap<>();
studentTable.put("student_name", "varchar(255)");
studentTable.put("rollNumber", "int");
studentTable.put("Address", "varchar(255)");
//create table
System.out.println(queryCreate("StudentRecord",studentTable,"s_id int"));
//insert student name roll number and address in created table
HashMap<String, String> studentData = new HashMap<>();
String[] colDataType = {"i","s","i"};
studentData.put("student_name","Ram Oza");
studentData.put("rollNumber", "1");
studentData.put("Address","Maitidevi");
System.out.println(queryInsert("StudentRecord",studentData,colDataType));
//update table--> Change name of Ram Oza to Ram Ojhas
HashMap<String,String> updatedStudentData = new HashMap<>();
updatedStudentData.put("student_name", "Ram Ojhas");
HashMap<String, String> condition = new HashMap<>();
condition.put("rollNumber", "1");
String[] dataTypeRoll = {"i"};
String[] updateDataType = {"s"};
System.out.println(queryUpdate("StudentRecord",updatedStudentData,condition,dataTypeRoll,updateDataType));
//delete table -> deleting record for s_id 4210
HashMap<String, String> conditionToDelete = new HashMap<>();
conditionToDelete.put("s_id","4210");
String[] delColDataType = {"i"};
System.out.println(queryDelete("StudentRecord",conditionToDelete,delColDataType));
}
//data type handler
public static String dataTypeHandler(String[] colDataType, HashMap<String, String> hashMap) {
int i = 0;
String colData = "";
for (String columnName : hashMap.keySet()) {
//assuming values to insert and coldata type are in order
//if int then no ""
if (colDataType[i] == "i") {
colData += hashMap.get(columnName) + ",";
}
//if string then ""
else if (colDataType[i] == "s") {
colData += '"' + hashMap.get(columnName) + '"' + ",";
}
//if char then ''
else if (colDataType[i] == "c") {
colData += "'" + hashMap.get(columnName) + "',";
}
//if boolean
else if (colDataType[i] == "b") {
if (hashMap.get(columnName) == "true" || hashMap.get(columnName) == "false") {
colData += hashMap.get(columnName) + ",";
} else {
colData += "null,";
}
} else {
colData += "null,";
}
i++;
}
return colData;
}
}