-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gil Mizrahi
committed
Jan 15, 2024
1 parent
f7f739e
commit 26ca7f0
Showing
13 changed files
with
1,195 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
crates/query-engine/translation/src/translation/mutation/insert.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//! Auto-generate insert mutations and translate them into sql ast. | ||
use crate::translation::error::Error; | ||
use crate::translation::query::values::translate_json_value; | ||
use query_engine_metadata::metadata; | ||
use query_engine_metadata::metadata::database; | ||
use query_engine_sql::sql::ast; | ||
use std::collections::BTreeMap; | ||
|
||
/// A representation of an auto-generated insert mutation. | ||
/// | ||
/// This can get us `INSERT INTO <table>(<columns>) VALUES (<values>)`. | ||
#[derive(Debug, Clone)] | ||
pub struct InsertMutation { | ||
pub collection_name: String, | ||
pub description: String, | ||
pub schema_name: ast::SchemaName, | ||
pub table_name: ast::TableName, | ||
pub columns: BTreeMap<String, metadata::database::ColumnInfo>, | ||
} | ||
|
||
/// generate an insert mutation. | ||
pub fn generate( | ||
collection_name: &String, | ||
table_info: &database::TableInfo, | ||
) -> (String, InsertMutation) { | ||
let name = format!("v1_insert_{collection_name}"); | ||
|
||
let description = format!("Insert into the {collection_name} table",); | ||
|
||
let insert_mutation = InsertMutation { | ||
collection_name: collection_name.clone(), | ||
description, | ||
schema_name: ast::SchemaName(table_info.schema_name.clone()), | ||
table_name: ast::TableName(table_info.table_name.clone()), | ||
columns: table_info.columns.clone(), | ||
}; | ||
|
||
(name, insert_mutation) | ||
} | ||
|
||
/// Given the description of an insert mutation (ie, `InsertMutation`), | ||
/// and the arguments, output the SQL AST. | ||
pub fn translate( | ||
// state: &mut crate::translation::helpers::State, | ||
mutation: &InsertMutation, | ||
arguments: BTreeMap<String, serde_json::Value>, | ||
) -> Result<ast::Insert, Error> { | ||
println!("{:#?}", mutation); | ||
let insert = ast::Insert { | ||
schema: mutation.schema_name.clone(), | ||
table: mutation.table_name.clone(), | ||
columns: mutation | ||
.columns | ||
.values() | ||
.map(|column_info| ast::ColumnName(column_info.name.clone())) | ||
.collect(), | ||
values: { | ||
let object = arguments.get("_object").unwrap(); // TODO | ||
match object { | ||
serde_json::Value::Object(object) => object | ||
.iter() | ||
.map(|(name, value)| { | ||
let column = mutation.columns.get(name).unwrap(); | ||
translate_json_value(value, &column.r#type).unwrap() | ||
}) | ||
.collect(), | ||
_ => todo!(), | ||
} | ||
}, | ||
returning: ast::Returning::ReturningStar, | ||
}; | ||
println!("{:#?}", insert); | ||
Ok(insert) | ||
} |
1 change: 1 addition & 0 deletions
1
crates/query-engine/translation/src/translation/mutation/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod delete; | ||
pub mod generate; | ||
pub mod insert; | ||
pub mod translate; | ||
pub use translate::translate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
crates/query-engine/translation/tests/goldenfiles/mutations/v1_insert/request.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"operations": [ | ||
{ | ||
"type": "procedure", | ||
"name": "v1_insert_Artist", | ||
"arguments": { | ||
"_object": { | ||
"id": 276, | ||
"name": "Olympians" | ||
} | ||
}, | ||
"fields": { | ||
"artist_id": { | ||
"type": "column", | ||
"column": "id" | ||
}, | ||
"name": { | ||
"type": "column", | ||
"column": "name" | ||
} | ||
} | ||
} | ||
], | ||
"collection_relationships": {} | ||
} |
22 changes: 22 additions & 0 deletions
22
crates/query-engine/translation/tests/goldenfiles/mutations/v1_insert/tables.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"tables": { | ||
"Artist": { | ||
"schemaName": "public", | ||
"tableName": "Artist", | ||
"columns": { | ||
"id": { | ||
"name": "ArtistId", | ||
"type": { | ||
"scalarType": "int4" | ||
} | ||
}, | ||
"name": { | ||
"name": "Name", | ||
"type": { | ||
"scalarType": "varchar" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.