@@ -305,15 +305,17 @@ It's gonna be great.
305
305
306
306
Here's the complete diff for this part:
307
307
``` diff
308
+ @@ -22,6 +22,8 @@ typedef enum MetaCommandResult_t MetaCommandResult;
309
+
308
310
enum PrepareResult_t {
309
311
PREPARE_SUCCESS,
310
312
+ PREPARE_NEGATIVE_ID,
311
313
+ PREPARE_STRING_TOO_LONG,
312
314
PREPARE_SYNTAX_ERROR,
313
315
PREPARE_UNRECOGNIZED_STATEMENT
314
- };
315
- @@ -33 ,8 +35 ,8 @@ const uint32_t COLUMN_USERNAME_SIZE = 32 ;
316
- const uint32_t COLUMN_EMAIL_SIZE = 255;
316
+ };
317
+ @@ -34 ,8 +36 ,8 @@ typedef enum StatementType_t StatementType ;
318
+ #define COLUMN_EMAIL_SIZE 255
317
319
struct Row_t {
318
320
uint32_t id;
319
321
- char username[COLUMN_USERNAME_SIZE];
@@ -322,69 +324,69 @@ Here's the complete diff for this part:
322
324
+ char email[COLUMN_EMAIL_SIZE + 1];
323
325
};
324
326
typedef struct Row_t Row;
325
-
326
- @@ -133,17 +135 ,40 @@ MetaCommandResult do_meta_command(InputBuffer* input_buffer) {
327
+
328
+ @@ -150,18 +152 ,40 @@ MetaCommandResult do_meta_command(InputBuffer* input_buffer, Table *table ) {
327
329
}
328
330
}
329
-
331
+
332
+ - PrepareResult prepare_statement(InputBuffer* input_buffer,
333
+ - Statement* statement) {
334
+ - if (strncmp(input_buffer->buffer, "insert", 6) == 0) {
330
335
+ PrepareResult prepare_insert(InputBuffer* input_buffer, Statement* statement) {
331
- + statement->type = STATEMENT_INSERT;
336
+ statement->type = STATEMENT_INSERT;
337
+ - int args_assigned = sscanf(
338
+ - input_buffer->buffer, "insert %d %s %s", &(statement->row_to_insert.id),
339
+ - statement->row_to_insert.username, statement->row_to_insert.email
340
+ - );
341
+ - if (args_assigned < 3) {
332
342
+
333
343
+ char* keyword = strtok(input_buffer->buffer, " ");
334
344
+ char* id_string = strtok(NULL, " ");
335
345
+ char* username = strtok(NULL, " ");
336
346
+ char* email = strtok(NULL, " ");
337
347
+
338
348
+ if (id_string == NULL || username == NULL || email == NULL) {
339
- + return PREPARE_SYNTAX_ERROR;
340
- + }
349
+ return PREPARE_SYNTAX_ERROR;
350
+ }
341
351
+
342
352
+ int id = atoi(id_string);
343
353
+ if (id < 0) {
344
- + return PREPARE_NEGATIVE_ID;
354
+ + return PREPARE_NEGATIVE_ID;
345
355
+ }
346
356
+ if (strlen(username) > COLUMN_USERNAME_SIZE) {
347
- + return PREPARE_STRING_TOO_LONG;
357
+ + return PREPARE_STRING_TOO_LONG;
348
358
+ }
349
359
+ if (strlen(email) > COLUMN_EMAIL_SIZE) {
350
- + return PREPARE_STRING_TOO_LONG;
360
+ + return PREPARE_STRING_TOO_LONG;
351
361
+ }
352
362
+
353
363
+ statement->row_to_insert.id = id;
354
364
+ strcpy(statement->row_to_insert.username, username);
355
365
+ strcpy(statement->row_to_insert.email, email);
356
366
+
357
- + return PREPARE_SUCCESS;
358
- + }
367
+ return PREPARE_SUCCESS;
359
368
+
360
- PrepareResult prepare_statement(InputBuffer* input_buffer,
361
- Statement* statement) {
362
- if (strncmp(input_buffer->buffer, "insert", 6) == 0) {
363
- - statement->type = STATEMENT_INSERT;
364
- - int args_assigned = sscanf(
365
- - input_buffer->buffer, "insert %d %s %s", &(statement->row_to_insert.id),
366
- - statement->row_to_insert.username, statement->row_to_insert.email);
367
- - if (args_assigned < 3) {
368
- - return PREPARE_SYNTAX_ERROR;
369
- - }
370
- - return PREPARE_SUCCESS;
371
- + return prepare_insert(input_buffer, statement);
369
+ + }
370
+ + PrepareResult prepare_statement(InputBuffer* input_buffer,
371
+ + Statement* statement) {
372
+ + if (strncmp(input_buffer->buffer, "insert", 6) == 0) {
373
+ + return prepare_insert(input_buffer, statement);
372
374
}
373
375
if (strcmp(input_buffer->buffer, "select") == 0) {
374
376
statement->type = STATEMENT_SELECT;
375
- @@ -205 ,6 +230 ,12 @@ int main(int argc, char* argv[]) {
377
+ @@ -223 ,6 +247 ,12 @@ int main(int argc, char* argv[]) {
376
378
switch (prepare_statement(input_buffer, &statement)) {
377
379
case (PREPARE_SUCCESS):
378
380
break;
379
381
+ case (PREPARE_NEGATIVE_ID):
380
- + printf("ID must be positive.\n");
381
- + continue;
382
+ + printf("ID must be positive.\n");
383
+ + continue;
382
384
+ case (PREPARE_STRING_TOO_LONG):
383
- + printf("String is too long.\n");
384
- + continue;
385
+ + printf("String is too long.\n");
386
+ + continue;
385
387
case (PREPARE_SYNTAX_ERROR):
386
- printf("Syntax error. Could not parse statement.\n");
387
- continue;
388
+ printf("Syntax error. Could not parse statement.\n");
389
+ continue;
388
390
```
389
391
And we added tests:
390
392
``` diff
@@ -474,4 +476,4 @@ And we added tests:
474
476
+ ])
475
477
+ end
476
478
+ end
477
- ```
479
+ ```
0 commit comments