OSDN Git Service

ドキュメントの内容をわかりやすくした
[webchat/WebChat.git] / init.js
1 var util = require("util");
2 var config = require("./configure.js");
3
4 module.exports = function(callback){
5         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         
16         if("name_hash" in config.alias)
17         {
18                 if(config.alias["name_hash"].type != "unsignednumber")
19                         throw "name_hashの型はunsignednumberでなければなりません";
20         }else{
21                 throw "name_hashが存在しません";
22         }
23         
24         if("name" in config.alias)
25         {
26                 if(config.alias["name"].type != "text")
27                         throw "nameの型はtextでなければなりません";
28         }else{
29                 throw "nameが存在しません";
30         }
31         
32         if("password" in config.alias)
33         {
34                 if(config.alias["password"].type != "password")
35                         throw "nameの型はpasswordでなければなりません";
36         }else{
37                 throw "passwordが存在しません";
38         }
39         
40         if("lastmodified" in config.alias)
41         {
42                 if(config.alias["lastmodified"].type != "datetime")
43                         throw "lastmodifiedの型はtextでなければなりません";
44         }else{
45                 throw "lastmodifiedが存在しません";
46         }
47         
48         async.waterfall([
49                 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){
62                         var def = {
63                                 ip:{
64                                         type : "text",
65                                         length : 64,
66                                         primary : true,
67                                 },
68                                 type:{
69                                         type : "text",
70                                         length : 1,
71                                         primary : true,
72                                 },
73                         };
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){
82                         var def = {
83                                 number:
84                                         {
85                                                 type : "unsignednumber",
86                                                 length:2,
87                                                 isnotempty : true,
88                                                 primary : true,
89                                         },
90                                 password:{
91                                         type : "text",
92                                         length : 16,
93                                 },
94                                 hiddenlog:{
95                                         type : "bool",
96                                 },
97                         };
98                         var query = GetCreateQuery(def,"rooms"); 
99                         pool.query(query,null,next);
100                 },
101         ],callback);
102 }
103
104 function GetDropTableQuery(tablename)
105 {
106         var result = util.format("DROP TABLE IF EXISTS %s;",tablename);
107
108         console.log(result);
109
110         return result;
111 }
112
113 function GetCreateQuery(def,tablename)
114 {
115         var result = "CREATE TABLE " + tablename + "(";
116         for(key in def)
117         {
118                 if(typeof(def[key].nodefinetable) != "undefined" && def[key].nodefinetable)
119                         continue;
120                 switch(def[key].type)
121                 {
122                         case "text":
123                                 result += util.format("%s VARCHAR(%d) ",key,def[key].length);
124                                 break;
125                         case "number":
126                                 result += util.format("%s %s ",key,GetIntType(def[key].length));
127                                 break;
128                         case "unsignednumber":
129                                 result += util.format("%s %s UNSIGNED ",key,GetIntType(def[key].length));
130                                 break;
131                         case "mail":
132                                 result += util.format("%s VARCHAR(%d) ",key,def[key].length);
133                                 break;
134                         case "password":
135                                 result += util.format("%s VARCHAR(%d) ",key,def[key].length);
136                                 break;
137                         case "textarea":
138                                 result += util.format("%s TEXT ",key);
139                                 break;
140                         case "bool":
141                                 result += util.format("%s BOOL ",key);
142                                 break
143                         case "datetime":
144                                 result +=  util.format("%s DATETIME ",key);
145                                 break;
146                         default:
147                                 throw util.format("invaild %s type:%s",key,def[key].type);
148                 }
149                 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                 }
161         }
162         result += ");";
163
164         console.log(result);
165
166         return result;
167 }
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 }