• R/O
  • HTTP
  • SSH
  • HTTPS

WebChat: Commit

チャットシステム


Commit MetaInfo

Revisionf1b33cf06fa5a63db6715de7ac205c143d3499c3 (tree)
Time2014-05-31 07:00:02
Authorgdkhd812 <jbh03215@hotm...>
Commitergdkhd812

Log Message

sequelizeに移行した

Change Summary

Incremental Difference

--- a/init.js
+++ b/init.js
@@ -1,18 +1,14 @@
11 var util = require("util");
22 var config = require("./configure.js");
33
4+var profilelist,ipbanlist,rooms;
5+
6+module.exports.GetProfileColletion = {};
7+module.exports.GetIpBanColletion = {};
8+module.exports.GetRoomInfomation = {};
49 module.exports = function(callback){
510 var async = require("async");
6-
7- var MySQLPool = new require("./mysql_pool.js");
8- var pool = new MySQLPool({
9- host : config.db_host,
10- user : config.db_user,
11- password : config.db_password,
12- port : config.db_port,
13- database : config.db_name,
14- });
15-
11+
1612 if("name_hash" in config.alias)
1713 {
1814 if(config.alias["name_hash"].type != "unsignednumber")
@@ -44,21 +40,19 @@ module.exports = function(callback){
4440 }else{
4541 throw "lastmodifiedが存在しません";
4642 }
47-
43+ var fs = require("fs");
4844 async.waterfall([
4945 function(next){
50- var query = GetDropTableQuery("profilelist");
51- pool.query(query,null,next);
52- },
53- function(result,next){
54- var query = GetCreateQuery(config.alias,"profilelist");
55- pool.query(query,null,next);
56- },
57- function(result,next){
58- var query = GetDropTableQuery("ipbanlist");
59- pool.query(query,null,next);
60- },
61- function(result,next){
46+ var Sequelize = require("sequelize");
47+ var pool = new Sequelize(config.db_name, config.db_user, config.db_password,{
48+ host:config.db_host,
49+ port:config.db_port
50+ });
51+
52+ var query = GetCreateQuery(Sequelize,config.alias,"profilelist");
53+ var profilelist = pool.define("profilelist",query);
54+ module.exports.GetProfileColletion = profilelist;
55+
6256 var def = {
6357 ip:{
6458 type : "text",
@@ -71,14 +65,10 @@ module.exports = function(callback){
7165 primary : true,
7266 },
7367 };
74- var query = GetCreateQuery(def,"ipbanlist");
75- pool.query(query,null,next);
76- },
77- function(result,next){
78- var query = GetDropTableQuery("rooms");
79- pool.query(query,null,next);
80- },
81- function(result,next){
68+ var query = GetCreateQuery(Sequelize,def,"ipbanlist");
69+ ipbanlist = pool.define("ipbanlist",query);
70+ module.exports.GetIpBanColletion = ipbanlist;
71+
8272 var def = {
8373 number:
8474 {
@@ -95,88 +85,81 @@ module.exports = function(callback){
9585 type : "bool",
9686 },
9787 };
98- var query = GetCreateQuery(def,"rooms");
99- pool.query(query,null,next);
88+ var query = GetCreateQuery(Sequelize,def,"rooms");
89+ rooms = pool.define("rooms",query);
90+ module.exports.GetRoomInfomation = rooms;
91+
92+ fs.exists("inited",function(exists){
93+ next(null,exists);
94+ });
10095 },
101- ],callback);
102-}
96+ function(exists,next){
97+ if(exists)
98+ next(null);
99+ else
100+ fs.open("inited","a",function(err,fd){
101+ fs.closeSync(fd);
103102
104-function GetDropTableQuery(tablename)
105-{
106- var result = util.format("DROP TABLE IF EXISTS %s;",tablename);
103+ module.exports.GetProfileColletion.drop();
104+ module.exports.GetProfileColletion.sync();
107105
108- console.log(result);
106+ module.exports.GetIpBanColletion.drop();
107+ module.exports.GetIpBanColletion.sync();
109108
110- return result;
109+ module.exports.GetRoomInfomation.drop();
110+ module.exports.GetRoomInfomation.sync();
111+
112+ next(null);
113+ });
114+ }
115+ ],function(err){
116+ callback(err);
117+ });
111118 }
112119
113-function GetCreateQuery(def,tablename)
120+function GetCreateQuery(Sequelize,def,tablename)
114121 {
115- var result = "CREATE TABLE " + tablename + "(";
122+ var result = {};
116123 for(key in def)
117124 {
118125 if(typeof(def[key].nodefinetable) != "undefined" && def[key].nodefinetable)
119126 continue;
127+ option = {};
120128 switch(def[key].type)
121129 {
122130 case "text":
123- result += util.format("%s VARCHAR(%d) ",key,def[key].length);
131+ option["type"] = Sequelize.STRING(def[key].length);
124132 break;
125133 case "number":
126- result += util.format("%s %s ",key,GetIntType(def[key].length));
134+ option["type"] = Sequelize.INTEGER(def[key].length);
127135 break;
128136 case "unsignednumber":
129- result += util.format("%s %s UNSIGNED ",key,GetIntType(def[key].length));
137+ option["type"] = Sequelize.INTEGER(def[key].length).UNSIGNED;
130138 break;
131139 case "mail":
132- result += util.format("%s VARCHAR(%d) ",key,def[key].length);
140+ option["type"] = Sequelize.STRING(def[key].length);
133141 break;
134142 case "password":
135- result += util.format("%s VARCHAR(%d) ",key,def[key].length);
143+ option["type"] = Sequelize.STRING(def[key].length);
136144 break;
137145 case "textarea":
138- result += util.format("%s TEXT ",key);
146+ option["type"] = Sequelize.TEXT;
139147 break;
140148 case "bool":
141- result += util.format("%s BOOL ",key);
149+ option["type"] = Sequelize.BOOLEAN;
142150 break
143151 case "datetime":
144- result += util.format("%s DATETIME ",key);
152+ option["type"] = Sequelize.DATE;
145153 break;
146154 default:
147155 throw util.format("invaild %s type:%s",key,def[key].type);
148156 }
149157 if(typeof(def[key].isnotempty) != "undefined" && def[key].isnotempty)
150- result += " NOT NULL ";
151- result += ",";
152- }
153-
154- for(key in def)
155- {
156- if(typeof(def[key].primary) != "undefined" && def[key].primary)
157- {
158- result += util.format("PRIMARY KEY(%s)",key);
159- break;
160- }
158+ option["allowNull"] = true;
159+ else if(typeof(def[key].primary) != "undefined" && def[key].primary)
160+ option["primaryKey"] = true;
161+ result[key] = option;
161162 }
162- result += ");";
163-
164- console.log(result);
165163
166164 return result;
167165 }
168-
169-function GetIntType(len)
170-{
171- switch(len)
172- {
173- case 1:
174- return "TINYINT";
175- case 2:
176- return "SMALLINT";
177- case 4:
178- return "INT";
179- }
180- console.log(len);
181- throw "Invaild Length";
182-}
--- a/ipban.js
+++ b/ipban.js
@@ -2,14 +2,7 @@
22 module.exports = function()
33 {
44 var config = require("./configure.js");
5- var MySQLPool = new require("./mysql_pool.js");
6- var pool = new MySQLPool({
7- host : config.db_host,
8- user : config.db_user,
9- password : config.db_password,
10- port : config.db_port,
11- database : config.db_name,
12- });
5+ var IpBanModel = require("./init").GetIpBanColletion;
136 var collection = {};
147 this.IsBaned = function(ip){
158 return collection[ip] == "r";
@@ -33,7 +26,7 @@ module.exports = function()
3326 var async = require("async");
3427 async.waterfall([
3528 function(next){
36- pool.query("TRUNCATE TABLE ipbanlist",null,next);
29+ IpBanModel.drop().done(next);
3730 },
3831 function(result,next){
3932 var items = new Array();
@@ -50,7 +43,8 @@ module.exports = function()
5043 collection[ip] = token[1];
5144 items.push(new Array(ip,collection[ip]));
5245 }
53- pool.query("INSERT INTO ipbanlist VALUES ?",[items],next);
46+ newIpBan = IpBanModel.build(items);
47+ newIpBan.save().done(next);
5448 },
5549 ],callfunc);
5650 }
@@ -59,7 +53,7 @@ module.exports = function()
5953 var async = require("async");
6054 async.waterfall([
6155 function(next){
62- pool.query("SELECT * FROM ipbanlist",null,next);
56+ IpBanModel.findAll().done(next);
6357 },
6458 function(result,next){
6559 for(var i = 0; i < result.length; i++)
--- a/main.js
+++ b/main.js
@@ -48,24 +48,12 @@ app.configure("production", function(){
4848 });
4949
5050 var async = require("async");
51-var fs = require("fs");
5251
5352 async.waterfall([
5453 function(next){
55- fs.exists("inited",function(exists){
56- next(null,exists);
57- });
54+ var init = require("./init");
55+ init(next);
5856 },
59- function(exists,next){
60- if(exists)
61- next(null);
62- else
63- fs.open("inited","a",function(err,fd){
64- fs.closeSync(fd);
65- var init = require("./init");
66- init(next);
67- });
68- }
6957 ],function(err){
7058 if(err != null)
7159 {
--- a/mysql_pool.js
+++ /dev/null
@@ -1,37 +0,0 @@
1-var async = require("async");
2-
3-module.exports = function(param)
4-{
5- this.query = function(query,param,callback)
6- {
7- async.waterfall([
8- function(next){
9- pool.acquire(next);
10- },
11- function(client,next){
12- client.query(query,param,function(err,result){
13- next(err,result,client);
14- });
15- },
16- function(result,client,next){
17- pool.release(client);
18- next(null,result);
19- }
20- ],callback);
21- }
22- var generic_pool = require("generic-pool");
23- var mysql = require("mysql");
24- pool = generic_pool.Pool({
25- name : "mysql",
26- max : 10,
27- create : function(cb){
28- var connection = mysql.createConnection(param);
29- connection.connect(function(err){
30- cb(err,connection);
31- });
32- },
33- destroy : function(db){
34- db.end();
35- }
36- });
37-}
--- a/package.json
+++ b/package.json
@@ -9,8 +9,8 @@
99 , "express": "3.1.0"
1010 , "socket.io": "0.9.13"
1111 , "date-utils" : "1.2.12"
12- , "generic-pool" : "2.0.3"
1312 , "mysql" : "2.0.0-alpha7"
13+ , "sequelize" : "1.7.x"
1414 , "murmurhash" : "0.0.2"
1515 , "lazy" : "1.0.9"
1616 }
--- a/profile.js
+++ b/profile.js
@@ -264,22 +264,15 @@ function RenderMessage(res,msg,info)
264264 //
265265 function ProfileCollection()
266266 {
267- var MySQLPool = new require("./mysql_pool.js");
268267 var murmurhash = require("murmurhash");
269- var pool = new MySQLPool({
270- host : config.db_host,
271- user : config.db_user,
272- password : config.db_password,
273- port : config.db_port,
274- database : config.db_name,
275- });
268+ var Profile = require("./init").GetProfileColletion;
276269 this.AuthAsync = function(name,password,cb){
277270 async.waterfall([
278271 function(next){
279- pool.query("SELECT password FROM profilelist WHERE name_hash = ? and name = ?",[murmurhash.v3(name),name],next);
272+ Profile.find({name_hash:murmurhash.v3(name), name:name}).done(next);
280273 },
281274 function(result,next){
282- if(result[0].password == md5_hex(password))
275+ if(result.password == md5_hex(password))
283276 next(null,true);
284277 else
285278 next(null,false);
@@ -287,30 +280,38 @@ function ProfileCollection()
287280 ],cb);
288281 }
289282 this.GetAsync = function(name,cb){
290- pool.query("SELECT * FROM profilelist WHERE name_hash = ? and name = ?",[murmurhash.v3(name),name],cb);
283+ Profile.find({name_hash:murmurhash.v3(name), name:name}).done(cb);
291284 }
292285 this.AddAsync = function(data,cb){
293- var item = GetItem(data);;
294- pool.query("INSERT INTO profilelist SET ?",[item],cb);
286+ newProfile = Profile.build(GetItem(data));
287+ newProfile.save().done(cb);
295288 }
296289 this.UpdatAsync = function(name,data,cb){
297- var item = GetItem(data);
298- pool.query("UPDATE profilelist SET ? WHERE name_hash = ? and name = ?",[item,murmurhash.v3(name),name],cb);
290+ Profile.update(GetItem(data),{name_hash:murmurhash.v3(name), name:name}).done(cb);
299291 }
300292 this.ClearAsync = function(cb){
301- pool.query("TRUNCATE TABLE profilelist",null,cb);
293+ Profile.drop().done(cb);
302294 }
303295 this.RemoveRangeAsync = function(names,cb){
304- pool.query("DELETE FROM profilelist WHERE name IN (?)",[names],cb);
296+ Profile.destroy({where:{
297+ in:[names]
298+ }
299+ }).done(cb);
305300 }
306301 this.RemoveAsync = function(name,cb){
307- pool.query("DELETE FROM profilelist WHERE name_hash = ? and name = ?",[murmurhash.v3(name),name],cb);
302+ Profile.destroy({name_hash:murmurhash.v3(name), name:name}).done(cb);
308303 }
309304 this.FindByNameAsync = function(pattern,start,count,cb){
310- pool.query("SELECT * FROM profilelist WHERE name LIKE ? LIMIT ?,?",[pattern+"%",start,count],cb);
305+ Profile.findAll({
306+ offset:start,
307+ limit:count,
308+ where:{
309+ like:pattern
310+ }
311+ }).done(cb);
311312 }
312313 this.ToArrayAsync = function(start,count,cb){
313- pool.query("SELECT name,lastmodified FROM profilelist LIMIT ?,?",[start,count],cb);
314+ Profile.findAll({offset:start,limit:count}).done(cb);
314315 }
315316
316317 var crypto = require("crypto");
--- a/public/profile/detail.ejs
+++ b/public/profile/detail.ejs
@@ -14,17 +14,17 @@ $(function(){
1414 <title>詳細画面</title>
1515 </head>
1616 <body>
17-<h1><%= list[0].name %>の詳細画面</h1>
17+<h1><%= list.name %>の詳細画面</h1>
1818 <div id="content">
1919 <% for(var key in alias) {%>
2020 <% if(typeof alias[key] != "undefined" && alias[key].visible) {%>
2121 <h2><%= alias[key].name %></h2>
22- <p id="item"><%- list[0][key] %></p>
22+ <p id="item"><%- list[key] %></p>
2323 <% } %>
2424 <% } %>
2525 <form action="/profile/detail" method="POST">
2626 <input type="hidden" name="_csrf" value="<%= token %>"></input>
27- <input type="hidden" value="<%= list[0].name %>" name="name"/>
27+ <input type="hidden" value="<%= list.name %>" name="name"/>
2828 <input type="submit" value="編集" name="edit"/>
2929 <input type="submit" value="削除" name="remove"/>
3030 <% if(!admin){ %>
--- a/public/profile/edit.ejs
+++ b/public/profile/edit.ejs
@@ -16,13 +16,13 @@
1616 <td><%= alias[key].name %></td>
1717 <td>
1818 <% if(alias[key].type == "textarea"){ %>
19- <textarea name="<%= key %>" rows="4" cols="50"><%= list[0][key] %></textarea>
19+ <textarea name="<%= key %>" rows="4" cols="50"><%= list[key] %></textarea>
2020 <% }else if(alias[key].type == "password"){ %>
2121 <input type="text" value="" name="<%= key %>"/><br/>
2222 <% }else if(typeof alias[key].readonly != "undefined" && alias[key].readonly){ %>
23- <input type="text" readonly value="<%= list[0][key] %>" name="<%= key %>"/>
23+ <input type="text" readonly value="<%= list[key] %>" name="<%= key %>"/>
2424 <% }else{ %>
25- <input type="text" value="<%= list[0][key] %>" name="<%= key %>"/>
25+ <input type="text" value="<%= list[key] %>" name="<%= key %>"/>
2626 <% } %>
2727 </td>
2828 </tr>
--- a/room.js
+++ b/room.js
@@ -3,14 +3,7 @@ module.exports = function()
33 {
44 var _this = this;
55 var config = require("./configure.js");
6- var MySQLPool = new require("./mysql_pool.js");
7- var pool = new MySQLPool({
8- host : config.db_host,
9- user : config.db_user,
10- password : config.db_password,
11- port : config.db_port,
12- database : config.db_name,
13- });
6+ var RoomInfomationModel = require("./init").GetRoomInfomation;
147 var collection = {};
158 this.Get = function(rno){
169 return collection[rno];
@@ -45,7 +38,7 @@ module.exports = function()
4538 var async = require("async");
4639 async.waterfall([
4740 function(next){
48- pool.query("TRUNCATE TABLE rooms",null,next);
41+ RoomInfomationModel.drop().done(next);
4942 },
5043 function(result,next){
5144 var util = require("util");
@@ -72,7 +65,8 @@ module.exports = function()
7265 Add(rno,password,romonly);
7366 items.push(new Array(rno,password,romonly));
7467 }
75- pool.query("INSERT INTO rooms VALUES ?",[items],callfunc);
68+ newRoom = RoomInfomationModel.build(items);
69+ newRoom.save().done(next);
7670 }
7771 ],callfunc);
7872 }
@@ -81,13 +75,12 @@ module.exports = function()
8175 var async = require("async");
8276 async.waterfall([
8377 function(next){
84- pool.query("SELECT * FROM rooms",null,next);
78+ RoomInfomationModel.findAll().done(next);
8579 },
8680 function(result,next){
8781 for(var i = 0; i < result.length; i++)
8882 {
89- //MySQLではTINYINTが使われている
90- Add(result[i].number,result[i].password,result[i].hiddenlog != 0);
83+ Add(result[i].number,result[i].password,result[i].hiddenlog);
9184 }
9285 next(null,null);
9386 }
Show on old repository browser