parse.cpp 10.5 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 22 23
//

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

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

#ifdef __cplusplus
extern "C" {
24 25 26
#endif  // __cplusplus
void relation_attr_init(RelAttr *relation_attr, const char *relation_name, const char *attribute_name)
{
羽飞's avatar
羽飞 已提交
27 28 29 30 31 32 33 34
  if (relation_name != nullptr) {
    relation_attr->relation_name = strdup(relation_name);
  } else {
    relation_attr->relation_name = nullptr;
  }
  relation_attr->attribute_name = strdup(attribute_name);
}

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

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

67 68 69
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
羽飞 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  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;
  }
}
85 86
void condition_destroy(Condition *condition)
{
羽飞's avatar
羽飞 已提交
87 88 89 90 91 92 93 94 95 96 97 98
  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);
  }
}

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

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

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

130 131
void selects_destroy(Selects *selects)
{
羽飞's avatar
羽飞 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  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;
}

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

  inserts->relation_name = strdup(relation_name);
  for (size_t i = 0; i < value_num; i++) {
    inserts->values[i] = values[i];
  }
  inserts->value_num = value_num;
}
159 160
void inserts_destroy(Inserts *inserts)
{
羽飞's avatar
羽飞 已提交
161 162 163 164 165 166 167 168 169
  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;
}

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

175 176 177
void deletes_set_conditions(Deletes *deletes, Condition conditions[], size_t condition_num)
{
  assert(condition_num <= sizeof(deletes->conditions) / sizeof(deletes->conditions[0]));
羽飞's avatar
羽飞 已提交
178 179 180 181 182
  for (size_t i = 0; i < condition_num; i++) {
    deletes->conditions[i] = conditions[i];
  }
  deletes->condition_num = condition_num;
}
183 184
void deletes_destroy(Deletes *deletes)
{
羽飞's avatar
羽飞 已提交
185 186 187 188 189 190 191 192
  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;
}

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

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

207 208
void updates_destroy(Updates *updates)
{
羽飞's avatar
羽飞 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221
  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;
}

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

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

void create_table_destroy(CreateTable *create_table)
{
羽飞's avatar
羽飞 已提交
234 235 236 237 238 239 240 241
  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;
}

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

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

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

void create_index_destroy(CreateIndex *create_index)
{
羽飞's avatar
羽飞 已提交
263 264 265 266 267 268 269 270 271
  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;
}

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

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

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

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

294 295
void load_data_init(LoadData *load_data, const char *relation_name, const char *file_name)
{
羽飞's avatar
羽飞 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308
  load_data->relation_name = strdup(relation_name);

  if (file_name[0] == '\'' || file_name[0] == '\"') {
    file_name++;
  }
  char *dup_file_name = strdup(file_name);
  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;
}

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

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

323 324
Query *query_create()
{
羽飞's avatar
羽飞 已提交
325 326 327 328 329 330 331 332 333 334
  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;
}

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

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

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

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

386 387
void query_destroy(Query *query)
{
羽飞's avatar
羽飞 已提交
388 389 390 391
  query_reset(query);
  free(query);
}
#ifdef __cplusplus
392 393
}  // extern "C"
#endif  // __cplusplus
羽飞's avatar
羽飞 已提交
394 395 396

////////////////////////////////////////////////////////////////////////////////

397
extern "C" int sql_parse(const char *st, Query *sqls);
羽飞's avatar
羽飞 已提交
398

399 400
RC parse(const char *st, Query *sqln)
{
羽飞's avatar
羽飞 已提交
401 402 403 404 405 406 407
  sql_parse(st, sqln);

  if (sqln->flag == SCF_ERROR)
    return SQL_SYNTAX;
  else
    return SUCCESS;
}