MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
data_type.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11#pragma once
12
13#include "common/lang/array.h"
14#include "common/lang/memory.h"
15#include "common/lang/string.h"
16#include "common/sys/rc.h"
17#include "common/type/attr_type.h"
18
19class Value;
20
28{
29public:
30 explicit DataType(AttrType attr_type) : attr_type_(attr_type) {}
31
32 virtual ~DataType() = default;
33
34 inline static DataType *type_instance(AttrType attr_type)
35 {
36 return type_instances_.at(static_cast<int>(attr_type)).get();
37 }
38
39 inline AttrType get_attr_type() const { return attr_type_; }
40
48 virtual int compare(const Value &left, const Value &right) const { return INT32_MAX; }
49
53 virtual RC add(const Value &left, const Value &right, Value &result) const { return RC::UNSUPPORTED; }
54
58 virtual RC subtract(const Value &left, const Value &right, Value &result) const { return RC::UNSUPPORTED; }
59
63 virtual RC multiply(const Value &left, const Value &right, Value &result) const { return RC::UNSUPPORTED; }
64
68 virtual RC divide(const Value &left, const Value &right, Value &result) const { return RC::UNSUPPORTED; }
69
73 virtual RC negative(const Value &val, Value &result) const { return RC::UNSUPPORTED; }
74
78 virtual RC cast_to(const Value &val, AttrType type, Value &result) const { return RC::UNSUPPORTED; }
79
83 virtual RC to_string(const Value &val, string &result) const { return RC::UNSUPPORTED; }
84
88 virtual int cast_cost(AttrType type)
89 {
90 if (type == attr_type_) {
91 return 0;
92 }
93 return INT32_MAX;
94 }
95
96 virtual RC set_value_from_str(Value &val, const string &data) const { return RC::UNSUPPORTED; }
97
98protected:
99 AttrType attr_type_;
100
101 static array<unique_ptr<DataType>, static_cast<int>(AttrType::MAXTYPE)> type_instances_;
102};
Definition: data_type.h:28
virtual RC to_string(const Value &val, string &result) const
将 val 转换为 string,并将结果保存到 result 中
Definition: data_type.h:83
virtual RC divide(const Value &left, const Value &right, Value &result) const
计算 left / right,并将结果保存到 result 中
Definition: data_type.h:68
virtual RC cast_to(const Value &val, AttrType type, Value &result) const
将 val 转换为 type 类型,并将结果保存到 result 中
Definition: data_type.h:78
virtual RC subtract(const Value &left, const Value &right, Value &result) const
计算 left - right,并将结果保存到 result 中
Definition: data_type.h:58
virtual int compare(const Value &left, const Value &right) const
Definition: data_type.h:48
virtual RC multiply(const Value &left, const Value &right, Value &result) const
计算 left * right,并将结果保存到 result 中
Definition: data_type.h:63
virtual RC add(const Value &left, const Value &right, Value &result) const
计算 left + right,并将结果保存到 result 中
Definition: data_type.h:53
virtual RC negative(const Value &val, Value &result) const
计算 -val,并将结果保存到 result 中
Definition: data_type.h:73
virtual int cast_cost(AttrType type)
计算从 type 到 attr_type 的隐式转换的 cost,如果无法转换,返回 INT32_MAX
Definition: data_type.h:88
属性的值
Definition: value.h:30