var util = require("util");
var config = require("./configure.js");
+var profilelist,ipbanlist,rooms;
+
+module.exports.GetProfileColletion = {};
+module.exports.GetIpBanColletion = {};
+module.exports.GetRoomInfomation = {};
module.exports = function(callback){
var async = require("async");
-
- var MySQLPool = new require("./mysql_pool.js");
- var pool = new MySQLPool({
- host : config.db_host,
- user : config.db_user,
- password : config.db_password,
- port : config.db_port,
- database : config.db_name,
- });
-
+
if("name_hash" in config.alias)
{
if(config.alias["name_hash"].type != "unsignednumber")
}else{
throw "lastmodifiedが存在しません";
}
-
+ var fs = require("fs");
async.waterfall([
function(next){
- var query = GetDropTableQuery("profilelist");
- pool.query(query,null,next);
- },
- function(result,next){
- var query = GetCreateQuery(config.alias,"profilelist");
- pool.query(query,null,next);
- },
- function(result,next){
- var query = GetDropTableQuery("ipbanlist");
- pool.query(query,null,next);
- },
- function(result,next){
+ var Sequelize = require("sequelize");
+ var pool = new Sequelize(config.db_name, config.db_user, config.db_password,{
+ host:config.db_host,
+ port:config.db_port
+ });
+
+ var query = GetCreateQuery(Sequelize,config.alias,"profilelist");
+ var profilelist = pool.define("profilelist",query);
+ module.exports.GetProfileColletion = profilelist;
+
var def = {
ip:{
type : "text",
primary : true,
},
};
- var query = GetCreateQuery(def,"ipbanlist");
- pool.query(query,null,next);
- },
- function(result,next){
- var query = GetDropTableQuery("rooms");
- pool.query(query,null,next);
- },
- function(result,next){
+ var query = GetCreateQuery(Sequelize,def,"ipbanlist");
+ ipbanlist = pool.define("ipbanlist",query);
+ module.exports.GetIpBanColletion = ipbanlist;
+
var def = {
number:
{
type : "bool",
},
};
- var query = GetCreateQuery(def,"rooms");
- pool.query(query,null,next);
+ var query = GetCreateQuery(Sequelize,def,"rooms");
+ rooms = pool.define("rooms",query);
+ module.exports.GetRoomInfomation = rooms;
+
+ fs.exists("inited",function(exists){
+ next(null,exists);
+ });
},
- ],callback);
-}
+ function(exists,next){
+ if(exists)
+ next(null);
+ else
+ fs.open("inited","a",function(err,fd){
+ fs.closeSync(fd);
-function GetDropTableQuery(tablename)
-{
- var result = util.format("DROP TABLE IF EXISTS %s;",tablename);
+ module.exports.GetProfileColletion.drop();
+ module.exports.GetProfileColletion.sync();
- console.log(result);
+ module.exports.GetIpBanColletion.drop();
+ module.exports.GetIpBanColletion.sync();
- return result;
+ module.exports.GetRoomInfomation.drop();
+ module.exports.GetRoomInfomation.sync();
+
+ next(null);
+ });
+ }
+ ],function(err){
+ callback(err);
+ });
}
-function GetCreateQuery(def,tablename)
+function GetCreateQuery(Sequelize,def,tablename)
{
- var result = "CREATE TABLE " + tablename + "(";
+ var result = {};
for(key in def)
{
if(typeof(def[key].nodefinetable) != "undefined" && def[key].nodefinetable)
continue;
+ option = {};
switch(def[key].type)
{
case "text":
- result += util.format("%s VARCHAR(%d) ",key,def[key].length);
+ option["type"] = Sequelize.STRING(def[key].length);
break;
case "number":
- result += util.format("%s %s ",key,GetIntType(def[key].length));
+ option["type"] = Sequelize.INTEGER(def[key].length);
break;
case "unsignednumber":
- result += util.format("%s %s UNSIGNED ",key,GetIntType(def[key].length));
+ option["type"] = Sequelize.INTEGER(def[key].length).UNSIGNED;
break;
case "mail":
- result += util.format("%s VARCHAR(%d) ",key,def[key].length);
+ option["type"] = Sequelize.STRING(def[key].length);
break;
case "password":
- result += util.format("%s VARCHAR(%d) ",key,def[key].length);
+ option["type"] = Sequelize.STRING(def[key].length);
break;
case "textarea":
- result += util.format("%s TEXT ",key);
+ option["type"] = Sequelize.TEXT;
break;
case "bool":
- result += util.format("%s BOOL ",key);
+ option["type"] = Sequelize.BOOLEAN;
break
case "datetime":
- result += util.format("%s DATETIME ",key);
+ option["type"] = Sequelize.DATE;
break;
default:
throw util.format("invaild %s type:%s",key,def[key].type);
}
if(typeof(def[key].isnotempty) != "undefined" && def[key].isnotempty)
- result += " NOT NULL ";
- result += ",";
- }
-
- for(key in def)
- {
- if(typeof(def[key].primary) != "undefined" && def[key].primary)
- {
- result += util.format("PRIMARY KEY(%s)",key);
- break;
- }
+ option["allowNull"] = true;
+ else if(typeof(def[key].primary) != "undefined" && def[key].primary)
+ option["primaryKey"] = true;
+ result[key] = option;
}
- result += ");";
-
- console.log(result);
return result;
}
-
-function GetIntType(len)
-{
- switch(len)
- {
- case 1:
- return "TINYINT";
- case 2:
- return "SMALLINT";
- case 4:
- return "INT";
- }
- console.log(len);
- throw "Invaild Length";
-}
module.exports = function()
{
var config = require("./configure.js");
- var MySQLPool = new require("./mysql_pool.js");
- var pool = new MySQLPool({
- host : config.db_host,
- user : config.db_user,
- password : config.db_password,
- port : config.db_port,
- database : config.db_name,
- });
+ var IpBanModel = require("./init").GetIpBanColletion;
var collection = {};
this.IsBaned = function(ip){
return collection[ip] == "r";
var async = require("async");
async.waterfall([
function(next){
- pool.query("TRUNCATE TABLE ipbanlist",null,next);
+ IpBanModel.drop().done(next);
},
function(result,next){
var items = new Array();
collection[ip] = token[1];
items.push(new Array(ip,collection[ip]));
}
- pool.query("INSERT INTO ipbanlist VALUES ?",[items],next);
+ newIpBan = IpBanModel.build(items);
+ newIpBan.save().done(next);
},
],callfunc);
}
var async = require("async");
async.waterfall([
function(next){
- pool.query("SELECT * FROM ipbanlist",null,next);
+ IpBanModel.findAll().done(next);
},
function(result,next){
for(var i = 0; i < result.length; i++)
});\r
\r
var async = require("async");\r
-var fs = require("fs");\r
\r
async.waterfall([\r
function(next){\r
- fs.exists("inited",function(exists){\r
- next(null,exists);\r
- });\r
+ var init = require("./init");\r
+ init(next);\r
},\r
- function(exists,next){\r
- if(exists)\r
- next(null);\r
- else\r
- fs.open("inited","a",function(err,fd){\r
- fs.closeSync(fd);\r
- var init = require("./init");\r
- init(next);\r
- });\r
- }\r
],function(err){\r
if(err != null)\r
{\r
+++ /dev/null
-var async = require("async");
-
-module.exports = function(param)
-{
- this.query = function(query,param,callback)
- {
- async.waterfall([
- function(next){
- pool.acquire(next);
- },
- function(client,next){
- client.query(query,param,function(err,result){
- next(err,result,client);
- });
- },
- function(result,client,next){
- pool.release(client);
- next(null,result);
- }
- ],callback);
- }
- var generic_pool = require("generic-pool");
- var mysql = require("mysql");
- pool = generic_pool.Pool({
- name : "mysql",
- max : 10,
- create : function(cb){
- var connection = mysql.createConnection(param);
- connection.connect(function(err){
- cb(err,connection);
- });
- },
- destroy : function(db){
- db.end();
- }
- });
-}
, "express": "3.1.0"
, "socket.io": "0.9.13"
, "date-utils" : "1.2.12"
- , "generic-pool" : "2.0.3"
, "mysql" : "2.0.0-alpha7"
+ , "sequelize" : "1.7.x"
, "murmurhash" : "0.0.2"
, "lazy" : "1.0.9"
}
//\r
function ProfileCollection()\r
{\r
- var MySQLPool = new require("./mysql_pool.js");\r
var murmurhash = require("murmurhash");\r
- var pool = new MySQLPool({\r
- host : config.db_host,\r
- user : config.db_user,\r
- password : config.db_password,\r
- port : config.db_port,\r
- database : config.db_name,\r
- });\r
+ var Profile = require("./init").GetProfileColletion;\r
this.AuthAsync = function(name,password,cb){\r
async.waterfall([\r
function(next){\r
- pool.query("SELECT password FROM profilelist WHERE name_hash = ? and name = ?",[murmurhash.v3(name),name],next);\r
+ Profile.find({name_hash:murmurhash.v3(name), name:name}).done(next);\r
},\r
function(result,next){\r
- if(result[0].password == md5_hex(password))\r
+ if(result.password == md5_hex(password))\r
next(null,true);\r
else\r
next(null,false);\r
],cb);\r
}\r
this.GetAsync = function(name,cb){\r
- pool.query("SELECT * FROM profilelist WHERE name_hash = ? and name = ?",[murmurhash.v3(name),name],cb);\r
+ Profile.find({name_hash:murmurhash.v3(name), name:name}).done(cb);\r
}\r
this.AddAsync = function(data,cb){\r
- var item = GetItem(data);;\r
- pool.query("INSERT INTO profilelist SET ?",[item],cb);\r
+ newProfile = Profile.build(GetItem(data));\r
+ newProfile.save().done(cb);\r
}\r
this.UpdatAsync = function(name,data,cb){\r
- var item = GetItem(data);\r
- pool.query("UPDATE profilelist SET ? WHERE name_hash = ? and name = ?",[item,murmurhash.v3(name),name],cb);\r
+ Profile.update(GetItem(data),{name_hash:murmurhash.v3(name), name:name}).done(cb);\r
}\r
this.ClearAsync = function(cb){\r
- pool.query("TRUNCATE TABLE profilelist",null,cb);\r
+ Profile.drop().done(cb);\r
}\r
this.RemoveRangeAsync = function(names,cb){\r
- pool.query("DELETE FROM profilelist WHERE name IN (?)",[names],cb);\r
+ Profile.destroy({where:{\r
+ in:[names]\r
+ }\r
+ }).done(cb);\r
}\r
this.RemoveAsync = function(name,cb){\r
- pool.query("DELETE FROM profilelist WHERE name_hash = ? and name = ?",[murmurhash.v3(name),name],cb);\r
+ Profile.destroy({name_hash:murmurhash.v3(name), name:name}).done(cb);\r
}\r
this.FindByNameAsync = function(pattern,start,count,cb){\r
- pool.query("SELECT * FROM profilelist WHERE name LIKE ? LIMIT ?,?",[pattern+"%",start,count],cb);\r
+ Profile.findAll({\r
+ offset:start,\r
+ limit:count,\r
+ where:{\r
+ like:pattern\r
+ }\r
+ }).done(cb);\r
}\r
this.ToArrayAsync = function(start,count,cb){\r
- pool.query("SELECT name,lastmodified FROM profilelist LIMIT ?,?",[start,count],cb);\r
+ Profile.findAll({offset:start,limit:count}).done(cb);\r
}\r
\r
var crypto = require("crypto");\r
<title>詳細画面</title>\r
</head>\r
<body>\r
-<h1><%= list[0].name %>の詳細画面</h1>\r
+<h1><%= list.name %>の詳細画面</h1>\r
<div id="content">\r
<% for(var key in alias) {%>\r
<% if(typeof alias[key] != "undefined" && alias[key].visible) {%>\r
<h2><%= alias[key].name %></h2>\r
- <p id="item"><%- list[0][key] %></p>\r
+ <p id="item"><%- list[key] %></p>\r
<% } %>\r
<% } %>\r
<form action="/profile/detail" method="POST">\r
<input type="hidden" name="_csrf" value="<%= token %>"></input>\r
- <input type="hidden" value="<%= list[0].name %>" name="name"/>\r
+ <input type="hidden" value="<%= list.name %>" name="name"/>\r
<input type="submit" value="編集" name="edit"/>\r
<input type="submit" value="削除" name="remove"/>\r
<% if(!admin){ %>\r
<td><%= alias[key].name %></td>\r
<td>\r
<% if(alias[key].type == "textarea"){ %>\r
- <textarea name="<%= key %>" rows="4" cols="50"><%= list[0][key] %></textarea>\r
+ <textarea name="<%= key %>" rows="4" cols="50"><%= list[key] %></textarea>\r
<% }else if(alias[key].type == "password"){ %>\r
<input type="text" value="" name="<%= key %>"/><br/>\r
<% }else if(typeof alias[key].readonly != "undefined" && alias[key].readonly){ %>\r
- <input type="text" readonly value="<%= list[0][key] %>" name="<%= key %>"/>\r
+ <input type="text" readonly value="<%= list[key] %>" name="<%= key %>"/>\r
<% }else{ %>\r
- <input type="text" value="<%= list[0][key] %>" name="<%= key %>"/>\r
+ <input type="text" value="<%= list[key] %>" name="<%= key %>"/>\r
<% } %>\r
</td>\r
</tr>\r
{
var _this = this;
var config = require("./configure.js");
- var MySQLPool = new require("./mysql_pool.js");
- var pool = new MySQLPool({
- host : config.db_host,
- user : config.db_user,
- password : config.db_password,
- port : config.db_port,
- database : config.db_name,
- });
+ var RoomInfomationModel = require("./init").GetRoomInfomation;
var collection = {};
this.Get = function(rno){
return collection[rno];
var async = require("async");
async.waterfall([
function(next){
- pool.query("TRUNCATE TABLE rooms",null,next);
+ RoomInfomationModel.drop().done(next);
},
function(result,next){
var util = require("util");
Add(rno,password,romonly);
items.push(new Array(rno,password,romonly));
}
- pool.query("INSERT INTO rooms VALUES ?",[items],callfunc);
+ newRoom = RoomInfomationModel.build(items);
+ newRoom.save().done(next);
}
],callfunc);
}
var async = require("async");
async.waterfall([
function(next){
- pool.query("SELECT * FROM rooms",null,next);
+ RoomInfomationModel.findAll().done(next);
},
function(result,next){
for(var i = 0; i < result.length; i++)
{
- //MySQL\82Å\82ÍTINYINT\82ª\8eg\82í\82ê\82Ä\82¢\82é
- Add(result[i].number,result[i].password,result[i].hiddenlog != 0);
+ Add(result[i].number,result[i].password,result[i].hiddenlog);
}
next(null,null);
}