Skip to content

Commit 850a6da

Browse files
authored
refactor(grpc): array comparison using json format (#257)
* refactor(grpc): array comparison using json format * to json value fix * rm debug prints
1 parent eaf192f commit 850a6da

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

crates/sqlite/sqlite/src/lib.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::sync::Arc;
33

44
use dojo_types::primitive::SqlType;
55
use dojo_types::schema::Ty;
6-
use serde_json::json;
76
use sqlx::{Pool, Sqlite};
87
use starknet::core::types::Felt;
98
use tokio::sync::mpsc::UnboundedSender;
@@ -170,24 +169,7 @@ impl Sql {
170169
collect_members(&column_name, member, columns, arguments)?;
171170
}
172171
}
173-
Ty::FixedSizeArray((array, size)) => {
174-
columns.push(format!("\"{}\"", prefix));
175-
let elements = array
176-
.iter()
177-
.map(|v| v.to_json_value())
178-
.collect::<Result<Vec<_>, _>>()?;
179-
180-
let value = json!({
181-
"elements": elements,
182-
"size": *size
183-
});
184-
185-
arguments.push(Argument::String(
186-
serde_json::to_string(&value)
187-
.map_err(|e| Error::Parse(ParseError::FromJsonStr(e)))?,
188-
));
189-
}
190-
Ty::Array(array) => {
172+
Ty::Array(array) | Ty::FixedSizeArray((array, _)) => {
191173
columns.push(format!("\"{}\"", prefix));
192174
let values = array
193175
.iter()

crates/sqlite/sqlite/src/model.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,16 +498,19 @@ fn build_composite_clause(
498498
fn prepare_comparison(
499499
value: &MemberValue,
500500
bind_values: &mut Vec<String>,
501-
historical: bool,
501+
json_format: bool,
502502
) -> Result<String, Error> {
503503
match value {
504504
MemberValue::String(value) => {
505505
bind_values.push(value.to_string());
506506
Ok("?".to_string())
507507
}
508508
MemberValue::Primitive(value) => {
509-
let value = if historical {
510-
Ty::Primitive(*value).to_json_value()?.to_string()
509+
let value = if json_format {
510+
Ty::Primitive(*value)
511+
.to_json_value()?
512+
.to_string()
513+
.replace("\"", "")
511514
} else {
512515
value.to_sql_value()
513516
};
@@ -518,19 +521,33 @@ fn build_composite_clause(
518521
"({})",
519522
values
520523
.iter()
521-
.map(|v| prepare_comparison(v, bind_values, historical))
524+
.map(|v| prepare_comparison(v, bind_values, json_format))
522525
.collect::<Result<Vec<String>, Error>>()?
523526
.join(", ")
524527
)),
525528
}
526529
}
527-
let value = prepare_comparison(&member.value, &mut bind_values, historical)?;
528530

529531
let model = member.model.clone();
530532
let operator = member.operator.clone();
531533

534+
// Determine if we need JSON formatting for values
535+
let array_index = parse_array_index(&member.member);
536+
let is_array_operation = matches!(
537+
operator,
538+
ComparisonOperator::Contains
539+
| ComparisonOperator::ContainsAll
540+
| ComparisonOperator::ContainsAny
541+
| ComparisonOperator::ArrayLengthEq
542+
| ComparisonOperator::ArrayLengthGt
543+
| ComparisonOperator::ArrayLengthLt
544+
) || array_index.is_some(); // Array indexing also needs JSON formatting
545+
546+
let value =
547+
prepare_comparison(&member.value, &mut bind_values, is_array_operation)?;
548+
532549
// Check if this field has array indexing syntax like "field[0]"
533-
if let Some((field_name, index)) = parse_array_index(&member.member) {
550+
if let Some((field_name, index)) = array_index {
534551
// Handle array element access
535552
let column_access = if historical {
536553
format!("JSON_EXTRACT({table}.data, '$.{field_name}')")

0 commit comments

Comments
 (0)