package compute
import (
"fmt"
)
type ConstType int
const (
ConstTypeInvalid ConstType = iota
ConstTypeInteger
ConstTypeDecimal
ConstTypeString
ConstTypeFloat
ConstTypeDate
ConstTypeInterval
ConstTypeBoolean
ConstTypeNull
)
type ConstValue struct {
Type ConstType
Integer int64
Decimal string
String string
Float float64
Date string
Interval struct {
Value int64
Unit string
}
Boolean bool
}
func NewIntegerConst(value int64) ConstValue {
return ConstValue{
Type: ConstTypeInteger,
Integer: value,
}
}
func NewDecimalConst(value string) ConstValue {
return ConstValue{
Type: ConstTypeDecimal,
Decimal: value,
}
}
func NewStringConst(value string) ConstValue {
return ConstValue{
Type: ConstTypeString,
String: value,
}
}
func NewFloatConst(value float64) ConstValue {
return ConstValue{
Type: ConstTypeFloat,
Float: value,
}
}
func NewDateConst(value string) ConstValue {
return ConstValue{
Type: ConstTypeDate,
Date: value,
}
}
func NewIntervalConst(value int64, unit string) ConstValue {
return ConstValue{
Type: ConstTypeInterval,
Interval: struct {
Value int64
Unit string
}{value, unit},
}
}
func NewBooleanConst(value bool) ConstValue {
return ConstValue{
Type: ConstTypeBoolean,
Boolean: value,
}
}
func NewNullConst() ConstValue {
return ConstValue{
Type: ConstTypeNull,
}
}
func (c ConstValue) GetInteger() (int64, error) {
if c.Type != ConstTypeInteger {
return 0, fmt.Errorf("not an integer constant")
}
return c.Integer, nil
}
func (c ConstValue) GetDecimal() (string, error) {
if c.Type != ConstTypeDecimal {
return "", fmt.Errorf("not a decimal constant")
}
return c.Decimal, nil
}
func (c ConstValue) GetString() (string, error) {
if c.Type != ConstTypeString {
return "", fmt.Errorf("not a string constant")
}
return c.String, nil
}
func (c ConstValue) GetFloat() (float64, error) {
if c.Type != ConstTypeFloat {
return 0, fmt.Errorf("not a float constant")
}
return c.Float, nil
}
func (c ConstValue) GetDate() (string, error) {
if c.Type != ConstTypeDate {
return "", fmt.Errorf("not a date constant")
}
return c.Date, nil
}
func (c ConstValue) GetInterval() (int64, string, error) {
if c.Type != ConstTypeInterval {
return 0, "", fmt.Errorf("not an interval constant")
}
return c.Interval.Value, c.Interval.Unit, nil
}
func (c ConstValue) GetBoolean() (bool, error) {
if c.Type != ConstTypeBoolean {
return false, fmt.Errorf("not a boolean constant")
}
return c.Boolean, nil
}
func (c ConstValue) IsNull() bool {
return c.Type == ConstTypeNull
}
func (c ConstValue) equal(o ConstValue) bool {
if c.Type != o.Type {
return false
}
switch c.Type {
case ConstTypeInteger:
return c.Integer == o.Integer
case ConstTypeDecimal:
return c.Decimal == o.Decimal
case ConstTypeString:
return c.String == o.String
case ConstTypeFloat:
return c.Float == o.Float
case ConstTypeDate:
return c.Date == o.Date
case ConstTypeInterval:
return c.Interval.Value == o.Interval.Value && c.Interval.Unit == o.Interval.Unit
case ConstTypeBoolean:
return c.Boolean == o.Boolean
case ConstTypeNull:
return true
default:
return true
}
}
func (c ConstValue) copy() ConstValue {
return c
}