Skip to content

Commit

Permalink
fix database creation for null values
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Sep 26, 2023
1 parent 6d77f81 commit 713f842
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
6 changes: 5 additions & 1 deletion src-tauri/src/db/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ CREATE TABLE Games (
FOREIGN KEY(SiteID) REFERENCES Sites,
FOREIGN KEY(WhiteID) REFERENCES Players,
FOREIGN KEY(BlackID) REFERENCES Players
);
);

INSERT INTO Players (ID, Name, Elo) VALUES (0, 'Unknown', NULL);
INSERT INTO Events (ID, Name) VALUES (0, 'Unknown');
INSERT INTO Sites (ID, Name) VALUES (0, 'Unknown');
43 changes: 18 additions & 25 deletions src-tauri/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,44 +196,37 @@ impl TempGame {
pub fn insert_to_db(&self, db: &mut SqliteConnection) -> Result<(), diesel::result::Error> {
let pawn_home = get_pawn_home(self.position.board());

let white;
let black;
let site_id;
let event_id;

if let Some(name) = &self.white_name {
white = create_player(db, name)?;
let white_id = if let Some(name) = &self.white_name {
create_player(db, name)?.id
} else {
white = Player::default();
}
if let Some(name) = &self.black_name {
black = create_player(db, name)?;
0
};
let black_id = if let Some(name) = &self.black_name {
create_player(db, name)?.id
} else {
black = Player::default();
}
0
};

if let Some(name) = &self.event_name {
let event = create_event(db, name)?;
event_id = event.id;
let event_id = if let Some(name) = &self.event_name {
create_event(db, name)?.id
} else {
event_id = 1;
}
0
};

if let Some(name) = &self.site_name {
let site = create_site(db, name)?;
site_id = site.id;
let site_id = if let Some(name) = &self.site_name {
create_site(db, name)?.id
} else {
site_id = 1;
}
0
};

let ply_count = (self.moves.len()) as i32;
let final_material = get_material_count(self.position.board());
let minimal_white_material = self.material_count.white.min(final_material.white) as i32;
let minimal_black_material = self.material_count.black.min(final_material.black) as i32;

let new_game = NewGame {
white_id: Some(white.id),
black_id: Some(black.id),
white_id,
black_id,
ply_count,
eco: self.eco.as_deref(),
round: self.round.as_deref(),
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ pub struct NewGame<'a> {
pub date: Option<&'a str>,
pub time: Option<&'a str>,
pub round: Option<&'a str>,
pub white_id: Option<i32>,
pub white_id: i32,
pub white_elo: Option<i32>,
pub black_id: Option<i32>,
pub black_id: i32,
pub black_elo: Option<i32>,
pub white_material: i32,
pub black_material: i32,
Expand Down

0 comments on commit 713f842

Please sign in to comment.