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"

羽飞's avatar
羽飞 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
class TupleCellSpec
{
public: 
  TupleCellSpec(const char *table_name, const char *field_name, const char *alias = nullptr);
  TupleCellSpec(const char *alias);

  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(); }

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

羽飞's avatar
羽飞 已提交
37 38 39 40
/**
 * 表示tuple中某个元素的值
 * @note 可以与value做合并
 */
羽飞's avatar
羽飞 已提交
41 42 43 44 45
class TupleCell
{
public: 
  TupleCell() = default;
  
羽飞's avatar
羽飞 已提交
46
  TupleCell(FieldMeta *meta, char *data, int length = 4)
羽飞's avatar
羽飞 已提交
47 48
    : TupleCell(meta->type(), data)
  {}
羽飞's avatar
羽飞 已提交
49 50 51 52 53 54 55 56
  TupleCell(AttrType attr_type, char *data, int length = 4)
    : attr_type_(attr_type)
  {
    this->set_data(data, length);
  }

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

  void set_type(AttrType type) { this->attr_type_ = type; }
羽飞's avatar
羽飞 已提交
59 60 61 62 63 64 65
  void set_data(char *data, int length);
  void set_data(const char *data, int length) { this->set_data(const_cast<char *>(data), length); }
  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
羽飞 已提交
66 67 68 69 70

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

  int compare(const TupleCell &other) const;

羽飞's avatar
羽飞 已提交
71
  const char *data() const;
羽飞's avatar
羽飞 已提交
72 73
  int length() const { return length_; }

羽飞's avatar
羽飞 已提交
74 75 76 77
  AttrType attr_type() const
  {
    return attr_type_;
  }
羽飞's avatar
羽飞 已提交
78

羽飞's avatar
羽飞 已提交
79 80 81 82 83 84 85 86 87 88
public:
  /**
   * 获取对应的值
   * 如果当前的类型与期望获取的类型不符,就会执行转换操作
   */
  int get_int() const;
  float get_float() const;
  std::string get_string() const;
  bool get_boolean() const;
  
羽飞's avatar
羽飞 已提交
89 90
private:
  AttrType attr_type_ = UNDEFINED;
羽飞's avatar
羽飞 已提交
91 92 93 94 95 96 97 98
  int length_;

  union {
    int   int_value_;
    float float_value_;
    bool  bool_value_;
  } num_value_;
  std::string str_value_;
羽飞's avatar
羽飞 已提交
99
};