Commit 9e188be4 authored by Laura Heimann's avatar Laura Heimann

sqlite database implementation for chartLibrary

parent 477e9099
...@@ -5,19 +5,40 @@ const fs = require('fs'); ...@@ -5,19 +5,40 @@ const fs = require('fs');
const { glob } = require('glob'); const { glob } = require('glob');
const path = require('path'); const path = require('path');
const UserSettings = require('@/modules/module.usersettings.js'); const UserSettings = require('@/modules/module.usersettings.js');
const sqlite3 = require('sqlite3').verbose();
class ChartLibrary { class ChartLibrary {
constructor() { constructor() {
const userDataPath = app.getPath('userData'); const userDataPath = app.getPath('userData');
this.path = path.join(userDataPath, 'ChartLibrary.json'); this.path = path.join(userDataPath, 'ChartLibrary.sqlite');
this.db = null;
} }
async getLibrary() { async getLibrary() {
let userSettings = new UserSettings();
let data = this.parseDataFile(this.path, false);
console.log("[ChartLibrary] Get Library"); console.log("[ChartLibrary] Get Library");
this.connectToDatabase();
this.db.serialize(() => {
this.createDatabaseIfNotExists();
// TODO: Check if Database is up to date and update if not
this.db.get('SELECT * FROM meta', (err, row) => {
if (err) {
console.error("[ChartLibrary] " + err.message);
}
console.log(row);
});
// TODO: Get Library
});
this.closeDatabase();
/*
let data = this.parseDataFile(this.path, false);
if(data == false) { if(data == false) {
return this.updateLibrary(); return this.updateLibrary();
} else { } else {
...@@ -26,18 +47,29 @@ class ChartLibrary { ...@@ -26,18 +47,29 @@ class ChartLibrary {
} else { } else {
return data; return data;
} }
} } */
} }
async updateLibrary() { async updateLibrary() {
console.log("[ChartLibrary] Library Refresh");
this.connectToDatabase();
this.db.serialize(() => {
this.createDatabaseIfNotExists();
// TODO: Update Library and Save to DB
});
this.closeDatabase();
/*
let userSettings = new UserSettings(); let userSettings = new UserSettings();
let data = { let data = {
folderMD5: "", folderMD5: "",
charts: [] charts: []
}; };
console.log("[ChartLibrary] Library Refresh");
data.folderMD5 = md5(userSettings.get('gameDirectory')); data.folderMD5 = md5(userSettings.get('gameDirectory'));
// Find all SRTB files // Find all SRTB files
...@@ -58,6 +90,7 @@ class ChartLibrary { ...@@ -58,6 +90,7 @@ class ChartLibrary {
console.log("[ChartLibrary] Wrote Library to: " + this.path); console.log("[ChartLibrary] Wrote Library to: " + this.path);
return data; return data;
*/
} }
parseSRTBFile(srtbFilePath) { parseSRTBFile(srtbFilePath) {
...@@ -180,12 +213,29 @@ class ChartLibrary { ...@@ -180,12 +213,29 @@ class ChartLibrary {
return data; return data;
} }
parseDataFile(filePath, defaults) { connectToDatabase() {
try { this.db = new sqlite3.Database(this.path, (err) => {
return JSON.parse(fs.readFileSync(filePath)); if (err) {
} catch(error) { console.error("[ChartLibrary] " + err.message);
return defaults; }
console.log('[ChartLibrary] Connected to Database');
});
}
closeDatabase() {
this.db.close((err) => {
if (err) {
console.error("[ChartLibrary] " + err.message);
}
console.log('[ChartLibrary] Database Closed');
});
} }
createDatabaseIfNotExists() {
// TODO: Finalize Database Structure
this.db.prepare(`
CREATE TABLE IF NOT EXISTS meta (folderMD5 TEXT, clientVersion TEXT);
`).run().finalize();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment