parse_defs.h 4.9 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11
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
struct RelAttr 
{
羽飞's avatar
羽飞 已提交
31 32 33
  std::string relation_name;   // relation name (may be NULL) 表名
  std::string attribute_name;  // attribute name              属性名
};
羽飞's avatar
羽飞 已提交
34

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

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

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

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

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

// struct of select
羽飞's avatar
羽飞 已提交
83 84
struct Selects
{
L
Longda Feng 已提交
85
  std::vector<RelAttr> attributes;  // attributes in select clause
羽飞's avatar
羽飞 已提交
86 87 88
  std::vector<std::string> relations;
  std::vector<Condition> conditions;
};
羽飞's avatar
羽飞 已提交
89 90

// struct of insert
羽飞's avatar
羽飞 已提交
91 92
struct Inserts
{
L
Longda Feng 已提交
93
  std::string relation_name;  // Relation to insert into
羽飞's avatar
羽飞 已提交
94 95
  std::vector<Value> values;
};
羽飞's avatar
羽飞 已提交
96 97

// struct of delete
羽飞's avatar
羽飞 已提交
98 99
struct Deletes
{
L
Longda Feng 已提交
100
  std::string relation_name;  // Relation to delete from
羽飞's avatar
羽飞 已提交
101 102
  std::vector<Condition> conditions;
};
羽飞's avatar
羽飞 已提交
103 104

// struct of update
羽飞's avatar
羽飞 已提交
105 106
struct Updates
{
L
Longda Feng 已提交
107 108 109
  std::string relation_name;   // Relation to update
  std::string attribute_name;  // Attribute to update
  Value value;                 // update value
羽飞's avatar
羽飞 已提交
110 111 112
  std::vector<Condition> conditions;
};

羽飞's avatar
羽飞 已提交
113 114
struct AttrInfo
{
L
Longda Feng 已提交
115 116 117
  AttrType type;     // Type of attribute
  std::string name;  // Attribute name
  size_t length;     // Length of attribute
羽飞's avatar
羽飞 已提交
118
};
羽飞's avatar
羽飞 已提交
119 120

// struct of craete_table
羽飞's avatar
羽飞 已提交
121 122
struct CreateTable
{
L
Longda Feng 已提交
123 124
  std::string relation_name;         // Relation name
  std::vector<AttrInfo> attr_infos;  // attributes
羽飞's avatar
羽飞 已提交
125
};
羽飞's avatar
羽飞 已提交
126 127

// struct of drop_table
羽飞's avatar
羽飞 已提交
128 129
struct DropTable
{
羽飞's avatar
羽飞 已提交
130 131
  std::string relation_name;  // Relation name
};
羽飞's avatar
羽飞 已提交
132 133

// struct of create_index
羽飞's avatar
羽飞 已提交
134 135
struct CreateIndex
{
羽飞's avatar
羽飞 已提交
136 137 138 139
  std::string index_name;      // Index name
  std::string relation_name;   // Relation name
  std::string attribute_name;  // Attribute name
};
羽飞's avatar
羽飞 已提交
140 141

// struct of  drop_index
羽飞's avatar
羽飞 已提交
142 143
struct DropIndex
{
L
Longda Feng 已提交
144 145
  std::string index_name;     // Index name
  std::string relation_name;  // Relation name
羽飞's avatar
羽飞 已提交
146 147
};

羽飞's avatar
羽飞 已提交
148 149
struct DescTable
{
羽飞's avatar
羽飞 已提交
150 151 152
  std::string relation_name;
};

羽飞's avatar
羽飞 已提交
153 154
struct LoadData
{
羽飞's avatar
羽飞 已提交
155 156 157 158
  std::string relation_name;
  std::string file_name;
};

159
class Command;
羽飞's avatar
羽飞 已提交
160 161
struct Explain
{
162
  std::unique_ptr<Command> cmd;
羽飞's avatar
羽飞 已提交
163 164
};

羽飞's avatar
羽飞 已提交
165 166
struct Error
{
羽飞's avatar
羽飞 已提交
167 168 169
  std::string error_msg;
  int line;
  int column;
羽飞's avatar
羽飞 已提交
170 171 172
};

// 修改yacc中相关数字编码为宏定义
羽飞's avatar
羽飞 已提交
173 174
enum SqlCommandFlag
{
羽飞's avatar
羽飞 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188
  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
羽飞 已提交
189
  SCF_CLOG_SYNC,
羽飞's avatar
羽飞 已提交
190 191 192
  SCF_ROLLBACK,
  SCF_LOAD_DATA,
  SCF_HELP,
羽飞's avatar
羽飞 已提交
193 194
  SCF_EXIT,
  SCF_EXPLAIN,
羽飞's avatar
羽飞 已提交
195 196
};
// struct of flag and sql_struct
羽飞's avatar
羽飞 已提交
197 198
class Command
{
羽飞's avatar
羽飞 已提交
199
public:
羽飞's avatar
羽飞 已提交
200
  enum SqlCommandFlag flag;
L
Longda Feng 已提交
201 202 203 204 205 206 207 208 209 210 211 212
  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
羽飞 已提交
213 214

public:
215 216
  Command();
  Command(enum SqlCommandFlag flag);
羽飞's avatar
羽飞 已提交
217
};
羽飞's avatar
羽飞 已提交
218

羽飞's avatar
羽飞 已提交
219 220 221 222
/**
 * 表示语法解析后的数据
 * 叫ParsedSqlNode 可能会更清晰一点
 */
羽飞's avatar
羽飞 已提交
223 224
class ParsedSqlResult
{
羽飞's avatar
羽飞 已提交
225
public:
226
  void add_command(std::unique_ptr<Command> command);
L
Longda Feng 已提交
227 228 229 230 231
  std::vector<std::unique_ptr<Command>> &commands()
  {
    return sql_commands_;
  }

羽飞's avatar
羽飞 已提交
232
private:
233
  std::vector<std::unique_ptr<Command>> sql_commands_;
羽飞's avatar
羽飞 已提交
234
};
羽飞's avatar
羽飞 已提交
235

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