Skip to content

Commit

Permalink
move dummy generation of tables into tsql lib
Browse files Browse the repository at this point in the history
  • Loading branch information
LetsMelon committed Aug 19, 2023
1 parent 18163af commit e1aac5c
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ name = "generate_tsql"
path = "./bin/generate_tsql.rs"

[dependencies]
tsql = { version = "0.1.0", path = "../lib" }
tsql = { version = "0.1.0", path = "../lib", features = ["generate"] }
pico-args = "0.5.0"
hmac-sha256 = "1.1.7"
48 changes: 1 addition & 47 deletions cli/bin/generate_tsql.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::process::exit;

use hmac_sha256::HMAC;
use tsql::types::{DataType, Field, Table, TableExtra};
use tsql::generate::generate_table;
use tsql::TransformTSQL;

#[derive(Debug)]
Expand Down Expand Up @@ -177,47 +175,3 @@ fn parse_args() -> Result<AppArgs, pico_args::Error> {
// TODO implement custom error
Ok(args.build().unwrap())
}

fn number_to_string(number: usize) -> String {
let bytes = number.to_le_bytes();
let h = HMAC::new(bytes);
let hash = h.finalize();

hash.to_vec()
.iter()
// TODO check if this maps all u8 values into ascii lowercase values
.map(|item| (item % 24 + 65) as char)
.collect::<String>()
}

fn generate_table(counter: usize, fields_per_table: usize) -> Table {
const DATATYPES: &[DataType] = &[
DataType::Int,
DataType::Double,
DataType::VarChar(100),
DataType::Char(6),
DataType::Uuid,
];

let name = number_to_string(counter);

let mut fields = HashMap::new();

for i in 0..fields_per_table {
let field_name = number_to_string(i.wrapping_add(counter.wrapping_mul(100)));
let datatype = DATATYPES[i.wrapping_add(counter) % DATATYPES.len()];

let field = Field::new(&field_name, datatype);

fields.insert(field_name, field);
}

// TODO check if `fields_per_table > 0`
let first_field_for_pk = fields.keys().next().unwrap().clone();

Table::new(
name,
fields,
TableExtra::new_with_pk(vec![first_field_for_pk]),
)
}
5 changes: 5 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ repository.workspace = true
anyhow = "1.0.75"
nom = "7.1.3"
static_assertions = "1.1.0"
hmac-sha256 = { version = "1.1.7", optional = true }

[dev-dependencies]
criterion = { version = "0.4", features = ["html_reports"] }

[features]
default = ["generate"]
generate = ["dep:hmac-sha256"]

[[bench]]
name = "parsing_tables"
harness = false
44 changes: 44 additions & 0 deletions lib/src/generate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::collections::HashMap;

use hmac_sha256::HMAC;

use crate::types::{Field, Table, TableExtra};

pub trait GenerateDummy {
fn generate_dummy(number: usize) -> Self;
}

pub(crate) fn hash_number(input: usize) -> [u8; 32] {
let bytes = input.to_le_bytes();
let h = HMAC::new(bytes);

h.finalize()
}

pub(crate) fn u8s_to_string(input: &[u8]) -> String {
input.iter().map(|item| (item % 24 + 65) as char).collect()
}

pub(crate) fn hash_number_and_stringify(input: usize) -> String {
u8s_to_string(&hash_number(input))
}

pub fn generate_table(counter: usize, fields_per_table: usize) -> Table {
let name = hash_number_and_stringify(counter);

let mut fields = HashMap::new();

for i in 0..fields_per_table {
let field = Field::generate_dummy(i.wrapping_add(counter.wrapping_mul(100)));

fields.insert(field.name.clone(), field);
}

let first_field_for_pk = fields.keys().next().unwrap().clone();

Table::new(
name,
fields,
TableExtra::new_with_pk(vec![first_field_for_pk]),
)
}
5 changes: 5 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ use types::{RawTableCollection, Table, TableCollection};

use crate::parser::parse;

#[cfg(feature = "generate")]
pub mod generate;
#[cfg(not(feature = "generate"))]
mod generate;

mod parser;
pub mod types;

Expand Down
28 changes: 28 additions & 0 deletions lib/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use std::rc::Rc;
use anyhow::{bail, Result};
use static_assertions::const_assert_eq;

use crate::generate::hash_number_and_stringify;
#[cfg(feature = "generate")]
use crate::generate::GenerateDummy;
use crate::parser::types::{FieldExtra, FieldType, RawDataType, RawField, RawTable};
use crate::{TransformSQL, TransformTSQL};

Expand Down Expand Up @@ -299,6 +302,15 @@ impl TransformTSQL for Field {
}
}

impl GenerateDummy for Field {
fn generate_dummy(number: usize) -> Self {
let name = hash_number_and_stringify(number);
let datatype = DataType::generate_dummy(number);

Field::new(name, datatype)
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum DataType {
Int,
Expand Down Expand Up @@ -389,6 +401,22 @@ impl TransformTSQL for DataType {
}
}

#[cfg(feature = "generate")]
impl GenerateDummy for DataType {
fn generate_dummy(number: usize) -> Self {
// TODO add more variants
const DATATYPES: &[DataType] = &[
DataType::Int,
DataType::Double,
DataType::VarChar(100),
DataType::Char(6),
DataType::Uuid,
];

DATATYPES[number % DATATYPES.len()]
}
}

/// Holds metadata for a [`Table`]
#[derive(Debug, Default)]
pub struct TableExtra {
Expand Down

0 comments on commit e1aac5c

Please sign in to comment.