parse.cpp 10.7 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4 5 6 7 8 9 10 11
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
12
// Created by Meiyi 
羽飞's avatar
羽飞 已提交
13 14 15 16 17 18 19 20 21
//

#include <mutex>
#include "sql/parser/parse.h"
#include "rc.h"
#include "common/log/log.h"

RC parse(char *st, Query *sqln);

羽飞's avatar
羽飞 已提交
22
void relation_attr_init(RelAttr *relation_attr, char *relation_name, char *attribute_name)
23
{
羽飞's avatar
羽飞 已提交
24
  if (relation_name != nullptr) {
羽飞's avatar
羽飞 已提交
25
    relation_attr->relation_name = relation_name;
羽飞's avatar
羽飞 已提交
26 27 28
  } else {
    relation_attr->relation_name = nullptr;
  }
羽飞's avatar
羽飞 已提交
29
  relation_attr->attribute_name = attribute_name;
羽飞's avatar
羽飞 已提交
30 31
}

32 33
void relation_attr_destroy(RelAttr *relation_attr)
{
羽飞's avatar
羽飞 已提交
34 35 36 37 38 39
  free(relation_attr->relation_name);
  free(relation_attr->attribute_name);
  relation_attr->relation_name = nullptr;
  relation_attr->attribute_name = nullptr;
}

40 41
void value_init_integer(Value *value, int v)
{
羽飞's avatar
羽飞 已提交
42 43 44 45
  value->type = INTS;
  value->data = malloc(sizeof(v));
  memcpy(value->data, &v, sizeof(v));
}
46 47
void value_init_float(Value *value, float v)
{
羽飞's avatar
羽飞 已提交
48 49 50 51
  value->type = FLOATS;
  value->data = malloc(sizeof(v));
  memcpy(value->data, &v, sizeof(v));
}
羽飞's avatar
羽飞 已提交
52 53 54 55 56
void value_init_string(Value *value, char *v)
{
  value->type = CHARS;
  value->data = v;
}
57 58
void value_init_string(Value *value, const char *v)
{
羽飞's avatar
羽飞 已提交
59 60 61
  value->type = CHARS;
  value->data = strdup(v);
}
62 63
void value_destroy(Value *value)
{
羽飞's avatar
羽飞 已提交
64 65 66 67 68
  value->type = UNDEFINED;
  free(value->data);
  value->data = nullptr;
}

69 70 71
void condition_init(Condition *condition, CompOp comp, int left_is_attr, RelAttr *left_attr, Value *left_value,
    int right_is_attr, RelAttr *right_attr, Value *right_value)
{
羽飞's avatar
羽飞 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  condition->comp = comp;
  condition->left_is_attr = left_is_attr;
  if (left_is_attr) {
    condition->left_attr = *left_attr;
  } else {
    condition->left_value = *left_value;
  }

  condition->right_is_attr = right_is_attr;
  if (right_is_attr) {
    condition->right_attr = *right_attr;
  } else {
    condition->right_value = *right_value;
  }
}
87 88
void condition_destroy(Condition *condition)
{
羽飞's avatar
羽飞 已提交
89 90 91 92 93 94 95 96 97 98 99 100
  if (condition->left_is_attr) {
    relation_attr_destroy(&condition->left_attr);
  } else {
    value_destroy(&condition->left_value);
  }
  if (condition->right_is_attr) {
    relation_attr_destroy(&condition->right_attr);
  } else {
    value_destroy(&condition->right_value);
  }
}

羽飞's avatar
羽飞 已提交
101
void attr_info_init(AttrInfo *attr_info, char *name, AttrType type, size_t length)
102
{
羽飞's avatar
羽飞 已提交
103
  attr_info->name = name;
羽飞's avatar
羽飞 已提交
104 105 106
  attr_info->type = type;
  attr_info->length = length;
}
107 108
void attr_info_destroy(AttrInfo *attr_info)
{
羽飞's avatar
羽飞 已提交
109 110 111 112 113
  free(attr_info->name);
  attr_info->name = nullptr;
}

void selects_init(Selects *selects, ...);
114 115
void selects_append_attribute(Selects *selects, RelAttr *rel_attr)
{
羽飞's avatar
羽飞 已提交
116 117
  selects->attributes[selects->attr_num++] = *rel_attr;
}
羽飞's avatar
羽飞 已提交
118
void selects_append_relation(Selects *selects, char *relation_name)
119
{
羽飞's avatar
羽飞 已提交
120
  selects->relations[selects->relation_num++] = relation_name;
羽飞's avatar
羽飞 已提交
121 122
}

