Skip to content

Commit

Permalink
Changed name generation for join and column aliases
Browse files Browse the repository at this point in the history
This will make sql statements more compact but harder to read.
More importantly, it fixes the issue of aliases becoming larger
than postgres allows.
  • Loading branch information
gammelalf committed Sep 6, 2024
1 parent 520df53 commit 4fa6e74
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Since 0.6.3
- improved error spans in or! and and!
- removed `AsDbType::from_primitive`
- fixed names of join aliases
- changed name generation for join and column aliases

Notes for publishing
--------------------
Expand Down
43 changes: 27 additions & 16 deletions src/internal/query_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;
use std::fmt::Write;

use rorm_db::sql::join_table::JoinType;
use rorm_db::sql::ordering::Ordering;
Expand Down Expand Up @@ -38,11 +40,7 @@ impl<'v> QueryContext<'v> {

/// Add a field to select returning its index and alias
pub fn select_field<F: Field, P: Path>(&mut self) -> (usize, String) {
let alias = format!(
"{path}__{field}",
path = P::add_to_context(self),
field = F::NAME
);
let alias = format!("{}", NumberAsAZ(self.selects.len()));
self.selects.push(Select {
table_name: PathId::of::<P>(),
column_name: F::NAME,
Expand All @@ -54,12 +52,7 @@ impl<'v> QueryContext<'v> {

/// Add a field to aggregate returning its index and alias
pub fn select_aggregation<A: AggregationFunc, F: Field, P: Path>(&mut self) -> (usize, String) {
let alias = format!(
"{path}__{field}___{func}",
path = P::add_to_context(self),
field = F::NAME,
func = A::NAME,
);
let alias = format!("{}", NumberAsAZ(self.selects.len()));
self.selects.push(Select {
table_name: PathId::of::<P>(),
column_name: F::NAME,
Expand Down Expand Up @@ -238,11 +231,7 @@ impl<'v> QueryContext<'v> {
{
let path_id = PathId::of::<P::Step<F>>();
if !self.join_aliases.contains_key(&path_id) {
let alias = format!(
"{field}__{path}",
field = F::NAME,
path = P::add_to_context(self)
);
let alias = format!("{}", NumberAsAZ(self.join_aliases.len()));
self.join_aliases.insert(path_id, alias);
self.joins.push({
Join {
Expand Down Expand Up @@ -282,3 +271,25 @@ struct OrderBy {
table_name: PathId,
ordering: Ordering,
}

/// Adapter to display a number using the alphabet as digits
struct NumberAsAZ(usize);
impl fmt::Display for NumberAsAZ {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
static ALPHABET: [char; 26] = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
];
let mut x = self.0;
match x {
0..26 => f.write_char(ALPHABET[x]),
_ => {
while x > 26 {
f.write_char(ALPHABET[x % 26])?;
x /= 26;
}
f.write_char(ALPHABET[x])
}
}
}
}

0 comments on commit 4fa6e74

Please sign in to comment.