more database work

This commit is contained in:
calzoneman 2013-05-25 17:12:08 -04:00
parent a6af5a2440
commit 098a30bbb0
2 changed files with 111 additions and 17 deletions

View File

@ -1,24 +1,35 @@
var mysql = require("mysql-libmysqlclient"); var mysql = require("mysql-libmysqlclient");
var Config = require("./config");
var Logger = require("./logger"); var Logger = require("./logger");
var db = false; var db = false;
var SERVER = "";
var USER = "";
var DATABASE = "";
var PASSWORD = "";
var CONFIG = {};
function setup(cfg) {
SERVER = cfg.MYSQL_SERVER;
USER = cfg.MYSQL_USER;
DATABASE = cfg.MYSQL_DB;
PASSWORD = cfg.MYSQL_PASSWORD;
CONFIG = cfg;
}
function getConnection() { function getConnection() {
if(db && db.connectedSync()) { if(db && db.connectedSync()) {
return db; return db;
} }
db = mysql.createConnectionSync(); db = mysql.createConnectionSync();
db.connectSync(Config.MYSQL_SERVER, Config.MYSQL_USER, db.connectSync(SERVER, USER, PASSWORD, DATABASE);
Config.MYSQL_PASSWORD, Config.MYSQL_DB);
if(!db.connectedSync()) { if(!db.connectedSync()) {
//Logger.errlog.log("DB connection failed"); //Logger.errlog.log("DB connection failed");
return false; return false;
} }
if(Config.DEBUG) { if(CONFIG.DEBUG) {
db._querySync = db.querySync; db._querySync = db.querySync;
db.querySync = function(q) { db.querySync = function(q) {
Logger.syslog.log("DEBUG: " + q); Logger.syslog.log("DEBUG: " + q);
this._querySync(q); return this._querySync(q);
} }
} }
return db; return db;
@ -114,13 +125,14 @@ function refreshGlobalBans() {
if(!results) { if(!results) {
Logger.errlog.log("! Failed to load global bans"); Logger.errlog.log("! Failed to load global bans");
} }
else {
var rows = results.fetchAllSync(); var rows = results.fetchAllSync();
global_bans = {}; global_bans = {};
for(var i = 0; i < rows.length; i++) { for(var i = 0; i < rows.length; i++) {
global_bans[rows[i].ip] = rows[i].note; global_bans[rows[i].ip] = rows[i].note;
} }
} }
}
function globalBanIP(ip, reason) { function globalBanIP(ip, reason) {
var db = getConnection(); var db = getConnection();
@ -153,8 +165,7 @@ function globalUnbanIP(ip) {
function registerChannel(name) { function registerChannel(name) {
var db = getConnection(); var db = getConnection();
if(!db) { if(!db) { return false;
return false;
} }
// Library table // Library table
@ -367,11 +378,63 @@ function getChannelRank(chan, name) {
return rows[0].rank; return rows[0].rank;
} }
function test() { function setChannelRank(chan, name, rank) {
var db = getConnection(); var db = getConnection();
var q = createQuery("INSERT INTO `?` VALUES (?, ?, ?, ?, ?, ?, ?, ?)", ["registrations", null, "bob2", "asdf", 1, "", 0, "", ""]); if(!db) {
console.log(q); return false;
//console.log(db.querySync(q));
} }
test(); var query = createQuery(
"INSERT INTO `?` VALUES (?, ?) ON DUPLICATE KEY UPDATE",
["chan_"+chan+"_ranks", rank, name]
);
return db.querySync(query);
}
function addToLibrary(chan, media) {
var db = getConnection();
if(!db) {
return false;
}
var query = createQuery(
["INSERT INTO `?` ",
"(`id`, `title`, `seconds`, `type`) ",
"VALUES ",
"(?, ?, ?, ?)"].join(""),
["chan_"+chan+"_library", media.id, media.title, media.seconds, media.type]
);
return db.querySync(query);
}
function removeFromLibrary(chan, id) {
var db = getConnection();
if(!db) {
return false;
}
var query = createQuery(
"DELETE FROM `?` WHERE id=?",
["chan_"+chan+"_library", id]
);
return db.querySync(query);
}
exports.setup = setup;
exports.getConnection = getConnection;
exports.createQuery = createQuery;
exports.init = init;
exports.checkGlobalBan = checkGlobalBan;
exports.refreshGlobalBans = refreshGlobalBans;
exports.globalBanIP = globalBanIP;
exports.globalUnbanIP = globalUnbanIP;
exports.registerChannel = registerChannel;
exports.loadChannel = loadChannel;
exports.deleteChannel = deleteChannel;
exports.getChannelRank = getChannelRank;
exports.setChannelRank = setChannelRank;
exports.addToLibrary = addToLibrary;
exports.removeFromLibrary = removeFromLibrary;

31
tests/database-new.js Normal file
View File

@ -0,0 +1,31 @@
var Database = require("../database-new");
Database.setup(require("../config-testing"));
var assert = require("assert");
var db = Database.getConnection();
// Empty database
db.realQuerySync("SHOW TABLES;");
result = db.storeResultSync();
var tables = [];
var tmp = result.fetchAllSync({"asArray": true});
tmp.forEach(function(t) { tables.push(t[0]); });
db.querySync("DROP TABLE " + tables.join(", "));
Database.init();
// Test global bans
assert(!Database.checkGlobalBan("127.0.0.1"));
Database.globalBanIP("192.168.1.12", "test");
Database.globalBanIP("192.168.2", "test");
Database.globalBanIP("192.167", "test");
Database.refreshGlobalBans();
assert(Database.checkGlobalBan("192.168.1.12"));
assert(Database.checkGlobalBan("192.168.2.24"));
assert(Database.checkGlobalBan("192.167.1.15"));
Database.globalUnbanIP("192.168.1.12");
Database.globalUnbanIP("192.167");
Database.refreshGlobalBans();
assert(!Database.checkGlobalBan("192.168.1.12"));
assert(!Database.checkGlobalBan("192.167.5.54"));
console.log("[PASS] Global Bans");
process.exit(0);