Skip to content

Commit 3797c84

Browse files
committed
refactor: use standalone methods to build setup query
1 parent 663ddc0 commit 3797c84

File tree

1 file changed

+86
-91
lines changed

1 file changed

+86
-91
lines changed

src/ops/storages/kuzu.rs

Lines changed: 86 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -206,98 +206,96 @@ impl setup::ResourceSetupStatus for GraphElementDataSetupStatus {
206206
}
207207
}
208208

209-
impl GraphElementDataSetupStatus {
210-
fn add_drop_cypher(
211-
&self,
212-
elem_type: &ElementType,
213-
cypher_builder: &mut CypherBuilder,
214-
) -> Result<()> {
215-
if !self.actions.drop_existing {
216-
return Ok(());
217-
}
218-
write!(
219-
cypher_builder.query_mut(),
220-
"DROP TABLE IF EXISTS {};\n",
221-
elem_type.label()
222-
)?;
223-
Ok(())
209+
fn add_drop_cypher(
210+
setup_status: &GraphElementDataSetupStatus,
211+
elem_type: &ElementType,
212+
cypher_builder: &mut CypherBuilder,
213+
) -> Result<()> {
214+
if !setup_status.actions.drop_existing {
215+
return Ok(());
224216
}
217+
write!(
218+
cypher_builder.query_mut(),
219+
"DROP TABLE IF EXISTS {};\n",
220+
elem_type.label()
221+
)?;
222+
Ok(())
223+
}
225224

226-
fn add_delete_orphaned_nodes_cypher(
227-
node_table: &str,
228-
cypher_builder: &mut CypherBuilder,
229-
) -> Result<()> {
230-
write!(
231-
cypher_builder.query_mut(),
232-
"MATCH (n:{node_table}) WITH n WHERE NOT (n)--() DELETE n;\n"
233-
)?;
234-
Ok(())
235-
}
225+
fn add_delete_orphaned_nodes_cypher(
226+
node_table: &str,
227+
cypher_builder: &mut CypherBuilder,
228+
) -> Result<()> {
229+
write!(
230+
cypher_builder.query_mut(),
231+
"MATCH (n:{node_table}) WITH n WHERE NOT (n)--() DELETE n;\n"
232+
)?;
233+
Ok(())
234+
}
236235

237-
fn add_create_alter_cypher(
238-
&self,
239-
elem_type: &ElementType,
240-
cypher_builder: &mut CypherBuilder,
241-
) -> Result<()> {
242-
let table_upsertion = if let Some(table_upsertion) = &self.actions.table_upsertion {
243-
table_upsertion
244-
} else {
245-
return Ok(());
246-
};
247-
match table_upsertion {
248-
TableUpsertionAction::Create { keys, values } => {
249-
write!(
250-
cypher_builder.query_mut(),
251-
"CREATE {kuzu_table_type} TABLE IF NOT EXISTS {table_name} (",
252-
kuzu_table_type = kuzu_table_type(elem_type),
253-
table_name = elem_type.label(),
254-
)?;
255-
if let Some((src, tgt)) = &self.referenced_node_tables {
256-
write!(cypher_builder.query_mut(), "FROM {src} TO {tgt}, ")?;
257-
}
258-
cypher_builder.query_mut().push_str(
259-
keys.iter()
260-
.chain(values.iter())
261-
.map(|(name, kuzu_type)| format!("{} {}", name, kuzu_type))
262-
.join(", ")
263-
.as_str(),
264-
);
265-
match elem_type {
266-
ElementType::Node(_) => {
267-
write!(
268-
cypher_builder.query_mut(),
269-
", {SELF_CONTAINED_TAG_FIELD_NAME} BOOL, PRIMARY KEY ({})",
270-
keys.iter().map(|(name, _)| name).join(", ")
271-
)?;
272-
}
273-
ElementType::Relationship(_) => {}
274-
}
275-
write!(cypher_builder.query_mut(), ");\n\n")?;
236+
fn add_create_alter_cypher(
237+
setup_status: &GraphElementDataSetupStatus,
238+
elem_type: &ElementType,
239+
cypher_builder: &mut CypherBuilder,
240+
) -> Result<()> {
241+
let table_upsertion = if let Some(table_upsertion) = &setup_status.actions.table_upsertion {
242+
table_upsertion
243+
} else {
244+
return Ok(());
245+
};
246+
match table_upsertion {
247+
TableUpsertionAction::Create { keys, values } => {
248+
write!(
249+
cypher_builder.query_mut(),
250+
"CREATE {kuzu_table_type} TABLE IF NOT EXISTS {table_name} (",
251+
kuzu_table_type = kuzu_table_type(elem_type),
252+
table_name = elem_type.label(),
253+
)?;
254+
if let Some((src, tgt)) = &setup_status.referenced_node_tables {
255+
write!(cypher_builder.query_mut(), "FROM {src} TO {tgt}, ")?;
276256
}
277-
TableUpsertionAction::Update {
278-
columns_to_delete,
279-
columns_to_upsert,
280-
} => {
281-
let table_name = elem_type.label();
282-
for name in columns_to_delete
283-
.iter()
284-
.chain(columns_to_upsert.iter().map(|(name, _)| name))
285-
{
286-
write!(
287-
cypher_builder.query_mut(),
288-
"ALTER TABLE {table_name} DROP IF EXISTS {name};\n"
289-
)?;
290-
}
291-
for (name, kuzu_type) in columns_to_upsert.iter() {
257+
cypher_builder.query_mut().push_str(
258+
keys.iter()
259+
.chain(values.iter())
260+
.map(|(name, kuzu_type)| format!("{} {}", name, kuzu_type))
261+
.join(", ")
262+
.as_str(),
263+
);
264+
match elem_type {
265+
ElementType::Node(_) => {
292266
write!(
293267
cypher_builder.query_mut(),
294-
"ALTER TABLE {table_name} ADD {name} {kuzu_type};\n",
268+
", {SELF_CONTAINED_TAG_FIELD_NAME} BOOL, PRIMARY KEY ({})",
269+
keys.iter().map(|(name, _)| name).join(", ")
295270
)?;
296271
}
272+
ElementType::Relationship(_) => {}
273+
}
274+
write!(cypher_builder.query_mut(), ");\n\n")?;
275+
}
276+
TableUpsertionAction::Update {
277+
columns_to_delete,
278+
columns_to_upsert,
279+
} => {
280+
let table_name = elem_type.label();
281+
for name in columns_to_delete
282+
.iter()
283+
.chain(columns_to_upsert.iter().map(|(name, _)| name))
284+
{
285+
write!(
286+
cypher_builder.query_mut(),
287+
"ALTER TABLE {table_name} DROP IF EXISTS {name};\n"
288+
)?;
289+
}
290+
for (name, kuzu_type) in columns_to_upsert.iter() {
291+
write!(
292+
cypher_builder.query_mut(),
293+
"ALTER TABLE {table_name} ADD {name} {kuzu_type};\n",
294+
)?;
297295
}
298296
}
299-
Ok(())
300297
}
298+
Ok(())
301299
}
302300

303301
////////////////////////////////////////////////////////////
@@ -501,9 +499,7 @@ impl StorageFactoryBase for Factory {
501499
if !change.setup_status.actions.drop_existing {
502500
continue;
503501
}
504-
change
505-
.setup_status
506-
.add_drop_cypher(&change.key.typ, &mut cypher_builder)?;
502+
add_drop_cypher(&change.setup_status, &change.key.typ, &mut cypher_builder)?;
507503

508504
partial_affected_node_tables.extend(
509505
change
@@ -517,20 +513,19 @@ impl StorageFactoryBase for Factory {
517513
}
518514
// Nodes first when creating.
519515
for change in node_changes.iter().chain(rel_changes.iter()) {
520-
change
521-
.setup_status
522-
.add_create_alter_cypher(&change.key.typ, &mut cypher_builder)?;
516+
add_create_alter_cypher(
517+
&change.setup_status,
518+
&change.key.typ,
519+
&mut cypher_builder,
520+
)?;
523521
}
524522

525523
debug!(
526524
"Partial affected node tables: {:?}",
527525
partial_affected_node_tables
528526
);
529527
for table in partial_affected_node_tables {
530-
GraphElementDataSetupStatus::add_delete_orphaned_nodes_cypher(
531-
table,
532-
&mut cypher_builder,
533-
)?;
528+
add_delete_orphaned_nodes_cypher(table, &mut cypher_builder)?;
534529
}
535530

536531
client.run_cypher(cypher_builder).await?;

0 commit comments

Comments
 (0)