OSDN Git Service

sequelizeに移行した
[webchat/WebChat.git] / init.js
diff --git a/init.js b/init.js
index 6bf7bd2..cbd80a6 100644 (file)
--- a/init.js
+++ b/init.js
@@ -1,18 +1,14 @@
 var util = require("util");
 var config = require("./configure.js");
 
+var profilelist,ipbanlist,rooms;
+
+module.exports.GetProfileColletion = {};
+module.exports.GetIpBanColletion = {};
+module.exports.GetRoomInfomation = {};
 module.exports = function(callback){
        var async = require("async");
-       
-       var MySQLPool = new require("./mysql_pool.js");
-       var pool = new MySQLPool({
-                               host     : config.db_host,
-                               user     : config.db_user,
-                               password : config.db_password,
-                               port     : config.db_port,
-                               database : config.db_name,
-                       });
-       
+               
        if("name_hash" in config.alias)
        {
                if(config.alias["name_hash"].type != "unsignednumber")
@@ -44,21 +40,19 @@ module.exports = function(callback){
        }else{
                throw "lastmodifiedが存在しません";
        }
-       
+       var fs = require("fs");
        async.waterfall([
                function(next){
-                       var query = GetDropTableQuery("profilelist"); 
-                       pool.query(query,null,next);
-               },
-               function(result,next){
-                       var query = GetCreateQuery(config.alias,"profilelist"); 
-                       pool.query(query,null,next);
-               },
-               function(result,next){
-                       var query = GetDropTableQuery("ipbanlist"); 
-                       pool.query(query,null,next);
-               },
-               function(result,next){
+                       var Sequelize = require("sequelize");
+                       var pool = new Sequelize(config.db_name, config.db_user, config.db_password,{
+                                               host:config.db_host,
+                                               port:config.db_port
+                                       });
+
+                       var query = GetCreateQuery(Sequelize,config.alias,"profilelist"); 
+                       var profilelist = pool.define("profilelist",query);
+                       module.exports.GetProfileColletion = profilelist;
+
                        var def = {
                                ip:{
                                        type : "text",
@@ -71,14 +65,10 @@ module.exports = function(callback){
                                        primary : true,
                                },
                        };
-                       var query = GetCreateQuery(def,"ipbanlist"); 
-                       pool.query(query,null,next);
-               },
-               function(result,next){
-                       var query = GetDropTableQuery("rooms"); 
-                       pool.query(query,null,next);
-               },
-               function(result,next){
+                       var query = GetCreateQuery(Sequelize,def,"ipbanlist"); 
+                       ipbanlist = pool.define("ipbanlist",query);
+                       module.exports.GetIpBanColletion = ipbanlist;
+
                        var def = {
                                number:
                                        {
@@ -95,88 +85,81 @@ module.exports = function(callback){
                                        type : "bool",
                                },
                        };
-                       var query = GetCreateQuery(def,"rooms"); 
-                       pool.query(query,null,next);
+                       var query = GetCreateQuery(Sequelize,def,"rooms"); 
+                       rooms = pool.define("rooms",query);
+                       module.exports.GetRoomInfomation = rooms;
+
+                       fs.exists("inited",function(exists){
+                               next(null,exists);
+                       });
                },
-       ],callback);
-}
+               function(exists,next){
+                       if(exists)
+                               next(null);
+                       else
+                               fs.open("inited","a",function(err,fd){
+                                       fs.closeSync(fd);
 
-function GetDropTableQuery(tablename)
-{
-       var result = util.format("DROP TABLE IF EXISTS %s;",tablename);
+                                       module.exports.GetProfileColletion.drop();
+                                       module.exports.GetProfileColletion.sync();
 
-       console.log(result);
+                                       module.exports.GetIpBanColletion.drop();
+                                       module.exports.GetIpBanColletion.sync();
 
-       return result;
+                                       module.exports.GetRoomInfomation.drop();
+                                       module.exports.GetRoomInfomation.sync();
+
+                                       next(null);
+                               });
+               }
+       ],function(err){
+               callback(err);
+       });
 }
 
-function GetCreateQuery(def,tablename)
+function GetCreateQuery(Sequelize,def,tablename)
 {
-       var result = "CREATE TABLE " + tablename + "(";
+       var result = {};
        for(key in def)
        {
                if(typeof(def[key].nodefinetable) != "undefined" && def[key].nodefinetable)
                        continue;
+               option = {};
                switch(def[key].type)
                {
                        case "text":
-                               result += util.format("%s VARCHAR(%d) ",key,def[key].length);
+                               option["type"] = Sequelize.STRING(def[key].length);
                                break;
                        case "number":
-                               result += util.format("%s %s ",key,GetIntType(def[key].length));
+                               option["type"] = Sequelize.INTEGER(def[key].length);
                                break;
                        case "unsignednumber":
-                               result += util.format("%s %s UNSIGNED ",key,GetIntType(def[key].length));
+                               option["type"] = Sequelize.INTEGER(def[key].length).UNSIGNED;
                                break;
                        case "mail":
-                               result += util.format("%s VARCHAR(%d) ",key,def[key].length);
+                               option["type"] = Sequelize.STRING(def[key].length);
                                break;
                        case "password":
-                               result += util.format("%s VARCHAR(%d) ",key,def[key].length);
+                               option["type"] = Sequelize.STRING(def[key].length);
                                break;
                        case "textarea":
-                               result += util.format("%s TEXT ",key);
+                               option["type"] = Sequelize.TEXT;
                                break;
                        case "bool":
-                               result += util.format("%s BOOL ",key);
+                               option["type"] = Sequelize.BOOLEAN;
                                break
                        case "datetime":
-                               result +=  util.format("%s DATETIME ",key);
+                               option["type"] = Sequelize.DATE;
                                break;
                        default:
                                throw util.format("invaild %s type:%s",key,def[key].type);
                }
                if(typeof(def[key].isnotempty) != "undefined" && def[key].isnotempty)
-                       result += " NOT NULL ";
-               result += ",";
-       }
-
-       for(key in def)
-       {
-               if(typeof(def[key].primary) != "undefined" && def[key].primary)
-               {
-                       result += util.format("PRIMARY KEY(%s)",key);
-                       break;
-               }
+                       option["allowNull"] = true;
+               else if(typeof(def[key].primary) != "undefined" && def[key].primary)
+                       option["primaryKey"] = true;
+               result[key] = option;
        }
-       result += ");";
-
-       console.log(result);
 
        return result;
 }
-
-function GetIntType(len)
-{
-       switch(len)
-       {
-               case 1:
-                       return "TINYINT";
-               case 2:
-                       return "SMALLINT";
-               case 4:
-                       return "INT";
-       }
-       console.log(len);
-       throw "Invaild Length";
-}