var util = require("util");
var config = require("./configure.js");
-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 : "webchat",
- });
+var profilelist,ipbanlist,rooms;
-if("name_hash" in config.alias)
-{
- if(config.alias["name_hash"].type != "unsignednumber")
- throw "name_hashの型はunsignednumberでなければなりません";
-}else{
- throw "name_hashが存在しません";
-}
+module.exports.GetProfileColletion = {};
+module.exports.GetIpBanColletion = {};
+module.exports.GetRoomInfomation = {};
+module.exports = function(callback){
+ var async = require("async");
+
+ if("name_hash" in config.alias)
+ {
+ if(config.alias["name_hash"].type != "unsignednumber")
+ throw "name_hashの型はunsignednumberでなければなりません";
+ }else{
+ throw "name_hashが存在しません";
+ }
+
+ if("name" in config.alias)
+ {
+ if(config.alias["name"].type != "text")
+ throw "nameの型はtextでなければなりません";
+ }else{
+ throw "nameが存在しません";
+ }
+
+ if("password" in config.alias)
+ {
+ if(config.alias["password"].type != "password")
+ throw "nameの型はpasswordでなければなりません";
+ }else{
+ throw "passwordが存在しません";
+ }
+
+ if("lastmodified" in config.alias)
+ {
+ if(config.alias["lastmodified"].type != "datetime")
+ throw "lastmodifiedの型はtextでなければなりません";
+ }else{
+ throw "lastmodifiedが存在しません";
+ }
-if("name" in config.alias)
-{
- if(config.alias["name"].type != "text")
- throw "nameの型はtextでなければなりません";
-}else{
- throw "nameが存在しません";
-}
+ 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
+ });
-if("password" in config.alias)
-{
- if(config.alias["password"].type != "password")
- throw "nameの型はpasswordでなければなりません";
-}else{
- throw "passwordが存在しません";
-}
+ var fs = require("fs");
-if("lastmodified" in config.alias)
-{
- if(config.alias["lastmodified"].type != "datetime")
- throw "lastmodifiedの型はtextでなければなりません";
-}else{
- throw "lastmodifiedが存在しません";
-}
+ async.waterfall([
+ function(next){
+ var query = GetCreateQuery(Sequelize,config.alias,"profilelist");
+ var profilelist = pool.define("profilelist",query);
+ module.exports.GetProfileColletion = profilelist;
-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 def = {
- ip:{
- type : "text",
- length : 64,
- primary : true,
- },
- type:{
- type : "text",
- length : 1,
- 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 def = {
- number:
- {
- type : "unsignednumber",
- length:2,
- isnotempty : true,
+ var def = {
+ ip:{
+ type : "text",
+ length : 64,
primary : true,
},
- password:{
- type : "text",
- length : 16,
- },
- hiddenlog:{
- type : "bool",
- },
- };
- var query = GetCreateQuery(def,"rooms");
- pool.query(query,null,next);
- },
-],function(err){
- if(err != null)
- console.log(err);
- process.exit();
-});
-
-
-function GetDropTableQuery(tablename)
-{
- var result = util.format("DROP TABLE IF EXISTS %s;",tablename);
+ type:{
+ type : "text",
+ length : 1,
+ },
+ };
+ var query = GetCreateQuery(Sequelize,def,"ipbanlist");
+ ipbanlist = pool.define("ipbanlist",query);
+ module.exports.GetIpBanColletion = ipbanlist;
- console.log(result);
+ var def = {
+ number:
+ {
+ type : "unsignednumber",
+ length:2,
+ isnotempty : true,
+ primary : true,
+ },
+ password:{
+ type : "text",
+ length : 16,
+ },
+ hiddenlog:{
+ type : "bool",
+ },
+ };
+ var query = GetCreateQuery(Sequelize,def,"rooms");
+ rooms = pool.define("rooms",query);
+ module.exports.GetRoomInfomation = rooms;
- return result;
+ fs.exists("inited",function(exists){
+ next(null,exists);
+ });
+ },
+ function(exists,next){
+ if(exists)
+ next(true);
+ else
+ fs.open("inited","a",function(err,fd){
+ fs.closeSync(fd);
+ next(null);
+ });
+ },
+ function(next){
+ module.exports.GetProfileColletion.sync({force:true}).done(next);
+ },
+ function(result,next){
+ pool.getQueryInterface().addIndex(
+ module.exports.GetProfileColletion.tableName,
+ ["name_hash"]
+ ).done(next);
+ },
+ function(result,next){
+ module.exports.GetIpBanColletion.sync({force:true}).done(next);
+ },
+ function(result,next){
+ module.exports.GetRoomInfomation.sync({force:true}).done(next);
+ }
+ ],function(err){
+ if(err)
+ callback(null);
+ else
+ 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)
- {
+ option["allowNull"] = true;
if(typeof(def[key].primary) != "undefined" && def[key].primary)
- {
- result += util.format("PRIMARY KEY(%s)",key);
- break;
- }
+ 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";
-}