expression.h 2.1 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

//
// Created by Wangyunlai on 2022/07/05.
//

羽飞's avatar
羽飞 已提交
15 16
#pragma once

羽飞's avatar
羽飞 已提交
17
#include <string.h>
羽飞's avatar
羽飞 已提交
18 19 20 21 22
#include "storage/common/field.h"
#include "sql/expr/tuple_cell.h"

class Tuple;

羽飞's avatar
羽飞 已提交
23 24 25 26 27 28
enum class ExprType {
  NONE,
  FIELD,
  VALUE,
};

羽飞's avatar
羽飞 已提交
29 30 31 32 33 34 35
class Expression
{
public: 
  Expression() = default;
  virtual ~Expression() = default;
  
  virtual RC get_value(const Tuple &tuple, TupleCell &cell) const = 0;
羽飞's avatar
羽飞 已提交
36
  virtual ExprType type() const = 0;
羽飞's avatar
羽飞 已提交
37 38 39 40 41 42 43 44 45 46 47
};

class FieldExpr : public Expression
{
public:
  FieldExpr() = default;
  FieldExpr(const Table *table, const FieldMeta *field) : field_(table, field)
  {}

  virtual ~FieldExpr() = default;

羽飞's avatar
羽飞 已提交
48 49 50 51 52
  ExprType type() const override
  {
    return ExprType::FIELD;
  }

羽飞's avatar
羽飞 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  Field &field()
  {
    return field_;
  }

  const Field &field() const
  {
    return field_;
  }

  const char *table_name() const
  {
    return field_.table_name();
  }

  const char *field_name() const
  {
    return field_.field_name();
  }

  RC get_value(const Tuple &tuple, TupleCell &cell) const override;
private:
  Field field_;
};

class ValueExpr : public Expression
{
public:
  ValueExpr() = default;
羽飞's avatar
羽飞 已提交
82
  ValueExpr(const Value &value) : tuple_cell_(value.type, (char *)value.data)
羽飞's avatar
羽飞 已提交
83 84 85 86 87
  {
    if (value.type == CHARS) {
      tuple_cell_.set_length(strlen((const char *)value.data));
    }
  }
羽飞's avatar
羽飞 已提交
88 89 90 91

  virtual ~ValueExpr() = default;

  RC get_value(const Tuple &tuple, TupleCell & cell) const override;
羽飞's avatar
羽飞 已提交
92 93 94 95 96 97 98 99
  ExprType type() const override
  {
    return ExprType::VALUE;
  }

  void get_tuple_cell(TupleCell &cell) const {
    cell = tuple_cell_;
  }
羽飞's avatar
羽飞 已提交
100 101

private:
羽飞's avatar
羽飞 已提交
102
  TupleCell tuple_cell_;
羽飞's avatar
羽飞 已提交
103
};