Skip to content

Commit d876834

Browse files
committed
Update diff in part4
1 parent 7bc1ab5 commit d876834

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

_parts/part4.md

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,17 @@ It's gonna be great.
305305

306306
Here's the complete diff for this part:
307307
```diff
308+
@@ -22,6 +22,8 @@ typedef enum MetaCommandResult_t MetaCommandResult;
309+
308310
enum PrepareResult_t {
309311
PREPARE_SUCCESS,
310312
+ PREPARE_NEGATIVE_ID,
311313
+ PREPARE_STRING_TOO_LONG,
312314
PREPARE_SYNTAX_ERROR,
313315
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
317319
struct Row_t {
318320
uint32_t id;
319321
- char username[COLUMN_USERNAME_SIZE];
@@ -322,69 +324,69 @@ Here's the complete diff for this part:
322324
+ char email[COLUMN_EMAIL_SIZE + 1];
323325
};
324326
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) {
327329
}
328330
}
329-
331+
332+
-PrepareResult prepare_statement(InputBuffer* input_buffer,
333+
- Statement* statement) {
334+
- if (strncmp(input_buffer->buffer, "insert", 6) == 0) {
330335
+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) {
332342
+
333343
+ char* keyword = strtok(input_buffer->buffer, " ");
334344
+ char* id_string = strtok(NULL, " ");
335345
+ char* username = strtok(NULL, " ");
336346
+ char* email = strtok(NULL, " ");
337347
+
338348
+ if (id_string == NULL || username == NULL || email == NULL) {
339-
+ return PREPARE_SYNTAX_ERROR;
340-
+ }
349+
return PREPARE_SYNTAX_ERROR;
350+
}
341351
+
342352
+ int id = atoi(id_string);
343353
+ if (id < 0) {
344-
+ return PREPARE_NEGATIVE_ID;
354+
+ return PREPARE_NEGATIVE_ID;
345355
+ }
346356
+ if (strlen(username) > COLUMN_USERNAME_SIZE) {
347-
+ return PREPARE_STRING_TOO_LONG;
357+
+ return PREPARE_STRING_TOO_LONG;
348358
+ }
349359
+ if (strlen(email) > COLUMN_EMAIL_SIZE) {
350-
+ return PREPARE_STRING_TOO_LONG;
360+
+ return PREPARE_STRING_TOO_LONG;
351361
+ }
352362
+
353363
+ statement->row_to_insert.id = id;
354364
+ strcpy(statement->row_to_insert.username, username);
355365
+ strcpy(statement->row_to_insert.email, email);
356366
+
357-
+ return PREPARE_SUCCESS;
358-
+}
367+
return PREPARE_SUCCESS;
359368
+
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);
372374
}
373375
if (strcmp(input_buffer->buffer, "select") == 0) {
374376
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[]) {
376378
switch (prepare_statement(input_buffer, &statement)) {
377379
case (PREPARE_SUCCESS):
378380
break;
379381
+ case (PREPARE_NEGATIVE_ID):
380-
+ printf("ID must be positive.\n");
381-
+ continue;
382+
+ printf("ID must be positive.\n");
383+
+ continue;
382384
+ case (PREPARE_STRING_TOO_LONG):
383-
+ printf("String is too long.\n");
384-
+ continue;
385+
+ printf("String is too long.\n");
386+
+ continue;
385387
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;
388390
```
389391
And we added tests:
390392
```diff
@@ -474,4 +476,4 @@ And we added tests:
474476
+ ])
475477
+ end
476478
+end
477-
```
479+
```

0 commit comments

Comments
 (0)