parse_defs.h 4.9 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
//

羽飞's avatar
羽飞 已提交
15
#pragma once
羽飞's avatar
羽飞 已提交
16 17

#include <stddef.h>
羽飞's avatar
羽飞 已提交
18 19 20
#include <memory>
#include <vector>
#include <string>
羽飞's avatar
羽飞 已提交
21 22 23 24 25 26 27

#define MAX_NUM 20
#define MAX_REL_NAME 20
#define MAX_ATTR_NAME 20
#define MAX_ERROR_MESSAGE 20
#define MAX_DATA 50

L
Longda Feng 已提交
28
// 属性结构体
羽飞's avatar
羽飞 已提交
29 30 31 32
struct RelAttr {
  std::string relation_name;   // relation name (may be NULL) 表名
  std::string attribute_name;  // attribute name              属性名
};
羽飞's avatar
羽飞 已提交
33

羽飞's avatar
羽飞 已提交
34
enum CompOp {
羽飞's avatar
羽飞 已提交
35 36 37 38 39 40 41
  EQUAL_TO,     //"="     0
  LESS_EQUAL,   //"<="    1
  NOT_EQUAL,    //"<>"    2
  LESS_THAN,    //"<"     3
  GREAT_EQUAL,  //">="    4
  GREAT_THAN,   //">"     5
  NO_OP
羽飞's avatar
羽飞 已提交
42
};
羽飞's avatar
羽飞 已提交
43

L
Longda Feng 已提交
44 45
// 属性值类型
enum AttrType {
羽飞's avatar
羽飞 已提交
46 47 48
  UNDEFINED,
  CHARS,
  INTS,
羽飞's avatar
羽飞 已提交
49 50 51
  FLOATS,
  BOOLEANS,
};
羽飞's avatar
羽飞 已提交
52

L
Longda Feng 已提交
53
// 属性值
羽飞's avatar
羽飞 已提交
54
struct Value {
L
Longda Feng 已提交
55 56 57 58
  AttrType type;  // type of value
  int int_value;
  float float_value;
  bool bool_value;
羽飞's avatar
羽飞 已提交
59 60 61 62 63
  std::string string_value;

  const char *data() const;
  int length();
};
羽飞's avatar
羽飞 已提交
64

羽飞's avatar
羽飞 已提交
65
struct Condition {
羽飞's avatar
羽飞 已提交
66 67 68 69 70 71 72 73 74
  int left_is_attr;    // TRUE if left-hand side is an attribute
                       // 1时,操作符左边是属性名,0时,是属性值
  Value left_value;    // left-hand side value if left_is_attr = FALSE
  RelAttr left_attr;   // left-hand side attribute
  CompOp comp;         // comparison operator
  int right_is_attr;   // TRUE if right-hand side is an attribute
                       // 1时,操作符右边是属性名,0时,是属性值
  RelAttr right_attr;  // right-hand side attribute if right_is_attr = TRUE 右边的属性
  Value right_value;   // right-hand side value if right_is_attr = FALSE
羽飞's avatar
羽飞 已提交
75
};
羽飞's avatar
羽飞 已提交
76 77

// struct of select
羽飞's avatar
羽飞 已提交
78
struct Selects {
L
Longda Feng 已提交
79
  std::vector<RelAttr> attributes;  // attributes in select clause
羽飞's avatar
羽飞 已提交
80 81 82
  std::vector<std::string> relations;
  std::vector<Condition> conditions;
};
羽飞's avatar
羽飞 已提交
83 84

// struct of insert
羽飞's avatar
羽飞 已提交
85
struct Inserts {
L
Longda Feng 已提交
86
  std::string relation_name;  // Relation to insert into
羽飞's avatar
羽飞 已提交
87 88
  std::vector<Value> values;
};
羽飞's avatar
羽飞 已提交
89 90

// struct of delete
羽飞's avatar
羽飞 已提交
91
struct Deletes {
L
Longda Feng 已提交
92
  std::string relation_name;  // Relation to delete from
羽飞's avatar
羽飞 已提交
93 94
  std::vector<Condition> conditions;
};
羽飞's avatar
羽飞 已提交
95 96

// struct of update
羽飞's avatar
羽飞 已提交
97
struct Updates {
L
Longda Feng 已提交
98 99 100
  std::string relation_name;   // Relation to update
  std::string attribute_name;  // Attribute to update
  Value value;                 // update value
羽飞's avatar
羽飞 已提交
101 102 103
  std::vector<Condition> conditions;
};

