pub mod alter_expr;
pub mod create_expr;
pub mod insert_expr;
pub mod select_expr;
use std::fmt;
use datatypes::data_type::ConcreteDataType;
use datatypes::types::TimestampType;
use datatypes::value::Value;
use rand::Rng;
use crate::error::Error;
use crate::ir::create_expr::ColumnOption;
use crate::ir::{AlterTableExpr, CreateTableExpr, Ident};
pub type CreateTableExprGenerator<R> =
Box<dyn Generator<CreateTableExpr, R, Error = Error> + Sync + Send>;
pub type AlterTableExprGenerator<R> =
Box<dyn Generator<AlterTableExpr, R, Error = Error> + Sync + Send>;
pub type ColumnOptionGenerator<R> = Box<dyn Fn(&mut R, &ConcreteDataType) -> Vec<ColumnOption>>;
pub type ConcreteDataTypeGenerator<R> = Box<dyn Random<ConcreteDataType, R>>;
pub type ValueGenerator<R> =
Box<dyn Fn(&mut R, &ConcreteDataType, Option<&dyn Random<Ident, R>>) -> Value>;
pub type TsValueGenerator<R> = Box<dyn Fn(&mut R, TimestampType) -> Value>;
pub trait Generator<T, R: Rng> {
type Error: Sync + Send + fmt::Debug;
fn generate(&self, rng: &mut R) -> Result<T, Self::Error>;
}
pub trait Random<T, R: Rng> {
fn gen(&self, rng: &mut R) -> T {
self.choose(rng, 1).remove(0)
}
fn choose(&self, rng: &mut R, amount: usize) -> Vec<T>;
}
#[macro_export]
macro_rules! impl_random {
($type: ident, $value:ident, $values: ident) => {
impl<R: Rng> Random<$type, R> for $value {
fn choose(&self, rng: &mut R, amount: usize) -> Vec<$type> {
let mut result = std::collections::BTreeSet::new();
while result.len() != amount {
result.insert($values.choose(rng).unwrap().clone());
}
let mut result = result.into_iter().map(Into::into).collect::<Vec<_>>();
result.shuffle(rng);
result
}
}
};
}