use vibesql_types::SqlValue;
use crate::errors::ExecutorError;
pub fn year(args: &[SqlValue]) -> Result<SqlValue, ExecutorError> {
if args.len() != 1 {
return Err(ExecutorError::UnsupportedFeature(
"YEAR requires exactly 1 argument".to_string(),
));
}
match &args[0] {
SqlValue::Null => Ok(SqlValue::Null),
SqlValue::Date(d) => Ok(SqlValue::Integer(d.year as i64)),
SqlValue::Timestamp(ts) => Ok(SqlValue::Integer(ts.date.year as i64)),
_ => Err(ExecutorError::UnsupportedFeature(
"YEAR requires date or timestamp argument".to_string(),
)),
}
}
pub fn month(args: &[SqlValue]) -> Result<SqlValue, ExecutorError> {
if args.len() != 1 {
return Err(ExecutorError::UnsupportedFeature(
"MONTH requires exactly 1 argument".to_string(),
));
}
match &args[0] {
SqlValue::Null => Ok(SqlValue::Null),
SqlValue::Date(d) => Ok(SqlValue::Integer(d.month as i64)),
SqlValue::Timestamp(ts) => Ok(SqlValue::Integer(ts.date.month as i64)),
_ => Err(ExecutorError::UnsupportedFeature(
"MONTH requires date or timestamp argument".to_string(),
)),
}
}
pub fn day(args: &[SqlValue]) -> Result<SqlValue, ExecutorError> {
if args.len() != 1 {
return Err(ExecutorError::UnsupportedFeature(
"DAY requires exactly 1 argument".to_string(),
));
}
match &args[0] {
SqlValue::Null => Ok(SqlValue::Null),
SqlValue::Date(d) => Ok(SqlValue::Integer(d.day as i64)),
SqlValue::Timestamp(ts) => Ok(SqlValue::Integer(ts.date.day as i64)),
_ => Err(ExecutorError::UnsupportedFeature(
"DAY requires date or timestamp argument".to_string(),
)),
}
}
pub fn hour(args: &[SqlValue]) -> Result<SqlValue, ExecutorError> {
if args.len() != 1 {
return Err(ExecutorError::UnsupportedFeature(
"HOUR requires exactly 1 argument".to_string(),
));
}
match &args[0] {
SqlValue::Null => Ok(SqlValue::Null),
SqlValue::Time(t) => Ok(SqlValue::Integer(t.hour as i64)),
SqlValue::Timestamp(ts) => Ok(SqlValue::Integer(ts.time.hour as i64)),
_ => Err(ExecutorError::UnsupportedFeature(
"HOUR requires time or timestamp argument".to_string(),
)),
}
}
pub fn minute(args: &[SqlValue]) -> Result<SqlValue, ExecutorError> {
if args.len() != 1 {
return Err(ExecutorError::UnsupportedFeature(
"MINUTE requires exactly 1 argument".to_string(),
));
}
match &args[0] {
SqlValue::Null => Ok(SqlValue::Null),
SqlValue::Time(t) => Ok(SqlValue::Integer(t.minute as i64)),
SqlValue::Timestamp(ts) => Ok(SqlValue::Integer(ts.time.minute as i64)),
_ => Err(ExecutorError::UnsupportedFeature(
"MINUTE requires time or timestamp argument".to_string(),
)),
}
}
pub fn second(args: &[SqlValue]) -> Result<SqlValue, ExecutorError> {
if args.len() != 1 {
return Err(ExecutorError::UnsupportedFeature(
"SECOND requires exactly 1 argument".to_string(),
));
}
match &args[0] {
SqlValue::Null => Ok(SqlValue::Null),
SqlValue::Time(t) => Ok(SqlValue::Integer(t.second as i64)),
SqlValue::Timestamp(ts) => Ok(SqlValue::Integer(ts.time.second as i64)),
_ => Err(ExecutorError::UnsupportedFeature(
"SECOND requires time or timestamp argument".to_string(),
)),
}
}