123 124 125
void selects_append_conditions(Selects *selects, Condition conditions[], size_t condition_num)
{
  assert(condition_num <= sizeof(selects->conditions) / sizeof(selects->conditions[0]));
羽飞's avatar
羽飞 已提交
126 127 128 129 130 131
  for (size_t i = 0; i < condition_num; i++) {
    selects->conditions[i] = conditions[i];
  }
  selects->condition_num = condition_num;
}

132 133
void selects_destroy(Selects *selects)
{
羽飞's avatar
羽飞 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  for (size_t i = 0; i < selects->attr_num; i++) {
    relation_attr_destroy(&selects->attributes[i]);
  }
  selects->attr_num = 0;

  for (size_t i = 0; i < selects->relation_num; i++) {
    free(selects->relations[i]);
    selects->relations[i] = NULL;
  }
  selects->relation_num = 0;

  for (size_t i = 0; i < selects->condition_num; i++) {
    condition_destroy(&selects->conditions[i]);
  }
  selects->condition_num = 0;
}

羽飞's avatar
羽飞 已提交
151
void inserts_init(Inserts *inserts, char *relation_name, Value values[], size_t value_num)
152 153
{
  assert(value_num <= sizeof(inserts->values) / sizeof(inserts->values[0]));
羽飞's avatar
羽飞 已提交
154

羽飞's avatar
羽飞 已提交
155
  inserts->relation_name = relation_name;
羽飞's avatar
羽飞 已提交
156 157 158 159 160
  for (size_t i = 0; i < value_num; i++) {
    inserts->values[i] = values[i];
  }
  inserts->value_num = value_num;
}
161 162
void inserts_destroy(Inserts *inserts)
{
羽飞's avatar
羽飞 已提交
163 164 165 166 167 168 169 170 171
  free(inserts->relation_name);
  inserts->relation_name = nullptr;

  for (size_t i = 0; i < inserts->value_num; i++) {
    value_destroy(&inserts->values[i]);
  }
  inserts->value_num = 0;
}

羽飞's avatar
羽飞 已提交
172
void deletes_init_relation(Deletes *deletes, char *relation_name)
173
{
羽飞's avatar
羽飞 已提交
174
  deletes->relation_name = relation_name;
羽飞's avatar
羽飞 已提交
175 176
}

177 178 179
void deletes_set_conditions(Deletes *deletes, Condition conditions[], size_t condition_num)
{
  assert(condition_num <= sizeof(deletes->conditions) / sizeof(deletes->conditions[0]));
羽飞's avatar
羽飞 已提交
180 181 182 183 184
  for (size_t i = 0; i < condition_num; i++) {
    deletes->conditions[i] = conditions[i];
  }
  deletes->condition_num = condition_num;
}
185 186
void deletes_destroy(Deletes *deletes)
{
羽飞's avatar
羽飞 已提交
187 188 189 190 191 192 193 194
  for (size_t i = 0; i < deletes->condition_num; i++) {
    condition_destroy(&deletes->conditions[i]);
  }
  deletes->condition_num = 0;
  free(deletes->relation_name);
  deletes->relation_name = nullptr;
}

羽飞's avatar
羽飞 已提交
195
void updates_init(Updates *updates, char *relation_name, char *attribute_name, Value *value,
196 197
    Condition conditions[], size_t condition_num)
{
羽飞's avatar
羽飞 已提交
198 199
  updates->relation_name = relation_name;
  updates->attribute_name = attribute_name;
羽飞's avatar
羽飞 已提交
200 201
  updates->value = *value;

202
  assert(condition_num <= sizeof(updates->conditions) / sizeof(updates->conditions[0]));
羽飞's avatar
羽飞 已提交
203 204 205 206 207 208
  for (size_t i = 0; i < condition_num; i++) {
    updates->conditions[i] = conditions[i];
  }
  updates->condition_num = condition_num;
}

209 210
void updates_destroy(Updates *updates)
{
羽飞's avatar
羽飞 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223
  free(updates->relation_name);
  free(updates->attribute_name);
  updates->relation_name = nullptr;
  updates->attribute_name = nullptr;

  value_destroy(&updates->value);

  for (size_t i = 0; i < updates->condition_num; i++) {
    condition_destroy(&updates->conditions[i]);
  }
  updates->condition_num = 0;
}

