<commit_before>/* jslint node: true */
'use strict';

var conf			= require('./config.js');
var sqlite3			= require('sqlite3');
var paths			= require('path');

//	database handles
var dbs = {};

exports.initializeDatabases			= initializeDatabases;

exports.dbs							= dbs;

function getDatabasePath(name) {
	return paths.join(conf.config.paths.db, name + '.sqlite3');
}

function initializeDatabases() {
	//	:TODO: this will need to change if more DB's are added
	dbs.user = new sqlite3.Database(getDatabasePath('user'));

	dbs.user.serialize(function serialized() {
		createUserTables();
		createInitialValues();
	});
}

function createUserTables() {
	dbs.user.run(
		'CREATE TABLE IF NOT EXISTS user (' + 
		'	id			INTEGER PRIMARY KEY,' +
		'	user_name	VARCHAR NOT NULL,' + 
		'	UNIQUE(user_name)' +
		');'
		);

	//	:TODO: create FK on delete/etc.

	dbs.user.run(
		'CREATE TABLE IF NOT EXISTS user_property (' +
		'	user_id		INTEGER NOT NULL,' +
		'	prop_name	VARCHAR NOT NULL,' +
		'	prop_value	VARCHAR,' +
		'	UNIQUE(user_id, prop_name),' +
		'	FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE' + 
		');'
		);

	dbs.user.run(
		'CREATE TABLE IF NOT EXISTS user_group (' + 
		'	group_id	INTEGER PRIMARY KEY,' + 
		'	group_name	VARCHAR NOT NULL,' + 
		'	UNIQUE(group_name)' + 
		');'
		);

	dbs.user.run(
		'CREATE TABLE IF NOT EXISTS user_group_member (' + 
		'	group_id	INTEGER NOT NULL,' + 
		'	user_id		INTEGER NOT NULL,' +
		'	UNIQUE(group_id, user_id),' +
		'	FOREIGN KEY(group_id) REFERENCES user_group(group_id) ON DELETE CASCADE' +
		');'
		);
}

function createInitialValues() {
	dbs.user.run(
		'INSERT OR IGNORE INTO user_group ' + 
		'VALUES(1, "users");'
		);

	dbs.user.run(
		'INSERT OR IGNORE INTO user_group ' +
		'VALUES(2, "sysops");'
		);<commit_msg>* Initial start of message DB<commit_after>/* jslint node: true */
'use strict';

var conf			= require('./config.js');
var sqlite3			= require('sqlite3');
var paths			= require('path');

//	database handles
var dbs = {};

exports.initializeDatabases			= initializeDatabases;

exports.dbs							= dbs;

function getDatabasePath(name) {
	return paths.join(conf.config.paths.db, name + '.sqlite3');
}

function initializeDatabases() {
	//	:TODO: this will need to change if more DB's are added
	dbs.user	= new sqlite3.Database(getDatabasePath('user'));
	dbs.message	= new sqlite3.Database(getDatabasePath('message'));

	dbs.user.serialize(function serialized() {
		createUserTables();
		createInitialValues();
	});

	dbs.message.serialize(function serialized() {
		createMessageBaseTables();
	});
}

function createUserTables() {
	dbs.user.run(
		'CREATE TABLE IF NOT EXISTS user (' + 
		'	id			INTEGER PRIMARY KEY,' +
		'	user_name	VARCHAR NOT NULL,' + 
		'	UNIQUE(user_name)' +
		');'
		);

	//	:TODO: create FK on delete/etc.

	dbs.user.run(
		'CREATE TABLE IF NOT EXISTS user_property (' +
		'	user_id		INTEGER NOT NULL,' +
		'	prop_name	VARCHAR NOT NULL,' +
		'	prop_value	VARCHAR,' +
		'	UNIQUE(user_id, prop_name),' +
		'	FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE' + 
		');'
		);

	dbs.user.run(
		'CREATE TABLE IF NOT EXISTS user_group (' + 
		'	group_id	INTEGER PRIMARY KEY,' + 
		'	group_name	VARCHAR NOT NULL,' + 
		'	UNIQUE(group_name)' + 
		');'
		);

	dbs.user.run(
		'CREATE TABLE IF NOT EXISTS user_group_member (' + 
		'	group_id	INTEGER NOT NULL,' + 
		'	user_id		INTEGER NOT NULL,' +
		'	UNIQUE(group_id, user_id),' +
		'	FOREIGN KEY(group_id) REFERENCES user_group(group_id) ON DELETE CASCADE' +
		');'
		);
}

function createMessageBaseTables() {
	dbs.message.run(
		'CREATE TABLE IF NOT EXISTS message (' +
		'	message_id		INTEGER PRIMARY KEY,' + 
		'	area_id			INTEGER NOT NULL,' +
		'	message_uuid	VARCHAR(36) NOT NULL,' + 
		'	reply_to_id		INTEGER,' +
		'	to_user_name	VARCHAR NOT NULL,' +
		'	from_user_name	VARCHAR NOT NULL,' +
		'	subject,' +	//	FTS
		'	message,' + //	FTS
		'	modified_timestamp	DATETIME' +
		');'
	);

	dbs.message.run(
		'CREATE VIRTUAL TABLE message_fts USING fts4 (' +
		'	content="message",' +
		'	subject,' +
		'	message' +
		');'
	);
}

function createInitialValues() {
	dbs.user.run(
		'INSERT OR IGNORE INTO user_group ' + 
		'VALUES(1, "users");'
		);

	dbs.user.run(
		'INSERT OR IGNORE INTO user_group ' +
		'VALUES(2, "sysops");'
		);
{'fraction_non_alphanumeric': 0.14868329060824984, 'fraction_numerical': 0.0037287345607084597, 'mean_word_length': 3.5756929637526653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1950276', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1645, 'n_words': 370}