tuple_cell.h 2.6 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 12 13 14
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
#pragma once

#include <iostream>
羽飞's avatar
羽飞 已提交
18 19
#include "storage/table/table.h"
#include "storage/field/field_meta.h"
羽飞's avatar
羽飞 已提交
20

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做合并
 */
羽飞's avatar
羽飞 已提交
49 50
class TupleCell 
{
L
Longda Feng 已提交
51
public:
羽飞's avatar
羽飞 已提交
52
  TupleCell() = default;
L
Longda Feng 已提交
53

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

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

L
Longda Feng 已提交
64 65 66 67
  void set_type(AttrType type)
  {
    this->attr_type_ = type;
  }
羽飞's avatar
羽飞 已提交
68
  void set_data(char *data, int length);
L
Longda Feng 已提交
69 70 71 72
  void set_data(const char *data, int length)
  {
    this->set_data(const_cast<char *>(data), length);
  }
羽飞's avatar
羽飞 已提交
73 74 75 76 77
  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
羽飞 已提交
78 79 80 81 82

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

  int compare(const TupleCell &other) const;

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

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

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

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

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