parse_defs.h 5.0 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 28

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

//属性结构体
羽飞'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 44

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

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

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

羽飞's avatar
羽飞 已提交
66
struct Condition {
羽飞's avatar
羽飞 已提交
67 68 69 70 71 72 73 74 75
  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
羽飞 已提交
76
};
羽飞's avatar
羽飞 已提交
77 78

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

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

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

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

struct AttrInfo
{
羽飞's avatar
羽飞 已提交
107
  AttrType type;  // Type of attribute
羽飞's avatar
羽飞 已提交
108
  std::string name; // Attribute name
羽飞's avatar
羽飞 已提交
109
  size_t length;  // Length of attribute
羽飞's avatar
羽飞 已提交
110
};
羽飞's avatar
羽飞 已提交
111 112

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

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

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

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

struct DescTable {
  std::string relation_name;
};

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

145
class Command;
羽飞's avatar
羽飞 已提交
146
struct Explain {
147
  std::unique_ptr<Command> cmd;
羽飞's avatar
羽飞 已提交
148 149 150 151 152 153 154
};

struct Error
{
  std::string error_msg;
  int line;
  int column;
羽飞's avatar
羽飞 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
};

// 修改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
羽飞 已提交
173
  SCF_CLOG_SYNC,
羽飞's avatar
羽飞 已提交
174 175 176
  SCF_ROLLBACK,
  SCF_LOAD_DATA,
  SCF_HELP,
羽飞's avatar
羽飞 已提交
177 178
  SCF_EXIT,
  SCF_EXPLAIN,
羽飞's avatar
羽飞 已提交
179 180
};
// struct of flag and sql_struct
181
class Command {
羽飞's avatar
羽飞 已提交
182
public:
羽飞's avatar
羽飞 已提交
183
  enum SqlCommandFlag flag;
羽飞's avatar
羽飞 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197
  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;

public:
198 199
  Command();
  Command(enum SqlCommandFlag flag);
羽飞's avatar
羽飞 已提交
200
};
羽飞's avatar
羽飞 已提交
201

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

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