L
Longda Feng 已提交
104 105 106 107
struct AttrInfo {
  AttrType type;     // Type of attribute
  std::string name;  // Attribute name
  size_t length;     // Length of attribute
羽飞's avatar
羽飞 已提交
108
};
羽飞's avatar
羽飞 已提交
109 110

// struct of craete_table
羽飞's avatar
羽飞 已提交
111
struct CreateTable {
L
Longda Feng 已提交
112 113
  std::string relation_name;         // Relation name
  std::vector<AttrInfo> attr_infos;  // attributes
羽飞's avatar
羽飞 已提交
114
};
羽飞's avatar
羽飞 已提交
115 116

// struct of drop_table
羽飞's avatar
羽飞 已提交
117 118 119
struct DropTable {
  std::string relation_name;  // Relation name
};
羽飞's avatar
羽飞 已提交
120 121

// struct of create_index
羽飞's avatar
羽飞 已提交
122 123 124 125 126
struct CreateIndex {
  std::string index_name;      // Index name
  std::string relation_name;   // Relation name
  std::string attribute_name;  // Attribute name
};
羽飞's avatar
羽飞 已提交
127 128

// struct of  drop_index
羽飞's avatar
羽飞 已提交
129
struct DropIndex {
L
Longda Feng 已提交
130 131
  std::string index_name;     // Index name
  std::string relation_name;  // Relation name
羽飞's avatar
羽飞 已提交
132 133 134 135 136 137 138 139 140 141 142
};

struct DescTable {
  std::string relation_name;
};

struct LoadData {
  std::string relation_name;
  std::string file_name;
};

143
class Command;
羽飞's avatar
羽飞 已提交
144
struct Explain {
145
  std::unique_ptr<Command> cmd;
羽飞's avatar
羽飞 已提交
146 147
};

L
Longda Feng 已提交
148
struct Error {
羽飞's avatar
羽飞 已提交
149 150 151
  std::string error_msg;
  int line;
  int column;
羽飞's avatar
羽飞 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
};

// 修改yacc中相关数字编码为宏定义
enum SqlCommandFlag {
  SCF_ERROR = 0,
  SCF_SELECT,
  SCF_INSERT,
  SCF_UPDATE,
  SCF_DELETE,
  SCF_CREATE_TABLE,
  SCF_DROP_TABLE,
  SCF_CREATE_INDEX,
  SCF_DROP_INDEX,
  SCF_SYNC,
  SCF_SHOW_TABLES,
  SCF_DESC_TABLE,
  SCF_BEGIN,
  SCF_COMMIT,
羽飞's avatar
羽飞 已提交
170
  SCF_CLOG_SYNC,
羽飞's avatar
羽飞 已提交
171 172 173
  SCF_ROLLBACK,
  SCF_LOAD_DATA,
  SCF_HELP,
羽飞's avatar
羽飞 已提交
174 175
  SCF_EXIT,
  SCF_EXPLAIN,
羽飞's avatar
羽飞 已提交
176 177
};
// struct of flag and sql_struct
178
class Command {
羽飞's avatar
羽飞 已提交
179
public:
羽飞's avatar
羽飞 已提交
180
  enum SqlCommandFlag flag;
L
Longda Feng 已提交
181 182 183 184 185 186 187 188 189 190 191 192
  Error error;
  Selects selection;
  Inserts insertion;
  Deletes deletion;
  Updates update;
  CreateTable create_table;
  DropTable drop_table;
  CreateIndex create_index;
  DropIndex drop_index;
  DescTable desc_table;
  LoadData load_data;
  Explain explain;
羽飞's avatar
羽飞 已提交
193 194

public:
195 196
  Command();
  Command(enum SqlCommandFlag flag);
羽飞's avatar
羽飞 已提交
197
};
羽飞's avatar
羽飞 已提交
198

羽飞's avatar
羽飞 已提交
199 200 201 202
/**
 * 表示语法解析后的数据
 * 叫ParsedSqlNode 可能会更清晰一点
 */
L
Longda Feng 已提交
203
class ParsedSqlResult {
羽飞's avatar
羽飞 已提交
204
public:
205
  void add_command(std::unique_ptr<Command> command);
L
Longda Feng 已提交
206 207 208 209 210
  std::vector<std::unique_ptr<Command>> &commands()
  {
    return sql_commands_;
  }

羽飞's avatar
羽飞 已提交
211
private:
212
  std::vector<std::unique_ptr<Command>> sql_commands_;
羽飞's avatar
羽飞 已提交
213
};
羽飞's avatar
羽飞 已提交
214

羽飞's avatar
羽飞 已提交
215 216
const char *attr_type_to_string(AttrType type);
AttrType attr_type_from_string(const char *s);