224 225
void create_table_append_attribute(CreateTable *create_table, AttrInfo *attr_info)
{
羽飞's avatar
羽飞 已提交
226 227
  create_table->attributes[create_table->attribute_count++] = *attr_info;
}
228

羽飞's avatar
羽飞 已提交
229
void create_table_init_name(CreateTable *create_table, char *relation_name)
230
{
羽飞's avatar
羽飞 已提交
231
  create_table->relation_name = relation_name;
羽飞's avatar
羽飞 已提交
232
}
233 234 235

void create_table_destroy(CreateTable *create_table)
{
羽飞's avatar
羽飞 已提交
236 237 238 239 240 241 242 243
  for (size_t i = 0; i < create_table->attribute_count; i++) {
    attr_info_destroy(&create_table->attributes[i]);
  }
  create_table->attribute_count = 0;
  free(create_table->relation_name);
  create_table->relation_name = nullptr;
}

羽飞's avatar
羽飞 已提交
244
void drop_table_init(DropTable *drop_table, char *relation_name)
245
{
羽飞's avatar
羽飞 已提交
246
  drop_table->relation_name = relation_name;
羽飞's avatar
羽飞 已提交
247
}
248 249 250

void drop_table_destroy(DropTable *drop_table)
{
羽飞's avatar
羽飞 已提交
251 252 253 254
  free(drop_table->relation_name);
  drop_table->relation_name = nullptr;
}

255
void create_index_init(
羽飞's avatar
羽飞 已提交
256
    CreateIndex *create_index, char *index_name, char *relation_name, char *attr_name)
257
{
羽飞's avatar
羽飞 已提交
258 259 260
  create_index->index_name = index_name;
  create_index->relation_name = relation_name;
  create_index->attribute_name = attr_name;
羽飞's avatar
羽飞 已提交
261
}
262 263 264

void create_index_destroy(CreateIndex *create_index)
{
羽飞's avatar
羽飞 已提交
265 266 267 268 269 270 271 272 273
  free(create_index->index_name);
  free(create_index->relation_name);
  free(create_index->attribute_name);

  create_index->index_name = nullptr;
  create_index->relation_name = nullptr;
  create_index->attribute_name = nullptr;
}

羽飞's avatar
羽飞 已提交
274
void drop_index_init(DropIndex *drop_index, char *index_name)
275
{
羽飞's avatar
羽飞 已提交
276
  drop_index->index_name = index_name;
羽飞's avatar
羽飞 已提交
277
}
278 279 280

void drop_index_destroy(DropIndex *drop_index)
{
羽飞's avatar
羽飞 已提交
281 282 283 284
  free((char *)drop_index->index_name);
  drop_index->index_name = nullptr;
}

羽飞's avatar
羽飞 已提交
285
void desc_table_init(DescTable *desc_table, char *relation_name)
286
{
羽飞's avatar
羽飞 已提交
287
  desc_table->relation_name = relation_name;
羽飞's avatar
羽飞 已提交
288 289
}

290 291
void desc_table_destroy(DescTable *desc_table)
{
羽飞's avatar
羽飞 已提交
292 293 294 295
  free((char *)desc_table->relation_name);
  desc_table->relation_name = nullptr;
}

羽飞's avatar
羽飞 已提交
296
void load_data_init(LoadData *load_data, char *relation_name, char *file_name)
297
{
羽飞's avatar
羽飞 已提交
298
  load_data->relation_name = relation_name;
羽飞's avatar
羽飞 已提交
299 300 301 302

  if (file_name[0] == '\'' || file_name[0] == '\"') {
    file_name++;
  }
羽飞's avatar
羽飞 已提交
303
  char *dup_file_name = file_name;
羽飞's avatar
羽飞 已提交
304 305 306 307 308 309 310
  int len = strlen(dup_file_name);
  if (dup_file_name[len - 1] == '\'' || dup_file_name[len - 1] == '\"') {
    dup_file_name[len - 1] = 0;
  }
  load_data->file_name = dup_file_name;
}

311 312
void load_data_destroy(LoadData *load_data)
{
羽飞's avatar
羽飞 已提交
313 314 315 316 317 318
  free((char *)load_data->relation_name);
  free((char *)load_data->file_name);
  load_data->relation_name = nullptr;
  load_data->file_name = nullptr;
}

