/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


"use strict";

exports.userExists = function(dbConnection, username, callback, table)
{
	if (!table) table = 'uniusers';
	dbConnection.query(
		// Seems unsafe? It is.
		// But escaper doesn't know that table name and column value are different things.
		'SELECT count(*) AS result FROM `'+table+'` WHERE user = ?',
		[username],
		function (error, result){
			if (!!error)
			{
				callback(error, undefined);
			}
			else
			{
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.idExists = function(dbConnection, id, callback) {
	dbConnection.query(
		'SELECT count(*) FROM `uniusers` WHERE `id`= ?',
		[id],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.userExists = function(dbConnection, user, callback) {
	dbConnection.query(
		'SELECT count(*) FROM `uniusers` WHERE `user` = ?',
		[user],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.mailExists = function(dbConnection, mail, callback) {
	dbConnection.query(
		'SELECT count(*) FROM `uniusers` WHERE `mail` = ?',
		[mail],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.sessionExists = function(dbConnection, sess, callback) {
	dbConnection.query(
		'SELECT count(*) FROM `uniusers` WHERE `sessid` = ?',
		[sess],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.sessionActive = function(dbConnection, sess, callback) {
	dbConnection.query(
		'SELECT `sessexpire` > NOW() FROM `uniusers` WHERE `sessid` = ?',
		[sess],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


"use strict";

exports.userExists = function(dbConnection, username, callback, table)
{
	if (!table) table = 'uniusers';
	dbConnection.query(
		// Seems unsafe? It is.
		// But escaper doesn't know that table name and column value are different things.
		'SELECT count(*) AS result FROM `'+table+'` WHERE user = ?',
		[username],
		function (error, result){
			if (!!error)
			{
				callback(error, undefined);
			}
			else
			{
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.idExists = function(dbConnection, id, callback) {
	dbConnection.query(
		'SELECT count(*) FROM `uniusers` WHERE `id`= ?',
		[id],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.mailExists = function(dbConnection, mail, callback) {
	dbConnection.query(
		'SELECT count(*) FROM `uniusers` WHERE `mail` = ?',
		[mail],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.sessionExists = function(dbConnection, sess, callback) {
	dbConnection.query(
		'SELECT count(*) FROM `uniusers` WHERE `sessid` = ?',
		[sess],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

exports.sessionActive = function(dbConnection, sess, callback) {
	dbConnection.query(
		'SELECT `sessexpire` > NOW() FROM `uniusers` WHERE `sessid` = ?',
		[sess],
		function (error, result) {
			if (!!error) {
				callback(error, undefined);
			}
			else {
				callback(undefined, (result.rows[0].result > 0));
			}
		}
	);
};

Fix build broken by b6c8c27f4449664fb1497fea84e07a0e92b08f94.