parse.cpp 2.2 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 15 16 17 18 19 20 21
//

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

RC parse(char *st, Query *sqln);

羽飞'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 44 45 46 47 48
  switch (type) {
    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
羽飞 已提交
49
  }
羽飞's avatar
羽飞 已提交
50
  return nullptr;
羽飞's avatar
羽飞 已提交
51 52
}

羽飞's avatar
羽飞 已提交
53
int Value::length()
54
{
羽飞's avatar
羽飞 已提交
55 56 57 58 59 60
  switch (type) {
    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
羽飞 已提交
61
  }
羽飞's avatar
羽飞 已提交
62
  return 0;
羽飞's avatar
羽飞 已提交
63 64
}

羽飞's avatar
羽飞 已提交
65 66
Query::Query()
    : flag(SCF_ERROR)
67
{
羽飞's avatar
羽飞 已提交
68 69
}

羽飞's avatar
羽飞 已提交
70 71 72
Query::Query(enum SqlCommandFlag _flag)
    : flag(_flag)
{}
羽飞's avatar
羽飞 已提交
73

羽飞's avatar
羽飞 已提交
74
void ParsedSqlResult::add_command(std::unique_ptr<Query> command)
羽飞's avatar
羽飞 已提交
75
{
羽飞's avatar
羽飞 已提交
76
  sql_commands_.emplace_back(std::move(command));
羽飞's avatar
羽飞 已提交
77 78
}

羽飞's avatar
羽飞 已提交
79 80
////////////////////////////////////////////////////////////////////////////////

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

羽飞's avatar
羽飞 已提交
83
RC parse(const char *st, ParsedSqlResult *sql_result)
84
{
羽飞's avatar
羽飞 已提交
85 86
  sql_parse(st, sql_result);
  return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
87
}