Skip to content

Commit

Permalink
QUERY: prettify alias generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dark-flames committed Dec 14, 2021
1 parent 375f34c commit 7b94d7c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
20 changes: 5 additions & 15 deletions core/src/query/alias.rs
@@ -1,9 +1,5 @@
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::iter::repeat;

use rand::{distributions::Alphanumeric, Rng, thread_rng};
use rand::rngs::ThreadRng;

use query_builder::{Alias, Expr, SelectItem};

Expand All @@ -14,17 +10,13 @@ pub type TableName = &'static str;

#[derive(Clone)]
pub struct AliasGenerator {
rng: ThreadRng,
alias: HashMap<AliasName, TableName>,
_path: HashMap<(String, String), AliasName>,
alias: HashMap<AliasName, TableName>
}

impl AliasGenerator {
pub fn create() -> AliasGenerator {
AliasGenerator {
rng: thread_rng(),
alias: Default::default(),
_path: Default::default(),
alias: Default::default()
}
}

Expand All @@ -44,17 +36,15 @@ impl AliasGenerator {
}

fn generate_alias(&mut self, table_name: &'static str) -> Alias {
let mut offset = 1;
let name = loop {
let alias_name: String = repeat(())
.map(|()| self.rng.sample(Alphanumeric))
.map(char::from)
.take(3)
.collect();
let alias_name: String = format!("{}_{}", table_name, offset);

if let Entry::Vacant(e) = self.alias.entry(alias_name.clone()) {
e.insert(table_name);
break alias_name;
}
offset += 1;
};

Alias { name }
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/update.rs
Expand Up @@ -9,6 +9,8 @@ fn test_update() {
.filter(|b| lt!(b.int, 114514))
.filter(|b| bt!(b.int, 1919))
.update()
.set(foo::boolean, false)
.set_default(foo::id)
.set_by(foo::long, |l| l + 1)
.sort(|f, helper| helper.asc(f.id))
.limit(20)
Expand Down

0 comments on commit 7b94d7c

Please sign in to comment.