expression.h 2.0 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 17 18 19 20 21
#pragma once

#include "storage/common/field.h"
#include "sql/expr/tuple_cell.h"

class Tuple;

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

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

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

  virtual ~FieldExpr() = default;

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

羽飞's avatar
羽飞 已提交
52 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
  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
羽飞 已提交
81
  ValueExpr(const Value &value) : tuple_cell_(value.type, (char *)value.data)
羽飞's avatar
羽飞 已提交
82 83 84 85 86
  {}

  virtual ~ValueExpr() = default;

  RC get_value(const Tuple &tuple, TupleCell & cell) const override;
羽飞's avatar
羽飞 已提交
87 88 89 90 91 92 93 94
  ExprType type() const override
  {
    return ExprType::VALUE;
  }

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

private:
羽飞's avatar
羽飞 已提交
97
  TupleCell tuple_cell_;
羽飞's avatar
羽飞 已提交
98
};