/*

	Copyright (c) 2020, 2023, The Unified Company.

	This code is part of Unify.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the ESA Software Community License - Strong Copyleft LICENSE,
	as published by the ESA.
	See the ESA Software Community License - Strong Copyleft LICENSE, for more details.

	https://unifyjs.org 

*/

import sqlite3			from './node_modules/better-sqlite3/lib/index.js';

import path 			from 'path';


export default class sqlite{


	file	= path.resolve( './storage/index.sqlite' );

	db;
	
	queryID = 0;

	type	= "file";

	constructor() {

		//this.setup();
	
	}
	
	setup() {

		if( global.sqlite == undefined ) {

			if( this.type == "file") {

				if( process.platform == "android" ) {

					this.db = sqlite3(  __dirname + "/storage/index.sqlite"  );

				} else {

					this.db = sqlite3(  this.file  );

				}

				//console.log("Loading database:			", this.file);
			
			} else {

				this.db = sqlite3(  ":memory:"  );

			}

		
		} else {

			this.db = global.sqlite;

		}
		
		this.db.pragma('journal_mode = WAL');
		
	}

	onConnect( err ) {

		if ( err ) {
		
			console.error("Database error:", err.message);
			
		}
		
		console.log('Connected to the database.');
		
	}

}