チャットシステム
Revision | f1b33cf06fa5a63db6715de7ac205c143d3499c3 (tree) |
---|---|
Time | 2014-05-31 07:00:02 |
Author | gdkhd812 <jbh03215@hotm...> |
Commiter | gdkhd812 |
sequelizeに移行した
@@ -1,18 +1,14 @@ | ||
1 | 1 | var util = require("util"); |
2 | 2 | var config = require("./configure.js"); |
3 | 3 | |
4 | +var profilelist,ipbanlist,rooms; | |
5 | + | |
6 | +module.exports.GetProfileColletion = {}; | |
7 | +module.exports.GetIpBanColletion = {}; | |
8 | +module.exports.GetRoomInfomation = {}; | |
4 | 9 | module.exports = function(callback){ |
5 | 10 | 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 | + | |
16 | 12 | if("name_hash" in config.alias) |
17 | 13 | { |
18 | 14 | if(config.alias["name_hash"].type != "unsignednumber") |
@@ -44,21 +40,19 @@ module.exports = function(callback){ | ||
44 | 40 | }else{ |
45 | 41 | throw "lastmodifiedが存在しません"; |
46 | 42 | } |
47 | - | |
43 | + var fs = require("fs"); | |
48 | 44 | async.waterfall([ |
49 | 45 | 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 | + | |
62 | 56 | var def = { |
63 | 57 | ip:{ |
64 | 58 | type : "text", |
@@ -71,14 +65,10 @@ module.exports = function(callback){ | ||
71 | 65 | primary : true, |
72 | 66 | }, |
73 | 67 | }; |
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 | + | |
82 | 72 | var def = { |
83 | 73 | number: |
84 | 74 | { |
@@ -95,88 +85,81 @@ module.exports = function(callback){ | ||
95 | 85 | type : "bool", |
96 | 86 | }, |
97 | 87 | }; |
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 | + }); | |
100 | 95 | }, |
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); | |
103 | 102 | |
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(); | |
107 | 105 | |
108 | - console.log(result); | |
106 | + module.exports.GetIpBanColletion.drop(); | |
107 | + module.exports.GetIpBanColletion.sync(); | |
109 | 108 | |
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 | + }); | |
111 | 118 | } |
112 | 119 | |
113 | -function GetCreateQuery(def,tablename) | |
120 | +function GetCreateQuery(Sequelize,def,tablename) | |
114 | 121 | { |
115 | - var result = "CREATE TABLE " + tablename + "("; | |
122 | + var result = {}; | |
116 | 123 | for(key in def) |
117 | 124 | { |
118 | 125 | if(typeof(def[key].nodefinetable) != "undefined" && def[key].nodefinetable) |
119 | 126 | continue; |
127 | + option = {}; | |
120 | 128 | switch(def[key].type) |
121 | 129 | { |
122 | 130 | case "text": |
123 | - result += util.format("%s VARCHAR(%d) ",key,def[key].length); | |
131 | + option["type"] = Sequelize.STRING(def[key].length); | |
124 | 132 | break; |
125 | 133 | case "number": |
126 | - result += util.format("%s %s ",key,GetIntType(def[key].length)); | |
134 | + option["type"] = Sequelize.INTEGER(def[key].length); | |
127 | 135 | break; |
128 | 136 | case "unsignednumber": |
129 | - result += util.format("%s %s UNSIGNED ",key,GetIntType(def[key].length)); | |
137 | + option["type"] = Sequelize.INTEGER(def[key].length).UNSIGNED; | |
130 | 138 | break; |
131 | 139 | case "mail": |
132 | - result += util.format("%s VARCHAR(%d) ",key,def[key].length); | |
140 | + option["type"] = Sequelize.STRING(def[key].length); | |
133 | 141 | break; |
134 | 142 | case "password": |
135 | - result += util.format("%s VARCHAR(%d) ",key,def[key].length); | |
143 | + option["type"] = Sequelize.STRING(def[key].length); | |
136 | 144 | break; |
137 | 145 | case "textarea": |
138 | - result += util.format("%s TEXT ",key); | |
146 | + option["type"] = Sequelize.TEXT; | |
139 | 147 | break; |
140 | 148 | case "bool": |
141 | - result += util.format("%s BOOL ",key); | |
149 | + option["type"] = Sequelize.BOOLEAN; | |
142 | 150 | break |
143 | 151 | case "datetime": |
144 | - result += util.format("%s DATETIME ",key); | |
152 | + option["type"] = Sequelize.DATE; | |
145 | 153 | break; |
146 | 154 | default: |
147 | 155 | throw util.format("invaild %s type:%s",key,def[key].type); |
148 | 156 | } |
149 | 157 | 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; | |
161 | 162 | } |
162 | - result += ");"; | |
163 | - | |
164 | - console.log(result); | |
165 | 163 | |
166 | 164 | return result; |
167 | 165 | } |
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 | -} |
@@ -2,14 +2,7 @@ | ||
2 | 2 | module.exports = function() |
3 | 3 | { |
4 | 4 | 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; | |
13 | 6 | var collection = {}; |
14 | 7 | this.IsBaned = function(ip){ |
15 | 8 | return collection[ip] == "r"; |
@@ -33,7 +26,7 @@ module.exports = function() | ||
33 | 26 | var async = require("async"); |
34 | 27 | async.waterfall([ |
35 | 28 | function(next){ |
36 | - pool.query("TRUNCATE TABLE ipbanlist",null,next); | |
29 | + IpBanModel.drop().done(next); | |
37 | 30 | }, |
38 | 31 | function(result,next){ |
39 | 32 | var items = new Array(); |
@@ -50,7 +43,8 @@ module.exports = function() | ||
50 | 43 | collection[ip] = token[1]; |
51 | 44 | items.push(new Array(ip,collection[ip])); |
52 | 45 | } |
53 | - pool.query("INSERT INTO ipbanlist VALUES ?",[items],next); | |
46 | + newIpBan = IpBanModel.build(items); | |
47 | + newIpBan.save().done(next); | |
54 | 48 | }, |
55 | 49 | ],callfunc); |
56 | 50 | } |
@@ -59,7 +53,7 @@ module.exports = function() | ||
59 | 53 | var async = require("async"); |
60 | 54 | async.waterfall([ |
61 | 55 | function(next){ |
62 | - pool.query("SELECT * FROM ipbanlist",null,next); | |
56 | + IpBanModel.findAll().done(next); | |
63 | 57 | }, |
64 | 58 | function(result,next){ |
65 | 59 | for(var i = 0; i < result.length; i++) |
@@ -48,24 +48,12 @@ app.configure("production", function(){ | ||
48 | 48 | }); |
49 | 49 | |
50 | 50 | var async = require("async"); |
51 | -var fs = require("fs"); | |
52 | 51 | |
53 | 52 | async.waterfall([ |
54 | 53 | function(next){ |
55 | - fs.exists("inited",function(exists){ | |
56 | - next(null,exists); | |
57 | - }); | |
54 | + var init = require("./init"); | |
55 | + init(next); | |
58 | 56 | }, |
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 | - } | |
69 | 57 | ],function(err){ |
70 | 58 | if(err != null) |
71 | 59 | { |
@@ -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 | -} |
@@ -9,8 +9,8 @@ | ||
9 | 9 | , "express": "3.1.0" |
10 | 10 | , "socket.io": "0.9.13" |
11 | 11 | , "date-utils" : "1.2.12" |
12 | - , "generic-pool" : "2.0.3" | |
13 | 12 | , "mysql" : "2.0.0-alpha7" |
13 | + , "sequelize" : "1.7.x" | |
14 | 14 | , "murmurhash" : "0.0.2" |
15 | 15 | , "lazy" : "1.0.9" |
16 | 16 | } |
@@ -264,22 +264,15 @@ function RenderMessage(res,msg,info) | ||
264 | 264 | // |
265 | 265 | function ProfileCollection() |
266 | 266 | { |
267 | - var MySQLPool = new require("./mysql_pool.js"); | |
268 | 267 | 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; | |
276 | 269 | this.AuthAsync = function(name,password,cb){ |
277 | 270 | async.waterfall([ |
278 | 271 | 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); | |
280 | 273 | }, |
281 | 274 | function(result,next){ |
282 | - if(result[0].password == md5_hex(password)) | |
275 | + if(result.password == md5_hex(password)) | |
283 | 276 | next(null,true); |
284 | 277 | else |
285 | 278 | next(null,false); |
@@ -287,30 +280,38 @@ function ProfileCollection() | ||
287 | 280 | ],cb); |
288 | 281 | } |
289 | 282 | 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); | |
291 | 284 | } |
292 | 285 | 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); | |
295 | 288 | } |
296 | 289 | 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); | |
299 | 291 | } |
300 | 292 | this.ClearAsync = function(cb){ |
301 | - pool.query("TRUNCATE TABLE profilelist",null,cb); | |
293 | + Profile.drop().done(cb); | |
302 | 294 | } |
303 | 295 | 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); | |
305 | 300 | } |
306 | 301 | 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); | |
308 | 303 | } |
309 | 304 | 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); | |
311 | 312 | } |
312 | 313 | 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); | |
314 | 315 | } |
315 | 316 | |
316 | 317 | var crypto = require("crypto"); |
@@ -14,17 +14,17 @@ $(function(){ | ||
14 | 14 | <title>詳細画面</title> |
15 | 15 | </head> |
16 | 16 | <body> |
17 | -<h1><%= list[0].name %>の詳細画面</h1> | |
17 | +<h1><%= list.name %>の詳細画面</h1> | |
18 | 18 | <div id="content"> |
19 | 19 | <% for(var key in alias) {%> |
20 | 20 | <% if(typeof alias[key] != "undefined" && alias[key].visible) {%> |
21 | 21 | <h2><%= alias[key].name %></h2> |
22 | - <p id="item"><%- list[0][key] %></p> | |
22 | + <p id="item"><%- list[key] %></p> | |
23 | 23 | <% } %> |
24 | 24 | <% } %> |
25 | 25 | <form action="/profile/detail" method="POST"> |
26 | 26 | <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"/> | |
28 | 28 | <input type="submit" value="編集" name="edit"/> |
29 | 29 | <input type="submit" value="削除" name="remove"/> |
30 | 30 | <% if(!admin){ %> |
@@ -16,13 +16,13 @@ | ||
16 | 16 | <td><%= alias[key].name %></td> |
17 | 17 | <td> |
18 | 18 | <% 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> | |
20 | 20 | <% }else if(alias[key].type == "password"){ %> |
21 | 21 | <input type="text" value="" name="<%= key %>"/><br/> |
22 | 22 | <% }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 %>"/> | |
24 | 24 | <% }else{ %> |
25 | - <input type="text" value="<%= list[0][key] %>" name="<%= key %>"/> | |
25 | + <input type="text" value="<%= list[key] %>" name="<%= key %>"/> | |
26 | 26 | <% } %> |
27 | 27 | </td> |
28 | 28 | </tr> |
@@ -3,14 +3,7 @@ module.exports = function() | ||
3 | 3 | { |
4 | 4 | var _this = this; |
5 | 5 | 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; | |
14 | 7 | var collection = {}; |
15 | 8 | this.Get = function(rno){ |
16 | 9 | return collection[rno]; |
@@ -45,7 +38,7 @@ module.exports = function() | ||
45 | 38 | var async = require("async"); |
46 | 39 | async.waterfall([ |
47 | 40 | function(next){ |
48 | - pool.query("TRUNCATE TABLE rooms",null,next); | |
41 | + RoomInfomationModel.drop().done(next); | |
49 | 42 | }, |
50 | 43 | function(result,next){ |
51 | 44 | var util = require("util"); |
@@ -72,7 +65,8 @@ module.exports = function() | ||
72 | 65 | Add(rno,password,romonly); |
73 | 66 | items.push(new Array(rno,password,romonly)); |
74 | 67 | } |
75 | - pool.query("INSERT INTO rooms VALUES ?",[items],callfunc); | |
68 | + newRoom = RoomInfomationModel.build(items); | |
69 | + newRoom.save().done(next); | |
76 | 70 | } |
77 | 71 | ],callfunc); |
78 | 72 | } |
@@ -81,13 +75,12 @@ module.exports = function() | ||
81 | 75 | var async = require("async"); |
82 | 76 | async.waterfall([ |
83 | 77 | function(next){ |
84 | - pool.query("SELECT * FROM rooms",null,next); | |
78 | + RoomInfomationModel.findAll().done(next); | |
85 | 79 | }, |
86 | 80 | function(result,next){ |
87 | 81 | for(var i = 0; i < result.length; i++) |
88 | 82 | { |
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); | |
91 | 84 | } |
92 | 85 | next(null,null); |
93 | 86 | } |