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 19
//

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

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

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

羽飞's avatar
羽飞 已提交
24
const char *attr_type_to_string(AttrType type)
25
{
羽飞's avatar
羽飞 已提交
26 27
  if (type >= UNDEFINED && type <= FLOATS) {
    return ATTR_TYPE_NAME[type];
羽飞's avatar
羽飞 已提交
28
  }
羽飞's avatar
羽飞 已提交
29
  return "unknown";
羽飞's avatar
羽飞 已提交
30
}
羽飞's avatar
羽飞 已提交
31
AttrType attr_type_from_string(const char *s)
32
{
羽飞's avatar
羽飞 已提交
33 34 35 36
  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
羽飞 已提交
37
  }
羽飞's avatar
羽飞 已提交
38
  return UNDEFINED;
羽飞's avatar
羽飞 已提交
39 40
}

羽飞's avatar
羽飞 已提交
41
const char *Value::data() const
42
{
羽飞's avatar
羽飞 已提交
43
  switch (type) {
L
Longda Feng 已提交
44 45 46 47 48 49 50 51 52 53
    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
羽飞 已提交
54
  }
羽飞's avatar
羽飞 已提交
55
  return nullptr;
羽飞's avatar
羽飞 已提交
56 57
}

羽飞's avatar
羽飞 已提交
58
int Value::length()
59
{
羽飞's avatar
羽飞 已提交
60
  switch (type) {
L
Longda Feng 已提交
61 62 63 64 65 66 67 68 69 70
    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
羽飞 已提交
71
  }
羽飞's avatar
羽飞 已提交
72
  return 0;
羽飞's avatar
羽飞 已提交
73 74
}

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

L
Longda Feng 已提交
78
Command::Command(enum SqlCommandFlag _flag) : flag(_flag)
羽飞's avatar
羽飞 已提交
79
{}
羽飞's avatar
羽飞 已提交
80

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

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

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

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