tuple_cell.h 2.6 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/6/7.
//

羽飞's avatar
羽飞 已提交
15 16 17 18 19 20
#pragma once

#include <iostream>
#include "storage/common/table.h"
#include "storage/common/field_meta.h"

L
Longda Feng 已提交
21 22
class TupleCellSpec {
public:
羽飞's avatar
羽飞 已提交
23 24 25
  TupleCellSpec(const char *table_name, const char *field_name, const char *alias = nullptr);
  TupleCellSpec(const char *alias);

L
Longda Feng 已提交
26 27 28 29 30 31 32 33 34 35 36 37
  const char *table_name() const
  {
    return table_name_.c_str();
  }
  const char *field_name() const
  {
    return field_name_.c_str();
  }
  const char *alias() const
  {
    return alias_.c_str();
  }
羽飞's avatar
羽飞 已提交
38 39 40 41 42 43 44

private:
  std::string table_name_;
  std::string field_name_;
  std::string alias_;
};

羽飞's avatar
羽飞 已提交
45 46 47 48
/**
 * 表示tuple中某个元素的值
 * @note 可以与value做合并
 */
L
Longda Feng 已提交
49 50
class TupleCell {
public:
羽飞's avatar
羽飞 已提交
51
  TupleCell() = default;
L
Longda Feng 已提交
52 53

  TupleCell(FieldMeta *meta, char *data, int length = 4) : TupleCell(meta->type(), data)
羽飞's avatar
羽飞 已提交
54
  {}
L
Longda Feng 已提交
55
  TupleCell(AttrType attr_type, char *data, int length = 4) : attr_type_(attr_type)
羽飞's avatar
羽飞 已提交
56 57 58 59 60 61
  {
    this->set_data(data, length);
  }

  TupleCell(const TupleCell &other) = default;
  TupleCell &operator=(const TupleCell &other) = default;
羽飞's avatar
羽飞 已提交
62

L
Longda Feng 已提交
63 64 65 66
  void set_type(AttrType type)
  {
    this->attr_type_ = type;
  }
羽飞's avatar
羽飞 已提交
67
  void set_data(char *data, int length);
L
Longda Feng 已提交
68 69 70 71
  void set_data(const char *data, int length)
  {
    this->set_data(const_cast<char *>(data), length);
  }
羽飞's avatar
羽飞 已提交
72 73 74 75 76
  void set_int(int val);
  void set_float(float val);
  void set_boolean(bool val);
  void set_string(const char *s, int len = 0);
  void set_value(const Value &value);
羽飞's avatar
羽飞 已提交
77 78 79 80 81

  void to_string(std::ostream &os) const;

  int compare(const TupleCell &other) const;

羽飞's avatar
羽飞 已提交
82
  const char *data() const;
L
Longda Feng 已提交
83 84 85 86
  int length() const
  {
    return length_;
  }
羽飞's avatar
羽飞 已提交
87

羽飞's avatar
羽飞 已提交
88 89 90 91
  AttrType attr_type() const
  {
    return attr_type_;
  }
羽飞's avatar
羽飞 已提交
92

羽飞's avatar
羽飞 已提交
93 94 95 96 97 98 99 100 101
public:
  /**
   * 获取对应的值
   * 如果当前的类型与期望获取的类型不符,就会执行转换操作
   */
  int get_int() const;
  float get_float() const;
  std::string get_string() const;
  bool get_boolean() const;
L
Longda Feng 已提交
102

羽飞's avatar
羽飞 已提交
103 104
private:
  AttrType attr_type_ = UNDEFINED;
羽飞's avatar
羽飞 已提交
105 106 107
  int length_;

  union {
L
Longda Feng 已提交
108
    int int_value_;
羽飞's avatar
羽飞 已提交
109
    float float_value_;
L
Longda Feng 已提交
110
    bool bool_value_;
羽飞's avatar
羽飞 已提交
111 112
  } num_value_;
  std::string str_value_;
羽飞's avatar
羽飞 已提交
113
};