parse.cpp 10.4 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);

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

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));
}
52 53
void value_init_string(Value *value, const char *v)
{
羽飞's avatar
羽飞 已提交
54 55 56
  value->type = CHARS;
  value->data = strdup(v);
}
57 58
void value_destroy(Value *value)
{
羽飞's avatar
羽飞 已提交
59 60 61 62 63
  value->type = UNDEFINED;
  free(value->data);
  value->data = nullptr;
}

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

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

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

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

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

146 147 148
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
羽飞 已提交
149 150 151 152 153 154 155

  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;
}
156 157
void inserts_destroy(Inserts *inserts)
{
羽飞's avatar
羽飞 已提交
158 159 160 161 162 163 164 165 166
  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;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

291 292
void load_data_init(LoadData *load_data, const char *relation_name, const char *file_name)
{
羽飞's avatar
羽飞 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305
  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;
}

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

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

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

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

361
    } break;
羽飞's avatar
羽飞 已提交
362
    case SCF_SHOW_TABLES:
363
      break;
羽飞's avatar
羽飞 已提交
364 365 366

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

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

383 384
void query_destroy(Query *query)
{
羽飞's avatar
羽飞 已提交
385 386 387 388 389 390
  query_reset(query);
  free(query);
}

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

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

393 394
RC parse(const char *st, Query *sqln)
{
羽飞's avatar
羽飞 已提交
395 396 397 398 399 400
  sql_parse(st, sqln);

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