OSDN Git Service

メッセージの文字化けを修正した
[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("list"); 
49                 pool.query(query,null,next);
50         },
51         function(result,next){
52                 var query = GetCreateQuery(config.alias,"list"); 
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                 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 }