· 4 years ago · Mar 13, 2021, 04:10 AM
1package drivers;
2
3import java.util.ArrayList;
4import java.util.regex.Matcher;
5import java.util.regex.Pattern;
6
7import apps.Database;
8import types.Driver;
9import types.Response;
10import types.Status;
11import types.Table;
12
13
14
15
16class Numbers {
17 private ArrayList<Object> listHelp;
18
19 @SuppressWarnings("unused")
20 public ArrayList<Object> real_values() {
21 return listHelp;
22 }
23}
24
25public class InsertOrReplaceTable implements Driver {
26 static final Pattern pattern = Pattern.compile(
27 //\s*(INSERT|REPLACE)\s+INTO\s+([a-z0-9_]+)\s*\((\s*[a-z0-9_]*\s*(?:,\s*[a-z0-9_]+\s*)*)\)*\s*VALUES\s*\((\s*[^0][-+".?!\s\w]*\s*(?:\s*,\s*[^0][-+".?!\s\w]*)*\s*)\)+
28 "\\s*(INSERT|REPLACE)\\s+INTO\\s+([a-z0-9_]+)\\s*\\(?(\\s*[a-z0-9_]*\\s*(?:,\\s*[a-z0-9_]+\\s*)*)\\)*?\\s*VALUES\\s*\\((\\s*[^0][-+\".?!\\s\\w]*\\s*(?:\\s*,\\s*[^0][-+\".?!\\s\\w]*)*\\s*)\\)+",
29 Pattern.CASE_INSENSITIVE
30 );
31 //group 1 = INSERT or REPLACE ?maybe
32 //group 2 = table_name
33 //group 3 = column_name
34 //group 4 = Value String
35
36 @Override
37 public Response execute(String query, Database db) {
38 Matcher matcher = pattern.matcher(query.strip());
39 if (!matcher.matches())
40 return new Response(query, Status.UNRECOGNIZED, null, null);//user input is a syntax error.
41
42
43 ArrayList<String> column_name = new ArrayList<String>();
44 ArrayList<String> value_strings = new ArrayList<String>();
45 ArrayList<Integer> pointer_array = new ArrayList<Integer>();
46 ArrayList<Object> row = new ArrayList<>();
47 ArrayList<Object> real_types = new ArrayList<>();
48
49 Numbers real_types2 = new Numbers();
50
51
52
53
54
55
56
57
58
59 //***************************USERS INPUTS ARE STORED INTO ARRAYLIST**********************
60 //table name input validation
61 String table_name = matcher.group(2);
62 if(table_name.length() > 15)
63 return new Response(query, Status.FAILED, "Table names must be between 1-15 charachter ", null);//semantic error
64 if(!db.tables().contains(table_name))//checks if table exist
65 return new Response(query, Status.FAILED, "Invalid entry: Table entered was not found in db please try again. ", null);//checks if name exist in Database
66
67 //SCHEMA COLUMN INPUT
68 Table table = db.tables().get(table_name);//retrives schema col names
69 @SuppressWarnings("unchecked")
70 ArrayList<String> schema_col_name = (ArrayList<String>) table.schema().get("column_names"); System.out.println("\n****************START HERE****************\n SCHEMA COLUMN NAMES ARRAY\nSize of column_names before inputs: " + schema_col_name.size() +"\nArray schema_col_types contains the following: " + schema_col_name + "\nSize of column_names after inputs: " + schema_col_name.size() + "\n****************FINSHED HERE****************\n" );
71 @SuppressWarnings("unchecked")
72 ArrayList<String> schema_col_types = (ArrayList<String>) table.schema().get("column_types"); System.out.println("\n****************START HERE****************\n SCHEMA COLUMN TYPES ARRAY\nSize of column_names before inputs: " + schema_col_types.size() +"\nArray schema_col_types contains the following: " + schema_col_types + "\nSize of column_names after inputs: " + schema_col_types.size() + "\n****************FINSHED HERE****************\n" );
73
74 //COLUMN NAME INPUT
75 String[] query_column_name = matcher.group(3).split("\\s*,\\s*");//splits the strings at the comma and stores it into an array
76 if(query_column_name.length > 15) //column length validation
77 return new Response(query, Status.FAILED, "Columms defined exceeds capacity, reduce the number of column inputs to 15. ", null);//checks the size of columns define is <= 15 //System.out.println("the size of query "+query_column_name.length);
78
79 //group one column_names placed into an ArrayList for easy modifications as needed
80 System.out.println("****************START HERE****************\n COLUMN NAMES ARRAY\nSize of column_names before inputs: " + column_name.size());
81 for(String temp: query_column_name) {
82 column_name.add(temp); System.out.print(temp + " "); //use this to check what the column_names being added
83 } System.out.println("Array column_names contains the following: " + column_name + "\nSize of column_names after inputs: " + column_name.size() + "\n****************FINSHED HERE****************\n" );
84
85 //VALUE STRING INPUT
86 String[] query_value_string = matcher.group(4).split("\\s*,\\s*");//splits the strings at the comma and stores it into an array
87
88 //from group 3 placed into an ArrayList for easy modifications as needed
89 System.out.println("****************START HERE****************\n VALUE STRINGS ARRAY\nSize of column_names before inputs: " + value_strings.size());
90 for(String temp: query_value_string) {
91 if(temp.length() >= 130)
92 return new Response(query, Status.FAILED, "Invalid entry: String length cant be longer than 128 characters. ", null);//checks the size of columns define is <= 15
93 value_strings.add(temp); System.out.print(temp + " ");
94 }System.out.println("\nArray value_strings contains the following: " + value_strings + "\nSize of column_names after inputs: " + value_strings.size() + "\n****************FINSHED HERE****************\n" );
95
96
97 //PRIMARY INPUT INDEX FROM SCHEMA IS FOUND
98 String schema_primary = table.schema().get("primary_index").toString();
99 int primary_converted_2int = Integer.parseInt(schema_primary); System.out.println("This should be the primary index number: " + table.schema().get("primary_index").toString());
100
101
102 //****************************************************SANITIZARTION STARTS HERE***************************************************//
103 //if column name is missing schema column name is used this sets the schema column name to be used
104 int helper = 0;
105 String countLength = matcher.group(3);
106 if(countLength.length() == 0) {System.out.println("This is my size of the column names before adding anything: " + countLength.length());
107 if(value_strings.size() != schema_col_name.size())
108 return new Response(query, Status.FAILED, "Invalid entry: The number of column names entered & values entered must match, try again. ", null);
109 column_name.clear();
110 while(value_strings.size() != column_name.size()) {
111 String foundWordAtIndex = schema_col_name.get(helper);
112 column_name.add(foundWordAtIndex);
113 helper++;
114 }
115 }
116 if(value_strings.size()!= column_name.size())
117 return new Response(query, Status.FAILED, "Invalid entry: The number of column names entered & values entered must match, try again. ", null);
118 if(column_name.size() > schema_col_name.size())
119 return new Response(query, Status.FAILED, "Invalid entry: The number of column names entered is larger then the schema, try again. ", null);
120
121 //*****************************************COMPARING ARRAYS MEET QUALIFICATIONS***************************************************//
122 //pointer array is setup here
123 for(String temp: column_name) {
124 if(schema_col_name.contains(temp)) {
125 int found = schema_col_name.indexOf(temp);
126 if(pointer_array.contains(found))
127 return new Response(query, Status.FAILED, "Invalid entry: duplicated column name entry. ", null);
128 pointer_array.add(found);
129 }
130 }System.out.println("\nHere are index numbers stored from the pointer_array: " + pointer_array);
131
132 //checks for missig primary
133 if(!pointer_array.contains(primary_converted_2int))
134 return new Response(query, Status.FAILED, "Invalid entry: Missing primary entry. ", null);
135
136 //create empty row array
137 while(row.size() < schema_col_name.size()) {
138 row.add(null);
139 }System.out.println("Here I am the rows array as nulls array: " + row);
140
141 //create empty real_types array
142 while(real_types.size() < schema_col_name.size()) {
143 real_types.add(null);
144 }System.out.println("Here I am the realy_types array as nulls array: " + real_types);
145
146 //pointer array is used to build the rows array
147 int counter = 0;
148 for(int i: pointer_array) {
149 int found = i;
150 String rowsWord = value_strings.get(counter);
151
152 row.set(found, rowsWord);
153 counter++;
154 }System.out.println("Here I am the ROW array with values inputted: " + row);
155
156 //****************************strings from query converted to there respective data type values matched from schema_col_types array**********************/
157 int counter2 = 0;
158 for(String temp: value_strings) {
159 int indexValue = pointer_array.get(counter2);
160 String typesString = schema_col_types.get(indexValue);
161 counter2++;
162 System.out.println("\nBefore converting me to the appropriate type I am a string before any converstion is made: " + typesString);
163 System.out.println("This is my string I am: " + temp + "\n");
164
165
166 //String validation check
167 if(typesString.equalsIgnoreCase("String")) {System.out.println("I am the type STRING I look like this before converstion: " + temp);
168 if(!temp.contains("\"") )
169 return new Response(query, Status.FAILED, "Invalid entry: this STRING is missing quoatation marks. ", null);
170
171 String result = temp.substring(1 , temp.length()-1);System.out.println("I am the type string I look like this after converstion: " + temp);
172 real_types.set(indexValue, result);
173 }
174 //boolean validation check
175 else if(typesString.equalsIgnoreCase("Boolean")) { System.out.println("I am the type BOOLEAN. I look like this before converstion: " + temp);
176 if(temp.contains("\"") )
177 return new Response(query, Status.FAILED, "Invalid entry: boolean columns cannot accept string literals ",null);
178
179 if(temp.equalsIgnoreCase("null")) {
180 real_types.set(indexValue, null);
181 break;
182 }
183 if(temp.matches("[0-9]"))
184 return new Response(query, Status.FAILED, "Invalid entry: boolean columns cannot accept integer literals ",null);
185
186 boolean result = Boolean.parseBoolean(temp);
187 real_types.set(indexValue, result);
188 System.out.println("I am the type BOOLEAN. I look like this after converstion: " + temp);
189 }
190
191 //Integer validation check
192 else if(typesString.equalsIgnoreCase("Integer")) { System.out.println("I am the type INTEGER. I look like this before converstion: " + temp);
193 if(temp.equalsIgnoreCase("null")) {
194 real_types.set(indexValue, null);
195
196 }
197 else
198 try {
199 int result=Integer.parseInt(temp);
200 if(temp.startsWith("0") && result != 0) {
201 return new Response(query, Status.FAILED, "Invalid entry: leading zeros not allowed. ", null);
202 }
203 real_types.set(indexValue, result);
204 }
205 catch (NumberFormatException e) {
206 return new Response(query, Status.FAILED, "The Number entered is not a valid 32 bit integer in java", null);
207 }//semantic error
208 }
209 }
210
211 System.out.println("I am the REAL_TYPES array I look like this: " + real_types );
212
213 //checks for null primary columns
214 if(real_types.get(primary_converted_2int) == null ) {
215 return new Response(query, Status.FAILED, "Invalid entry: Primary entries can not be null. ", null);
216 }
217
218 ////*****************************************INSERTING ROWS STARTS HERE***************************************************//
219 int rowsModed = 0;
220 String Insert_or_Replace = matcher.group(1);
221
222 //inserts rows
223 if(Insert_or_Replace.equalsIgnoreCase("insert")) {
224 if(table.state().contains(real_types.get(primary_converted_2int)))
225 return new Response(query, Status.FAILED, "Invliad entry: Primary key already exists", null);
226
227 else table.state().put(real_types.get(primary_converted_2int), real_types);
228 }
229
230 //replaces rows
231 if(Insert_or_Replace.equalsIgnoreCase("replace")) {
232 rowsModed++;
233 table.state().put(real_types.get(primary_converted_2int), real_types);
234 }
235 int size = table.state().size();
236
237 System.out.println("\n");
238 return new Response(query, Status.SUCCESSFUL, "Tables name is: " + table_name + ", This tables size is: " + size + " & the number of rows modified: " + rowsModed, table);
239 }
240}