|
| 1 | +pub fn create_indices(con: &rusqlite::Connection) -> rusqlite::Result<()> { |
| 2 | + con.execute_batch("CREATE INDEX if not exists commitFileID ON commitFile (fileID)")?; |
| 3 | + con.execute_batch("CREATE INDEX if not exists commitAuthorEmail ON commitAuthor (authorEmail)") |
| 4 | +} |
| 5 | + |
| 6 | +pub fn create(name: &str) -> rusqlite::Result<rusqlite::Connection> { |
| 7 | + let con = rusqlite::Connection::open(format!("{name}.db"))?; |
| 8 | + con.execute_batch( |
| 9 | + r#" |
| 10 | + CREATE TABLE if not exists commits( |
| 11 | + hash character(40) NOT NULL PRIMARY KEY, |
| 12 | + authorDate text NOT NULL, |
| 13 | + committerName text NOT NULL, |
| 14 | + committerEmail text NOT NULL, |
| 15 | + committerDate text NOT NULL |
| 16 | + ) |
| 17 | +"#, |
| 18 | + )?; |
| 19 | + con.execute_batch( |
| 20 | + r#" |
| 21 | + CREATE TABLE if not exists files( |
| 22 | + fileID integer NOT NULL PRIMARY KEY, |
| 23 | + filePath text UNIQUE |
| 24 | + ) |
| 25 | +"#, |
| 26 | + )?; |
| 27 | + con.execute_batch( |
| 28 | + r#" |
| 29 | + CREATE TABLE if not exists commitFile( |
| 30 | + hash character(40), |
| 31 | + fileID text, |
| 32 | + linesAdded integer, |
| 33 | + linesRemoved integer, |
| 34 | + FOREIGN KEY (hash) REFERENCES commits (hash), |
| 35 | + FOREIGN KEY (fileID) REFERENCES files (fileID), |
| 36 | + PRIMARY KEY (hash, fileID) |
| 37 | + ) |
| 38 | + "#, |
| 39 | + )?; |
| 40 | + con.execute_batch( |
| 41 | + r#" |
| 42 | + CREATE TABLE if not exists author( |
| 43 | + authorEmail text NOT NULL PRIMARY KEY, |
| 44 | + authorName text NOT NULL |
| 45 | + ) |
| 46 | +"#, |
| 47 | + )?; |
| 48 | + con.execute_batch( |
| 49 | + r#" |
| 50 | + CREATE TABLE if not exists commitAuthor( |
| 51 | + hash character(40), |
| 52 | + authorEmail text, |
| 53 | + FOREIGN KEY (hash) REFERENCES commits (hash), |
| 54 | + FOREIGN KEY (authorEmail) REFERENCES author (authorEmail), |
| 55 | + PRIMARY KEY (hash, authorEmail) |
| 56 | + ) |
| 57 | +"#, |
| 58 | + )?; |
| 59 | + |
| 60 | + Ok(con) |
| 61 | +} |
0 commit comments