mirror of
https://github.com/NexVeridian/wikidata-to-surrealdb.git
synced 2025-09-02 01:49:13 +00:00
swap to Option<Thing>
This commit is contained in:
parent
e0d2cef391
commit
08d8d2d63b
3 changed files with 27 additions and 9 deletions
11
Useful queries.md
Normal file
11
Useful queries.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
# Get number of episodes
|
||||
let $number_of_episodes = (select claims.claims[where id = Property:1113][0].value.ClaimValueData.Quantity.amount as number_of_episodes from Entity where label = "Black Clover, season 1")[0].number_of_episodes;
|
||||
|
||||
return $number_of_episodes[0].number_of_episodes;
|
||||
|
||||
update Entity SET number_of_episodes=$number_of_episodes where label = "Black Clover, season 1";
|
||||
|
||||
# Get Parts
|
||||
let $parts = (select claims.claims[where id = Property:527].value.Thing as parts from Entity where label = "Black Clover")[0].parts;
|
||||
|
||||
return $parts;
|
10
src/main.rs
10
src/main.rs
|
@ -33,13 +33,17 @@ async fn main() -> Result<(), Error> {
|
|||
let json: Value = from_str(&line)?;
|
||||
let data = Entity::from_json(json).expect("Failed to parse JSON");
|
||||
|
||||
let (id, claims, data) = EntityMini::from_entity(data);
|
||||
let (mut claims, mut data) = EntityMini::from_entity(data);
|
||||
|
||||
let id = data.id.clone().expect("No ID");
|
||||
data.id = None;
|
||||
let _: Option<EntityMini> = db.delete(&id).await?;
|
||||
let _: Option<EntityMini> = db.create(&id).content(data.clone()).await?;
|
||||
|
||||
let _: Option<Claims> = db.delete(&claims.0).await?;
|
||||
let _: Option<Claims> = db.create(&claims.0).content(claims.1).await?;
|
||||
let id = claims.id.clone().expect("No ID");
|
||||
claims.id = None;
|
||||
let _: Option<Claims> = db.delete(&id).await?;
|
||||
let _: Option<Claims> = db.create(&id).content(claims).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
15
src/utils.rs
15
src/utils.rs
|
@ -31,6 +31,7 @@ impl ClaimData {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Claims {
|
||||
pub id: Option<Thing>,
|
||||
pub claims: Vec<Claim>,
|
||||
}
|
||||
|
||||
|
@ -42,25 +43,26 @@ pub struct Claim {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct EntityMini {
|
||||
pub id: Option<Thing>,
|
||||
pub label: String,
|
||||
pub claims: Thing,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
impl EntityMini {
|
||||
pub fn from_entity(entity: Entity) -> (Thing, (Thing, Claims), Self) {
|
||||
pub fn from_entity(entity: Entity) -> (Claims, Self) {
|
||||
let thing_claim = Thing {
|
||||
id: get_id_entity(&entity).id,
|
||||
tb: "Claims".to_string(),
|
||||
};
|
||||
|
||||
(
|
||||
get_id_entity(&entity),
|
||||
(
|
||||
thing_claim.clone(),
|
||||
Self::flatten_claims(entity.claims.clone()),
|
||||
),
|
||||
Claims {
|
||||
id: Some(thing_claim.clone()),
|
||||
..Self::flatten_claims(entity.claims.clone())
|
||||
},
|
||||
Self {
|
||||
id: Some(get_id_entity(&entity)),
|
||||
label: get_name(&entity),
|
||||
claims: thing_claim,
|
||||
description: get_description(&entity).unwrap_or("".to_string()),
|
||||
|
@ -70,6 +72,7 @@ impl EntityMini {
|
|||
|
||||
fn flatten_claims(claims: Vec<(Pid, ClaimValue)>) -> Claims {
|
||||
Claims {
|
||||
id: None,
|
||||
claims: claims
|
||||
.iter()
|
||||
.flat_map(|(pid, claim_value)| {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue