parse.cpp 2.2 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. */

//
L
Longda Feng 已提交
12
// Created by Meiyi
羽飞's avatar
羽飞 已提交
13 14 15 16 17 18
//

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

19
RC parse(char *st, Command *sqln);
羽飞's avatar
羽飞 已提交
20

羽飞's avatar
羽飞 已提交
21
const char *ATTR_TYPE_NAME[] = {"undefined", "chars", "ints", "floats", "booleans"};
羽飞's avatar
羽飞 已提交
22

羽飞's avatar
羽飞 已提交
23
const char *attr_type_to_string(AttrType type)
24
{
羽飞's avatar
羽飞 已提交
25 26
  if (type >= UNDEFINED && type <= FLOATS) {
    return ATTR_TYPE_NAME[type];
羽飞's avatar
羽飞 已提交
27
  }
羽飞's avatar
羽飞 已提交
28
  return "unknown";
羽飞's avatar
羽飞 已提交
29
}
羽飞's avatar
羽飞 已提交
30
AttrType attr_type_from_string(const char *s)
31
{
羽飞's avatar
羽飞 已提交
32 33 34 35
  for (unsigned int i = 0; i < sizeof(ATTR_TYPE_NAME) / sizeof(ATTR_TYPE_NAME[0]); i++) {
    if (0 == strcmp(ATTR_TYPE_NAME[i], s)) {
      return (AttrType)i;
    }
羽飞's avatar
羽飞 已提交
36
  }
羽飞's avatar
羽飞 已提交
37
  return UNDEFINED;
羽飞's avatar
羽飞 已提交
38 39
}

羽飞's avatar
羽飞 已提交
40
const char *Value::data() const
41
{
羽飞's avatar
羽飞 已提交
42
  switch (type) {
L
Longda Feng 已提交
43 44 45 46 47 48 49 50 51 52
    case INTS:
      return (const char *)&int_value;
    case FLOATS:
      return (const char *)&float_value;
    case BOOLEANS:
      return (const char *)&bool_value;
    case CHARS:
      return (const char *)string_value.data();
    case UNDEFINED:
      return nullptr;
羽飞's avatar
羽飞 已提交
53
  }
羽飞's avatar
羽飞 已提交
54
  return nullptr;
羽飞's avatar
羽飞 已提交
55 56
}

羽飞's avatar
羽飞 已提交
57
int Value::length()
58
{
羽飞's avatar
羽飞 已提交
59
  switch (type) {
L
Longda Feng 已提交
60 61 62 63 64 65 66 67 68 69
    case INTS:
      return sizeof(int_value);
    case FLOATS:
      return sizeof(float_value);
    case BOOLEANS:
      return sizeof(bool_value);
    case CHARS:
      return string_value.size();
    case UNDEFINED:
      return 0;
羽飞's avatar
羽飞 已提交
70
  }
羽飞's avatar
羽飞 已提交
71
  return 0;
羽飞's avatar
羽飞 已提交
72 73
}

L
Longda Feng 已提交
74 75
Command::Command() : flag(SCF_ERROR)
{}
羽飞's avatar
羽飞 已提交
76

羽飞's avatar
羽飞 已提交
77
Command::Command(SqlCommandFlag _flag) : flag(_flag)
羽飞's avatar
羽飞 已提交
78
{}
羽飞's avatar
羽飞 已提交
79

80
void ParsedSqlResult::add_command(std::unique_ptr<Command> command)
羽飞's avatar
羽飞 已提交
81
{
羽飞's avatar
羽飞 已提交
82
  sql_commands_.emplace_back(std::move(command));
羽飞's avatar
羽飞 已提交
83 84
}

羽飞's avatar
羽飞 已提交
85 86
////////////////////////////////////////////////////////////////////////////////

羽飞's avatar
羽飞 已提交
87
int sql_parse(const char *st, ParsedSqlResult *sql_result);
羽飞's avatar
羽飞 已提交
88

羽飞's avatar
羽飞 已提交
89
RC parse(const char *st, ParsedSqlResult *sql_result)
90
{
羽飞's avatar
羽飞 已提交
91 92
  sql_parse(st, sql_result);
  return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
93
}