OSDN Git Service

Validator.Messageを廃止した
[webchat/WebChat.git] / init.js
1 var util = require("util");
2 var config = require("./configure.js");
3 var async = require("async");
4
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 : "webchat",
12                 });
13
14 if("name_hash" in config.alias)
15 {
16         if(config.alias["name_hash"].type != "unsignednumber")
17                 throw "name_hashの型はunsignednumberでなければなりません";
18 }else{
19         throw "name_hashが存在しません";
20 }
21
22 if("name" in config.alias)
23 {
24         if(config.alias["name"].type != "text")
25                 throw "nameの型はtextでなければなりません";
26 }else{
27         throw "nameが存在しません";
28 }
29
30 if("password" in config.alias)
31 {
32         if(config.alias["password"].type != "password")
33                 throw "nameの型はpasswordでなければなりません";
34 }else{
35         throw "passwordが存在しません";
36 }
37
38 if("lastmodified" in config.alias)
39 {
40         if(config.alias["lastmodified"].type != "datetime")
41                 throw "lastmodifiedの型はtextでなければなりません";
42 }else{
43         throw "lastmodifiedが存在しません";
44 }
45
46 async.waterfall([
47         function(next){
48                 var query = GetDropTableQuery("profilelist"); 
49                 pool.query(query,null,next);
50         },
51         function(result,next){
52                 var query = GetCreateQuery(config.alias,"profilelist"); 
53                 pool.query(query,null,next);
54         },
55         function(result,next){
56                 var query = GetDropTableQuery("ipbanlist"); 
57                 pool.query(query,null,next);
58         },
59         function(result,next){
60                 var def = {
61                         ip:{
62                                 type : "text",
63                                 length : 64,
64                                 primary : true,
65                         },
66                         type:{
67                                 type : "text",
68                                 length : 1,
69                                 primary : true,
70                         },
71                 };
72                 var query = GetCreateQuery(def,"ipbanlist"); 
73                 pool.query(query,null,next);
74         },
75         function(result,next){
76                 var query = GetDropTableQuery("rooms"); 
77                 pool.query(query,null,next);
78         },
79         function(result,next){
80                 var def = {
81                         number:
82                                 {
83                                         type : "unsignednumber",
84                                         length:2,
85                                         isnotempty : true,
86                                         primary : true,
87                                 },
88                         password:{
89                                 type : "text",
90                                 length : 16,
91                         },
92                         hiddenlog:{
93                                 type : "bool",
94                         },
95                 };
96                 var query = GetCreateQuery(def,"rooms"); 
97                 pool.query(query,null,next);
98         },
99 ],function(err){
100         if(err != null)
101                 console.log(err);
102         process.exit();
103 });
104
105
106 function GetDropTableQuery(tablename)
107 {
108         var result = util.format("DROP TABLE IF EXISTS %s;",tablename);
109
110         console.log(result);
111
112         return result;
113 }
114
115 function GetCreateQuery(def,tablename)
116 {
117         var result = "CREATE TABLE " + tablename + "(";
118         for(key in def)
119         {
120                 if(typeof(def[key].nodefinetable) != "undefined" && def[key].nodefinetable)
121                         continue;
122                 switch(def[key].type)
123                 {
124                         case "text":
125                                 result += util.format("%s VARCHAR(%d) ",key,def[key].length);
126                                 break;
127                         case "number":
128                                 result += util.format("%s %s ",key,GetIntType(def[key].length));
129                                 break;
130                         case "unsignednumber":
131                                 result += util.format("%s %s UNSIGNED ",key,GetIntType(def[key].length));
132                                 break;
133                         case "mail":
134                                 result += util.format("%s VARCHAR(%d) ",key,def[key].length);
135                                 break;
136                         case "password":
137                                 result += util.format("%s VARCHAR(%d) ",key,def[key].length);
138                                 break;
139                         case "textarea":
140                                 result += util.format("%s TEXT ",key);
141                                 break;
142                         case "bool":
143                                 result += util.format("%s BOOL ",key);
144                                 break
145                         case "datetime":
146                                 result +=  util.format("%s DATETIME ",key);
147                                 break;
148                         default:
149                                 throw util.format("invaild %s type:%s",key,def[key].type);
150                 }
151                 if(typeof(def[key].isnotempty) != "undefined" && def[key].isnotempty)
152                         result += " NOT NULL ";
153                 result += ",";
154         }
155
156         for(key in def)
157         {
158                 if(typeof(def[key].primary) != "undefined" && def[key].primary)
159                 {
160                         result += util.format("PRIMARY KEY(%s)",key);
161                         break;
162                 }
163         }
164         result += ");";
165
166         console.log(result);
167
168         return result;
169 }
170
171 function GetIntType(len)
172 {
173         switch(len)
174         {
175                 case 1:
176                         return "TINYINT";
177                 case 2:
178                         return "SMALLINT";
179                 case 4:
180                         return "INT";
181         }
182         console.log(len);
183         throw "Invaild Length";
184 }