-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathparser.cpp
More file actions
133 lines (118 loc) · 3.74 KB
/
parser.cpp
File metadata and controls
133 lines (118 loc) · 3.74 KB
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
// process a query
/*
* author - mandeep singh
* 13-04-2017
* copyright 2017 reserved
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
*/
/*
* QUERIES (terminate with semicolon(;), tokens should be space separated)
1. SELECT * FROM TABLE_NAME; OR SELECT COL1,COL2... FROM TABLE_NAME;
*/
/*
// ----------------------------------------------------------------------
// File : parser.cpp
// Author : Mandeep singh
// Purpose : parses the query
//
//
// DeepDataBase, Copyright (C) 2015 - 2017
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// ----------------------------------------------------------------------
*/
#include "parser.h"
#include "display.h"
#include<cstring>
#include<iostream>
//tokenize select query for processing
void tokenize_select(char query[]){
// break the query into tokens
char buffer[1024];
vector <string> token_vector;
//vector <string> temp;
strcpy(buffer, query);
char *token = strtok(buffer, " ,;");
while (token) {
string token_temp(token);
if(token_temp != " " && token_temp != "\n" ){
token_vector.push_back(token_temp);
}
token = strtok(NULL, " ,;");
}
process_select(token_vector);
}
//tokenize create query for processing
void tokenize_create(char query[]){
// CREATE TABLE [IF NOT EXISTS] <tablename> (<column1> <datatype>, <column2> <datatype>, PRIMARY KEY(<column1>));
/*
* implementation1
CREATE TABLE <tablename> (<column1> <datatype>, <column2> <datatype>);
*/
char buffer[1024];
vector <string> token_vector;
strcpy(buffer, query);
char *token = strtok(buffer, " ,;");
while (token) {
string token_temp(token);
if(token_temp != " " && token_temp != "\n" ){
std::transform(token_temp.begin(), token_temp.end(), token_temp.begin(), ::tolower);
token_vector.push_back(token_temp);
}
token = strtok(NULL, " ,;");
}
// for(int i=0;i<token_vector.size();i++){
// cout<<token_vector[i]<<endl;
// }
cout<<endl;
}
void get_query(){
char *query;
query = (char*) malloc (sizeof(char)*50);
printf("enter query to test\n");
fflush(stdin);
fflush(stdout);
fgets(query,sizeof(char)*MAX_NAME,stdin);
fgets(query,sizeof(char)*MAX_NAME,stdin);
//
char buffer[1024];
strcpy(buffer, query);
char *token = strtok(buffer, " ");
if(token){
string token_temp(token);
if(token_temp != " " && token_temp != "\n"){
//cout<<"token:: "<<token<<endl;
if(token_temp == "select"){
tokenize_select(query);
}else if(token_temp == "create"){
tokenize_create(query);
}
}
}
//printf("\nquery:: %s\n",query);
}
void parse_create(){
string s;
cout<<"enter create query\n";
cin.ignore();
getline (cin, s);
int openpos = s.find("(");
int closepos = s.find(")");
string token = s.substr(0, openpos);
string tbetween = s.substr(openpos+1, s.length()-openpos-2);
cout<<token<<endl;
cout<<tbetween<<endl;
}