319 320
void query_init(Query *query)
{
羽飞's avatar
羽飞 已提交
321 322 323 324
  query->flag = SCF_ERROR;
  memset(&query->sstr, 0, sizeof(query->sstr));
}

325 326
Query *query_create()
{
羽飞's avatar
羽飞 已提交
327 328 329 330 331 332 333 334 335 336
  Query *query = (Query *)malloc(sizeof(Query));
  if (nullptr == query) {
    LOG_ERROR("Failed to alloc memroy for query. size=%ld", sizeof(Query));
    return nullptr;
  }

  query_init(query);
  return query;
}

337 338
void query_reset(Query *query)
{
羽飞's avatar
羽飞 已提交
339 340 341
  switch (query->flag) {
    case SCF_SELECT: {
      selects_destroy(&query->sstr.selection);
342
    } break;
羽飞's avatar
羽飞 已提交
343 344
    case SCF_INSERT: {
      inserts_destroy(&query->sstr.insertion);
345
    } break;
羽飞's avatar
羽飞 已提交
346 347
    case SCF_DELETE: {
      deletes_destroy(&query->sstr.deletion);
348
    } break;
羽飞's avatar
羽飞 已提交
349 350
    case SCF_UPDATE: {
      updates_destroy(&query->sstr.update);
351
    } break;
羽飞's avatar
羽飞 已提交
352 353
    case SCF_CREATE_TABLE: {
      create_table_destroy(&query->sstr.create_table);
354
    } break;
羽飞's avatar
羽飞 已提交
355 356
    case SCF_DROP_TABLE: {
      drop_table_destroy(&query->sstr.drop_table);
357
    } break;
羽飞's avatar
羽飞 已提交
358 359
    case SCF_CREATE_INDEX: {
      create_index_destroy(&query->sstr.create_index);
360
    } break;
羽飞's avatar
羽飞 已提交
361 362
    case SCF_DROP_INDEX: {
      drop_index_destroy(&query->sstr.drop_index);
363
    } break;
羽飞's avatar
羽飞 已提交
364 365
    case SCF_SYNC: {

366
    } break;
羽飞's avatar
羽飞 已提交
367
    case SCF_SHOW_TABLES:
368
      break;
羽飞's avatar
羽飞 已提交
369 370 371

    case SCF_DESC_TABLE: {
      desc_table_destroy(&query->sstr.desc_table);
372
    } break;
羽飞's avatar
羽飞 已提交
373 374 375

    case SCF_LOAD_DATA: {
      load_data_destroy(&query->sstr.load_data);
376
    } break;
羽飞's avatar
羽飞 已提交
377
    case SCF_CLOG_SYNC:
羽飞's avatar
羽飞 已提交
378 379 380 381 382 383
    case SCF_BEGIN:
    case SCF_COMMIT:
    case SCF_ROLLBACK:
    case SCF_HELP:
    case SCF_EXIT:
    case SCF_ERROR:
384
      break;
羽飞's avatar
羽飞 已提交
385 386 387
  }
}

388 389
void query_destroy(Query *query)
{
羽飞's avatar
羽飞 已提交
390 391 392 393
  query_reset(query);
  free(query);
}

羽飞's avatar
羽飞 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
const char *ATTR_TYPE_NAME[] = {"undefined", "chars", "ints", "floats"};

const char *attr_type_to_string(AttrType type)
{
  if (type >= UNDEFINED && type <= FLOATS) {
    return ATTR_TYPE_NAME[type];
  }
  return "unknown";
}
AttrType attr_type_from_string(const char *s)
{
  for (unsigned int i = 0; i < sizeof(ATTR_TYPE_NAME) / sizeof(ATTR_TYPE_NAME[0]); i++) {
    if (0 == strcmp(ATTR_TYPE_NAME[i], s)) {
      return (AttrType)i;
    }
  }
  return UNDEFINED;
}

羽飞's avatar
羽飞 已提交
413 414
////////////////////////////////////////////////////////////////////////////////

羽飞's avatar
羽飞 已提交
415
int sql_parse(const char *st, Query *sqls);
羽飞's avatar
羽飞 已提交
416

417 418
RC parse(const char *st, Query *sqln)
{
羽飞's avatar
羽飞 已提交
419 420 421 422 423 424
  sql_parse(st, sqln);

  if (sqln->flag == SCF_ERROR)
    return SQL_SYNTAX;
  else
    return SUCCESS;
羽飞's avatar
羽飞 已提交
425
}