<commit_before>const SettingProvider = require('./base');

/**
 * Uses an SQLite database to store settings with guilds
 * @extends {SettingProvider}
 */
class SQLiteProvider extends SettingProvider {
	/**
	 * @external SQLiteDatabase
	 * @see {@link https://www.npmjs.com/package/sqlite}
	 */

	/**
	 * @param {SQLiteDatabase} db Database for the provider
	 */
	constructor(db) {
		super();

		/**
		 * Database that will be used for storing/retrieving settings
		 * @type {SQLiteDatabase}
		 */
		this.db = db;

		/**
		 * Client that the provider is for (set once the client is ready, after using {@link Client#setProvider})
		 * @type {?CommandoClient}
		 */
		this.client = null;

		/**
		 * Settings cached in memory, mapped by guild ID (or 'global')
		 * @type {Map}
		 * @private
		 */
		this.settings = new Map();

		/**
		 * Prepared statement to insert or replace a settings row
		 * @type {SQLiteStatement}
		 * @private
		 */
		this.insertOrReplaceStmt = null;

		/**
		 * Prepared statement to delete an entire settings row
		 * @type {SQLiteStatement}
		 * @private
		 */
		this.deleteStmt = null;

		/**
		 * @external SQLiteStatement
		 * @see {@link https://www.npmjs.com/package/sqlite}
		 */
	}

	async init(client) {
		this.client = client;
		await this.db.run('CREATE TABLE IF NOT EXISTS settings (guild INTEGER PRIMARY KEY, settings TEXT)');

		// Load all settings
		const rows = await this.db.all('SELECT CAST(guild as TEXT) as guild, settings FROM settings');
		for(const row of rows) {
			const settings = JSON.parse(row.settings);
			this.settings.set(row.guild || 'global', settings);
			if(row.guild !== 'global' && !client.guilds.has(row.guild)) continue;
			this.setupGuild(row.guild || 'global', settings);
		}

		// Prepare statements
		const statements = await Promise.all([
			this.db.prepare('INSERT OR REPLACE INTO settings VALUES(?, ?)'),
			this.db.prepare('DELETE FROM settings WHERE guild = ?')
		]);
		this.insertOrReplaceStmt = statements[0];
		this.deleteStmt = statements[1];

		// Listen for changes
		client
			.on('commandPrefixChange', (guild, prefix) => this.set(guild, 'prefix', prefix))
			.on('commandStatusChange', (guild, command, enabled) => this.set(guild, `cmd-${command.name}`, enabled))
			.on('groupStatusChange', (guild, group, enabled) => this.set(guild, `grp-${group.name}`, enabled))
			.on('guildCreate', guild => {
				const settings = this.settings.get(guild.id);
				if(!settings) return;
				this.setupGuild(guild.id, settings);
			})
			.on('commandRegister', command => {
				for(const [guild, settings] of this.settings) {
					if(guild !== 'global' && !client.guilds.has(guild)) continue;
					this.setupGuildCommand(client.guilds.get(guild), command, settings);
				}
			})
			.on('groupRegister', group => {
				for(const [guild, settings] of this.settings) {
					if(guild !== 'global' && !client.guilds.has(guild)) continue;
					this.setupGuildGroup(client.guilds.get(guild), group, settings);
				}
			});
	}

	async destroy() {
		// do nothing
	}<commit_msg>Added statement finalization to destroy<commit_after>const SettingProvider = require('./base');

/**
 * Uses an SQLite database to store settings with guilds
 * @extends {SettingProvider}
 */
class SQLiteProvider extends SettingProvider {
	/**
	 * @external SQLiteDatabase
	 * @see {@link https://www.npmjs.com/package/sqlite}
	 */

	/**
	 * @param {SQLiteDatabase} db Database for the provider
	 */
	constructor(db) {
		super();

		/**
		 * Database that will be used for storing/retrieving settings
		 * @type {SQLiteDatabase}
		 */
		this.db = db;

		/**
		 * Client that the provider is for (set once the client is ready, after using {@link Client#setProvider})
		 * @type {?CommandoClient}
		 */
		this.client = null;

		/**
		 * Settings cached in memory, mapped by guild ID (or 'global')
		 * @type {Map}
		 * @private
		 */
		this.settings = new Map();

		/**
		 * Prepared statement to insert or replace a settings row
		 * @type {SQLiteStatement}
		 * @private
		 */
		this.insertOrReplaceStmt = null;

		/**
		 * Prepared statement to delete an entire settings row
		 * @type {SQLiteStatement}
		 * @private
		 */
		this.deleteStmt = null;

		/**
		 * @external SQLiteStatement
		 * @see {@link https://www.npmjs.com/package/sqlite}
		 */
	}

	async init(client) {
		this.client = client;
		await this.db.run('CREATE TABLE IF NOT EXISTS settings (guild INTEGER PRIMARY KEY, settings TEXT)');

		// Load all settings
		const rows = await this.db.all('SELECT CAST(guild as TEXT) as guild, settings FROM settings');
		for(const row of rows) {
			const settings = JSON.parse(row.settings);
			this.settings.set(row.guild || 'global', settings);
			if(row.guild !== 'global' && !client.guilds.has(row.guild)) continue;
			this.setupGuild(row.guild || 'global', settings);
		}

		// Prepare statements
		const statements = await Promise.all([
			this.db.prepare('INSERT OR REPLACE INTO settings VALUES(?, ?)'),
			this.db.prepare('DELETE FROM settings WHERE guild = ?')
		]);
		this.insertOrReplaceStmt = statements[0];
		this.deleteStmt = statements[1];

		// Listen for changes
		client
			.on('commandPrefixChange', (guild, prefix) => this.set(guild, 'prefix', prefix))
			.on('commandStatusChange', (guild, command, enabled) => this.set(guild, `cmd-${command.name}`, enabled))
			.on('groupStatusChange', (guild, group, enabled) => this.set(guild, `grp-${group.name}`, enabled))
			.on('guildCreate', guild => {
				const settings = this.settings.get(guild.id);
				if(!settings) return;
				this.setupGuild(guild.id, settings);
			})
			.on('commandRegister', command => {
				for(const [guild, settings] of this.settings) {
					if(guild !== 'global' && !client.guilds.has(guild)) continue;
					this.setupGuildCommand(client.guilds.get(guild), command, settings);
				}
			})
			.on('groupRegister', group => {
				for(const [guild, settings] of this.settings) {
					if(guild !== 'global' && !client.guilds.has(guild)) continue;
					this.setupGuildGroup(client.guilds.get(guild), group, settings);
				}
			});
	}

	async destroy() {
		await Promise.all([
			this.insertOrReplaceStmt.finalize(),
			this.deleteStmt.finalize()
		]);
	}
{'fraction_non_alphanumeric': 0.15336134453781514, 'fraction_numerical': 0.0006464124111182935, 'mean_word_length': 3.4978197674418605, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, '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': '1425307', 'n_tokens_mistral': 2268, 'n_tokens_neox': 1940, 'n_words': 500}