@@ -70,21 +70,28 @@ struct whereClauseS* convert_conditions(ParsedSQL *parsed) {
7070
7171 for (int i = 0 ; i < parsed -> num_conditions ; i ++ ) {
7272 struct whereClauseS * node = malloc (sizeof (struct whereClauseS ));
73- node -> attribute = parsed -> conditions [i ].column ;
74- node -> operator = get_operator_string (parsed -> conditions [i ].op );
75- node -> value = parsed -> conditions [i ].value ;
7673
77- // Simple type inference for the test
78- if (parsed -> conditions [i ].is_numeric ) {
79- node -> value_type = 0 ; // Integer/Number
74+ if (parsed -> conditions [i ].is_nested && parsed -> conditions [i ].nested_sql ) {
75+ node -> attribute = NULL ;
76+ node -> operator = NULL ;
77+ node -> value = NULL ;
78+ node -> value_type = 0 ;
79+ node -> sub = convert_conditions (parsed -> conditions [i ].nested_sql );
8080 } else {
81- node -> value_type = 1 ; // String
81+ node -> attribute = parsed -> conditions [i ].column ;
82+ node -> operator = get_operator_string (parsed -> conditions [i ].op );
83+ node -> value = parsed -> conditions [i ].value ;
84+
85+ // Simple type inference for the test
86+ if (parsed -> conditions [i ].is_numeric ) {
87+ node -> value_type = 0 ; // Integer/Number
88+ } else {
89+ node -> value_type = 1 ; // String
90+ }
91+ node -> sub = NULL ;
8292 }
8393
84- // TODO - Handle sub-expressions and nested conditions
85- // Note: Nested conditions are not currently supported by the test
8694 node -> next = NULL ;
87- node -> sub = NULL ;
8895
8996 // Set logical operator for the *previous* node to connect to this one
9097 // The parser stores logic ops between conditions. logic_ops[0] is between cond[0] and cond[1]
@@ -120,7 +127,7 @@ void run_test_query(struct engineS *engine, const char *query, int max_rows) {
120127 // Print query for testing
121128 printf ("Executing Query: %s\n" , query );
122129
123- // Tokenize
130+ // Tokenize the provided query
124131 Token tokens [MAX_TOKENS ];
125132 int num_tokens = tokenize (query , tokens , MAX_TOKENS );
126133 if (num_tokens <= 0 ) {
@@ -142,13 +149,15 @@ void run_test_query(struct engineS *engine, const char *query, int max_rows) {
142149 }
143150 }
144151
145- // Handle non-SELECT commands here and return early
152+ // Execute based on command type
146153 switch (parsed .command ) {
147154 case CMD_INSERT : {
148155 if (parsed .num_values != 12 ) {
149156 printf ("Error: INSERT requires exactly 12 values.\n" );
150157 return ;
151158 }
159+
160+ // Assign arguments to a record struct
152161 record r ;
153162 r .command_id = strtoull (parsed .insert_values [0 ], NULL , 10 );
154163 safe_copy (r .raw_command , sizeof (r .raw_command ), parsed .insert_values [1 ]);
@@ -166,30 +175,56 @@ void run_test_query(struct engineS *engine, const char *query, int max_rows) {
166175 safe_copy (r .host_name , sizeof (r .host_name ), parsed .insert_values [10 ]);
167176 r .risk_level = atoi (parsed .insert_values [11 ]);
168177
178+ // Execute Insert
179+ clock_t insertStart = clock (); // Start timer for benchmarking
169180 if (executeQueryInsertSerial (engine , parsed .table , & r )) {
170- printf ("Insert successful.\n\n" );
181+ printf ("Insert successful. Execution Time: %ld \n\n" , ( clock () - insertStart ) / CLOCKS_PER_SEC );
171182 } else {
172- printf ("Insert failed.\n\n" );
183+ printf ("Insert failed. Execution Time: %ld \n\n" , ( clock () - insertStart ) / CLOCKS_PER_SEC );
173184 }
185+
174186 return ;
175187 }
176188
177189 case CMD_DELETE : {
190+ // Get the WHERE clause from arguments
178191 struct whereClauseS * whereClause = convert_conditions (& parsed );
192+
193+ // Execute delete
194+ clock_t deleteStart = clock (); // Start timer for benchmarking
179195 struct resultSetS * result = executeQueryDeleteSerial (engine , parsed .table , whereClause );
196+
180197 if (result ) {
181- printf ("Delete successful. Rows affected: %d\n " , result -> numRecords );
198+ printf ("Delete successful. Rows affected: %d. Execution Time: %ld\n\n " , result -> numRecords , (( clock () - deleteStart ) / CLOCKS_PER_SEC ) );
182199 freeResultSet (result );
183200 } else {
184- printf ("Delete failed.\n" );
201+ printf ("Delete failed. Execution Time: %ld\n\n" , (( clock () - deleteStart ) / CLOCKS_PER_SEC ) );
185202 }
203+
186204 free_where_clause_list (whereClause );
187205 return ;
188206 }
189207
190208 case CMD_SELECT : {
191- // Handled below
192- break ;
209+ // Get the WHERE clause from arguments
210+ struct whereClauseS * whereClause = convert_conditions (& parsed );
211+
212+ // Execute select
213+ struct resultSetS * result = executeQuerySelectSerial (
214+ engine ,
215+ selectItems ,
216+ numSelectItems ,
217+ parsed .table ,
218+ whereClause
219+ );
220+
221+ // Verify and Print)
222+ printTable (NULL , result , max_rows ); // Limit to max_rows for testing
223+
224+ // Cleanup
225+ if (result ) freeResultSet (result ); // Free the results object
226+ free_where_clause_list (whereClause ); // Free where clause linked list
227+ printf ("\n" );
193228 }
194229
195230 case CMD_NONE : {
@@ -198,28 +233,8 @@ void run_test_query(struct engineS *engine, const char *query, int max_rows) {
198233 }
199234
200235 default : {
201- printf ( "Unsupported command.\n" );
236+ fprintf ( stderr , "Unsupported command.\n" );
202237 return ;
203238 }
204239 }
205-
206- // Retreive the WHERE clause
207- struct whereClauseS * whereClause = convert_conditions (& parsed );
208-
209- // Execute
210- struct resultSetS * result = executeQuerySelectSerial (
211- engine ,
212- selectItems ,
213- numSelectItems ,
214- parsed .table ,
215- whereClause
216- );
217-
218- // Verify and Print
219- printTable (NULL , result , max_rows );
220-
221- // Cleanup
222- if (result ) freeResultSet (result ); // Free the results object
223- free_where_clause_list (whereClause ); // Free where clause linked list
224- printf ("\n" );
225240}
0 commit comments