Skip to content

Commit

Permalink
refactor(torii-grpc): correct pagination logic for grpc query member (#…
Browse files Browse the repository at this point in the history
…2256)

* refactor(torii-grpc): correct pagination logic for grpc query member

* fix: sql uqery

* fix: order by

* refactor: order by event id

* fmt

* fix: tests
  • Loading branch information
Larkooo authored Aug 9, 2024
1 parent 39f60a8 commit dc46e9c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
35 changes: 28 additions & 7 deletions crates/torii/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ pub fn build_sql_query(
entity_relation_column: &str,
where_clause: Option<&str>,
where_clause_arrays: Option<&str>,
) -> Result<(String, HashMap<String, String>), Error> {
limit: Option<u32>,
offset: Option<u32>,
) -> Result<(String, HashMap<String, String>, String), Error> {
fn parse_ty(
path: &str,
name: &str,
Expand Down Expand Up @@ -398,9 +400,21 @@ pub fn build_sql_query(
"SELECT {entities_table}.id, {entities_table}.keys, {selections_clause} FROM \
{entities_table}{join_clause}"
);
let mut count_query =
format!("SELECT COUNT({entities_table}.id) FROM {entities_table}{join_clause}",);

if let Some(where_clause) = where_clause {
query = format!("{} WHERE {}", query, where_clause);
query += &format!(" WHERE {}", where_clause);
count_query += &format!(" WHERE {}", where_clause);
}
query += &format!(" ORDER BY {entities_table}.event_id DESC");

if let Some(limit) = limit {
query += &format!(" LIMIT {}", limit);
}

if let Some(offset) = offset {
query += &format!(" OFFSET {}", offset);
}

if let Some(where_clause_arrays) = where_clause_arrays {
Expand All @@ -409,7 +423,7 @@ pub fn build_sql_query(
}
}

Ok((query, formatted_arrays_queries))
Ok((query, formatted_arrays_queries, count_query))
}

/// Populate the values of a Ty (schema) from SQLite row.
Expand Down Expand Up @@ -992,9 +1006,16 @@ mod tests {
],
});

let query =
build_sql_query(&vec![position, player_config], "entities", "entity_id", None, None)
.unwrap();
let query = build_sql_query(
&vec![position, player_config],
"entities",
"entity_id",
None,
None,
None,
None,
)
.unwrap();

let expected_query =
"SELECT entities.id, entities.keys, [Test-Position].external_player AS \
Expand All @@ -1006,7 +1027,7 @@ mod tests {
entities.id = [Test-Position$vec].entity_id JOIN [Test-Position] ON entities.id = \
[Test-Position].entity_id JOIN [Test-PlayerConfig$favorite_item] ON entities.id = \
[Test-PlayerConfig$favorite_item].entity_id JOIN [Test-PlayerConfig] ON entities.id \
= [Test-PlayerConfig].entity_id";
= [Test-PlayerConfig].entity_id ORDER BY entities.event_id DESC";
// todo: completely tests arrays
assert_eq!(query.0, expected_query);
}
Expand Down
28 changes: 18 additions & 10 deletions crates/torii/grpc/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,14 @@ impl DojoWorld {
.map_err(ParseError::FromStr)?;
let schemas = self.model_cache.schemas(&model_ids).await?;

let (entity_query, arrays_queries) = build_sql_query(
let (entity_query, arrays_queries, _) = build_sql_query(
&schemas,
table,
entity_relation_column,
Some(&format!("{table}.id = ?")),
Some(&format!("{table}.id = ?")),
None,
None,
)?;

let row =
Expand Down Expand Up @@ -433,12 +435,14 @@ impl DojoWorld {
.map_err(ParseError::FromStr)?;
let schemas = self.model_cache.schemas(&model_ids).await?;

let (entity_query, arrays_queries) = build_sql_query(
let (entity_query, arrays_queries, _) = build_sql_query(
&schemas,
table,
entity_relation_column,
Some(&format!("{table}.id = ?")),
Some(&format!("{table}.id = ?")),
None,
None,
)?;

let row = sqlx::query(&entity_query).bind(entity_id).fetch_one(&self.pool).await?;
Expand Down Expand Up @@ -525,17 +529,21 @@ impl DojoWorld {

let table_name = member_clause.model;
let column_name = format!("external_{}", member_clause.member);
let (entity_query, arrays_queries) = build_sql_query(
let (entity_query, arrays_queries, count_query) = build_sql_query(
&schemas,
table,
entity_relation_column,
Some(&format!(
"[{table_name}].{column_name} {comparison_operator} ? ORDER BY {table}.event_id \
DESC LIMIT ? OFFSET ?"
)),
Some(&format!("[{table_name}].{column_name} {comparison_operator} ?")),
None,
limit,
offset,
)?;

let total_count = sqlx::query_scalar(&count_query)
.bind(comparison_value.clone())
.fetch_one(&self.pool)
.await?;

let db_entities = sqlx::query(&entity_query)
.bind(comparison_value.clone())
.bind(limit)
Expand All @@ -553,8 +561,6 @@ impl DojoWorld {
.iter()
.map(|row| map_row_to_entity(row, &arrays_rows, schemas.clone()))
.collect::<Result<Vec<_>, Error>>()?;
// Since there is not limit and offset, total_count is same as number of entities
let total_count = entities_collection.len() as u32;
Ok((entities_collection, total_count))
}

Expand Down Expand Up @@ -698,12 +704,14 @@ impl DojoWorld {
.map_err(ParseError::FromStr)?;
let schemas = self.model_cache.schemas(&model_ids).await?;

let (entity_query, arrays_queries) = build_sql_query(
let (entity_query, arrays_queries, _) = build_sql_query(
&schemas,
table,
entity_relation_column,
Some(&format!("[{table}].id = ?")),
Some(&format!("[{table}].id = ?")),
None,
None,
)?;

let row = sqlx::query(&entity_query).bind(entity_id).fetch_one(&self.pool).await?;
Expand Down
4 changes: 3 additions & 1 deletion crates/torii/grpc/src/server/subscriptions/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,14 @@ impl Service {
.map_err(ParseError::FromStr)?;
let schemas = cache.schemas(&model_ids).await?;

let (entity_query, arrays_queries) = build_sql_query(
let (entity_query, arrays_queries, _) = build_sql_query(
&schemas,
"entities",
"entity_id",
Some("entities.id = ?"),
Some("entities.id = ?"),
None,
None,
)?;

let row = sqlx::query(&entity_query).bind(&entity.id).fetch_one(&pool).await?;
Expand Down
4 changes: 3 additions & 1 deletion crates/torii/grpc/src/server/subscriptions/event_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,14 @@ impl Service {
.map_err(ParseError::FromStr)?;
let schemas = cache.schemas(&model_ids).await?;

let (entity_query, arrays_queries) = build_sql_query(
let (entity_query, arrays_queries, _) = build_sql_query(
&schemas,
"event_messages",
"event_message_id",
Some("event_messages.id = ?"),
Some("event_messages.id = ?"),
None,
None,
)?;

let row = sqlx::query(&entity_query).bind(&entity.id).fetch_one(&pool).await?;
Expand Down

0 comments on commit dc46e9c

Please sign in to comment.