-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdb.js
158 lines (132 loc) · 5.27 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* 20131015
*/
var sqlite3 = require('sqlite3').verbose();
var fsys = require('fs');
var util = require('util');
const debuglog = util.debuglog('db');
var db ;
var serverStart;
////////////////////////////////////////////////////////////////////////////////
function createDb () {
db = new sqlite3.Database('./chatServer.sqlite3', createTables);
}
////////////////////////////////////////////////////////////////////////////////
function createTables () {
createTableUserInfo ();
}
////////////////////////////////////////////////////////////////////////////////
function createTableUserInfo () {
// 별명, 이름, 비번, 연락처
db.run("CREATE TABLE IF NOT EXISTS UserInfo \
(userid VARCHAR(30), nick VARCHAR(100), passwd VARCHAR(20), PRIMARY KEY(userid) )",
createTableFriendList );
}
////////////////////////////////////////////////////////////////////////////////
function createTableFriendList () {
db.run("CREATE TABLE IF NOT EXISTS FriendList (userid VARCHAR(30), friendid VARCHAR(30), PRIMARY KEY(userid,friendid))",
tableCreted );
}
////////////////////////////////////////////////////////////////////////////////
function tableCreted () {
debuglog("createDb Done!!");
serverStart();
}
////////////////////////////////////////////////////////////////////////////////
exports.authUser = function(userid,passwd, cb) {
debuglog('authUser invoked...');
// test select
var sqlStr = "SELECT count(1) user_exists, nick FROM UserInfo WHERE userid=? AND passwd=?";
debuglog("authUser/sqlStr:"+sqlStr)
db.get(sqlStr,userid,passwd, function(err, row) {
console.log('err: ' + err);
console.log('row.user_exists: '+ row.user_exists);
cb (err, row.user_exists, row.nick);
});
}
////////////////////////////////////////////////////////////////////////////////
exports.checkUserId = function(aryData, cb) {
var userid = aryData[0];
debuglog('checkUserId invoked...');
var sqlStr = "SELECT count(1) userExists FROM UserInfo WHERE userid=?";
//debuglog("checkUserId/sqlStr:"+sqlStr);
db.get(sqlStr,userid, function(err, row) {
console.log('err: ' + err);
console.log('row.userExists: '+ row.userExists);
cb (err, row.userExists);
});
}
////////////////////////////////////////////////////////////////////////////////
exports.registerUser = function(aryData, cb) {
//"userid|passwd|nick"
var userid = aryData[0];
var passwd = aryData[1];
var nick = aryData[2];
debuglog("userid: " + userid);
debuglog("nick : " + nick);
debuglog("passwd: " + passwd);
db.run("INSERT INTO UserInfo VALUES (?,?,?)", userid, nick, passwd, cb);
}
////////////////////////////////////////////////////////////////////////////////
exports.validateFriendId = function (friendid, cb) {
debuglog('validateFriendId invoked...');
var sqlStr = "SELECT count(1) cnt, nick FROM UserInfo WHERE userid==?";
db.get(sqlStr,friendid, function(err, row) {
debuglog('err: ' + err);
debuglog('row.cnt: '+ row.cnt);
debuglog('row.nick: '+ row.nick);
cb (err, row.cnt, row.nick);
});
}
////////////////////////////////////////////////////////////////////////////////
exports.removeFriendId = function(userid, friendid, cb) {
db.run("DELETE FROM FriendList WHERE userid=? AND friendid=?", userid, friendid, cb);
}
////////////////////////////////////////////////////////////////////////////////
exports.addMyFriend = function (userid, friendid, cb) {
db.run("INSERT INTO FriendList(userid,friendid) VALUES (?,?)", userid, friendid, cb);
}
////////////////////////////////////////////////////////////////////////////////
exports.getMyFriendCount = function(userid, cb) {
debuglog('getMyFriendCount invoked...');
var sqlStr = "SELECT count(friendid) totalCnt FROM FriendList WHERE userid==?";
debuglog("getMyFriendCount/sqlStr:"+sqlStr);
db.get(sqlStr,userid, function(err, row) {
console.log('err: ' + err);
console.log('row.totalCnt: '+ row.totalCnt);
cb (err, row.totalCnt);
});
}
////////////////////////////////////////////////////////////////////////////////
exports.getMyFriendList = function (userid, cb, totalCnt) {
debuglog('getMyFriendList invoked...');
var sqlStr = "SELECT a.friendid, b.nick FROM FriendList A,userinfo B WHERE A.userid='"+userid+"' and B.userid = A.friendid";
debuglog("getMyFriendList/sqlStr:"+sqlStr);
db.all(sqlStr, function(err, rows) {
console.log('err: ' + err);
rows.forEach(function (row) {
console.log(row);
cb (row, totalCnt);
});
//closeDb();
});
}
////////////////////////////////////////////////////////////////////////////////
exports.checkAndCreateDB = function (cb) {
serverStart = cb;
fsys.exists('./chatServer.sqlite3', function (exists) {
//debuglog(exists ? "db exists!!" : "db not exists");
if(!exists) {
debuglog("createDb call...");
createDb();
} else {
debuglog("1.db exists...");
db = new sqlite3.Database('./chatServer.sqlite3', serverStart );
}
});
}
////////////////////////////////////////////////////////////////////////////////
function closeDb () {
console.log("closeDb");
db.close();
}